Skip to content

Commit

Permalink
fix: panic while reading information_schema. KEY_COLUMN_USAGE (#4318)
Browse files Browse the repository at this point in the history
* fix: table might be dropped during iteration

* fix: panic while reading information_schema.key_column_usage

* fix: key_column_usage wrong results
  • Loading branch information
killme2008 authored Jul 9, 2024
1 parent d1f1fad commit f1d17a8
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 57 deletions.
102 changes: 45 additions & 57 deletions src/catalog/src/information_schema/key_column_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use datatypes::prelude::{ConcreteDataType, MutableVector, ScalarVectorBuilder, V
use datatypes::schema::{ColumnSchema, Schema, SchemaRef};
use datatypes::value::Value;
use datatypes::vectors::{ConstantVector, StringVector, StringVectorBuilder, UInt32VectorBuilder};
use futures_util::TryStreamExt;
use snafu::{OptionExt, ResultExt};
use store_api::storage::{ScanRequest, TableId};

Expand Down Expand Up @@ -211,69 +212,56 @@ impl InformationSchemaKeyColumnUsageBuilder {
.context(UpgradeWeakCatalogManagerRefSnafu)?;
let predicates = Predicates::from_scan_request(&request);

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;
}
let mut stream = catalog_manager.tables(&catalog_name, &schema_name);

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();
while let Some(table) = stream.try_next().await? {
let mut primary_constraints = vec![];

for (idx, column) in schema.column_schemas().iter().enumerate() {
if column.is_time_index() {
self.add_key_column_usage(
&predicates,
&schema_name,
TIME_INDEX_CONSTRAINT_NAME,
&catalog_name,
&schema_name,
&table_name,
&column.name,
1, //always 1 for time index
);
}
if keys.contains(&idx) {
primary_constraints.push((
catalog_name.clone(),
schema_name.clone(),
table_name.clone(),
column.name.clone(),
));
}
// TODO(dimbtp): foreign key constraint not supported yet
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() {
if column.is_time_index() {
self.add_key_column_usage(
&predicates,
&schema_name,
TIME_INDEX_CONSTRAINT_NAME,
&catalog_name,
&schema_name,
table_name,
&column.name,
1, //always 1 for time index
);
}
if keys.contains(&idx) {
primary_constraints.push((
catalog_name.clone(),
schema_name.clone(),
table_name.to_string(),
column.name.clone(),
));
}
} else {
unreachable!();
// 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,
);
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,
);
}
}
}

self.finish()
Expand Down
25 changes: 25 additions & 0 deletions tests/cases/standalone/common/show/show_index.result
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,31 @@ CREATE TABLE IF NOT EXISTS system_metrics (

Affected Rows: 0

CREATE TABLE IF NOT EXISTS test (
a STRING,
b STRING,
c DOUBLE,
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(a, b),
TIME INDEX(ts)
);

Affected Rows: 0

SHOW INDEX;

Error: 2000(InvalidSyntax), Unexpected token while parsing SQL statement: SHOW INDEX;, expected: '{FROM | IN} table', found: ;

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 | 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 | |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+

SHOW INDEX FROM system_metrics;

+----------------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+----------------------------+---------+---------------+---------+------------+
Expand Down Expand Up @@ -51,3 +72,7 @@ DROP TABLE system_metrics;

Affected Rows: 0

DROP TABLE test;

Affected Rows: 0

13 changes: 13 additions & 0 deletions tests/cases/standalone/common/show/show_index.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@ CREATE TABLE IF NOT EXISTS system_metrics (
TIME INDEX(ts)
);

CREATE TABLE IF NOT EXISTS test (
a STRING,
b STRING,
c DOUBLE,
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(a, b),
TIME INDEX(ts)
);

SHOW INDEX;

SHOW INDEX FROM test;

SHOW INDEX FROM system_metrics;

SHOW INDEX FROM system_metrics in public;
Expand All @@ -20,3 +31,5 @@ SHOW INDEX FROM system_metrics like '%util%';
SHOW INDEX FROM system_metrics WHERE Key_name = 'TIME INDEX';

DROP TABLE system_metrics;

DROP TABLE test;

0 comments on commit f1d17a8

Please sign in to comment.