Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: show index to display skipping index #5297

Merged
merged 4 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub(crate) const TIME_INDEX_CONSTRAINT_NAME: &str = "TIME INDEX";
pub(crate) const INVERTED_INDEX_CONSTRAINT_NAME: &str = "INVERTED INDEX";
/// Fulltext index constraint name
pub(crate) const FULLTEXT_INDEX_CONSTRAINT_NAME: &str = "FULLTEXT INDEX";
/// Skipping index constraint name
pub(crate) const SKIPPING_INDEX_CONSTRAINT_NAME: &str = "SKIPPING INDEX";

/// The virtual table implementation for `information_schema.KEY_COLUMN_USAGE`.
pub(super) struct InformationSchemaKeyColumnUsage {
Expand Down Expand Up @@ -225,6 +227,12 @@ impl InformationSchemaKeyColumnUsageBuilder {
let keys = &table_info.meta.primary_key_indices;
let schema = table.schema();

// For compatibility, use primary key columns as inverted index columns.
let pk_as_inverted_index = !schema
.column_schemas()
.iter()
.any(|c| c.has_inverted_index_key());

for (idx, column) in schema.column_schemas().iter().enumerate() {
let mut constraints = vec![];
if column.is_time_index() {
Expand All @@ -242,14 +250,20 @@ impl InformationSchemaKeyColumnUsageBuilder {
// TODO(dimbtp): foreign key constraint not supported yet
if keys.contains(&idx) {
constraints.push(PRI_CONSTRAINT_NAME);

if pk_as_inverted_index {
constraints.push(INVERTED_INDEX_CONSTRAINT_NAME);
}
}
if column.is_inverted_indexed() {
constraints.push(INVERTED_INDEX_CONSTRAINT_NAME);
}

if column.has_fulltext_index_key() {
if column.is_fulltext_indexed() {
constraints.push(FULLTEXT_INDEX_CONSTRAINT_NAME);
}
if column.is_skipping_indexed() {
constraints.push(SKIPPING_INDEX_CONSTRAINT_NAME);
}

if !constraints.is_empty() {
let aggregated_constraints = constraints.join(", ");
Expand Down
11 changes: 9 additions & 2 deletions src/datatypes/src/schema/column_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,15 @@ impl ColumnSchema {
.unwrap_or(false)
}

pub fn has_fulltext_index_key(&self) -> bool {
self.metadata.contains_key(FULLTEXT_KEY)
pub fn is_fulltext_indexed(&self) -> bool {
self.fulltext_options()
.unwrap_or_default()
.map(|option| option.enable)
.unwrap_or_default()
}

pub fn is_skipping_indexed(&self) -> bool {
self.skipping_index_options().unwrap_or_default().is_some()
}

pub fn has_inverted_index_key(&self) -> bool {
Expand Down
28 changes: 18 additions & 10 deletions src/query/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,19 +402,22 @@ pub async fn show_index(
query_ctx.current_schema()
};

let primary_key_expr = case(col("constraint_name").like(lit("%PRIMARY%")))
.when(lit(true), lit("greptime-primary-key-v1"))
.otherwise(null())
.context(error::PlanSqlSnafu)?;
let inverted_index_expr = case(col("constraint_name").like(lit("%INVERTED INDEX%")))
zhongzc marked this conversation as resolved.
Show resolved Hide resolved
.when(lit(true), lit("greptime-inverted-index-v1"))
.otherwise(null())
.context(error::PlanSqlSnafu)?;
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 skipping_index_expr = case(col("constraint_name").like(lit("%SKIPPING INDEX%")))
.when(lit(true), lit("greptime-bloom-filter-v1"))
.otherwise(null())
.context(error::PlanSqlSnafu)?;

let select = vec![
// 1 as `Non_unique`: contain duplicates
Expand All @@ -435,7 +438,12 @@ pub async fn show_index(
.alias(COLUMN_NULLABLE_COLUMN),
concat_ws(
lit(", "),
vec![inverted_index_expr.clone(), fulltext_index_expr.clone()],
vec![
primary_key_expr,
inverted_index_expr,
fulltext_index_expr,
skipping_index_expr,
],
)
.alias(INDEX_INDEX_TYPE_COLUMN),
lit("").alias(COLUMN_COMMENT_COLUMN),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ SHOW CREATE TABLE test;
| | ) |
+-------+---------------------------------------------------------------------------------------+

SHOW INDEX FROM test;

+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+
| test | 1 | FULLTEXT INDEX | 1 | message | A | | | | YES | greptime-fulltext-index-v1 | | | YES | |
| test | 1 | TIME INDEX | 1 | time | A | | | | NO | | | | YES | |
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+

ALTER TABLE test MODIFY COLUMN message UNSET FULLTEXT;

Affected Rows: 0
Expand All @@ -115,6 +124,14 @@ SHOW CREATE TABLE test;
| | ) |
+-------+-------------------------------------+

SHOW INDEX FROM test;

+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| test | 1 | TIME INDEX | 1 | time | A | | | | NO | | | | YES | |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+

ALTER TABLE test MODIFY COLUMN message SET FULLTEXT WITH(analyzer = 'Chinese', case_sensitive = 'true');

Affected Rows: 0
Expand All @@ -136,6 +153,15 @@ SHOW CREATE TABLE test;
| | ) |
+-------+---------------------------------------------------------------------------------------+

SHOW INDEX FROM test;

+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+
| test | 1 | FULLTEXT INDEX | 1 | message | A | | | | YES | greptime-fulltext-index-v1 | | | YES | |
| test | 1 | TIME INDEX | 1 | time | A | | | | NO | | | | YES | |
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+

ALTER TABLE test MODIFY COLUMN message SET FULLTEXT WITH(analyzer = 'Chinese', case_sensitive = 'false');

Error: 1004(InvalidArguments), Invalid column option, column name: message, error: FULLTEXT index already enabled
Expand All @@ -161,6 +187,14 @@ SHOW CREATE TABLE test;
| | ) |
+-------+-------------------------------------+

SHOW INDEX FROM test;

+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| test | 1 | TIME INDEX | 1 | time | A | | | | NO | | | | YES | |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+

ALTER TABLE test MODIFY COLUMN message SET FULLTEXT WITH(analyzer = 'Chinglish', case_sensitive = 'false');

Error: 1002(Unexpected), Invalid fulltext option: Chinglish, expected: 'English' | 'Chinese'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,28 @@ SELECT * FROM test WHERE MATCHES(message, 'hello') ORDER BY message;
-- SQLNESS ARG restart=true
SHOW CREATE TABLE test;

SHOW INDEX FROM test;

ALTER TABLE test MODIFY COLUMN message UNSET FULLTEXT;

SHOW CREATE TABLE test;

SHOW INDEX FROM test;

ALTER TABLE test MODIFY COLUMN message SET FULLTEXT WITH(analyzer = 'Chinese', case_sensitive = 'true');

SHOW CREATE TABLE test;

SHOW INDEX FROM test;

ALTER TABLE test MODIFY COLUMN message SET FULLTEXT WITH(analyzer = 'Chinese', case_sensitive = 'false');

ALTER TABLE test MODIFY COLUMN message UNSET FULLTEXT;

SHOW CREATE TABLE test;

SHOW INDEX FROM test;

ALTER TABLE test MODIFY COLUMN message SET FULLTEXT WITH(analyzer = 'Chinglish', case_sensitive = 'false');

ALTER TABLE test MODIFY COLUMN message SET FULLTEXT WITH(analyzer = 'Chinese', case_sensitive = 'no');
Expand Down
Loading
Loading