Repository navigation
Fix LOGICAL_ERROR on INSERT into a table whose projection has a subquery in its SELECT list or GROUP BY - #130143
Conversation
… name A subquery is named by the hash of its AST (`__subquery_<hash>`). The projection metadata is named from the stored projection query, analysed in place, while `calculateByQuery` (since ClickHouse#118375) renames the columns of the Analyzer pipeline to names built by converting the query tree back to AST. That conversion does not reproduce the subquery (a subquery without `FROM` gets `FROM system.one`, other shapes hash differently too), so a projection column like `b IN (SELECT ...)` was calculated as `in(b, __subquery_X)` while the metadata describes `in(b, __subquery_Y)`, and every INSERT, MATERIALIZE PROJECTION or merge rebuilding a projection with `WHERE` or an aggregate projection failed with LOGICAL_ERROR (found by the AST fuzzer on the `05515_global_subquery_projection_where` seed). On 26.9 the same INSERT silently stored 0 in such a column. The unresolved names of the functions are now taken from the AST each query tree node was built from, which is the very AST the metadata was named from; `toAST` stays the fallback for a node without one (the `tuple` wrapper added around a tuple `GROUP BY` key). The names are keyed by node, so a resolution that replaces the `IN` node loses them: `rewrite_in_to_join` does, so `calculateByQuery` pins it to 0 next to the other settings it pins. Creating the table under `rewrite_in_to_join = 1` gives the same metadata names (they come from `InterpreterSelectQuery`, which does not read it), so `fillProjectionDescriptionByQuery` needs no pin. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold review of the diff (0 blockers, 0 majors).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-11:20261108-124600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261108-121000 |
|
Workflow [PR], commit [09d9bc4] Summary: ✅
AI ReviewSummaryReviewed PR #130143 at The new subquery regression test and two existing aggregate naming regression tests passed locally using the PR's ARM Fast test binary; all outputs matched their references. The full CI workflow remains in progress. Final VerdictStatus: ✅ Approve. |
|
cc @alexey-milovidov @KochetovNicolai, could you review this and add the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 17 s compile time in total, 7 of them have a recent master baseline. |
fa0eb19
The only conflict is in `ProjectionDescription::calculateByQuery`: master also has the `enable_parallel_replicas` line of #129863 and the positional arguments change of #126185 there, neither of which is on 26.10. Keep the 26.10 code and add only the `rewrite_in_to_join` line of #130143. Also take the three lines of #129006 that materialize constant columns of the calculated projection block. On 26.10 a projection column such as `exists(SELECT ...)` is calculated as a `ColumnConst`, and once #130143 gives it the right name, writing the projection part fails with `Bad cast from type DB::ColumnConst to DB::ColumnVector<char8_t>` (the `t_subquery_normal` case of 05569). The rest of #129006 is not needed on 26.10: its trigger, `optimize_constant_columns_after_filter`, does not exist there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #130143 to 26.10: Fix `LOGICAL_ERROR` on `INSERT` into a table whose projection has a subquery in its `SELECT` list or `GROUP BY`
…le whose projection has a subquery in its `SELECT` list or `GROUP BY`
Backport #130143 to 26.10: Fix `LOGICAL_ERROR` on `INSERT` into a table whose projection has a subquery in its `SELECT` list or `GROUP BY`
Related: #118375
Related: #129863
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
INSERT,MATERIALIZE PROJECTIONand merges failing withLOGICAL_ERRORProjection ... calculated the columns ... while its metadata describes the columns ...for a table with a projection that has a subquery (x IN (SELECT ...),EXISTS (SELECT ...)) in itsSELECTlist orGROUP BYand aWHEREclause or aggregation.Description
On 26.10.2.103,
INSERT INTO t VALUES (1, 1)intot (a UInt32, b UInt32, PROJECTION p (SELECT b IN (SELECT b FROM src), a, b WHERE a > 0 ORDER BY b))fails withCode: 49 ... Projection p calculated the columns in(b, __subquery_X), a, b while its metadata describes the columns a, b, in(b, __subquery_Y). Found by the AST fuzzer: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=129764&sha=7327e93b8ee576277e7ba9a36190e846b366ddac&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%2C%20old_compatibility%29A subquery is named by the hash of its AST. The projection metadata is named from the stored query AST, while
calculateByQuery(since #118375) names the calculated columns by converting the query tree back to AST, which does not reproduce the subquery (for example it addsFROM system.one), so the hash differs. It affectsIN,GLOBAL IN,NOT IN,EXISTS, nested and aggregate arguments, andGROUP BYkeys, whenever the projection is calculated by a query (WHEREor aggregation).The change:
collectUnresolvedNodeNamestakes the name of a function from the AST it was built from, which is the AST the metadata was named from, and keepstoASTas the fallback.calculateByQueryalso pinsrewrite_in_to_join = 0, because that rewrite replaces theINnode and its name would be lost again.Validation: the new test fails on master in every arm (normal projection with
WHEREacrossDETACH/ATTACH, the fuzzer'sGLOBAL INwithGLOBAL JOIN, aggregate projection,MATERIALIZE PROJECTION,INSERTunderrewrite_in_to_join) and passes with the fix; 50 randomized runs pass; all 356 stateless tests matchingprojectionpass.Not changed: these column names embed an AST hash that differs between versions, so a part written by another version lacks such a column; no reader of it was observed.
Workflow [PR]
Sync PR [sync-upstream/pr/130143]
Version info
26.11.1.1676-master(included in26.11and later)26.10.11.5