Skip to content

Cherry pick #125828 to 26.3: Fix wrong results and NOT_FOUND_COLUMN_IN_BLOCK for a GROUP BY key inside an if chain or an ORDER BY tuple - #133202

Merged
robot-ch-test-poll2 merged 4 commits into
backport/26.3/125828from
cherrypick/26.3/125828
Nov 22, 2026
Merged

robot-ch-test-poll2 merged 4 commits into
backport/26.3/125828from
cherrypick/26.3/125828

Conversation

@robot-clickhouse-ci-2

Copy link
Copy Markdown
Contributor

Original pull-request #125828

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 3 commits October 14, 2026 15:37
After `GROUP BY` only the keys and the aggregates exist, and the planner
matches a subexpression to a key column by its action name. Two passes
dissolved a key node into its arguments in such an expression, so the
planner recomputed it from the arguments: those either do not exist after
aggregation (`NOT_FOUND_COLUMN_IN_BLOCK`), or, under `group_by_use_nulls`,
are not `NULL` on the subtotal rows where the key is `NULL` (wrong results).

- `IfChainToMultiIfPass` spliced every nested 3-argument `if` of an else
  branch into one `multiIf`, also a key or its `group_by_use_nulls` copy.
- `OrderByTupleEliminationPass` split every `ORDER BY tuple(...)`, also a
  tuple key. The analyzer expands top-level tuple keys, so such a key comes
  from `IfConstantConditionPass` folding `if(1, tuple(a), tuple(0))`.

#122512 made `IfConstantConditionPass` also fold the `Nullable` copy of a
constant-condition key, so the constant-condition forms of #125803 and
#125804 regressed on master; a bare `if` key nested in a chain, and the
`group_by_use_nulls = 0` forms, were already broken on 26.3 - 26.9.

Both passes now ask `isGroupByKeyAfterAggregation`: the node is a copy on
which `convertToNullable` was called (new `FunctionNode::isConvertedToNullable`,
which also covers an `Array` copy whose type cannot become `Nullable`), or
equal to a key by `compareGroupByKeys`. `IfChainToMultiIfPass` rewrites
nodes while it walks the query, and with the identifier resolve cache a key
node is shared with its references, so it classifies the query's `if` nodes
by pointer when it enters the query, before rewriting it, and only when the
setting is on and the query has an `if` key. The check for the copy's own
rewrite now uses the same flag instead of comparing result types.

Closes #125803
Closes #125804

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the collectGroupByKeyIfNodes and isGroupByKeyAfterAggregation comments to their invariants.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-and-order-by-tuple

Fix wrong results and `NOT_FOUND_COLUMN_IN_BLOCK` for a `GROUP BY` key inside an `if` chain or an `ORDER BY` tuple
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 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 20, 2026
Resolve the 26.3 conflicts of the cherry-pick of #125828:

- IfChainToMultiIfPass.cpp: keep the 26.3 "result type changed" return.
  The conflicting master hunk changes the `group_by_use_nulls` multiIf
  rewrite that #122512 added; 26.3 does not have #122512, so there is
  nothing to change there. All other hunks of #125828 apply as is.
- OptimizeKeyExpressionsUtils.{h,cpp}: the files do not exist on 26.3
  (master added them in the GROUP BY passes refactor). Add them with only
  the two helpers #125828 introduces, getGroupByKeys and
  isGroupByKeyAfterAggregation; compareGroupByKeys already exists in
  Analyzer/ValidationUtils.h on 26.3.
- 05397 test: `SET enable_analyzer = 1` for the 26.3 old analyzer jobs,
  and drop `enable_identifier_resolve_cache = 1` from one statement (the
  setting does not exist on 26.3). The reference is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator

Unlike #132938, this one is worth having on 26.3: #125828 also fixes queries that fail on 26.3 without #122512. On 26.3.67.30, 8 of the 16 statements of 05397_group_by_key_in_if_chain_and_order_by_tuple fail. For example, SELECT if(1, tuple(a), tuple(0)) AS k, count() FROM (SELECT number % 3 AS a FROM numbers(10)) GROUP BY k ORDER BY k throws NOT_FOUND_COLUMN_IN_BLOCK with default settings, and two optimize_if_chain_to_multiif = 1, group_by_use_nulls = 1 queries return wrong rows.

I pushed a resolution to my fork: groeneai:resolve-26.3-cherrypick-125828 at 2c2c71b2c1ec, a merge of backport/26.3/125828 into this branch (a fast-forward from 2134b447b887). git diff backport/26.3/125828 2c2c71b2c1ec touches only the 7 files of #125828. Differences from master:

Validation on a debug build of that tree: 05397 passes, 50/50 with randomized settings. The same tree without the two pass changes fails the same 8 statements as 26.3.67.30. The 37 related group_by_use_nulls / multiIf / tuple ORDER BY tests that run in clickhouse local pass as on 26.3.67.30.

To apply it:

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

If 26.3 should not get this fix, this PR can be closed.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Applied the resolution from groeneai:resolve-26.3-cherrypick-125828: cherrypick/26.3/125828 is fast-forwarded to 2c2c71b2c1ec, a merge of backport/26.3/125828 (4ee098dd9bc3) into the previous head 2134b447b887.

Checked before pushing: the diff against backport/26.3/125828 has only the 7 files of #125828, and IfChainToMultiIfPass.cpp, OrderByTupleEliminationPass.cpp and OptimizeKeyExpressionsUtils.cpp pass a -fsyntax-only compilation on the 26.3 tree. I did not rerun the tests. The dropped hunk (keeping a rewritten multiIf Nullable for a group_by_use_nulls key copy) belongs to the rewrite from #122512, which 26.3 does not have; there such a node is left as if, as before.

@robot-ch-test-poll2
robot-ch-test-poll2 merged commit 11264ad into backport/26.3/125828 Nov 22, 2026
2 checks passed
@robot-ch-test-poll2
robot-ch-test-poll2 deleted the cherrypick/26.3/125828 branch November 22, 2026 17:15
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