Skip to content

Cast an ORC list, map or struct requested as another type instead of reading default values - #130171

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:orc-cast-requested-type-of-another-kind
Nov 8, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:orc-cast-requested-type-of-another-kind

Conversation

@groeneai

@groeneai groeneai commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #130020
Related: #130102
Related: #130116

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 reading an ORC list, map or struct column requested as a type of another kind, for example a Tuple over a map, or a data lake table whose schema disagrees with an ORC data file: the column was silently returned as default values, and a Map whose value had another kind crashed the server. Now the value is cast to the requested type, as the ORC format documentation describes, or the query fails with the cast error.

Description

On 26.10.2.103, SELECT * FROM file('m.orc', ORC, 'id Int64, payload Tuple(value Nullable(String))') over an ORC map column payload returns 2 (NULL), payload String returns 2 '', and m Map(String, String) over a map<string, struct<...>> column is a SIGSEGV (an abort on master debug builds).

updateIncludeTypeIds walks the requested type and the file type together to choose the ORC columns to read. For a list, map or struct node whose requested type is of another kind, it selected nothing. The column was then filled as a missing column (THERE_IS_NO_COLUMN with input_format_orc_allow_missing_columns = 0), so the cast to the requested type was never attempted; a map whose value was unselected was read with a single child and indexed out of bounds.

Now such a node is selected in full: it is read with its file type and castColumnToRequestedType converts it (Map -> String, Map -> Array(Tuple(...)), a map value) or throws the cast error naming the column and both types, like the Arrow reader. A map read without a Map request now gets a valid key type (a dictionary-encoded string key was LowCardinality(Nullable(String)), which Map rejects). Same-kind requests, named-tuple field pruning and genuinely missing columns are unchanged.

Not covered: a map value Tuple that shares no element with the file's struct still reads out of bounds (same kind, different trigger and fix site).

Validation: the new test fails on master (defaults, two aborts) and passes with the fix, 50/50 with random settings; the 133 stateless tests that read ORC show no new failures.

🤖 Generated with Claude Code


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

Version info

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

groeneai and others added 2 commits November 8, 2026 14:16
…reading default values

`updateIncludeTypeIds` chooses the ORC columns to read by walking the
requested type and the file type together. For a `list`, `map` or
`struct` node whose requested type is of another kind it selected
nothing, so the column was filled as a missing one (default values, or
`THERE_IS_NO_COLUMN` with `input_format_orc_allow_missing_columns = 0`)
and the cast to the requested type documented in ORC.mdx never ran. A
map whose value was left unselected was read with a single child, and
`readColumnFromORCColumn` indexed `getSubtype(1)` out of bounds
(`std::out_of_range`, an abort in debug builds, a SIGSEGV on the
releases).

Such a node is now selected in full. `readColumnFromORCColumn` already
reads a node whose hint is of another kind with its file type, and
`castColumnToRequestedType` then converts it or throws the cast error,
as the Arrow reader does. Same-kind requests, named-tuple field pruning,
unions and genuinely missing columns are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A map selected because its requested type is of another kind is read
without a `Map` hint, so a dictionary-encoded string key arrives as
`LowCardinality(Nullable(String))`, which `DataTypeMap` rejects
(`BAD_ARGUMENTS`). Cast such a key to the same type without
nullability, the key type schema inference gives the file, before the
cast to the requested type runs.

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

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (2 rounds): 1 fixed, 4 noted
  • ❌ AGREE, fixed: a map read without a Map request (a kind mismatch, or under one) got a LowCardinality(Nullable(String)) key for a dictionary-encoded string key, which Map rejects (BAD_ARGUMENTS), so the cast never ran. The key is now cast to its non-Nullable type, as the hinted key already was; dictionary-encoded arms added to the test. Round 2 re-review of the fix: no findings.
  • 💡 DISAGREE: Critical Bug Fix category. That category is left to maintainers; Bug Fix holds (26.10.2.103 returns defaults for data).
  • 💡 Noted: an Iceberg ORC file read under a newer schema matches columns by name (pre-existing for primitive kinds; field-id mapping is a separate change).
  • 💡 Noted: until Fix Iceberg v3 variant columns in ORC data files being read as NULL #130116 merges, an Iceberg variant struct requested as Dynamic/JSON reads as the raw struct instead of NULL.
  • 💡 Noted: a map value Tuple sharing no element with the file's struct is a different trigger with a different fix (listed under "Not covered").

@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. SELECT * FROM file('m.orc', ORC, 'id Int64, payload Tuple(value Nullable(String))') over a ClickHouse-written ORC map returns 2 (NULL) on every run on master 7b5f2bf328f9f; m Map(String, String) over a map<string, struct<...>> column aborts (signal 6) on every run of the master debug build.
b Root cause explained? updateIncludeTypeIds matched the requested type against the ORC list/map/struct node and selected nothing when the kinds differed. The column was then filled as a missing column, so castColumnToRequestedType never ran; a map value left unselected made the map arrive with one child, and getSubtype(1) threw std::out_of_range (SIGSEGV on the apache/orc releases).
c Fix matches root cause? Yes. The node is now selected in full, read with its file type and converted by the existing cast, as ORC.mdx documents and the Arrow reader does. No guard at the crash site, no new error path.
d Test intent preserved / new tests added? New 05574_orc_requested_type_of_another_kind: 9 conversions the cast rejects (incl. allow_missing_columns = 0), 5 it performs (incl. a map value and ms.values, both aborted on master), 4 same-kind controls; 4 of those arms read a dictionary-encoded map key.
e Both directions demonstrated? Yes. With the master binary the test prints defaults and aborts twice; with the fix it matches the reference. 50/50 with random settings.
f Fix is general across code paths? updateIncludeTypeIds is the only place the requested and the file type are matched; UNION already selects an unhinted branch in full. Arrow already casts; Parquet rejects kind mismatches by design (#130102 for #130020). Not covered: a map value Tuple sharing no element with the file's struct (same kind, different fix site).
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked: top level, list element, struct field, map value; Nullable, non-Nullable, LowCardinality(Nullable), Dynamic, JSON; NULL structs; dictionary-encoded string map keys (input_format_orc_dictionary_as_low_cardinality = 1); Nested (flattened, unflattened, dotted); empty struct; case-insensitive matching; allow_missing_columns 0/1; filter push down on.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format change. Only requests that returned nothing but defaults change, to the cast result or the cast error.
i Invariants and contracts preserved? Every requested ORC node is selected (parents follow via selectChildren); same-kind named-tuple pruning, the primitive shortcut and the UNION/default paths are unchanged. A map read without a Map hint casts a Nullable key to the non-Nullable type Map requires (a real NULL key throws). 131 of 133 ORC stateless tests pass locally; the other 2 need CI-only fixtures and fail identically on master.

Session id: cron:clickhouse-impl-self-slot-57:20261108-144100

@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [aaf1887]

Summary: ✅


AI Review

Summary

This PR reads ORC list, map, and struct nodes requested as another type in full, allowing the existing cast to convert them or report an error instead of silently returning defaults. It also removes nullable wrappers from unhinted map keys before constructing DataTypeMap. Reviewed head aaf1887d9b112f82f34fa26a438a6aee682f8007, the full modified reader, regression tests, and all prior discussion. No actionable findings. Consumer tracing covered recursive Array/Map/Tuple construction and nullable wrapping, which retain column/type pairs; union branch conversion, which repairs branch types before assembly; NestedColumnExtractHelper and Nested::unwrapNullableTuple, which handle extraction and parent nulls; and orcColumnsToCHChunk, which applies null defaults and castColumnToRequestedType before returning columns.

Missing context / blind spots
  • ⚠️ No ClickHouse binary or configured build is available locally, so I could not independently rerun 05574_orc_requested_type_of_another_kind. The author reports failure on master and successful repeated runs with the patch. The available CI finish report reports dropped bugfix-validation jobs. A successful ORC-enabled regression run and bugfix validation would close this execution gap.
Final Verdict

Status: ✅ Approve. No code changes requested or new inline findings. CI validation remains outstanding.

@groeneai

groeneai commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov @vitlibar, could you review this and add the can be tested label so CI runs? The native ORC reader selected no columns for a list, map or struct requested as a type of another kind, so it filled the column with defaults (or read a map with one child out of bounds); now it reads the node with its file type and castColumnToRequestedType converts it or throws, like the Arrow reader.

@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-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). labels Nov 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing aaf1887d9 with master 4f3ed3395 (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, 14 s compile time in total, 7 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov self-assigned this Nov 8, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 8, 2026
Merged via the queue into ClickHouse:master with commit 2aef13f Nov 8, 2026
373 of 375 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 Nov 8, 2026
@Algunenano Algunenano added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Nov 9, 2026
@Algunenano Algunenano self-assigned this Nov 9, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 9, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 9, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 9, 2026
Backport #130171 to 26.9: Cast an ORC list, map or struct requested as another type instead of reading default values
clickhouse-gh Bot added a commit that referenced this pull request Nov 9, 2026
Backport #130171 to 26.10: Cast an ORC list, map or struct requested as another type instead of reading default values
Algunenano added a commit that referenced this pull request Nov 9, 2026
Backport #130171 to 26.8: Cast an ORC list, map or struct requested as another type instead of reading default values
Algunenano added a commit that referenced this pull request Nov 9, 2026
Backport #130171 to 26.3: Cast an ORC list, map or struct requested as another type instead of reading default values
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…s another type instead of reading default values
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…s another type instead of reading default values
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…s another type instead of reading default values
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…as another type instead of reading default values
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 22, 2026
…ceberg-variant

Conflicts in `src/CMakeLists.txt` and `NativeORCBlockInputFormat.{cpp,h}`:
- Master builds `VariantEncoding.cpp` without Parquet for Avro too, so the
  condition is now `elseif (USE_AVRO OR USE_ORC)`.
- `ORCColumnToCHColumn` gained the data lake arguments on master; it now
  takes `FormatSettings` in place of the five settings it took from it,
  keeping the new arguments.
- `decodeVariantColumn` takes `Parquet::VariantSettings` on master.
- The `updateIncludeTypeIds` hunk that selected a struct whole for a
  `Dynamic` or `JSON` request is dropped: since ClickHouse#130171 every list, map or
  struct requested as another kind is selected whole.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 22, 2026
… throwing

Since ClickHouse#130171 an ORC struct requested as another kind is read whole and
cast to the requested type, so `file('t.orc', ORC, 'v JSON')` over
`struct<a:int,b:string>` returns `{"a":1,"b":"x"}` on master, as
Parquet does. The variant reader threw `TYPE_MISMATCH` for every struct
that is not exactly two binary fields `metadata` and `value`, which
after the master merge would reject such input.

Only a variant-shaped struct is decoded now; any other struct takes the
cast path. The test checks the cast for a plain struct, a struct with
other field names and a struct of `string` fields.

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 comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). 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 Pull request should be backported intentionally. Use this label with great care! 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants