Skip to content

Commit 96d89cf

Browse files
committed
feat(query): expose column ids in system columns
1 parent ec61b69 commit 96d89cf

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

src/query/storages/system/src/columns_table.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use databend_common_expression::TableDataType;
2525
use databend_common_expression::TableField;
2626
use databend_common_expression::TableSchemaRefExt;
2727
use databend_common_expression::infer_table_schema;
28+
use databend_common_expression::types::NumberDataType;
2829
use databend_common_expression::types::StringType;
30+
use databend_common_expression::types::UInt64Type;
2931
use databend_common_expression::utils::FromData;
3032
use databend_common_functions::BUILTIN_FUNCTIONS;
3133
use databend_common_meta_app::schema::CatalogInfo;
@@ -84,10 +86,12 @@ impl AsyncSystemTable for ColumnsTable {
8486
let mut default_exprs: Vec<String> = Vec::with_capacity(rows.len());
8587
let mut is_nullables: Vec<String> = Vec::with_capacity(rows.len());
8688
let mut comments: Vec<String> = Vec::with_capacity(rows.len());
89+
let mut column_ids: Vec<Option<u64>> = Vec::with_capacity(rows.len());
8790
for column_info in rows.into_iter() {
8891
names.push(column_info.column.name().clone());
8992
tables.push(column_info.table_name);
9093
databases.push(column_info.database_name);
94+
column_ids.push(column_info.column_id);
9195
types.push(column_info.column.data_type().wrapped_display());
9296
let data_type = column_info
9397
.column
@@ -123,6 +127,7 @@ impl AsyncSystemTable for ColumnsTable {
123127
StringType::from_data(default_exprs),
124128
StringType::from_data(is_nullables),
125129
StringType::from_data(comments),
130+
UInt64Type::from_opt_data(column_ids),
126131
]))
127132
}
128133
}
@@ -133,6 +138,7 @@ struct TableColumnInfo {
133138

134139
column: TableField,
135140
column_comment: String,
141+
column_id: Option<u64>,
136142
}
137143

138144
impl ColumnsTable {
@@ -149,6 +155,10 @@ impl ColumnsTable {
149155
TableField::new("default_expression", TableDataType::String),
150156
TableField::new("is_nullable", TableDataType::String),
151157
TableField::new("comment", TableDataType::String),
158+
TableField::new(
159+
"column_id",
160+
TableDataType::Nullable(Box::new(TableDataType::Number(NumberDataType::UInt64))),
161+
),
152162
]);
153163

154164
let table_info = TableInfo {
@@ -210,6 +220,9 @@ impl ColumnsTable {
210220
table_name: table.name().into(),
211221
column: field.clone(),
212222
column_comment: "".to_string(),
223+
// View output columns are inferred from the query plan here; their
224+
// ids are not stable table column ids.
225+
column_id: None,
213226
})
214227
}
215228
}
@@ -223,6 +236,7 @@ impl ColumnsTable {
223236
table_name: table.name().into(),
224237
column: field.clone(),
225238
column_comment: "".to_string(),
239+
column_id: Some(field.column_id() as u64),
226240
})
227241
}
228242
}
@@ -253,6 +267,7 @@ impl ColumnsTable {
253267
table_name: table.name().into(),
254268
column: field.clone(),
255269
column_comment: comment,
270+
column_id: Some(field.column_id() as u64),
256271
})
257272
}
258273
}

tests/sqllogictests/suites/base/01_system/01_0006_system_columns.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,22 @@ columntest tid2
4343
columntest tid3
4444
columntest tid4
4545

46+
query TI
47+
SELECT lower(name), column_id FROM system.columns WHERE database = 'default' AND table = 't' ORDER BY name
48+
----
49+
id 0
50+
4651
statement ok
4752
drop view if exists default.test_v_t;
4853

4954
statement ok
5055
create view default.test_v_t as select * from default.t;
5156

57+
query TB
58+
SELECT lower(name), column_id IS NULL FROM system.columns WHERE database = 'default' AND table = 'test_v_t' ORDER BY name
59+
----
60+
id true
61+
5262
statement ok
5363
drop table default.t;
5464

0 commit comments

Comments
 (0)