Skip to content

Cherry pick #117802 to 26.9: Move AI functions to the end of PREWHERE - #127519

Merged
PedroTadim merged 20 commits into
backport/26.9/117802from
cherrypick/26.9/117802
Oct 30, 2026
Merged

PedroTadim merged 20 commits into
backport/26.9/117802from
cherrypick/26.9/117802

Conversation

@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

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-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

george-larionov and others added 19 commits October 1, 2026 21:39
`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
…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
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 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 Oct 23, 2026
@robot-clickhouse-ci-1

Copy link
Copy Markdown
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]>
@PedroTadim
PedroTadim merged commit e5c75f9 into backport/26.9/117802 Oct 30, 2026
1 check passed
@PedroTadim
PedroTadim deleted the cherrypick/26.9/117802 branch October 30, 2026 08:41
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