Skip to content

Fix WHERE on a Hive partition column skipping files whose rows match it - #131256

Merged
PedroTadim merged 4 commits into
ClickHouse:masterfrom
groeneai:hive-path-filter-value-conversion-127133
Nov 16, 2026
Merged

PedroTadim merged 4 commits into
ClickHouse:masterfrom
groeneai:hive-path-filter-value-conversion-127133

Conversation

@groeneai

@groeneai groeneai commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #127133
Related: #131202

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 WHERE condition on a Hive partition column (use_hive_partitioning = 1) skipping files whose rows match it when the path filter parsed the partition value differently from the reader, for example k0=300 for a Nullable(UInt8) column (read as NULL) or a=NULL for a Nullable(String) column (read as the string 'NULL'). Closes #127133.

Description

On 26.10.2.103 (and 26.3.33.24, 26.8.21.12), SELECT count() FROM file('d/k0=300/data.jsonl', JSONEachRow, 'c0 Int32, k0 Nullable(UInt8)') WHERE k0 IS NULL SETTINGS use_hive_partitioning = 1 returns 0, while the file's only row reads k0 as NULL.

A WHERE on a partition column is first evaluated on the listed paths to skip files. VirtualColumnUtils::addPathAndFileToVirtualColumns built that filter block with text deserialization, while the readers convert with convertFieldToType: text parsing wraps 300 to 44 for UInt8, rejects -1 (Code: 632) and reads the text NULL as SQL NULL, so the condition was evaluated on a value the query never sees.

The filter now calls HivePartitioningUtils::convertPartitionValueToType, the readers' conversion (added by #131202; this PR is stacked on it, only the last commit is new). For the table functions it sees every value as the column reads it. A path value the reader rejects (k0=300 for UInt8, or the text NULL for Nullable(Int64)) now fails a filter on that column with the reader's TYPE_MISMATCH, as reading it already does; before, such a path was skipped or not depending on how the filter parsed it. Tables with stored format settings (ENGINE = File(...) SETTINGS bool_true_representation = 'si') still convert in the filter with the query's format settings, unchanged here.

Validation: the new test fails on the base and passes with the fix (100/100 randomized runs); the existing Hive tests pass, including the s3 ones. Filtering 20000 paths costs about 9 ms more in a debug build (0.203 -> 0.212 s median).

🤖 Generated with Claude Code


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

Version info

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

groeneai and others added 2 commits November 13, 2026 06:45
…n type

`convertFieldToType` returns `Null` for a value outside the range of the
target type. The two places that materialize a Hive partition value from
the path, `HivePartitioningUtils::addPartitionColumnsToChunk` and the Hive
branch of `VirtualColumnUtils::addRequestedFileLikeStorageVirtualsToChunk`,
passed that result straight to `createColumnConst`. For a type that cannot
hold NULL the column insert then failed with `BAD_GET` (`UInt8` with
`k0=300` or `k0=-1`, `Array(UInt8)` with `k0=[256]`, `Tuple`, ...), and for
`LowCardinality(UInt8)` with the `LOGICAL_ERROR` "ColumnUnique can't contain
null values".

Both sites now call `HivePartitioningUtils::convertPartitionValueToType`,
which throws `TYPE_MISMATCH` naming the value, the path, the type and the
column when the result is `Null` and `canContainNull(type)` is false. That
is the error class an unparsable value already gets. `Nullable` and
`LowCardinality(Nullable)` keep reading such a value as NULL;
`Variant` and `Dynamic` are unchanged. `convertFieldToTypeOrThrow` is not
reused: it also throws for a `Nullable` target, and its message names
neither the column nor the path.

The path pre-filter (`addPathAndFileToVirtualColumns`) parses values with
text deserialization rather than `convertFieldToType`, so it does not hit
this error, and is left unchanged.

Closes ClickHouse#131087

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With `use_hive_partitioning = 1`, a `WHERE` on a Hive partition column is
first evaluated on the listed paths to skip files before reading
(`getFilterByPathAndFileIndexes`). `addPathAndFileToVirtualColumns` built
that block with text deserialization of the column type, while the readers
convert the value with `convertFieldToType` (via
`HivePartitioningUtils::convertPartitionValueToType`). Text parsing wraps
an out-of-range integer (`300` -> 44 for `UInt8`), rejects `-1` for an
unsigned type and reads the text `NULL` as SQL NULL for `Nullable(String)`,
where the readers give NULL, NULL and the string 'NULL'. So the filter
evaluated the condition on a value the query never sees and skipped
matching files: `WHERE k0 IS NULL` over `k0=300` with `Nullable(UInt8)`
returned no rows, and `WHERE a IS NOT NULL` dropped the `a=NULL` directory
of a `Nullable(String)` column (ClickHouse#127133).

Use the readers' conversion in the filter. A value the column type cannot
hold now fails a filter on that column with the reader's TYPE_MISMATCH,
as reading the column already does.

Closes ClickHouse#127133

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; 0 findings) plus a separate cold review (1 finding).

# Sev Finding Verdict Evidence / action
1 💡 The description named the new TYPE_MISMATCH of a filter on an unreadable path value only for an out-of-range integer (k0=300, UInt8), not for the text NULL under Nullable(Int64) AGREE, fixed added to the description

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

Session id: cron:clickhouse-review-self-slot-58:20261113-102100

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. file('d/k0=300/data.jsonl', JSONEachRow, 'c0 Int32, k0 Nullable(UInt8)') with use_hive_partitioning = 1: no WHERE reads k0 = NULL, WHERE k0 IS NULL returns 0 rows, every run (master and 26.10.2.103).
b Root cause explained? The _path pre-filter (addPathAndFileToVirtualColumns) parsed the value with text deserialization, the readers with convertFieldToType. They disagree for out-of-range integers (wrap vs NULL), -1 for unsigned types (reject vs NULL) and the text NULL for Nullable(String) (SQL NULL vs the string), so the filter skipped files the query matches.
c Fix matches root cause? Yes. The filter calls the readers' own conversion (HivePartitioningUtils::convertPartitionValueToType); no guard or special case.
d Test intent preserved / new tests added? New test 05629_hive_partitioning_path_filter_value_conversion: out-of-range and -1 for Nullable(UInt8), LowCardinality(Nullable(UInt8)), #127133's a=NULL with Nullable(String), a guard that the filter still skips a non-matching file, the error for a value the column cannot hold, and s3. Existing Hive tests pass.
e Both directions demonstrated? The test fails on the exact base commit (#131202 head) and passes with the fix; 100/100 randomized runs pass.
f Fix is general across code paths? The three places that materialize a Hive value now share one conversion; every pre-filter caller (file, url, object storage, *Cluster, S3Queue/AzureQueue) goes through the fixed function.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked 8 types (Nullable(UInt8), UInt8, Nullable(String), String, Nullable(Int64), Date, Decimal(9, 2), Bool) x 8 values (300, -1, 5, NULL, empty, __HIVE_DEFAULT_PARTITION__, 1.234, true) on both binaries: after the fix the filter agrees with the reader on every readable value and gives the reader's error otherwise.
h Backward compatible? (maintainer-approved exception only) No setting or format change. The only queries that change from success to an error are filters on a path value the column cannot read at all (an unfiltered read of it already fails); stated in the PR description.
i Invariants and contracts preserved? The filter block keeps one row per path with equal column sizes and the _idx mapping; a conversion error propagates from the same loop as the previous parse errors did.

Session id: cron:clickhouse-impl-self-slot-57:20261113-093600

@clickhouse-gh

clickhouse-gh Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [172fdbe]

Summary: ✅


AI Review

Summary

Reviewed head 172fdbe97faa773038afb8d9b3e72f320286d3c2. The change makes Hive path filtering use the readers' conversion, fixing skipped matching files for out-of-range Nullable values and literal NULL strings, and reports TYPE_MISMATCH for values a non-nullable column cannot hold. No new actionable findings. All 15 local-file cases from the two new regression tests passed against this head's Fast test binary, including both reference-output comparisons and all six expected exceptions; S3 cases were not rerun locally.

The earlier stored-table format-settings discrepancy remains in the current code, but the author supplied the requested dedicated follow-up in PR #131571. That addresses the tracking alternative in the previous finding; the agreed deferral is accepted and the thread is left resolved. The deliberate conversion error during filtering is also accepted, and the storage-name consistency change is present in the current code.

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 labels Nov 13, 2026
@PedroTadim PedroTadim self-assigned this Nov 13, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-table-functions Table functions for temporary read/write to external systems (FROM ... tableFunction()). label Nov 13, 2026
Comment thread src/Storages/VirtualColumnUtils.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 172fdbe97 with master 7074838a9 (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, 19 s compile time in total, 7 of them have a recent master baseline.

Job report

@PedroTadim

Copy link
Copy Markdown
Member

🕵️ Review — WHERE on a Hive partition column

The core fix is correct: the path-filter now materializes partition values with the same convertPartitionValueToType the readers use (instead of deserializeWholeText), so a WHERE on a partition column is no longer evaluated against a mis-parsed value. 05629 covers it well. Two notes, both low severity:

1. Eager TYPE_MISMATCH at filter-build time — a design choice worth confirming (VirtualColumnUtils.cpp, the addPathAndFileToVirtualColumns conversion).

The filter column is now built by convertPartitionValueToType for every listed path, so a single out-of-range partition directory throws TYPE_MISMATCH before the predicate can prune it. Example: paths .../k0=300/... and .../k0=5/... with k0 UInt8, query … WHERE k0 = 5 — building the filter calls convertPartitionValueToType("300", UInt8) and throws, so the query fails even though only k0=5 was wanted and k0=300 would have been pruned. Before this PR the (mis-parsed) filter pruned the bad path and returned the row.

This is asserted as intended by 05628/05629, so it may be deliberate — but on object storage one stray non-representable partition directory now breaks all queries that filter that column to a fitting subset, not just the ones that read it. Worth a conscious decision between "fail the whole batch" (current) and "treat a non-representable value as not matching a predicate on a representable value, prune it, and defer the error to read time" (so only queries that actually read the bad partition fail). The latter also sits better with the repo's fail-close guidance.

2. column.name vs getNameInStorage() inconsistency.

The new filter call passes column.name, while the reader paths in this same PR use column.getNameInStorage(). It's benign today (Hive partition columns in the filter block carry only top-level names), but the two paths should use the same accessor so a future subcolumn-named partition column can't re-open a filter/reader divergence — and so the TYPE_MISMATCH message prints a consistent column name.

The path filter now takes the value of a Hive partition column from the
path by `getNameInStorage()`, the name the readers use, instead of the
filter block's column name, and prints the same name in the conversion
error. The filter block columns carry no storage name, so the filter
keeps the partition columns as `NameAndTypePair`s while building the
block. No output changes: every Hive partition column is a plain
`(name, type)` pair today, so both names are equal.

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

Copy link
Copy Markdown
Collaborator Author

Thanks for the review.

  1. I'm keeping the error at filter build. The filter cannot know whether a value the column cannot hold matches a predicate. Treating it as not matching (in effect NULL) prunes k0=300 for k0 = 5, but also for k0 != 5, NOT (k0 = 5) or k0 >= 0, which the real value does match: those queries would return fewer rows instead of failing, while k0 != 5 OR c0 > 0 (not pushed into the path filter) reads the file and fails. That is the filter/reader disagreement this PR removes. A predicate that does not read k0 (e.g. _path LIKE '%k0=5/%') still skips the bad path without an error.

    It is also not a new failure mode: on master (fdc4389) a value the type cannot parse already fails at filter build: k0={-1,5} or k0={abc,5}, k0 UInt8, WHERE k0 = 5 gives Code: 632. This PR only stops wrapping out-of-range numbers (on master 300 is compared as 44, so WHERE k0 = 44 reads the k0=300 file). Declaring the column Nullable gives the skip explicitly and in both places: with k0 Nullable(UInt8), WHERE k0 = 5 returns 5, and reading k0=300 gives NULL.

  2. Done in 172fdbe: the filter looks the value up by getNameInStorage() and prints that name in the error, like the readers. Its block columns carry no storage name, so it keeps the partition columns as NameAndTypePairs while building the block. No output change (every Hive partition column is a plain (name, type) pair today): the 24 Hive stateless tests I can run here pass (05628 and 05629 included), and a filter-vs-reader matrix of 8 types x 8 path values is identical to before.

@clickhouse-gh clickhouse-gh Bot left a comment

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.

AI review approves this commit, see the AI Review comment.

@PedroTadim
PedroTadim enabled auto-merge November 16, 2026 10:18
@PedroTadim
PedroTadim added this pull request to the merge queue Nov 16, 2026
Merged via the queue into ClickHouse:master with commit 7bd0720 Nov 16, 2026
192 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 Nov 16, 2026
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()). 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.

Hive pre-filter treats path value NULL as SQL NULL, reader keeps the string

3 participants