Skip to content

Fix handling of groups and named unions in CapnProto format - #133530

Merged
alexbakharew merged 7 commits into
masterfrom
fix-capnproto-group-fields
Nov 23, 2026
Merged

alexbakharew merged 7 commits into
masterfrom
fix-capnproto-group-fields

Conversation

@nikitamikhaylov

@nikitamikhaylov nikitamikhaylov commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

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 and incorrect results in the CapnProto format when the schema contains groups or named unions declared inline in a struct (g :group { ... }, u :union { ... }).

Details

A Cap'n Proto group (and a named union, which is a group containing a union) is not a separate object: its fields are stored inline in the data and pointer sections of the struct that contains it.

CapnProtoSerializer treated such a field as a pointer field of the parent struct:

  • the slot offset was read from the proto of a field that is a group and has no slot;
  • the struct, Nullable and Map serializers then accessed the group through getPointerField(offset) of the parent.

As a result, the values of a group were written to and read from the wrong place. If the parent struct had no pointers, the accessed slot was outside of the struct, which could corrupt the message being built and crash the server.

Now a group is accessed through the storage of its parent struct, both when writing and when reading. Schemas that use standalone structs (including the documented Nullable representation as a separate struct with a union) are not affected.

Note: the output for schemas with inline groups changes. The previous layout was not valid for other Cap'n Proto implementations, and the previous reader returned default values for group fields in correctly encoded messages.

The same code paths had three more problems, which are fixed here as well:

  • the value of a named union used as Nullable was read and written at the offset of its Void member when the Void member is declared first;
  • a Tuple column under a flattened name (like g.h Tuple(x UInt64, y UInt64)) accessed the serializers of the fields out of bounds;
  • arrays written to the same list through flattened names (like l.x and l.y) were not checked to have the same size, and a longer array was written out of bounds of the list. Now it throws SIZES_OF_ARRAYS_DONT_MATCH.

Default values of fields declared in the schema are still ignored, as before this change.

Related: #133604

Tests

05843_capnproto_group_fields covers groups in a struct without pointers, groups in a struct with pointers, nested groups, named unions as Nullable (including null-first unions after other fields), tuples under flattened names, groups inside nested structs and list elements, and schema inference.

05844_capnproto_flattened_list_sizes covers arrays of different sizes written to the same list, in a group and in a standalone struct.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.12.1.259-master (included in 26.12 and later)
  • Backported to: 26.11.1.2930, 26.10.12.4, 26.9.38.7, 26.8.45.7, 26.3.71.4

A Cap'n Proto group (`g :group { ... }`, and a named union
`u :union { ... }` as well) is stored inline in the struct that contains
it. The serializer treated such a field as a pointer field of the parent
struct, so the data was written to and read from the wrong place, and
for a parent struct without pointers the access was out of bounds of the
struct.

Access the group through the storage of its parent struct, both when
writing and when reading, and do not read the slot offset from the proto
of a field that is not a slot.

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

clickhouse-gh Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cd2705b]

Summary: ✅


AI Review

Summary

PR #133530, reviewed at cd2705ba7dc339e2fb4d5cafc902c0d25ae8be19, fixes inline group and named-union storage in CapnProto, payload offsets, flattened tuple traversal, and mismatched flattened-list lengths. It also publishes cached schemas with an atomic rename. No new actionable findings remain. Earlier safety findings are fixed; the pre-existing schema-default limitation is explicitly tracked in #133604.

Missing context / blind spots
  • ⚠️ No local build or executable was available. Both new regression tests passed repeatedly in ASan/UBSan targeted CI at 9155cf45, whose serializer and test files match the current head. The subsequent schema-cache rename change was reviewed from source; current-head CI is still running, so its build and runtime results remain to be confirmed.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Nov 22, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). label Nov 22, 2026
Comment thread src/Formats/CapnProtoSerializer.cpp Resolved
Comment thread src/Formats/CapnProtoSerializer.cpp Resolved
nikitamikhaylov and others added 3 commits November 22, 2026 15:41
…n `CapnProto`

Address review: `CapnProtoNullableSerializer` took the slot offset of the
value from the first member of the union, assuming both members have the
same offset. This holds for a standalone struct with a union, but not for
a named union declared inline in a struct: the `Void` member always has
offset 0, while the value can be in any slot of the enclosing struct. For
a union with the `Void` member first, the value was written over (and read
from) the slot 0 of the parent, corrupting a sibling field.

Now the offset is taken from the non-`Void` member.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…in `CapnProto`

Address review: for a column with a flattened name such as `g.h` and the
type `Tuple`, `CapnProtoStructureSerializer` for `g` is an adapter with a
single field serializer (for `h`), but `writeRowImpl` and `readRowImpl`
decided what to do by the type of the column: for `ColumnTuple` they
iterated over the tuple elements and accessed `fields_serializers` and
`fields_offsets` out of bounds.

Now the serializer remembers whether it maps a struct to a `Tuple`, and
otherwise passes the whole column to its single field. This affected both
groups and standalone structs.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Extend `05843_capnproto_group_fields` with:
- named unions with the `Void` member first, declared after other data and
  pointer fields, so the value is not in the slot 0; the written data is
  also read through a struct with the same layout and plain fields to check
  that the sibling fields and the discriminants are in the right places;
- a `Tuple` column under a flattened name (`g.h`), for a group and for a
  standalone struct.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Comment thread src/Formats/CapnProtoSerializer.cpp Resolved
Comment thread src/Formats/CapnProtoSerializer.cpp Resolved
@stresshouse

stresshouse Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 29 queries analysed

no significant changes detected. K_source=4 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: a177ba98-cf12-48f9-bc4b-67e304de3938
  • MIRAI run: dd61a645-7d2a-4c1f-a636-b1d3aafb16fc
  • PR check IDs:
    • clickbench_867483_1795386548
    • clickbench_867485_1795386548
    • tpch_adapted_1_official_867525_1795386548
    • tpch_adapted_1_official_867547_1795386548

@SmitaRKulkarni SmitaRKulkarni self-assigned this Nov 22, 2026
Columns with flattened names (like `l.x` and `l.y`) are written to the
same list and share its cached builder, which is created with the size
of the first array. An array of another size was then written through
that builder: a longer one accessed its element builders out of bounds,
a shorter one silently left the remaining elements unset.

Throw `SIZES_OF_ARRAYS_DONT_MATCH` in `CapnProtoArraySerializer` instead.
It applies to lists inside groups and inside standalone structs.

Co-Authored-By: Claude Fable 5.2 <[email protected]>

@SmitaRKulkarni SmitaRKulkarni left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@clickhouse-gh

clickhouse-gh Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing cd2705ba7 with master 0b5613b71 (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

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

Job report

@watson-ch

watson-ch Bot commented Nov 23, 2026

Copy link
Copy Markdown

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

Labels added:

  • v26.6-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.

@alexbakharew
alexbakharew added this pull request to the merge queue Nov 23, 2026
Merged via the queue into master with commit 3dac6f4 Nov 23, 2026
199 checks passed
@alexbakharew
alexbakharew deleted the fix-capnproto-group-fields branch November 23, 2026 12:36
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Nov 23, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 23, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 23, 2026
Backport #133530 to 26.11: Fix handling of groups and named unions in CapnProto format
clickhouse-gh Bot added a commit that referenced this pull request Nov 23, 2026
Backport #133530 to 26.10: Fix handling of groups and named unions in CapnProto format
clickhouse-gh Bot added a commit that referenced this pull request Nov 23, 2026
Backport #133530 to 26.8: Fix handling of groups and named unions in CapnProto format
SmitaRKulkarni added a commit that referenced this pull request Nov 23, 2026
Backport #133530 to 26.9: Fix handling of groups and named unions in CapnProto format
robot-ch-test-poll added a commit that referenced this pull request Nov 23, 2026
Cherry pick #133530 to 26.3: Fix handling of groups and named unions in CapnProto format
robot-clickhouse added a commit that referenced this pull request Nov 23, 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 23, 2026
PedroTadim added a commit that referenced this pull request Nov 24, 2026
Backport #133530 to 26.3: Fix handling of groups and named unions in CapnProto format
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Nov 24, 2026
…ache-drop-while-in-use

Conflict in `FormatSchemaInfo::storeSchemaOnDisk`: master (ClickHouse#133530) replaced the
`renameExchange`/`renameNoReplace` pair with a plain `fs::rename`. Kept master's
rename and this PR's `format_schema_cache_pause_before_publish` pause before it.

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

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 v26.6-must-backport v26.8-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants