Repository navigation
Refactoring: Extract relation statistics, bloom filter sizing preparing for MinMaxRuntimeFilter - #120232
Conversation
|
Workflow [PR], commit [ddc075d] Summary: ✅
AI ReviewSummaryThis PR extracts shared relation-statistics estimation and runtime Bloom-filter sizing helpers out of Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit ddc075d.
Changed lines: Changed C/C++ lines covered: 240/253 (94.86%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes7 object files changed (+193.01 KiB total), 3 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units24 translation units recompiled, 124 s compile time in total, 21 of them have a recent master baseline.
Translation units without a recent master baseline:
|
| return std::max(total, CascadesDefaults::MIN_ROW_WIDTH); | ||
| } | ||
|
|
||
| RelationStats parseTableStatsHint(const String & stats_hint_json, const String & table_name); |
There was a problem hiding this comment.
Moved to debugHelpers.{h, cpp}
|
|
||
| namespace QueryPlanOptimizations | ||
| { | ||
| void remapColumnStats(std::unordered_map<String, ColumnStats> & mapped, const ActionsDAG & actions); |
There was a problem hiding this comment.
Moved to RelationStatistics.{h, cpp}
| /// Propagate per-column statistics through `actions`, rekeying the map in place by output name. | ||
| /// An output inherits an input's stats when it is that input, an alias of it, or a deterministic | ||
| /// single-argument function of it (which cannot increase the distinct count). | ||
| void remapColumnStats(std::unordered_map<String, ColumnStats> & mapped, const ActionsDAG & actions); |
There was a problem hiding this comment.
Previously additionally forward declared in Cascades/StatisticsDerivation.cpp.
Moved to RelationStatistics.{h, cpp}
| */ | ||
| RelationStats parseTableStatsHint(const String & stats_hint_json, const String & table_name); | ||
| RelationStats parseTableStatsHint(ContextPtr context, const String & table_name); | ||
| RelationStats getRandomizedStats(UInt64 seed, size_t relation_index, const String & table_name, const Block & header); |
There was a problem hiding this comment.
All three function decls previously forward declared in optimizeJoin.h too.
Moved to debugHelpers.h
|
|
||
| RelationStats parseTableStatsHint(ContextPtr context, const String & table_name); | ||
| RelationStats parseTableStatsHint(const String & stats_hint_json, const String & table_name); | ||
| RelationStats getRandomizedStats(UInt64 seed, size_t relation_index, const String & table_name, const Block & header); |
There was a problem hiding this comment.
All three function decls previously forward declared in debugHelpers.cpp too.
Moved to debugHelpers.h
| String dump() const; | ||
| }; | ||
|
|
||
| struct RelationStats |
There was a problem hiding this comment.
Moved to RelationStatistics.h
| static constexpr UInt64 MAX_RUNTIME_BLOOM_FILTER_BYTES = 16 * 1024 * 1024; | ||
| static constexpr UInt64 MAX_RUNTIME_BLOOM_FILTER_HASH_FUNCTIONS = 10; | ||
| static constexpr UInt64 DEFAULT_RUNTIME_BLOOM_FILTER_BYTES = 512 * 1024; | ||
| static constexpr UInt64 DEFAULT_RUNTIME_BLOOM_FILTER_HASH_FUNCTIONS = 3; |
There was a problem hiding this comment.
All four definitions moved to RuntimeFilterBloomSizing.cpp
| throw Exception( | ||
| ErrorCodes::PARAMETER_OUT_OF_BOUND, | ||
| "Specified runtime bloom filter hash function count {} is too big, maximum: {}", | ||
| bloom_filter_hash_functions, MAX_RUNTIME_BLOOM_FILTER_HASH_FUNCTIONS); |
There was a problem hiding this comment.
Whole hunk extracted into resolveRuntimeBloomFilterDefaults (replacing zeroes by default values), validateRuntimeBloomFilterParameters (throwing if value exceeds limit) in RuntimeFilterBloomSizing.cpp
| = static_cast<double>(*distinct_keys_hint) / HashJoinEntry::MAX_OVERESTIMATION_FACTOR; | ||
| const double predicted_fill_rate = -std::expm1( | ||
| -static_cast<double>(bloom_filter_hash_functions) * least_distinct_keys | ||
| / (static_cast<double>(bytes_limit) * 8.0)); |
There was a problem hiding this comment.
Extracted into estimateRuntimeBloomFilterSetBitsRatio in RuntimeFilterBloomSizing.cpp
| const double ideal_bloom_filter_bytes = std::ceil(-static_cast<double>(hash_functions) * static_cast<double>(distinct_keys) / std::log1p(-target_fill_rate) / 8.0); | ||
| const double clamped_bloom_filter_bytes = std::clamp(ideal_bloom_filter_bytes, 0.0, static_cast<double>(MAX_STATS_SIZED_BLOOM_FILTER_BYTES)); | ||
| return std::max(static_cast<UInt64>(clamped_bloom_filter_bytes), default_bloom_filter_bytes); | ||
| } |
There was a problem hiding this comment.
Moved to RuntimeFilterBloomSizing.cpp and renamed to growRuntimeBloomFilterBytesFromStats.
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
There was a problem hiding this comment.
I'm fine with these changes. Overall, they make the code more modular and improve the readability as well as the intended semantics.
I have a small concern about the refactoring of few methods into debugHelpers.h.
For e.g., it contains the method getRandomizedStats(..) which is more generic (and not only debug helper) and can be used in various scenarios when testing e.g., plan robustness hand-in-hand with cardinality estimation, etc. I would have called this file something like utils.h or other name containing utils as prefix. It looks to me that its more about utilities than mere debugging.
Thank you! I'll rename it in a follow up PR right away. |
`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]>
`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]>
Prep for #96779.
This PR is extracting
class RelationStats,remapColumnStatstoRelationStatistics.hand moves bloom filter sizing and debugging related functions into dedicated source and header files too. This is to prepare for additional callers in the code that is planning MinMaxRuntimeFilters.Changelog category (leave one):
Workflow [PR]
Sync PR [sync-upstream/pr/120232]
Version info
26.9.1.1603(included in26.9and later)