Skip to content

Commit

Permalink
[apache#6493] feat(CLI): Support table format output for Schema and T…
Browse files Browse the repository at this point in the history
…able command

fix some bugs.
  • Loading branch information
Abyss-lord committed Feb 23, 2025
1 parent bccc0c1 commit f266f5a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

package org.apache.gravitino.cli.commands;

import org.apache.gravitino.Catalog;
import org.apache.gravitino.Schema;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.ErrorMessages;
import org.apache.gravitino.client.GravitinoClient;
Expand Down Expand Up @@ -50,12 +48,9 @@ public ListSchema(CommandContext context, String metalake, String catalog) {
@Override
public void handle() {
String[] schemas = new String[0];
Catalog tableCatalog = null;

try {
GravitinoClient client = buildClient(metalake);
tableCatalog = client.loadCatalog(catalog);
schemas = tableCatalog.asSchemas().listSchemas();
schemas = client.loadCatalog(catalog).asSchemas().listSchemas();
} catch (NoSuchMetalakeException err) {
exitWithError(ErrorMessages.UNKNOWN_METALAKE);
} catch (NoSuchCatalogException err) {
Expand All @@ -65,15 +60,10 @@ public void handle() {
}

if (schemas.length == 0) {
printInformation("No schemas found in catalog " + catalog);
printInformation("No schemas exist.");
return;
}
// PERF load table may cause performance issue
Schema[] schemaObjects = new Schema[schemas.length];
for (int i = 0; i < schemas.length; i++) {
schemaObjects[i] = tableCatalog.asSchemas().loadSchema(schemas[i]);
}

printResults(schemaObjects);
printResults(schemas);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.Namespace;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.rel.Table;
import org.apache.gravitino.rel.TableCatalog;

/** List the names of all tables in a schema. */
public class ListTables extends TableCommand {
Expand All @@ -50,32 +48,22 @@ public ListTables(CommandContext context, String metalake, String catalog, Strin
public void handle() {
NameIdentifier[] tables = null;
Namespace name = Namespace.of(schema);
TableCatalog tableCatalog = null;

try {
tableCatalog = tableCatalog();
tables = tableCatalog.listTables(name);
tables = tableCatalog().listTables(name);
} catch (Exception exp) {
exitWithError(exp.getMessage());
}

List<String> tableNames = new ArrayList<>();
for (NameIdentifier table : tables) {
tableNames.add(table.name());
}
// PERF load table may cause performance issue
Table[] tableObjects = new Table[tableNames.size()];
int i = 0;
for (NameIdentifier tableIdent : tables) {
tableObjects[i] = tableCatalog.loadTable(tableIdent);
i++;
for (int i = 0; i < tables.length; i++) {
tableNames.add(tables[i].name());
}

if (tableNames.isEmpty()) {
printInformation("No tables exist.");
return;
}

printResults(tableObjects);
printResults(tableNames);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,10 @@ public static void output(Object entity, CommandContext context) {
new CatalogListTableFormat(context).output((Catalog[]) entity);
} else if (entity instanceof Schema) {
new SchemaTableFormat(context).output((Schema) entity);
} else if (entity instanceof Schema[]) {
new SchemaListTableFormat(context).output((Schema[]) entity);
} else if (entity instanceof Table) {
new TableDetailsTableFormat(context).output((Table) entity);
} else if (entity instanceof Table[]) {
new TableListTableFormat(context).output((Table[]) entity);
} else if (entity instanceof String[]) {
new ListTableFormat(context).output((String[]) entity);
} else {
throw new IllegalArgumentException("Unsupported object type");
}
Expand Down Expand Up @@ -497,7 +495,7 @@ public MetalakeListTableFormat(CommandContext context) {
@Override
public String getOutput(Metalake[] metalakes) {

Column columnName = new Column(context, "metalake");
Column columnName = new Column(context, "Name");
Arrays.stream(metalakes).forEach(metalake -> columnName.addCell(metalake.name()));

return getTableFormat(columnName);
Expand Down Expand Up @@ -544,7 +542,7 @@ public CatalogListTableFormat(CommandContext context) {
/** {@inheritDoc} */
@Override
public String getOutput(Catalog[] catalogs) {
Column columnName = new Column(context, "catalog");
Column columnName = new Column(context, "Name");
Arrays.stream(catalogs).forEach(metalake -> columnName.addCell(metalake.name()));

return getTableFormat(columnName);
Expand Down Expand Up @@ -573,25 +571,6 @@ public String getOutput(Schema schema) {
}
}

/**
* Formats an array of Schemas into a single-column table display. Lists all schema names in a
* vertical format.
*/
static final class SchemaListTableFormat extends TableFormat<Schema[]> {
public SchemaListTableFormat(CommandContext context) {
super(context);
}

/** {@inheritDoc} */
@Override
public String getOutput(Schema[] schemas) {
Column column = new Column(context, "schema");
Arrays.stream(schemas).forEach(schema -> column.addCell(schema.name()));

return getTableFormat(column);
}
}

/**
* Formats a single {@link Table} instance into a two-column table display. Displays table details
* including name and comment information.
Expand Down Expand Up @@ -625,20 +604,21 @@ public String getOutput(Table table) {
}
}

/**
* Formats an array of {@link Table} into a single-column table display. Lists all table names in
* a vertical format.
*/
static final class TableListTableFormat extends TableFormat<Table[]> {
public TableListTableFormat(CommandContext context) {
static final class ListTableFormat extends TableFormat<String[]> {
/**
* Creates a new {@link TableFormat} with the specified properties.
*
* @param context the command context.
*/
public ListTableFormat(CommandContext context) {
super(context);
}

/** {@inheritDoc} */
@Override
public String getOutput(Table[] tables) {
public String getOutput(String[] entities) {
Column column = new Column(context, "name");
Arrays.stream(tables).forEach(table -> column.addCell(table.name()));
Arrays.stream(entities).forEach(column::addCell);

return getTableFormat(column);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void testMetalakeListCommand() {
String output = new String(outputStream.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(
"+-------------+\n"
+ "| Metalake |\n"
+ "| Name |\n"
+ "+-------------+\n"
+ "| my_metalake |\n"
+ "+-------------+",
Expand Down Expand Up @@ -170,7 +170,7 @@ public void testCatalogListCommand() {
String output = new String(outputStream.toByteArray(), StandardCharsets.UTF_8).trim();
assertEquals(
"+-----------+\n"
+ "| Catalog |\n"
+ "| Name |\n"
+ "+-----------+\n"
+ "| postgres |\n"
+ "| postgres2 |\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ void testListMetalakeWithTableFormat() {
String output = new String(outContent.toByteArray(), StandardCharsets.UTF_8).trim();
Assertions.assertEquals(
"+-----------+\n"
+ "| Metalake |\n"
+ "| Name |\n"
+ "+-----------+\n"
+ "| metalake1 |\n"
+ "| metalake2 |\n"
Expand All @@ -282,16 +282,11 @@ void testCatalogDetailsWithTableFormat() {
@Test
void testListCatalogWithTableFormat() {
CommandContext mockContext = getMockContext();
Catalog mockCatalog1 =
getMockCatalog("catalog1", Catalog.Type.FILESET, "provider1", "This is a catalog");
Catalog mockCatalog2 =
getMockCatalog("catalog2", Catalog.Type.RELATIONAL, "provider2", "This is another catalog");

TableFormat.output(new Catalog[] {mockCatalog1, mockCatalog2}, mockContext);
TableFormat.output(new String[] {"catalog1", "catalog2"}, mockContext);
String output = new String(outContent.toByteArray(), StandardCharsets.UTF_8).trim();
Assertions.assertEquals(
"+----------+\n"
+ "| Catalog |\n"
+ "| Name |\n"
+ "+----------+\n"
+ "| catalog1 |\n"
+ "| catalog2 |\n"
Expand All @@ -318,14 +313,11 @@ void testSchemaDetailsWithTableFormat() {
@Test
void testListSchemaWithTableFormat() {
CommandContext mockContext = getMockContext();
Schema mockSchema1 = getMockSchema("schema1", "This is a schema");
Schema mockSchema2 = getMockSchema("schema2", "This is another schema");

TableFormat.output(new Schema[] {mockSchema1, mockSchema2}, mockContext);
TableFormat.output(new String[] {"schema1", "schema2"}, mockContext);
String output = new String(outContent.toByteArray(), StandardCharsets.UTF_8).trim();
Assertions.assertEquals(
"+---------+\n"
+ "| Schema |\n"
+ "| Name |\n"
+ "+---------+\n"
+ "| schema1 |\n"
+ "| schema2 |\n"
Expand Down Expand Up @@ -353,10 +345,8 @@ void testTableDetailsWithTableFormat() {
@Test
void testListTableWithTableFormat() {
CommandContext mockContext = getMockContext();
Table mockTable1 = getMockTable("table1", "This is a table");
Table mockTable2 = getMockTable("table2", "This is another table");

TableFormat.output(new Table[] {mockTable1, mockTable2}, mockContext);
TableFormat.output(new String[] {"table1", "table2"}, mockContext);
String output = new String(outContent.toByteArray(), StandardCharsets.UTF_8).trim();
Assertions.assertEquals(
"+--------+\n"
Expand Down

0 comments on commit f266f5a

Please sign in to comment.