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

commit 5f845189ef48e143e27641de86ae71d423464af2
Author: Andrew Wong <[email protected]>
AuthorDate: Wed Jun 16 16:42:08 2021 -0700

    [tools] tooling to set/clear table comments
    
    This patch introduces the 'kudu table set_comment' and 'kudu table
    clear_comment' tools to allow users to alter table comments. I found
    this handy in testing out table comments on a real cluster.
    
    Change-Id: I0b8ea0ce05c642651eeadc1cb0cc654c5302921c
    Reviewed-on: http://gerrit.cloudera.org:8080/17600
    Tested-by: Kudu Jenkins
    Reviewed-by: Grant Henke <[email protected]>
---
 src/kudu/tools/kudu-tool-test.cc    | 48 +++++++++++++++++++++++++++++++++++++
 src/kudu/tools/tool_action_table.cc | 34 ++++++++++++++++++++++++++
 2 files changed, 82 insertions(+)

diff --git a/src/kudu/tools/kudu-tool-test.cc b/src/kudu/tools/kudu-tool-test.cc
index 45ef158..6843c9b 100644
--- a/src/kudu/tools/kudu-tool-test.cc
+++ b/src/kudu/tools/kudu-tool-test.cc
@@ -1177,6 +1177,7 @@ TEST_F(ToolTest, TestModeHelp) {
     const string kCmd = "table";
     const vector<string> kTableModeRegexes = {
         "add_range_partition.*Add a range partition for table",
+        "clear_comment.*Clear the comment for a table",
         "column_remove_default.*Remove write_default value for a column",
         "column_set_block_size.*Set block size for a column",
         "column_set_compression.*Set compression type for a column",
@@ -1195,6 +1196,7 @@ TEST_F(ToolTest, TestModeHelp) {
         "rename_column.*Rename a column",
         "rename_table.*Rename a table",
         "scan.*Scan rows from a table",
+        "set_comment.*Set the comment for a table",
         "set_extra_config.*Change a extra configuration value on a table",
         "set_limit.*Set the write limit for a table",
         "statistics.*Get table statistics",
@@ -1202,6 +1204,7 @@ TEST_F(ToolTest, TestModeHelp) {
     NO_FATALS(RunTestHelp(kCmd, kTableModeRegexes));
     NO_FATALS(RunTestHelpRpcFlags(kCmd,
         { "add_range_partition",
+          "clear_comment",
           "column_remove_default",
           "column_set_block_size",
           "column_set_compression",
@@ -1220,6 +1223,7 @@ TEST_F(ToolTest, TestModeHelp) {
           "rename_column",
           "rename_table",
           "scan",
+          "set_comment",
           "set_extra_config",
           "statistics",
         }));
@@ -4062,6 +4066,50 @@ TEST_F(ToolTest, TestAlterColumn) {
   }
 }
 
+TEST_F(ToolTest, TestTableComment) {
+  NO_FATALS(StartExternalMiniCluster());
+  const string& kTableName = "kudu.test_alter_comment";
+  // Create the table.
+  TestWorkload workload(cluster_.get());
+  workload.set_table_name(kTableName);
+  workload.set_num_replicas(1);
+  workload.Setup();
+
+  string master_addr = cluster_->master()->bound_rpc_addr().ToString();
+  // Check the table comment starts out empty.
+  shared_ptr<KuduClient> client;
+  ASSERT_OK(KuduClientBuilder()
+                .add_master_server_addr(master_addr)
+                .Build(&client));
+  shared_ptr<KuduTable> table;
+  ASSERT_OK(client->OpenTable(kTableName, &table));
+  ASSERT_TRUE(table->comment().empty()) << table->comment();
+
+  ObjectIdGenerator generator;
+  const auto table_comment = generator.Next();
+  NO_FATALS(RunActionStdoutNone(Substitute("table set_comment $0 $1 $2",
+                                           master_addr,
+                                           kTableName,
+                                           table_comment)));
+  ASSERT_OK(client->OpenTable(kTableName, &table));
+  ASSERT_EQ(table_comment, table->comment());
+
+  // Make attempt to "remove" the comment by replacing it with quotes. This
+  // doesn't actually clear the comment.
+  NO_FATALS(RunActionStdoutNone(Substitute("table set_comment $0 $1 \"\"",
+                                           master_addr,
+                                           kTableName)));
+  ASSERT_OK(client->OpenTable(kTableName, &table));
+  ASSERT_EQ("\"\"", table->comment());
+
+  // Using the 'clear_comment' command, we can remove it.
+  NO_FATALS(RunActionStdoutNone(Substitute("table clear_comment $0 $1",
+                                           master_addr,
+                                           kTableName)));
+  ASSERT_OK(client->OpenTable(kTableName, &table));
+  ASSERT_TRUE(table->comment().empty()) << table->comment();
+}
+
 TEST_F(ToolTest, TestColumnSetDefault) {
   NO_FATALS(StartExternalMiniCluster());
   constexpr const char* const kTableName = "kudu.table.set.default";
diff --git a/src/kudu/tools/tool_action_table.cc 
b/src/kudu/tools/tool_action_table.cc
index 005157d..59fbf83 100644
--- a/src/kudu/tools/tool_action_table.cc
+++ b/src/kudu/tools/tool_action_table.cc
@@ -904,6 +904,26 @@ Status ColumnSetBlockSize(const RunnerContext& context) {
   return alterer->Alter();
 }
 
+Status ClearComment(const RunnerContext& context) {
+  const string& table_name = FindOrDie(context.required_args, kTableNameArg);
+  client::sp::shared_ptr<KuduClient> client;
+  RETURN_NOT_OK(CreateKuduClient(context, &client));
+  unique_ptr<KuduTableAlterer> alterer(client->NewTableAlterer(table_name));
+  alterer->SetComment("");
+  return alterer->Alter();
+}
+
+Status SetComment(const RunnerContext& context) {
+  const string& table_name = FindOrDie(context.required_args, kTableNameArg);
+  const string& table_comment = FindOrDie(context.required_args, 
kColumnCommentArg);
+
+  client::sp::shared_ptr<KuduClient> client;
+  RETURN_NOT_OK(CreateKuduClient(context, &client));
+  unique_ptr<KuduTableAlterer> alterer(client->NewTableAlterer(table_name));
+  alterer->SetComment(table_comment);
+  return alterer->Alter();
+}
+
 Status ColumnSetComment(const RunnerContext& context) {
   const string& table_name = FindOrDie(context.required_args, kTableNameArg);
   const string& column_name = FindOrDie(context.required_args, kColumnNameArg);
@@ -1426,6 +1446,18 @@ unique_ptr<Mode> BuildTableMode() {
       .AddRequiredParameter({ kColumnNameArg, "Name of the table column to 
delete" })
       .Build();
 
+  unique_ptr<Action> set_comment =
+      ClusterActionBuilder("set_comment", &SetComment)
+      .Description("Set the comment for a table")
+      .AddRequiredParameter({ kTableNameArg, "Name of the table to alter" })
+      .AddRequiredParameter({ kColumnCommentArg, "Comment of the table" })
+      .Build();
+
+  unique_ptr<Action> clear_comment =
+      ClusterActionBuilder("clear_comment", &ClearComment)
+      .Description("Clear the comment for a table")
+      .AddRequiredParameter({ kTableNameArg, "Name of the table to alter" })
+      .Build();
 
   unique_ptr<Action> statistics =
       ClusterActionBuilder("statistics", &GetTableStatistics)
@@ -1457,6 +1489,7 @@ unique_ptr<Mode> BuildTableMode() {
       .Description("Operate on Kudu tables")
       .AddMode(BuildSetTableLimitMode())
       .AddAction(std::move(add_range_partition))
+      .AddAction(std::move(clear_comment))
       .AddAction(std::move(column_remove_default))
       .AddAction(std::move(column_set_block_size))
       .AddAction(std::move(column_set_compression))
@@ -1475,6 +1508,7 @@ unique_ptr<Mode> BuildTableMode() {
       .AddAction(std::move(rename_column))
       .AddAction(std::move(rename_table))
       .AddAction(std::move(scan_table))
+      .AddAction(std::move(set_comment))
       .AddAction(std::move(set_extra_config))
       .AddAction(std::move(statistics))
       .Build();

Reply via email to