Repository navigation
Cherry pick #122512 to 26.3: Fix constant-condition if/multiIf GROUP BY keys with the identifier resolve cache and group_by_use_nulls - #132938
Closed
robot-ch-test-poll3 wants to merge 10 commits into
Closed
robot-ch-test-poll3 wants to merge 10 commits into
robot-ch-test-poll3 wants to merge 10 commits into
Conversation
…onPass
The pass folded in `enterImpl`. `InDepthQueryTreeVisitorWithContext::visit`
then descends into the children of the replacement without entering the
replacement itself, so one visit folded only every other level of a chain of
constant conditions: `if(0, n, if(0, n, if(0, n, if(0, n, n))))` became
`if(0, n, if(0, n, n))`.
With `enable_identifier_resolve_cache` (on by default since 26.6) every later
lookup of a GROUP BY alias returns the node already resolved for it, so the
GROUP BY key and its ORDER BY / HAVING / LIMIT BY / GROUPING SETS references
are one shared node, while the projection is a separate copy. The visitor does
not deduplicate, so it reaches the shared node once per referencing slot, and
the second visit folds one more level in place, inside the shared parent. The
key then no longer matches the projection by action name, the planner tries to
recompute the projection from a column that aggregation has consumed, and the
query fails with NOT_FOUND_COLUMN_IN_BLOCK, e.g.
SELECT if(0, s, if(0, s, if(0, s, if(0, s, s)))) AS r FROM t GROUP BY r ORDER BY r
Folding in `leaveImpl` decides a node after its arguments, so a chain
collapses completely in one visit, visiting an already folded node again is a
no-op, and every copy of the expression converges to the same result with the
cache on or off. The fold rule itself is unchanged. The old analyzer's
equivalent, `OptimizeIfWithConstantConditionVisitor`, was bottom-up as well.
Closes: #122404
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…p_by_use_nulls
`IfConstantConditionPass` runs after analysis and rewrites a GROUP BY key that
is an `if`/`multiIf` with a constant condition to the branch it always takes,
through every nested constant condition. When that branch is a column, the
aggregation groups by the column, and ROLLUP/CUBE/GROUPING SETS output it as
Nullable under `group_by_use_nulls`. A correlated reference to the column
matched no registered key, so it kept its non-Nullable type while its
placeholder was bound to the Nullable header column. For a `Bool` column the
cast wrapper then received a `ColumnNullable` and failed with
`Bad cast from type DB::ColumnNullable to DB::ColumnVector<char8_t>`:
SELECT (SELECT c0) FROM (SELECT 1::Bool) t0(c0)
GROUP BY if(1, c0, false) WITH ROLLUP SETTINGS group_by_use_nulls = 1
The analyzer now registers every column such a key may collapse to, through
the branches of nested `if`/`multiIf`, without checking the conditions: one
can become constant only in a later pass, for example when
`optimize_inverse_dictionary_lookup` replaces a dictionary lookup that matches
no key with `0`. The correlated-column lookup converts a reference to such a
column to Nullable, as it does for a reference to a key. A column the
aggregation does not group by is not in its output, so such a reference is
rejected as before. The column is registered for correlated references only,
since for other expressions it is not a key.
The test of the bottom-up fold is renumbered from 05267 to 05272, since 05267
is used by another open PR.
Related: #106377
Related: #100173
Co-Authored-By: Claude Opus 5.5 <[email protected]>
With `group_by_use_nulls` and ROLLUP/CUBE/GROUPING SETS, the analyzer
replaces an expression equal to a GROUP BY key with a copy of the key made
Nullable. `IfConstantConditionPass` folded a constant-condition key to its
branch but never such a copy, whose type is the Nullable form of the branch
type, so the copy no longer matched the key by name and was computed from
the aggregation output, which lacks the other branch's columns:
SELECT if(0, b, a) AS k, count() FROM t GROUP BY k WITH ROLLUP
SETTINGS group_by_use_nulls = 1
failed with NOT_FOUND_COLUMN_IN_BLOCK. With the bottom-up fold this also
broke keys such as `if(0, b, if(0, b, a))`, which the top-down fold left
matching the copy.
The pass now folds such a copy to its branch made Nullable, as
`ComparisonTupleEliminationPass` does (#96426). Constant branches are left
as they were.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
ROLLUP, CUBE and GROUPING SETS take their grouping sets from the distinct
GROUP BY keys: the planner merges keys with the same action name, and
grouping() is resolved against the keys before the pass runs. Folding
every level of a constant-condition chain can make two different keys
equal:
SELECT a, if(0, b, if(0, b, a)) AS k, count() FROM t
GROUP BY ROLLUP(a, k)
lost the grouping set that rolls up only `k`. A single level such as
`if(0, b, a)` was merged the same way before.
The pass now leaves a query unfolded when folding would make two of its
ROLLUP/CUBE/GROUPING SETS keys equal.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the nullable_collapsed_group_by_key_columns and registerConstantConditionBranchColumns comments to one line each; the reasons are in the commit messages above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The skip for a query whose folded keys would merge did not match the
merge it guards against.
It stopped the traversal at the skipped query, so its subqueries were not
folded either. A correlated subquery under `group_by_use_nulls` inside such
a subquery then failed with NOT_IMPLEMENTED (can't find correlated column),
where master returns rows. The skip now covers only the query's own
expressions, and each nested query decides for itself.
It also fired when the keys that become equal are in the same grouping
sets:
SELECT (SELECT c0) FROM t0
GROUP BY GROUPING SETS ((if(1, c0, 0), if(1, c0, 2)), ())
Merging such keys changes neither the grouping sets nor any value, and
without the fold the correlated read of c0 failed with NOT_IMPLEMENTED
where master returns rows. The skip now requires two keys that become
equal to be in different grouping sets, which any two ROLLUP or CUBE keys
are.
It compared keys without aliases, while the planner tells apart the
columns of two sources that differ only in alias. With `s2` and `s3` the
same subquery under two aliases, it took
if(0, s2.b, if(0, s2.b, s1.a)), if(0, s3.b, if(0, s3.b, s1.a))
for one key, so folding both to `s1.a` lost CUBE and ROLLUP grouping sets,
and it took the folded keys `s1.a` and `s2.a` for one key, so it skipped a
query that merges nothing. Keys are now compared as the planner compares
them: without the aliases in the expression, and with each column named
after the unique alias of its source.
Hashing a column hashed its whole source as well, so the check made the
analysis of 1200 keys from one subquery take 0.91 s instead of 0.61 s
(2.76 s instead of 1.57 s for 2400). Naming the columns after their
sources removes that: 0.62 s and 1.58 s, also when the keys can fold.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tiIfPass
With `group_by_use_nulls`, a clause that reads a ROLLUP/CUBE/GROUPING SETS
key after the aggregation reads a copy of the key that is made Nullable.
IfChainToMultiIfPass compared the type of the `multiIf` with that Nullable
type, so it rewrote an `if` chain in the key but not in the copy. The copy
then has another name than the key and is computed from the columns it
reads instead of being read from the aggregation: the query fails with
NOT_FOUND_COLUMN_IN_BLOCK, or, when those columns are other keys, returns
the value of the chain where the key is rolled up:
SELECT a, b, if(a = 'x', a, if(b = 'y', b, a)) AS k, count()
FROM (SELECT 'x' AS a, 'w' AS b) GROUP BY ROLLUP(a, b, k) ORDER BY ALL
SETTINGS group_by_use_nulls = 1, optimize_if_chain_to_multiif = 1
returned `x w x` twice and `x \N x` instead of `x w \N` and `x \N \N`
(26.3 to 26.9 and master). The pass now compares with the type of the
function itself and keeps the copy Nullable, as ComparisonTupleEliminationPass
does since #96426.
The ROLLUP/CUBE/GROUPING SETS skip in IfConstantConditionPass leaves such a
chain in place when folding it would merge two keys, so without this
`ROLLUP(a, if(0, b, if(0, b, a)))` failed with NOT_FOUND_COLUMN_IN_BLOCK
under optimize_if_chain_to_multiif = 1, where master returns rows.
This is the `if` chain query of #103419; its `multiIf` query
(MultiIfToIfPass) is not changed.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tantConditionPass` folds the keys, renumber tests
…ld-bottom-up Fix constant-condition `if`/`multiIf` GROUP BY keys with the identifier resolve cache and `group_by_use_nulls`
Collaborator
|
I suggest closing this 26.3 cherry-pick instead of resolving it.
If 26.3 should get this anyway, the two PRs have to go together; reply here and I will push a resolution of the pair to my fork. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #122512
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-picklocally.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:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job