Skip to content

Declare raw-byte leaves of Arrow IPC columns with their decoded type - #132416

Merged
yakov-olkhovskiy merged 4 commits into
masterfrom
arrow-ipc-nested-hint-type-consistency
Nov 19, 2026
Merged

yakov-olkhovskiy merged 4 commits into
masterfrom
arrow-ipc-nested-hint-type-consistency

Conversation

@KochetovNicolai

@KochetovNicolai KochetovNicolai commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

The native Arrow IPC reader (Arrow, ArrowStream) can decode a binary child of a struct as IPv6 or a big integer when a dotted header column requests it, for example "t.s" IPv6. The parent t can be requested too, with a type that does not describe s, for example String. In that case the decoded t still declared the child as String, because the declared type comes from the Arrow schema. The raw-byte rewrite after decoding follows only the target type of the parent, so it did not fix this child. The cast of t then read the IPv6 column as a ColumnString, and the server crashed. The same happens for a struct inside a list (a String, "a.s" Array(IPv6)). It also happens for a dictionary-encoded fixed_size_binary(16) child requested as UUID (t String, "t.u" UUID), whose dictionary values are converted before any record batch is decoded.

The fix extends matchColumnNullability, which already compares the declared type with the decoded column at each level, to leaf columns. A String or FixedString leaf that was decoded as IPv6, UUID, Int128, UInt128, Int256 or UInt256 is now declared with that type. So every column of the chunk that uses the decoded field sees a type that matches its column. A String or FixedString leaf that holds a column of any other type throws a LOGICAL_ERROR, so a conversion this walk does not know about becomes an exception instead of undefined behavior.

An Arrow union crashed the same way without any dotted name. The children of a union restart the dotted path, so the struct child s of a union child has the path s, and the lookup of requested types found an unrelated header column s, for example u String, s IPv6. Its type changed how the child decoded: raw bytes were read as IPv6, or the null map of a struct was kept for a Tuple. Meanwhile decodeUnion declares its children from the Arrow schema. Requested types are no longer looked up below a union, neither while decoding its children nor for the dictionaries of fields below it. The dictionary positions of such fields are kept apart from equally named positions outside the union.

The values of a dictionary are decoded before any record batch, once for each field that encodes the dictionary, as that field would decode them inline. Fields sharing a dictionary id were told apart only by their position, meaning the dotted name and the list depth. But two fields at one position can resolve different requested types: a field literally named t.u takes the type requested for t.u, while the child u of a struct t takes the type of u in a requested t Tuple(...). Both read the decoding of whichever came first in the schema. For example, with t Tuple(u UUID), `t.u` String, the field named t.u returned the text of a UUID instead of its 16 bytes. Each decoding is now identified by the position together with the requested type.

Tests: 05816_arrow_ipc_dotted_raw_byte_hint_parent_type, 05828_arrow_ipc_dictionary_raw_byte_hint_parent_type, 05829_arrow_ipc_union_unrelated_hint, 05830_arrow_ipc_union_dictionary_unrelated_hint, and 05831_arrow_ipc_shared_dictionary_dotted_name. The last two need pyarrow, hence no-fasttest.

Related: #132550
Related: #120702

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix a crash in the Arrow and ArrowStream input formats when a requested type changed how a nested field was decoded while another requested column read that field with a type that does not describe it: a struct column together with a dotted subcolumn of it, such as t String, "t.s" IPv6 (including dictionary-encoded UUID children), or an Arrow union with a struct child named like another requested column.


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

Version info

  • Merged into: 26.11.1.2801-master (included in 26.11 and later)
  • Backported to: 26.10.8.33, 26.9.34.15, 26.8.42.13

A dotted header column such as `t.s IPv6` makes the native Arrow IPC
reader decode the binary child `s` of the struct `t` as `IPv6`. When the
parent `t` is requested too, with a type that does not describe `s` (for
example `String`), the decoded `t` still declared the child as `String`,
because its declared type comes from the Arrow schema. The raw-byte rewrite
after decoding follows the parent target type only, so it did not reconcile
the child, and the cast of `t` read the `IPv6` column as a `ColumnString`.

`matchColumnNullability` already walks the declared type alongside the
decoded column. Extend it to leaves: a `String` or `FixedString` leaf that
was decoded as `IPv6` or a big integer is declared with that type.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@KochetovNicolai KochetovNicolai added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Nov 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6cc3253]

Summary: ✅


AI Review

Summary

The PR keeps decoded Arrow and ArrowStream raw-byte leaves consistent with their declared types, prevents unrelated header hints from reaching union children, and distinguishes shared dictionary decodings by their resolved hints. The three prior findings are addressed, and no new actionable findings remain. Consumer tracing covered dictionary registration and materialization, nested extraction, raw-byte rewriting, and final casts; these paths receive consistent column/type pairs. The three Fast test regressions pass; the discussion supplies local validation for the two pyarrow regressions.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Nov 17, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6cc3253ff with master f3e79c38e (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.

Object file sizes

4 object files changed (+65.98 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Processors/Formats/Impl/ArrowIPC/RecordBatchDecoder.cpp.o 709.48 KiB 775.48 KiB +65.99 KiB (+9.30%)

711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

8 translation units recompiled, 25 s compile time in total, 8 of them have a recent master baseline.

Job report

cursoragent and others added 2 commits November 18, 2026 00:55
…ded type

A dictionary-encoded `fixed_size_binary(16)` child is converted to `UUID` when a dotted header
column such as `t.u UUID` requests it: `decodeDictionaryBatch` rewrites the dictionary values with
`reinterpretRawByteColumns`. `rawByteLeafType` listed only the types that `reinterpretStringLeaf`
produces, so `matchColumnNullability` kept declaring the leaf as `FixedString(16)`. When the parent
`t` was requested too, with a type that does not describe `u` (for example `String`), the cast of `t`
read the `UUID` column as a `FixedString` one: a failed `assert_cast` in debug builds, undefined
behavior in release builds.

Declare such a leaf as `UUID`, and throw a `LOGICAL_ERROR` for a `String` or `FixedString` leaf that
holds a column of any other type, so a conversion this walk does not know becomes an exception
instead of undefined behavior.

Review: #132416 (comment)
Related: #132416
Co-authored-by: Yakov Olkhovskiy <[email protected]>
The children of an Arrow union restart the dotted path, so the struct child `s` of a union child
has the path `s`, and the lookup in the requested types found the unrelated header column `s`. Its
type changed how the child decodes (raw bytes read as `IPv6`, or the null map of a struct kept for a
`Tuple`), while `decodeUnion` declares its children from the Arrow schema, so the cast of the union
read the column as another type: a failed `assert_cast` in debug builds, a segmentation fault in
release builds. A dictionary of a field below a union got the same hint, from
`collectDictionaryUses` and again in `decodeDictionaryValues`.

Look up no requested type while decoding the children of a union, and the values of a dictionary
used below one. Keep these dictionary positions apart from equally named positions outside the
union (`FieldPosition::below_union`), so a dictionary that a field outside the union shares does not
hand the union the decoding requested for that field.

Review: #132416 (comment)
Related: #132416
Co-authored-by: Yakov Olkhovskiy <[email protected]>
@cla-assistant

cla-assistant Bot commented Nov 18, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ KochetovNicolai
❌ cursoragent
You have signed the CLA already but the status is still pending? Let us recheck it.

…d type

The values of an Arrow dictionary are decoded once for each use: the position of a field encoding
it, and the requested type hint the field resolves there. The uses were deduplicated and the
decodings registered by position alone, but two fields at one position can resolve different
hints: a field literally named `t.u` takes the type requested for `t.u`, while the child `u` of a
struct `t` takes the type of `u` in a requested `t Tuple(...)`. When such fields shared a dictionary
id, both read the decoding of whichever came first in the schema. For example, with
``t Tuple(u UUID), `t.u` String`` the field named `t.u` returned the 36-byte text of a `UUID`
instead of its 16 bytes, and requesting it as `IPv6` threw.

Key the uses and the registry by the position together with the hint, and look the decoding up with
the use that `decodeField` and `decodeBatchColumn` resolve for the field. A use missing from the
registry is now a `LOGICAL_ERROR` instead of a `std::out_of_range`.

Review: #132416 (comment)
Co-authored-by: Yakov Olkhovskiy <[email protected]>
@yakov-olkhovskiy
yakov-olkhovskiy added this pull request to the merge queue Nov 18, 2026
Merged via the queue into master with commit d962a9f Nov 19, 2026
378 of 380 checks passed
@yakov-olkhovskiy
yakov-olkhovskiy deleted the arrow-ipc-nested-hint-type-consistency branch November 19, 2026 00:12
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 19, 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 19, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 19, 2026
The branch is in conflict with master. Conflicts in the ArrowIPC reader only: master (ClickHouse#132416) keys the
dictionary uses and the registry by position and requested hint (`DictionaryUse::operator==`,
`DictionaryRegistry::useKey`, `dictionaryUse`), as this branch did with `DictionaryUse::key`; master's code
is kept and the branch's copy dropped. Master's `below_union` position (ClickHouse#132550) is kept with this
branch's exact-name hint lookup (`resolveHint` with case-insensitive matching), the per-struct
`structFieldHints` and the top-level field name as the starting path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov added a commit that referenced this pull request Nov 19, 2026
…bcolumn-hint-with-parent

Resolve conflicts with #132416, which keys
the decodings of a shared dictionary by position and requested type (`DictionaryUse::operator==`,
`DictionaryRegistry::useKey`), skips requested types below an Arrow union, and re-declares
raw-byte leaves in `matchColumnNullability` (`rawByteLeafType`).

- `DictionaryUse` keeps both `keep_raw_bytes` and `operator==`; `keep_raw_bytes` is derived from
  the position and the requested types, so it takes part in the equality but not in `useKey`.
- `collectDictionaryUses` takes `below_union` and `RequestedTypes`; below a union no requested
  types are consulted for `keep_raw_bytes` either, and neither for `rawByteTarget` and
  `wrapsInNullable` while decoding below a union.
- `matchColumnNullability` uses master's `rawByteLeafType` with its `LOGICAL_ERROR` check, and keeps
  this PR's re-declaration of converted dictionary (`LowCardinality`) leaves.
- `positionKey` is dropped in favor of master's `useKey`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov added a commit that referenced this pull request Nov 19, 2026
…several requests

`05816_arrow_ipc_dotted_raw_byte_hint_parent_type` and
`05828_arrow_ipc_dictionary_raw_byte_hint_parent_type` came from
#132416 and pinned that a parent requested as
`String` together with a dotted subcolumn such as `t.s IPv6` reads the child as converted for
the subcolumn. This PR intentionally keeps the bytes of a field that is decoded for several
requested columns and converts each column by its own type after decoding, so the parent
`t String` now reads the raw bytes of `s` / `u`, exactly as when `t` is requested alone. The
subcolumns are unchanged. Update the references and the test comments.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
cursor Bot pushed a commit that referenced this pull request Nov 19, 2026
Resolve the conflicts of the cherry-pick of #132416 to 26.9. The fix applies in full; the differences
from the original diff come only from master-only neighbours that 26.9 does not have:

- `ArrowIPCBlockInputFormat.cpp`: 26.9 still checks that dictionary values are unique after registering
  them (master dropped the check in #125677). Keep the check and look the values up by the dictionary use,
  as the registry is now keyed by it.
- `RecordBatchDecoder.cpp`: 26.9 has no `TypeKind::RunEndEncoded` case in `collectDictionaryUses`
  (run-end encoding, #126207, is master-only), so there is no call to pass `below_union` to there.
  The struct case keeps the 26.9 helpers `tupleElementHint` / `childPath` (master renamed them in
  #125674) and passes `below_union` as the original change does.
- `RecordBatchDecoder.cpp`: include `base/scope_guard.h` for the two new `SCOPE_EXIT` uses. On master
  the include came with the big-endian reader (#126562); 26.9 does not include it, directly or
  transitively.
- `RecordBatchDecoder.h`: no `[[nodiscard]]` on the changed declarations, as on the rest of the 26.9
  file (master added them in #126158).

Co-authored-by: yakov <[email protected]>
cursor Bot pushed a commit that referenced this pull request Nov 19, 2026
Resolve the conflicts of the cherry-pick of #132416 to 26.10. The fix applies in full; the differences
from the original diff come only from master-only neighbours that 26.10 does not have:

- `RecordBatchDecoder.cpp`: 26.10 has no `TypeKind::RunEndEncoded` case in `collectDictionaryUses`
  (run-end encoding, #126207, is master-only), so there is no call to pass `below_union` to there.
  The struct case keeps the 26.10 helpers `tupleElementHint` / `childPath` (master renamed them in
  #125674) and passes `below_union` as the original change does.
- `RecordBatchDecoder.cpp`: include `base/scope_guard.h` for the two new `SCOPE_EXIT` uses. On master
  the include came with the big-endian reader (#126562); 26.10 does not include it, directly or
  transitively.
- `RecordBatchDecoder.h`: no `[[nodiscard]]` on the changed declarations, as on the rest of the 26.10
  file (master added them in #126158).

Co-authored-by: yakov <[email protected]>
robot-ch-test-poll2 added a commit that referenced this pull request Nov 19, 2026
Cherry pick #132416 to 26.9: Declare raw-byte leaves of Arrow IPC columns with their decoded type
robot-clickhouse added a commit that referenced this pull request Nov 19, 2026
robot-ch-test-poll2 added a commit that referenced this pull request Nov 19, 2026
Cherry pick #132416 to 26.10: Declare raw-byte leaves of Arrow IPC columns with their decoded type
robot-clickhouse added a commit that referenced this pull request Nov 19, 2026
cursor Bot pushed a commit that referenced this pull request Nov 19, 2026
Backport only the part of #132416 that applies to 26.8: requested types are no longer looked up below an
Arrow union.

26.8 has the same lookup: the children of a union restart the dotted path, so a struct field `s` inside a
union child resolves the requested type of an unrelated header column `s`. On 26.8 a `Nullable` requested
type keeps the null map of a struct, while `decodeUnion` declares its children from the schema, so the
cast of the union read a `ColumnNullable` as a `ColumnTuple`: a `Bad cast` logical error in debug builds,
and garbage values in release builds, also on `INSERT ... FORMAT ArrowStream`. The fix sets
`decoding_below_union` in `decodeUnion` and skips the lookup in `resolveTargetHint` while it is set, as on
master. It also includes `base/scope_guard.h` for `SCOPE_EXIT`, which 26.8 does not include.

The rest of #132416 does not apply to 26.8: it changes the decode-time raw-byte conversion and the
decoding of dictionary values once per field position and requested type, which came with #110531 and
are not on 26.8 (26.8 decodes each dictionary once, without requested types). Its tests are adapted:
- `05829_arrow_ipc_union_unrelated_hint` gets a case with a `Nullable` unrelated column, the shape that
  reaches 26.8. Its other cases pass on 26.8 with or without the fix.
- `05830_arrow_ipc_union_dictionary_unrelated_hint` is kept; it passes on 26.8.
- `05816`, `05828` and `05831` are dropped: they cover the parts that are not on 26.8, and they fail on
  26.8 with or without the fix, because 26.8 does not read those raw-byte subcolumns and converts a
  dictionary-encoded `fixed_size_binary(16)` to `UUID` with a cast.

Co-authored-by: yakov <[email protected]>
robot-ch-test-poll added a commit that referenced this pull request Nov 19, 2026
Cherry pick #132416 to 26.8: Declare raw-byte leaves of Arrow IPC columns with their decoded type
robot-clickhouse added a commit that referenced this pull request Nov 19, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 19, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 19, 2026
Backport #132416 to 26.10: Declare raw-byte leaves of Arrow IPC columns with their decoded type
clickhouse-gh Bot added a commit that referenced this pull request Nov 19, 2026
Backport #132416 to 26.9: Declare raw-byte leaves of Arrow IPC columns with their decoded type
clickhouse-gh Bot added a commit that referenced this pull request Nov 20, 2026
Backport #132416 to 26.8: Declare raw-byte leaves of Arrow IPC columns with their decoded type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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-critical-bugfix 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.

5 participants