Skip to content

Fix shitted string offsets of sparse Nullable(String) columns in the native protocol - #126937

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-native-sparse-nullable-string-offsets-126854
Oct 22, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-native-sparse-nullable-string-offsets-126854

Conversation

@groeneai

@groeneai groeneai commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #126854
Related: #110320

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 wrong strings, sometimes followed by an UNKNOWN_PACKET_FROM_SERVER error, when a sparse Nullable(String) column is sent over the native protocol to clickhouse-client or between servers by remote, cluster and Distributed, for example toString(nullIf(x, 0)) over a sparse column, or toString of a Nullable column stored with nullable_serialization_version = 'allow_sparse'. Closes #126854.

Description

On 26.8 and 26.9, SELECT k, toString(nullIf(i, 0)) FROM t ORDER BY k over a sparse column i returns wrong strings through clickhouse-client (18133 instead of 1813) and then fails with UNKNOWN_PACKET_FROM_SERVER; with nullable_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 String column as an offsets stream plus the characters. For rows starting at offset > 0, 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 writes the values from index 1, and in Sparse(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. Array sends 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

  • Merged into: 26.11.1.490-master (included in 26.11 and later)
  • Backported to: 26.10.2.72, 26.9.25.11, 26.8.34.9

groeneai and others added 2 commits October 20, 2026 11:24
…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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
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).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test checks that the stored columns are sparse, not that the computed column is still sparse when it is sent, so it could pass with the fix reverted (05427_native_sparse_nullable_string_offsets.sql) DISAGREE With the fix reverted the test failed 20 of 20 randomized runs (UNKNOWN_PACKET_FROM_SERVER at the first client query; max_threads 1, 2, 3 and 32 all drawn). The only setting that masked it, optimize_read_in_order = 0, is clamped by the Random settings limits line, checked in system.query_log. Bugfix validation also requires the test to fail on master.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-10:20261020-112700

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. On an unfixed master server, clickhouse client -q "SELECT k, toString(nullIf(i, 0)) FROM e WHERE k IN (0, 49, 99) OR k < 2 ORDER BY k" prints 18133/663 and fails with UNKNOWN_PACKET_FROM_SERVER every time; the allow_sparse case prints -0n/ann/an\x06 with exit 0. Also reproduced on official 26.9.19.9 and 26.8.28.14.
b Root cause explained? The network branch of SerializationString::serializeBinaryBulkWithSizeStream wrote absolute offsets for a range starting at offset > 0 but the characters from offsets[offset - 1]; the reader counts offsets from the range start. SerializationSparse writes values from index 1, and the NULL default row of Sparse(Nullable(String)) can keep a non-empty nested string, so all offsets are shifted by its length.
c Fix matches root cause? Yes. The writer rebases the offsets by offsets[offset - 1]; the zero-base path keeps the old bulk write. No change in the reader, SerializationSparse or NativeWriter.
d Test intent preserved / new tests added? New SQL test 05427_native_sparse_nullable_string_offsets (computed sparse column, stored allow_sparse column, INSERT SELECT from remote()), with premise checks that both columns are sparse. No existing test changed.
e Both directions demonstrated? Yes. Unfixed server: 20/20 randomized runs FAIL. Fixed server: 50/50 randomized runs pass. All manual arms match HTTP output byte for byte with the fix.
f Fix is general across code paths? Yes. One writer serves every native-protocol consumer (client results, remote/cluster/Distributed reads and inserts, temporary data, query cache). The on-disk branch already writes range-relative sizes. Array offsets use the same convention but never get a non-zero base: every NativeWriter::writeData caller starts at row 0 and Array cannot be sparse.
g Fix generalizes across inputs (params/datatypes/wrappers)? Only Sparse(Nullable(String)) has an offsets stream with a possibly non-empty default; the test covers a computed and a stored column, both compact and wide parts are sparse. Sparse(String), non-sparse columns and limit == 0 keep the old bytes.
h Backward compatible? (maintainer-approved exception only) Yes. The fixed stream is the documented layout; unfixed master and released 26.8.18.2 / 26.9.19.9 clients read a fixed server correctly. No setting, no protocol revision change.
i Invariants and contracts preserved? A network size stream for rows [offset, offset + limit) starts from 0 and ends at the number of character bytes written, on every path including limit == 0 and a zero base.

Session id: cron:clickhouse-impl-slot-40:20261020-110300

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Avogar, could you review this and add the can be tested label so CI runs? Since #110320 the native size stream carried absolute string offsets for a range starting at row > 0 (only SerializationSparse writes such a range, and the NULL default row of Sparse(Nullable(String)) can keep a non-empty nested string), while the reader counts them from the first written row; the writer now rebases them by offsets[offset - 1].

@clickhouse-gh

clickhouse-gh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [588830b]

Summary: ✅


AI Review

Summary

The PR rebases cumulative string offsets when serializing a range over the native protocol, excluding bytes belonging to omitted rows. This fixes incorrect results and UNKNOWN_PACKET_FROM_SERVER exceptions for sparse Nullable(String) columns. No actionable findings were identified in head 588830b92028b3f79316acd2ac60b787630cf5c2. The focused regression test covers computed and stored sparse columns and persistence through remote; the PR discussion records failing runs without the fix, passing runs with it, and compatibility checks with released clients.

Missing context / blind spots
  • ⚠️ Broader CI and automated bugfix validation are still pending in the restarted workflow. The new regression test passed in Fast test; completed sanitizer and full-suite jobs would close the remaining CI coverage gap.
Final Verdict

Status: ✅ Approve. No code changes requested.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-data-types Type system & core datatypes used across query planning/execution. labels Oct 20, 2026
@alexey-milovidov alexey-milovidov added can be tested Allows running workflows for external contributors pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Oct 21, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 21, 2026
@alexey-milovidov alexey-milovidov changed the title Fix shifted string offsets of sparse Nullable(String) columns in the native protocol Fix shitted string offsets of sparse Nullable(String) columns in the native protocol Oct 21, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 21, 2026
Merged via the queue into ClickHouse:master with commit a4ab377 Oct 22, 2026
525 of 529 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added pr-synced-to-cloud The PR is synced to the cloud repo pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR labels Oct 22, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 22, 2026
Backport #126937 to 26.10: Fix shitted string offsets of sparse `Nullable(String)` columns in the native protocol
clickhouse-gh Bot added a commit that referenced this pull request Oct 22, 2026
Backport #126937 to 26.8: Fix shitted string offsets of sparse `Nullable(String)` columns in the native protocol
clickhouse-gh Bot added a commit that referenced this pull request Oct 22, 2026
Backport #126937 to 26.9: Fix shitted string offsets of sparse `Nullable(String)` columns in the native protocol
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 22, 2026
strtgbb pushed a commit to Altinity/ClickHouse that referenced this pull request Oct 22, 2026
…rse `Nullable(String)` columns in the native protocol
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-data-types Type system & core datatypes used across query planning/execution. 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

3 participants