Skip to content

Cherry pick #131361 to 26.10: Hash each AND-chain operand once in optimize_and_compare_chain - #132486

Merged
robot-ch-test-poll2 merged 8 commits into
backport/26.10/131361from
cherrypick/26.10/131361
Nov 18, 2026
Merged

robot-ch-test-poll2 merged 8 commits into
backport/26.10/131361from
cherrypick/26.10/131361

Conversation

@robot-ch-test-poll2

Copy link
Copy Markdown
Contributor

Original pull-request #131361

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 7 commits November 13, 2026 07:24
`tryOptimizeAndCompareChain` keyed every container (comparison pairs,
constants, visited set, derived-conjunct filters, existing conjuncts) by
`QueryTreeNodePtr` and passed raw nodes through the DFS, so every lookup
re-ran `getTreeHash`. The hash of a column also hashes its source, so when
the operands are columns of a large inlined subquery (many non-materialized
CTEs) each lookup walks the whole subquery: about 30 walks per chain for 5
distinct operands. On the query from #131134 (CTE depth 8) the visitor
hashed 16.6M nodes for a 15K-node tree.

Compute a `QueryTreeNodePtrWithHash` once per operand per call and pass the
key through the containers and the DFS (the pattern #108757 used for
`resolveLambda`). The existing-conjunct sets hashed every conjunct and
every candidate; a derived conjunct is always `comparison(operand,
constant)`, so index existing conjuncts of that shape by the key of their
first argument and decide with `isEqual` inside the bucket. The derived set
is unchanged.

Nodes hashed for the #131134 query drop from 16.6M to 3.4M, which is below
the default `optimize_and_compare_chain_max_hash_work` budget, so at
default settings the derivation now completes instead of being reverted.
With the budget off (`compatibility` before 26.7) query analysis is 2.1x
faster.

Related: #131134

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Hashing each operand once removed every `getTreeHash` call from the DFS of
`tryOptimizeAndCompareChain`, so `optimize_and_compare_chain_max_hash_work`
no longer bounded the traversal. Each DFS step now charges the budget the
hash work its precomputed key took, which is what the step used to pay for
looking the operand up.

Related: #131134

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

A lookup by an operand key that `tryOptimizeAndCompareChain` already
computed is charged the hash work of that key, so
`optimize_and_compare_chain_max_hash_work` counts what it counted when
every container access re-hashed the operand. Existing conjuncts of the
shape `comparison(operand, constant)` are indexed by operand key and then
constant key, so deduplicating a derived conjunct no longer scans every
conjunct written on the same operand.

Related: #131134

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…in work budget, as hashing the whole conjunct did.

Related: #131134

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Rewrite the operand-key memo comment as a standalone statement (drop the reference to the previous budget accounting).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nds-once

Hash each `AND`-chain operand once in `optimize_and_compare_chain`
@robot-ch-test-poll2 robot-ch-test-poll2 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 labels Nov 17, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

The only conflict is one line in tryOptimizeAndCompareChain (LogicalExpressionOptimizerPass.cpp): the indexHint branch condition !join_left_side_preserved comes from #126551, which is not on 26.10. I kept the 26.10 condition preserved_join_tables.empty() there and took only this PR's change on that line (insert_conjunct(existing_hint_conjuncts, left.node, and_node) instead of existing_hint_conjuncts.emplace(and_node).second). The rest applies as is: against backport/26.10/131361 the merge changes only the PR's 4 files, and the src/ lines are identical to master's except that condition.

On the 26.10 tree (clang-22, debug): 72 of the 76 stateless tests that use optimize_and_compare_chain or indexHint pass; the other 4 fail only because of my local setup (remote to 127.0.0.2, a fixed port number, missing S3 and user_files fixtures). 04492_optimize_and_compare_chain_work_budget passes 50 of 50 with random settings. For the query in analyzer_and_compare_chain_inlined_ctes.xml, EXPLAIN QUERY TREE gives the same output with and without the change. With optimize_and_compare_chain_max_hash_work = 0 the query takes 456 ms instead of 929-981 ms (median server time; the 26.10 baseline is the same tree without the change).

I cannot push to cherrypick/26.10/131361. The merge is on my fork, and its first parent is this PR's head c14cab494f33, so applying it is a fast-forward:

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

@robot-ch-test-poll2

Copy link
Copy Markdown
Contributor Author

The 26.10 branch moved since this cherry-pick was created (now at 6466c42). Re-tried the merge against it and it applies cleanly, so the bot is proceeding without manual resolution.

@robot-ch-test-poll2
robot-ch-test-poll2 merged commit 4abf7bd into backport/26.10/131361 Nov 18, 2026
1 check passed
@robot-ch-test-poll2
robot-ch-test-poll2 deleted the cherrypick/26.10/131361 branch November 18, 2026 22:23
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-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