Skip to content

Fix Parquet PREWHERE evaluating a conversion on the rows its guard rejects when both share a subexpression - #132560

Open
groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:parquet-prewhere-carry-shared-intermediates
Open

groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:parquet-prewhere-carry-shared-intermediates

Conversation

@groeneai

@groeneai groeneai commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #132525
Related: #129178

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 a CANNOT_PARSE_DATETIME (or another conversion error) when reading Parquet files with a filter whose conditions share an expression, such as substring(s, 1, 19) != 'invalid' AND toDateTime64(substring(s, 1, 19), 3) > '2020-01-01': the conversion was evaluated on the rows rejected by the earlier condition. Closes #132525.

Description

On 26.10.2.103 (also 26.8, 26.3), a file() read of Parquet with WHERE substring(s, 1, 19) != 'invaliddatetime' AND toDateTime64(substring(s, 1, 19), 3) > '2020-01-01' rejects valid input with CANNOT_PARSE_DATETIME. The same query on MergeTree, or with optimize_move_to_prewhere = 0, returns the row.

Root cause. tryBuildPrewhereSteps splits the PREWHERE so that the guard filters before toDateTime64 runs, and passes the shared substring(s, 1, 19) from step 1 to step 2 by name. The Parquet reader passes columns between steps only through slots of extended_sample_block, which has no slot for that intermediate, so preparePrewhere dropped the split and ran everything as one step, where toDateTime64 sees every row.

Change. Such an intermediate gets a hidden slot in RowSubgroup::output after the columns of extended_sample_block. The producing step writes it, later steps read it, and the existing per-step filtering and end-of-PREWHERE cleanup cover it. The splitter is unchanged; a generated name equal to a file column (04342) still falls back to one step.

Tests. New 05828 (WHERE, 3-step PREWHERE, row policy) fails on master and passes with the fix 50/50 under randomized settings. All parquet/prewhere stateless tests: no new failures.

Price. Such queries now run the same steps as the twin without a shared expression. On a dictionary-encoded column the old single step could evaluate the whole condition once per dictionary value; measured on 2M rows (debug build): 24 ms before, 71 ms after, 64 ms for the twin on master. Plain-encoded: 82 ms before and after.


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

groeneai and others added 2 commits November 18, 2026 01:50
`tryBuildPrewhereSteps` splits a PREWHERE conjunction so that a guard filters
before a condition that may throw, e.g.
`substring(s, 1, 19) != 'invaliddatetime' AND toDateTime64(substring(s, 1, 19), 3) > ...`.
The shared `substring(s, 1, 19)` is computed in step 1 and passed to step 2
by name. The Parquet V3 reader passes columns between steps only through
slots of `extended_sample_block` (query outputs and file columns), so the
intermediate had no slot and `preparePrewhere` dropped the split. In the
single step `toDateTime64` ran on every row and threw CANNOT_PARSE_DATETIME
on the row the guard rejects. MergeTree and Memory pass a name-keyed block
between steps and return the row.

Give such an intermediate a hidden slot in `RowSubgroup::output` after the
columns of `extended_sample_block`, recorded in `prewhere_intermediates`.
The producing step writes it like any prewhere output, later steps read it
in `applyPrewhere`, the existing per-step filter loop filters it, and the
last step drops every slot past `sample_block`. The name -> slot map is per
`add_step` call, so the row-level filter and PREWHERE stay independent. A
generated name equal to a file column still falls back to one step (04342).
`decode_dictionary_as_replicated` skips a last step whose only input is an
intermediate.

Closes ClickHouse#132525

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Remove the unslotted_outputs comment, which restates the rule stated just above it.

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; 1 full pass).

# Sev Finding Verdict Evidence / action
1 ❌ Keeping the split lets a stateful function in PREWHERE (rowNumberInBlock()) see only the rows earlier steps kept, so the result changes DISAGREE In the repro, rowNumberInBlock() = 1 reads no column, and MergeTreeWhereOptimizer never moves such a conjunct (MergeTreeWhereOptimizer.cpp:490), so it stays in WHERE. Master also filters in its single PREWHERE step and returns the same []. In an explicit PREWHERE, the split is the one master already takes without a shared subexpression, and the one MergeTree takes.

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

Session id: cron:clickhouse-review-slot-8:20261118-015500

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The issue's query on a 2-row File(Parquet) table: master e8cb3be fails 10/10 with Code: 41 CANNOT_PARSE_DATETIME under randomized settings.
b Root cause explained? The guard and the conversion share substring(s, 1, 19). The splitter passes it from step 1 to step 2 by name, the Parquet reader has no slot for it, so preparePrewhere dropped the split and evaluated toDateTime64 on every row in one step.
c Fix matches root cause? Yes. The reader now gives such an intermediate a hidden slot after extended_sample_block, so the split the splitter chose (the steps MergeTree runs) is executed. No setting, tag or guard.
d Test intent preserved / new tests added? New 05828: projected WHERE, non-projected WHERE, 3-step PREWHERE, row policy. All four fail on master. Removing each new code path (never allocate a slot, skip the intermediate read, skip the dictionary-path bound check) makes it fail.
e Both directions demonstrated? Master 10/10 FAIL, fix 50/50 OK, same test and runner, randomized settings.
f Fix is general across code paths? Both callers (row-level filter and PREWHERE) get the slots, each with its own name map. MergeTree and Memory already pass intermediates by name. s3(), url() and data lakes read Parquet through the same reader.
g Fix generalizes across inputs (params/datatypes/wrappers)? String, Nullable, LowCardinality, LowCardinality(Nullable) with NULL and empty rows, 2- and 3-step splits, row policy plus PREWHERE together: results equal to MergeTree.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or protocol change. Results only change where master threw: every parquet/prewhere stateless test has the same verdict on both binaries, except the new one.
i Invariants and contracts preserved? A step reads only columns every earlier step filtered (the existing filter loop covers every populated slot). Intermediates never reach the output: they are dropped after the last step. A step that passes one through does not write it again.

Session id: cron:clickhouse-impl-slot-41:20261118-004600

@clickhouse-gh

clickhouse-gh Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4fea046]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

The PR preserves split Parquet PREWHERE evaluation by carrying shared expressions between steps, preventing guarded conversions from evaluating rejected rows. No actionable findings at 4fea04675d54d0249064f5f1c8728c2201f66ed9. The added slots are handled by preparePrewhere and applyPrewhere; subgroup initialization allocates them, physical decoding and cache operations retain their original index bounds, and ReadManager::read delivers only requested columns. Final-step cleanup and ReadManager::clearRowSubgroup release the intermediates. The new regression test covers projected and unprojected expressions, three steps, and row policies, and passed at the reviewed head.

Missing context / blind spots
  • ⚠️ Automated bugfix validation was dropped after an earlier configuration failure; the workflow completion check consequently failed. Rerunning those jobs would independently confirm the author's reported before/after results. 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 can be tested Allows running workflows for external contributors comp-parquet-reader-v3 Issues related to the native Parquet reader. labels Nov 18, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 4fea046

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Build profile diff / LogClusterNotReady: the endpoint never became ready CI infra, not from this PR: all 8 readiness probes to the CI logs cluster ended Read timed out. (read timeout=3), so the job raised before comparing anything. 4 Build profile diff runs on 4 PRs went red the same way between 03:00Z and 05:00Z #129675 (mine, open)
PR praktika rollup of the row above #129675 (mine, open)

The other CIDB FAIL rows on this head are Config Workflow / Pre Hooks and Finish Workflow / Post Hooks of the runs before the can be tested label.

Session id: cron:our-pr-ci-monitor:20261118-060016

This branch has not been deployed

No deployments
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-parquet-reader-v3 Issues related to the native Parquet reader. pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parquet PREWHERE evaluates a guarded conversion on the rows its guard rejects when both share a subexpression

1 participant