Skip to content

Keep subcolumns extracted after reading when a condition moves to PREWHERE - #126573

Merged
alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
JayPokale:prewhere-subcolumn-file
Oct 23, 2026
Merged

alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
JayPokale:prewhere-subcolumn-file

Conversation

@JayPokale

@JayPokale JayPokale commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #126420

Queries on file, url and object storage that select a subcolumn and filter with another condition failed when that condition moved to PREWHERE: SELECT id, bbox FROM file('t.parquet') WHERE bbox.xmin > 5 AND c = '1' gave Not found column bbox.xmin: in block ... (the Overture Maps pattern from the issue). All three queries in the issue work with optimize_move_to_prewhere = 0.

Some subcolumns are not read by the format itself but extracted from their column after reading by ExtractColumnsTransform, which extracts ReadFromFormatInfo::requested_columns. That covers t.x requested together with t (filterTupleColumnsToRead reads only t and points t.x at it), and dynamic subcolumns such as j.a of a JSON column. updateFormatPrewhereInfo rebuilt requested_columns and source_header from the format header after PREWHERE, which has only t, so these subcolumns disappeared from the read step's output and the remaining filter could not find them.

The fix keeps requesting each such subcolumn while its column is still in the format header after PREWHERE, and adds it to source_header right after the format columns, before the hive partition and virtual columns, which the source appends after extraction. All three failing queries from the issue keep their column after PREWHERE (it shows up in the error message's block), so this covers them.

05413_file_prewhere_extracted_subcolumn runs the three queries from the issue with PREWHERE on (the default). The expected output is what the current master build returns for them with optimize_move_to_prewhere = 0.

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):

Fix NOT_FOUND_COLUMN_IN_BLOCK when a query on file, url or object storage selects a subcolumn together with its column (or a JSON subcolumn) and another condition is moved to PREWHERE.


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

Version info

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

…WHERE

A subcolumn the format does not read itself (`t.x` requested together with `t`, or `j.a` of a JSON column) is
extracted from its column after reading. `updateFormatPrewhereInfo` rebuilt the requested columns and the source
header from the format header alone, dropping such subcolumns, so the filter left after PREWHERE failed with
`NOT_FOUND_COLUMN_IN_BLOCK`. Keep requesting them while their column is still read.

Closes: ClickHouse#126420
@clickhouse-gh clickhouse-gh Bot added the comp-table-functions Table functions for temporary read/write to external systems (FROM ... tableFunction()). label Oct 18, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 19, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [346e13a]

Summary: ✅


AI Review

Summary

The PR preserves subcolumns extracted after format reading when PREWHERE rebuilds the read headers, fixing NOT_FOUND_COLUMN_IN_BLOCK for the reported Tuple, Nullable(Tuple), and JSON queries. Reviewed the current diff, surrounding implementation, shared callers, extraction and header ordering, and all prior discussion. No actionable findings. The added regression test covers all three reported queries and passed 50 ASan/UBSan runs on revision bd6a97a6b. No local build or tests were run during this review.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default external Author is not a member of the ClickHouse organization labels Oct 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 346e13aab with master 44cf9c049 (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

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

Job report

@JayPokale

Copy link
Copy Markdown
Contributor Author

The red checks on bd6a97a are unrelated to this change, which only touches prepareReadingFromFormat (reading from files):

  • 02317_distinct_in_order_optimization is labeled flaky, and all 75 reruns with the same randomized settings passed.
  • 05401_autopr_set_transplant_unpaired_sets (automatic parallel replicas on MergeTree) timed out under randomized settings; it passed all 53 runs without them.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged master to pick up #127002 (fix for the flaky 02317_distinct_in_order_optimization under parallel replicas). The other red check, the 05401_autopr_set_transplant_unpaired_sets timeout with randomized settings, is unrelated and being fixed in #127515. 05413_file_prewhere_extracted_subcolumn passes locally on the merged tree.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 23, 2026
Merged via the queue into ClickHouse:master with commit a7e8f4b Oct 23, 2026
178 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 23, 2026
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-ClickHouse__ClickHouse that referenced this pull request Nov 6, 2026
…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]>
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-table-functions Table functions for temporary read/write to external systems (FROM ... tableFunction()). external Author is not a member of the ClickHouse organization 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.

NOT_FOUND_COLUMN_IN_BLOCK with PREWHERE on a subcolumn of a Parquet file (Tuple, Nullable(Tuple), JSON)

3 participants