Repository navigation
Fix handling of groups and named unions in CapnProto format - #133530
Conversation
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]>
|
Workflow [PR], commit [cd2705b] Summary: ✅
AI ReviewSummaryPR #133530, reviewed at Missing context / blind spots
Final VerdictStatus: ✅ Approve |
Co-Authored-By: Claude Fable 5.2 <[email protected]>
…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]>
|
📊 Cloud Performance Report ✅ AI verdict: 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
|
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]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 11 s compile time in total, 8 of them have a recent master baseline. |
|
Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/2266 Labels added:
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. |
Backport #133530 to 26.11: Fix handling of groups and named unions in CapnProto format
Backport #133530 to 26.10: Fix handling of groups and named unions in CapnProto format
Backport #133530 to 26.8: Fix handling of groups and named unions in CapnProto format
Backport #133530 to 26.9: Fix handling of groups and named unions in CapnProto format
Cherry pick #133530 to 26.3: Fix handling of groups and named unions in CapnProto format
Backport #133530 to 26.3: Fix handling of groups and named unions in CapnProto format
…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]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a crash and incorrect results in the
CapnProtoformat 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.
CapnProtoSerializertreated such a field as a pointer field of the parent struct:NullableandMapserializers then accessed the group throughgetPointerField(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
Nullablerepresentation 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:
Nullablewas read and written at the offset of itsVoidmember when theVoidmember is declared first;Tuplecolumn under a flattened name (likeg.h Tuple(x UInt64, y UInt64)) accessed the serializers of the fields out of bounds;l.xandl.y) were not checked to have the same size, and a longer array was written out of bounds of the list. Now it throwsSIZES_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_fieldscovers groups in a struct without pointers, groups in a struct with pointers, nested groups, named unions asNullable(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_sizescovers 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
26.12.1.259-master(included in26.12and later)26.11.1.2930,26.10.12.4,26.9.38.7,26.8.45.7,26.3.71.4