Skip to content

Commit

Permalink
[Enhancement] Support describe files()
Browse files Browse the repository at this point in the history
Signed-off-by: wyb <wybb86@gmail.com>
  • Loading branch information
wyb committed Sep 2, 2024
1 parent 953cad4 commit ebc541b
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,10 @@ public Void visitCancelAlterTableStatement(CancelAlterTableStmt statement, Conne

@Override
public Void visitDescTableStmt(DescribeStmt statement, ConnectContext context) {
if (statement.isTableFunctionTable()) {
return null;
}

try {
Authorizer.checkAnyActionOnTable(context.getCurrentUserIdentity(), context.getCurrentRoleIds(),
statement.getDbTableName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
import com.starrocks.catalog.MysqlTable;
import com.starrocks.catalog.OlapTable;
import com.starrocks.catalog.Table;
import com.starrocks.catalog.TableFunctionTable;
import com.starrocks.catalog.Type;
import com.starrocks.common.AnalysisException;
import com.starrocks.common.DdlException;
import com.starrocks.common.ErrorCode;
import com.starrocks.common.ErrorReport;
import com.starrocks.common.proc.ExternalTableProcDir;
Expand Down Expand Up @@ -308,6 +310,11 @@ public Void visitShowDataStatement(ShowDataStmt node, ConnectContext context) {

@Override
public Void visitDescTableStmt(DescribeStmt node, ConnectContext context) {
if (node.isTableFunctionTable()) {
descTableFunctionTable(node, context);
return null;
}

node.getDbTableName().normalization(context);
TableName tableName = node.getDbTableName();
String catalogName = tableName.getCatalog();
Expand All @@ -331,6 +338,24 @@ public Void visitDescTableStmt(DescribeStmt node, ConnectContext context) {
return null;
}

private void descTableFunctionTable(DescribeStmt node, ConnectContext context) {
Table table = null;
try {
table = new TableFunctionTable(node.getTableFunctionProperties());
} catch (DdlException e) {
throw new StorageAccessException(e);
}

List<Column> columns = table.getFullSchema();
for (Column column : columns) {
List<String> row = Arrays.asList(
column.getName(),
column.getType().canonicalName().toLowerCase(),
column.isAllowNull() ? "YES" : "NO");
node.getTotalRows().add(row);
}
}

private void descInternalCatalogTable(DescribeStmt node, ConnectContext context) {
Database db = GlobalStateMgr.getCurrentState().getDb(node.getDb());
if (db == null) {
Expand Down
33 changes: 32 additions & 1 deletion fe/fe-core/src/main/java/com/starrocks/sql/ast/DescribeStmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class DescribeStmt extends ShowStmt {

Expand Down Expand Up @@ -63,6 +64,13 @@ public class DescribeStmt extends ShowStmt {
.addColumn(new Column("Table", ScalarType.createVarchar(30)))
.build();

private static final ShowResultSetMetaData DESC_TABLE_FUNCTION_TABLE_META_DATA =
ShowResultSetMetaData.builder()
.addColumn(new Column("Field", ScalarType.createVarchar(20)))
.addColumn(new Column("Type", ScalarType.createVarchar(20)))
.addColumn(new Column("Null", ScalarType.createVarchar(10)))
.build();

// empty col num equals to DESC_OLAP_TABLE_ALL_META_DATA.size()
public static final List<String> EMPTY_ROW = initEmptyRow();

Expand All @@ -75,6 +83,9 @@ public class DescribeStmt extends ShowStmt {
private boolean isOlapTable;
private boolean isMaterializedView;

private boolean isTableFunctionTable = false;
private Map<String, String> tableFunctionProperties = null;

public DescribeStmt(TableName dbTableName, boolean isAllTables) {
this(dbTableName, isAllTables, NodePosition.ZERO);
}
Expand All @@ -86,6 +97,14 @@ public DescribeStmt(TableName dbTableName, boolean isAllTables, NodePosition pos
this.isAllTables = isAllTables;
}

public DescribeStmt(Map<String, String> tableFunctionProperties, NodePosition pos) {
super(pos);
this.dbTableName = null;
this.totalRows = new LinkedList<>();
this.isTableFunctionTable = true;
this.tableFunctionProperties = tableFunctionProperties;
}

public boolean isAllTables() {
return isAllTables;
}
Expand Down Expand Up @@ -126,8 +145,16 @@ public void setOlapTable(boolean olapTable) {
isOlapTable = olapTable;
}

public boolean isTableFunctionTable() {
return isTableFunctionTable;
}

public Map<String, String> getTableFunctionProperties() {
return tableFunctionProperties;
}

public List<List<String>> getResultRows() throws AnalysisException {
if (isAllTables || isMaterializedView) {
if (isAllTables || isMaterializedView || isTableFunctionTable) {
return totalRows;
} else {
Preconditions.checkNotNull(node);
Expand All @@ -137,6 +164,10 @@ public List<List<String>> getResultRows() throws AnalysisException {

@Override
public ShowResultSetMetaData getMetaData() {
if (isTableFunctionTable) {
return DESC_TABLE_FUNCTION_TABLE_META_DATA;
}

if (!isAllTables) {
if (isMaterializedView) {
return DESC_OLAP_TABLE_META_DATA;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1245,9 +1245,14 @@ public ParseNode visitShowTemporaryTablesStatement(StarRocksParser.ShowTemporary

@Override
public ParseNode visitDescTableStatement(StarRocksParser.DescTableStatementContext context) {
QualifiedName qualifiedName = getQualifiedName(context.qualifiedName());
TableName targetTableName = qualifiedNameToTableName(qualifiedName);
return new DescribeStmt(targetTableName, context.ALL() != null, createPos(context));
if (context.qualifiedName() != null) {
QualifiedName qualifiedName = getQualifiedName(context.qualifiedName());
TableName targetTableName = qualifiedNameToTableName(qualifiedName);
return new DescribeStmt(targetTableName, context.ALL() != null, createPos(context));
}

Map<String, String> tableFunctionProperties = getPropertyList(context.propertyList());
return new DescribeStmt(tableFunctionProperties, createPos(context));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ showAlterStatement
;

descTableStatement
: (DESC | DESCRIBE) table=qualifiedName ALL?
: (DESC | DESCRIBE) ((table=qualifiedName ALL?) | (FILES propertyList))
;

createTableLikeStatement
Expand Down
10 changes: 9 additions & 1 deletion test/sql/test_files/R/csv_format
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ $3 decimal(38,9) YES false None
$4 boolean YES false None
-- !result

shell: ossutil64 rm -rf oss://${oss_bucket}/test_files/csv_format/${uuid0}/ > /dev/null
desc files('path' = 'oss://${oss_bucket}/test_files/csv_format/${uuid0}/*', 'format'='csv', "csv.column_separator"=",","csv.row_delimiter"="\n");
-- result:
$1 bigint YES
$2 varchar(1048576) YES
$3 double YES
$4 boolean YES
-- !result

shell: ossutil64 rm -rf oss://${oss_bucket}/test_files/csv_format/${uuid0}/ > /dev/null
3 changes: 2 additions & 1 deletion test/sql/test_files/T/csv_format
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ select * from files('path' = 'oss://${oss_bucket}/test_files/csv_format/${uuid0}

-- check schema
desc t1;
desc files('path' = 'oss://${oss_bucket}/test_files/csv_format/${uuid0}/*', 'format'='csv', "csv.column_separator"=",","csv.row_delimiter"="\n");

-- clean
shell: ossutil64 rm -rf oss://${oss_bucket}/test_files/csv_format/${uuid0}/ > /dev/null
shell: ossutil64 rm -rf oss://${oss_bucket}/test_files/csv_format/${uuid0}/ > /dev/null

0 comments on commit ebc541b

Please sign in to comment.