Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#5277] improvement(cli): display index information for tables in the Gravitino CLI #5476

Merged
merged 14 commits into from
Nov 14, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.apache.gravitino.cli.commands.ListColumns;
import org.apache.gravitino.cli.commands.ListEntityTags;
import org.apache.gravitino.cli.commands.ListGroups;
import org.apache.gravitino.cli.commands.ListIndexes;
import org.apache.gravitino.cli.commands.ListMetalakeProperties;
import org.apache.gravitino.cli.commands.ListMetalakes;
import org.apache.gravitino.cli.commands.ListRoles;
Expand Down Expand Up @@ -108,7 +109,7 @@ public class GravitinoCommandLine {
*
* @param line Parsed command line object.
* @param options Available options for the CLI.
* @param entity The entity to apply the command to e.g. metalake, catalog, schema, table etc etc.
* @param entity The entity to apply the command to e.g. metlake, catalog, schema, table etc.
orenccl marked this conversation as resolved.
Show resolved Hide resolved
* @param command The type of command to run i.e. list, details, update, delete, or create.
*/
public GravitinoCommandLine(CommandLine line, Options options, String entity, String command) {
Expand Down Expand Up @@ -349,6 +350,8 @@ private void handleTableCommand() {
if (CommandActions.DETAILS.equals(command)) {
if (line.hasOption(GravitinoOptions.AUDIT)) {
new TableAudit(url, ignore, metalake, catalog, schema, table).handle();
} else if (line.hasOption(GravitinoOptions.INDEX)) {
new ListIndexes(url, ignore, metalake, catalog, schema, table).handle();
} else {
new TableDetails(url, ignore, metalake, catalog, schema, table).handle();
}
orenccl marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class GravitinoOptions {
public static final String TAG = "tag";
public static final String ROLE = "role";
public static final String AUDIT = "audit";
public static final String INDEX = "index";
public static final String FORCE = "force";

/**
Expand All @@ -61,6 +62,7 @@ public Options options() {
options.addOption(createArgOption("m", METALAKE, "metalake name"));
options.addOption(createSimpleOption("i", IGNORE, "ignore client/sever version check"));
options.addOption(createSimpleOption("a", AUDIT, "display audit information"));
options.addOption(createSimpleOption("d", INDEX, "Display index infromation"));

// Create/update options
options.addOption(createArgOption(null, RENAME, "new entity name"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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 java.util.Arrays;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.rel.indexes.Index;

/** Displays the index of a table. */
public class ListIndexes extends TableCommand {

protected final String schema;
protected final String table;

/**
* Displays the index 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.
*/
public ListIndexes(
String url,
boolean ignoreVersions,
String metalake,
String catalog,
String schema,
String table) {
super(url, ignoreVersions, metalake, catalog);
this.schema = schema;
this.table = table;
}

/** Displays the details of a table's index. */
public void handle() {
Index[] indexes;

try {
NameIdentifier name = NameIdentifier.of(schema, table);
indexes = tableCatalog().loadTable(name).index();
} catch (Exception exp) {
System.err.println(exp.getMessage());
return;
}

StringBuilder all = new StringBuilder();
for (Index index : indexes) {
// Flatten nested field names into dot-separated strings (e.g., "a.b.c")
Arrays.stream(index.fieldNames())
// Convert nested fields to a single string
.map(nestedFieldName -> String.join(".", nestedFieldName))
.forEach(
fieldName ->
all.append(fieldName)
.append(",")
.append(index.name())
.append(System.lineSeparator()));
}

System.out.print(all);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void handle() {
Table gTable = null;

try {
NameIdentifier name = NameIdentifier.of(table);
NameIdentifier name = NameIdentifier.of(schema, table);
gTable = tableCatalog().loadTable(name);
} catch (Exception exp) {
System.err.println(exp.getMessage());
Expand Down
8 changes: 7 additions & 1 deletion docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ gcli table list --name catalog_postgres.hr
#### Show tables details

```bash
gcli column list --name catalog_postgres.hr.departments
gcli table details --name catalog_postgres.hr.departments
```

#### Show tables audit information
Expand All @@ -374,6 +374,12 @@ gcli column list --name catalog_postgres.hr.departments
gcli table details --name catalog_postgres.hr.departments --audit
```

### Show table indexes

```bash
gcli table details --name catalog_mysql.db.iceberg_namespace_properties --index
```

#### Delete a table

```bash
Expand Down
Loading