Skip to content

Commit

Permalink
[Enhancement] information_schema.COLUMNS support complex type (#33431)
Browse files Browse the repository at this point in the history
Signed-off-by: Astralidea <[email protected]>
(cherry picked from commit a1a8427)

# Conflicts:
#	fe/fe-core/src/main/java/com/starrocks/service/FrontendServiceImpl.java
  • Loading branch information
Astralidea authored and mergify[bot] committed Oct 24, 2023
1 parent 9ca0ed9 commit 2833b67
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 3 deletions.
15 changes: 12 additions & 3 deletions be/src/exec/vectorized/schema_scanner/schema_columns_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,12 @@ Status SchemaColumnsScanner::fill_chunk(ChunkPtr* chunk) {
// DATA_TYPE
{
ColumnPtr column = (*chunk)->get_column_by_slot_id(8);
std::string str = to_mysql_data_type_string(_desc_result.columns[_column_index].columnDesc);
Slice value(str.c_str(), str.length());
std::string value;
if (_desc_result.columns[_column_index].columnDesc.__isset.dataType) {
value = _desc_result.columns[_column_index].columnDesc.dataType;
} else {
value = to_mysql_data_type_string(_desc_result.columns[_column_index].columnDesc);
}
fill_column_with_slot<TYPE_VARCHAR>(column.get(), (void*)&value);
}
break;
Expand Down Expand Up @@ -393,7 +397,12 @@ Status SchemaColumnsScanner::fill_chunk(ChunkPtr* chunk) {
// COLUMN_TYPE
{
ColumnPtr column = (*chunk)->get_column_by_slot_id(16);
std::string value = type_to_string(_desc_result.columns[_column_index].columnDesc);
std::string value;
if (_desc_result.columns[_column_index].columnDesc.__isset.columnTypeStr) {
value = _desc_result.columns[_column_index].columnDesc.columnTypeStr;
} else {
value = type_to_string(_desc_result.columns[_column_index].columnDesc);
}
fill_column_with_slot<TYPE_VARCHAR>(column.get(), (void*)&value);
}
break;
Expand Down
9 changes: 9 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/catalog/ArrayType.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ public boolean isBooleanType() {
public boolean isNullTypeItem() {
return itemType.isNull();
}

public String toMysqlDataTypeString() {
return "array";
}

// This implementation is the same as BE schema_columns_scanner.cpp type_to_string
public String toMysqlColumnTypeString() {
return toSql();
}
}


9 changes: 9 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/catalog/MapType.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,14 @@ public MapType deserialize(JsonElement jsonElement, java.lang.reflect.Type type,
return new MapType(keyType, valueType);
}
}

public String toMysqlDataTypeString() {
return "map";
}

// This implementation is the same as BE schema_columns_scanner.cpp type_to_string
public String toMysqlColumnTypeString() {
return toSql();
}
}

30 changes: 30 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/catalog/ScalarType.java
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ public String toSql(int depth) {
case DATETIME:
case HLL:
case BITMAP:
case BINARY:
case PERCENTILE:
case JSON:
case FUNCTION:
Expand Down Expand Up @@ -730,4 +731,33 @@ public String canonicalName() {
return toString();
}
}

// This implementation is the same as BE schema_columns_scanner.cpp to_mysql_data_type_string
public String toMysqlDataTypeString() {
switch (type) {
case BOOLEAN:
return "tinyint";
case LARGEINT:
return "bigint unsigned";
case DECIMAL32:
case DECIMAL64:
case DECIMAL128:
case DECIMALV2:
return "decimal";
default:
return type.toString().toLowerCase();
}
}

// This implementation is the same as BE schema_columns_scanner.cpp type_to_string
public String toMysqlColumnTypeString() {
switch (type) {
case BOOLEAN:
return "tinyint(1)";
case LARGEINT:
return "bigint(20) unsigned";
default:
return toSql();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,14 @@ public StructType deserialize(JsonElement jsonElement, java.lang.reflect.Type ty
return new StructType(structFields);
}
}

public String toMysqlDataTypeString() {
return "struct";
}

// This implementation is the same as BE schema_columns_scanner.cpp type_to_string
public String toMysqlColumnTypeString() {
return toSql();
}
}

10 changes: 10 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/catalog/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -1619,4 +1619,14 @@ public static Type getInnermostType(Type type) throws AnalysisException {
public String canonicalName() {
return toString();
}

// This is used for information_schema.COLUMNS DATA_TYPE
public String toMysqlDataTypeString() {
return "unknown";
}

// This is used for information_schema.COLUMNS COLUMN_TYPE
public String toMysqlColumnTypeString() {
return "unknown";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ private boolean setColumnDesc(List<TColumnDef> columns, Table table, long limit,
OlapTable olapTable = (OlapTable) table;
tableKeysType = olapTable.getKeysType().name().substring(0, 3).toUpperCase();
}
<<<<<<< HEAD
for (Column column : table.getBaseSchema()) {
final TColumnDesc desc =
new TColumnDesc(column.getName(), column.getPrimitiveType().toThrift());
Expand Down Expand Up @@ -793,6 +794,40 @@ private boolean setColumnDesc(List<TColumnDef> columns, Table table, long limit,
if (limit > 0 && columns.size() >= limit) {
return true;
}
=======
desc.setColumnDefault(column.getMetaDefaultValue(null));
final Integer columnLength = column.getType().getColumnSize();
if (columnLength != null) {
desc.setColumnLength(columnLength);
}
final Integer decimalDigits = column.getType().getDecimalDigits();
if (decimalDigits != null) {
desc.setColumnScale(decimalDigits);
}
desc.setAllowNull(column.isAllowNull());
if (column.isKey()) {
// COLUMN_KEY (UNI, AGG, DUP, PRI)
desc.setColumnKey(tableKeysType);
} else {
desc.setColumnKey("");
}
desc.setDataType(column.getType().toMysqlDataTypeString());
desc.setColumnTypeStr(column.getType().toMysqlColumnTypeString());
final TColumnDef colDef = new TColumnDef(desc);
final String comment = column.getComment();
if (comment != null) {
colDef.setComment(comment);
}
columns.add(colDef);
// add db_name and table_name values to TColumnDesc if needed
if (needSetDbAndTable) {
columns.get(columns.size() - 1).columnDesc.setDbName(db);
columns.get(columns.size() - 1).columnDesc.setTableName(tbl);
}
// if user set limit, then only return limit size result
if (limit > 0 && columns.size() >= limit) {
return true;
>>>>>>> a1a8427d4e ([Enhancement] information_schema.COLUMNS support complex type (#33431))
}
}
return false;
Expand Down
36 changes: 36 additions & 0 deletions fe/fe-core/src/test/java/com/starrocks/catalog/TypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,42 @@ public void testCanonicalName() {
}
}

@Test
public void testMysqlDataType() {
Object[][] testCases = new Object[][] {
{ScalarType.createType(PrimitiveType.BOOLEAN), "tinyint"},
{ScalarType.createType(PrimitiveType.LARGEINT), "bigint unsigned"},
{ScalarType.createDecimalV3NarrowestType(18, 4), "decimal"},
{new ArrayType(Type.INT), "array"},
{new MapType(Type.INT, Type.INT), "map"},
{new StructType(Lists.newArrayList(Type.INT)), "struct"},
};

for (Object[] tc : testCases) {
Type type = (Type) tc[0];
String name = (String) tc[1];
Assert.assertEquals(name, type.toMysqlDataTypeString());
}
}

@Test
public void testMysqlColumnType() {
Object[][] testCases = new Object[][] {
{ScalarType.createType(PrimitiveType.BOOLEAN), "tinyint(1)"},
{ScalarType.createType(PrimitiveType.LARGEINT), "bigint(20) unsigned"},
{ScalarType.createDecimalV3NarrowestType(18, 4), "decimal64(18, 4)"},
{new ArrayType(Type.INT), "array<int(11)>"},
{new MapType(Type.INT, Type.INT), "map<int(11),int(11)>"},
{new StructType(Lists.newArrayList(Type.INT)), "struct<col1 int(11)>"},
};

for (Object[] tc : testCases) {
Type type = (Type) tc[0];
String name = (String) tc[1];
Assert.assertEquals(name, type.toMysqlColumnTypeString());
}
}

@Test
public void testMapSerialAndDeser() {
// map<int,struct<c1:int,cc1:string>>
Expand Down
3 changes: 3 additions & 0 deletions gensrc/thrift/FrontendService.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ struct TColumnDesc {
23: optional string dbName
24: optional string tableName
25: optional string columnDefault
// Let FE control the type, which makes it easier to modify and display complex types
26: optional string columnTypeStr
27: optional string dataType
}

// A column definition; used by CREATE TABLE and DESCRIBE <table> statements. A column
Expand Down

0 comments on commit 2833b67

Please sign in to comment.