Repository navigation
Conversation
|
|
|
Workflow [PR], commit [a6b8f9d] Summary: ❌
AI ReviewSummaryThis PR enables Findings❌ Blockers
💡 Nits
ClickHouse Rules
Final Verdict
|
| @@ -2023,19 +2023,32 @@ bool InterpreterCreateQuery::doCreateTable(ASTCreateQuery & create, | |||
| /// NOTE: CREATE query may be rewritten by Storage creator or table function | |||
| if (create.as_table_function) | |||
| { | |||
| auto table_function_ast = create.as_table_function->ptr(); | |||
| auto table_function = TableFunctionFactory::instance().get(table_function_ast, getContext()); | |||
| if (create.storage) | |||
There was a problem hiding this comment.
CREATE TABLE ... ENGINE ... AS table_function(...) now leaves create.storage set while create.as_table_function is still present at this stage. With restore_replace_external_table_functions_to_null = 1, setEngine currently treats this as impossible and throws LOGICAL_ERROR ("Storage should not be created yet, it's a bug.").
This turns a valid query into an internal exception under a supported setting.
Please handle the create.storage && create.as_table_function case explicitly (for example, avoid throwing and clear as_table_function/skip replacement when an explicit engine is already provided).
660f75b to
dd83f18
Compare
| { | ||
| if (getContext()->getSettingsRef()[Setting::restore_replace_external_table_functions_to_null]) | ||
| { | ||
| if (create.storage) { |
There was a problem hiding this comment.
With restore_replace_external_table_functions_to_null = 1, this new create.storage fast-path returns before replacing/removing as_table_function.
For a query like CREATE TABLE t ENGINE = MergeTree AS remote(...), execution then reaches getTablePropertiesAndNormalizeCreateQuery, where create.as_table_function is still set and getActualTableStructureWithAccess is called. That can still initiate external access, which conflicts with the setting contract (“replace external table functions to Null to not initiate external connections”).
Could we either (1) nullify/skip as_table_function under this setting (and require explicit columns when needed), or (2) throw a clear exception when AS table_function is used together with this setting and no local structure is available?
| @@ -1243,6 +1243,19 @@ void InterpreterCreateQuery::setEngine(ASTCreateQuery & create) const | |||
| { | |||
| if (getContext()->getSettingsRef()[Setting::restore_replace_external_table_functions_to_null]) | |||
There was a problem hiding this comment.
The purpose of this setting is to avoid initiating connections to external services (S3, MySQL, remote ClickHouse, etc) when executing A CREATE TABLE or RESTORE query (useful for testing). Will it avoid external connections for a query with a remote table function and some engine?
There was a problem hiding this comment.
After looking a bit more at this setting, it seems that (and AI pointed that out as well) that we will not be able to infer the table structure without a remote call.
The existing behaviour for the table functions is the following:
set restore_replace_external_table_functions_to_null=1
create table bar as file('test.csv')
Received exception from server (version 26.4.1):
Code: 80. DB::Exception: Received from localhost:9000. DB::Exception: Incorrect CREATE query: required list of column descriptions or AS section or SELECT.. (INCORRECT_QUERY)
Since the code under if resets the as_table_function pointer, and Null engine has supports_schema_inference == false. Which is a bit misleading, since the query has the AS section, but I guess it's fine for testing purposes.
So with this setting active, I'd suggest
- Check that if storage is set, then it has an engine specified, before checking the setting. If the engine is not specified, the query is invalid, so I think we should report this error in the first place.
- If the table function is
allow_readonlyand engine is specified, override it with Null engine ifsource_access_typeis set. - Remove the else branch with exception, since it's a valid state now
This way we preserve the existing behaviour for table functions without specified engine and for allow_readonly functions with specified engine we override engine with NullEngine as for non-table function queries. We still want to fail if table function is not allow_readonly if the engine is specified, since we're not able to infer the table structure, unless it's explicitly specified (more on that below).
Does it make sense?
I've also found some issues with column list parsing in case engine is set, I'm fixing it now along with the style inconsistencies.
I've also found some interesting behaviour with create as table function query if the column list specified - inserting values into the new table fails with error NOT_FOUND_COLUMN_IN_BLOCK unless the specified columns are identical to the columns inferred from the table function. Which makes sense, but wouldn't it be better to fail to create a table with incompatible columns?
Example:
Given that test.csv has the following structure: (name Nullable(String), age Nullable(Int64))
create table bar (a Int64) as file('test.csv')
select * from bar
Received exception from server (version 26.4.1):
Code: 10. DB::Exception: Received from localhost:9000. DB::Exception: Column `a` not found in table default.bar (2f1e0874-d862-40f4-94fe-e09922afb0dd). (NOT_FOUND_COLUMN_IN_BLOCK)
create table baz (name Nullable(String)) as file('test.csv')
insert into baz values('aaa')
Code: 10. DB::Exception: Received from localhost:9000. DB::Exception: Column `a` not found in table default.bar (2f1e0874-d862-40f4-94fe-e09922afb0dd). (NOT_FOUND_COLUMN_IN_BLOCK)
create table foo (name Nullable(String), age Nullable(Int64), foo Int64) as file('test.csv')
Received exception from server (version 26.4.1):
Code: 122. DB::Exception: Received from localhost:9000. DB::Exception: Source storage and table function have different structure: While executing WaitForAsyncInsert. (INCOMPATIBLE_COLUMNS)
Though, select * query works if the specified set of columns is a subset of the table function's columns.
So my current plan is to compare the existing ColumnsDescription with the inferred ColumnsDescription from the table function and throw an INCOMPATIBLE_COLUMNS error if they are different, thus failing the creation of the table. Does it make sense?
There was a problem hiding this comment.
Yes, we cannot infer the table structure without a remote call, so if no structure is specified and the table function requires a remote call - we should throw an exception. Actually, this combination (restore_replace_external_table_functions_to_null=1 + another engine with as table_function()) doesn't make much sense: "another engine with as table_function()" is useful for schema inference, but restore_replace_external_table_functions_to_null=1 disallows schema inference for some table functions
It's not necessary to reset the engine to Null (and even better to keep the original engine) for allow_readonly table functions, we just need to avoid remote calls. It's also okay to disallow this combination completely
I've also found some interesting behaviour with create as table function query if the column list specified - inserting values into the new table fails with error NOT_FOUND_COLUMN_IN_BLOCK unless the specified columns are identical to the columns inferred from the table function. Which makes sense, but wouldn't it be better to fail to create a table with incompatible columns?
Yes, it would be better, but usually users don't specify the structure when creating tables like this. The possibility to specify the structure is mostly for internal usage: ClickHouse simply stores CREATE TABLE statements in .sql files and executes them as ATTACH TABLE on server loading. It wasn't possible to load a table without a list of columns, and if ClickHouse fails to get the structure from a remote source - it would fail to start, so we saved the previously inferred structure to the metadata file. If the remote structure changes later (but remains compatible, e.g. new columns, data type changes) - ClickHouse will try to convert it when executing SELECT/INSERT, that's why
select * query works if the specified set of columns is a subset of the table function's columns.
So my current plan is to compare the existing ColumnsDescription with the inferred ColumnsDescription from the table function and throw an INCOMPATIBLE_COLUMNS error if they are different, thus failing the creation of the table. Does it make sense?
Yes, it makes sense, but we need to make sure that this check is performed for CREATE only, not for ATTACH
dd83f18 to
a6b8f9d
Compare
| if (create.as_table_function && create.storage && create.storage->engine) | ||
| { | ||
| /// 1. In CREATE ... AS table_function() ENGINE = ... queries table_function should be used | ||
| /// only for table structure inference, so the after the structure is inferred the |
There was a problem hiding this comment.
Typo in the comment: "so the after the structure is inferred" has an extra the.
Suggested wording: "so after the structure is inferred".
LLVM Coverage Report
Changed lines: 89.66% (52/58) | lost baseline coverage: 2 line(s) · Uncovered code |
However, the early return happens if The issue with potential deadlock on shutdown in Will look into the failing test in |
Nothing, AI is wrong Yes, the Stress tests failure is unrelated, but the |
Changelog category (leave one):
Changelog entry:
Users can create a table with structure inferred from a table function, but with a different engine. The table function's engine is ignored.
Documentation entry for user-facing changes
Fixes #50571.
This PR allows users to specify engine for CREATE TABLE AS table_function() queries.
The table function is used to infer table structure, i.e. the list of columns with names and types, and the new table is created with the specified engine. The table function's engine is ignored.
Example:
where test.csv is the following:
creates the table equivalent to the following query: