diff --git a/src/catalog/src/information_schema.rs b/src/catalog/src/information_schema.rs index 92427552425f..f806e307b757 100644 --- a/src/catalog/src/information_schema.rs +++ b/src/catalog/src/information_schema.rs @@ -13,6 +13,7 @@ // limitations under the License. mod columns; +mod key_column_usage; mod memory_table; mod schemata; mod table_names; @@ -41,6 +42,7 @@ pub use table_names::*; use self::columns::InformationSchemaColumns; use crate::error::Result; +use crate::information_schema::key_column_usage::InformationSchemaKeyColumnUsage; use crate::information_schema::memory_table::{get_schema_columns, MemoryTable}; use crate::information_schema::schemata::InformationSchemaSchemata; use crate::information_schema::tables::InformationSchemaTables; @@ -131,6 +133,10 @@ impl InformationSchemaProvider { tables.insert(TABLES.to_string(), self.build_table(TABLES).unwrap()); tables.insert(SCHEMATA.to_string(), self.build_table(SCHEMATA).unwrap()); tables.insert(COLUMNS.to_string(), self.build_table(COLUMNS).unwrap()); + tables.insert( + KEY_COLUMN_USAGE.to_string(), + self.build_table(KEY_COLUMN_USAGE).unwrap(), + ); // Add memory tables for name in MEMORY_TABLES.iter() { @@ -173,6 +179,10 @@ impl InformationSchemaProvider { CHECK_CONSTRAINTS => setup_memory_table!(CHECK_CONSTRAINTS), EVENTS => setup_memory_table!(EVENTS), FILES => setup_memory_table!(FILES), + KEY_COLUMN_USAGE => Some(Arc::new(InformationSchemaKeyColumnUsage::new( + self.catalog_name.clone(), + self.catalog_manager.clone(), + )) as _), SCHEMATA => Some(Arc::new(InformationSchemaSchemata::new( self.catalog_name.clone(), self.catalog_manager.clone(), diff --git a/src/catalog/src/information_schema/key_column_usage.rs b/src/catalog/src/information_schema/key_column_usage.rs new file mode 100644 index 000000000000..7c8a4995acfd --- /dev/null +++ b/src/catalog/src/information_schema/key_column_usage.rs @@ -0,0 +1,338 @@ +// Copyright 2023 Greptime Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::sync::{Arc, Weak}; + +use arrow_schema::SchemaRef as ArrowSchemaRef; +use common_catalog::consts::INFORMATION_SCHEMA_KEY_COLUMN_USAGE_TABLE_ID; +use common_error::ext::BoxedError; +use common_query::physical_plan::TaskContext; +use common_recordbatch::adapter::RecordBatchStreamAdapter; +use common_recordbatch::{RecordBatch, SendableRecordBatchStream}; +use datafusion::physical_plan::stream::RecordBatchStreamAdapter as DfRecordBatchStreamAdapter; +use datafusion::physical_plan::streaming::PartitionStream as DfPartitionStream; +use datafusion::physical_plan::SendableRecordBatchStream as DfSendableRecordBatchStream; +use datatypes::prelude::{ConcreteDataType, ScalarVectorBuilder, VectorRef}; +use datatypes::schema::{ColumnSchema, Schema, SchemaRef}; +use datatypes::vectors::{StringVectorBuilder, UInt32VectorBuilder}; +use snafu::{OptionExt, ResultExt}; +use store_api::storage::TableId; + +use super::KEY_COLUMN_USAGE; +use crate::error::{ + CreateRecordBatchSnafu, InternalSnafu, Result, UpgradeWeakCatalogManagerRefSnafu, +}; +use crate::information_schema::InformationTable; +use crate::CatalogManager; + +/// The virtual table implementation for `information_schema.KEY_COLUMN_USAGE`. +pub(super) struct InformationSchemaKeyColumnUsage { + schema: SchemaRef, + catalog_name: String, + catalog_manager: Weak, +} + +impl InformationSchemaKeyColumnUsage { + pub(super) fn new(catalog_name: String, catalog_manager: Weak) -> Self { + Self { + schema: Self::schema(), + catalog_name, + catalog_manager, + } + } + + pub(crate) fn schema() -> SchemaRef { + Arc::new(Schema::new(vec![ + ColumnSchema::new( + "constraint_catalog", + ConcreteDataType::string_datatype(), + false, + ), + ColumnSchema::new( + "constraint_schema", + ConcreteDataType::string_datatype(), + false, + ), + ColumnSchema::new( + "constraint_name", + ConcreteDataType::string_datatype(), + false, + ), + ColumnSchema::new("table_catalog", ConcreteDataType::string_datatype(), false), + ColumnSchema::new("table_schema", ConcreteDataType::string_datatype(), false), + ColumnSchema::new("table_name", ConcreteDataType::string_datatype(), false), + ColumnSchema::new("column_name", ConcreteDataType::string_datatype(), false), + ColumnSchema::new( + "ordinal_position", + ConcreteDataType::uint32_datatype(), + false, + ), + ColumnSchema::new( + "position_in_unique_constraint", + ConcreteDataType::uint32_datatype(), + true, + ), + ColumnSchema::new( + "referenced_table_schema", + ConcreteDataType::string_datatype(), + true, + ), + ColumnSchema::new( + "referenced_table_name", + ConcreteDataType::string_datatype(), + true, + ), + ColumnSchema::new( + "referenced_column_name", + ConcreteDataType::string_datatype(), + true, + ), + ])) + } + + fn builder(&self) -> InformationSchemaKeyColumnUsageBuilder { + InformationSchemaKeyColumnUsageBuilder::new( + self.schema.clone(), + self.catalog_name.clone(), + self.catalog_manager.clone(), + ) + } +} + +impl InformationTable for InformationSchemaKeyColumnUsage { + fn table_id(&self) -> TableId { + INFORMATION_SCHEMA_KEY_COLUMN_USAGE_TABLE_ID + } + + fn table_name(&self) -> &'static str { + KEY_COLUMN_USAGE + } + + fn schema(&self) -> SchemaRef { + self.schema.clone() + } + + fn to_stream(&self) -> Result { + let schema = self.schema.arrow_schema().clone(); + let mut builder = self.builder(); + let stream = Box::pin(DfRecordBatchStreamAdapter::new( + schema, + futures::stream::once(async move { + builder + .make_key_column_usage() + .await + .map(|x| x.into_df_record_batch()) + .map_err(Into::into) + }), + )); + Ok(Box::pin( + RecordBatchStreamAdapter::try_new(stream) + .map_err(BoxedError::new) + .context(InternalSnafu)?, + )) + } +} + +/// Builds the `information_schema.KEY_COLUMN_USAGE` table row by row +/// +/// Columns are based on +struct InformationSchemaKeyColumnUsageBuilder { + schema: SchemaRef, + catalog_name: String, + catalog_manager: Weak, + + constraint_catalog: StringVectorBuilder, + constraint_schema: StringVectorBuilder, + constraint_name: StringVectorBuilder, + table_catalog: StringVectorBuilder, + table_schema: StringVectorBuilder, + table_name: StringVectorBuilder, + column_name: StringVectorBuilder, + ordinal_position: UInt32VectorBuilder, + position_in_unique_constraint: UInt32VectorBuilder, + referenced_table_schema: StringVectorBuilder, + referenced_table_name: StringVectorBuilder, + referenced_column_name: StringVectorBuilder, +} + +impl InformationSchemaKeyColumnUsageBuilder { + fn new( + schema: SchemaRef, + catalog_name: String, + catalog_manager: Weak, + ) -> Self { + Self { + schema, + catalog_name, + catalog_manager, + constraint_catalog: StringVectorBuilder::with_capacity(42), + constraint_schema: StringVectorBuilder::with_capacity(42), + constraint_name: StringVectorBuilder::with_capacity(42), + table_catalog: StringVectorBuilder::with_capacity(42), + table_schema: StringVectorBuilder::with_capacity(42), + table_name: StringVectorBuilder::with_capacity(42), + column_name: StringVectorBuilder::with_capacity(42), + ordinal_position: UInt32VectorBuilder::with_capacity(42), + position_in_unique_constraint: UInt32VectorBuilder::with_capacity(42), + referenced_table_schema: StringVectorBuilder::with_capacity(42), + referenced_table_name: StringVectorBuilder::with_capacity(42), + referenced_column_name: StringVectorBuilder::with_capacity(42), + } + } + + /// Construct the `information_schema.KEY_COLUMN_USAGE` virtual table + async fn make_key_column_usage(&mut self) -> Result { + let catalog_name = self.catalog_name.clone(); + let catalog_manager = self + .catalog_manager + .upgrade() + .context(UpgradeWeakCatalogManagerRefSnafu)?; + + let mut time_index_constraints = vec![]; + let mut primary_constraints = vec![]; + + for schema_name in catalog_manager.schema_names(&catalog_name).await? { + if !catalog_manager + .schema_exists(&catalog_name, &schema_name) + .await? + { + continue; + } + + for table_name in catalog_manager + .table_names(&catalog_name, &schema_name) + .await? + { + if let Some(table) = catalog_manager + .table(&catalog_name, &schema_name, &table_name) + .await? + { + let keys = &table.table_info().meta.primary_key_indices; + let schema = table.schema(); + + for (idx, column) in schema.column_schemas().iter().enumerate() { + if column.is_time_index() { + time_index_constraints.push(( + schema_name.clone(), + table_name.clone(), + column.name.clone(), + )); + } + if keys.contains(&idx) { + primary_constraints.push(( + schema_name.clone(), + table_name.clone(), + column.name.clone(), + )); + } + // TODO(dimbtp): foreign key constraint not supported yet + } + } else { + unreachable!(); + } + } + } + + for (i, (schema_name, table_name, column_name)) in + time_index_constraints.into_iter().enumerate() + { + self.add_key_column_usage( + &schema_name, + "TIME INDEX", + &schema_name, + &table_name, + &column_name, + i as u32 + 1, + ); + } + for (i, (schema_name, table_name, column_name)) in + primary_constraints.into_iter().enumerate() + { + self.add_key_column_usage( + &schema_name, + "PRIMARY", + &schema_name, + &table_name, + &column_name, + i as u32 + 1, + ); + } + + self.finish() + } + + // TODO(dimbtp): Foreign key constraint has not `None` value for last 4 + // fields, but it is not supported yet. + fn add_key_column_usage( + &mut self, + constraint_schema: &str, + constraint_name: &str, + table_schema: &str, + table_name: &str, + column_name: &str, + ordinal_position: u32, + ) { + self.constraint_catalog.push(Some("def")); + self.constraint_schema.push(Some(constraint_schema)); + self.constraint_name.push(Some(constraint_name)); + self.table_catalog.push(Some("def")); + self.table_schema.push(Some(table_schema)); + self.table_name.push(Some(table_name)); + self.column_name.push(Some(column_name)); + self.ordinal_position.push(Some(ordinal_position)); + self.position_in_unique_constraint.push(None); + self.referenced_table_schema.push(None); + self.referenced_table_name.push(None); + self.referenced_column_name.push(None); + } + + fn finish(&mut self) -> Result { + let columns: Vec = vec![ + Arc::new(self.constraint_catalog.finish()), + Arc::new(self.constraint_schema.finish()), + Arc::new(self.constraint_name.finish()), + Arc::new(self.table_catalog.finish()), + Arc::new(self.table_schema.finish()), + Arc::new(self.table_name.finish()), + Arc::new(self.column_name.finish()), + Arc::new(self.ordinal_position.finish()), + Arc::new(self.position_in_unique_constraint.finish()), + Arc::new(self.referenced_table_schema.finish()), + Arc::new(self.referenced_table_name.finish()), + Arc::new(self.referenced_column_name.finish()), + ]; + RecordBatch::new(self.schema.clone(), columns).context(CreateRecordBatchSnafu) + } +} + +impl DfPartitionStream for InformationSchemaKeyColumnUsage { + fn schema(&self) -> &ArrowSchemaRef { + self.schema.arrow_schema() + } + + fn execute(&self, _: Arc) -> DfSendableRecordBatchStream { + let schema = self.schema.arrow_schema().clone(); + let mut builder = self.builder(); + Box::pin(DfRecordBatchStreamAdapter::new( + schema, + futures::stream::once(async move { + builder + .make_key_column_usage() + .await + .map(|x| x.into_df_record_batch()) + .map_err(Into::into) + }), + )) + } +} diff --git a/src/catalog/src/information_schema/table_names.rs b/src/catalog/src/information_schema/table_names.rs index bfdc56d89217..e1b23b4f6938 100644 --- a/src/catalog/src/information_schema/table_names.rs +++ b/src/catalog/src/information_schema/table_names.rs @@ -25,5 +25,6 @@ pub const COLLATIONS: &str = "collations"; pub const COLLATION_CHARACTER_SET_APPLICABILITY: &str = "collation_character_set_applicability"; pub const CHECK_CONSTRAINTS: &str = "check_constraints"; pub const EVENTS: &str = "events"; +pub const KEY_COLUMN_USAGE: &str = "key_column_usage"; pub const FILES: &str = "files"; pub const SCHEMATA: &str = "schemata"; diff --git a/src/common/catalog/src/consts.rs b/src/common/catalog/src/consts.rs index 9e8b9e4a0768..6af76120a5f8 100644 --- a/src/common/catalog/src/consts.rs +++ b/src/common/catalog/src/consts.rs @@ -58,6 +58,8 @@ pub const INFORMATION_SCHEMA_EVENTS_TABLE_ID: u32 = 13; pub const INFORMATION_SCHEMA_FILES_TABLE_ID: u32 = 14; /// id for information_schema.SCHEMATA pub const INFORMATION_SCHEMA_SCHEMATA_TABLE_ID: u32 = 15; +/// id for information_schema.KEY_COLUMN_USAGE +pub const INFORMATION_SCHEMA_KEY_COLUMN_USAGE_TABLE_ID: u32 = 16; /// ----- End of information_schema tables ----- pub const MITO_ENGINE: &str = "mito"; diff --git a/tests/cases/standalone/common/show/show_databases_tables.result b/tests/cases/standalone/common/show/show_databases_tables.result index 8a1e606ad110..76a69d56aa8b 100644 --- a/tests/cases/standalone/common/show/show_databases_tables.result +++ b/tests/cases/standalone/common/show/show_databases_tables.result @@ -31,6 +31,7 @@ show tables; | engines | | events | | files | +| key_column_usage | | schemata | | tables | +---------------------------------------+ diff --git a/tests/cases/standalone/common/system/information_schema.result b/tests/cases/standalone/common/system/information_schema.result index a97660ee5839..a119b028a344 100644 --- a/tests/cases/standalone/common/system/information_schema.result +++ b/tests/cases/standalone/common/system/information_schema.result @@ -23,6 +23,7 @@ order by table_schema, table_name; | greptime | information_schema | engines | LOCAL TEMPORARY | 5 | | | greptime | information_schema | events | LOCAL TEMPORARY | 13 | | | greptime | information_schema | files | LOCAL TEMPORARY | 14 | | +| greptime | information_schema | key_column_usage | LOCAL TEMPORARY | 16 | | | greptime | information_schema | schemata | LOCAL TEMPORARY | 15 | | | greptime | information_schema | tables | LOCAL TEMPORARY | 3 | | | greptime | public | numbers | LOCAL TEMPORARY | 2 | test_engine | @@ -30,132 +31,144 @@ order by table_schema, table_name; select * from information_schema.columns order by table_schema, table_name; -+---------------+--------------------+---------------------------------------+----------------------------+-----------+---------------+----------------+-------------+-------------+----------------+ -| table_catalog | table_schema | table_name | column_name | data_type | semantic_type | column_default | is_nullable | column_type | column_comment | -+---------------+--------------------+---------------------------------------+----------------------------+-----------+---------------+----------------+-------------+-------------+----------------+ -| greptime | information_schema | build_info | git_branch | String | FIELD | | No | String | | -| greptime | information_schema | build_info | git_commit | String | FIELD | | No | String | | -| greptime | information_schema | build_info | git_commit_short | String | FIELD | | No | String | | -| greptime | information_schema | build_info | git_dirty | String | FIELD | | No | String | | -| greptime | information_schema | build_info | pkg_version | String | FIELD | | No | String | | -| greptime | information_schema | character_sets | default_collate_name | String | FIELD | | No | String | | -| greptime | information_schema | character_sets | character_set_name | String | FIELD | | No | String | | -| greptime | information_schema | character_sets | maxlen | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | character_sets | description | String | FIELD | | No | String | | -| greptime | information_schema | check_constraints | check_clause | String | FIELD | | No | String | | -| greptime | information_schema | check_constraints | constraint_name | String | FIELD | | No | String | | -| greptime | information_schema | check_constraints | constraint_schema | String | FIELD | | No | String | | -| greptime | information_schema | check_constraints | constraint_catalog | String | FIELD | | No | String | | -| greptime | information_schema | collation_character_set_applicability | character_set_name | String | FIELD | | No | String | | -| greptime | information_schema | collation_character_set_applicability | collation_name | String | FIELD | | No | String | | -| greptime | information_schema | collations | collation_name | String | FIELD | | No | String | | -| greptime | information_schema | collations | sortlen | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | collations | character_set_name | String | FIELD | | No | String | | -| greptime | information_schema | collations | id | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | collations | is_default | String | FIELD | | No | String | | -| greptime | information_schema | collations | is_compiled | String | FIELD | | No | String | | -| greptime | information_schema | column_privileges | table_schema | String | FIELD | | No | String | | -| greptime | information_schema | column_privileges | table_catalog | String | FIELD | | No | String | | -| greptime | information_schema | column_privileges | grantee | String | FIELD | | No | String | | -| greptime | information_schema | column_privileges | table_name | String | FIELD | | No | String | | -| greptime | information_schema | column_privileges | column_name | String | FIELD | | No | String | | -| greptime | information_schema | column_privileges | privilege_type | String | FIELD | | No | String | | -| greptime | information_schema | column_privileges | is_grantable | String | FIELD | | No | String | | -| greptime | information_schema | column_statistics | histogram | String | FIELD | | No | String | | -| greptime | information_schema | column_statistics | schema_name | String | FIELD | | No | String | | -| greptime | information_schema | column_statistics | table_name | String | FIELD | | No | String | | -| greptime | information_schema | column_statistics | column_name | String | FIELD | | No | String | | -| greptime | information_schema | columns | column_type | String | FIELD | | No | String | | -| greptime | information_schema | columns | column_comment | String | FIELD | | Yes | String | | -| greptime | information_schema | columns | table_schema | String | FIELD | | No | String | | -| greptime | information_schema | columns | is_nullable | String | FIELD | | No | String | | -| greptime | information_schema | columns | column_default | String | FIELD | | Yes | String | | -| greptime | information_schema | columns | semantic_type | String | FIELD | | No | String | | -| greptime | information_schema | columns | table_catalog | String | FIELD | | No | String | | -| greptime | information_schema | columns | data_type | String | FIELD | | No | String | | -| greptime | information_schema | columns | column_name | String | FIELD | | No | String | | -| greptime | information_schema | columns | table_name | String | FIELD | | No | String | | -| greptime | information_schema | engines | savepoints | String | FIELD | | No | String | | -| greptime | information_schema | engines | xa | String | FIELD | | No | String | | -| greptime | information_schema | engines | transactions | String | FIELD | | No | String | | -| greptime | information_schema | engines | comment | String | FIELD | | No | String | | -| greptime | information_schema | engines | support | String | FIELD | | No | String | | -| greptime | information_schema | engines | engine | String | FIELD | | No | String | | -| greptime | information_schema | events | event_definition | String | FIELD | | No | String | | -| greptime | information_schema | events | last_executed | DateTime | FIELD | | No | DateTime | | -| greptime | information_schema | events | database_collation | String | FIELD | | No | String | | -| greptime | information_schema | events | collation_connection | String | FIELD | | No | String | | -| greptime | information_schema | events | character_set_client | String | FIELD | | No | String | | -| greptime | information_schema | events | originator | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | events | event_catalog | String | FIELD | | No | String | | -| greptime | information_schema | events | event_schema | String | FIELD | | No | String | | -| greptime | information_schema | events | event_name | String | FIELD | | No | String | | -| greptime | information_schema | events | definer | String | FIELD | | No | String | | -| greptime | information_schema | events | time_zone | String | FIELD | | No | String | | -| greptime | information_schema | events | event_body | String | FIELD | | No | String | | -| greptime | information_schema | events | event_comment | String | FIELD | | No | String | | -| greptime | information_schema | events | event_type | String | FIELD | | No | String | | -| greptime | information_schema | events | execute_at | DateTime | FIELD | | No | DateTime | | -| greptime | information_schema | events | interval_value | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | events | interval_field | String | FIELD | | No | String | | -| greptime | information_schema | events | sql_mode | String | FIELD | | No | String | | -| greptime | information_schema | events | starts | DateTime | FIELD | | No | DateTime | | -| greptime | information_schema | events | ends | DateTime | FIELD | | No | DateTime | | -| greptime | information_schema | events | status | String | FIELD | | No | String | | -| greptime | information_schema | events | on_completion | String | FIELD | | No | String | | -| greptime | information_schema | events | created | DateTime | FIELD | | No | DateTime | | -| greptime | information_schema | events | last_altered | DateTime | FIELD | | No | DateTime | | -| greptime | information_schema | files | logfile_group_name | String | FIELD | | No | String | | -| greptime | information_schema | files | data_free | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | extra | String | FIELD | | No | String | | -| greptime | information_schema | files | status | String | FIELD | | No | String | | -| greptime | information_schema | files | checksum | String | FIELD | | No | String | | -| greptime | information_schema | files | check_time | DateTime | FIELD | | No | DateTime | | -| greptime | information_schema | files | file_id | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | file_name | String | FIELD | | No | String | | -| greptime | information_schema | files | file_type | String | FIELD | | No | String | | -| greptime | information_schema | files | tablespace_name | String | FIELD | | No | String | | -| greptime | information_schema | files | table_catalog | String | FIELD | | No | String | | -| greptime | information_schema | files | table_schema | String | FIELD | | No | String | | -| greptime | information_schema | files | table_name | String | FIELD | | No | String | | -| greptime | information_schema | files | update_time | DateTime | FIELD | | No | DateTime | | -| greptime | information_schema | files | logfile_group_number | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | engine | String | FIELD | | No | String | | -| greptime | information_schema | files | fulltext_keys | String | FIELD | | No | String | | -| greptime | information_schema | files | deleted_rows | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | update_count | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | free_extents | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | total_extents | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | extent_size | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | initial_size | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | maximum_size | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | autoextend_size | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | creation_time | DateTime | FIELD | | No | DateTime | | -| greptime | information_schema | files | last_update_time | DateTime | FIELD | | No | DateTime | | -| greptime | information_schema | files | last_access_time | DateTime | FIELD | | No | DateTime | | -| greptime | information_schema | files | recover_time | DateTime | FIELD | | No | DateTime | | -| greptime | information_schema | files | transaction_counter | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | version | String | FIELD | | No | String | | -| greptime | information_schema | files | row_format | String | FIELD | | No | String | | -| greptime | information_schema | files | table_rows | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | avg_row_length | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | data_length | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | max_data_length | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | index_length | Int64 | FIELD | | No | Int64 | | -| greptime | information_schema | files | create_time | DateTime | FIELD | | No | DateTime | | -| greptime | information_schema | schemata | catalog_name | String | FIELD | | No | String | | -| greptime | information_schema | schemata | schema_name | String | FIELD | | No | String | | -| greptime | information_schema | schemata | default_character_set_name | String | FIELD | | No | String | | -| greptime | information_schema | schemata | default_collation_name | String | FIELD | | No | String | | -| greptime | information_schema | schemata | sql_path | String | FIELD | | Yes | String | | -| greptime | information_schema | tables | engine | String | FIELD | | Yes | String | | -| greptime | information_schema | tables | table_catalog | String | FIELD | | No | String | | -| greptime | information_schema | tables | table_id | UInt32 | FIELD | | Yes | UInt32 | | -| greptime | information_schema | tables | table_type | String | FIELD | | No | String | | -| greptime | information_schema | tables | table_name | String | FIELD | | No | String | | -| greptime | information_schema | tables | table_schema | String | FIELD | | No | String | | -| greptime | public | numbers | number | UInt32 | TAG | | No | UInt32 | | -+---------------+--------------------+---------------------------------------+----------------------------+-----------+---------------+----------------+-------------+-------------+----------------+ ++---------------+--------------------+---------------------------------------+-------------------------------+-----------+---------------+----------------+-------------+-------------+----------------+ +| table_catalog | table_schema | table_name | column_name | data_type | semantic_type | column_default | is_nullable | column_type | column_comment | ++---------------+--------------------+---------------------------------------+-------------------------------+-----------+---------------+----------------+-------------+-------------+----------------+ +| greptime | information_schema | build_info | git_branch | String | FIELD | | No | String | | +| greptime | information_schema | build_info | git_commit | String | FIELD | | No | String | | +| greptime | information_schema | build_info | git_commit_short | String | FIELD | | No | String | | +| greptime | information_schema | build_info | git_dirty | String | FIELD | | No | String | | +| greptime | information_schema | build_info | pkg_version | String | FIELD | | No | String | | +| greptime | information_schema | character_sets | description | String | FIELD | | No | String | | +| greptime | information_schema | character_sets | character_set_name | String | FIELD | | No | String | | +| greptime | information_schema | character_sets | default_collate_name | String | FIELD | | No | String | | +| greptime | information_schema | character_sets | maxlen | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | check_constraints | check_clause | String | FIELD | | No | String | | +| greptime | information_schema | check_constraints | constraint_name | String | FIELD | | No | String | | +| greptime | information_schema | check_constraints | constraint_schema | String | FIELD | | No | String | | +| greptime | information_schema | check_constraints | constraint_catalog | String | FIELD | | No | String | | +| greptime | information_schema | collation_character_set_applicability | character_set_name | String | FIELD | | No | String | | +| greptime | information_schema | collation_character_set_applicability | collation_name | String | FIELD | | No | String | | +| greptime | information_schema | collations | collation_name | String | FIELD | | No | String | | +| greptime | information_schema | collations | character_set_name | String | FIELD | | No | String | | +| greptime | information_schema | collations | id | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | collations | is_default | String | FIELD | | No | String | | +| greptime | information_schema | collations | is_compiled | String | FIELD | | No | String | | +| greptime | information_schema | collations | sortlen | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | column_privileges | table_schema | String | FIELD | | No | String | | +| greptime | information_schema | column_privileges | grantee | String | FIELD | | No | String | | +| greptime | information_schema | column_privileges | table_catalog | String | FIELD | | No | String | | +| greptime | information_schema | column_privileges | table_name | String | FIELD | | No | String | | +| greptime | information_schema | column_privileges | column_name | String | FIELD | | No | String | | +| greptime | information_schema | column_privileges | privilege_type | String | FIELD | | No | String | | +| greptime | information_schema | column_privileges | is_grantable | String | FIELD | | No | String | | +| greptime | information_schema | column_statistics | histogram | String | FIELD | | No | String | | +| greptime | information_schema | column_statistics | schema_name | String | FIELD | | No | String | | +| greptime | information_schema | column_statistics | table_name | String | FIELD | | No | String | | +| greptime | information_schema | column_statistics | column_name | String | FIELD | | No | String | | +| greptime | information_schema | columns | column_type | String | FIELD | | No | String | | +| greptime | information_schema | columns | table_catalog | String | FIELD | | No | String | | +| greptime | information_schema | columns | column_comment | String | FIELD | | Yes | String | | +| greptime | information_schema | columns | is_nullable | String | FIELD | | No | String | | +| greptime | information_schema | columns | column_default | String | FIELD | | Yes | String | | +| greptime | information_schema | columns | semantic_type | String | FIELD | | No | String | | +| greptime | information_schema | columns | data_type | String | FIELD | | No | String | | +| greptime | information_schema | columns | column_name | String | FIELD | | No | String | | +| greptime | information_schema | columns | table_name | String | FIELD | | No | String | | +| greptime | information_schema | columns | table_schema | String | FIELD | | No | String | | +| greptime | information_schema | engines | xa | String | FIELD | | No | String | | +| greptime | information_schema | engines | transactions | String | FIELD | | No | String | | +| greptime | information_schema | engines | comment | String | FIELD | | No | String | | +| greptime | information_schema | engines | support | String | FIELD | | No | String | | +| greptime | information_schema | engines | engine | String | FIELD | | No | String | | +| greptime | information_schema | engines | savepoints | String | FIELD | | No | String | | +| greptime | information_schema | events | starts | DateTime | FIELD | | No | DateTime | | +| greptime | information_schema | events | event_comment | String | FIELD | | No | String | | +| greptime | information_schema | events | database_collation | String | FIELD | | No | String | | +| greptime | information_schema | events | collation_connection | String | FIELD | | No | String | | +| greptime | information_schema | events | character_set_client | String | FIELD | | No | String | | +| greptime | information_schema | events | originator | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | events | event_catalog | String | FIELD | | No | String | | +| greptime | information_schema | events | event_schema | String | FIELD | | No | String | | +| greptime | information_schema | events | event_name | String | FIELD | | No | String | | +| greptime | information_schema | events | definer | String | FIELD | | No | String | | +| greptime | information_schema | events | time_zone | String | FIELD | | No | String | | +| greptime | information_schema | events | event_body | String | FIELD | | No | String | | +| greptime | information_schema | events | event_definition | String | FIELD | | No | String | | +| greptime | information_schema | events | event_type | String | FIELD | | No | String | | +| greptime | information_schema | events | execute_at | DateTime | FIELD | | No | DateTime | | +| greptime | information_schema | events | interval_value | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | events | interval_field | String | FIELD | | No | String | | +| greptime | information_schema | events | sql_mode | String | FIELD | | No | String | | +| greptime | information_schema | events | ends | DateTime | FIELD | | No | DateTime | | +| greptime | information_schema | events | status | String | FIELD | | No | String | | +| greptime | information_schema | events | on_completion | String | FIELD | | No | String | | +| greptime | information_schema | events | created | DateTime | FIELD | | No | DateTime | | +| greptime | information_schema | events | last_altered | DateTime | FIELD | | No | DateTime | | +| greptime | information_schema | events | last_executed | DateTime | FIELD | | No | DateTime | | +| greptime | information_schema | files | free_extents | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | row_format | String | FIELD | | No | String | | +| greptime | information_schema | files | extra | String | FIELD | | No | String | | +| greptime | information_schema | files | status | String | FIELD | | No | String | | +| greptime | information_schema | files | checksum | String | FIELD | | No | String | | +| greptime | information_schema | files | check_time | DateTime | FIELD | | No | DateTime | | +| greptime | information_schema | files | file_id | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | file_name | String | FIELD | | No | String | | +| greptime | information_schema | files | file_type | String | FIELD | | No | String | | +| greptime | information_schema | files | tablespace_name | String | FIELD | | No | String | | +| greptime | information_schema | files | table_catalog | String | FIELD | | No | String | | +| greptime | information_schema | files | table_schema | String | FIELD | | No | String | | +| greptime | information_schema | files | table_name | String | FIELD | | No | String | | +| greptime | information_schema | files | logfile_group_name | String | FIELD | | No | String | | +| greptime | information_schema | files | logfile_group_number | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | engine | String | FIELD | | No | String | | +| greptime | information_schema | files | fulltext_keys | String | FIELD | | No | String | | +| greptime | information_schema | files | deleted_rows | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | update_count | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | update_time | DateTime | FIELD | | No | DateTime | | +| greptime | information_schema | files | total_extents | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | extent_size | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | initial_size | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | maximum_size | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | autoextend_size | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | creation_time | DateTime | FIELD | | No | DateTime | | +| greptime | information_schema | files | last_update_time | DateTime | FIELD | | No | DateTime | | +| greptime | information_schema | files | last_access_time | DateTime | FIELD | | No | DateTime | | +| greptime | information_schema | files | recover_time | DateTime | FIELD | | No | DateTime | | +| greptime | information_schema | files | transaction_counter | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | version | String | FIELD | | No | String | | +| greptime | information_schema | files | create_time | DateTime | FIELD | | No | DateTime | | +| greptime | information_schema | files | table_rows | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | avg_row_length | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | data_length | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | max_data_length | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | index_length | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | files | data_free | Int64 | FIELD | | No | Int64 | | +| greptime | information_schema | key_column_usage | ordinal_position | UInt32 | FIELD | | No | UInt32 | | +| greptime | information_schema | key_column_usage | constraint_schema | String | FIELD | | No | String | | +| greptime | information_schema | key_column_usage | referenced_column_name | String | FIELD | | Yes | String | | +| greptime | information_schema | key_column_usage | referenced_table_name | String | FIELD | | Yes | String | | +| greptime | information_schema | key_column_usage | referenced_table_schema | String | FIELD | | Yes | String | | +| greptime | information_schema | key_column_usage | position_in_unique_constraint | UInt32 | FIELD | | Yes | UInt32 | | +| greptime | information_schema | key_column_usage | constraint_catalog | String | FIELD | | No | String | | +| greptime | information_schema | key_column_usage | column_name | String | FIELD | | No | String | | +| greptime | information_schema | key_column_usage | table_schema | String | FIELD | | No | String | | +| greptime | information_schema | key_column_usage | table_catalog | String | FIELD | | No | String | | +| greptime | information_schema | key_column_usage | constraint_name | String | FIELD | | No | String | | +| greptime | information_schema | key_column_usage | table_name | String | FIELD | | No | String | | +| greptime | information_schema | schemata | catalog_name | String | FIELD | | No | String | | +| greptime | information_schema | schemata | default_collation_name | String | FIELD | | No | String | | +| greptime | information_schema | schemata | default_character_set_name | String | FIELD | | No | String | | +| greptime | information_schema | schemata | sql_path | String | FIELD | | Yes | String | | +| greptime | information_schema | schemata | schema_name | String | FIELD | | No | String | | +| greptime | information_schema | tables | table_schema | String | FIELD | | No | String | | +| greptime | information_schema | tables | table_catalog | String | FIELD | | No | String | | +| greptime | information_schema | tables | engine | String | FIELD | | Yes | String | | +| greptime | information_schema | tables | table_name | String | FIELD | | No | String | | +| greptime | information_schema | tables | table_type | String | FIELD | | No | String | | +| greptime | information_schema | tables | table_id | UInt32 | FIELD | | Yes | UInt32 | | +| greptime | public | numbers | number | UInt32 | TAG | | No | UInt32 | | ++---------------+--------------------+---------------------------------------+-------------------------------+-----------+---------------+----------------+-------------+-------------+----------------+ create database my_db; @@ -277,6 +290,34 @@ select count(*) from build_info; | 1 | +----------+ +desc table key_column_usage; + ++-------------------------------+--------+-----+------+---------+---------------+ +| Column | Type | Key | Null | Default | Semantic Type | ++-------------------------------+--------+-----+------+---------+---------------+ +| constraint_catalog | String | | NO | | FIELD | +| constraint_schema | String | | NO | | FIELD | +| constraint_name | String | | NO | | FIELD | +| table_catalog | String | | NO | | FIELD | +| table_schema | String | | NO | | FIELD | +| table_name | String | | NO | | FIELD | +| column_name | String | | NO | | FIELD | +| ordinal_position | UInt32 | | NO | | FIELD | +| position_in_unique_constraint | UInt32 | | YES | | FIELD | +| referenced_table_schema | String | | YES | | FIELD | +| referenced_table_name | String | | YES | | FIELD | +| referenced_column_name | String | | YES | | FIELD | ++-------------------------------+--------+-----+------+---------+---------------+ + +select * from key_column_usage; + ++--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+-----------------------+------------------------+ +| constraint_catalog | constraint_schema | constraint_name | table_catalog | table_schema | table_name | column_name | ordinal_position | position_in_unique_constraint | referenced_table_schema | referenced_table_name | referenced_column_name | ++--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+-----------------------+------------------------+ +| def | my_db | TIME INDEX | def | my_db | foo | ts | 1 | | | | | +| def | public | PRIMARY | def | public | numbers | number | 1 | | | | | ++--------------------+-------------------+-----------------+---------------+--------------+------------+-------------+------------------+-------------------------------+-------------------------+-----------------------+------------------------+ + -- tables not implemented desc table COLUMN_PRIVILEGES; diff --git a/tests/cases/standalone/common/system/information_schema.sql b/tests/cases/standalone/common/system/information_schema.sql index 6550fb544c61..ef0fbdeb7578 100644 --- a/tests/cases/standalone/common/system/information_schema.sql +++ b/tests/cases/standalone/common/system/information_schema.sql @@ -57,6 +57,10 @@ desc table build_info; select count(*) from build_info; +desc table key_column_usage; + +select * from key_column_usage; + -- tables not implemented desc table COLUMN_PRIVILEGES;