Repository navigation
Fix shitted string offsets of sparse Nullable(String) columns in the native protocol - #126937
Conversation
…rotocol Since ClickHouse#110320 the native protocol (revision 54492+) sends a String column as an offsets stream plus the characters. For a range starting at offset > 0 the network branch of SerializationString::serializeBinaryBulkWithSizeStream wrote the column's absolute offsets but the characters from offsets[offset - 1], while the reader and the protocol spec count the offsets from the first written row. SerializationSparse always writes the values column from index 1, and in Sparse(Nullable(String)) the NULL default row at index 0 can keep a non-empty nested string (toString(nullIf(i, 0)) keeps '0' under the NULL). So every offset was too large by that length: the receiver split the characters at wrong positions and read into the next stream, returning wrong strings, sometimes followed by UNKNOWN_PACKET_FROM_SERVER, in clickhouse-client and in remote/cluster/Distributed reads. Rebase the offsets by offsets[offset - 1] in the writer. A zero base keeps the existing bulk write, so all other traffic is byte-identical. The fixed stream is the documented layout, which every 54492+ reader already decodes. The test pins optimize_read_in_order = 1: with a full sort the column is materialized before it is sent and the bug does not show. It also pins serialization_info_version = 'with_types', without which a Nullable column is never stored sparse. Closes ClickHouse#126854 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the inaccurate '(like Array offsets)' remark (Array network offsets are absolute) and fold the network-offsets comment into one line. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding; bounded: 1 full pass + 1 fix round + 1 delta recheck).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20261020-112700 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20261020-110300 |
|
cc @Avogar, could you review this and add the |
|
Workflow [PR], commit [588830b] Summary: ✅
AI ReviewSummaryThe PR rebases cumulative string offsets when serializing a range over the native protocol, excluding bytes belonging to omitted rows. This fixes incorrect results and Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested. |
Nullable(String) columns in the native protocolNullable(String) columns in the native protocol
a4ab377
Backport #126937 to 26.10: Fix shitted string offsets of sparse `Nullable(String)` columns in the native protocol
Backport #126937 to 26.8: Fix shitted string offsets of sparse `Nullable(String)` columns in the native protocol
Backport #126937 to 26.9: Fix shitted string offsets of sparse `Nullable(String)` columns in the native protocol
…rse `Nullable(String)` columns in the native protocol
Closes: #126854
Related: #110320
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong strings, sometimes followed by an
UNKNOWN_PACKET_FROM_SERVERerror, when a sparseNullable(String)column is sent over the native protocol toclickhouse-clientor between servers byremote,clusterandDistributed, for exampletoString(nullIf(x, 0))over a sparse column, ortoStringof aNullablecolumn stored withnullable_serialization_version = 'allow_sparse'. Closes #126854.Description
On 26.8 and 26.9,
SELECT k, toString(nullIf(i, 0)) FROM t ORDER BY kover a sparse columnireturns wrong strings throughclickhouse-client(18133instead of1813) and then fails withUNKNOWN_PACKET_FROM_SERVER; withnullable_serialization_version = 'allow_sparse'the values are wrong with no error. HTTP output is correct.Root cause: since #110320, the native protocol (revision 54492+) sends a
Stringcolumn as an offsets stream plus the characters. For rows starting atoffset > 0,SerializationString::serializeBinaryBulkWithSizeStreamwrote the column's absolute offsets but the characters fromoffsets[offset - 1], while the reader and the protocol spec count the offsets from the first written row.SerializationSparsewrites the values from index 1, and inSparse(Nullable(String))the NULL default row at index 0 can keep a non-empty nested string ('0'above), so every offset is too large by its length and the receiver splits the characters wrongly and reads into the next stream.Change: the writer subtracts
offsets[offset - 1]from the offsets it writes. When that is 0 (non-sparse columns,Sparse(String), ranges starting at row 0) the existing bulk write is unchanged.Arraysends its offsets the same way but is not affected: it cannot be sparse, so it is always written from row 0.Compatibility: the fixed stream is the documented layout, which all 54492+ readers already decode; checked with 26.8.18.2 and 26.9.19.9 clients against a fixed server. The defect is on the sending side, so an unfixed 26.8/26.9 server keeps sending shifted offsets.
Validation: the new test fails on master and passes with the fix (50/50 randomized runs). It pins
optimize_read_in_order = 1, because with a full sort the column is no longer sparse when it is sent and the bug does not show.Workflow [PR]
Sync PR [sync-upstream/pr/126937]
Version info
26.11.1.490-master(included in26.11and later)26.10.2.72,26.9.25.11,26.8.34.9