This is an automated email from the ASF dual-hosted git repository.

granthenke pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/kudu.git

commit 04d3ba7a0a60817d8142b70a222a9ed3b6724f6f
Author: Grant Henke <[email protected]>
AuthorDate: Thu May 6 14:26:47 2021 -0500

    [python] KUDU-3164: Add Python table comment support
    
    This patch adds table comment support to the Python client.
    
    Change-Id: I533572fc5157a6d3beb6d1005d9f351f674c3340
    Reviewed-on: http://gerrit.cloudera.org:8080/17405
    Tested-by: Kudu Jenkins
    Reviewed-by: Attila Bukor <[email protected]>
---
 python/kudu/client.pyx           | 24 +++++++++++++++++++++++-
 python/kudu/libkudu_client.pxd   |  3 +++
 python/kudu/tests/test_client.py | 31 +++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/python/kudu/client.pyx b/python/kudu/client.pyx
index 777ee99..f26fafc 100644
--- a/python/kudu/client.pyx
+++ b/python/kudu/client.pyx
@@ -363,7 +363,7 @@ cdef class Client:
         return from_hybridtime(self.cp.GetLatestObservedTimestamp())
 
     def create_table(self, table_name, Schema schema, partitioning,
-            n_replicas=None, owner=None):
+            n_replicas=None, owner=None, comment=None):
         """
         Creates a new Kudu table from the passed Schema and options.
 
@@ -388,6 +388,8 @@ cdef class Client:
                 c.num_replicas(n_replicas)
             if owner:
                 c.set_owner(tobytes(owner))
+            if comment:
+                c.set_comment(tobytes(comment))
             s = c.Create()
             check_status(s)
         finally:
@@ -840,6 +842,11 @@ cdef class Table:
         def __get__(self):
             return frombytes(self.ptr().owner())
 
+    property comment:
+        """Comment on the table."""
+        def __get__(self):
+            return frombytes(self.ptr().comment())
+
     def rename(self, new_name):
         raise NotImplementedError
 
@@ -3256,6 +3263,21 @@ cdef class TableAlterer:
         self._alterer.SetOwner(tobytes(new_owner))
         return self
 
+    def set_comment(self, new_comment):
+        """
+        Set the comment on the table.
+
+        Parameters
+        ----------
+        new_comment : string
+
+        Returns
+        -------
+        self : TableAlterer
+        """
+        self._alterer.SetComment(tobytes(new_comment))
+        return self
+
     def alter(self):
         """
         Alter table. Returns a new table object upon completion of the alter.
diff --git a/python/kudu/libkudu_client.pxd b/python/kudu/libkudu_client.pxd
index dc3b782..aa91cd7 100644
--- a/python/kudu/libkudu_client.pxd
+++ b/python/kudu/libkudu_client.pxd
@@ -605,6 +605,7 @@ cdef extern from "kudu/client/client.h" namespace 
"kudu::client" nogil:
         KuduTableCreator& num_replicas(int n_replicas)
         KuduTableCreator& wait(c_bool wait)
         KuduTableCreator& set_owner(const string& owner)
+        KuduTableCreator& set_comment(const string& comment)
 
         Status Create()
 
@@ -622,6 +623,7 @@ cdef extern from "kudu/client/client.h" namespace 
"kudu::client" nogil:
                                              RangePartitionBound 
lower_bound_type,
                                              RangePartitionBound 
upper_bound_type)
         KuduTableAlterer& SetOwner(const string& new_owner)
+        KuduTableAlterer& SetComment(const string& new_comment)
 
         KuduTableAlterer& wait(c_bool wait)
 
@@ -634,6 +636,7 @@ cdef extern from "kudu/client/client.h" namespace 
"kudu::client" nogil:
         string& name()
         string& id()
         string& owner()
+        string& comment()
         KuduSchema& schema()
         int num_replicas()
 
diff --git a/python/kudu/tests/test_client.py b/python/kudu/tests/test_client.py
index b7f4ffe..6305285 100755
--- a/python/kudu/tests/test_client.py
+++ b/python/kudu/tests/test_client.py
@@ -170,6 +170,22 @@ class TestClient(KuduTestBase, unittest.TestCase):
             except:
                 pass
 
+    def test_create_table_with_different_comment(self):
+        name = 'table_with_different_comment'
+        try:
+            self.client.create_table(
+                name, self.schema,
+                partitioning=Partitioning().add_hash_partitions(['key'], 2),
+                comment='new comment')
+
+            self.assertEqual('new comment', self.client.table(name).comment)
+
+        finally:
+            try:
+                self.client.delete_table(name)
+            except:
+                pass
+
     def test_insert_nonexistent_field(self):
         table = self.client.table(self.ex_table)
         op = table.new_insert()
@@ -390,6 +406,21 @@ class TestClient(KuduTestBase, unittest.TestCase):
         finally:
             self.client.delete_table(name)
 
+    def test_alter_table_change_comment(self):
+        name = 'alter-comment'
+        try:
+            self.client.create_table(name,
+                                     self.schema,
+                                     self.partitioning)
+            table = self.client.table(name)
+            alterer = self.client.new_table_alterer(table)
+            table = alterer.set_comment('change comment').alter()
+            self.assertEqual('change comment', self.client.table(name).comment)
+            with self.assertRaises(TypeError):
+                alterer.set_comment(None).alter()
+        finally:
+            self.client.delete_table(name)
+
     def test_alter_column(self):
         try:
             self.client.create_table('alter-column',

Reply via email to