From 6d37ee09d48af8a162ff40f3e731b811af67d485 Mon Sep 17 00:00:00 2001 From: robot-clickhouse Date: Fri, 10 Nov 2023 11:07:58 +0000 Subject: [PATCH] Backport #55540 to 23.8: Select from system tables when table based on table function. --- src/TableFunctions/TableFunctionMySQL.cpp | 4 +- .../TableFunctionPostgreSQL.cpp | 4 +- src/TableFunctions/TableFunctionS3.cpp | 4 +- src/TableFunctions/TableFunctionSQLite.cpp | 4 +- ...ith_inaccsessible_table_function.reference | 12 ++++++ ...bles_with_inaccsessible_table_function.sql | 43 +++++++++++++++++++ 6 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 tests/queries/0_stateless/02888_system_tables_with_inaccsessible_table_function.reference create mode 100644 tests/queries/0_stateless/02888_system_tables_with_inaccsessible_table_function.sql diff --git a/src/TableFunctions/TableFunctionMySQL.cpp b/src/TableFunctions/TableFunctionMySQL.cpp index 03bd22645516..a5c6962697d0 100644 --- a/src/TableFunctions/TableFunctionMySQL.cpp +++ b/src/TableFunctions/TableFunctionMySQL.cpp @@ -66,7 +66,7 @@ StoragePtr TableFunctionMySQL::executeImpl( const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, - ColumnsDescription /*cached_columns*/, + ColumnsDescription cached_columns, bool /*is_insert_query*/) const { auto res = std::make_shared( @@ -76,7 +76,7 @@ StoragePtr TableFunctionMySQL::executeImpl( configuration->table, configuration->replace_query, configuration->on_duplicate_clause, - ColumnsDescription{}, + cached_columns, ConstraintsDescription{}, String{}, context, diff --git a/src/TableFunctions/TableFunctionPostgreSQL.cpp b/src/TableFunctions/TableFunctionPostgreSQL.cpp index 322e0df7c15e..6ed5883c3cc9 100644 --- a/src/TableFunctions/TableFunctionPostgreSQL.cpp +++ b/src/TableFunctions/TableFunctionPostgreSQL.cpp @@ -20,13 +20,13 @@ namespace ErrorCodes StoragePtr TableFunctionPostgreSQL::executeImpl(const ASTPtr & /*ast_function*/, - ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/, bool /*is_insert_query*/) const + ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool /*is_insert_query*/) const { auto result = std::make_shared( StorageID(getDatabaseName(), table_name), connection_pool, configuration->table, - ColumnsDescription{}, + cached_columns, ConstraintsDescription{}, String{}, context, diff --git a/src/TableFunctions/TableFunctionS3.cpp b/src/TableFunctions/TableFunctionS3.cpp index df9e5afcaffc..712d40dc9c07 100644 --- a/src/TableFunctions/TableFunctionS3.cpp +++ b/src/TableFunctions/TableFunctionS3.cpp @@ -336,7 +336,7 @@ bool TableFunctionS3::supportsReadingSubsetOfColumns(const ContextPtr & context) return FormatFactory::instance().checkIfFormatSupportsSubsetOfColumns(configuration.format, context); } -StoragePtr TableFunctionS3::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription /*cached_columns*/, bool /*is_insert_query*/) const +StoragePtr TableFunctionS3::executeImpl(const ASTPtr & /*ast_function*/, ContextPtr context, const std::string & table_name, ColumnsDescription cached_columns, bool /*is_insert_query*/) const { S3::URI s3_uri (configuration.url); @@ -345,6 +345,8 @@ StoragePtr TableFunctionS3::executeImpl(const ASTPtr & /*ast_function*/, Context columns = parseColumnsListFromString(configuration.structure, context); else if (!structure_hint.empty()) columns = structure_hint; + else if (!cached_columns.empty()) + columns = cached_columns; StoragePtr storage = std::make_shared( configuration, diff --git a/src/TableFunctions/TableFunctionSQLite.cpp b/src/TableFunctions/TableFunctionSQLite.cpp index 27e6fcf1fd15..dfd104ef81a2 100644 --- a/src/TableFunctions/TableFunctionSQLite.cpp +++ b/src/TableFunctions/TableFunctionSQLite.cpp @@ -29,13 +29,13 @@ namespace ErrorCodes StoragePtr TableFunctionSQLite::executeImpl(const ASTPtr & /*ast_function*/, - ContextPtr context, const String & table_name, ColumnsDescription /*cached_columns*/, bool /*is_insert_query*/) const + ContextPtr context, const String & table_name, ColumnsDescription cached_columns, bool /*is_insert_query*/) const { auto storage = std::make_shared(StorageID(getDatabaseName(), table_name), sqlite_db, database_path, remote_table_name, - ColumnsDescription{}, ConstraintsDescription{}, context); + cached_columns, ConstraintsDescription{}, context); storage->startup(); return storage; diff --git a/tests/queries/0_stateless/02888_system_tables_with_inaccsessible_table_function.reference b/tests/queries/0_stateless/02888_system_tables_with_inaccsessible_table_function.reference new file mode 100644 index 000000000000..5efe10177dd6 --- /dev/null +++ b/tests/queries/0_stateless/02888_system_tables_with_inaccsessible_table_function.reference @@ -0,0 +1,12 @@ +tablefunc01 StorageProxy CREATE TABLE default.tablefunc01 (`x` Int32) AS postgresql(\'127.121.0.1:5432\', \'postgres_db\', \'postgres_table\', \'postgres_user\', \'[HIDDEN]\') [] 1 1 +tablefunc02 StorageProxy CREATE TABLE default.tablefunc02 (`x` Int32) AS mysql(\'127.123.0.1:3306\', \'mysql_db\', \'mysql_table\', \'mysql_user\', \'[HIDDEN]\') [] 1 1 +tablefunc03 StorageProxy CREATE TABLE default.tablefunc03 (`a` Int32) AS sqlite(\'db_path\', \'table_name\') [] 1 1 +tablefunc04 StorageProxy CREATE TABLE default.tablefunc04 (`a` Int32) AS mongodb(\'127.0.0.1:27017\', \'test\', \'my_collection\', \'test_user\', \'[HIDDEN]\', \'a Int\') [] 1 1 +tablefunc05 StorageProxy CREATE TABLE default.tablefunc05 (`a` Int32) AS redis(\'127.0.0.1:6379\', \'key\', \'key UInt32\') [] 1 1 +tablefunc06 StorageProxy CREATE TABLE default.tablefunc06 (`a` Int32) AS s3(\'http://some_addr:9000/cloud-storage-01/data.tsv\', \'M9O7o0SX5I4udXhWxI12\', \'[HIDDEN]\', \'TSV\') [] 1 1 +tablefunc01 StorageProxy CREATE TABLE default.tablefunc01 (`x` Int32) AS postgresql(\'127.121.0.1:5432\', \'postgres_db\', \'postgres_table\', \'postgres_user\', \'[HIDDEN]\') [] 1 1 +tablefunc02 StorageProxy CREATE TABLE default.tablefunc02 (`x` Int32) AS mysql(\'127.123.0.1:3306\', \'mysql_db\', \'mysql_table\', \'mysql_user\', \'[HIDDEN]\') [] 1 1 +tablefunc03 StorageProxy CREATE TABLE default.tablefunc03 (`a` Int32) AS sqlite(\'db_path\', \'table_name\') [] 1 1 +tablefunc04 StorageProxy CREATE TABLE default.tablefunc04 (`a` Int32) AS mongodb(\'127.0.0.1:27017\', \'test\', \'my_collection\', \'test_user\', \'[HIDDEN]\', \'a Int\') [] 1 1 +tablefunc05 StorageProxy CREATE TABLE default.tablefunc05 (`a` Int32) AS redis(\'127.0.0.1:6379\', \'key\', \'key UInt32\') [] 1 1 +tablefunc06 StorageProxy CREATE TABLE default.tablefunc06 (`a` Int32) AS s3(\'http://some_addr:9000/cloud-storage-01/data.tsv\', \'M9O7o0SX5I4udXhWxI12\', \'[HIDDEN]\', \'TSV\') [] 1 1 diff --git a/tests/queries/0_stateless/02888_system_tables_with_inaccsessible_table_function.sql b/tests/queries/0_stateless/02888_system_tables_with_inaccsessible_table_function.sql new file mode 100644 index 000000000000..adcdeecb9e1e --- /dev/null +++ b/tests/queries/0_stateless/02888_system_tables_with_inaccsessible_table_function.sql @@ -0,0 +1,43 @@ +-- Tags: no-fasttest + +DROP DATABASE IF EXISTS {CLICKHOUSE_DATABASE:Identifier}; + +CREATE DATABASE {CLICKHOUSE_DATABASE:Identifier}; + + +CREATE TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc01 (x int) AS postgresql('127.121.0.1:5432', 'postgres_db', 'postgres_table', 'postgres_user', '124444'); +CREATE TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc02 (x int) AS mysql('127.123.0.1:3306', 'mysql_db', 'mysql_table', 'mysql_user','123123'); +CREATE TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc03 (a int) AS sqlite('db_path', 'table_name'); +CREATE TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc04 (a int) AS mongodb('127.0.0.1:27017','test', 'my_collection', 'test_user', 'password', 'a Int'); +CREATE TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc05 (a int) AS redis('127.0.0.1:6379', 'key', 'key UInt32'); +CREATE TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc06 (a int) AS s3('http://some_addr:9000/cloud-storage-01/data.tsv', 'M9O7o0SX5I4udXhWxI12', '9ijqzmVN83fzD9XDkEAAAAAAAA', 'TSV'); + + +CREATE TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc01_without_schema AS postgresql('127.121.0.1:5432', 'postgres_db', 'postgres_table', 'postgres_user', '124444'); -- { serverError 614 } +CREATE TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc02_without_schema AS mysql('127.123.0.1:3306', 'mysql_db', 'mysql_table', 'mysql_user','123123'); -- {serverError 279 } + +SELECT name, engine, engine_full, create_table_query, data_paths, notEmpty([metadata_path]), notEmpty([uuid]) + FROM system.tables + WHERE name like '%tablefunc%' and database=currentDatabase() + ORDER BY name; + +DETACH TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc01; +DETACH TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc02; +DETACH TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc03; +DETACH TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc04; +DETACH TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc05; +DETACH TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc06; + +ATTACH TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc01; +ATTACH TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc02; +ATTACH TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc03; +ATTACH TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc04; +ATTACH TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc05; +ATTACH TABLE {CLICKHOUSE_DATABASE:Identifier}.tablefunc06; + +SELECT name, engine, engine_full, create_table_query, data_paths, notEmpty([metadata_path]), notEmpty([uuid]) + FROM system.tables + WHERE name like '%tablefunc%' and database=currentDatabase() + ORDER BY name; + +DROP DATABASE IF EXISTS {CLICKHOUSE_DATABASE:Identifier};