Repository navigation
Cherry pick #117802 to 26.9: Move AI functions to the end of PREWHERE - #127519
Merged
Merged
Conversation
`MergeTreeWhereOptimizer` prices a condition by the size of the columns it reads, so an AI function making one request per row looks nearly free and can be scheduled ahead of a cheap condition. In PREWHERE it is then evaluated on every row read instead of only the rows that survive the cheap conditions. On the 28.7M-row `hackernews` dataset, an `aiFilter` written before a selective condition on the same column issued a request for all 24.4M comments; it now issues 21. Co-Authored-By: Claude Opus 5 <[email protected]>
They derive from `IFunction` directly, so the `FunctionBaseAI` override does not reach them. The test now lists every AI function, because a new one can miss the trait the same way. Co-Authored-By: Claude Opus 5 <[email protected]>
The test asserts the outcome of the WHERE-to-PREWHERE move, and `clickhouse-test` randomizes `optimize_move_to_prewhere`. The flaky check drew `False`, so nothing moved and the "cheap condition still moved" column read 0. Co-Authored-By: Claude Opus 5 <[email protected]>
A capture's DAG is not among the condition's children, so `arrayExists(x -> aiFilter(x, ...), arr)` was still moved to PREWHERE and evaluated once per row read. `FunctionCapture` now walks its inner DAG, the same way `isSuitableForConstantFolding` does. Co-Authored-By: Claude Opus 5 <[email protected]>
Also report it from `FunctionExpression`, which covers constant-folded lambdas. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… WHERE Each PREWHERE step sees only the rows passing the previous ones, so an AI function is called only for rows passing the cheaper conditions, and PREWHERE still saves reading the remaining columns. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
With `allow_reorder_prewhere_conditions = 0` an expensive condition is moved to PREWHERE in the written order. The test reads the final order from `EXPLAIN json = 1`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…e-ai-functions # Conflicts: # src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp # src/Storages/MergeTree/MergeTreeWhereOptimizer.h
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…e-ai-functions # Conflicts: # src/Storages/MergeTree/MergeTreeWhereOptimizer.cpp # src/Storages/MergeTree/MergeTreeWhereOptimizer.h
…amic / Variant adaptors Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…e-ai-functions # Conflicts: # src/Storages/MergeTree/MergeTreeSplitPrewhereIntoReadSteps.cpp
A cheap condition that cannot be moved to PREWHERE stays in WHERE, so moving an expensive condition to PREWHERE would evaluate it first. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…e-ai-functions # Conflicts: # src/Functions/FunctionsMiscellaneous.h
…nctions Move AI functions to the end of PREWHERE
Contributor
Author
|
Dear @george-larionov, @rschu1ze, this cherry-pick PR has not been updated for 3d0h1m32s. Please resolve the conflicts to backport #117802, or close this PR if the backport is no longer needed. This PR will be automatically closed after 7 days of inactivity. |
This branch has no `may_throw` in `MergeTreeSplitPrewhereIntoReadSteps` (from #111954), no `hasObservableSideEffects` and no `is_runtime_filter`. Keep the split step as it is here, and port the rest: `isExpensive` for the AI functions and their adaptors, and the `MergeTreeWhereOptimizer` changes that put expensive conditions in their own group and move them to PREWHERE after all other conditions. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 #117802
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