Skip to content

Commit

Permalink
[apache#5746] improve(CLI): Support table format output for Audit com…
Browse files Browse the repository at this point in the history
…mand

Support table format output for Audit command.
  • Loading branch information
Abyss-lord committed Feb 19, 2025
1 parent 80eaca6 commit 2985aff
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public abstract class BaseOutputFormat<T> implements OutputFormat<T> {
* @param context the command context, must not be null;
*/
public BaseOutputFormat(CommandContext context) {
Preconditions.checkNotNull(context, "context cannot be null");
Preconditions.checkNotNull(context, "CommandContext cannot be null");
this.context = context;
this.limit = context.outputLimit();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import org.apache.gravitino.Metalake;
import org.apache.gravitino.Schema;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.rel.Table;

/**
* Abstract base class for formatting entity information into ASCII-art tables. Provides
Expand Down Expand Up @@ -89,10 +88,6 @@ public static void output(Object entity, CommandContext context) {
new SchemaTableFormat(context).output((Schema) entity);
} else if (entity instanceof Schema[]) {
new SchemasTableFormat(context).output((Schema[]) entity);
} else if (entity instanceof Table) {
new TableTableFormat(context).output((Table) entity);
} else if (entity instanceof Table[]) {
new TablesTableFormat(context).output((Table[]) entity);
} else if (entity instanceof Audit) {
new AuditTableFormat(context).output((Audit) entity);
} else {
Expand Down Expand Up @@ -529,13 +524,13 @@ public MetalakeTableFormat(CommandContext context) {
/** {@inheritDoc} */
@Override
public String getOutput(Metalake metalake) {
Column columnName = new Column(context, "METALAKE");
Column columnComment = new Column(context, "COMMENT");
Column columnA = new Column(context, "METALAKE");
Column columnB = new Column(context, "COMMENT");

columnName.addCell(metalake.name());
columnComment.addCell(metalake.comment());
columnA.addCell(metalake.name());
columnB.addCell(metalake.comment());

return getTableFormat(columnName, columnComment);
return getTableFormat(columnA, columnB);
}
}

Expand All @@ -556,10 +551,10 @@ public String getOutput(Metalake[] metalakes) {
output("No metalakes exist.", System.err);
return null;
} else {
Column columnName = new Column(context, "METALAKE");
Arrays.stream(metalakes).forEach(metalake -> columnName.addCell(metalake.name()));
Column columnA = new Column(context, "METALAKE");
Arrays.stream(metalakes).forEach(metalake -> columnA.addCell(metalake.name()));

return getTableFormat(columnName);
return getTableFormat(columnA);
}
}
}
Expand All @@ -577,17 +572,17 @@ public CatalogTableFormat(CommandContext context) {
/** {@inheritDoc} */
@Override
public String getOutput(Catalog catalog) {
Column columnName = new Column(context, "CATALOG");
Column columnType = new Column(context, "TYPE");
Column columnProvider = new Column(context, "PROVIDER");
Column columnComment = new Column(context, "COMMENT");
Column columnA = new Column(context, "CATALOG");
Column columnB = new Column(context, "TYPE");
Column columnC = new Column(context, "PROVIDER");
Column columnD = new Column(context, "COMMENT");

columnName.addCell(catalog.name());
columnType.addCell(catalog.type().name());
columnProvider.addCell(catalog.provider());
columnComment.addCell(catalog.comment());
columnA.addCell(catalog.name());
columnB.addCell(catalog.type().name());
columnC.addCell(catalog.provider());
columnD.addCell(catalog.comment());

return getTableFormat(columnName, columnType, columnProvider, columnComment);
return getTableFormat(columnA, columnB, columnC, columnD);
}
}

Expand All @@ -608,10 +603,10 @@ public String getOutput(Catalog[] catalogs) {
output("No metalakes exist.", System.err);
return null;
} else {
Column columnName = new Column(context, "CATALOG");
Arrays.stream(catalogs).forEach(metalake -> columnName.addCell(metalake.name()));
Column columnA = new Column(context, "CATALOG");
Arrays.stream(catalogs).forEach(metalake -> columnA.addCell(metalake.name()));

return getTableFormat(columnName);
return getTableFormat(columnA);
}
}
}
Expand All @@ -628,13 +623,13 @@ public SchemaTableFormat(CommandContext context) {
/** {@inheritDoc} */
@Override
public String getOutput(Schema schema) {
Column columnName = new Column(context, "SCHEMA");
Column columnComment = new Column(context, "COMMENT");
Column columnA = new Column(context, "SCHEMA");
Column columnB = new Column(context, "COMMENT");

columnName.addCell(schema.name());
columnComment.addCell(schema.comment());
columnA.addCell(schema.name());
columnB.addCell(schema.comment());

return getTableFormat(columnName, columnComment);
return getTableFormat(columnA, columnB);
}
}

Expand All @@ -654,61 +649,10 @@ public String getOutput(Schema[] schemas) {
output("No schemas exist.", System.err);
return null;
} else {
Column columnName = new Column(context, "SCHEMA");
Arrays.stream(schemas).forEach(schema -> columnName.addCell(schema.name()));
Column column = new Column(context, "SCHEMA");
Arrays.stream(schemas).forEach(schema -> column.addCell(schema.name()));

return getTableFormat(columnName);
}
}
}

/**
* Formats a single Table instance into a three-column table display. Displays table details
* including column-name, column-type, and column-comment information.
*/
static final class TableTableFormat extends TableFormat<Table> {
public TableTableFormat(CommandContext context) {
super(context);
}

/** {@inheritDoc} */
@Override
public String getOutput(Table entity) {
Column columnName = new Column(context, "NAME");
Column columnType = new Column(context, "TYPE");
Column columnComment = new Column(context, "COMMENT");

for (org.apache.gravitino.rel.Column column : entity.columns()) {
columnName.addCell(column.name());
columnType.addCell(column.dataType().simpleString());
columnComment.addCell(column.comment());
}

return getTableFormat(columnName, columnType, columnComment);
}
}

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

/** {@inheritDoc} */
@Override
public String getOutput(Table[] entities) {
if (entities.length == 0) {
return null;
} else {
Column columnName = new Column(context, "NAME");
for (Table table : entities) {
columnName.addCell(table.name());
}

return getTableFormat(columnName);
return getTableFormat(column);
}
}
}
Expand All @@ -721,17 +665,17 @@ public AuditTableFormat(CommandContext context) {
/** {@inheritDoc} */
@Override
public String getOutput(Audit audit) {
Column columnCreator = new Column(context, "creator");
Column columnCreateTime = new Column(context, "create_time");
Column columnModified = new Column(context, "modified");
Column columnModifyTime = new Column(context, "modify_time");
Column columnA = new Column(context, "creator");
Column columnB = new Column(context, "create_time");
Column columnC = new Column(context, "modified");
Column columnD = new Column(context, "modify_time");

columnCreator.addCell(audit.creator());
columnCreateTime.addCell(audit.createTime());
columnModified.addCell(audit.lastModifier());
columnModifyTime.addCell(audit.lastModifiedTime());
columnA.addCell(audit.creator());
columnB.addCell(audit.createTime());
columnC.addCell(audit.lastModifier());
columnD.addCell(audit.lastModifiedTime());

return getTableFormat(columnCreator, columnCreateTime, columnModified, columnModifyTime);
return getTableFormat(columnA, columnB, columnC, columnD);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import org.apache.gravitino.Schema;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.outputs.PlainFormat;
import org.apache.gravitino.rel.Table;
import org.apache.gravitino.rel.types.Types;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -124,31 +122,6 @@ void testListSchemaWithPlainFormat() {
Assertions.assertEquals("schema1\n" + "schema2", output);
}

@Test
void testTableDetailsWithPlainFormat() {
CommandContext mockContext = getMockContext();
Table mockTable = getMockTable();
PlainFormat.output(mockTable, mockContext);
String output = new String(outContent.toByteArray(), StandardCharsets.UTF_8).trim();
Assertions.assertEquals(
"demo_table\n"
+ "col1, boolean, This is a column\n"
+ "col2, string, This is another column\n"
+ "This is a table",
output);
}

@Test
void testListTableWithPlainFormat() {
CommandContext mockContext = getMockContext();
Table mockTable1 = getMockTable("table1", "This is a table");
Table mockTable2 = getMockTable("table2", "This is another table");

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

@Test
void testAuditWithPlainFormat() {
CommandContext mockContext = getMockContext();
Expand Down Expand Up @@ -219,34 +192,6 @@ private Schema getMockSchema(String name, String comment) {
return mockSchema;
}

private Table getMockTable() {
Table mockTable = mock(Table.class);
when(mockTable.name()).thenReturn("demo_table");
when(mockTable.comment()).thenReturn("This is a table");
org.apache.gravitino.rel.Column mockColumn1 = mock(org.apache.gravitino.rel.Column.class);
when(mockColumn1.name()).thenReturn("col1");
when(mockColumn1.dataType()).thenReturn(Types.BooleanType.get());
when(mockColumn1.comment()).thenReturn("This is a column");

org.apache.gravitino.rel.Column mockColumn2 = mock(org.apache.gravitino.rel.Column.class);
when(mockColumn2.name()).thenReturn("col2");
when(mockColumn2.dataType()).thenReturn(Types.StringType.get());
when(mockColumn2.comment()).thenReturn("This is another column");

when(mockTable.columns())
.thenReturn(new org.apache.gravitino.rel.Column[] {mockColumn1, mockColumn2});

return mockTable;
}

private Table getMockTable(String name, String comment) {
Table mockTable = mock(Table.class);
when(mockTable.name()).thenReturn(name);
when(mockTable.comment()).thenReturn(comment);

return mockTable;
}

private Audit getMockAudit() {
Audit mockAudit = mock(Audit.class);
when(mockAudit.creator()).thenReturn("demo_user");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.cli.outputs.Column;
import org.apache.gravitino.cli.outputs.TableFormat;
import org.apache.gravitino.rel.Table;
import org.apache.gravitino.rel.types.Types;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -367,40 +365,6 @@ void testListSchemaWithTableFormat() {
output);
}

@Test
void testTableDetailsWithTableFormat() {
CommandContext mockContext = getMockContext();
Table mockTable = getMockTable();
TableFormat.output(mockTable, mockContext);
String output = new String(outContent.toByteArray(), StandardCharsets.UTF_8).trim();
Assertions.assertEquals(
"+------+---------+------------------------+\n"
+ "| NAME | TYPE | COMMENT |\n"
+ "+------+---------+------------------------+\n"
+ "| col1 | boolean | This is a column |\n"
+ "| col2 | string | This is another column |\n"
+ "+------+---------+------------------------+",
output);
}

@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);
String output = new String(outContent.toByteArray(), StandardCharsets.UTF_8).trim();
Assertions.assertEquals(
"+--------+\n"
+ "| NAME |\n"
+ "+--------+\n"
+ "| table1 |\n"
+ "| table2 |\n"
+ "+--------+",
output);
}

@Test
void testAuditWithTableFormat() {
CommandContext mockContext = getMockContext();
Expand Down Expand Up @@ -480,32 +444,6 @@ private Schema getMockSchema(String name, String comment) {
return mockSchema;
}

private Table getMockTable() {
Table mockTable = mock(Table.class);
org.apache.gravitino.rel.Column mockColumn1 = mock(org.apache.gravitino.rel.Column.class);
when(mockColumn1.name()).thenReturn("col1");
when(mockColumn1.dataType()).thenReturn(Types.BooleanType.get());
when(mockColumn1.comment()).thenReturn("This is a column");

org.apache.gravitino.rel.Column mockColumn2 = mock(org.apache.gravitino.rel.Column.class);
when(mockColumn2.name()).thenReturn("col2");
when(mockColumn2.dataType()).thenReturn(Types.StringType.get());
when(mockColumn2.comment()).thenReturn("This is another column");

when(mockTable.columns())
.thenReturn(new org.apache.gravitino.rel.Column[] {mockColumn1, mockColumn2});

return mockTable;
}

private Table getMockTable(String name, String comment) {
Table mockTable = mock(Table.class);
when(mockTable.name()).thenReturn(name);
when(mockTable.comment()).thenReturn(comment);

return mockTable;
}

private Audit getMockAudit() {
Audit mockAudit = mock(Audit.class);
when(mockAudit.creator()).thenReturn("demo_user");
Expand Down

0 comments on commit 2985aff

Please sign in to comment.