Skip to content

Cherry pick #126491 to 26.8: Estimate an array join as 100 rows per input row in join reordering - #128690

Merged
robot-clickhouse-ci-1 merged 6 commits into
backport/26.8/126491from
cherrypick/26.8/126491
Nov 2, 2026
Merged

robot-clickhouse-ci-1 merged 6 commits into
backport/26.8/126491from
cherrypick/26.8/126491

Conversation

@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

Original pull-request #126491

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

groeneai and others added 5 commits October 17, 2026 12:03
An array join multiplies each input row by its array size, which is not
estimated, so since #98533 an `ExpressionStep` with `arrayJoin` gives no row
estimate, and an `ArrayJoinStep` (the lowered `arrayJoin` function and the
`ARRAY JOIN` clause) never gave one. The hash join swap needs an estimate on
both inputs, so a join with an array join input over a read that selects no
rows kept building the other, possibly huge, input.

`RelationStats` now carries `proven_empty`, set only by the read branch whose
exact range analysis selected no rows and kept by steps that cannot add rows.
An array join over such an input is estimated at 0 rows; any other array join
keeps returning no estimate, because a child row count is neither an upper
nor a lower bound and an estimated zero (a keyless aggregation, a truncated
statistics selectivity) is not emptiness.

Related: #126481

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A statistics estimate of 0 rows is only an estimate, so an array join over
such a read kept no estimate and the hash join kept building the other input.
Execution-time part pruning proves the read empty only when it runs: it is
off with `use_statistics_for_part_pruning = 0` (which `compatibility` below
26.4 sets) and under FINAL. The issue's LEFT JOIN query runs with the former,
its INNER JOIN query with FINAL.

When the statistics estimate of a MergeTree read is 0, the estimator now asks
the statistics part pruner whether the read's filter excludes every analyzed
part. The check shares its conditions and its per-part test with
`filterPartsByStatistics`, which is only split, not changed. It does not
depend on `use_statistics_for_part_pruning`, because it skips no part and
only feeds the join order. Under FINAL it holds only for ReplacingMergeTree,
whose FINAL picks stored rows; SummingMergeTree and the other merging modes
compute new rows. It is stricter than part pruning on stale statistics: any
pending mutation or patch part, or a part storing a filtered column with
another type than the table (a killed MODIFY COLUMN), gives no proof.

Related: #126481

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop comments that narrate the new code and shorten the field and function contracts to one line each.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
An array join made the row estimate of its relation unknown, so a hash join
never compared the sides and kept building from the other side, even when the
array join input was empty (#126481). Multiply the input estimate by 100
instead (the sizes of the arrays are unknown), as suggested in review.

This replaces the emptiness proof by range analysis and part statistics of
the previous commits, which is reverted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nput

Estimate an array join as 100 rows per input row in join reordering
@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 Nov 2, 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 #126491 is applied there, with two adaptations:
- the third argument (`for_runtime_filter_transport`) of the recursive call
  does not exist on 26.8 and is dropped;
- `toUInt64Saturating` (added by #124008, master-only) does not exist on 26.8,
  so the file gets a static copy of it with the same body.

The setting `query_plan_lower_array_join_function` does not exist on 26.8
(`arrayJoin` is never lowered to an `ArrayJoinStep` there), so the test
`05406_join_swap_array_join_estimate` drops it from four queries. Those
queries then run the `arrayJoin` expression path, with the same result,
and the reference is unchanged.

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

groeneai commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

The conflict is a modify/delete: RelationStatisticsEstimator.cpp does not exist on 26.8, because it was split out of optimizeJoin.cpp by #120232, which is not on 26.8. I applied the change of #126491 to estimateReadRowsCount in optimizeJoin.cpp instead, with two adaptations: the recursive call drops the for_runtime_filter_transport argument, which 26.8 does not have, and the file gets a static copy of toUInt64Saturating (#124008, also not on 26.8). 26.8 has no query_plan_lower_array_join_function, so the test drops that setting from four queries; the reference is unchanged. Against backport/26.8/126491 the merge changes only optimizeJoin.cpp and the two test files.

On the 26.8 tree (clang-22, debug) 05406_join_swap_array_join_estimate passes, 50 of 50 with random settings. The official 26.8.34.13 fails it: no swap in the four cases where the array join side is empty or small.

I cannot push to cherrypick/26.8/126491. The merge is on my fork and its first parent is this PR's head 4e3df38e874c, so applying it is a fast-forward:

git fetch https://github.com/groeneai/ClickHouse.git groeneai/resolve-26.8-cherrypick-126491
git push origin FETCH_HEAD:cherrypick/26.8/126491

#126491 alone also brings #128220 to 26.8: with a filter on the array-joined elements, this resolution builds the hash table from the larger table (aj_r in the filtered queries of #128248's test), while 26.8.34.13 builds it from the array join side. #128248 fixes that on master and has no backport label.

@robot-clickhouse-ci-1
robot-clickhouse-ci-1 merged commit 8842d6a into backport/26.8/126491 Nov 2, 2026
1 check passed
@robot-clickhouse-ci-1
robot-clickhouse-ci-1 deleted the cherrypick/26.8/126491 branch November 2, 2026 18:35
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.

3 participants