Repository navigation
Estimate an array join as 100 rows per input row in join reordering - #126491
Conversation
An array join multiplies each input row by its array size, which is not estimated, so since ClickHouse#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: ClickHouse#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: ClickHouse#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]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding), plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261017-133800 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261017-130000 |
|
Workflow [PR], commit [ec0d109] Summary: ✅
AI ReviewSummaryThe PR estimates Final VerdictStatus: ✅ Approve |
|
cc @vdimir @CurtizJ, could you review this? An array join input now gets a 0-row estimate only when range analysis or the per-part statistics prove it empty, so the hash join builds from it again (#126481), while the #98533 guard still holds for every other array join; the statistics proof reuses the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 17 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - 035d190Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The two Session id: cron:our-pr-ci-monitor:20261018-080006 |
vdimir
left a comment
There was a problem hiding this comment.
Maybe let's keep current logic as is, but only use some multiplier for array join step instead of turning estimation into unknown?
| try | ||
| { | ||
| return statistics_pruner.checkPartCanMatch(estimates).can_be_true; | ||
| } | ||
| catch (const Exception &) | ||
| { | ||
| tryLogCurrentException(log, fmt::format( | ||
| "Failed to apply statistics pruning for part {}, skipping statistics pruning for this part", | ||
| part.data_part->name), LogsLevel::debug); | ||
| return true; | ||
| } |
There was a problem hiding this comment.
Removed together with the statistics proof in ec0d109; the PR no longer touches MergeTreeDataSelectExecutor.
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 (ClickHouse#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]>
|
Done in ec0d109: an array join ( One measured cost: 2 rows with 1M-element arrays joined to a 100k-row table now build 2M rows (153 MB) instead of 100k (30 MB), as on 26.6. Any multiplier below the real array size does that. I picked 100, the default row estimate PostgreSQL uses for |
|
Checked on the current head (ec0d109) and on it merged with current master, with both queries of #126481 as written there (local
With the PR the join builds from the empty |
CI finish ledger - ec0d109Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261028-193003 |
| remapColumnStats(stats.column_stats, expression_step->getExpression()); | ||
| else | ||
| for (const auto & column : array_join_step->getColumns()) | ||
| stats.column_stats.erase(column); |
There was a problem hiding this comment.
Do we just drop all column stats and keep only the relation cardinality for the array join case? Why is that?
There was a problem hiding this comment.
No, only the array-joined column loses them. The other columns keep their stats: for ExpressionStep they go through remapColumnStats as usual, and for ArrayJoinStep only the columns in getColumns() are erased. Repeating rows (or dropping the ones with an empty array) can only shrink a column's NDV and value range, so those stay valid bounds.
The array-joined column is different because its input stats describe whole arrays, not elements. It can have such stats: they are inherited through CAST or a one-argument function of a column with statistics. With ExpressionStep the ARRAY_JOIN node already stops the lineage in describeActionsDAGLineageHop; ArrayJoinStep replaces the column under the same name, hence the explicit erase.
Measured on a build of this head, t1 with uniq statistics on c1 (7 distinct) and s (1000 distinct), where CAST(s, 'Array(String)') has only 10 distinct elements:
CAST(s, 'Array(String)') AS a, no array join:1000 rows, columns: [a: 1000, c1: 7], so the array column claims 1000 distinct values;arrayJoin(...) AS k, lowered toArrayJoinStep, not lowered, and asARRAY JOIN:100000 rows, columns: [c1: 7]on this PR,unknown rows, columns: []on master.
4e3df38
|
@groeneai @vdimir — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
…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]>
Backport #126491 to 26.10: Estimate an array join as 100 rows per input row in join reordering
`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]>
`estimateReadRowsCount` is in `optimizeJoin.cpp` on 26.9: 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.9 and is dropped; - `toUInt64Saturating` (added by #124008, master-only) does not exist on 26.9, so the file gets a static copy of it with the same body. The test `05406_join_swap_array_join_estimate` is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cherry pick #126491 to 26.8: Estimate an array join as 100 rows per input row in join reordering
…t row in join reordering
Cherry pick #126491 to 26.9: Estimate an array join as 100 rows per input row in join reordering
…t row in join reordering
Backport #126491 to 26.8: Estimate an array join as 100 rows per input row in join reordering
Backport #126491 to 26.9: Estimate an array join as 100 rows per input row in join reordering
`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]>
`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]>
Closes: #126481
Related: #113336
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Join reordering estimates a relation with
arrayJoinorARRAY JOINas 100 rows per input row instead of leaving its size unknown, so a hash join builds its hash table from a small or empty array join input instead of reading the other input into it. Closes #126481.Description
On 26.8, the LEFT JOIN query of #126481 reads all 6M rows of the right table into the hash table (peak 1.3 GB), although the
arrayJoinside has no rows; 26.6 built from that side.The build side is swapped only when both sides have a row estimate. Since #98533 an
ExpressionStepwitharrayJoinhas none, and anArrayJoinStep(the lowered function, the default since 26.10, andARRAY JOIN) never had one. Now both get the estimate of their input times 100, because the sizes of the arrays are unknown. A filter above the array join is not applied to the input's estimate, and the statistics of the array-joined columns are dropped.The cost: few input rows with very large arrays are estimated too low, as on 26.6, which passed the input's count through unchanged. 2 rows with 1M-element arrays joined to a 100k-row table now build 2M rows (153 MB) instead of 100k (30 MB).
Validation (
MergeTreeinstead ofSharedMergeTree, debug build, on the PR head and merged with current master): both queries of the issue, with and withoutcompatibility = '25.6', no longer read the right table (noJoinBuildTableRowCount), peak 6-8 MB instead of 1.3-1.75 GB, same results. The test covers both carriers with an empty input, a small input and an input estimated above the other side; with a multiplier of 1 the last two cases fail.Workflow [PR]
Sync PR [sync-upstream/pr/126491]
Version info
26.11.1.917-master(included in26.11and later)