kudu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdcry...@apache.org
Subject [1/5] incubator-kudu git commit: [python] - Allow to set a lower/upper bound on the scanner
Date Fri, 05 Feb 2016 20:53:42 GMT
Repository: incubator-kudu
Updated Branches:
  refs/heads/branch-0.7.0 7e4a4100a -> a2bb870c3


[python] - Allow to set a lower/upper bound on the scanner

This allows to set a lower/upper band on the scanner pretty much
like the c++ client does.

Change-Id: Ie7943c913cec957015202ae0d8f8a4add0096fc0
Reviewed-on: http://gerrit.cloudera.org:8080/2030
Tested-by: Kudu Jenkins
Reviewed-by: Wes McKinney <wes@cloudera.com>
Reviewed-by: Todd Lipcon <todd@apache.org>
(cherry picked from commit 4294cbe594c7febd6a763ed6e336bc3bf979d6b1)
Reviewed-on: http://gerrit.cloudera.org:8080/2072
Reviewed-by: Jean-Daniel Cryans
Tested-by: Jean-Daniel Cryans


Project: http://git-wip-us.apache.org/repos/asf/incubator-kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-kudu/commit/73e332f2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-kudu/tree/73e332f2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-kudu/diff/73e332f2

Branch: refs/heads/branch-0.7.0
Commit: 73e332f2ac1354a6f5d878fa6137e0a0d735ec15
Parents: 7e4a410
Author: David Alves <david.alves@cloudera.com>
Authored: Wed Feb 3 20:08:03 2016 -0800
Committer: Jean-Daniel Cryans <jdcryans@gerrit.cloudera.org>
Committed: Fri Feb 5 20:51:03 2016 +0000

----------------------------------------------------------------------
 python/kudu/client.pyx            | 55 ++++++++++++++++++++++++++++++++--
 python/kudu/libkudu_client.pxd    |  2 ++
 python/kudu/schema.pxd            |  3 ++
 python/kudu/tests/test_scanner.py | 15 ++++++++++
 4 files changed, 72 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/73e332f2/python/kudu/client.pyx
----------------------------------------------------------------------
diff --git a/python/kudu/client.pyx b/python/kudu/client.pyx
index 30f2dac..05f67ae 100644
--- a/python/kudu/client.pyx
+++ b/python/kudu/client.pyx
@@ -1024,6 +1024,41 @@ cdef class Scanner:
         check_status(self.scanner.SetFaultTolerant())
         return self
 
+    def new_bound(self):
+        """
+        Returns a new instance of a ScanBound (subclass of PartialRow) to be
+        later set with add_lower_bound()/add_exclusive_upper_bound().
+
+        Returns
+        -------
+        bound : ScanBound
+        """
+        return ScanBound(self.table)
+
+    def add_lower_bound(self, ScanBound bound):
+        """
+        Sets the (inclusive) lower bound of the scan.
+        Returns a reference to itself to facilitate chaining.
+
+        Returns
+        -------
+        self : Scanner
+        """
+        check_status(self.scanner.AddLowerBound(deref(bound.row)))
+        return self
+
+    def add_exclusive_upper_bound(self, ScanBound bound):
+        """
+        Sets the (exclusive) upper bound of the scan.
+        Returns a reference to itself to facilitate chaining.
+
+        Returns
+        -------
+        self : Scanner
+        """
+        check_status(self.scanner.AddExclusiveUpperBound(deref(bound.row)))
+        return self
+
     def open(self):
         """
         Returns a reference to itself to facilitate chaining
@@ -1104,16 +1139,14 @@ cdef class KuduError:
         return "KuduError('%s')" % (self.error.status().ToString())
 
 
-cdef class WriteOperation:
+cdef class PartialRow:
     cdef:
         Table table
         KuduPartialRow* row
-        bint applied
 
     def __cinit__(self, Table table):
         # This gets called before any subclass cinit methods
         self.table = table
-        self.applied = 0
 
     def __setitem__(self, key, value):
         if isinstance(key, basestring):
@@ -1167,6 +1200,22 @@ cdef class WriteOperation:
     cdef add_to_session(self, Session s):
         pass
 
+cdef class ScanBound(PartialRow):
+    def __cinit__(self, Table table):
+        self.row = self.table.schema.new_row()
+
+    def __dealloc__(self):
+        del self.row
+
+cdef class WriteOperation(PartialRow):
+    cdef:
+        # Whether the WriteOperation has been applied.
+        # Set by subclasses.
+        bint applied
+
+    def __cinit__(self, Table table):
+        self.applied = 0
+
 
 cdef class Insert(WriteOperation):
     cdef:

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/73e332f2/python/kudu/libkudu_client.pxd
----------------------------------------------------------------------
diff --git a/python/kudu/libkudu_client.pxd b/python/kudu/libkudu_client.pxd
index 9071666..5405f84 100644
--- a/python/kudu/libkudu_client.pxd
+++ b/python/kudu/libkudu_client.pxd
@@ -596,6 +596,8 @@ cdef extern from "kudu/client/client.h" namespace "kudu::client" nogil:
         Status SetTimeoutMillis(int millis)
         Status SetProjectedColumnNames(const vector[string]& col_names)
         Status SetFaultTolerant()
+        Status AddLowerBound(const KuduPartialRow& key)
+        Status AddExclusiveUpperBound(const KuduPartialRow& key)
 
         string ToString()
 

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/73e332f2/python/kudu/schema.pxd
----------------------------------------------------------------------
diff --git a/python/kudu/schema.pxd b/python/kudu/schema.pxd
index b70f8ad..085aaa0 100644
--- a/python/kudu/schema.pxd
+++ b/python/kudu/schema.pxd
@@ -57,3 +57,6 @@ cdef class Schema:
 
     cdef inline DataType loc_type(self, int i):
         return self.schema.Column(i).type()
+
+    cdef inline KuduPartialRow* new_row(self):
+        return self.schema.NewRow()

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/73e332f2/python/kudu/tests/test_scanner.py
----------------------------------------------------------------------
diff --git a/python/kudu/tests/test_scanner.py b/python/kudu/tests/test_scanner.py
index 70a054b..63aa3ac 100644
--- a/python/kudu/tests/test_scanner.py
+++ b/python/kudu/tests/test_scanner.py
@@ -95,6 +95,21 @@ class TestScanner(KuduTestBase, unittest.TestCase):
 
         self.assertEqual(sorted(tuples), [(20, 'hello_20'), (22, 'hello_22')])
 
+    def test_scan_with_bounds(self):
+        scanner = self.table.scanner()
+        scanner.set_fault_tolerant()
+        lower_bound = scanner.new_bound()
+        lower_bound['key'] = 50
+        scanner.add_lower_bound(lower_bound)
+        upper_bound = scanner.new_bound()
+        upper_bound['key'] = 55
+        scanner.add_exclusive_upper_bound(upper_bound)
+        scanner.open()
+
+        tuples = scanner.read_all_tuples()
+
+        self.assertEqual(sorted(tuples), self.tuples[50:55])
+
     def test_scan_invalid_predicates(self):
         scanner = self.table.scanner()
         sv = self.table['string_val']


Mime
View raw message