Repository navigation
Add eval table function - #104396
Add eval table function#104396niyue wants to merge 63 commits into
eval table function#104396Conversation
|
Workflow [PR], commit [056b8a4] Summary: ❌
AI ReviewSummaryThis PR adds the experimental Findings
Final VerdictStatus: Minimum required actions: fix the old-analyzer named-collection override paths in |
962cf52 to
003efb1
Compare
|
I've addressed the review comments above. The CI is still failing, but the remaining failed tests appear to be unrelated to this change. Could you please take another look when you have a moment? Thanks. |
003efb1 to
5f730c6
Compare
…n or a subquery returning single string row.
Run the normal set-operation normalization visitors on the generated `SELECT` before passing it to `StorageView`. This keeps generated `UNION ALL`, `UNION DISTINCT`, `INTERSECT`, and `EXCEPT` queries on the same path as regular queries and avoids planner exceptions from `UNION_DEFAULT`.
…ntalSettings`, add `eval` to the aspell dictionary, and make nested `eval` detection use a case-sensitive comparison.
5f730c6 to
86e74bb
Compare
Allow `StorageView` to assign a temporary table identifier for the `eval` table function before replacing it with the generated subquery. This avoids a `LOGICAL_ERROR` from `StorageView::replaceWithSubquery` for queries such as `SELECT * FROM eval(...)`.
Avoid resolving nested `eval` table function arguments on the initiator when they are used under `remote` or `cluster`. Like `view`, `eval` can generate a `SELECT` that references tables available only on shards. Reuse the `view` distributed rewrite path for table functions that expose an inner `SELECT`, and add an integration regression test for a remote-only table. Addresses reviewer feedback on ClickHouse#104396 (comment)
Permit `eval(SELECT ...)` in `QueryNormalizer` for the old analyzer path, matching the parser and `TableFunctionEval` behavior. Add a stateless regression test with `enable_analyzer` disabled. Addresses reviewer feedback on ClickHouse#104396 (comment)
Document that `eval` is normally analyzed on the initiator, but nested usage under `remote` or `cluster` is resolved on remote shards. Addresses reviewer feedback on ClickHouse#104396 (comment)
|
This was fixed by #105146. Let's update the branch. |
Drop the old `kql` table function layer left in `ExpressionListParsers.cpp` after merging `master`. The layer referenced removed `ParserKQLTableFunction` code and broke the parser build, while current `master` parses `kql` through the normal table function path. Issue: ClickHouse#101293 Tests: - `ninja -C build clickhouse_parsers` - `git diff --check -- src/Parsers/ExpressionListParsers.cpp` - `build/programs/clickhouse local --multiquery < tests/queries/0_stateless/04241_kql_array_index_no_oom.sql`
# Conflicts: # ci/jobs/scripts/check_style/aspell-ignore/en/aspell-dict.txt
|
Merged This is a pure The two |
The `eval` table function builds its inner `StorageView` from a generated query that may pin its own `SETTINGS enable_analyzer = ...`, captured in the view's inner query context. `TableFunctionEval::getActualTableStructure` infers the structure with that inner context, and `StorageView::readImpl` executes the inner query with the same analyzer mode. With `analyzer_inline_views = 1`, `QueryAnalyzer::inlineViewSubqueryIfNeeded` rebuilt and re-resolved the view with the new analyzer using the outer scope's settings, ignoring the inner analyzer mode. When the two differed (for example an `eval` query pinned to the old analyzer under a new-analyzer outer query), the inlined subquery produced a different column name than the inferred structure (`abs(a)` vs `abs(1)`), so the inlined result disagreed with the inferred schema. Skip inlining for views whose inner analyzer mode differs from the outer one, falling back to the non-inlined read path (`readImpl`), which already honors the inner context. Ordinary views have no inner query context and are unaffected. Addresses the review on `src/Storages/StorageView.cpp:130`. ClickHouse#104396 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Updating the branch to fix the |
Wrap the generated query with an explicit column alias list derived from the inferred schema, so anonymous generated projection names stay stable between schema inference and execution.
Teach `remote` to mark named-collection `database` and `db` overrides whose value is a table function as table-expression arguments. Resolve `eval` expression arguments through those wrappers before sending the query to shards, so outer `WITH` aliases are preserved for both AST and serialized-plan distributed paths.
Merge `niyue/feat/eval-table-func` into the local feature branch while preserving the already-pushed remote history. Resolve the overlapping `QueryAnalyzer` changes so shard-side wrappers and named-collection overrides both resolve nested `eval` expression arguments before shipping table-function ASTs to remote shards.
Keep the nested `eval` table-function guard in table-expression mode while walking wrapper expressions such as `equals(database, eval(...))`. This prevents generated `eval` queries from bypassing the nested `eval` restriction through `remote` named collection overrides.
Build a settings-aware context before analyzing or executing the input `SELECT` query passed to the `eval` table function. This keeps interpreter selection consistent with the input query's own `SETTINGS` and reuses the same helper for generated query schema inference and execution.
Use table-function argument metadata when traversing nested table functions, so ordinary scalar arguments such as a `remote` sharding key retain normal alias resolution. Add an integration regression for a scalar SQL UDF named `eval`.
| return false; | ||
|
|
||
| const auto * value_function = function_args_expr->children[1]->as<ASTFunction>(); | ||
| return value_function && TableFunctionFactory::instance().isTableFunctionName(value_function->name); |
There was a problem hiding this comment.
database = <expr> on named-collection remote(...) used to be a constant-expression slot. This name-only TableFunctionFactory::instance().isTableFunctionName(...) check reintroduces the scalar-function ambiguity that the rest of the PR had to unwind for eval: a pre-existing SQL UDF such as CREATE FUNCTION eval AS x -> 'system' is now reinterpreted as a table-function override, so remote(remote_nc, database = eval(1)) stops evaluating the RHS as a database name and can start failing with Table function \eval` is disabledeven though the query never intended to use the experimental table function. Because the same helper also drivesgetTableExpressionArgumentIndexes`, the analyzer/distributed path follows the table-function branch too.
This needs a compatibility story that preserves the old constant-expression behavior for scalar/UDF calls whose names collide with table functions.
There was a problem hiding this comment.
Fixed. Handle scalar function eval as well
There was a problem hiding this comment.
This still is not fixed under the old analyzer. The new UserDefinedSQLFunctionFactory guard only affects isDatabaseOverrideWithTableFunctionValue, but TableFunctionRemote::parseArguments still has the name-only branch at src/TableFunctions/TableFunctionRemote.cpp:117-119:
if (function && TableFunctionFactory::instance().isTableFunctionName(function->name)) remote_table_function_ptr = arg_ast;
Context::executeTableFunction still calls TableFunctionFactory::get on the raw AST under enable_analyzer = 0, so SELECT count() FROM remote(remote_eval_scalar, database = eval(1)) SETTINGS enable_analyzer = 0 reinterprets the SQL UDF as the experimental table function before the RHS can be folded.
Treat a named-collection `database` override as a constant expression when its function name is also registered as a SQL UDF. This prevents a scalar UDF named `eval` from being interpreted as the experimental `eval` table function.
| { | ||
| /// Named collection overrides wrap their table expression in a scalar function such as `equals`. | ||
| for (auto & child : function->children) | ||
| substituteWithLiteralsInEvalTableFunction(child, literals, context); |
There was a problem hiding this comment.
Under enable_analyzer = 0 this still misses the named-collection wrapper case. ASTFunction::children only contains the ASTExpressionList, so the recursion stops at equals(database, ...) and never reaches the nested eval(q). A query like WITH 'SELECT x FROM remote_only_eval_table' AS q SELECT * FROM remote(remote_eval, database = eval(q)) SETTINGS allow_experimental_eval_table_function = 1, enable_analyzer = 0 still sends eval(q) without substituting q, so the shard sees an unresolved identifier.
Can we recurse through function->arguments->children here (the same way checkNoNestedEvalTableFunction does) and add an old-analyzer regression for the named-collection override form?
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 527/569 (92.62%) · Uncovered code |
|
Let's find ways to simplify this code. What if:
Let's submit another PR aimed at maximum simplification of the implementation. |
A maximally simplified implementation of the `eval` table function: the argument is a constant expression evaluated to a query string, and the generated `SELECT` query is wrapped into `StorageView`, same as the `view` table function. `eval(SELECT ...)` is syntactic sugar for a scalar subquery argument `eval((SELECT ...))`. The old analyzer is not supported, `eval` cannot be used as an argument of another table function, and the generated query must be self-contained. Requested in ClickHouse#104396 (comment) Closes ClickHouse#101293 Co-Authored-By: Claude Fable 5 <[email protected]>
A maximally simplified implementation of the `eval` table function: the argument is a constant expression evaluated to a query string, and the generated `SELECT` query is wrapped into `StorageView`, same as the `view` table function. `eval(SELECT ...)` is syntactic sugar for a scalar subquery argument `eval((SELECT ...))`. The old analyzer is not supported, `eval` cannot be used as an argument of another table function, and the generated query must be self-contained. Requested in ClickHouse#104396 (comment) Closes ClickHouse#101293 Co-Authored-By: Claude Fable 5 <[email protected]>
Description
eval#101293evalfor constructing a query from a constant expression or anotherSELECTquery.Changelog category (leave one):
Changelog entry:
eval, which evaluates a constant expression or single-rowSELECTquery to a query string and executes the resulting singleSELECTquery. The feature is disabled by default and can be enabled with settingallow_experimental_eval_table_function.Documentation entry for user-facing changes