Skip to content

Commit

Permalink
[#6533] improvement(CLI): Add default values to column list output in…
Browse files Browse the repository at this point in the history
… CLI (#6538)

### What changes were proposed in this pull request?

improvement(CLI): Add default values to column list output in CLI

### Why are the changes needed?

Fix: #6533

### Does this PR introduce _any_ user-facing change?

No

### How was this patch tested?

local test
Hive Catalog
```bash
gcli column list -m demo_metalake --name Hive_catalog.default.test_dates -i
name,datatype,default_value,comment,nullable,auto_increment
id,integer,,N/A,true,false
event_date,date,,N/A,true,
event_timestamp,timestamp,,N/A,true,

gcli column list -m demo_metalake --name Hive_catalog.default.test_dates -i --output table
+-----------------+-----------+---------+---------------+----------+---------+
|      Name       |   Type    | Default | AutoIncrement | Nullable | Comment |
+-----------------+-----------+---------+---------------+----------+---------+
| id              | integer   |         | false         | true     | N/A     |
| event_date      | date      |         |               | true     | N/A     |
| event_timestamp | timestamp |         |               | true     | N/A     |
+-----------------+-----------+---------+---------------+----------+---------+

gcli table details -m demo_metalake --name Hive_catalog.default.test_dates -i --output table
+-----------------+-----------+---------+---------------+----------+---------+
|      Name       |   Type    | Default | AutoIncrement | Nullable | Comment |
+-----------------+-----------+---------+---------------+----------+---------+
| id              | integer   |         | false         | true     | N/A     |
| event_date      | date      |         |               | true     | N/A     |
| event_timestamp | timestamp |         |               | true     | N/A     |
+-----------------+-----------+---------+---------------+----------+---------+
```

Mysql Catalog
```bash
gcli column list -m demo_metalake --name Mysql_catalog.gravitino_db.catalog_meta -i
name,datatype,default_value,comment,nullable,auto_increment
catalog_id,long unsigned,,catalog id,false,false
catalog_name,varchar(128),,catalog name,false,
metalake_id,long unsigned,,metalake id,false,false
type,varchar(64),,catalog type,false,
provider,varchar(64),,catalog provider,false,
catalog_comment,varchar(256),'',catalog comment,true,
properties,external(MEDIUMTEXT),,catalog properties,true,
audit_info,external(MEDIUMTEXT),,catalog audit info,false,
current_version,integer unsigned,1,catalog current version,false,false
last_version,integer unsigned,1,catalog last version,false,false
deleted_at,long unsigned,0,catalog deleted at,false,false

gcli column list -m demo_metalake --name Mysql_catalog.gravitino_db.catalog_meta -i --output table
+-----------------+----------------------+---------+---------------+----------+-------------------------+
|      Name       |         Type         | Default | AutoIncrement | Nullable |         Comment         |
+-----------------+----------------------+---------+---------------+----------+-------------------------+
| catalog_id      | long unsigned        |         | false         | false    | catalog id              |
| catalog_name    | varchar(128)         |         |               | false    | catalog name            |
| metalake_id     | long unsigned        |         | false         | false    | metalake id             |
| type            | varchar(64)          |         |               | false    | catalog type            |
| provider        | varchar(64)          |         |               | false    | catalog provider        |
| catalog_comment | varchar(256)         | ''      |               | true     | catalog comment         |
| properties      | external(MEDIUMTEXT) |         |               | true     | catalog properties      |
| audit_info      | external(MEDIUMTEXT) |         |               | false    | catalog audit info      |
| current_version | integer unsigned     | 1       | false         | false    | catalog current version |
| last_version    | integer unsigned     | 1       | false         | false    | catalog last version    |
| deleted_at      | long unsigned        | 0       | false         | false    | catalog deleted at      |
+-----------------+----------------------+---------+---------------+----------+-------------------------+
```
  • Loading branch information
Abyss-lord authored Feb 27, 2025
1 parent 54a5371 commit a95933d
Show file tree
Hide file tree
Showing 7 changed files with 702 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,6 @@ public void handle() {
exitWithError(exp.getMessage());
}

StringBuilder all = new StringBuilder();
for (int i = 0; i < columns.length; i++) {
String name = columns[i].name();
String dataType = columns[i].dataType().simpleString();
String comment = columns[i].comment();
String nullable = columns[i].nullable() ? "true" : "false";
String autoIncrement = columns[i].autoIncrement() ? "true" : "false";

if (i == 0) {
all.append("name,datatype,comment,nullable,auto_increment" + System.lineSeparator());
}
// TODO default values
all.append(
name
+ ","
+ dataType
+ ","
+ comment
+ ","
+ nullable
+ ","
+ autoIncrement
+ System.lineSeparator());
}

printResults(all.toString());
printResults(columns);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@
package org.apache.gravitino.cli.outputs;

import com.google.common.base.Preconditions;
import java.util.Arrays;
import java.util.regex.Pattern;
import org.apache.gravitino.rel.expressions.Expression;
import org.apache.gravitino.rel.expressions.FunctionExpression;
import org.apache.gravitino.rel.expressions.literals.Literal;
import org.apache.gravitino.rel.types.Type;

public class LineUtil {
// This expression is primarily used to match characters that have a display width of
// 2, such as characters from Korean, Chinese
private static final Pattern FULL_WIDTH_PATTERN =
Pattern.compile(
"[\u1100-\u115F\u2E80-\uA4CF\uAC00-\uD7A3\uF900-\uFAFF\uFE10-\uFE19\uFE30-\uFE6F\uFF00-\uFF60\uFFE0-\uFFE6]");
public static final String EMPTY_DEFAULT_VALUE = "";
public static final String EMPTY_STRING_TYPE_DEFAULT_VALUE = "''";

/**
* Get the display width of a string.
Expand Down Expand Up @@ -100,4 +107,88 @@ public static String capitalize(String str) {
}
}
}

/**
* Get the default value of a column. if the column has a default value, return it as a string. if
* the column does not set a default value, return {@link #EMPTY_DEFAULT_VALUE}. if the column is
* of string type and the default value is an empty string, return {@link
* #EMPTY_STRING_TYPE_DEFAULT_VALUE}
*
* @param column the column to get.
* @return the default value as a string.
*/
public static String getDefaultValue(org.apache.gravitino.rel.Column column) {
Expression defaultValue = column.defaultValue();
if (defaultValue == null
|| defaultValue == org.apache.gravitino.rel.Column.DEFAULT_VALUE_NOT_SET) {
return EMPTY_DEFAULT_VALUE;
}

if (defaultValue instanceof Literal && ((Literal<?>) defaultValue).value() != null) {
String defaultValueStr = ((Literal<?>) defaultValue).value().toString().trim();
if ("".equalsIgnoreCase(defaultValueStr) && isStringType(column)) {
return EMPTY_STRING_TYPE_DEFAULT_VALUE;
}
return defaultValueStr;
} else if (defaultValue instanceof FunctionExpression) {
return defaultValue.toString();
} else if (defaultValue.references().length == 0) {
return EMPTY_DEFAULT_VALUE;
}

return Arrays.toString(defaultValue.references());
}

/**
* If the column is of integer type, return whether it is auto-incremented, otherwise return an
* empty string.
*
* @param column the column to check.
* @return if the column is of integer type and auto-incremented, return "true", otherwise return
* an empty string.
*/
public static String getAutoIncrement(org.apache.gravitino.rel.Column column) {
if (isIntegerType(column)) {
return column.autoIncrement() ? "true" : "false";
}

return "";
}

/**
* Check if a column is of integer type.
*
* @param column the column to check.
* @return true if the column is of integer type, false otherwise.
*/
public static boolean isIntegerType(org.apache.gravitino.rel.Column column) {
Type.Name columnTypeName = column.dataType().name();
return columnTypeName == Type.Name.LONG
|| columnTypeName == Type.Name.INTEGER
|| columnTypeName == Type.Name.SHORT
|| columnTypeName == Type.Name.BYTE;
}

/**
* Check if a column is of string type.
*
* @param column the column to check.
* @return true if the column is of string type, false otherwise.
*/
public static boolean isStringType(org.apache.gravitino.rel.Column column) {
Type.Name columnTypeName = column.dataType().name();
return columnTypeName == Type.Name.STRING
|| columnTypeName == Type.Name.VARCHAR
|| columnTypeName == Type.Name.FIXEDCHAR;
}

/**
* Get the comment of a column. If the column does not have a comment, return "N/A".
*
* @param column the column to get.
* @return the comment of the column.
*/
public static String getComment(org.apache.gravitino.rel.Column column) {
return column.comment() == null ? "N/A" : column.comment();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.gravitino.Metalake;
import org.apache.gravitino.Schema;
import org.apache.gravitino.cli.CommandContext;
import org.apache.gravitino.rel.Column;
import org.apache.gravitino.rel.Table;

/** Plain format to print a pretty string to standard out. */
Expand Down Expand Up @@ -58,6 +59,8 @@ public static void output(Object entity, CommandContext context) {
new TableListPlainFormat(context).output((Table[]) entity);
} else if (entity instanceof Audit) {
new AuditPlainFormat(context).output((Audit) entity);
} else if (entity instanceof Column[]) {
new ColumnListPlainFormat(context).output((Column[]) entity);
} else {
throw new IllegalArgumentException("Unsupported object type");
}
Expand Down Expand Up @@ -231,4 +234,43 @@ public String getOutput(Audit audit) {
audit.lastModifiedTime() == null ? "N/A" : audit.lastModifiedTime());
}
}

/**
* Formats an array of {@link org.apache.gravitino.rel.Column} into a six-column table display.
* Lists all column names, types, default values, auto-increment, nullable, and comments in a
* plain format.
*/
static final class ColumnListPlainFormat extends PlainFormat<Column[]> {

/**
* Creates a new {@link ColumnListPlainFormat} with the specified output properties.
*
* @param context The command context.
*/
public ColumnListPlainFormat(CommandContext context) {
super(context);
}

/** {@inheritDoc} */
@Override
public String getOutput(Column[] columns) {
String header =
COMMA_JOINER.join(
"name", "datatype", "default_value", "comment", "nullable", "auto_increment");
StringBuilder data = new StringBuilder();
for (int i = 0; i < columns.length; i++) {
String name = columns[i].name();
String dataType = columns[i].dataType().simpleString();
String defaultValue = LineUtil.getDefaultValue(columns[i]);
String comment = LineUtil.getComment(columns[i]);
String nullable = columns[i].nullable() ? "true" : "false";
String autoIncrement = LineUtil.getAutoIncrement(columns[i]);

data.append(
COMMA_JOINER.join(name, dataType, defaultValue, comment, nullable, autoIncrement));
data.append(System.lineSeparator());
}
return NEWLINE_JOINER.join(header, data.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public static void output(Object entity, CommandContext context) {
new TableListTableFormat(context).output((Table[]) entity);
} else if (entity instanceof Audit) {
new AuditTableFormat(context).output((Audit) entity);
} else if (entity instanceof org.apache.gravitino.rel.Column[]) {
new ColumnListTableFormat(context).output((org.apache.gravitino.rel.Column[]) entity);
} else {
throw new IllegalArgumentException("Unsupported object type");
}
Expand Down Expand Up @@ -609,6 +611,7 @@ public TableDetailsTableFormat(CommandContext context) {
public String getOutput(Table table) {
Column columnName = new Column(context, "name");
Column columnType = new Column(context, "type");
Column columnDefaultValue = new Column(context, "default");
Column columnAutoIncrement = new Column(context, "AutoIncrement");
Column columnNullable = new Column(context, "nullable");
Column columnComment = new Column(context, "comment");
Expand All @@ -617,14 +620,20 @@ public String getOutput(Table table) {
for (org.apache.gravitino.rel.Column column : columns) {
columnName.addCell(column.name());
columnType.addCell(column.dataType().simpleString());
columnAutoIncrement.addCell(column.autoIncrement());
columnDefaultValue.addCell(LineUtil.getDefaultValue(column));
columnAutoIncrement.addCell(LineUtil.getAutoIncrement(column));
columnNullable.addCell(column.nullable());
columnComment.addCell(
column.comment() == null || column.comment().isEmpty() ? "N/A" : column.comment());
}

return getTableFormat(
columnName, columnType, columnAutoIncrement, columnNullable, columnComment);
columnName,
columnType,
columnDefaultValue,
columnAutoIncrement,
columnNullable,
columnComment);
}
}

Expand Down Expand Up @@ -673,4 +682,49 @@ public String getOutput(Audit audit) {
return getTableFormat(columnCreator, columnCreateTime, columnModified, columnModifyTime);
}
}

/**
* Formats an array of {@link org.apache.gravitino.rel.Column} into a six-column table display.
* Lists all column names, types, default values, auto-increment, nullable, and comments in a
* vertical format.
*/
static final class ColumnListTableFormat extends TableFormat<org.apache.gravitino.rel.Column[]> {

/**
* Creates a new {@link TableFormat} with the specified properties.
*
* @param context the command context.
*/
public ColumnListTableFormat(CommandContext context) {
super(context);
}

/** {@inheritDoc} */
@Override
public String getOutput(org.apache.gravitino.rel.Column[] columns) {
Column columnName = new Column(context, "name");
Column columnType = new Column(context, "type");
Column columnDefaultVal = new Column(context, "default");
Column columnAutoIncrement = new Column(context, "AutoIncrement");
Column columnNullable = new Column(context, "nullable");
Column columnComment = new Column(context, "comment");

for (org.apache.gravitino.rel.Column column : columns) {
columnName.addCell(column.name());
columnType.addCell(column.dataType().simpleString());
columnDefaultVal.addCell(LineUtil.getDefaultValue(column));
columnAutoIncrement.addCell(LineUtil.getAutoIncrement(column));
columnNullable.addCell(column.nullable());
columnComment.addCell(LineUtil.getComment(column));
}

return getTableFormat(
columnName,
columnType,
columnDefaultVal,
columnAutoIncrement,
columnNullable,
columnComment);
}
}
}
Loading

0 comments on commit a95933d

Please sign in to comment.