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

shaofengshi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/main by this push:
     new 12e50e50b [#5383] Add support for rename and update command command 
for Tables in Gravitino CLI (#5391)
12e50e50b is described below

commit 12e50e50bc8fdc758bdbe7f8e92e052c95576f15
Author: Justin Mclean <jus...@classsoftware.com>
AuthorDate: Mon Dec 2 17:50:40 2024 +1100

    [#5383] Add support for rename and update command command for Tables in 
Gravitino CLI (#5391)
    
    ### What changes were proposed in this pull request?
    
    Added support for rename and update command command for Tables in the
    Gravitino CLI.
    
    ### Why are the changes needed?
    
    Expanding the CLI support.
    
    Fix: #5383
    
    ### Does this PR introduce _any_ user-facing change?
    
    No, but adds more commands to the CLI.
    
    ### How was this patch tested?
    
    Tested locally on MySQL, Postgres and Hive databases.
---
 .../apache/gravitino/cli/GravitinoCommandLine.java |  9 +++
 .../apache/gravitino/cli/TestableCommandLine.java  | 24 ++++++
 .../gravitino/cli/commands/UpdateTableComment.java | 94 ++++++++++++++++++++++
 .../gravitino/cli/commands/UpdateTableName.java    | 94 ++++++++++++++++++++++
 .../apache/gravitino/cli/TestTableCommands.java    | 56 +++++++++++++
 5 files changed, 277 insertions(+)

diff --git 
a/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java 
b/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java
index e7a36d016..33eca22b3 100644
--- 
a/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java
+++ 
b/clients/cli/src/main/java/org/apache/gravitino/cli/GravitinoCommandLine.java
@@ -326,6 +326,15 @@ public class GravitinoCommandLine extends 
TestableCommandLine {
       newRemoveTableProperty(url, ignore, metalake, catalog, schema, table, 
property).handle();
     } else if (CommandActions.PROPERTIES.equals(command)) {
       newListTableProperties(url, ignore, metalake, catalog, schema, 
table).handle();
+    } else if (CommandActions.UPDATE.equals(command)) {
+      if (line.hasOption(GravitinoOptions.COMMENT)) {
+        String comment = line.getOptionValue(GravitinoOptions.COMMENT);
+        newUpdateTableComment(url, ignore, metalake, catalog, schema, table, 
comment).handle();
+      }
+      if (line.hasOption(GravitinoOptions.RENAME)) {
+        String newName = line.getOptionValue(GravitinoOptions.RENAME);
+        newUpdateTableName(url, ignore, metalake, catalog, schema, table, 
newName).handle();
+      }
     }
   }
 
diff --git 
a/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java 
b/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java
index 57de13f0a..546c92e33 100644
--- 
a/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java
+++ 
b/clients/cli/src/main/java/org/apache/gravitino/cli/TestableCommandLine.java
@@ -105,6 +105,8 @@ import 
org.apache.gravitino.cli.commands.UpdateFilesetComment;
 import org.apache.gravitino.cli.commands.UpdateFilesetName;
 import org.apache.gravitino.cli.commands.UpdateMetalakeComment;
 import org.apache.gravitino.cli.commands.UpdateMetalakeName;
+import org.apache.gravitino.cli.commands.UpdateTableComment;
+import org.apache.gravitino.cli.commands.UpdateTableName;
 import org.apache.gravitino.cli.commands.UpdateTagComment;
 import org.apache.gravitino.cli.commands.UpdateTagName;
 import org.apache.gravitino.cli.commands.UpdateTopicComment;
@@ -314,6 +316,28 @@ public class TestableCommandLine {
     return new TableDistribution(url, ignore, metalake, catalog, schema, 
table);
   }
 
+  protected UpdateTableComment newUpdateTableComment(
+      String url,
+      boolean ignore,
+      String metalake,
+      String catalog,
+      String schema,
+      String table,
+      String comment) {
+    return new UpdateTableComment(url, ignore, metalake, catalog, schema, 
table, comment);
+  }
+
+  protected UpdateTableName newUpdateTableName(
+      String url,
+      boolean ignore,
+      String metalake,
+      String catalog,
+      String schema,
+      String table,
+      String rename) {
+    return new UpdateTableName(url, ignore, metalake, catalog, schema, table, 
rename);
+  }
+
   protected SetTableProperty newSetTableProperty(
       String url,
       boolean ignore,
diff --git 
a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateTableComment.java
 
b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateTableComment.java
new file mode 100644
index 000000000..ff0cfa760
--- /dev/null
+++ 
b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateTableComment.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.cli.commands;
+
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.cli.ErrorMessages;
+import org.apache.gravitino.client.GravitinoClient;
+import org.apache.gravitino.exceptions.NoSuchCatalogException;
+import org.apache.gravitino.exceptions.NoSuchMetalakeException;
+import org.apache.gravitino.exceptions.NoSuchSchemaException;
+import org.apache.gravitino.exceptions.NoSuchTableException;
+import org.apache.gravitino.rel.TableChange;
+
+/** Update the comment of a table. */
+public class UpdateTableComment extends Command {
+
+  protected final String metalake;
+  protected final String catalog;
+  protected final String schema;
+  protected final String table;
+  protected final String comment;
+
+  /**
+   * Update the comment of a table.
+   *
+   * @param url The URL of the Gravitino server.
+   * @param ignoreVersions If true don't check the client/server versions 
match.
+   * @param metalake The name of the metalake.
+   * @param catalog The name of the catalog.
+   * @param schema The name of the schema.
+   * @param table The name of the table.
+   * @param comment New metalake comment.
+   */
+  public UpdateTableComment(
+      String url,
+      boolean ignoreVersions,
+      String metalake,
+      String catalog,
+      String schema,
+      String table,
+      String comment) {
+    super(url, ignoreVersions);
+    this.metalake = metalake;
+    this.catalog = catalog;
+    this.schema = schema;
+    this.table = table;
+    this.comment = comment;
+  }
+
+  /** Update the comment of a table. */
+  public void handle() {
+    try {
+      NameIdentifier tableName = NameIdentifier.of(schema, table);
+      GravitinoClient client = buildClient(metalake);
+      TableChange change = TableChange.updateComment(comment);
+
+      client.loadCatalog(catalog).asTableCatalog().alterTable(tableName, 
change);
+    } catch (NoSuchMetalakeException err) {
+      System.err.println(ErrorMessages.UNKNOWN_METALAKE);
+      return;
+    } catch (NoSuchCatalogException err) {
+      System.err.println(ErrorMessages.UNKNOWN_CATALOG);
+      return;
+    } catch (NoSuchSchemaException err) {
+      System.err.println(ErrorMessages.UNKNOWN_SCHEMA);
+      return;
+    } catch (NoSuchTableException err) {
+      System.err.println(ErrorMessages.UNKNOWN_TABLE);
+      return;
+    } catch (Exception exp) {
+      System.err.println(exp.getMessage());
+      return;
+    }
+
+    System.out.println(table + " comment changed.");
+  }
+}
diff --git 
a/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateTableName.java
 
b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateTableName.java
new file mode 100644
index 000000000..e2f4fae61
--- /dev/null
+++ 
b/clients/cli/src/main/java/org/apache/gravitino/cli/commands/UpdateTableName.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.cli.commands;
+
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.cli.ErrorMessages;
+import org.apache.gravitino.client.GravitinoClient;
+import org.apache.gravitino.exceptions.NoSuchCatalogException;
+import org.apache.gravitino.exceptions.NoSuchMetalakeException;
+import org.apache.gravitino.exceptions.NoSuchSchemaException;
+import org.apache.gravitino.exceptions.NoSuchTableException;
+import org.apache.gravitino.rel.TableChange;
+
+/** Update the name of a table. */
+public class UpdateTableName extends Command {
+
+  protected final String metalake;
+  protected final String catalog;
+  protected final String schema;
+  protected final String table;
+  protected final String name;
+
+  /**
+   * Update the name of a table.
+   *
+   * @param url The URL of the Gravitino server.
+   * @param ignoreVersions If true don't check the client/server versions 
match.
+   * @param metalake The name of the metalake.
+   * @param catalog The name of the catalog.
+   * @param schema The name of the schema.
+   * @param table The name of the table.
+   * @param name The new metalake name.
+   */
+  public UpdateTableName(
+      String url,
+      boolean ignoreVersions,
+      String metalake,
+      String catalog,
+      String schema,
+      String table,
+      String name) {
+    super(url, ignoreVersions);
+    this.metalake = metalake;
+    this.catalog = catalog;
+    this.schema = schema;
+    this.table = table;
+    this.name = name;
+  }
+
+  /** Update the name of a table. */
+  public void handle() {
+    try {
+      NameIdentifier tableName = NameIdentifier.of(schema, table);
+      GravitinoClient client = buildClient(metalake);
+      TableChange change = TableChange.rename(name);
+
+      client.loadCatalog(catalog).asTableCatalog().alterTable(tableName, 
change);
+    } catch (NoSuchMetalakeException err) {
+      System.err.println(ErrorMessages.UNKNOWN_METALAKE);
+      return;
+    } catch (NoSuchCatalogException err) {
+      System.err.println(ErrorMessages.UNKNOWN_CATALOG);
+      return;
+    } catch (NoSuchSchemaException err) {
+      System.err.println(ErrorMessages.UNKNOWN_SCHEMA);
+      return;
+    } catch (NoSuchTableException err) {
+      System.err.println(ErrorMessages.UNKNOWN_TABLE);
+      return;
+    } catch (Exception exp) {
+      System.err.println(exp.getMessage());
+      return;
+    }
+
+    System.out.println(table + " name changed.");
+  }
+}
diff --git 
a/clients/cli/src/test/java/org/apache/gravitino/cli/TestTableCommands.java 
b/clients/cli/src/test/java/org/apache/gravitino/cli/TestTableCommands.java
index 59b6511be..718f63e75 100644
--- a/clients/cli/src/test/java/org/apache/gravitino/cli/TestTableCommands.java
+++ b/clients/cli/src/test/java/org/apache/gravitino/cli/TestTableCommands.java
@@ -37,6 +37,8 @@ import org.apache.gravitino.cli.commands.TableAudit;
 import org.apache.gravitino.cli.commands.TableDetails;
 import org.apache.gravitino.cli.commands.TableDistribution;
 import org.apache.gravitino.cli.commands.TablePartition;
+import org.apache.gravitino.cli.commands.UpdateTableComment;
+import org.apache.gravitino.cli.commands.UpdateTableName;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -299,4 +301,58 @@ class TestTableCommands {
     commandLine.handleCommandLine();
     verify(mockSetProperties).handle();
   }
+
+  @Test
+  void testUpdateTableCommentsCommand() {
+    UpdateTableComment mockUpdate = mock(UpdateTableComment.class);
+    
when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);
+    
when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");
+    when(mockCommandLine.hasOption(GravitinoOptions.NAME)).thenReturn(true);
+    
when(mockCommandLine.getOptionValue(GravitinoOptions.NAME)).thenReturn("catalog.schema.users");
+    when(mockCommandLine.hasOption(GravitinoOptions.COMMENT)).thenReturn(true);
+    
when(mockCommandLine.getOptionValue(GravitinoOptions.COMMENT)).thenReturn("New 
comment");
+    GravitinoCommandLine commandLine =
+        spy(
+            new GravitinoCommandLine(
+                mockCommandLine, mockOptions, CommandEntities.TABLE, 
CommandActions.UPDATE));
+    doReturn(mockUpdate)
+        .when(commandLine)
+        .newUpdateTableComment(
+            GravitinoCommandLine.DEFAULT_URL,
+            false,
+            "metalake_demo",
+            "catalog",
+            "schema",
+            "users",
+            "New comment");
+    commandLine.handleCommandLine();
+    verify(mockUpdate).handle();
+  }
+
+  @Test
+  void testupdateTableNmeCommand() {
+    UpdateTableName mockUpdate = mock(UpdateTableName.class);
+    
when(mockCommandLine.hasOption(GravitinoOptions.METALAKE)).thenReturn(true);
+    
when(mockCommandLine.getOptionValue(GravitinoOptions.METALAKE)).thenReturn("metalake_demo");
+    when(mockCommandLine.hasOption(GravitinoOptions.NAME)).thenReturn(true);
+    
when(mockCommandLine.getOptionValue(GravitinoOptions.NAME)).thenReturn("catalog.schema.users");
+    when(mockCommandLine.hasOption(GravitinoOptions.RENAME)).thenReturn(true);
+    
when(mockCommandLine.getOptionValue(GravitinoOptions.RENAME)).thenReturn("people");
+    GravitinoCommandLine commandLine =
+        spy(
+            new GravitinoCommandLine(
+                mockCommandLine, mockOptions, CommandEntities.TABLE, 
CommandActions.UPDATE));
+    doReturn(mockUpdate)
+        .when(commandLine)
+        .newUpdateTableName(
+            GravitinoCommandLine.DEFAULT_URL,
+            false,
+            "metalake_demo",
+            "catalog",
+            "schema",
+            "users",
+            "people");
+    commandLine.handleCommandLine();
+    verify(mockUpdate).handle();
+  }
 }

Reply via email to