Skip to content

Fix Parquet v3 reader logical error on key-condition column with no file mapping - #109267

Merged
PedroTadim merged 7 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-107951-site6-parquet-keycond-unmapped
Jul 22, 2026
Merged

PedroTadim merged 7 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-107951-site6-parquet-keycond-unmapped

Conversation

@groeneai

@groeneai groeneai commented Jul 3, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #107951 (AST fuzzer, site 6)

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 a logical error (Logical error: 'KeyCondition uses PREWHERE output') in the Parquet v3 reader that occurred when a format-level key condition references a column that is not read from the Parquet file.

Description

The Parquet v3 reader (src/Processors/Formats/Impl/Parquet/Reader.cpp) maps each column used by the format-level key_condition to a file-readable output column, so it can enable row-group min/max pruning for those columns. It threw a LOGICAL_ERROR ("KeyCondition uses PREWHERE output") when the mapping was absent, on the assumption that the key condition only ever references columns read from the file. This logical error is a caught exception in release builds; it aborts the server only in debug and sanitizer builds.

The key_condition can reference a column that is not read from the file when that column appears only inside indexHint and the query reads no data column for it — for example an aggregate such as SELECT count() combined with both a PREWHERE indexHint(<cond on s>) and a WHERE indexHint(<cond on s>) on the same column s. The mapping to a file-readable output column is then absent and the reader throws the logical error. This reproduces with plain client SQL under the old analyzer (enable_analyzer = 0); the new (default) analyzer maps the column and is unaffected. The match with a non-constant pattern in the original fuzzer query is incidental — a plain indexHint(s = 'x') reproduces it just as well.

Fix: a key-condition column with no file-readable output column has no column-chunk or page-index statistics, so it cannot participate in pruning. Skip it at both pruning sites that map key-condition columns to file outputs: the row-group loop (leave its range as the whole universe, exactly as the reader already does for a column whose min/max statistics are missing) and the page-index loop right after it (extractSingleColumnConditions, active by default via input_format_parquet_page_filter_push_down), which otherwise threw LOGICAL_ERROR "Column condition uses PREWHERE output" on the identical unmapped column. A query that would otherwise succeed keeps returning correct, unpruned results, and the logical error is removed.

Observed on AST fuzzer (amd_debug, targeted) runs of two independent Parquet-v3-reader PRs (#106952 and #102499); never on master in 90 days.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106952&sha=80f44a37c823b1f13a3655214db9cbe9bc66a934&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29

A stateless test is added (tests/queries/0_stateless/04498_parquet_key_condition_prewhere_output). It exercises the crash with plain client SQL under enable_analyzer = 0. Verified against the CI debug builds: it aborts on master HEAD (SIGABRT, Logical error: 'KeyCondition uses PREWHERE output' at Reader.cpp:370) and passes on this PR, so per-arch Bugfix validation validates the fix.

Version info

  • Merged into: 26.7.1.1331 (included in 26.7 and later)

The Parquet v3 reader maps each column referenced by the format-level
key_condition to a file-readable output column, to enable row-group min/max
pruning. It asserted (LOGICAL_ERROR) when the mapping was absent, on the
assumption that the key condition only references columns read from the file.

The AST fuzzer's internal execution path can construct a key_condition that
references a column not read from the file (e.g. a PREWHERE output produced by
match(<non-const>, s), which normal SQL rejects with Code 44 before it reaches
the reader). The mapping is then absent and the reader aborts.

A key-condition column with no file-readable output column has no column-chunk
statistics, so it cannot participate in row-group pruning. Leave its range as
the whole universe (skip it), exactly as the reader already does for a column
whose min/max statistics are missing. The query that would otherwise succeed
keeps returning correct, unpruned results, and the abort is removed.

Found by the AST fuzzer (issue ClickHouse#107951, site 6). Not reproducible via client
SQL: the analyzer rejects the trigger before execution.

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

groeneai commented Jul 3, 2026 •

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes, with plain client SQL (correction to the original wording below). The crash reproduces under the old analyzer (enable_analyzer = 0) with an aggregate plus PREWHERE indexHint(s = 'x') WHERE indexHint(s = 'y') on a column s that is never read as an output column. Covered by the stateless test 04498_parquet_key_condition_prewhere_output. My earlier attempts failed only because I tested under the default (new) analyzer, which maps the column.
b Root cause explained? Reader::prefilterAndInitRowGroups maps each key_condition->getUsedColumns() index to a file-readable output column, to enable row-group min/max pruning. It asserted LOGICAL_ERROR when the mapping was absent, assuming the key condition only references file-read columns. The fuzzer-built condition references a non-file column, so sample_block_to_output_columns_idx[idx] is empty and the reader throws the logical error.
c Fix matches root cause? Yes. Such a column has no column-chunk statistics, so it cannot participate in pruning; leaving its hyperrectangle range as the whole universe (skip it) is the correct behavior. The change is throw -> continue, exactly mirroring how the reader already handles a column with missing min/max stats (getHyperrectangleForRowGroup continue on !allow_stats / !__isset.statistics).
d Test intent preserved / new tests added? No existing test is weakened. No new stateless test is added because the trigger is not constructible via client SQL (analyzer rejects it pre-execution); prior internal-path-only sites of this consolidation (#107951) shipped test-less for the same reason. Existing parquet filter-pushdown/prewhere tests still pass (see e).
e Both directions demonstrated? Direction "passes with fix": 02841_parquet_filter_pushdown, 03596_parquet_prewhere_page_skip_bug, 03914_parquet_v3_prewhere_non_bool_filter all PASS on the patched debug binary (Build ID 20a794aa...). These exercise the exact key_condition->getUsedColumns() min/max-pruning path. Direction "fails without fix": established by code inspection - the removed line is a bare throw LOGICAL_ERROR on the !output_idx.has_value() branch; the patch is a pure superset (byte-identical behavior on the reachable has_value() path, continue instead of the logical-error throw on the unreachable-via-SQL branch).
f Fix is general across code paths? Yes. This is the only site that maps key-condition columns to file outputs. The three downstream consumers of the resulting used_by_key_condition flag (min/max stats, bloom filter, dictionary filter) already treat an unset flag as "no pruning", so skipping an unmapped column is consistent with all of them. Not a symptom guard: the unmapped column legitimately has no stats, so skipping pruning is the correct semantics, not a mask over an upstream defect.
g Fix generalizes across inputs? N/A for datatype/wrapper matrix - the fix is type-agnostic (it acts on the presence/absence of a file-readable output-column mapping, independent of the column's type). It applies uniformly to any key-condition column with no file mapping.
h Backward compatible? Yes. No setting, format, wire, or on-disk change. Behavior change is strictly: an internal-only logical error becomes a correct, unpruned result. No SettingsChangesHistory.cpp impact.
i Invariants and contracts preserved? Yes. The pruning invariant is preserved: an unmapped column simply does not narrow its hyperrectangle range (stays whole-universe), which checkInHyperrectangle handles as "cannot prune". No primitive_columns[...].used_by_key_condition is set for the skipped column, so the three downstream pruning consumers remain consistent. No lifetime/concurrency contracts touched.

Session id: cron:clickhouse-worker-slot-0:20260703-092701

@groeneai

groeneai commented Jul 3, 2026 •

Copy link
Copy Markdown
Collaborator Author

cc @al13n321 @scanhex12 could you review this? The Parquet v3 reader threw a LOGICAL_ERROR when the format-level key condition references a column not read from the file (reachable only on the AST-fuzzer internal path). The fix skips such a column for row-group pruning instead of asserting, exactly as it already does for a column with missing min/max stats. Issue #107951 (site 6).

@clickhouse-gh

clickhouse-gh Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ddd60e5]

Summary: ✅


AI Review

Summary

This PR removes the LOGICAL_ERROR throws in the Parquet v3 reader when a format-level key_condition refers to a column that is not mapped to a file output, and it adds a regression test that covers the old-analyzer repro in a parallel-safe form. I reviewed the current head, the prior discussion, and the resolved bot thread; I did not find any remaining correctness, coverage, or metadata issues in the final patch.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 3, 2026
The row-group pruning loop was fixed to skip a key-condition column with no
file-readable output column (no column-chunk stats). The page-index setup
runs extractSingleColumnConditions right after and mapped the same column
indices; with input_format_parquet_page_filter_push_down enabled (default),
an unmapped column still threw LOGICAL_ERROR "Column condition uses PREWHERE
output", so the AST-fuzzer shape could still abort before any data is read.

An unmapped column has no page-index statistics either, so it cannot
participate in page-level pruning. Skip it there too (page-level pruning is
disabled for that condition), matching the row-group site.

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

groeneai commented Jul 3, 2026 •

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes, with plain client SQL. Correction to an earlier claim in this thread: the crash IS reproducible from SQL under the old analyzer (enable_analyzer = 0) with an aggregate plus PREWHERE indexHint(s = 'x') WHERE indexHint(s = 'y'), so column s is never read as an output column. Covered by the stateless test 04498_parquet_key_condition_prewhere_output (aborts on master HEAD in debug/sanitizer builds, passes on this PR). The non-constant match pattern in the original fuzzer query is incidental.
b Root cause explained? Yes. Reader::prefilterAndInitRowGroups maps each key_condition column index to a file-readable output via sample_block_to_output_columns_idx. A PREWHERE-only / non-file column has no mapping. The first commit made the row-group loop continue on unmapped, but the page-index loop right after (extractSingleColumnConditions, gated by input_format_parquet_page_filter_push_down=1 default) still threw LOGICAL_ERROR "Column condition uses PREWHERE output" on the identical unmapped index, so the same fuzzer shape throws before reading data.
c Fix matches root cause? Yes. The unmapped column has no page-index statistics, so it cannot drive page-level pruning; skip it (continue) so pruning is simply disabled for that condition, mirroring the row-group site. Not a symptom guard: it is the same semantically-correct handling the reader already uses when a column has no stats.
d Test intent preserved / new tests added? No stateless test (documented in the PR body): the trigger is only reachable on the AST-fuzzer internal path; the analyzer rejects the SQL form. No existing test weakened.
e Both directions demonstrated? Yes. Same forced repro on a Build-ID-verified debug binary: old throw -> Logical error: 'Column condition uses PREWHERE output', which aborts the server in the debug build (signal 6, connection refused). Fixed continue -> query returns 499, server alive. This logical error is a caught exception in release builds; it aborts only in debug and sanitizer builds.
f Fix is general across code paths? Yes. This IS the generalization the reviewer asked for: both sites that map key-condition columns to file outputs (row-group loop line ~366 and page-index loop line ~440) now handle the unmapped column identically. Audited the other three sample_block_to_output_columns_idx uses (lines 708, 736, 2228): none is the key-condition-pruning pattern; each already handles the no-mapping case via its own branch (prewhere-output tracking / else / chassert). No third sibling site.
g Fix generalizes across inputs? N/A for input datatypes: the branch is a structural mapping test (column has a file output or not), independent of the column's type/wrapper/value. It fires for any key-condition column without a file-readable output.
h Backward compatible? Yes. No setting, format, or on-disk/wire change. Removes a logical error; a query that previously raised it now returns correct unpruned results. No SettingsChangesHistory change needed.
i Invariants and contracts preserved? Yes. continue only skips attaching a column_index_condition for a column that has no page index, which is exactly the precondition the following code (output_info.is_primitive, decoder.allow_stats) already checks and continues on. Pruning is a pure optimization: skipping it yields a correct superset of granules/pages, never wrong results. No lock/ownership/lifetime contract touched.

Session id: cron:clickhouse-worker-slot-4:20260703-114900

@groeneai

groeneai commented Jul 3, 2026 •

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 4c737a7

CI fully finished (Finish Workflow SUCCESS). No failed checks. This PR fixes the Parquet v3 reader logical error on a key-condition column with no file mapping (both the row-group-pruning and page-index sites), related to #107951.

No failures to attribute.

Session id: cron:our-pr-ci-monitor:20260703-163000

@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, Logical error does not lead to server abort in the release build - it is just an exception. Correct the description and remember this.

@groeneai groeneai changed the title Fix Parquet v3 reader abort on key-condition column with no file mapping Fix Parquet v3 reader logical error on key-condition column with no file mapping Jul 5, 2026
@groeneai

groeneai commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator Author

@alexey-milovidov Corrected. The description, changelog entry, and comments no longer describe the LOGICAL_ERROR as a release-build abort; it is a caught exception in release builds and aborts only in debug and sanitizer builds. No code change.

@alexey-milovidov

Copy link
Copy Markdown
Member

The bugfix validation check should have failed here, but it didn't fail. We have to figure out why.

@alexey-milovidov

Copy link
Copy Markdown
Member

The AST fuzzer constructed a query that failed:

SELECT count() IGNORE NULLS FROM file('02841.parquet') PREWHERE indexHint(match(toFixedString('-0.2147483648', 255), s)) WHERE indexHint(toFixedString('', 13) NOT LIKE s) LIMIT 19

Why can it not be reproduced with a test?

@alexey-milovidov

Copy link
Copy Markdown
Member

The bugfix validation check should have failed here, but it didn't fail. We have to figure out why.

Ok, this is solved. It is green, because the PR description has a link to CI report.

…output crash

The `KeyCondition uses PREWHERE output` logical error in the Parquet v3 reader
is reproducible with a plain SQL test after all. The trigger is not the
non-constant `match` pattern, but the combination of an aggregate (so the
key-condition column is never read as an output column) with both a
`PREWHERE indexHint(...)` and a `WHERE indexHint(...)` on that column, under the
old analyzer (`enable_analyzer = 0`).

Verified against downloaded CI debug builds: the test crashes on master HEAD
(`SIGABRT`, `Logical error: 'KeyCondition uses PREWHERE output'` at
`Reader.cpp:370`) and passes on this PR, so per-arch Bugfix validation now
catches the fix.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@groeneai

groeneai commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator Author

@alexey-milovidov You are right, and thanks for the test. It does reproduce from plain SQL under the old analyzer (enable_analyzer = 0): with an aggregate plus PREWHERE indexHint(s = 'x') WHERE indexHint(s = 'y'), s is never read as an output column, so the key-condition column has no file mapping. My earlier "not client-constructible" claim was wrong; I had only tried it under the default (new) analyzer, which maps the column. The non-constant match in the fuzzer query was incidental. I verified 04498_parquet_key_condition_prewhere_output locally (0 / 10 / 0, no abort on the fixed binary) and corrected the earlier gate comments.

The test used a fixed file('04498.parquet') in the shared user_files dir.
Under the flaky check it runs many times concurrently; one iteration's
INSERT ... engine_file_truncate_on_insert=1 truncated the file to 0 bytes
while a concurrent iteration read it, giving Code 117 'Parquet file too
short: 0 bytes' / Code 636 'file is empty'.

Rewrite as a .sh test using clickhouse-local and a per-test unique file
($CLICKHOUSE_TMP/$CLICKHOUSE_TEST_UNIQUE_NAME.parquet), so concurrent runs
never collide. Same three queries and same 0/10/0 reference; test intent
(detect the 'KeyCondition uses PREWHERE output' logical error) unchanged.

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

groeneai commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator Author

The flaky check failed on the new test: it used a fixed file('04498.parquet') in the shared user_files dir, and the flaky check runs it many times concurrently. One iteration's INSERT ... engine_file_truncate_on_insert=1 truncated the file to 0 bytes while a concurrent iteration read it, so some runs saw Code 117 Parquet file too short: 0 bytes / Code 636 file is empty.

Rewrote it as a .sh test using clickhouse-local and a per-test unique file ($CLICKHOUSE_TMP/$CLICKHOUSE_TEST_UNIQUE_NAME.parquet), so concurrent runs no longer collide. Same three queries, same 0/10/0 reference. Verified 30 concurrent copies all pass locally (head 1275d5d).

@groeneai

groeneai commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 1275d5d

Every failure below has an owner. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (asan/tsan/msan/debug, flaky check) / 04498_parquet_key_condition_prewhere_output PR-caused (parallel filename collision in the new test) fixed in this PR @ 1275d5d (rewritten as parallel-safe .sh; flaky check now green, 57/57)
AST fuzzer (amd_debug) / STID 0993-27f0 "Block structure mismatch in UnionStep" chronic trunk AST-fuzzer bug (14 master + many unrelated PRs / 30d) #107719 (ours, open)

Session id: cron:our-pr-ci-monitor:20260708-050000

alexey-milovidov and others added 2 commits July 19, 2026 13:26
…test comment

The style check rejects the wording "new analyzer" because the analyzer is enabled by default since 24.3 and is no longer new. The Style check failure also dropped all downstream jobs including Bugfix validation, which made Finish Workflow red.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109267&sha=b5e76b6e2755e911c785fa9d04205ce7a2b2eda7&name_0=PR&name_1=Style%20check
PR: ClickHouse#109267

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 80.10% 86.20% +6.10%
Functions 91.00% 92.10% +1.10%
Branches 72.20% 78.20% +6.00%

Changed lines: Changed C/C++ lines covered: 12/12 (100.00%) · Uncovered code

Full report · Diff report

@PedroTadim
PedroTadim added this pull request to the merge queue Jul 22, 2026
Merged via the queue into ClickHouse:master with commit d638785 Jul 22, 2026
178 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 22, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…test comment

The style check rejects the wording "new analyzer" because the analyzer is enabled by default since 24.3 and is no longer new. The Style check failure also dropped all downstream jobs including Bugfix validation, which made Finish Workflow red.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109267&sha=b5e76b6e2755e911c785fa9d04205ce7a2b2eda7&name_0=PR&name_1=Style%20check
PR: ClickHouse#109267

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…1-site6-parquet-keycond-unmapped

Fix Parquet v3 reader logical error on key-condition column with no file mapping
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 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.

4 participants