Skip to content

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 - #132990

Merged
robot-ch-test-poll2 merged 3 commits into
backport/26.10/130143from
cherrypick/26.10/130143
Nov 22, 2026
Merged

robot-ch-test-poll2 merged 3 commits into
backport/26.10/130143from
cherrypick/26.10/130143

Conversation

@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

Original pull-request #130143

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 2 commits November 8, 2026 12:43
… 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 #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]>
…ubquery-names

Fix `LOGICAL_ERROR` on `INSERT` into a table whose projection has a subquery in its `SELECT` list or `GROUP BY`
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Nov 19, 2026
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]>
@groeneai

Copy link
Copy Markdown
Collaborator

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, and neither is on 26.10. I kept the 26.10 code and added only the rewrite_in_to_join line.

The resolution also takes the three lines of #129006 that materialize constant columns of the calculated projection block (convertToFullColumnIfConst, at the same place as on master). On 26.10 the t_subquery_normal case of the new test has a projection column exists(SELECT ...) that is calculated as a ColumnConst, and without those lines writing the projection part fails with Bad cast from type DB::ColumnConst to DB::ColumnVector<char8_t>. The rest of #129006 is not needed on 26.10, because its trigger optimize_constant_columns_after_filter does not exist there. So the diff against backport/26.10/130143 is the diff of #130143 plus those three lines.

On a 26.10 debug build, 05569_projection_calculation_subquery_names passes in 50 of 50 randomized runs, and every other test matching projection that fails on my machine fails the same way without the change. Without the change, every case of 05569 fails with the original Projection p calculated the columns ... error.

I cannot push to cherrypick/26.10/130143. The branch on my fork has this PR's head fa0eb19c6db6 as an ancestor, so applying it is a fast-forward:

git fetch https://github.com/groeneai/ClickHouse.git resolve-26.10-cherrypick-130143
git push origin FETCH_HEAD:cherrypick/26.10/130143

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Applied the resolution from the comment above: cherrypick/26.10/130143 is fast-forwarded to 85c0d85587ec, a merge of backport/26.10/130143 (66c617140dc3) into the previous head fa0eb19c6db6.

I reviewed it before pushing. The diff against backport/26.10/130143 is limited to src/Storages/ProjectionsDescription.cpp and the 05569_projection_calculation_subquery_names test: the change of #130143 plus the three convertToFullColumnIfConst lines of #129006, at the same place as on master. The rewrite_in_to_join setting exists on 26.10, and a -fsyntax-only check of ProjectionsDescription.cpp on the 26.10 tree passes. I did not rerun the test locally.

@robot-ch-test-poll2
robot-ch-test-poll2 merged commit 9ffed1b into backport/26.10/130143 Nov 22, 2026
2 checks passed
@robot-ch-test-poll2
robot-ch-test-poll2 deleted the cherrypick/26.10/130143 branch November 22, 2026 15:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants