Repository: kudu
Updated Branches:
  refs/heads/master 030630ac0 -> 5360339df


KUDU-16: python client support for limits

This patch allows python client users to impose a limit on the number of
rows returned by a single scanner.

Change-Id: I49e24ff45c7dd359b400e4270fe7573305075b39
Reviewed-on: http://gerrit.cloudera.org:8080/9924
Reviewed-by: Todd Lipcon <t...@apache.org>
Tested-by: Kudu Jenkins


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

Branch: refs/heads/master
Commit: 5360339df6d2e6e0a143dfab2ea971c91d3af143
Parents: 030630a
Author: Andrew Wong <anjuw...@g.ucla.edu>
Authored: Tue Apr 3 23:50:40 2018 -0700
Committer: Grant Henke <granthe...@apache.org>
Committed: Thu Apr 5 14:04:24 2018 +0000

----------------------------------------------------------------------
 python/kudu/client.pyx            | 17 +++++++++++++++++
 python/kudu/libkudu_client.pxd    |  1 +
 python/kudu/tests/test_scanner.py |  9 +++++++++
 3 files changed, 27 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/5360339d/python/kudu/client.pyx
----------------------------------------------------------------------
diff --git a/python/kudu/client.pyx b/python/kudu/client.pyx
index ab43851..2be2c83 100644
--- a/python/kudu/client.pyx
+++ b/python/kudu/client.pyx
@@ -1623,6 +1623,23 @@ cdef class Scanner:
 
         return self
 
+    def set_limit(self, limit):
+        """
+        Set a limit on the number of rows returned by this scanner.
+        Must be a positive value.
+
+        Parameters
+        ----------
+        limit : the maximum number of rows to return
+
+        Returns
+        -------
+        self : Scanner
+        """
+        if limit <= 0:
+            raise ValueError("Limit must be positive.")
+        check_status(self.scanner.SetLimit(<int64_t> limit))
+
     def set_fault_tolerant(self):
         """
         Makes the underlying KuduScanner fault tolerant.

http://git-wip-us.apache.org/repos/asf/kudu/blob/5360339d/python/kudu/libkudu_client.pxd
----------------------------------------------------------------------
diff --git a/python/kudu/libkudu_client.pxd b/python/kudu/libkudu_client.pxd
index e2bc90d..83cda7d 100644
--- a/python/kudu/libkudu_client.pxd
+++ b/python/kudu/libkudu_client.pxd
@@ -678,6 +678,7 @@ cdef extern from "kudu/client/client.h" namespace 
"kudu::client" nogil:
         Status SetFaultTolerant()
         Status AddLowerBound(const KuduPartialRow& key)
         Status AddExclusiveUpperBound(const KuduPartialRow& key)
+        Status SetLimit(int64_t limit)
         Status KeepAlive()
         Status GetCurrentServer(KuduTabletServer** server)
 

http://git-wip-us.apache.org/repos/asf/kudu/blob/5360339d/python/kudu/tests/test_scanner.py
----------------------------------------------------------------------
diff --git a/python/kudu/tests/test_scanner.py 
b/python/kudu/tests/test_scanner.py
index 23d5462..83e1193 100644
--- a/python/kudu/tests/test_scanner.py
+++ b/python/kudu/tests/test_scanner.py
@@ -59,6 +59,15 @@ class TestScanner(TestScanBase):
         tuples = _read_predicates(preds)
         self.assertEqual(sorted(tuples), self.tuples[20:50])
 
+    def test_scan_limit(self):
+        # Set limits both below and above the max number of rows.
+        limits = [self.nrows - 1, self.nrows, self.nrows + 1]
+        for limit in limits:
+            scanner = self.table.scanner()
+            scanner.set_limit(limit)
+            tuples = scanner.read_all_tuples()
+            self.assertEqual(len(tuples), min(limit, self.nrows))
+
     def test_scan_rows_string_predicate_and_projection(self):
         scanner = self.table.scanner()
         scanner.set_projected_column_names(['key', 'string_val'])

Reply via email to