Repository navigation
Optimize filtered constant columns - #105541
Conversation
|
Workflow [PR], commit [06869da] Summary: ✅
AI ReviewSummaryThe PR replaces eligible input columns with Missing context / blind spots
Final Verdict✅ Approve. |
f18c917 to
ae0a620
Compare
Avoid replacing floating-point columns after `equals`, because `+0.0` and `-0.0` compare equal but have different bit representations. Materialize `Nullable` constant columns before passing them downstream to avoid invalid keys in grouping sets with `group_by_use_nulls`. PR: ClickHouse#105541 Tests: ninja -C build programs/clickhouse Tests: tests/clickhouse-test --no-random-settings --no-random-merge-tree-settings 04259_filter_constant_column_after_where Tests: local `03305_mergine_aggregated_filter_push_down` smoke with `127.0.0.{1,1}` because this macOS host has no `127.0.0.2` loopback alias
Cover additional `FilterTransform` cases for filtered `ColumnConst` columns, including convertible constants, non-input expressions, `Nullable`, `LowCardinality`, omitted filtered columns, partially omitted equality columns, and empty results.
Avoid replacing floating-point columns after `equals`, because `+0.0` and `-0.0` compare equal but have different bit representations. Materialize `Nullable` constant columns before passing them downstream to avoid invalid keys in grouping sets with `group_by_use_nulls`. PR: ClickHouse#105541 Tests: ninja -C build programs/clickhouse Tests: tests/clickhouse-test --no-random-settings --no-random-merge-tree-settings 04259_filter_constant_column_after_where Tests: local `03305_mergine_aggregated_filter_push_down` smoke with `127.0.0.{1,1}` because this macOS host has no `127.0.0.2` loopback alias
Skip the constant-column rewrite when an `equals` predicate constrains a `Decimal` column with a floating-point constant. Such comparisons can keep multiple distinct `Decimal` values after conversion through floating-point comparison paths, so replacing the result with one converted constant changes query-visible output.
ae0a620 to
34f9cd9
Compare
Make the `equals` safety check recursive over constrained and constant types before replacing filtered columns with `ColumnConst`. This skips rewrites when the constrained type contains `Float`, or when it contains `Decimal` and the constant side contains `Float`. Add stateless coverage for nested `Tuple(Float64)` and nested `Tuple(Decimal128)` predicates that must preserve distinct surviving values. Review: ClickHouse#105541 (comment)
Skip the constant-column rewrite when the constrained column type contains `Dynamic`, because runtime variants are not visible through static child-type traversal and can carry non-representation-unique values.
`equals` between `String` and `FixedString` can keep multiple distinct `String` payloads because of zero-padded `FixedString` comparison semantics. Do not replace the filtered `String` column with a single `ColumnConst` in that case
`MergingAggregated` can receive partially aggregated blocks after expression or filter steps. Numeric aggregation merge methods read key columns through raw data, so a key can be read past its single nested value and produce wrong grouping keys. Materialize only key columns before merge and keep aggregate state columns unchanged.
d6403a1 to
73f7811
Compare
Fix `04259_filter_constant_column_after_where` by fixing `query_plan_merge_filters` and `query_plan_optimize_lazy_materialization` after the explicit filter-merge coverage queries. The test checks `dumpColumnStructure`, so randomized filter splitting and lazy materialization can change whether a column is observed as `ColumnConst` or a full column without changing query semantics.
The `equals` function for `Variant` can keep rows with different active variants for the same literal value, such as `Int8(1)` and `UInt8(1)` in `Variant(Int8, UInt8)`. Rewriting the constrained column to one `ColumnConst` would collapse those rows to a single active variant. Skip the optimization when the constrained result type recursively contains `Variant`, and cover both direct `Variant` and nested `Tuple(Variant)` cases. Review: ClickHouse#105541 (comment) Tests: `ninja -C build src/CMakeFiles/dbms.dir/Processors/Transforms/FilterTransform.cpp.o > build/build_filter_transform_variant_guard_object_20260525.log 2>&1`
|
Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/2163 Labels added:
These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels. |
|
Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/2163 Labels added:
These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels. |
|
Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/2163 Labels added:
These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels. |
Add a triggered expansion to `.claude/skills/review/SKILL.md` for changes that let a component produce its output in a form it did not produce before. It asks the review to enumerate the consumers (including internal reuses of the component) and list how each handles the new form, to check that a claimed normalization point precedes every use on each path, to ask for one fix where a recurring class starts instead of another point fix, and to require tests whose inputs reach the form-specific code of a consumer. The AI review of ClickHouse#105541 accepted the conversion in `MergeTreeDataPartWriterOnDisk::prepareBlockForWriting` as the boundary for constant columns, while `MergeTreeDataWriter::writeProjectionPartImpl` used the block before any writer existed (`infos.add`, sorting, `mergeBlock`). The test it asked for used only `String` and `DateTime` columns, which reach the generic `SerializationInfo::add`, not the downcast in `SerializationInfoTuple::add`. Consumers were reported one per round for five months. Asked for in ClickHouse#128533 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
Optimizes FilterTransform to replace physical input columns with
ColumnConstafter filters that prove the column has a single value, such asWHERE x = 'hello'. This reduces work for later pipeline steps and lets downstream expressions observe the filtered column as constant.The optimization currently handles equality predicates through
ANDconditions and preserves conservative behavior forOR,NOT, and multi-value predicates.This PR partially fixes issue: #34326. It covers the
ColumnConstoptimization for columns constrained by equality filters, while the remainingLowCardinalitypart is left out because changing a filtered column toLowCardinalitymay alter the column type/representation contract which seems more risky.Changelog category (leave one):
Changelog entry:
Performance benchmark
I measured this locally with a
RelWithDebInfobuild. Each case usesclickhouse benchmark --iterations 10; the table reports the median (50%) latency.group_by_const_keys_after_filterSELECT count(), sum(v) FROM constant_column_after_filter WHERE k1 = 42 AND k2 = 43 AND k3 = 44 AND k4 = 45 AND k5 = 46 AND k6 = 47 AND k7 = 48 AND k8 = 49 GROUP BY k1, k2, k3, k4, k5, k6, k7, k8 FORMAT Null SETTINGS max_threads = 1, optimize_move_to_prewhere = 0, query_plan_optimize_prewhere = 0, query_plan_optimize_lazy_materialization = 00.941 sec0.792 sec15.8%fasterexpressions_on_const_columns_after_filterSELECT sum(length(toString(k1))), sum(length(toString(k2))), sum(length(toString(k3))), sum(length(toString(k4))), sum(length(toString(k5))), sum(length(toString(k6))), sum(length(toString(k7))), sum(length(toString(k8))) FROM constant_column_after_filter WHERE k1 = 42 AND k2 = 43 AND k3 = 44 AND k4 = 45 AND k5 = 46 AND k6 = 47 AND k7 = 48 AND k8 = 49 FORMAT Null SETTINGS max_threads = 1, optimize_move_to_prewhere = 0, query_plan_optimize_prewhere = 0, query_plan_optimize_lazy_materialization = 00.768 sec0.434 sec43.5%fasterNote
Medium Risk
Touches core query execution filtering and mutates output column representations, which could subtly affect type/nullable/LC behavior; additional guards reduce risk but correctness/perf impacts span many queries.
Overview
Filter optimization:
FilterTransformnow analyzes the filterActionsDAGand, forand-combinedequalspredicates against constants (e.g.x = 'hello'), records columns that become single-valued and rewrites those output columns toColumnConstafter filtering.The rewrite is applied on fast paths (all-rows-pass / constant-true filter) and after partial filtering, with safeguards to skip float-related cases and to avoid unsafe conversions (notably float/decimal comparisons), plus handling for nullable/low-cardinality-nullable columns.
Adds a new stateless test (
04259_filter_constant_column_after_where) covering aliases, multipleANDequalities, non-handledOR/NOT, nullable/low-cardinality cases, and float/decimal edge conditions.Reviewed by Cursor Bugbot for commit 9a6d76a. Bugbot is set up for automated code reviews on this repo. Configure here.
Workflow [PR]
Sync PR [sync-upstream/pr/105541]
Version info
26.11.1.1030-master(included in26.11and later)