Skip to content

Keep the PREWHERE filter column for evaluating defaults of missing columns - #117533

Merged
alexey-milovidov merged 13 commits into
masterfrom
fix-single-step-prewhere-default-column-115273
Oct 12, 2026
Merged

alexey-milovidov merged 13 commits into
masterfrom
fix-single-step-prewhere-default-column-115273

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 1, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fixed a column added by ALTER TABLE ... ADD COLUMN ... DEFAULT <expression> being filled with the type default instead of the evaluated expression when it is read through a single-conjunct explicit PREWHERE on the expression's dependency.

Documentation entry for user-facing changes

A PREWHERE step stores the columns it projects out, so that a later step in the readers chain can still evaluate the DEFAULT expression of a column that is missing from the part. Columns still present in the result block are skipped, to avoid keeping and filtering them twice — but the filter column is present in the block at that point and is erased from the result only further down. So it was neither kept as an additional column nor available as a result column, and it vanished.

With a single-conjunct explicit PREWHERE over a column that is itself the condition, that column is exactly the one the DEFAULT expression needs:

CREATE TABLE ad (a UInt8) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO ad SELECT 1 FROM numbers(150);
ALTER TABLE ad ADD COLUMN b Int32 DEFAULT CAST(a % 13, 'Int32') AFTER a;

SELECT groupUniqArray(b) FROM (SELECT b FROM ad WHERE a);      -- [1]
SELECT groupUniqArray(b) FROM (SELECT b FROM ad PREWHERE a);   -- [0], the Int32 default

The two-conjunct spelling was already correct, because the first step then keeps the column for the second one, which is what the fix for #111757 covered; this is the surviving single-step face of it.

Closes: #115273
Related: #111757


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

Version info

  • Merged into: 26.10.1.2240-master (included in 26.10 and later)

…lumns

A `PREWHERE` step stores the columns it projects out, so that a later step in
the readers chain can still evaluate the `DEFAULT` expression of a column that
is missing from the part. Columns still present in the result block are
skipped, to avoid keeping and filtering them twice - but the filter column is
present in the block at that point and is erased from the result only further
down. So it was neither kept as an additional column nor available as a result
column, and it vanished.

With a single-conjunct explicit `PREWHERE` over a column that is itself the
condition, that column is exactly the one the `DEFAULT` expression needs:

    CREATE TABLE ad (a UInt8) ENGINE = MergeTree ORDER BY tuple();
    INSERT INTO ad SELECT 1 FROM numbers(150);
    ALTER TABLE ad ADD COLUMN b Int32 DEFAULT CAST(a % 13, 'Int32') AFTER a;

    SELECT groupUniqArray(b) FROM (SELECT b FROM ad WHERE a);      -- [1]
    SELECT groupUniqArray(b) FROM (SELECT b FROM ad PREWHERE a);   -- [0], the Int32 default

The two-conjunct spelling was already correct, because the first step then
keeps the column for the second one, which is what the fix for #111757
covered; this is the surviving single-step face of it.

Closes: #115273
Related: #111757
@clickhouse-gh

clickhouse-gh Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [36768f0]

Summary: ✅


AI Review

Summary

This PR fixes the remaining single-step PREWHERE case from #115273 by preserving a removed filter column in result.additional_columns so the later main reader can still evaluate DEFAULT expressions for columns missing in the part. I reviewed the diff, the surrounding MergeTreeRangeReader / MergeTreeReadersChain flow, the earlier related multistep and patch-parts regressions, and the prior review thread about later filter steps. I did not find a remaining actionable correctness, concurrency, or performance issue in the current implementation, so I have no new inline findings to post.

Missing context / blind spots
  • ⚠️ I did not execute the new stateless test locally because this checkout has no prebuilt clickhouse binary or build_* directory; running tests/queries/0_stateless/05227_single_step_prewhere_default_column.sql would close that gap.
Final Verdict

✅ No actionable review findings on the current diff.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 36768f09e with master 39e8259ca (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 62b7ea5c5; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 12 s compile time in total, 7 of them have a recent master baseline.

Job report

@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Sep 4, 2026
alexey-milovidov and others added 9 commits September 10, 2026 08:45
The `05099` prefix is now crowded on master; move the test to `05177`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…r merging master

The `05177` prefix is now also used by another test on master.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…s gone since v26.9

Disabling `enable_analyzer` now throws `SETTING_CONSTRAINT_VIOLATION`, so the case can no longer run.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 CI status on head 747c6392. None of the reds come from this change:

  • Upgrade check (amd_release): countResample "The range given in function countResample is empty" while attaching tables. This is the known 26.9 upgrade-check problem. The fix, Fix 26.9 upgrade check #121590, is merged into 26.9 and clears once a new 26.9 release is published.
  • Performance Comparison (arm_release, 4/6 and 5/6): formats_columns_nullable #1 and serialization_pool_lookup #0. Neither query uses PREWHERE over a column missing in the part, which is the only path this PR changes. Both runners are also tagged disk-almost-full / under-utilized CPU.

@groeneai, investigate the failures: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117533&sha=747c6392cd17dd54b771d957d08be33540c2595c&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%204%2F6%29 and https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117533&sha=747c6392cd17dd54b771d957d08be33540c2595c&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%205%2F6%29 and provide a fix in a separate PR. If the fix is already in progress, link it here.

The AI Review Major (the row policy USING a + PREWHERE a chain) is answered in its review thread: the case returns correct results, so I resolved the thread.

@groeneai

Copy link
Copy Markdown
Collaborator

Investigated both.

@groeneai

Copy link
Copy Markdown
Collaborator

Follow-up on serialization_pool_lookup #0 (5/6): fixing PR #122517 (test-only).

Query #0 groups by a single-type Dynamic key in all threads, so every row looks up the same SerializationObjectPool entry, and its time is fixed per server process: the samples of one server agree within 0.7% (p50), while the per-process medians span 0.643-0.840 s (arm, p05-p95). Each A/B run compares two random draws of it, and this run's reference drew a fast one. #122517 runs #0 in one thread; #1 and #2 keep the contention coverage.

Correction to my comment above: the 594 dashboard /history points are not raw samples. The raw arm master runs of 2026-09-05 to 2026-09-21 (CIDB query_metrics_v2) span 0.64-0.91 s, and 0.670 s is about their 4th percentile, not below every run.

pull Bot pushed a commit to erickirt/ClickHouse that referenced this pull request Sep 27, 2026
Query #0 groups 5M rows by a single-type Dynamic key. For every row the
aggregation looks up the variant's default serialization in
SerializationObjectPool: a shared lock on the key's shard mutex,
weak_ptr::lock, and the release of the returned SerializationPtr. These
are atomic read-modify-writes on two cache lines that every aggregation
thread writes for every row. One key means one shard, so the pool's
sharding cannot spread the load, and the query is bound by how fast those
lines move between cores. That speed is fixed per server process.

CI data (query_metric_runs_v1, arm, 1177 runs since 2026-09-17): the 5-7
samples of one server agree within 0.7% (p50), but the per-process medians
span 0.643-0.840 s (p05-p95). Fast and slow processes do the same work
(same arena bytes, memory, two-level hash tables and page faults) and
differ only in user CPU time (9.5 vs 12.1 s). The wall time does not
depend on the thread count: arm 0.754 s with 16 threads vs 0.775 s with
12, amd 0.669 s with 16 threads on 8 pinned cores vs 0.686 s with 8. So
each A/B run compares two random draws of that per-process speed. Runs
whose new/old median moved by more than 15% (raw samples):

                                 arm PR   arm master   amd PR   amd master
    serialization_pool_lookup #0  123/789     54/558    68/176     220/560
    serialization_pool_lookup #1   41/789     11/558     5/176      20/560
    serialization_pool_lookup #2    0/789      0/558     0/176       0/560
    group_by_dynamic_keys #0        0/782      0/559     0/173       0/559

group_by_dynamic_keys #0 runs the same per-row lookup path with
max_threads = 1. On PR ClickHouse#117533 the performance dashboard scored #0 as a
confirmed regression (arm 5/6, 0.670 -> 0.799 s) that the shard's own
report did not flag.

Run #0 in one thread, from numbers instead of numbers_mt, so it times the
pool's hit path itself, which does not depend on the process. Queries #1
(two keys, where sharding spreads the load) and #2 (the multi-threaded
MergeTree read) keep the contention coverage.

Local check on x86 with the official 26.10.1.820 master build, perf.py,
7 runs: old #0 1.76 s with 12 threads (15.4 s user CPU per run), new #0
0.57 s (0.56 s user CPU), same result. Ten fresh servers pinned to 8
cores with max_threads = 8: old #0 per-process medians 0.68-1.08 s; new
#0 0.56-0.58 s in 8 of them and 0.72-0.73 s in the two whose table
fills were also about 25% slower (9.2-10.0 s vs 7.4-8.6 s).

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

Copy link
Copy Markdown
Member Author

🕵 Thanks @groeneai. Merged master (now at f8bb7742), which brings in #122458 for formats_columns_nullable #1. serialization_pool_lookup #0 will be stabilized by #122517 once it lands; it is unrelated to this change. Rebuilt and re-ran 05227_single_step_prewhere_default_column on the merged tree; it passes.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Merged master (now at 13283331) and re-ran 05227_single_step_prewhere_default_column on the rebuilt tree; it passes. The reds on f8bb7742 don't come from this change:

@groeneai, investigate the failures: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117533&sha=f8bb7742bfd6d0ed5571ed7cf104fa6c904748e7&name_0=PR (the amd_fuzzers link overflow and 05237_with_expression_alias_scope_descendant with DBReplicated) and provide a fix in a separate PR. If the fix is already in progress, link it here.

@groeneai

groeneai commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

Both already have fixes:

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks @groeneai. Confirmed: 13283331 already contains #123738 (the amd_fuzzers link fix) and the revert in #123789. CI on 13283331 is green, so nothing more is needed here. The 05237_with_expression_alias_scope_descendant code fix will land with #123790.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Oct 12, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Single-conjunct PREWHERE fills an ALTER-ADDed DEFAULT column with the type default when it is the only column read

3 participants