Skip to content

Leave the row estimate of a filtered array join unknown in join reordering - #128248

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:filtered-array-join-estimate-unknown
Oct 30, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:filtered-array-join-estimate-unknown

Conversation

@groeneai

@groeneai groeneai commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #128220
Related: #126491

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...

Description

Since #126491 (master only, not in 26.10), a join with an ARRAY JOIN or arrayJoin subquery that filters the array-joined elements estimates that input as 100 rows per input row, as if the filter did not exist. The hash join then builds from the other input, which can be much larger.

The array-join branch of estimateReadRowsCount estimates its input without the filter, which is correct because the filter reads the array join output. But it then multiplied by 100 without checking either the filter passed from above (query_plan_fuse_filter_into_array_join = 0, or a filter the fuse pass does not take) or the step's own element filter (ArrayJoinStep::hasElementFilter). The MergeTree and sub-join branches of the function return no estimate for a filter they cannot account for.

A filtered array join now has no row estimate, unless its input is estimated at 0 rows (the #126481 case). An unfiltered array join keeps the estimate of 100 rows per input row.

05406_join_swap_array_join_estimate gets four new cases: element filter, filter step, a non-lowered arrayJoin with a filter, and an empty input with an element filter. The first three fail on master. On a debug build, a 5,000,000-row table joined to an ARRAY JOIN over 100,000 rows filtered to 11 builds the hash table from 5,000,000 rows on master (344 MiB peak) and from 11 rows with this change (4.3 MiB). The result is the same.

Trade-off: when the element filter is unselective and the array join is written on the right, the written order is kept, as on 26.10. A 1,000-row table joined to the same array join with WHERE k >= 0 builds 100,000 rows instead of 1,000.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.944-master (included in 26.11 and later)

groeneai and others added 2 commits October 30, 2026 13:30
…ering

Since ClickHouse#126491 the array-join branch of `estimateReadRowsCount` estimates an
array join as 100 rows per input row. It ignored both the filter passed from
above (a `FilterStep` over the array join, e.g. with
`query_plan_fuse_filter_into_array_join = 0`) and the step's own element
filter (`ArrayJoinStep::hasElementFilter`), so a filtered array join got a
known, inflated row count and `swap_on_sizes` built the hash table from the
other, possibly much larger, input.

Like the other branches of the function do for a filter they cannot account
for, a filtered array join now has no row estimate, unless its input is
estimated at 0 rows, which a filter cannot change. An unfiltered array join
keeps the multiplier.

Closes ClickHouse#128220

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The build side alone also passes with any known estimate of `s` up to 1000 rows, so N1-N3 now also count the plans showing `s[no_stats~?]`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 rounds, 1 finding, 0 in round 2) plus my own review.

# Sev Finding Verdict Evidence / action
1 ⚠️ New 05406 arms check only the build side, not the unknown estimate or the fused carrier AGREE in part, fixed @ 0d0dad8 Added a s[no_stats~?] count to the three filtered arms. Not asserting Element filter column: the fuse pass is off under serialize_query_plan (fuseFilterIntoArrayJoin.cpp:16), which the distributed-plan jobs set
2 💡 ReadFromMemoryStorageStep also ignores a filter DISAGREE (out of scope) Same on 26.10 and before #126491, a different read branch; not this regression
3 💡 A FilterStep whose own actions compute arrayJoin skips the array-join branch DISAGREE (out of scope) Needs query_plan_lower_array_join_function = 0, unchanged since 26.10, under-estimates instead

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261030-140400

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The extended 05406_join_swap_array_join_estimate.sql through clickhouse local, no randomization: the three filtered cases print 0 1 (the other input is built) on master every run. The issue's own block reproduces as well.
b Root cause explained? The array-join branch of estimateReadRowsCount multiplies its input's estimate by 100 without checking the filter passed from above or ArrayJoinStep::hasElementFilter, so a filtered array join gets a known, inflated row count and swap_on_sizes builds from the other side.
c Fix matches root cause? Yes. A filtered array join now has no estimate (input estimated at 0 stays 0), which is what the other branches of the same function do with a filter they cannot account for.
d Test intent preserved / new tests added? Yes. 05406 is extended (additions only) with element filter, filter step, non-lowered arrayJoin with a filter, and empty input with an element filter. Its existing lines are unchanged and pass.
e Both directions demonstrated? Yes. Master: the three filtered cases fail (locally, and 20/20 randomized runs). Fix: the full reference matches; 50/50 randomized runs and 20/20 with --replicated-database pass.
f Fix is general across code paths? Yes. One condition covers both filter carriers and both step kinds (ArrayJoinStep, ExpressionStep with arrayJoin), and every consumer of the estimate (join reordering, hash join swap, distributed plan, runtime filter sizing) reads this function. Each case was checked with a mutant that reverts it.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A for types (plan-time row count). Boundaries covered: input estimated at 0, unknown input, unfiltered array join, both values of query_plan_fuse_filter_into_array_join.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; plan only, results identical.
i Invariants and contracts preserved? Yes. An unknown estimate is written as nullopt with imprecise_estimate = true, as the sub-join branch does; column statistics handling is unchanged.

Session id: cron:clickhouse-impl-slot-5:20261030-125800

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @vdimir @davenger, could you review this and add the can be tested label so CI runs? Since #126491, an array join whose elements are filtered is estimated at 100 rows per input row as if it were unfiltered, so the hash join builds from the larger side; this leaves that estimate unknown (an input estimated at 0 rows stays 0), as the other branches of estimateReadRowsCount do for a filter they cannot account for.

@clickhouse-gh

clickhouse-gh Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0d0dad8]

Summary: ✅


AI Review

Summary

This PR makes the row estimate unknown for a filtered ARRAY JOIN or arrayJoin, while preserving zero for an empty input and the existing multiplier for unfiltered inputs. No actionable findings. The focused 05406_join_swap_array_join_estimate regression test passes in CI at 0d0dad8e5149ba5c949dc90cfeb5c1a6da0ea6b2; the description provides before/after memory measurements and explains the tradeoff for unselective filters.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 30, 2026
@cv4g cv4g added the can be tested Allows running workflows for external contributors label Oct 30, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Oct 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 0d0dad8e5 with master 509fb3681 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 17 s compile time in total, 7 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov self-assigned this Oct 30, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 30, 2026
Merged via the queue into ClickHouse:master with commit 85015d0 Oct 30, 2026
354 of 355 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 Oct 30, 2026
@groeneai

groeneai commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator Author

#126491 now has v26.8-must-backport (cherry-picks #128690 for 26.8 and #128691 for 26.9, backport #128692 for 26.10), but this PR, which fixes the regression #126491 introduced (#128220), has no backport label. Without it, those branches get #128220. I measured it on the 26.8 and 26.9 resolutions of the cherry-picks: with a filter on the array-joined elements, the hash join builds from the larger table (0 1 instead of 1 0 in the cases this PR adds to 05406), while 26.8.34.13 and 26.9.25.15 build from the array join side. #128692 carries the same array join estimate as master had between the two merges. A v26.8-must-backport label here would keep the two changes together; its 26.8 and 26.9 cherry-picks will conflict the same way, and I can resolve them.

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Nov 2, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 3, 2026
Backport #128248 to 26.10: Leave the row estimate of a filtered array join unknown in join reordering
@vdimir vdimir added v26.8-must-backport and removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Nov 3, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 3, 2026
alexey-milovidov pushed a commit that referenced this pull request Nov 4, 2026
`estimateReadRowsCount` is in `optimizeJoin.cpp` on 26.8: it moved to
`RelationStatisticsEstimator.cpp` only with #120232, which is master-only.
So the change of #128248 is applied there, on top of the 26.8 resolution
of #126491, with one adaptation: `ArrayJoinStep` has no element filter on
26.8 (`query_plan_fuse_filter_into_array_join` does not exist there), so
only a filter above the step leaves the row count unknown.

The test `05406_join_swap_array_join_estimate` drops the settings that do
not exist on 26.8 (`query_plan_fuse_filter_into_array_join`,
`query_plan_lower_array_join_function`), so its "element filter" and
"filter step" queries are the same query there. The reference is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-clickhouse added a commit that referenced this pull request Nov 4, 2026
Cherry pick #128248 to 26.8: Leave the row estimate of a filtered array join unknown in join reordering
robot-clickhouse added a commit that referenced this pull request Nov 4, 2026
alexey-milovidov pushed a commit that referenced this pull request Nov 4, 2026
`estimateReadRowsCount` is in `optimizeJoin.cpp` on 26.9: it moved to
`RelationStatisticsEstimator.cpp` only with #120232, which is master-only.
So the change of #128248 is applied there, on top of the 26.9 resolution
of #126491. The hunk is the same as on master.

The test `05406_join_swap_array_join_estimate` is the same as on master.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
robot-ch-test-poll4 added a commit that referenced this pull request Nov 4, 2026
Cherry pick #128248 to 26.9: Leave the row estimate of a filtered array join unknown in join reordering
robot-clickhouse added a commit that referenced this pull request Nov 4, 2026
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 4, 2026
@PedroTadim PedroTadim added v26.8-must-backport and removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR v26.8-must-backport v26.10-must-backport labels Nov 4, 2026
@robot-clickhouse robot-clickhouse added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 4, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 4, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 4, 2026
Backport #128248 to 26.8: Leave the row estimate of a filtered array join unknown in join reordering
clickhouse-gh Bot added a commit that referenced this pull request Nov 4, 2026
Backport #128248 to 26.9: Leave the row estimate of a filtered array join unknown in join reordering
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
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 comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-not-for-changelog This PR should not be mentioned in the changelog pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Array-join filter ignored in join estimate, wrong side picked for hash build

10 participants