Skip to content

Commit

Permalink
[Enhancement] information_schema.columns support generated column exp…
Browse files Browse the repository at this point in the history
…ression (backport #49712) (#49734)

Signed-off-by: srlch <linzichao@starrocks.com>
  • Loading branch information
srlch authored Aug 13, 2024
1 parent 7d24400 commit 8d4956f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 3 deletions.
9 changes: 8 additions & 1 deletion be/src/exec/schema_scanner/schema_columns_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,14 @@ Status SchemaColumnsScanner::fill_chunk(ChunkPtr* chunk) {
// GENERATION_EXPRESSION
{
ColumnPtr column = (*chunk)->get_column_by_slot_id(23);
fill_data_column_with_null(column.get());
if (_desc_result.columns[_column_index].columnDesc.__isset.generatedColumnExprStr &&
_desc_result.columns[_column_index].columnDesc.generatedColumnExprStr.size() != 0) {
std::string* str = &_desc_result.columns[_column_index].columnDesc.generatedColumnExprStr;
Slice value(str->c_str(), str->length());
fill_column_with_slot<TYPE_VARCHAR>(column.get(), (void*)&value);
} else {
fill_data_column_with_null(column.get());
}
}
break;
}
Expand Down
7 changes: 7 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/catalog/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,13 @@ public static Column read(DataInput in) throws IOException {
return GsonUtils.GSON.fromJson(json, Column.class);
}

public String generatedColumnExprToString() {
if (generatedColumnExprSerialized != null && generatedColumnExprSerialized.expressionSql != null) {
return SqlParser.parseSqlToExpr(generatedColumnExprSerialized.expressionSql, SqlModeHelper.MODE_DEFAULT).toSql();
}
return null;
}

@Override
public void gsonPostProcess() throws IOException {
if (generatedColumnExprSerialized != null && generatedColumnExprSerialized.expressionSql != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,10 @@ private boolean setColumnDesc(List<TColumnDef> columns, Table table, long limit,
}
desc.setDataType(column.getType().toMysqlDataTypeString());
desc.setColumnTypeStr(column.getType().toMysqlColumnTypeString());
String generatedColumnExprStr = column.generatedColumnExprToString();
if (generatedColumnExprStr != null && !generatedColumnExprStr.isEmpty()) {
desc.setGeneratedColumnExprStr(generatedColumnExprStr);
}
final TColumnDef colDef = new TColumnDef(desc);
final String comment = column.getComment();
if (comment != null) {
Expand Down
1 change: 1 addition & 0 deletions gensrc/thrift/FrontendService.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ struct TColumnDesc {
// Let FE control the type, which makes it easier to modify and display complex types
26: optional string columnTypeStr
27: optional string dataType
28: optional string generatedColumnExprStr
}

// A column definition; used by CREATE TABLE and DESCRIBE <table> statements. A column
Expand Down
53 changes: 52 additions & 1 deletion test/sql/test_materialized_column/R/test_materialized_column
Original file line number Diff line number Diff line change
Expand Up @@ -1074,4 +1074,55 @@ SELECT * FROM t;
-- !result
DROP DATABASE test_compaction_after_adding_generated_column;
-- result:
-- !result
-- !result
-- name: test_information_schema_generated_column
CREATE TABLE `t_information_schema_generated_column_1` (
`k` BIGINT NOT NULL COMMENT "",
`v` BIGINT AS k + 10
) ENGINE=OLAP
DUPLICATE KEY(`k`)
DISTRIBUTED BY HASH(`k`) BUCKETS 1
PROPERTIES (
"replication_num" = "1",
"in_memory" = "false",
"enable_persistent_index" = "false",
"replicated_storage" = "false"
);
-- result:
-- !result
CREATE TABLE `t_information_schema_generated_column_2` (
`k` BIGINT NOT NULL COMMENT "",
`v` BIGINT AS k + 10
) ENGINE=OLAP
DUPLICATE KEY(`k`)
DISTRIBUTED BY HASH(`k`) BUCKETS 1
PROPERTIES (
"replication_num" = "1",
"in_memory" = "false",
"enable_persistent_index" = "false",
"replicated_storage" = "false"
);
-- result:
-- !result
select COLUMN_NAME, GENERATION_EXPRESSION from information_schema.columns where TABLE_NAME = 't_information_schema_generated_column_1';
-- result:
k None
v k + 10
-- !result
select COLUMN_NAME, GENERATION_EXPRESSION from information_schema.columns where TABLE_NAME = 't_information_schema_generated_column_2';
-- result:
k None
v k + 10
-- !result
DROP TABLE t_information_schema_generated_column_1;
-- result:
-- !result
DROP TABLE t_information_schema_generated_column_2;
-- result:
-- !result
select COLUMN_NAME, GENERATION_EXPRESSION from information_schema.columns where TABLE_NAME = 't_information_schema_generated_column_1';
-- result:
-- !result
select COLUMN_NAME, GENERATION_EXPRESSION from information_schema.columns where TABLE_NAME = 't_information_schema_generated_column_2';
-- result:
-- !result
38 changes: 37 additions & 1 deletion test/sql/test_materialized_column/T/test_materialized_column
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,40 @@ SELECT sleep(30);

SELECT * FROM t;

DROP DATABASE test_compaction_after_adding_generated_column;
DROP DATABASE test_compaction_after_adding_generated_column;

-- name: test_information_schema_generated_column
CREATE TABLE `t_information_schema_generated_column_1` (
`k` BIGINT NOT NULL COMMENT "",
`v` BIGINT AS k + 10
) ENGINE=OLAP
DUPLICATE KEY(`k`)
DISTRIBUTED BY HASH(`k`) BUCKETS 1
PROPERTIES (
"replication_num" = "1",
"in_memory" = "false",
"enable_persistent_index" = "false",
"replicated_storage" = "false"
);

CREATE TABLE `t_information_schema_generated_column_2` (
`k` BIGINT NOT NULL COMMENT "",
`v` BIGINT AS k + 10
) ENGINE=OLAP
DUPLICATE KEY(`k`)
DISTRIBUTED BY HASH(`k`) BUCKETS 1
PROPERTIES (
"replication_num" = "1",
"in_memory" = "false",
"enable_persistent_index" = "false",
"replicated_storage" = "false"
);

select COLUMN_NAME, GENERATION_EXPRESSION from information_schema.columns where TABLE_NAME = 't_information_schema_generated_column_1';
select COLUMN_NAME, GENERATION_EXPRESSION from information_schema.columns where TABLE_NAME = 't_information_schema_generated_column_2';

DROP TABLE t_information_schema_generated_column_1;
DROP TABLE t_information_schema_generated_column_2;

select COLUMN_NAME, GENERATION_EXPRESSION from information_schema.columns where TABLE_NAME = 't_information_schema_generated_column_1';
select COLUMN_NAME, GENERATION_EXPRESSION from information_schema.columns where TABLE_NAME = 't_information_schema_generated_column_2';

0 comments on commit 8d4956f

Please sign in to comment.