diff --git a/src/catalog/src/system_schema/information_schema/key_column_usage.rs b/src/catalog/src/system_schema/information_schema/key_column_usage.rs index 56713dabba28..42cfa53fdb38 100644 --- a/src/catalog/src/system_schema/information_schema/key_column_usage.rs +++ b/src/catalog/src/system_schema/information_schema/key_column_usage.rs @@ -54,6 +54,10 @@ const INIT_CAPACITY: usize = 42; pub(crate) const PRI_CONSTRAINT_NAME: &str = "PRIMARY"; /// Time index constraint name pub(crate) const TIME_INDEX_CONSTRAINT_NAME: &str = "TIME INDEX"; +/// Inverted index constraint name +pub(crate) const INVERTED_INDEX_CONSTRAINT_NAME: &str = "INVERTED INDEX"; +/// Fulltext index constraint name +pub(crate) const FULLTEXT_INDEX_CONSTRAINT_NAME: &str = "FULLTEXT INDEX"; /// The virtual table implementation for `information_schema.KEY_COLUMN_USAGE`. pub(super) struct InformationSchemaKeyColumnUsage { @@ -216,14 +220,13 @@ impl InformationSchemaKeyColumnUsageBuilder { let mut stream = catalog_manager.tables(&catalog_name, &schema_name, None); while let Some(table) = stream.try_next().await? { - let mut primary_constraints = vec![]; - let table_info = table.table_info(); let table_name = &table_info.name; let keys = &table_info.meta.primary_key_indices; let schema = table.schema(); for (idx, column) in schema.column_schemas().iter().enumerate() { + let mut constraints = vec![]; if column.is_time_index() { self.add_key_column_usage( &predicates, @@ -236,30 +239,31 @@ impl InformationSchemaKeyColumnUsageBuilder { 1, //always 1 for time index ); } + // TODO(dimbtp): foreign key constraint not supported yet if keys.contains(&idx) { - primary_constraints.push(( - catalog_name.clone(), - schema_name.clone(), - table_name.to_string(), - column.name.clone(), - )); + constraints.push(PRI_CONSTRAINT_NAME); + } + if column.is_inverted_indexed() { + constraints.push(INVERTED_INDEX_CONSTRAINT_NAME); + } + + if column.has_fulltext_index_key() { + constraints.push(FULLTEXT_INDEX_CONSTRAINT_NAME); } - // TODO(dimbtp): foreign key constraint not supported yet - } - for (i, (catalog_name, schema_name, table_name, column_name)) in - primary_constraints.into_iter().enumerate() - { - self.add_key_column_usage( - &predicates, - &schema_name, - PRI_CONSTRAINT_NAME, - &catalog_name, - &schema_name, - &table_name, - &column_name, - i as u32 + 1, - ); + if !constraints.is_empty() { + let aggregated_constraints = constraints.join(", "); + self.add_key_column_usage( + &predicates, + &schema_name, + &aggregated_constraints, + &catalog_name, + &schema_name, + table_name, + &column.name, + idx as u32 + 1, + ); + } } } } diff --git a/src/datatypes/src/schema/column_schema.rs b/src/datatypes/src/schema/column_schema.rs index aee9efd9625d..7a96ab5e2bf2 100644 --- a/src/datatypes/src/schema/column_schema.rs +++ b/src/datatypes/src/schema/column_schema.rs @@ -164,6 +164,10 @@ impl ColumnSchema { .unwrap_or(false) } + pub fn has_fulltext_index_key(&self) -> bool { + self.metadata.contains_key(FULLTEXT_KEY) + } + pub fn has_inverted_index_key(&self) -> bool { self.metadata.contains_key(INVERTED_INDEX_KEY) } diff --git a/src/query/src/sql.rs b/src/query/src/sql.rs index 062bd8e14e18..3337503d097c 100644 --- a/src/query/src/sql.rs +++ b/src/query/src/sql.rs @@ -40,7 +40,7 @@ use common_recordbatch::RecordBatches; use common_time::timezone::get_timezone; use common_time::Timestamp; use datafusion::common::ScalarValue; -use datafusion::prelude::SessionContext; +use datafusion::prelude::{concat_ws, SessionContext}; use datafusion_expr::{case, col, lit, Expr}; use datatypes::prelude::*; use datatypes::schema::{ColumnDefaultConstraint, ColumnSchema, RawSchema, Schema}; @@ -400,6 +400,20 @@ pub async fn show_index( query_ctx.current_schema() }; + let fulltext_index_expr = case(col("constraint_name").like(lit("%FULLTEXT INDEX%"))) + .when(lit(true), lit("greptime-fulltext-index-v1")) + .otherwise(null()) + .context(error::PlanSqlSnafu)?; + + let inverted_index_expr = case( + col("constraint_name") + .like(lit("%INVERTED INDEX%")) + .or(col("constraint_name").like(lit("%PRIMARY%"))), + ) + .when(lit(true), lit("greptime-inverted-index-v1")) + .otherwise(null()) + .context(error::PlanSqlSnafu)?; + let select = vec![ // 1 as `Non_unique`: contain duplicates lit(1).alias(INDEX_NONT_UNIQUE_COLUMN), @@ -417,8 +431,11 @@ pub async fn show_index( .otherwise(lit(YES_STR)) .context(error::PlanSqlSnafu)? .alias(COLUMN_NULLABLE_COLUMN), - // TODO(dennis): maybe 'BTREE'? - lit("greptime-inverted-index-v1").alias(INDEX_INDEX_TYPE_COLUMN), + concat_ws( + lit(", "), + vec![inverted_index_expr.clone(), fulltext_index_expr.clone()], + ) + .alias(INDEX_INDEX_TYPE_COLUMN), lit("").alias(COLUMN_COMMENT_COLUMN), lit("").alias(INDEX_COMMENT_COLUMN), lit(YES_STR).alias(INDEX_VISIBLE_COLUMN), diff --git a/tests/cases/standalone/common/show/show_index.result b/tests/cases/standalone/common/show/show_index.result index 995da87c133d..6f179687dbb5 100644 --- a/tests/cases/standalone/common/show/show_index.result +++ b/tests/cases/standalone/common/show/show_index.result @@ -1,11 +1,15 @@ CREATE TABLE IF NOT EXISTS system_metrics ( host STRING, - idc STRING, + idc STRING FULLTEXT, cpu_util DOUBLE, memory_util DOUBLE, disk_util DOUBLE, + desc1 STRING, + desc2 STRING FULLTEXT, + desc3 STRING FULLTEXT, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(host, idc), + INVERTED INDEX(idc, desc1, desc2), TIME INDEX(ts) ); @@ -33,28 +37,34 @@ SHOW INDEX FROM test; +-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+ | test | 1 | PRIMARY | 1 | a | A | | | | YES | greptime-inverted-index-v1 | | | YES | | | test | 1 | PRIMARY | 2 | b | A | | | | YES | greptime-inverted-index-v1 | | | YES | | -| test | 1 | TIME INDEX | 1 | ts | A | | | | NO | greptime-inverted-index-v1 | | | YES | | +| test | 1 | TIME INDEX | 1 | ts | A | | | | NO | | | | YES | | +-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+ SHOW INDEX FROM system_metrics; -+----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+ -| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression | -+----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+ -| system_metrics | 1 | PRIMARY | 1 | host | A | | | | YES | greptime-inverted-index-v1 | | | YES | | -| system_metrics | 1 | PRIMARY | 2 | idc | A | | | | YES | greptime-inverted-index-v1 | | | YES | | -| system_metrics | 1 | TIME INDEX | 1 | ts | A | | | | NO | greptime-inverted-index-v1 | | | YES | | -+----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+ ++----------------+------------+-----------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+--------------------------------------------------------+---------+---------------+---------+------------+ +| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression | ++----------------+------------+-----------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+--------------------------------------------------------+---------+---------------+---------+------------+ +| system_metrics | 1 | INVERTED INDEX | 6 | desc1 | A | | | | YES | greptime-inverted-index-v1 | | | YES | | +| system_metrics | 1 | INVERTED INDEX, FULLTEXT INDEX | 7 | desc2 | A | | | | YES | greptime-inverted-index-v1, greptime-fulltext-index-v1 | | | YES | | +| system_metrics | 1 | FULLTEXT INDEX | 8 | desc3 | A | | | | YES | greptime-fulltext-index-v1 | | | YES | | +| system_metrics | 1 | PRIMARY | 1 | host | A | | | | YES | greptime-inverted-index-v1 | | | YES | | +| system_metrics | 1 | PRIMARY, INVERTED INDEX, FULLTEXT INDEX | 2 | idc | A | | | | YES | greptime-inverted-index-v1, greptime-fulltext-index-v1 | | | YES | | +| system_metrics | 1 | TIME INDEX | 1 | ts | A | | | | NO | | | | YES | | ++----------------+------------+-----------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+--------------------------------------------------------+---------+---------------+---------+------------+ SHOW INDEX FROM system_metrics in public; -+----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+ -| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression | -+----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+ -| system_metrics | 1 | PRIMARY | 1 | host | A | | | | YES | greptime-inverted-index-v1 | | | YES | | -| system_metrics | 1 | PRIMARY | 2 | idc | A | | | | YES | greptime-inverted-index-v1 | | | YES | | -| system_metrics | 1 | TIME INDEX | 1 | ts | A | | | | NO | greptime-inverted-index-v1 | | | YES | | -+----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+ ++----------------+------------+-----------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+--------------------------------------------------------+---------+---------------+---------+------------+ +| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression | ++----------------+------------+-----------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+--------------------------------------------------------+---------+---------------+---------+------------+ +| system_metrics | 1 | INVERTED INDEX | 6 | desc1 | A | | | | YES | greptime-inverted-index-v1 | | | YES | | +| system_metrics | 1 | INVERTED INDEX, FULLTEXT INDEX | 7 | desc2 | A | | | | YES | greptime-inverted-index-v1, greptime-fulltext-index-v1 | | | YES | | +| system_metrics | 1 | FULLTEXT INDEX | 8 | desc3 | A | | | | YES | greptime-fulltext-index-v1 | | | YES | | +| system_metrics | 1 | PRIMARY | 1 | host | A | | | | YES | greptime-inverted-index-v1 | | | YES | | +| system_metrics | 1 | PRIMARY, INVERTED INDEX, FULLTEXT INDEX | 2 | idc | A | | | | YES | greptime-inverted-index-v1, greptime-fulltext-index-v1 | | | YES | | +| system_metrics | 1 | TIME INDEX | 1 | ts | A | | | | NO | | | | YES | | ++----------------+------------+-----------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+--------------------------------------------------------+---------+---------------+---------+------------+ SHOW INDEX FROM system_metrics like '%util%'; @@ -62,11 +72,11 @@ Error: 1001(Unsupported), SQL statement is not supported, keyword: like SHOW INDEX FROM system_metrics WHERE Key_name = 'TIME INDEX'; -+----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+ -| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression | -+----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+ -| system_metrics | 1 | TIME INDEX | 1 | ts | A | | | | NO | greptime-inverted-index-v1 | | | YES | | -+----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+ ++----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ +| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression | ++----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ +| system_metrics | 1 | TIME INDEX | 1 | ts | A | | | | NO | | | | YES | | ++----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+ DROP TABLE system_metrics; diff --git a/tests/cases/standalone/common/show/show_index.sql b/tests/cases/standalone/common/show/show_index.sql index 3f804db3845f..f0c5894a0ad7 100644 --- a/tests/cases/standalone/common/show/show_index.sql +++ b/tests/cases/standalone/common/show/show_index.sql @@ -1,11 +1,15 @@ CREATE TABLE IF NOT EXISTS system_metrics ( host STRING, - idc STRING, + idc STRING FULLTEXT, cpu_util DOUBLE, memory_util DOUBLE, disk_util DOUBLE, + desc1 STRING, + desc2 STRING FULLTEXT, + desc3 STRING FULLTEXT, ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(host, idc), + INVERTED INDEX(idc, desc1, desc2), TIME INDEX(ts) );