Skip to content

Fix Parquet reads of subcolumns like n.null when PREWHERE consumes their column - #128326

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:parquet-prewhere-subcolumn-remainder
Nov 6, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:parquet-prewhere-subcolumn-remainder

Conversation

@groeneai

@groeneai groeneai commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #124685
Related: #126573

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 into CHANGELOG.md):

Fixed NOT_FOUND_COLUMN_IN_BLOCK when a query over Parquet data (file, url, s3 and other object storages) selects a subcolumn such as n.null or arr.size0 and an explicit PREWHERE consumes its column, for example SELECT countIf(n.null) FROM file('x.parquet') PREWHERE n > 5. The same fix applies to ORC data files in an Iceberg table with the Parquet format.

Description

On 26.10.2.103 (also 26.9, 26.8 and 26.3), SELECT countIf(n.null), count() FROM file('x.parquet') PREWHERE n > 5 fails with Not found column __table1.n.null in block.

Parquet reads .null, .size0 and dynamic subcolumns through their whole column. After PREWHERE, updateFormatPrewhereInfo built the read step's header from the format header, so when PREWHERE consumed n, n.null was lost. #126573 re-requests the subcolumn when its column survives PREWHERE, but that leaves the whole column n in the header. On master, lazy materialization then aborts a debug build with default settings: SELECT n.null, number FROM file('x.parquet') WHERE number > 5 ORDER BY number LIMIT 3 -> chassert(header.columns() == required_inputs.size()) in splitExpressionStep.

Change: build the post-PREWHERE header from the query-level requested columns, as SourceStepWithFilter::updatePrewhereInfo does, and keep in the format header any column an explicit PREWHERE consumed if a selected subcolumn is extracted from it. This replaces the loop from #126573. The object-storage fallback that applies PREWHERE after the reader (ORC data files of an Iceberg table read as Parquet) keeps that column too.

This is the source part of #124685, reverted in #125121 because its test failed under DatabaseReplicated. The test now has no-replicated-database.

Validation: the new test (file, s3, Iceberg) fails on master and passes 50/50 with randomized settings, 20/20 with parallel replicas. Found by the AST fuzzer (report, STID 2728-4657; that query passes since #126573). Overlaps #126099.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.1571-master (included in 26.11 and later)

…RE read header

`prepareReadingFromFormat` keeps query-level `requested_columns` / `source_header`,
which can hold subcolumns such as `n.null` or `arr.size0`, and a format-level
`format_header`, where Parquet reads such a subcolumn through its whole column
(`n`). After PREWHERE, `updateFormatPrewhereInfo` set `source_header` to the
post-PREWHERE `format_header`, and ClickHouse#126573 then re-requested a subcolumn only
when its column was still read. Two shapes stayed broken:

- an explicit PREWHERE that consumes the column a selected subcolumn is read
  through drops the subcolumn: `SELECT countIf(n.null), count() FROM
  file('x.parquet') PREWHERE n > 5` fails with NOT_FOUND_COLUMN_IN_BLOCK, also on
  released 26.3, 26.8, 26.9 and 26.10;
- when the column survives an optimizer PREWHERE, the read step outputs the whole
  column `n` that no step requested, and lazy materialization over it trips
  `chassert(header.columns() == required_inputs.size())` in
  `splitExpressionStep` with default settings: `SELECT n.null, number FROM
  file('x.parquet') WHERE number > 5 ORDER BY number LIMIT 3`.

Compute the post-PREWHERE source header by applying the PREWHERE actions to the
query-level requested block, as `SourceStepWithFilter::updatePrewhereInfo` does,
keep each requested column with its storage mapping, and keep a storage column
in `format_header` when an explicit PREWHERE consumes the column a selected
subcolumn is extracted from. This replaces the ClickHouse#126573 loop, whose cases the new
construction covers (05413 passes unchanged).

The object-storage fallback that re-applies PREWHERE after the reader, for a
data file whose format has no PREWHERE (ORC files of an Iceberg table read as
Parquet, schema-evolved files), now keeps the inputs listed in the format header
among the PREWHERE outputs, so the column survives the filter there too.

This re-lands the source part of ClickHouse#124685 (reverted in ClickHouse#125121 because its test
failed under DatabaseReplicated: the replicated DDL worker does not see the
`TEMPORARY TABLE` that the Iceberg block passes to `IcebergLocal`). The test now
carries `no-replicated-database` with the reason line of
`05291_iceberg_optimize_explicit_metadata`. Its explicit-PREWHERE `s3` arm pins
`parallel_replicas_for_cluster_engines = 0`, because under parallel replicas
`s3` is read through its `-Cluster` storage, which rejects PREWHERE.

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

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding) plus my own cold review (2 nits).

# Sev Finding Verdict Evidence / action
1 ⚠️ The lazy-materialization arms check only rows, so they would stay green if the rewrite declined; add EXPLAIN checks for LazilyReadFromFile DISAGREE The lazy read step is created before splitExpressionStep runs (optimizeLazyMaterialization.cpp:697-705 before :737), so the master abort on exactly these arms proves the rewrite is taken; the fix defers n.null/arr.size0 the same way, and the randomized lazy settings are pinned in the arms.
2 💡 Two guard arms (indexHint(n.null), WHERE number > 5) pass on master since #126573 DISAGREE They guard the .null/.size0 shapes of the loop this PR replaces, which 05413 does not cover.
3 💡 getByName on a missing storage column throws at plan time DISAGREE Reachable only for a subcolumn of a Hive partition column, which fails the same way on master with or without PREWHERE.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261030-235000

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. On a master debug build, SELECT countIf(n.null), count() FROM file('x.parquet') PREWHERE n > 5 fails with NOT_FOUND_COLUMN_IN_BLOCK every time (also on 26.3.60.2, 26.8.34.13, 26.9.25.15, 26.10.2.103), and SELECT n.null, number FROM file('x.parquet') WHERE number > 5 ORDER BY number LIMIT 3 aborts with chassert(header.columns() == required_inputs.size()) with default settings.
b Root cause explained? Parquet reads .null/.size0 through the whole column. After PREWHERE the read header was the format header (plus #126573's surviving subcolumns), so a subcolumn whose column PREWHERE consumed was dropped, and a surviving column stayed in the header unrequested, which breaks lazy materialization's header/input bookkeeping.
c Fix matches root cause? Yes. The header is built where it is wrong, from the query-level requested columns as SourceStepWithFilter::updatePrewhereInfo does; no consumer or check is relaxed.
d Test intent preserved / new tests added? New 05339_parquet_prewhere_subcolumn_read_through_column (file, s3, Iceberg ORC read as Parquet): explicit PREWHERE consuming the column, lazy materialization over .null/.size0, the STID indexHint shape. PREWHERE pinned on via Random settings limits, validated with a negative control.
e Both directions demonstrated? Master build: test FAIL, 9 local shapes fail (2 abort). Fixed build: 50/50 randomized, 20/20 with parallel replicas, skipped under DatabaseReplicated, every shape equals its no-PREWHERE result. Build IDs checked.
f Fix is general across code paths? The function serves file, url, s3/azure/hdfs and data lakes, optimizer and explicit PREWHERE; the object-storage fallback that applies PREWHERE after the reader is fixed too. 86 related tests: same results as master.
g Fix generalizes across inputs (params/datatypes/wrappers)? Nullable .null, Array .size0, LowCardinality(Nullable) .null, tuple elements, Nullable(Tuple) and JSON (05413), _file and Hive partition columns, n IS NULL, PREWHERE + WHERE, lazy on/off.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change; failing queries now succeed.
i Invariants and contracts preserved? After PREWHERE the read header holds exactly the query-level columns the step outputs, and the format header every column they are extracted from; Hive and virtual columns stay at the end.

Session id: cron:clickhouse-impl-slot-40:20261030-231000

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @al13n321 @kssenii, could you review this and add the can be tested label so CI runs? It re-lands the source part of #124685 on top of #126573: after PREWHERE the Parquet read header is built from the query-level requested columns, so a subcolumn like n.null survives when an explicit PREWHERE consumes n, and the object-storage fallback that filters after the reader keeps that column too.

@clickhouse-gh

clickhouse-gh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3787f16]

Summary: ✅


AI Review

Summary

The change preserves requested subcolumns through PREWHERE, keeps their physical parent columns available for extraction, and corrects the output header used by lazy materialization. The stateless regression covers file, s3, and ORC files read through IcebergLocal. No actionable findings at commit 3787f1682295e36e821489f2395bbdf36233dfa8.

Missing context / blind spots
  • ⚠️ Runtime validation relies on the author's documented failing-master/passing-patched runs. No local binary or build is available, and the CI report reports dropped bugfix validation jobs. A completed bugfix validation run would provide independent confirmation.
Final Verdict

Status: ✅ Approve. The focused regression coverage and documented before/after results support the fix; no code changes requested.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). labels Oct 31, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 31, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 3787f1682 with master 9f79cc172 (stripped binary size, per-symbol sizes and ThinLTO time; 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

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

Job report

@groeneai

groeneai commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator Author

This also fixes a LOGICAL_ERROR that La Casa Del Dolor hit on master (report, STID 2409-5283): the whole column n that stays in the read header after PREWHERE reaches a JOIN, and the JOIN rejects it.

INSERT INTO FUNCTION file('x.parquet') SELECT number, if(number % 3 = 0, NULL, number) AS n FROM numbers(20);
SELECT count() FROM file('x.parquet') AS f INNER JOIN numbers(20) AS t ON f.number = t.number WHERE f.n.null AND f.number > 5;

With the CI release binaries: master 93b4bc10 fails with Cannot find input column n on its position in inputs of expression actions DAG (LOGICAL_ERROR), 26.10 fails with NOT_FOUND_COLUMN_IN_BLOCK, and this PR (merge 279d2d48) returns 5, the same as with optimize_move_to_prewhere = 0.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok to me.

@alexey-milovidov alexey-milovidov self-assigned this Nov 6, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 6, 2026
Merged via the queue into ClickHouse:master with commit d52267d Nov 6, 2026
527 of 531 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 6, 2026
alexey-milovidov added a commit to rorylshanks/ClickHouse that referenced this pull request Nov 7, 2026
…-variant-support

Resolve the conflict in `updateFormatPrewhereInfo` by keeping the PR's construction:
it retains all `PREWHERE` inputs in `format_header` and builds `source_header` from
the requested columns, which covers the case fixed on master in ClickHouse#128326
(`05339_parquet_prewhere_subcolumn_read_through_column`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). 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.

3 participants