Repository navigation
Apply signed integer settings sent in the legacy binary format, and validate non-zero settings there - #118610
Conversation
The signed-integral arm of SettingFieldNumber<T>::readBinary decoded the wire value into a local `x` and then assigned from `value`, the field's own data member, so the value that arrived was discarded. BaseSettings::read calls resetToDefault() before its loop, so at that point `value` is the declared default; and operator=(Type) sets changed = true. The setting therefore ended up marked as changed while holding its default, which is worse than a no-op: checkLimits in ReadFromMergeTree (and other .changed-gated consumers) prefer the query-level value over the table-level one, so the bogus override wins with a default value. readVarInt had already consumed the correct number of bytes, so the settings stream stayed in sync and no other setting in the packet was affected. The format is selected by TCPHandler::processQuery for any native protocol peer declaring a revision below DBMS_MIN_REVISION_WITH_SETTINGS_SERIALIZED_AS_STRINGS (54429), and TCPHandler enforces no minimum revision, so this is reachable from ordinary use. 23 signed query settings are affected; four of them are Iceberg, DeltaLake and Paimon time-travel selectors, where dropping the value reads a different snapshot rather than merely losing a limit. Introduced in 300727a (2020-07-27), which mechanically rewrote set(static_cast<Type>(x)) into *this = static_cast<T>(...) in both integral arms and typed `value` instead of `x` in the signed one. It has been wrong in every release since, and survived because no test drives a signed setting through the binary format: the only Settings::write/read test uses STRINGS_WITH_FLAGS, which goes through setValueString. The symmetric writer was already correct, so the wire format does not change and no compatibility handling is required; this restores the read side to what the writer has always emitted. Every readBinary in src/Core/SettingsFields.{h,cpp} was audited: the signed arm was the only carrier that assigned from its own member. SettingAutoWrapper and BaseSettings::readBinary delegate to the same leaf and are fixed transitively. The regression test speaks the raw native protocol pinned to revision 54428 so the exchanged, revision-gated field set is fixed and cannot drift. It requires TOO_MANY_PARTITIONS for max_partitions_to_read = 1 against a two-partition table, with the same query and no settings as a control proving the exception comes from the transported setting rather than from the hand-rolled handshake. Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review (click to expand)Two independent passes before this PR was opened: an automated adversarial re-review by a Automated pass: 0 findings. Coverage: all changed files, the full numeric-setting My own pass: 6 findings, no blocker. Two were in the PR description's prose and are
Checked and clean. The fix is symmetric with the writer ( Session id: cron:clickhouse-review-slot-8:20260907-153100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20260907-142900 |
|
Workflow [PR], commit [6233c36] Summary: ❌
AI ReviewSummaryThis PR fixes two bugs in legacy binary settings deserialization: signed integral settings now use the decoded wire value, and Findings❌ Blockers
Tests
Final VerdictChanges requested. LLVM Coverage ReportMeasured on commit 6233c36.
Changed lines: Changed C/C++ lines covered: 29/29 (100.00%) · Uncovered code |
|
cc @Algunenano @tavplubix, could you review this? The signed-integral arm of |
SettingFieldNonZeroUInt64 re-runs checkValueNonZero() from every mutating entry point it overrides: both constructors, both assignment operators and parseFromString. readBinary was the one it did not override, so the base SettingFieldNumber<UInt64>::readBinary ran instead, and the *this = static_cast<T>(x) in it is statically bound to SettingFieldUInt64::operator=, which never reaches the derived check. Of the twelve type-erased operations in SettingFieldOps, read_binary was the only mutating one that could store a value the type forbids: assign_from_field, parse_from_string, cast_value, value_to_string and string_to_value all dispatch on the derived type, and typed_copy copies an instance that was validated when it was built. A query arriving over TCP does not expose this, because SettingsConstraints::checkOrClamp casts every changed setting through Settings::castValueUtil, which builds a temporary of the derived type and assigns to it, so the zero is already rejected there with the same BAD_ARGUMENTS. The gap is reachable where nothing runs after the decode: QueryPlanSerializationSettings::readBinary, which declares grace_hash_join_initial_buckets, grace_hash_join_max_buckets and temporary_files_buffer_size and has no constraints check, and the DBMS_DISTRIBUTED_SIGNATURE_HEADER_OLD_FORMAT branch of DistributedAsyncInsertHeader, which reads a persisted batch header with SettingsWriteFormat::BINARY. With the override the TCP path raises the same error code and the same message, only at the decode step instead of the later constraints check, and permitted values are unaffected. The regression is a gtest rather than a stateless test because the constraints check masks the defect on every SQL-reachable path, so a functional test cannot fail on it. It drives Settings::read with SettingsWriteFormat::BINARY, the same entry point the old distributed header format uses, pins the error code so a misspelled setting name cannot satisfy the assertion, and reads a permitted value through the same path as a control. SettingFieldNonZeroUInt64 is the only type in the tree that derives from a setting field; every other one is final, so there is no sibling site to change. Reported by clickhouse-gh[bot] and requested by Algunenano in review of this PR. Co-Authored-By: Claude Opus 5 <[email protected]>
|
|
||
| void SettingFieldNonZeroUInt64::readBinary(ReadBuffer & in) | ||
| { | ||
| SettingFieldUInt64::readBinary(in); |
There was a problem hiding this comment.
This moves NonZeroUInt64 validation into the shared binary leaf, which also runs when old distributed async-insert queue files are deserialized. DistributedSink still persists ordinary query settings into those files and does not strip max_parallel_replicas; before dd6599868adb6cbc on 2024-03-12 that setting was a plain UInt64, so 0 was serializable in a queue header. After this change DistributedAsyncInsertHeader::read throws BAD_ARGUMENTS while loading that old-format header (src/Storages/Distributed/DistributedAsyncInsertHeader.cpp:127), and the queue code treats that as a hard failure rather than a quarantinable broken batch (src/Storages/Distributed/DistributedAsyncInsertBatch.cpp:232-246, src/Storages/Distributed/DistributedAsyncInsertDirectoryQueue.cpp:455-490). That means an upgrade can wedge async-insert draining on a stale on-disk batch that used to replay, even though max_parallel_replicas is irrelevant on the PoolMode::GET_ONE replay path.
Could we preserve the legacy queue-file reader here? The safe options seem to be either grandfathering 0 in the DBMS_DISTRIBUTED_SIGNATURE_HEADER_OLD_FORMAT branch, or keeping this generic readBinary permissive and re-validating only in the callers that actually need the invariant (for example QueryPlanSerializationSettings / the TCP decode path).
There was a problem hiding this comment.
Answered in the existing thread on line 263, where the same point was already open: #118610 (comment)
Short version: the queue-classification half is confirmed, BAD_ARGUMENTS is absent from isDistributedSendBroken so the file is retried rather than quarantined. The first of the two suggested shapes is not available as written, because a catch around insert_settings->read(in, BINARY) leaves the file buffer mid list and the following readStringBinary(insert_query, in) would then read setting bytes as the query. The second shape is available and measured, and the choice between it and the current one is with the reviewer.
CI finish ledger - 6233c36Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260908-013052 |
5eaf70f
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed two defects in the legacy binary settings format, used for native-protocol clients declaring a revision below 54429. Signed integer settings (for example
max_partitions_to_readoriceberg_snapshot_id) were silently ignored, and were reported as changed while holding their default, so they overrode the table-level value with that default. Settings required to be greater than zero (for examplemax_block_size) were not validated when read from that format.Description
Two defects in the legacy binary settings format, which
TCPHandlerselects for any native-protocol peer declaring a revision belowDBMS_MIN_REVISION_WITH_SETTINGS_SERIALIZED_AS_STRINGS(54429); no minimum revision is enforced.1. Signed integral settings were discarded.
SettingFieldNumber<T>::readBinarydecoded into a local, then assigned fromvalue, its own data member.BaseSettings::readresets to defaults first andoperator=setschanged, so the setting was flagged as a user override while holding its default, andReadFromMergeTreethen prefers that default over the table-level value. 22 signed query settings are affected, six of them snapshot selectors for Iceberg, DeltaLake and Paimon. Introduced in300727afa316a01c(2020-07-27).2. A non-zero setting was not validated.
SettingFieldNonZeroUInt64re-runscheckValueNonZero()from every mutating entry point it overrides, but notreadBinary, whose base*this = static_cast<T>(x)binds statically toSettingFieldUInt64::operator=. Of the twelve operations inSettingFieldOps,read_binarywas the only mutating one that could store a forbidden value. Requested by @ Algunenano in review.Both writers were already correct, so the wire format is unchanged.
On (2), a TCP query's outcome is unchanged:
SettingsConstraints::checkOrClampalready rejects the zero with the sameBAD_ARGUMENTS, now raised at the decode step. The override matters where nothing runs after the decode:QueryPlanSerializationSettings::readBinaryand the old-format branch ofDistributedAsyncInsertHeader. The modern header format already rejects zero there, so this makes the legacy branch consistent; the one behaviour change is that a pre-2024-03 old-format batch carrying an explicit0would stop draining.Tests. A raw native-protocol client pinned to revision 54428 requires
TOO_MANY_PARTITIONSformax_partitions_to_read = 1. A gtest drivesSettings::readwithSettingsWriteFormat::BINARY, because the constraints check masks (2) on every SQL-reachable path. Both fail without this change.Workflow [PR]
Sync PR [sync-upstream/pr/118610]