From 3827ecccb9e425dd742337b815dbdeb71ca87499 Mon Sep 17 00:00:00 2001 From: dantengsky Date: Thu, 1 Aug 2024 18:51:46 +0800 Subject: [PATCH] prevent index creation on attach table --- src/query/sql/src/planner/binder/ddl/index.rs | 15 +++++++++++++++ .../02_0004_attach_table.result | 4 ++++ .../04_attach_read_only/02_0004_attach_table.sh | 3 +++ .../04_0001_check_mutations.result | 2 ++ .../04_0001_check_mutations.sh | 8 ++++++-- 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/query/sql/src/planner/binder/ddl/index.rs b/src/query/sql/src/planner/binder/ddl/index.rs index 2d55447ea08de..f7f5a0fef4c4f 100644 --- a/src/query/sql/src/planner/binder/ddl/index.rs +++ b/src/query/sql/src/planner/binder/ddl/index.rs @@ -246,6 +246,13 @@ impl Binder { let table_entry = &tables[0]; let table = table_entry.table(); + if table.is_read_only() { + return Err(ErrorCode::UnsupportedIndex(format!( + "Table {} is read-only, creating index not allowed", + table.name() + ))); + } + if !table.support_index() { return Err(ErrorCode::UnsupportedIndex(format!( "Table engine {} does not support create index", @@ -416,6 +423,14 @@ impl Binder { self.normalize_object_identifier_triple(catalog, database, table); let table = self.ctx.get_table(&catalog, &database, &table).await?; + + if table.is_read_only() { + return Err(ErrorCode::UnsupportedIndex(format!( + "Table {} is read-only, creating inverted index not allowed", + table.name() + ))); + } + if !table.support_index() { return Err(ErrorCode::UnsupportedIndex(format!( "Table engine {} does not support create inverted index", diff --git a/tests/suites/5_ee/04_attach_read_only/02_0004_attach_table.result b/tests/suites/5_ee/04_attach_read_only/02_0004_attach_table.result index 4ccdba913b93f..f22487d3b50e3 100644 --- a/tests/suites/5_ee/04_attach_read_only/02_0004_attach_table.result +++ b/tests/suites/5_ee/04_attach_read_only/02_0004_attach_table.result @@ -15,6 +15,10 @@ 1 2 <<<< +#### select attach table from system.tables +>>>> select is_attach from system.tables where name = 'table_to'; +ATTACH +<<<< #### select attach table with self-defined connection >>>> select * from table_to2 order by a; 0 diff --git a/tests/suites/5_ee/04_attach_read_only/02_0004_attach_table.sh b/tests/suites/5_ee/04_attach_read_only/02_0004_attach_table.sh index 44764146d1e4c..9d6f3e2b5d09b 100755 --- a/tests/suites/5_ee/04_attach_read_only/02_0004_attach_table.sh +++ b/tests/suites/5_ee/04_attach_read_only/02_0004_attach_table.sh @@ -37,6 +37,9 @@ echo "attach table table_to2 's3://testbucket/admin/data/$storage_prefix' connec comment "select attach table" query "select * from table_to order by a;" +comment "select attach table from system.tables" +query "select is_attach from system.tables where name = 'table_to';" + comment "select attach table with self-defined connection" query "select * from table_to2 order by a;" diff --git a/tests/suites/5_ee/04_attach_read_only/04_0001_check_mutations.result b/tests/suites/5_ee/04_attach_read_only/04_0001_check_mutations.result index 57df7269bca15..665eaeecf843b 100755 --- a/tests/suites/5_ee/04_attach_read_only/04_0001_check_mutations.result +++ b/tests/suites/5_ee/04_attach_read_only/04_0001_check_mutations.result @@ -1,3 +1,5 @@ +create index should fail +Error: APIError: ResponseError with 1601: Table test_json_read_only is read-only, creating inverted index not allowed create virtual column should fail Error: APIError: ResponseError with 3905: Modification not permitted: Table 'test_json_read_only' is READ ONLY, preventing any changes or updates. alter virtual column should fail diff --git a/tests/suites/5_ee/04_attach_read_only/04_0001_check_mutations.sh b/tests/suites/5_ee/04_attach_read_only/04_0001_check_mutations.sh index 6524c9b48a755..55edabf1875f8 100755 --- a/tests/suites/5_ee/04_attach_read_only/04_0001_check_mutations.sh +++ b/tests/suites/5_ee/04_attach_read_only/04_0001_check_mutations.sh @@ -3,15 +3,19 @@ CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) . "$CURDIR"/../../../shell_env.sh -echo "create database if not exists test_attach_only;" | $BENDSQL_CLIENT_CONNECT +echo "create or replace database test_attach_only;" | $BENDSQL_CLIENT_CONNECT # mutation related enterprise features -echo "create table test_attach_only.test_json(id int, val json) 's3://testbucket/admin/data/' connection=(access_key_id ='minioadmin' secret_access_key ='minioadmin' endpoint_url='${STORAGE_S3_ENDPOINT_URL}');" | $BENDSQL_CLIENT_CONNECT +echo "create or replace table test_attach_only.test_json(id int, val json) 's3://testbucket/admin/data/' connection=(access_key_id ='minioadmin' secret_access_key ='minioadmin' endpoint_url='${STORAGE_S3_ENDPOINT_URL}');" | $BENDSQL_CLIENT_CONNECT echo "insert into test_attach_only.test_json values(1, '{\"a\":33,\"b\":44}'),(2, '{\"a\":55,\"b\":66}')" | $BENDSQL_CLIENT_CONNECT storage_prefix=$(mysql -uroot -h127.0.0.1 -P3307 -e "set global hide_options_in_show_create_table=0;show create table test_attach_only.test_json" | grep -i snapshot_location | awk -F'SNAPSHOT_LOCATION='"'"'|_ss' '{print $2}') echo "attach table test_attach_only.test_json_read_only 's3://testbucket/admin/data/$storage_prefix' connection=(access_key_id ='minioadmin' secret_access_key ='minioadmin' endpoint_url='${STORAGE_S3_ENDPOINT_URL}');" | $BENDSQL_CLIENT_CONNECT + +echo "create index should fail" +echo "CREATE INVERTED INDEX IF NOT EXISTS idx1 ON test_attach_only.test_json_read_only(val)" | $BENDSQL_CLIENT_CONNECT + echo "create virtual column should fail" echo "CREATE VIRTUAL COLUMN (val['a'], val['b']) FOR test_attach_only.test_json_read_only" | $BENDSQL_CLIENT_CONNECT