Skip to content

Keep row policy input columns in the header of file-like reads - #122714

Merged
KochetovNicolai merged 6 commits into
masterfrom
fix-file-row-policy-prewhere-header
Sep 30, 2026
Merged

KochetovNicolai merged 6 commits into
masterfrom
fix-file-row-policy-prewhere-header

Conversation

@KochetovNicolai

@KochetovNicolai KochetovNicolai commented Sep 28, 2026 •

Copy link
Copy Markdown
Member
Claude

A row policy on File, URL and object storage tables (S3, data lakes) removed its input columns from the header of the reading step. There were two effects:

  • ReadFromFile applied the row policy to its header only when PREWHERE was set. When optimizePrewhere moved a WHERE to PREWHERE, the header lost the policy inputs, but the step above the read kept a DAG input for them. The lazy
    materialization optimizer failed on this input with a LOGICAL_ERROR exception. Lazy materialization for reading local Parquet files (file / File) #114262 made it tolerate such inputs (908a3e3). This PR reverts that change, because the input no longer
    occurs.
  • A DEFAULT column computed from a policy input failed with UNKNOWN_IDENTIFIER, because the source computes defaults after the format applied the row policy. For File, this happened with PREWHERE. For URL and S3, it happened
    always.

Now a row policy is never part of ReadFromFormatInfo and never changes its headers. The sources still apply it via FormatFilterInfo.

Related: #114262
Related: #122682

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 UNKNOWN_IDENTIFIER for a DEFAULT column that depends on a column used by a row policy, in tables with the File, URL, S3 and other object storage engines.


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

Version info

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

KochetovNicolai and others added 3 commits September 28, 2026 17:06
…WHERE

Without PREWHERE, `StorageFile::read` does not apply the row-level filter to
the format header, so the input columns of the row policy stay in the output
header of `ReadFromFile`. When `optimizePrewhere` added PREWHERE,
`ReadFromFile::updatePrewhereInfo` also applied the row-level filter, and the
header lost these columns. There were two effects:

- `optimizePrewhere` built the step above the read from a DAG that was made
  for the old header, so the DAG had an input that the new header did not
  have. `optimizeLazyMaterialization` failed on it with a `LOGICAL_ERROR`
  exception, which 908a3e3 made tolerated.
- A `DEFAULT` column computed from a row policy input failed with
  `UNKNOWN_IDENTIFIER`, because `StorageFileSource` computes defaults after
  the format applied the filter.

Now the row-level filter is never propagated into `ReadFromFormatInfo` of
`ReadFromFile`. The source still applies it through `FormatFilterInfo`.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…on optimizer"

This reverts commit 908a3e3.

The dangling input came from the `ReadFromFile` header change on PREWHERE,
which is fixed now. A DAG input without a header column is a bug again.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…torage reads

`IStorageURLBase::read`, `StorageURLWithFailover::read` and
`StorageObjectStorage::read` applied the row-level filter to the format
header, also without PREWHERE, so the input columns of the row policy were
removed. A `DEFAULT` column computed from such a column failed with
`UNKNOWN_IDENTIFIER`, because the source computes defaults after the format
applied the filter.

Now the row-level filter is never part of `ReadFromFormatInfo`: the
`row_level_filter` field and the parameter of `updateFormatPrewhereInfo` are
removed. The sources still apply the filter via `FormatFilterInfo`. The
fallback `FilterTransform` in `StorageObjectStorageSource` (for data files
whose format does not support PREWHERE) keeps the input columns of the
row-level filter as well.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c732c77]

Summary: ✅


AI Review

Summary

This PR removes row policies from ReadFromFormatInfo headers for file-like reads so PREWHERE no longer shrinks the read-step header, fixes the DEFAULT-column regression on File/URL/S3, and reverts the lazy-materialization workaround for dangling DAG inputs. The file and URL parts look directionally correct, but the new object-storage fallback path still rebuilds the row-policy filter DAG too narrowly and can drop selected columns from mixed-format reads, so I would block until that path preserves the original pass-through outputs.

Findings

❌ Blockers

  • [src/Storages/ObjectStorage/StorageObjectStorageSource.cpp:1670-1682] The fallback row-policy FilterTransform is now rebuilt from cloneSubDAG({&filter_node}), and then its outputs are replaced with the filter node plus only the filter inputs. FilterTransform derives its declared header from ActionsDAG::updateHeader, so every selected column that the policy does not reference disappears from the step header even though the rest of the pipeline still expects it. In the mixed-format fallback path this breaks queries like SELECT k FROM t / SELECT s, k FROM t with USING a on an Iceberg table that reads an ORC file: the row-policy fallback step advertises only a, and ExtractColumnsTransform or later stages can no longer find k / s. Suggested fix: keep the original FilterDAGInfo pass-through outputs and only suppress filter-column removal when the filter output aliases an input, or explicitly append the original non-filter outputs before building the fallback ExpressionActions.
Final Verdict

Status: ❌ Block

Minimum required actions:

  • Preserve non-policy selected columns in the object-storage fallback row-policy DAG.
  • Re-run the new mixed-format Iceberg / ORC row-policy tests after that fix.

LLVM Coverage Report

Measured on commit c732c77.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.60% 79.60% +0.00%

Changed lines: Changed C/C++ lines covered: 101/111 (90.99%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Sep 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c732c7705 with master 5ac4352fc (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

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

Job report

@KochetovNicolai
KochetovNicolai marked this pull request as ready for review September 29, 2026 11:17
@yariks5s yariks5s self-assigned this Sep 29, 2026
…ilter

For data files whose format does not support PREWHERE (ORC files in an
Iceberg table with format `Parquet`), `StorageObjectStorageSource` applies
the row policy with a `FilterTransform`. Adding the inputs to the outputs of
the DAG did not prevent `FilterTransform` from removing the filter column. For
`USING a`, the filter column is the input `a`, so `a` was removed and the
query failed with `NOT_FOUND_COLUMN_IN_BLOCK`.

Now the DAG is built again with `cloneSubDAG` from the filter node, with
aliases removed. If the filter node is an input, the filter column is not
removed. Otherwise it is an output. All inputs are outputs as well.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
else
row_level_outputs.push_back(row_level_filter_node);

row_level_outputs.insert(row_level_outputs.end(), row_level_dag.getInputs().begin(), row_level_dag.getInputs().end());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Rebuilding the fallback row-policy actions from cloneSubDAG({&filter_node}) drops the original DAG's pass-through outputs. cloneSubDAG only keeps the policy's dependencies, and then this code re-exports only those inputs, so any selected column that the policy does not reference disappears from the declared header. In the mixed-format fallback path that breaks queries like SELECT k FROM t with USING a: the original FilterDAGInfo preserved k for pipeline compatibility, but this rebuilt DAG does not, so ExtractColumnsTransform / later stages no longer see k.

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.

This is not true. Columns that are not inputs are pass-through by default.
A test was added to verify this case works.

@clickhouse-gh clickhouse-gh Bot added comp-table-functions Table functions for temporary read/write to external systems (FROM ... tableFunction()). and removed comp-query-optimizer Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa... labels Sep 29, 2026
KochetovNicolai and others added 2 commits September 29, 2026 16:50
For files that do not support `PREWHERE` (ORC files of an Iceberg table with
format `Parquet`), the source applies the row policy after the reader. The
columns that the policy does not use must stay in the block, in any order of
the selected columns, for a policy that is an input (`USING a`) and for a
function (`USING a > 0`).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@KochetovNicolai
KochetovNicolai added this pull request to the merge queue Sep 30, 2026
Merged via the queue into master with commit 75e72dd Sep 30, 2026
150 checks passed
@KochetovNicolai
KochetovNicolai deleted the fix-file-row-policy-prewhere-header branch September 30, 2026 14:30
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 30, 2026
superdiaodiao pushed a commit to superdiaodiao/ClickHouse that referenced this pull request Oct 1, 2026
KochetovNicolai added a commit that referenced this pull request Oct 1, 2026
…column

When the filter column of a split filter has the same name as an input,
`ActionsDAG::split` renames it in the first half, and the second half
reads it through an input under the new name, with an alias back to the
old one. `trySplitFilter` then called `removeUnusedResult` with the old
name, which removed only the alias. The filter step stopped outputting the
column, and the second half kept an input with no column in the header.
Removing unused columns rejects such an input with a logical error:

    The header [_CAST(NULL_Nullable(Nothing), 'Nullable(Nothing)'_String)]
    has a column for only 1 of the DAG's 2 inputs

Now the renamed input is removed as well, and should the second half still
read it, the filter step keeps the column.

Found by the AST fuzzer in the stress test:

    SELECT x FROM (SELECT arrayJoin([1]) AS x GROUP BY NULL WITH TOTALS) WHERE NULL
    INTERSECT DISTINCT SELECT DISTINCT 1

CI: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122952&sha=79586cd61a93fb2471e94dd87e8a93349f0e7172&name_0=PR
PR: #122952
Related: #122714

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-table-functions Table functions for temporary read/write to external systems (FROM ... tableFunction()). 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