You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by al...@apache.org on 2023/03/19 16:47:52 UTC

[kudu] branch master updated: [Python] Add support for comment in ColumnSchema

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

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


The following commit(s) were added to refs/heads/master by this push:
     new e1f75753c [Python] Add support for comment in ColumnSchema
e1f75753c is described below

commit e1f75753c4a45e640b5de398dd8a5584036ecece
Author: Marton Greber <gr...@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 <al...@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 510bc3483..f7e1950db 100644
--- a/python/kudu/libkudu_client.pxd
+++ b/python/kudu/libkudu_client.pxd
@@ -174,6 +174,7 @@ cdef extern from "kudu/client/schema.h" namespace "kudu::client" nogil:
         c_bool is_nullable()
         DataType type()
         KuduColumnTypeAttributes type_attributes()
+        string& comment()
 
         c_bool Equals(KuduColumnSchema& other)
         void CopyFrom(KuduColumnSchema& other)
@@ -214,6 +215,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 ee1265e50..739dff1f9 100644
--- a/python/kudu/schema.pyx
+++ b/python/kudu/schema.pyx
@@ -252,6 +252,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
@@ -481,6 +485,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.
@@ -534,7 +553,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.
@@ -567,6 +586,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
         --------
@@ -605,6 +626,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 0000d6210..be46ef4bb 100755
--- a/python/kudu/tests/test_client.py
+++ b/python/kudu/tests/test_client.py
@@ -565,6 +565,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')
 
@@ -748,8 +756,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 42a5d5aa3..b3ba26b2c 100644
--- a/python/kudu/tests/test_schema.py
+++ b/python/kudu/tests/test_schema.py
@@ -291,6 +291,21 @@ class TestSchema(unittest.TestCase):
         assert schema[3].nullable
         assert not schema[4].nullable
 
+    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)