kudu-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ha...@apache.org
Subject kudu git commit: Add a column renaming tool
Date Thu, 10 May 2018 23:40:53 GMT
Repository: kudu
Updated Branches:
  refs/heads/master 3855328b7 -> c18e7e1a6


Add a column renaming tool

This commit introduces a tool to rename a table's column. Similar to
table renaming tool, when HMS integration feature is enabled, users can
use this tool to rename table's column that have hive incompatible
names. In order to allow these tables to be upgraded.

Change-Id: Ibe98ce52d6865fa61c0903b38cfac3e86fc05a66
Reviewed-on: http://gerrit.cloudera.org:8080/10368
Tested-by: Hao Hao <hao.hao@cloudera.com>
Reviewed-by: Dan Burkert <danburkert@apache.org>


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

Branch: refs/heads/master
Commit: c18e7e1a6c811fc432ecc5f0bce66afc91bf4cce
Parents: 3855328
Author: hahao <hao.hao@cloudera.com>
Authored: Wed May 9 18:17:08 2018 -0700
Committer: Hao Hao <hao.hao@cloudera.com>
Committed: Thu May 10 23:24:02 2018 +0000

----------------------------------------------------------------------
 src/kudu/tools/kudu-tool-test.cc    | 40 +++++++++++++++++++++++++++++++-
 src/kudu/tools/tool_action_table.cc | 32 +++++++++++++++++++++----
 2 files changed, 67 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kudu/blob/c18e7e1a/src/kudu/tools/kudu-tool-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/tools/kudu-tool-test.cc b/src/kudu/tools/kudu-tool-test.cc
index cc4d08c..6e8aeae 100644
--- a/src/kudu/tools/kudu-tool-test.cc
+++ b/src/kudu/tools/kudu-tool-test.cc
@@ -1847,7 +1847,7 @@ TEST_F(ToolTest, TestRenameTable) {
 
   string master_addr = cluster_->master()->bound_rpc_addr().ToString();
   string out;
-  NO_FATALS(RunActionStdoutNone(Substitute("table rename $0 $1 $2",
+  NO_FATALS(RunActionStdoutNone(Substitute("table rename_table $0 $1 $2",
                                            master_addr, kTableName,
                                            kNewTableName)));
   shared_ptr<KuduClient> client;
@@ -1858,6 +1858,44 @@ TEST_F(ToolTest, TestRenameTable) {
   ASSERT_OK(client->OpenTable(kNewTableName, &table));
 }
 
+TEST_F(ToolTest, TestRenameColumn) {
+  NO_FATALS(StartExternalMiniCluster());
+  const string& kTableName = "table";
+  const string& kColumnName = "col.0";
+  const string& kNewColumnName = "col_0";
+
+  KuduSchemaBuilder schema_builder;
+  schema_builder.AddColumn("key")
+      ->Type(client::KuduColumnSchema::INT32)
+      ->NotNull()
+      ->PrimaryKey();
+  schema_builder.AddColumn(kColumnName)
+      ->Type(client::KuduColumnSchema::INT32)
+      ->NotNull();
+  KuduSchema schema;
+  ASSERT_OK(schema_builder.Build(&schema));
+
+  // Create the table.
+  TestWorkload workload(cluster_.get());
+  workload.set_table_name(kTableName);
+  workload.set_schema(schema);
+  workload.set_num_replicas(1);
+  workload.Setup();
+
+  string master_addr = cluster_->master()->bound_rpc_addr().ToString();
+  string out;
+  NO_FATALS(RunActionStdoutNone(Substitute("table rename_column $0 $1 $2 $3",
+                                           master_addr, kTableName,
+                                           kColumnName, kNewColumnName)));
+  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_STR_CONTAINS(table->schema().ToString(), kNewColumnName);
+}
+
 // This test is parameterized on the serialization mode and Kerberos.
 class ControlShellToolTest :
     public ToolTest,

http://git-wip-us.apache.org/repos/asf/kudu/blob/c18e7e1a/src/kudu/tools/tool_action_table.cc
----------------------------------------------------------------------
diff --git a/src/kudu/tools/tool_action_table.cc b/src/kudu/tools/tool_action_table.cc
index b9f5308..6fd0037 100644
--- a/src/kudu/tools/tool_action_table.cc
+++ b/src/kudu/tools/tool_action_table.cc
@@ -15,8 +15,6 @@
 // specific language governing permissions and limitations
 // under the License.
 
-#include "kudu/tools/tool_action.h"
-
 #include <iostream>
 #include <memory>
 #include <string>
@@ -28,11 +26,13 @@
 
 #include "kudu/client/client.h"
 #include "kudu/client/replica_controller-internal.h"
+#include "kudu/client/schema.h"
 #include "kudu/client/shared_ptr.h"
 #include "kudu/gutil/map-util.h"
 #include "kudu/gutil/stl_util.h"
 #include "kudu/gutil/strings/split.h"
 #include "kudu/gutil/strings/substitute.h"
+#include "kudu/tools/tool_action.h"
 #include "kudu/tools/tool_action_common.h"
 #include "kudu/util/status.h"
 
@@ -103,6 +103,8 @@ namespace {
 
 const char* const kTableNameArg = "table_name";
 const char* const kNewTableNameArg = "new_table_name";
+const char* const kColumnNameArg = "column_name";
+const char* const kNewColumnNameArg = "new_column_name";
 
 Status CreateKuduClient(const RunnerContext& context,
                         client::sp::shared_ptr<KuduClient>* client) {
@@ -129,7 +131,19 @@ Status RenameTable(const RunnerContext& context) {
   RETURN_NOT_OK(CreateKuduClient(context, &client));
   unique_ptr<KuduTableAlterer> alterer(client->NewTableAlterer(table_name));
   return alterer->RenameTo(new_table_name)
-      ->Alter();
+                ->Alter();
+}
+
+Status RenameColumn(const RunnerContext& context) {
+  const string& table_name = FindOrDie(context.required_args, kTableNameArg);
+  const string& column_name = FindOrDie(context.required_args, kColumnNameArg);
+  const string& new_column_name = FindOrDie(context.required_args, kNewColumnNameArg);
+
+  client::sp::shared_ptr<KuduClient> client;
+  RETURN_NOT_OK(CreateKuduClient(context, &client));
+  unique_ptr<KuduTableAlterer> alterer(client->NewTableAlterer(table_name));
+  alterer->AlterColumn(column_name)->RenameTo(new_column_name);
+  return alterer->Alter();
 }
 
 Status ListTables(const RunnerContext& context) {
@@ -149,13 +163,22 @@ unique_ptr<Mode> BuildTableMode() {
       .Build();
 
   unique_ptr<Action> rename_table =
-      ActionBuilder("rename", &RenameTable)
+      ActionBuilder("rename_table", &RenameTable)
       .Description("Rename a table")
       .AddRequiredParameter({ kMasterAddressesArg, kMasterAddressesArgDesc })
       .AddRequiredParameter({ kTableNameArg, "Name of the table to rename" })
       .AddRequiredParameter({ kNewTableNameArg, "New table name" })
       .Build();
 
+  unique_ptr<Action> rename_column =
+      ActionBuilder("rename_column", &RenameColumn)
+          .Description("Rename a column")
+          .AddRequiredParameter({ kMasterAddressesArg, kMasterAddressesArgDesc })
+          .AddRequiredParameter({ kTableNameArg, "Name of the table to alter" })
+          .AddRequiredParameter({ kColumnNameArg, "Name of the table column to rename" })
+          .AddRequiredParameter({ kNewColumnNameArg, "New column name" })
+          .Build();
+
   unique_ptr<Action> list_tables =
       ActionBuilder("list", &ListTables)
       .Description("List all tables")
@@ -167,6 +190,7 @@ unique_ptr<Mode> BuildTableMode() {
       .Description("Operate on Kudu tables")
       .AddAction(std::move(delete_table))
       .AddAction(std::move(rename_table))
+      .AddAction(std::move(rename_column))
       .AddAction(std::move(list_tables))
       .Build();
 }


Mime
View raw message