Repository navigation
Keep trailing zero bytes when converting FixedString to String - #122086
alexey-milovidov wants to merge 120 commits into
Conversation
`CAST(toFixedString('a', 2) AS String)` used to return `'a'`: the conversion
removed the zero bytes that pad a `FixedString` value to its length. This is a
lossy conversion, and it disagrees with other paths that keep the bytes as is,
such as the conversion of constant `IN` set elements, which made
`'a' IN (toFixedString('a', 2))` false while `'a' = toFixedString('a', 2)` is true.
Now the bytes are copied as is. The new setting
`cast_fixed_string_to_string_strip_trailing_zeros` (default `false`,
`true` with `compatibility` before 26.10) restores the old behavior.
Related: #117293 (comment)
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Workflow [PR], commit [ee5d7f2] Summary: ❌
|
The array-search functions coerce the constant with `castColumn`, which has no query context and now keeps the trailing zero bytes of a `FixedString`. The index kept stripping them, so it hashed a value the function never compares and pruned granules with matching rows (`04654_bloom_filter_array_fixed_string_constant`). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Their cast to `String` now keeps the trailing zero bytes, so the needle must not be
stripped before the lookup, and on a `FixedString(N)` column only a needle of exactly
`N` bytes can match. Before, `hasAny(arr, [toFixedString('hello', 10)])` returned
different rows with and without the index (`05223_text_index_fixed_string_padded_needle`).
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… transform over `String`
The constant is compared zero-padded, so it matches a family of `String` values, while
the cast to `String` now keeps all its bytes and gives one of them. With
`ORDER BY reverse(tuple(bin(p), reverse(p)))`, `toFixedString('abc', 257) = p`
selected no rows (`04612_fixedstring_const_string_key_condition`).
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…cial case `groupConcat` cut trailing zero bytes to match `CAST(FixedString AS String)`, which no longer does that (`03235_groupArray_string_consistency`). `castForIf` existed only to keep the padding that `castColumn` used to cut, so it is now the same as `castColumn`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…eep-trailing-zeros
…ey sound for it
`equals` between a `FixedString` and an `Enum` casts both to `String`, which now keeps
the padding, so `toFixedString('7', 4) = CAST('7' AS Enum8('7' = 3))` became false while
the same comparison with the `String` constant `'7'` stays true. Compare the `FixedString`
side as is with the other side cast to `String`, as for a `String` argument.
`KeyCondition` wrapped such a key into a cast to `String` and pruned the matching granule.
For `equals`/`notEquals`, pad the constant to the key width instead, which keeps it a point;
decline other comparisons (`04652_enum_constant_vs_string_column`).
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…the conversion to `String` Where the output of the conversion is the point of the test, the references change. Where the test is about something else, it avoids the padding (`toStringCutToZero`, exact `FixedString` widths). The `bloom_filter` and `text` index tests keep comparing the indexed answer with the unindexed one; their pinned rows and comments follow the new semantics, and their pruning cells use constants that can still match: a constant wider than a `FixedString` element now matches nothing, and the index declines it. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122086&sha=8bf6d1d7432a06a04da8923840a560c45fd4ac00&name_0=PR&name_1=Fast%20test Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…eep-trailing-zeros Resolve the conflict with the move of the settings changes history into the setting declarations (ee6b44f): the history records this PR added to `SettingsChangesHistory.cpp` now live as trailing arguments of the `DECLARE` of each setting. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…eep-trailing-zeros
…lement by element
The cast of both arrays to their least supertype keeps the zero padding of
the `FixedString` elements, while the scalar comparison of `FixedString`
with `String` or `Enum` is zero-padded. So `[toFixedString('a', 2)] = ['a']`
was false while `toFixedString('a', 2) = 'a'` is true. Such arrays now go
through the element-wise lexicographic comparison, which reuses the scalar one.
Address review: #122086 (comment)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rip_trailing_zeros`
`multiIf` and `transform` (and `CASE`, which uses them) convert their
arguments with `castColumn`, which runs without a query context, so the
setting did not restore the old conversion of `FixedString` to `String`
in them. They now read the setting when they are created and pass it to the
cast: `castColumn` and `createInternalCast` take it as a flag that stands
for the setting when there is no context.
`if` is left as is: both its constant and non-constant paths kept the
padding before 26.10 as well, so `SELECT hex(if(1, toFixedString('a', 2), 'x'))`
was `6100` then too.
`optimize_if_transform_strings_to_enum` no longer rewrites `transform`
with a `FixedString` array of results: the rewrite took the values of the
array as they are, regardless of how `transform` converts them.
Address review: #122086 (comment)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The inherited `addBatchSparseSinglePlace` adds all non-default values first and the default rows after them, which reorders the result of an order-dependent function. Iterate row by row instead, as `groupFormat` does. This made `03235_groupArray_string_consistency` fail with `ratio_of_defaults_for_sparse_serialization = 0`: `groupConcat` over a sparse `FixedString` column was reordered, while its cast to `String`, which now keeps the zero padding, is no longer sparse and was not. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122086&sha=5e3b4d8fa4b1729d36b1c8c802a322c3ceecb0c2&name_0=PR&name_1=Stateless%20tests%20%28arm_asan_ubsan%2C%20targeted%29 PR: #122086 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…version to `String` `multiIf`/`CASE` with a `FixedString` branch, and `Arrow`/`Parquet` fixed-size binary data inserted into `String` columns keep the trailing zero bytes now. `02764_csv_trim_whitespaces` is marked `long`: it runs hundreds of `clickhouse-local` processes and takes more than 180 seconds in the flaky check under ASan. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122086&sha=5e3b4d8fa4b1729d36b1c8c802a322c3ceecb0c2&name_0=PR PR: #122086 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test was added on master and converts `Nullable(FixedString(24))` to `String` with `toString`, which now keeps the zero padding. Use `toStringCutToZero` like `02764_csv_trim_whitespaces` does, so the test checks the whitespace handling rather than the padding. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122086&sha=cd6696d96f345cec52a8a777e5c7d45602bb44f7&name_0=PR&name_1=Fast%20test PR: #122086 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…eep-trailing-zeros
`IDataType::forEachChild` was removed on master in favor of the walks in `DataTypes/TypeTree.h`, which broke the build of `FunctionsComparison.h`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122086&sha=589e7c4935e85fa51140e303bacbb68a2ee26eff&name_0=PR&name_1=Fast%20test PR: #122086 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…eep-trailing-zeros Resolve the conflict in `MergeTreeIndexBloomFilter.cpp`: keep the `Enum` name handling from `master` and call `coerceStringFieldLikeSearchFunction` with the signature of this branch. The comment of `tryConvertEnumNameConstant` is updated: the cast to `String` now keeps the padding. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…g` padding `has`, `indexOf`, `hasAny` and `hasAll` compare an `Enum` with a `FixedString` after the cast of both to `String`, which keeps the zero padding now, so a padded `FixedString` needle no longer matches the name of the enum value. Add needles without padding to keep covering the match, and keep the padded ones with the new expected results; the results with and without the index still agree. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s` in `updateHash` `FunctionConvertSettings::updateHash`, `FunctionMultiIf` and `FunctionTransform` captured the setting but did not hash it, so expressions that differ only in it could share `query_condition_cache` entries. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…to_string_strip_trailing_zeros` Without hashing the setting in `updateHash`, the second query of each pair was served the "no matching rows" verdict of the first one and returned 0. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Merged |
…FixedString` constant
An `Enum` is ordered against a `FixedString` constant by its name, while
the folds of `optimize_redundant_comparisons` compare the `Enum` ordinals.
The two orders differ in general, e.g. for `Enum8('a' = 2, 'b' = 1)`, so
only an equality or inequality keeps the converted bound.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…side as a join key Like arrays, maps with a `FixedString` key or value are compared with maps of another type element by element, zero-padded, while the join keys convert a `FixedString` to `String` without its trailing zero bytes. Add a test for this and for the ordered `Enum` comparison folds. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… like at execution The adaptors for `Dynamic` and `Variant` arguments build the function again during the execution, when the context of the scope that created the resolver (e.g. a subquery with `SETTINGS`) may have expired. This threw `Context has expired` from `arrayReduceInRanges` in the test `05616_fixed_string_enum_single_name_and_const_array_comparison`. Parse the name and parameters of the aggregate once, during the analysis, and reuse them. `FunctionInitializeAggregation` did not use its context. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122086&sha=8420b3e782dcb289512d1606401636ec9dbc92cb&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29 PR: #122086 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Pushed fd05e68 (merged
@groeneai, investigate the failure: The two older threads (persisted key expressions on upgrade, |
|
The fix is #130670, merged 2026-11-11 19:06 UTC. The run you linked was created at 13:27 UTC that day, so its build did not include it. This PR did not cause the failure. The tables in question come from CIDB, last 10 days,
|
|
The separate PR for the remaining case is #131426. It also sets |
# Conflicts: # src/Interpreters/ActionsDAG.cpp # src/Interpreters/ActionsDAG.h # src/Storages/MergeTree/MergeTreeIndexBloomFilter.cpp
…of `optimize_and_compare_chain`
An `Enum` is ordered against a `FixedString` constant by its name, while
`tryNormalizeConstEdge` converted the constant to an `Enum` ordinal and put
the edge into the ordinal order domain, so transitive inference derived
wrong conjuncts for e.g. `Enum8('a' = 2, 'b' = 1)`. Only an equality or
inequality is normalized now.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…very row `gatherAlignedArrayElements` deferred a constant side only when its element type contained `Array` or `Map`, so a long `FixedString` leaf or a `Dynamic` holding an array was still copied once per input row. Decide by the size of the elements of the constant instead, whatever carries the payload. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 Pushed 2d23af9 (merged
@groeneai, thanks: the Two threads still need a decision from the author: persisted key expressions after upgrade, and |
…eep-trailing-zeros Conflicts were with the `[[nodiscard]]` annotations added on master: kept the PR's members and added `[[nodiscard]]` to them. In `castOrDefault.cpp` the local `getSourceNullMap` was replaced on master by `getOuterNullMap`, which the PR code already uses. In `if.cpp` the PR's removal of `castForIf` is kept. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…g` conversion mode #118561 added a cast of the keys to the common type in `preCalculateKeys` (used by the runtime filters). It was built without a context, which now keeps the trailing zero bytes of a `FixedString`, so `JOIN ... USING` with runtime filters dropped all rows (`05334_cast_fixed_string_to_string_join_keys`). Convert the keys as `predicateOperandsToCommonType` does: without the trailing zero bytes and under the distinct name `_CAST_strip_trailing_zeros`. Update the tests from master that pin the old conversion: - `05136_join_fixed_string_zero_bytes`: arrays with `FixedString` elements are now compared element by element, zero-padded like the scalars, so the operator and the join both give 8 instead of 4. - `05137_join_fixed_string_zero_bytes_runtime_filter`: the new name of the key cast in `EXPLAIN`. - `05813_parquet_fixed_string_null_as_default`: a NULL read as `String` is the default `FixedString(2)` converted to `String`, which now keeps its two zero bytes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…c` in a serialized plan `ConcatOverloadResolver` delegates a single argument to the `toString` it created from the context, but `getFixedStringToStringStripTrailingZeros` only looked into `FunctionToOverloadResolverAdaptor`, so the `FunctionBaseDynamicAdaptor` around `concat` reported no mode and the deserialized plan used the outer scope's one. A resolver may now implement `IFunctionWithFixedStringToStringMode` itself, and `ConcatOverloadResolver` reports the mode of its `toString`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The decision to defer a constant side in `gatherAlignedArrayElements` averaged the element sizes over the whole constant, so one large element among many small ones was still copied for every input row (a 1 MB first element next to 20000 empty strings needed a 953 MiB allocation). Sum the sizes of exactly the positions that gathering would copy - the common prefix of the constant row for each input row. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…onstant arrays, and null-safe join keys Co-Authored-By: Claude Opus 5.5 <[email protected]>
…settings history The 26.11 release branch was created without this change, so the new default belongs to 26.12: `compatibility` set to 26.11 or earlier keeps removing the trailing zero bytes. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…lue zero-padded Test `05815_transform_fixedstring_shorter_entry`, added in master for #132103, expects `transform('ab', [toFixedString('ab', 4)], [1], 0)` to find the entry, like `equals` does. The fix in master relied on the conversion of the entries to `String` removing the trailing zero bytes, which this branch no longer does by default. Convert such entries without the trailing zero bytes whatever the setting is, as the join keys do. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Mark `getFixedStringToStringStripTrailingZeros` and `serializeNestedColumn` as `[[nodiscard]]` (`modernize-use-nodiscard`) and allow the short-lived `std::vector` in `FunctionsComparison.h`. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122086&sha=c4f5e9b5de261c818b7ccc499dad3b813fe11607&name_0=PR #122086 Co-Authored-By: Claude Fable 5.2 <[email protected]>
…eep-trailing-zeros Conflicts: - `FunctionsConversion.cpp`, `FunctionsConversion.h`: both sides added includes and a member of `FunctionConvertSettings` (`cast_fixed_string_to_string_strip_trailing_zeros` here, `json_use_optimized_type_conversion` on master), both are kept. The new copy constructor for a nested JSON `CAST` takes `cast_fixed_string_to_string_strip_trailing_zeros` from the base settings. - `RuntimeFilterLookup.cpp`: master applies the `CAST` function recorded from the probe-side filter, which was built with the `FixedString` to `String` conversion of the join keys, so the separate `addCast` with the override is not needed. - `MergeTreeIndexBloomFilter.cpp`: master only rewrote the trimming of a `FixedString` constant with `trimRight`, while this branch removed the trimming; the removal is kept. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…ring` in the logical expression optimizer
A `FixedString` inside an `Array` or a `Tuple` constant is compared with a `String` zero-padded element by
element, so the constant converted to the type of the column is only one of the values it matches. The chain
`k = [toFixedString('a', 2)] OR ...` over an `Array(String)` was rewritten to `IN` and lost the row `['a']`,
and `k = [toFixedString('a', 2)] AND k = ['a']` was folded to false. `tryConvertToColumnType` now declines
such constants, as it does for a top-level `FixedString` compared with a `String`.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
…esolver The adaptors for `Dynamic` and `Variant` arguments hash the overload resolver instead of the function it builds. `ConcatOverloadResolver` did not override `updateHash`, so single-argument `concat` over a `Dynamic` or a `Variant` had the same query condition cache key with different values of `cast_fixed_string_to_string_strip_trailing_zeros`. It now hashes the `toString` it delegates to. Co-Authored-By: Claude Fable 5.2 <[email protected]>
…th a `FixedString` `executeNullableWithoutSupertype` and `executeNotDistinctWithFixedString` converted constant operands to full columns, so `isNotDistinctFrom` of a `Nullable(Enum)` column with a `Nullable` constant `FixedString` of 1 MB replicated the constant for every row and exceeded the memory limit. The nested value of a constant, and the elements of a constant tuple, are now passed on as constants. Test `05873_fixed_string_composite_constant_chain_concat_hash_null_safe_constant` covers this and the two previous commits. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Closes: #120986
Closes: #129837
Related: #117293
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The conversion of
FixedStringtoString(CAST,toString, and implicit conversions) no longer removes trailing zero bytes:CAST(toFixedString('a', 2) AS String)is now'a\0'instead of'a'. The old behavior can be restored with the settingcast_fixed_string_to_string_strip_trailing_zeros = 1, which is also enabled bycompatibilityset to a version before 26.12.Description
The conversion of
FixedString(N)toStringremoved the trailing zero bytes of each value. That makes it lossy:toFixedString('a', 2)and aFixedString(2)value holding the bytes'a\0'on purpose both become'a', and a binaryFixedString(a hash, an IPv6 address, a packed structure) that ends in a zero byte comes out shorter thanN. It also disagrees with other conversions that keep the bytes as is, such as the conversion of constantINset elements to the left-hand type. That mismatch came up in #117293 (comment).Now the bytes are copied as is. The new setting
cast_fixed_string_to_string_strip_trailing_zerosrestores the old behavior. It isfalseby default andtruewithcompatibilitybefore 26.12.castColumnwithout a query context gets the new behavior as well.This PR changes only the conversion.
equalsbetweenStringandFixedStringstill compares zero-padded, so'a' = toFixedString('a', 2)stays true whiletoFixedString('a', 2)::String = 'a'is now false.The new default is on deliberately, to see in CI which tests and internal code paths depend on the old behavior. Test
05256_cast_fixed_string_to_string_trailing_zeroscovers both modes,Nullable,LowCardinality, andcompatibility.Internal code that emulated the old conversion follows the new one: the
bloom_filterandtextindex analysis no longer strip aFixedStringconstant forhas/hasAny/hasAlland the map functions (they returned different rows with and without the index),groupConcatoverFixedStringkeeps the padding, andKeyConditiondeclines aFixedStringconstant pushed through a key transform overString.equalsbetween aFixedStringand anEnumnow compares zero-padded, like with aString, and the primary key pads the constant to the key width for it. Arrays withFixedStringelements are compared with arrays of another element type element by element, like the scalars, so[toFixedString('a', 2)] = ['a']stays true. The other elements of such arrays (for example, the other fields of a tuple) are compared as in arrays of the same type, bycompareAt.CASEwith aFixedStringmixed with another type in its keys, also inside aTupleor anArray, goes throughmultiIf. The primary key analysis does not rewritecoalescecomparisons when a branch with aFixedStringis converted to the result type.multiIfandtransform(andCASEthrough them) convert their arguments without the query context, but readcast_fixed_string_to_string_strip_trailing_zerosthemselves and pass it to the cast.ifkept the padding before this change as well. AFixedStringentry oftransformlooked up by aStringvalue is converted without its trailing zero bytes, whatever the setting is, sotransform('ab', [toFixedString('ab', 4)], [1], 0)finds the entry, as'ab' = toFixedString('ab', 4)is true (05815_transform_fixedstring_shorter_entry). The join keys (ON,USING, both analyzers, the runtime filters and their index analysis, the shuffle hashing of a distributedJOIN, and the predicates pushed across the join) keep removing the trailing zero bytes in the conversion of aFixedStringkey toString, whatever the setting is, soJOIN ... ON fs = smatches the same rows asfs = s. A comparison of arrays withFixedStringelements with arrays of another element type is not used as a join key: it compares zero-padded element by element, which the converted keys cannot reproduce, so it stays a filter over the joined rows. The primary key uses order comparisons of aFixedStringkey with anEnumtoo, with the constant padded to the key width, unless theEnumname ends with a zero byte. AFixedStringconstant compared with anEnumis normalized without its trailing zero bytes by the logical expression optimizer, and equality chains with such constants are not rewritten toIN. Note thathas/hasAny/mapContainsKeybetweenStringandFixedStringelements, andarraywith elements of both types, now see the padding, and they convert without the query context, socast_fixed_string_to_string_strip_trailing_zerosdoes not restore them.groupConcatover a sparse column now keeps the order of the rows: it added the default rows after all the others.A NULL of a nullable Parquet
FIXED_LEN_BYTE_ARRAYcolumn read asStringwithinput_format_null_as_defaultis the defaultFixedString(N)converted toString, so it is nowNzero bytes instead of an empty string (05813_parquet_fixed_string_null_as_default).A cast keeps its
FixedStringconversion mode when the query plan is serialized (two new bits of the node flags in the serializedActionsDAG, ignored by older receivers), so the forced stripping of the join keys survives a distributed plan. The partition and primary key analysis declines conditions where aFixedStringnested in a composite type (such asArray(FixedString(N))) would be converted to another type.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/122086]