Repository navigation
Add eval table function - #110132
Add eval table function#110132
eval table function#110132Conversation
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 #104396 (comment) Closes #101293 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Preview deployment for your docs. Learn more about Mintlify Previews.
|
|
Workflow [PR], commit [064453c] Summary: ✅
AI ReviewSummaryPR #110132 adds an experimental Findings❌ Blockers
Tests
Final VerdictStatus: Minimum required actions: restore normal preflight parity for generated queries at least for AST-size enforcement, and add focused regressions for the remaining semantics-sensitive paths. |
The `eval` table function requires the analyzer and throws `NOT_IMPLEMENTED` when `enable_analyzer = 0`. The `old analyzer` CI job forces `enable_analyzer = 0` for the whole session, so every query in `04512_eval_table_function` that exercises the analyzer path failed there with `NOT_IMPLEMENTED` instead of the expected result. Add the `no-old-analyzer` tag to skip the test under that job. The test still runs in full under the regular (analyzer) jobs, where it also covers the old-analyzer rejection explicitly via `SET enable_analyzer = 0` at the end, so no coverage is lost.
…uery result cache
Two safety fixes for the experimental `eval` table function, following the
philosophy of keeping it maximally locked down:
* `CREATE TABLE ... AS eval(...)` is now rejected. The source expression would
be re-evaluated on every `ATTACH`, so such a table could fail to attach after
a restart (the experimental setting might be disabled) or silently change if
the expression depends on parameters, settings, or time. There is no stable
persisted representation, so `canBeUsedToCreateTable` returns `false`.
* The generated query is opaque to the query result cache, which only sees the
outer `eval('...')` call. `SELECT * FROM eval('SELECT now()')` could be cached
as if deterministic, and `eval('SELECT * FROM system.processes')` bypassed the
system-table guard. `eval` is now treated as potentially non-deterministic and
potentially reading a system table, so such queries are not cached by default.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…by `eval`
The generated query did not go through `validateAnalyzerSettings`, so its
`SETTINGS` clause could flip `enable_analyzer` / `allow_experimental_analyzer`
even though `eval` is analyzer-only, while a usual query is rejected with
`INCORRECT_QUERY` when this setting is changed in a subquery. Expose
`validateAnalyzerSettings` from `executeQuery` and run it on the generated
query before storing it, so `eval('SELECT 1 SETTINGS enable_analyzer = 0')`
is rejected the same way as a plain query, and add regressions.
Co-Authored-By: Claude Fable 5 <[email protected]>
|
@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110132&sha=978fd8028f27596a4f2e5cf37506a5425ba215d7&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 and provide a fix in a separate PR. It is unrelated to this PR: a |
|
Investigated the unrelated Summary: this materialized-CTE Not caused by this PR. |
| settings[Setting::max_parser_depth], | ||
| settings[Setting::max_parser_backtracks]); | ||
|
|
||
| checkNoEval(query); |
There was a problem hiding this comment.
eval parses the generated SELECT and then immediately walks/stores it, but unlike executeQueryImpl it never calls checkASTSizeLimits. That makes max_ast_depth / max_ast_elements ineffective for inner queries: a tiny outer SELECT * FROM eval('...') can smuggle a huge or very deep AST into checkNoEval and the analyzer even though executing the same inner SELECT directly would be rejected. Please apply the normal AST-size gate here, on a context with the generated query's own SETTINGS already applied.
There was a problem hiding this comment.
Done: #110211 applies the AST size limits (max_ast_depth/max_ast_elements) to the generated query, reading them from a context with the inner query's own SETTINGS applied.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 142/143 (99.30%) · Uncovered code |
|
The unrelated |
…unction Add `eval` table function
Closes: #101293
Closes: #104396
A maximally simplified implementation of the
evaltable function, as requested in #104396 (comment):evalthrowsNOT_IMPLEMENTEDunlessenable_analyzer = 1.evalis not allowed as an argument of another table function (e.g.remote('host', eval(...))throwsUNSUPPORTED_METHOD). The generated query can still be executed on a remote server through a query argument ofview:remote('host', view(SELECT * FROM eval(...))).WITH-alias substitution machinery. The argument is resolved by the analyzer in the outer scope like any other table function argument (soWITH 'SELECT 1' AS q SELECT * FROM eval(q)works through common code), and the generated query is self-contained: it is resolved in its own scope and cannot reference names of the outer query.The implementation reduces to a table function that evaluates its argument via
evaluateConstantExpression, parses the result withParserSelectWithUnionQuery(so any non-SELECTstatement or output option is a syntax error), and wraps the generated query intoStorageView— the same mechanism as theviewtable function. Theeval(SELECT ...)form from the issue is kept as pure syntactic sugar: the parser wraps the bare query into a scalar subquery, soeval(SELECT ...)is equivalent toeval((SELECT ...)), and the single-row, single-column, andNULLchecks come from the standard scalar subquery evaluation. The generated query cannot useevalagain.Compared to #104396, this PR does not touch
ApplyWithSubqueryVisitor,QueryNormalizer,StorageView,StorageDistributed,SelectStreamFactory, or theITableFunctioninterface. The only change outside the new table function, the parser layer, and the settings declarations is a three-line nesting ban inQueryAnalyzer::resolveTableFunction.A known limitation shared with the
viewtable function: a generated query with an unnamed scalar subquery column (e.g.eval('SELECT (SELECT 42)')) fails, because the auto-generated_subquery_Ncolumn name is not stable between schema inference and execution (the same happens withview(SELECT (SELECT 42))on current master). Aliasing the column works:eval('SELECT (SELECT 42) AS x').Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Added an experimental table function
eval, which evaluates a constant expression to a query string and executes the resulting singleSELECTquery. The feature is disabled by default and can be enabled with the settingallow_experimental_eval_table_function. Author: Yue Ni.Version info
26.7.1.842(included in26.7and later)