diff --git a/be/src/exec/vectorized/schema_scanner/schema_columns_scanner.cpp b/be/src/exec/vectorized/schema_scanner/schema_columns_scanner.cpp index 3649f81c72d58..598e9b43153e4 100644 --- a/be/src/exec/vectorized/schema_scanner/schema_columns_scanner.cpp +++ b/be/src/exec/vectorized/schema_scanner/schema_columns_scanner.cpp @@ -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(column.get(), (void*)&value); } break; @@ -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(column.get(), (void*)&value); } break; diff --git a/fe/fe-core/src/main/java/com/starrocks/catalog/ArrayType.java b/fe/fe-core/src/main/java/com/starrocks/catalog/ArrayType.java index 9c1120e44b175..b5a9a705c494e 100644 --- a/fe/fe-core/src/main/java/com/starrocks/catalog/ArrayType.java +++ b/fe/fe-core/src/main/java/com/starrocks/catalog/ArrayType.java @@ -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(); + } } diff --git a/fe/fe-core/src/main/java/com/starrocks/catalog/MapType.java b/fe/fe-core/src/main/java/com/starrocks/catalog/MapType.java index cc3b435d7968a..21c5b8f2fb800 100644 --- a/fe/fe-core/src/main/java/com/starrocks/catalog/MapType.java +++ b/fe/fe-core/src/main/java/com/starrocks/catalog/MapType.java @@ -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(); + } } diff --git a/fe/fe-core/src/main/java/com/starrocks/catalog/ScalarType.java b/fe/fe-core/src/main/java/com/starrocks/catalog/ScalarType.java index 255bd15944c39..cd3795c6eb13e 100644 --- a/fe/fe-core/src/main/java/com/starrocks/catalog/ScalarType.java +++ b/fe/fe-core/src/main/java/com/starrocks/catalog/ScalarType.java @@ -526,6 +526,7 @@ public String toSql(int depth) { case DATETIME: case HLL: case BITMAP: + case BINARY: case PERCENTILE: case JSON: case FUNCTION: @@ -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(); + } + } } diff --git a/fe/fe-core/src/main/java/com/starrocks/catalog/StructType.java b/fe/fe-core/src/main/java/com/starrocks/catalog/StructType.java index d9f598fa7322d..e2db9f991bcab 100644 --- a/fe/fe-core/src/main/java/com/starrocks/catalog/StructType.java +++ b/fe/fe-core/src/main/java/com/starrocks/catalog/StructType.java @@ -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(); + } } diff --git a/fe/fe-core/src/main/java/com/starrocks/catalog/Type.java b/fe/fe-core/src/main/java/com/starrocks/catalog/Type.java index cb8a5d2c57726..32497b37e8c7a 100644 --- a/fe/fe-core/src/main/java/com/starrocks/catalog/Type.java +++ b/fe/fe-core/src/main/java/com/starrocks/catalog/Type.java @@ -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"; + } } diff --git a/fe/fe-core/src/main/java/com/starrocks/service/FrontendServiceImpl.java b/fe/fe-core/src/main/java/com/starrocks/service/FrontendServiceImpl.java index 53fbe19fcf390..c9407b1f9558f 100644 --- a/fe/fe-core/src/main/java/com/starrocks/service/FrontendServiceImpl.java +++ b/fe/fe-core/src/main/java/com/starrocks/service/FrontendServiceImpl.java @@ -755,6 +755,7 @@ private boolean setColumnDesc(List 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()); @@ -793,6 +794,40 @@ private boolean setColumnDesc(List 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; diff --git a/fe/fe-core/src/test/java/com/starrocks/catalog/TypeTest.java b/fe/fe-core/src/test/java/com/starrocks/catalog/TypeTest.java index 177208aab8699..431eefac32d24 100644 --- a/fe/fe-core/src/test/java/com/starrocks/catalog/TypeTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/catalog/TypeTest.java @@ -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"}, + {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.toMysqlColumnTypeString()); + } + } + @Test public void testMapSerialAndDeser() { // map> diff --git a/gensrc/thrift/FrontendService.thrift b/gensrc/thrift/FrontendService.thrift index 784ed652eb070..217a36f3309d8 100644 --- a/gensrc/thrift/FrontendService.thrift +++ b/gensrc/thrift/FrontendService.thrift @@ -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 statements. A column