Skip to content

Resolve JOIN runtime filter keys by header position - #121647

Open
cv4g wants to merge 6 commits into
masterfrom
runtime-filter-statistics-provenance
Open

cv4g wants to merge 6 commits into
masterfrom
runtime-filter-statistics-provenance

Conversation

@cv4g

@cv4g cv4g commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Prep for #96779 (MinMaxRuntimeFilter), #97501 (Introduce statistics-based disabling of join runtime filters).
Merges after #121597 (RuntimeFilterBuildOptions, merged).
Merges after #121736 (Track and cache provenance-aware relation statistics for JOIN optimization).

JOIN runtime filters are ultimately addressed by column name, but query-plan headers can legally contain duplicate names. This occurs, for example, after correlated-subquery decorrelation or projections such as SELECT column, *.

The previous guard inspected build-side key names before JoinStepLogical::preCalculateKeys(). It could not reliably identify computed keys introduced during key pre-calculation, and it disabled runtime filtering too broadly for unrelated duplicate columns. In affected plans, an ambiguous build key could also lead to a Block structure mismatch in JoinStep error.

Changes

  • Add a reusable header index that resolves a column only when:
    • its name occurs exactly once; and
    • its type matches the requested key type.
  • Resolve runtime-filter build keys after preCalculateKeys(), covering both plain and computed keys.
  • Skip only ambiguous scalar keys while retaining filters for other unambiguous keys.
  • Fail closed for multi-key LEFT ANTI JOIN tuple filters if any component key is ambiguous.
  • Avoid installing an empty FilterStep when no executable runtime filter remains.
  • Reuse calculated JOIN-key inputs and expose a repeated computed key only once in the calculated-key header.
  • Extend coverage for duplicate key and non-key columns, repeated computed keys, mixed predicates, and multi-key
    anti joins.
  • Centralize runtime Bloom-filter sizing and extract runtime-filter construction into a helper without changing
    their execution policy.

As this PR is stacked on top of #121736 please review git commits 9d7306d and 115317d only.

Changelog category (leave one):

  • Bug Fix

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

Fixed JOIN runtime-filter planning for build-side headers containing duplicate column names. Runtime-filter keys are now resolved against unique header positions, preventing Block structure mismatch errors while retaining filters for unambiguous keys.


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

@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b1a9db7]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_asan_ubsan, targeted) FAIL
05042_ie_join_selectivity_stats_edge_cases FAIL cidb
05042_ie_join_selectivity_stats_edge_cases FAIL cidb
Stateless tests (amd_asan_ubsan, flaky check) FAIL
05042_ie_join_selectivity_stats_edge_cases FAIL cidb
05042_ie_join_selectivity_stats_edge_cases FAIL cidb
05042_ie_join_selectivity_stats_edge_cases FAIL cidb
Stateless tests (amd_tsan, flaky check) FAIL
05042_ie_join_selectivity_stats_edge_cases FAIL cidb
05042_ie_join_selectivity_stats_edge_cases FAIL cidb
Stateless tests (amd_msan, flaky check) FAIL
05042_ie_join_selectivity_stats_edge_cases FAIL cidb
05042_ie_join_selectivity_stats_edge_cases FAIL cidb
Stateless tests (amd_debug, flaky check) FAIL
05042_ie_join_selectivity_stats_edge_cases FAIL cidb
05042_ie_join_selectivity_stats_edge_cases FAIL cidb
Stateless tests (amd_binary, flaky check) FAIL
05042_ie_join_selectivity_stats_edge_cases FAIL cidb
05042_ie_join_selectivity_stats_edge_cases FAIL cidb
05042_ie_join_selectivity_stats_edge_cases FAIL cidb

AI Review

Summary

This is a stacked branch, and I reviewed the requested runtime-filter commits 9c74d241453beeafaacd15003c1a0243dce3412b and 115317de0e98520ed3f5ff321ea8b3a2fcc25e02 against the current merged code. The build-side duplicate-key handling, repeated computed-key deduplication, and the follow-up fix that stops a rejected runtime-filter attempt from overriding query_plan_merge_filters / query_plan_filter_push_down all look consistent with the surrounding join-planning and execution paths. I did not find a remaining correctness, safety, or compatibility issue in that reviewed scope.

Missing context / blind spots
  • ⚠️ This is a stacked PR. Per the author's request, I limited the substantive review to 9c74d241453beeafaacd15003c1a0243dce3412b and 115317de0e98520ed3f5ff321ea8b3a2fcc25e02; the sibling statistics changes merged into the branch for #121736 were only checked enough to verify that the previously reported null_fraction issue is fixed in the current code.
Final Verdict

✅ No new blockers or majors in the reviewed scope.

LLVM Coverage Report

Measured on commit b1a9db7.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 718/755 (95.10%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Sep 22, 2026
@cv4g

This comment was marked as resolved.

@clickhouse-gh clickhouse-gh Bot added the comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... label Sep 22, 2026
@cv4g
cv4g force-pushed the runtime-filter-statistics-provenance branch from 2c8f112 to 9d7306d Compare September 23, 2026 10:07
@cv4g cv4g changed the title Track relation statistics provenance and resolve JOIN runtime filter keys positionally Resolve JOIN runtime filter keys by header position Sep 23, 2026
size_t filters_to_merge,
const DataTypePtr & target_type,
const RuntimeFilterBuildOptions & build_options,
const RuntimeFilterConfig & runtime_filter_config)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

File local change. This function body got extracted from a BuildRuntimeFilterTransform constructor below.

parameters.bytes
= growRuntimeBloomFilterBytesFromStats(*distinct_keys_hint, parameters.hash_functions, parameters.bytes, max_ratio_of_set_bits);
return parameters;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Extracted from AdaptiveSetRuntimeFilter::switchToApproximateFilter in src/Processors/QueryPlan/RuntimeFilterLookup.cpp

const auto bloom = sizeRuntimeBloomFilter(
RuntimeBloomFilterParameters{exact_filter->getBytesLimit(), bloom_filter_hash_functions},
distinct_keys_hint,
max_ratio_of_set_bits_in_bloom_filter);

@cv4g cv4g Sep 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Merged from:

  • exact_filter->getBytesLimit() from line 678
  • evaluation of distinct_keys_hint and call to growRuntimeBloomFilterBytesFromStats in line 682-683

This also allows to merge if (distinct_keys_hint) and if (distinct_keys_hint_matches_filter_key) in lines 680, 678 to remove one level of nesting (see new line 685).

@cv4g
cv4g force-pushed the runtime-filter-statistics-provenance branch from 9d7306d to 9c74d24 Compare September 23, 2026 12:21
@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default and removed pr-improvement Pull request with some product improvements labels Sep 23, 2026
@clickhouse-gh clickhouse-gh Bot added comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... and removed comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Sep 23, 2026
@cv4g

cv4g commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor Author

This is a stacked PR. Please do only review 9c74d24 and 115317d. Any other non-master commit on this branch belongs to #121736.

@cv4g cv4g added the pr-performance Pull request with some performance improvements label Oct 6, 2026

This branch has not been deployed

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

Labels

comp-joins JOINs end-to-end (planning hooks + runtime join operators/algorithms). Single bucket to avoid pla... pr-bugfix Pull request with bugfix, not backported by default pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant