Repository navigation
Fix optimize_functions_to_subcolumns rewriting onto the wrong subcolumn - #115777
Conversation
`optimize_functions_to_subcolumns` rewrites a function to a subcolumn of its argument (`length(arr)` to `arr.size0`, `isNull(n)` to `n.null`, `mapKeys(m)` to `m.keys`, ...) and hardcodes the type that subcolumn has. It only checked that a subcolumn with the constructed name exists, which is wrong in two independent ways, both giving wrong results: * Subcolumn names are flat, so a `Tuple` element or a `JSON` path can claim the name with the same type. ``Tuple(`a.size` UInt64, `a` String)`` exposes `a.size` twice, and declaration order decides which one a read returns, so `length(c.a)` returned the element instead of the length of `a`. The same shape affects `empty`, `notEmpty`, `isNull`, `isNotNull`, `count`, `mapKeys`, `mapValues`, `mapContains` and `m['key']`. * An enclosing `Nullable` wraps the subcolumn without changing which substream it is. ``Nullable(JSON(`a` Array(Int64)))`` exposes `c.a` as a bare `Array` but `c.a.size0` as `Nullable(UInt64)`, so `length(c.a)` returned NULL instead of 0 for a NULL row. Reachable only because `Array` and `Map` cannot be inside `Nullable`. To tell the cases apart, subcolumn resolution now reports the substreams path that identifies the resolved subcolumn, not just its data: `IDataType::getSubcolumnData` and `getDynamicSubcolumnData` become `getSubcolumnInfo` and `getDynamicSubcolumnInfo` and return a `SubcolumnInfo` holding both. Subcolumns resolved from the data rather than from the static enumeration get a synthesized path as well, so every resolved subcolumn is identifiable the same way: a `Map` key is `MapKeyValue`, a JSON path is `ObjectTypedPath`/`ObjectDynamicPath`, a `Dynamic` type subcolumn is `DynamicData` + `VariantElement`, and the JSON subcolumns built from many paths at once get `ObjectDistinctPaths`/`ObjectSubObject`/`ObjectCombinedPath`. Resolving through `Array` and `Nullable` contributes `ArrayElements` and `NullableElements`, so a dynamic resolution produces the same path the static enumeration would. The optimizer then asks the serialization that owns the substream whether the resolved subcolumn is really the one the rewrite means, and additionally requires the exact type. Neither check catches the other's case. This subsumes the two one-off guards `subcolumnIsNullableInStorage` and `nestedTypeHasNullSubcolumn`. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [20ae2b3] Summary: ✅
AI ReviewSummaryThis PR hardens Findings❌ Blockers
Final VerdictChanges requested: the exact-case/path validation looks good, but the case-insensitive shadowing invariant is still only enforced for LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 337/351 (96.01%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Object file sizes323 object files changed (+105.88 KiB total), 0 added.
737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units2990 translation units recompiled, 15448 s compile time in total, 2990 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.88 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
`optimizeTupleOrVariantElement` still only checked that a subcolumn with the
constructed name exists, so a sibling element flattening to the same name won
when it was declared first:
Tuple(`t.a` UInt64, t Tuple(a UInt64))
tupleElement(c.t, 'a') off = 1, on = 99
Tuple(`t.Int64` UInt64, t Variant(Int64, String))
variantElement(c.t, 'Int64') off = 7, on = 99
`tupleElementNameIsAmbiguousWhenFlattened` does not cover this: it only looks at
whether the element name itself contains a dot, not at the parent path plus the
element name. Reversing the declaration order makes both correct, because the
nested element then wins the first exact match.
Check the resolved substreams path here as well, through
`SerializationTuple::isElementSubcolumn` and
`SerializationVariant::isElementSubcolumn`. The element name is part of the
check: both candidates end in a `TupleElement`, and only the name tells
`TupleElement("t.a")` from `TupleElement("a")`. `QBit` delegates to its nested
`Tuple` serialization, so the same predicate covers it.
Unlike the rewrites that hardcode a type, these take the element type from the
type definition, where an enclosing `Nullable` legitimately wraps it in storage,
so `canOptimizeToExpectedSubcolumn` now takes the expected type optionally and
only the path is checked for them.
Also pin `optimize_functions_to_subcolumns` in the test before the block that
asserts the rewrites still fire: those queries read it from the session, and CI
randomizes it.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
| GetColumnsOptions(GetColumnsOptions::All).withRegularSubcolumns(), subcolumn_name); | ||
| return actual && actual->type->isNullable(); | ||
| } | ||
| auto resolved = storage_snapshot->tryGetColumn(GetColumnsOptions(GetColumnsOptions::All).withSubcolumns(), subcolumn_name); |
There was a problem hiding this comment.
canOptimizeToExpectedSubcolumn now filters exact-case flat-name collisions, but it still does not block case-insensitive top-level shadowing. sourceHasColumnCaseInsensitive is only used in the Tuple branch below, so with input_format_{orc,parquet,arrow}_case_insensitive_column_matching = 1 a schema like A.SIZE0 UInt64, a Array(UInt64) or V.INT64 UInt64, v Variant(Int64, String) will still rewrite to a.size0 / v.Int64 and let the reader bind that dotted name to the top-level column instead of the intended subcolumn. I think the iequals guard needs to live in the common dotted-name check (or be applied at every rewrite site), not only on tupleElement.
…he nested subcolumn fix
`optimize_functions_to_subcolumns` name collision (#115777) merged into master after this branch was cut, so its files exist on both sides. Master's versions win there, including the `memory-engine` tag added to `04935_optimize_functions_to_subcolumns_name_collision` afterwards. `IDataType::getSubcolumnInfo` keeps this branch's body, which delegates to `SubcolumnResolution::findSubcolumn`; master's inline implementation and its `makeSubcolumnInfo` helper are superseded by `subcolumnResolution.cpp`, which populates the same `substreams_path`. In `IMergeTreeDataPart::getCompressionCodecForPart` the two sides changed adjacent lines: master added a warning when no data file is found, this branch takes the naming scheme from the part. Both are kept. `MergeTreeData.cpp` loses both conflicting includes: master dropped `absl/container/inlined_vector.h` and this branch made `boost/container_hash/hash.hpp` unused by removing `collect_nested` from the part-columns interning key. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Related: #115632
Related: #115394
optimize_functions_to_subcolumnsrewrites a function to a subcolumn of its argument(
length(arr)toarr.size0,isNull(n)ton.null,mapKeys(m)tom.keys, ...) andhardcodes the type that subcolumn has. It only checked that a subcolumn with that name exists,
which is wrong in two independent ways, both giving wrong results.
A
Tupleelement or aJSONpath can claim the name with the same type, and declaration orderdecides which one a read returns:
The same shape affects
empty,notEmpty,isNull,isNotNull,count,mapKeys,mapValues,mapContainsandm['key'].An enclosing
Nullablewraps the subcolumn without changing which substream it is -c.ais abare
Arraywhilec.a.size0isNullable(UInt64), becauseArraycannot be insideNullable.Only
lengthlacked the guard the other rewrites already had:To tell these apart, subcolumn resolution now reports the substreams path identifying the
resolved subcolumn, not just its data (
getSubcolumnDatabecomesgetSubcolumnInfo).Subcolumns resolved from the data get a synthesized path too, so all of them are identifiable
the same way. The optimizer asks the serialization owning the substream whether the resolved
subcolumn is the one the rewrite means, and additionally requires the exact type; neither check
catches the other's case. Subsumes
subcolumnIsNullableInStorageandnestedTypeHasNullSubcolumn.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results from
optimize_functions_to_subcolumns. ATupleelement or aJSONpath named like an automatic subcolumn claimed it, so for examplelength(c.a)onTuple(`a.size` UInt64, `a` String)returned the element instead of the length;empty,notEmpty,isNull,isNotNull,count,mapKeys,mapValues,mapContainsandm['key']were affected the same way. Separately,lengthon anArrayorMapinside aNullable(for exampleNullable(JSON(`a` Array(Int64)))) returned NULL instead of 0 for a NULL row.Workflow [PR]
Sync PR [sync-upstream/pr/115777]
Version info
26.9.1.1084(included in26.9and later)