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

laiyingchun pushed a commit to branch branch-1.17.x
in repository https://gitbox.apache.org/repos/asf/kudu.git

commit 27e85acf735944be833ce810dc3bb0dc0796a503
Author: Marton Greber <greber...@gmail.com>
AuthorDate: Sat Mar 4 10:08:59 2023 +0000

    [Python] Add support for comment in ColumnSchema
    
    This change adds support for column comments in the Python client.
    It is a followup change to: e2870925f9a07da43658447248a4e64acde398b4.
    
    Change-Id: I28a9f650bd1bf686244070151419d0a93b9a499c
    Reviewed-on: http://gerrit.cloudera.org:8080/19581
    Tested-by: Kudu Jenkins
    Reviewed-by: Alexey Serbin <ale...@apache.org>
    Reviewed-on: http://gerrit.cloudera.org:8080/19694
    Reviewed-by: Marton Greber <greber...@gmail.com>
    Reviewed-by: Yingchun Lai <laiyingc...@apache.org>
---
 python/kudu/libkudu_client.pxd   |  2 ++
 python/kudu/schema.pyx           | 26 +++++++++++++++++++++++++-
 python/kudu/tests/common.py      |  2 +-
 python/kudu/tests/test_client.py | 13 +++++++++++--
 python/kudu/tests/test_schema.py | 15 +++++++++++++++
 5 files changed, 54 insertions(+), 4 deletions(-)

diff --git a/python/kudu/libkudu_client.pxd b/python/kudu/libkudu_client.pxd
index 4766dc6c2..671aca067 100644
--- a/python/kudu/libkudu_client.pxd
+++ b/python/kudu/libkudu_client.pxd
@@ -175,6 +175,7 @@ cdef extern from "kudu/client/schema.h" namespace 
"kudu::client" nogil:
         DataType type()
         c_bool is_immutable()
         KuduColumnTypeAttributes type_attributes()
+        string& comment()
 
         c_bool Equals(KuduColumnSchema& other)
         void CopyFrom(KuduColumnSchema& other)
@@ -217,6 +218,7 @@ cdef extern from "kudu/client/schema.h" namespace 
"kudu::client" nogil:
          KuduColumnSpec* Length(uint16_t length);
 
          KuduColumnSpec* RenameTo(const string& new_name)
+         KuduColumnSpec* Comment(const string& comment)
 
 
     cdef cppclass KuduSchemaBuilder:
diff --git a/python/kudu/schema.pyx b/python/kudu/schema.pyx
index d19e6d3dd..00394e2b4 100644
--- a/python/kudu/schema.pyx
+++ b/python/kudu/schema.pyx
@@ -256,6 +256,10 @@ cdef class ColumnSchema:
             result.type_attributes = new 
KuduColumnTypeAttributes(self.schema.type_attributes())
             return result
 
+    property comment:
+        def __get__(self):
+            return frombytes(self.schema.comment())
+
     def equals(self, other):
         if not isinstance(other, ColumnSchema):
             return False
@@ -503,6 +507,21 @@ cdef class ColumnSpec:
         self.spec.RenameTo(tobytes(new_name))
         return self
 
+    def comment(self, comment):
+        """
+        The comment for the column.
+
+        Parameters
+        ----------
+        comment : str
+
+        Retruns
+        -------
+        self : ColumnSpec
+        """
+        self.spec.Comment(tobytes(comment))
+        return self
+
     def block_size(self, block_size):
         """
         Set the target block size for the column.
@@ -556,7 +575,7 @@ cdef class SchemaBuilder:
 
     def add_column(self, name, type_=None, nullable=None, compression=None, 
encoding=None,
                    primary_key=False, non_unique_primary_key=False, 
block_size=None, default=None,
-                   precision=None, scale=None, length=None):
+                   precision=None, scale=None, length=None, comment=None):
         """
         Add a new column to the schema. Returns a ColumnSpec object for further
         configuration and use in a fluid programming style.
@@ -589,6 +608,8 @@ cdef class SchemaBuilder:
           Use this scale for the decimal column
         length : int
           Use this length for the varchar column
+        comment : string, default ''
+          Comment of the column schema
 
         Examples
         --------
@@ -627,6 +648,9 @@ cdef class SchemaBuilder:
         if length is not None:
             result.length(length)
 
+        if comment is not None:
+            result.comment(comment)
+
         if primary_key:
             result.primary_key()
 
diff --git a/python/kudu/tests/common.py b/python/kudu/tests/common.py
index 3841edab7..4087832b6 100644
--- a/python/kudu/tests/common.py
+++ b/python/kudu/tests/common.py
@@ -126,7 +126,7 @@ class KuduTestBase(object):
     @classmethod
     def example_schema(cls):
         builder = kudu.schema_builder()
-        builder.add_column('key', kudu.int32, nullable=False)
+        builder.add_column('key', kudu.int32, nullable=False, 
comment='key_comment')
         builder.add_column('int_val', kudu.int32)
         builder.add_column('string_val', kudu.string, default=None)
         builder.add_column('unixtime_micros_val', kudu.unixtime_micros)
diff --git a/python/kudu/tests/test_client.py b/python/kudu/tests/test_client.py
index 7b78fa888..ce76a87d3 100755
--- a/python/kudu/tests/test_client.py
+++ b/python/kudu/tests/test_client.py
@@ -661,6 +661,14 @@ class TestClient(KuduTestBase, unittest.TestCase):
             # Confirm column rename
             col = table['string_val_renamed']
 
+            # Check if existing comment can be altered, and new comment can be 
specified
+            alterer = self.client.new_table_alterer(table)
+            alterer.alter_column('key').comment('new_key_comment')
+            alterer.alter_column('int_val').comment('int_val_comment')
+            table = alterer.alter()
+
+            assert table['key'].spec.comment == 'new_key_comment'
+            assert table['int_val'].spec.comment == 'int_val_comment'
         finally:
             self.client.delete_table('alter-column')
 
@@ -844,8 +852,9 @@ class TestClient(KuduTestBase, unittest.TestCase):
             alterer.alter_column(col_name).compression('none')
             alterer.alter()
 
-            # TODO(martongreber): once column comments are added to the python 
client
-            # check whether the auto-incrementing column's comment can be 
altered.
+            alterer = self.client.new_table_alterer(table)
+            alterer.alter_column(col_name).comment('new_comment')
+            alterer.alter()
 
         finally:
             try:
diff --git a/python/kudu/tests/test_schema.py b/python/kudu/tests/test_schema.py
index f5045fd33..069201f64 100644
--- a/python/kudu/tests/test_schema.py
+++ b/python/kudu/tests/test_schema.py
@@ -305,6 +305,21 @@ class TestSchema(unittest.TestCase):
         assert schema[1].mutable
         assert not schema[2].mutable
 
+    def test_column_comment(self):
+        comment = "test_comment"
+        builder = kudu.schema_builder()
+        (builder.add_column('key', 'int64', nullable=False)
+         .primary_key()
+         .comment(comment))
+
+        builder.add_column('data1', 'double').nullable(True)
+        schema = builder.build()
+        assert isinstance(schema[0].comment, str)
+        assert len(schema[0].comment) > 0
+        assert schema[0].comment == comment
+        assert isinstance(schema[1].comment, str)
+        assert len(schema[1].comment) == 0
+
     def test_auto_incrementing_column_name(self):
         name = Schema.get_auto_incrementing_column_name()
         assert isinstance(name, str)

Reply via email to