Skip to content

Push down volume-reducing functions in query plan - #106199

Merged
alexey-milovidov merged 72 commits into
ClickHouse:masterfrom
fastio:feature-push-down-volume-reducing-functions
Aug 21, 2026
Merged

alexey-milovidov merged 72 commits into
ClickHouse:masterfrom
fastio:feature-push-down-volume-reducing-functions

Conversation

@fastio

@fastio fastio commented May 31, 2026 •

Copy link
Copy Markdown
Contributor

Adds a query plan optimization that pushes volume-reducing functions (length, lengthUTF8, empty, notEmpty) below the Sorting and Filter steps, so those steps carry the fixed-size result instead of the original String / FixedString argument.

The rewrite is done with ActionsDAG::split: the function nodes form the first part, which becomes a new step below the child, and the second part becomes the parent step's ActionsDAG and reads the results as inputs. It only fires when the wide argument column really stops flowing through the child step — the column is removed from the child's output, and unless the child reads it itself it is not even produced below the child. tryExecuteFunctionsAfterSorting and trySplitFilter keep the pushed functions where they are, so the optimizations do not move the same nodes in opposite directions.

For the shape from the issue below, SELECT avg(length(s)) FROM test WHERE notEmpty(s), the filter still needs s to evaluate its condition, but length(s) is now computed before the filter, so the wide column is not copied for the surviving rows.

Enabled by default; can be turned off with query_plan_push_down_volume_reducing_functions = 0.

An earlier attempt at the same optimization was #86139 by @talmawash, credited as a co-author.

Closes: #82378
Related: #86139

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Added a query plan optimization that pushes volume-reducing functions (length, lengthUTF8, empty, notEmpty) below the Sorting and Filter steps, replacing the wide String / FixedString argument with the fixed-size result, so it is neither buffered by a sort nor copied by a filter. Controlled by the new setting query_plan_push_down_volume_reducing_functions, enabled by default.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Workflow [PR]
Sync PR [sync-upstream/pr/106199]

Version info

  • Merged into: 26.8.1.1857 (included in 26.8 and later)

fastio added 2 commits May 31, 2026 21:24
Move length / lengthUTF8 / empty / notEmpty below Filter, Sort, Limit
and Expression steps so the wide source column gets pruned from the
intermediate steps. Disabled by default; enable via the new setting
query_plan_push_down_volume_reducing_functions. References ClickHouse#82378.
@EmeraldShift

Copy link
Copy Markdown
Contributor

How does[n't] this relate to #86139?

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8400cc8]

Summary: ✅


AI Review

Summary

This PR adds a query-plan optimization that pushes supported volume-reducing functions below Sorting and Filter steps so wide String / FixedString arguments do not have to cross those barriers. The optimizer logic and stateless regressions look substantially tightened compared with the earlier revisions, but the performance evidence is still incomplete: part of the new perf XML does not exercise the rewrite that the PR claims to improve.

Tests
  • ⚠️ The new benchmark in tests/performance/push_down_volume_reducing_functions.xml still includes no-op cases for this PR. length(arr) is explicitly unsupported by the current isSupportedArgumentType gate, and the motivating avg(length(s)) ... WHERE notEmpty(s) query is only a pushdown case when optimize_functions_to_subcolumns = 0 (the stateless regression suite already pins that setting off for exactly that reason). As written, the perf job does not fully measure the new filter/supported-type behavior behind the Performance Improvement category.
Final Verdict

Needs changes before merge: the added performance benchmark should be adjusted so it actually measures the optimization paths this PR introduces.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.00% 87.00% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 396/446 (88.79%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jun 1, 2026
@fastio
fastio force-pushed the feature-push-down-volume-reducing-functions branch from 7a05bc2 to ce7c70b Compare June 2, 2026 09:23
fastio added 2 commits June 14, 2026 12:23
Restore the unconditional `arrayJoin` guard in `tryExecuteFunctionsAfterSorting`
that a dangling `if` had gated behind the opt-in setting (reintroducing ClickHouse#82279 in
the default plan), and run all bail-out guards in `tryPushDownVolumeReducingFunction`
before mutating the parent so a late bail cannot leave the plan rewritten without
the pushed step. Also drop the unrelated `USE_NURAFT` guard accidentally added to
`gtest_compact_children_set.cpp`.
@clickhouse-gh

clickhouse-gh Bot commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 34 queries analysed

This PR adds a new query-plan optimization that pushes the string-length functions length, lengthUTF8, empty, and notEmpty below Sorting and Filter steps, replacing the wide String/FixedString argument with the fixed-size result. Neither flagged TPC-H query exercises those functions, so the change cannot plausibly alter their execution paths. Q17's flagged regression (median 64 to 101 ms) is downgraded to not sure because the source runs were very noisy and the diff is off-path; Q7 was already inside the master variance band and is likewise run-to-run variance. No PR-attributable regressions or improvements were found in the flagged set.

clickbench

🟢 No significant changes

tpch_adapted_1_official

⚠️ 2 inconclusive

Flagged queries (2 of 22)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 7 not_sure 67 79 +17.9% <0.0001 The +17.9% sits inside the wide master variance band (66-560 ms), and Q7 runs no length/empty/notEmpty on strings for this plan optimization to touch, so this reads as run-to-run variance.
⚠️ 17 not_sure 64 101 +57.8% <0.0001 Q17 executes no length/empty/notEmpty on strings, so this string-length plan optimization cannot affect its execution; the +57.8% pairs with highly variable source runs and reads as noise.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

Debug info
  • StressHouse run: 8bb20ce6-befd-4b00-b511-d313f10af50a
  • MIRAI run: 5246fb5e-304e-4bd5-aa53-b68267711339
  • PR check IDs:
    • clickbench_389312_1787293695
    • clickbench_389318_1787293694
    • clickbench_389328_1787293694
    • tpch_adapted_1_official_389354_1787293694
    • tpch_adapted_1_official_389382_1787293694

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged current master and fixed the Fast-test failure. The alias test now reflects the optimized plan: the unused sibling alias is removed before the FilterStep, so pushing length(a) is beneficial. Verified locally with 04101_volume_reducing_function_push_down using the rebuilt binary. CI failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106199&sha=6c77e761ec3fd0ec7fd77e9d9015ad28970b3db3&name_0=PR&name_1=Fast%20test

@novikd novikd self-assigned this Aug 18, 2026
alexey-milovidov and others added 2 commits August 21, 2026 02:22
The check is `countIf(explain LIKE '%[volume-reducing functions]%') = 0`,
which returns `1` when nothing is pushed — the desired outcome the test
title states. The reference wrongly expected `0`, contradicting both the
intent and the actual (correct) plan output in the Fast test:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106199&sha=92c2e55413f979659320dae421d916af4ffea6ab
ClickHouse#106199

Co-Authored-By: Claude Fable 5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged current master and fixed the failing reference on 92c2e55: the mixed-filter-functions check is countIf(...) = 0, which yields 1 when nothing is pushed — exactly the behavior the code change achieves; the reference wrongly expected 0. Verified 04101_volume_reducing_function_push_down locally with the rebuilt binary after the merge. CI on the previous run: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106199&sha=92c2e55413f979659320dae421d916af4ffea6ab

Comment on lines +20 to +24
<query>SELECT length(arr) FROM volume_reducing_functions ORDER BY key FORMAT Null</query>

<!-- The wide column is only needed to evaluate the condition, so it is no longer copied
by the filter. This is the shape from issue #82378. -->
<query>SELECT avg(length(s)) FROM volume_reducing_functions WHERE notEmpty(s) FORMAT Null</query>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This benchmark still gives false confidence for the Performance Improvement claim because two of the added queries do not exercise this optimization at all.

  • length(arr) is now permanently rejected by isSupportedArgumentType in pushDownVolumeReducingFunctions.cpp, so this line is a no-op on both the baseline and patched binaries.
  • The motivating avg(length(s)) ... WHERE notEmpty(s) shape only measures this pass when optimize_functions_to_subcolumns = 0; the new stateless regression suite pins that setting off for exactly that reason. Here it is left at the default, so the baseline can already rewrite length(s) through subcolumns and this benchmark misses the new filter case.

Please replace these with queries the new rewrite actually owns, for example by disabling optimize_functions_to_subcolumns for the issue-shaped filter query and by dropping the Array case or switching it to a supported String / FixedString workload.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 21, 2026
Merged via the queue into ClickHouse:master with commit 55e5ac5 Aug 21, 2026
177 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 21, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

@fastio @alexey-milovidov — title/description suggestions:

Description (currently incomplete):

  • Why: Does not mention that optimize_functions_to_subcolumns (default 1) rewrites length(s), empty(s), notEmpty(s) and length(arr) into .size/.size0 subcolumn reads before this pass runs. For the flagship shape SELECT avg(length(s)) FROM test WHERE notEmpty(s) the plan at default settings is avg(s.size) with Filter: s.size != 0 and is byte-identical with the new setting on and off — so the quoted issue shape is not actually affected by this PR at defaults.

  • Suggested addition:

    Note on reachability: optimize_functions_to_subcolumns (default 1) rewrites length/empty/notEmpty over String and Array into .size/.size0 subcolumn reads in the analyzer, before this plan pass runs. At default settings the pass therefore fires only for shapes the subcolumn rewrite leaves alone (lengthUTF8, FixedString arguments, or when the same column is also read under another name). In particular SELECT avg(length(s)) FROM test WHERE notEmpty(s) — the shape from Certain functions can be pushed up in the query pipeline into WHERE if they reduce data size #82378 — plans identically with the setting on and off.

Ignore if intentional.

@clickgapai

Copy link
Copy Markdown
Contributor

@fastio @novikd @alexey-milovidov — ClickGap found the following in this PR:

Close anything that's wrong or already addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Certain functions can be pushed up in the query pipeline into WHERE if they reduce data size

7 participants