Repository navigation
Cherry pick #131361 to 26.10: Hash each AND-chain operand once in optimize_and_compare_chain - #132486
Conversation
`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]>
…cessed, as before. 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`
|
The only conflict is one line in On the 26.10 tree (clang-22, debug): 72 of the 76 stateless tests that use I cannot push to |
|
The |
f378d83 to
835ba43
Compare
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-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