Skip to content

Fix LOGICAL_ERROR crash in Parquet reader for nullable columns with filter - #102628

Merged
Avogar merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-parquet-reader-null-check
Apr 20, 2026
Merged

Avogar merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-parquet-reader-null-check

Conversation

@groeneai

@groeneai groeneai commented Apr 13, 2026 •

Copy link
Copy Markdown
Collaborator

Linked issues

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

Fix LOGICAL_ERROR crash "Unexpected number of rows in column subchunk" in native Parquet V3 reader when reading nullable columns with a WHERE filter.

What this PR fixes

Two bugs in the native Parquet V3 reader (src/Processors/Formats/Impl/Parquet/Reader.cpp):

1. Inverted null_map check (primary fix, line 1422):

The memchr check searched for byte 0 (non-null marker) instead of byte 1 (null marker) in the null_map. The null_map uses standard ClickHouse convention: 1 = NULL, 0 = NOT NULL, as documented in the same file:

/// Non-null element: null_map->push_back(0).
/// Null element: null_map->push_back(1).

When ALL values in a filtered subchunk were NULL and the output column was non-nullable with input_format_null_as_default=0:

  • The check incorrectly cleared the null_map (no byte 0 found = no non-null entries)
  • The column stayed at 0 rows while rows_pass was 1
  • Triggered: Logical error: 'Unexpected number of rows in column subchunk 0 1'

The inverted check also caused the opposite problem: reading non-null data from a Parquet nullable column into a non-nullable ClickHouse column with null_as_default=0 would incorrectly throw CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN even when no NULLs were present.

2. Disabled use_filter_in_decoder for nullable columns:

The filter-in-decoder optimization processes ALL rows (passing + non-passing) through processDefLevelsForInnermostColumn, so the null_map ends up with entries for all rows rather than just filtered rows. Additionally, the decoder applies the filter at consecutive encoded-value indices, but with nullable columns, null rows have no encoded values, so the filter-to-row index mapping is wrong. Disabled this optimization for nullable columns — the standard row-range iteration path correctly handles both issues.

How it was tested

  • Regression test 04099_parquet_nullable_null_check_fix.sh: creates a Parquet file with nullable string column (NULLs every 3rd row), verifies:
    1. null_as_default=1 with non-nullable output → NULLs become empty strings ✓
    2. Nullable output → NULLs preserved ✓
    3. Non-nullable output, null_as_default=0, filter hits NULL rows → CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN (not LOGICAL_ERROR crash) ✓
    4. Non-nullable output, null_as_default=0, filter hits only non-null rows → succeeds ✓
  • 50/50 passes with clickhouse-local
  • Confirmed the crash (Aborted/core dump) reproduces without the fix and is resolved with it

Closes #99019

Version info

  • Merged into: 26.4.1.1084 (included in 26.4 and later)
  • Backported to: 26.3.17.51

…ilter

Two bugs in the native Parquet V3 reader (Reader.cpp):

1. Inverted null_map check (primary fix): The memchr check at line 1422
   searched for byte 0 (non-null marker) instead of byte 1 (null marker).
   The null_map uses standard ClickHouse convention: 1 = NULL, 0 = NOT NULL.
   When ALL values in a filtered subchunk were NULL and the output column
   was non-nullable with input_format_null_as_default=0, the check
   incorrectly cleared the null_map (instead of throwing
   CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN), leaving the column with 0 rows
   while rows_pass was 1, triggering: "Logical error: Unexpected number of
   rows in column subchunk 0 1".

   The inverted check also caused the opposite bug: reading non-null data
   from a Parquet nullable column into a non-nullable ClickHouse column
   with null_as_default=0 would incorrectly throw CANNOT_INSERT_NULL even
   when no NULLs were present.

2. Disabled filter-in-decoder for nullable columns: The
   use_filter_in_decoder optimization reads ALL rows through
   processDefLevelsForInnermostColumn (populating the null_map for all
   rows, not just filtered ones) and then passes the filter to the decoder.
   With nullable columns, the decoder applies the filter at consecutive
   encoded-value indices, but null rows have no encoded values, so the
   filter-to-row mapping is wrong. Disable this optimization for nullable
   columns, falling back to the correct row-range iteration path.

Closes ClickHouse#99019

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

Copy link
Copy Markdown
Collaborator Author

Pre-PR Validation Gate (session: cron:clickhouse-ci-task-worker:20260413-221500)

a) Deterministic repro? Yes:

clickhouse-local -q "SELECT number AS id, if(number%3=0, NULL, toString(number)) AS val FROM numbers(20) INTO OUTFILE '/tmp/test.parquet' FORMAT Parquet SETTINGS output_format_parquet_use_custom_encoder=0"
clickhouse-local -q "SELECT id, val FROM file('/tmp/test.parquet', Parquet, 'id UInt64, val String') WHERE id = 0 SETTINGS input_format_null_as_default = 0"

Crashes 100% of the time without the fix (Aborted/core dump). With the fix: correctly throws CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN.

b) Root cause explained? Yes. The memchr check at Reader.cpp:1422 searched for byte 0 (non-null) instead of byte 1 (null) in the null_map. When all filtered values were NULL (null_map all 1s), memchr for 0 found nothing → didn't throw → cleared null_map → no expand() → column stayed at 0 rows → 0 != rows_pass → LOGICAL_ERROR.

c) Fix matches root cause? Yes. Changed memchr(..., 0, ...) to memchr(..., 1, ...) to correctly detect null entries. Also disabled use_filter_in_decoder for nullable columns to prevent a related null_map/filter index mismatch.

d) Test intent preserved / new test added? Yes. New regression test 04099_parquet_nullable_null_check_fix covers 4 scenarios: null_as_default on/off × nullable/non-nullable output.

e) Demonstrated in both directions? Yes. Without fix: Aborted (core dumped) on NULL rows AND incorrect CANNOT_INSERT_NULL on non-null rows. With fix: proper error on NULL rows, success on non-null rows.

f) Fix is general? Yes. The memchr fix covers all nullable Parquet columns (not just the specific stress test case). The use_filter_in_decoder fix prevents a class of issues where the filter optimization doesn't account for null_map/encoded-value index misalignment.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @al13n321 @Avogar — could you review this? It fixes an inverted memchr null_map check in the Parquet V3 reader that caused a LOGICAL_ERROR crash ("Unexpected number of rows in column subchunk 0 1") when reading nullable columns with a WHERE filter into non-nullable output with null_as_default=0. Also disables the filter-in-decoder optimization for nullable columns to prevent a related null_map/filter index mismatch.

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Apr 15, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b5170b5]

Summary: ❌

job_name test_name status info comment
Unit tests (asan_ubsan) error IGNORED

AI Review

Summary

This PR fixes a real correctness bug in the native Parquet V3 reader for nullable columns under filtering: the null check now correctly searches for 1 in null_map (NULL marker), and the use_filter_in_decoder optimization is disabled when a null map is needed to avoid row/encoded-value misalignment. The regression test covers the failing path and non-regression scenarios. I did not find additional correctness, safety, concurrency, or performance issues in the changes.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 15, 2026
Two issues fixed:
1. Pin input_format_parquet_use_native_reader_v3=1 in all queries — the test
   exercises V3 reader behavior (filter pushdown); the legacy Arrow-based
   reader has different semantics (reads ALL rows before casting, so
   null_as_default=0 errors even when WHERE excludes NULL rows).

2. Add '| head -1' to grep pipeline — CI's --send_logs_level=warning causes
   CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN to appear in both a server log
   message and the exception, producing duplicate grep matches.

3. Replace section 4 (read only non-null rows from file with NULLs) with
   reading from a NULL-free file — the V3 reader processes data at subchunk
   granularity, so NULLs in the same subchunk as requested rows cause errors
   regardless of the WHERE filter. The new test validates the fix doesn't
   break the non-null case.

Verified: 50/50 passes with full randomization.

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

Copy link
Copy Markdown
Collaborator Author

Test fix pushed (commit b5170b5)

The regression test 04099_parquet_nullable_null_check_fix had a ~10-20% failure rate in CI (56 failures across 11 check types). Root causes identified and fixed:

Issue 1: Legacy reader path. CI randomizes input_format_parquet_use_native_reader_v3. When set to 0, the legacy Arrow-based reader is used, which reads ALL rows before casting — so null_as_default=0 errors even when the WHERE clause excludes NULL rows. Fixed by pinning input_format_parquet_use_native_reader_v3 = 1 in all queries, since this test specifically exercises the V3 reader fix.

Issue 2: Duplicate grep match. CI adds --send_logs_level=warning to the client, causing CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN to appear in both a server log message and the exception. The grep -o then produced 2 lines instead of 1. Fixed with | head -1.

Issue 3: Non-deterministic section 4. The original test tried to read only non-null rows (WHERE id IN (1, 2, 4)) from a file containing NULLs with null_as_default=0. This fails intermittently because the V3 reader processes data at subchunk granularity — NULLs in the same subchunk cause errors regardless of the WHERE filter. Replaced with reading from a NULL-free file, which reliably validates the fix doesn't break the non-null case.

Verified: 50/50 passes with full randomization locally.

Session: cron:clickhouse-ci-task-worker:20260415-161500

@clickhouse-gh

clickhouse-gh Bot commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.00% 84.10% +0.10%
Functions 90.90% 90.90% +0.00%
Branches 76.50% 76.60% +0.10%

Changed lines: 90.00% (9/10) · Uncovered code

Full report · Diff report

@Avogar

Avogar commented Apr 20, 2026

Copy link
Copy Markdown
Member

@Avogar
Avogar added this pull request to the merge queue Apr 20, 2026
Merged via the queue into ClickHouse:master with commit 728336b Apr 20, 2026
159 of 161 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 20, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @groeneai @Avogar — the changelog category for this PR might need a look.

Current: Bug Fix (user-visible misbehavior in an official stable release)
Suggested: Critical Bug Fix (crash, data loss, RBAC) or LOGICAL_ERROR

Why: The PR fixes a LOGICAL_ERROR exception (which causes abort/core dump in debug builds). LOGICAL_ERROR is explicitly listed under 'Critical Bug Fix (crash, data loss, RBAC) or LOGICAL_ERROR' category, not the general 'Bug Fix' category. The linked issue #99019 confirms this reproduces on ClickHouse 26.2.4.23 (a supported stable release).

Could you verify this is correct? Ignore if the current category is intentional.

@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to 26.3 (LTS), 26.2, 26.1, 25.8 (LTS), but no backport label was found.

Affected code: decodePrimitiveColumn in Reader.cpp — memchr null check and use_filter_in_decoder condition — introduced in 25.8.

Why: The memchr null detection bug exists since the V3 reader was introduced in 25.8 (setting added as false, false in SettingsChangesHistory for 25.8). It was enabled by default in 25.11 (false, true). The use_filter_in_decoder bug affects 26.2+. Both cause LOGICAL_ERROR exceptions. The linked issue #99019 confirms the bug reproduces on 26.2.4.23. All supported branches (25.8, 26.1, 26.2, 26.3) contain the V3 reader code; the memchr fix applies to all, and the filter-in-decoder fix applies to 26.2+.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

@watson-ch

watson-ch Bot commented May 15, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/ClickHouse/clickhouse-core-incidents/issues/1508

Labels added:

  • v25.12-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

@watson-ch

watson-ch Bot commented May 15, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/ClickHouse/clickhouse-core-incidents/issues/1509

Labels added:

  • v25.12-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label May 15, 2026
zvonand added a commit to Altinity/ClickHouse that referenced this pull request May 18, 2026
Antalya 26.3 Backport of ClickHouse#102628 - Fix LOGICAL_ERROR crash in Parquet reader for nullable columns with filter
@watson-ch watson-ch Bot added v26.2-must-backport and removed pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels May 19, 2026
@watson-ch

watson-ch Bot commented May 19, 2026

Copy link
Copy Markdown

Backport labels have been automatically added based on https://github.com/ClickHouse/clickhouse-core-incidents/issues/1731

Labels added:

  • v26.2-must-backport

These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels.

@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label May 19, 2026
robot-ch-test-poll2 added a commit that referenced this pull request May 19, 2026
Cherry pick #102628 to 26.2: Fix LOGICAL_ERROR crash in Parquet reader for nullable columns with filter
robot-clickhouse added a commit that referenced this pull request May 19, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label May 19, 2026
il9ue pushed a commit to il9ue/ClickHouse that referenced this pull request May 20, 2026
…-null-check

Fix LOGICAL_ERROR crash in Parquet reader for nullable columns with filter
Avogar added a commit that referenced this pull request May 21, 2026
Backport #102628 to 26.2: Fix LOGICAL_ERROR crash in Parquet reader for nullable columns with filter
subkanthi pushed a commit to Altinity/ClickHouse that referenced this pull request May 26, 2026
…-null-check

Fix LOGICAL_ERROR crash in Parquet reader for nullable columns with filter
subkanthi pushed a commit to Altinity/ClickHouse that referenced this pull request May 26, 2026
Antalya 26.3 Backport of ClickHouse#102628 - Fix LOGICAL_ERROR crash in Parquet reader for nullable columns with filter
@jrogov

jrogov commented Jul 14, 2026

Copy link
Copy Markdown

@Avogar I might be misunderstanding how backporting process works in Clickhouse, but shouldn't this also be backported to v26.3?

@groeneai

Copy link
Copy Markdown
Collaborator Author

The backport targets come from the `vXX.Y-must-backport` labels, which were added automatically based on the linked core-incidents reports (v25.12 from #1509, v26.2 from #1731), not chosen manually. This fix was already backported to 26.2.19.43. Adding v26.3 is a release-maintainer call, so it is up to Avogar / the release team to apply the label if 26.3 should get it. As the author I do not add backport labels or open manual backport PRs myself.

@jrogov

jrogov commented Jul 14, 2026

Copy link
Copy Markdown

That's the thing: I can reproduce the issue on the most recent v26.3 LTS version. Looking at the code, it's expected – the necessary changes simply aren't there.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Thanks for confirming - you are right, the fix is absent from 26.3. Only the v25.12-must-backport and v26.2-must-backport labels were applied here (auto-added from core-incidents #1509 and #1731), so the backport bot created 25.12 and 26.2 PRs but never a 26.3 one. 26.3 is a separate LTS line that was not labeled.

Since 26.3 is confirmed affected, the fix should be backported there too. @Avogar could you add the v26.3-must-backport label? The automation will then cherry-pick the fix.

For verification against 26.3 HEAD, the fix is in merge commit 728336b (two parts in the native Parquet reader: the inverted null_map memchr check, and disabling use_filter_in_decoder for nullable columns).

@Avogar Avogar added v26.3-must-backport and removed pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore labels Jul 14, 2026
@Avogar

Avogar commented Jul 14, 2026

Copy link
Copy Markdown
Member

Let's backport to 26.3

@groeneai

Copy link
Copy Markdown
Collaborator Author

The `v26.3-must-backport` label is now applied, so the backport automation will cherry-pick this into 26.3 the same way it did for 25.12 and 26.2 (see #105311). Nothing further needed manually. Thanks @jrogov for confirming it reproduces there.

@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 14, 2026
Avogar added a commit that referenced this pull request Jul 17, 2026
Backport #102628 to 26.3: Fix LOGICAL_ERROR crash in Parquet reader for nullable columns with filter
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
…t reader for nullable columns with filter
bytehalo-infra added a commit to sentioxyz/ClickHouse that referenced this pull request Sep 25, 2026
PROTO: Parquet V3 reader, Nullable columns with WHERE -> exc LOGICAL_ERROR.
Effect in the production build a394e92: its upstream test(s) 04099_parquet_nullable_null_check_fix fail on it and pass on official 26.8.8.8, run on isolated servers (evidence/fix_ci_20260925/upstream_repro/server_triage_PROD263_vs_UP2688.tsv).
Upstream patch unchanged, with its test(s): 04099_parquet_nullable_null_check_fix.

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 pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v25.12-must-backport v26.2-must-backport v26.3-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error reading Parquet file in v26.2.4.23

9 participants