Repository navigation
Preserve Geometry type name in flipCoordinates - #110694
alexey-milovidov merged 9 commits into
Conversation
flipCoordinates(g) over a Geometry argument returned the underlying Variant(...) type instead of Geometry, because the function relied on the generic FunctionBaseVariantAdaptor (useDefaultImplementationForVariant defaults to true). That adaptor executes the function per subtype and rebuilds a bare Variant, dropping the custom Geometry name. The flipped value could then not be passed to functions requiring a geometry type (e.g. areaCartesian failed with ILLEGAL_TYPE_OF_ARGUMENT). Handle the Variant argument directly: disable useDefaultImplementationForVariant and flip each variant sub-column in place, keeping the discriminators and offsets unchanged. flipCoordinates preserves geometry structure, so no rows move between variants and the reassembled column matches the input Variant type. This makes getReturnTypeImpl's arguments[0] (the custom-named Geometry type) the correct result type. Closes: ClickHouse#110680 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-0:20260716-125100 |
|
cc @Avogar @scanhex12 — could you review? Preserves the |
|
Workflow [PR], commit [5d4dc1a] Summary: ✅
AI ReviewSummaryThis PR fixes the original Final Verdict✅ No findings. |
|
@groeneai can you check if there are more functions not overriding |
identity() and __scalarSubqueryResult return their argument column verbatim, so the declared result type must equal the argument type exactly. Neither overrode useDefaultImplementationForVariant (which defaults to useDefaultImplementationForNulls, also not overridden here), so the generic Variant adaptor ran per subtype and reassembled a bare Variant, dropping any custom name such as Geometry. As a result identity(g) over a Geometry returned Variant(...) and could not be passed to functions requiring a geometry (e.g. areaCartesian). Same bug class as flipCoordinates (this PR). Disable the default Variant implementation so the passthrough keeps the exact argument type, mirroring the existing LowCardinality override in the same class. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
@PedroTadim Audited Scope: Findings:
Both fixes verified in both directions (fails without, passes with) on debug builds. |
Pre-PR validation gate — identity() commit 966f86c (click to expand)
Session id: cron:clickhouse-worker-slot-2:20260716-134400 |
ColumnVariant keeps an empty subcolumn for every declared alternative, even arms with no rows in the current block. executeForVariant walked all declared arms and tried to flip them, throwing ILLEGAL_TYPE_OF_ARGUMENT on an unused non-geometry arm (e.g. String in Variant(Point, String)). Skip arms that are empty in the current block, leaving them untouched. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Pushed d47a726 addressing the empty-arm review finding. Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-8:20260716-141100 |
… Variant arms A populated Variant arm of a type flipCoordinates cannot process (e.g. String in Variant(Point, String)) unconditionally threw ILLEGAL_TYPE_OF_ARGUMENT, regressing the documented variant_throw_on_type_mismatch = 0 behavior of the default Variant adaptor. Mirror that adaptor: with the setting disabled, such rows become NULL; with it enabled (the default), the throw is preserved. Geometry-name preservation is unchanged. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Avogar
left a comment
There was a problem hiding this comment.
The bot review comment is valid. We should do it only for Geometry type, not for all Variant types. Maybe, we can add a new method similar to useDefaultImplementationForVariant to check if function can use it over Variant with custom name. Like useDefaultImplementationForVariantWithCustomName that will return useDefaultImplementationForVariant by default. WDYT?
… Variant Per review: the manual Variant handling opted flipCoordinates out of the default FunctionBaseVariantAdaptor for every Variant input, not just the custom-named Geometry. Add useDefaultImplementationForVariantWithCustomName() (defaults to useDefaultImplementationForVariant()); IFunctionOverloadResolver::build() now uses it to decide the adaptor for a custom-named Variant argument. flipCoordinates returns false only there, so a Geometry keeps its custom name via the manual path while an ordinary Variant goes through the adaptor unchanged (its variant_throw_on_type_mismatch handling matches master). Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Done, implemented exactly that in fb97639. Added On the mismatch setting: for an ordinary Variant, flipCoordinates now behaves identically to master. Note master already throws |
CI finish ledger — fb97639Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only
Session id: cron:our-pr-ci-monitor:20260717-030000 |
CI finish ledger — fb97639Every failure below has an owner: a fixing PR, or a full-effort fix task. Only
Session id: cron:our-pr-ci-monitor:20260717-033000 |
| if (!base && useDefaultImplementationForVariant()) | ||
| /// A custom-named Variant (e.g. Geometry) is gated by useDefaultImplementationForVariantWithCustomName() instead, so a | ||
| /// function can handle it directly (to keep the custom name) while ordinary Variant inputs still go through the adaptor. | ||
| if (!base && (useDefaultImplementationForVariant() || useDefaultImplementationForVariantWithCustomName())) |
There was a problem hiding this comment.
| if (!base && (useDefaultImplementationForVariant() || useDefaultImplementationForVariantWithCustomName())) | |
| if (!base && (useDefaultImplementationForVariant())) |
I think if useDefaultImplementationForVariant is disabled, we should not check for useDefaultImplementationForVariantWithCustomName
There was a problem hiding this comment.
Applied in 5ac29b5. The outer condition is back to useDefaultImplementationForVariant().
Note the two suggestions together change the semantics of the cell where useDefaultImplementationForVariant is false and useDefaultImplementationForVariantWithCustomName is true, so I checked it: that cell is unreachable today, because the custom-name hook defaults to the plain one and flipCoordinates is the only override in the tree (custom-name false, plain true). 03444, 03950, 03780, 03781, 04202 and 04264 produce byte-identical output before and after, measured on two builds from the same tree.
| const bool use_adaptor = arg.type->hasCustomName() | ||
| ? useDefaultImplementationForVariantWithCustomName() | ||
| : useDefaultImplementationForVariant(); | ||
| if (!use_adaptor) | ||
| break; |
There was a problem hiding this comment.
| const bool use_adaptor = arg.type->hasCustomName() | |
| ? useDefaultImplementationForVariantWithCustomName() | |
| : useDefaultImplementationForVariant(); | |
| if (!use_adaptor) | |
| break; | |
| if (arg.type->hasCustomName() && !useDefaultImplementationForVariantWithCustomName()) | |
| break; |
There was a problem hiding this comment.
Applied in 5ac29b5, with two adjustments your form surfaced.
The hook now takes the argument type. SimpleAggregateFunction also attaches a custom name, so SimpleAggregateFunction(anyLast, Variant(Point, String)) satisfies both isVariant and hasCustomName: with a name-blind hook flipCoordinates skipped the adaptor for it and then rejected the argument, while master flips it and returns Variant(Point, String). Regression cases for that and for SimpleAggregateFunction(anyLast, Geometry) are in 03444; both now match master.
The excluded argument also continues instead of breaking, so an ordinary Variant in another position still gets the adaptor, and build passes the chosen index to FunctionBaseVariantAdaptor rather than letting it re-select the first Variant. That combination is not reachable while flipCoordinates is the only opt-out and is unary, so it is only about keeping the new hook's contract true.
| throw Exception(ErrorCodes::ILLEGAL_COLUMN, "Illegal column {} of first argument of function {}", column->getName(), getName()); | ||
|
|
||
| const auto & variant_types = variant_type->getVariants(); | ||
| const auto local_to_global = column_variant->getLocalToGlobalDiscriminatorsMapping(); |
There was a problem hiding this comment.
local_to_global is a bad name, local to global what? And why not to use column_variant->globalDiscriminatorByLocal in places we need global discr??
There was a problem hiding this comment.
Fixed in 5ac29b5. The local is gone; the loop reads column_variant->globalDiscriminatorByLocal(local_discr) directly and the reassembly passes column_variant->getLocalToGlobalDiscriminatorsMapping().
| /// ColumnVariant keeps an empty subcolumn for every declared alternative, even ones with | ||
| /// no rows in the current block. Leave such arms untouched: they carry no data to flip and | ||
| /// may be non-geometry types (e.g. String in Variant(Point, String)) that flipCoordinates | ||
| /// cannot process. Only arms with rows in this block are flipped. |
There was a problem hiding this comment.
This code should process only Geometry type. Not any other Variant with unsopported types
There was a problem hiding this comment.
Done in 5ac29b5: executeImpl rejects a Variant whose name is not Geometry before calling executeForVariant, so the invariant is checked here rather than assumed from build.
Worth flagging that Geometry is not the only custom-named Variant: SimpleAggregateFunction is registered through registerDataTypeCustom and wraps any type, so it can carry a Variant too. That is why the resolver hook had to become type-aware as well, otherwise this check turned a working query into ILLEGAL_TYPE_OF_ARGUMENT (details in the reply on the build thread).
CI finish ledger — 56d3ec3CI is fully finished on this head (174 check-runs completed, Finish Workflow green). The head is @alexey-milovidov's master merge, so nothing was pushed here. Every failure below has an owner.
This PR's diff is Session id: cron:our-pr-ci-monitor:20260729-063000 |
Keep the outer condition in `IFunctionOverloadResolver::build` as `useDefaultImplementationForVariant`, so a function that disables the plain flag is never re-admitted by the custom-name flag, and express the per-argument decision as the custom-named opt-out only. `useDefaultImplementationForVariantWithCustomName` now takes the argument type, because `SimpleAggregateFunction` also attaches a custom name: a name-blind hook made `flipCoordinates` skip the adaptor for `SimpleAggregateFunction(anyLast, Variant(Point, String))` and then reject the argument, while master flips it and returns `Variant(Point, String)`. The scan also continues past an excluded argument, and `FunctionBaseVariantAdaptor` is told which argument to execute instead of re-selecting the first `Variant` itself. In `flipCoordinates`, reject a non-`Geometry` `Variant` in `executeForVariant` rather than relying on the resolver to keep one away, and read global discriminators through `ColumnVariant::globalDiscriminatorByLocal`. 04635 built its non-identity discriminator mapping by casting the result of `flipCoordinates` back to `Geometry`, which only worked while that result was the name-sorted plain `Variant`; reverting the area/perimeter fix left it green. It now builds the reordered column directly from a `Variant` with a different alternative order, in one case reading it with `areaCartesian`/`perimeterCartesian` and in another passing it through `flipCoordinates`, whose per-arm type lookup needs the same mapping. Regression cases for both `SimpleAggregateFunction` shapes added to 03444.
CI finish ledger — 5ac29b5Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260729-140000 |
Build (arm_tidy) failed on the previous head with src/Functions/flipCoordinates.cpp:124:60: error: loop variable has narrower type 'ColumnVariant::Discriminator' than iteration's upper bound 'const size_t' [bugprone-too-small-loop-variable,-warnings-as-errors] clang-tidy runs with warnings-as-errors in that job, so this stopped the build and dropped every downstream Build and Bugfix validation node. The loop counter is a Discriminator because that is what the ColumnVariant accessors take, while getNumVariants returns size_t. Narrow the bound instead of widening the counter: a Variant may declare at most ColumnVariant::MAX_NESTED_COLUMNS alternatives, which is Discriminator's own maximum, and that limit is enforced both in the ColumnVariant constructor and in the DataTypeVariant constructor, so the count always fits and the cast cannot truncate. Comparing two Discriminator values also keeps the loop semantics exactly as they were. Widening the counter to size_t was the other option but it only moves the diagnostic: globalDiscriminatorByLocal takes a Discriminator, so passing a size_t there fails with clang-diagnostic-implicit-int-conversion instead. Verified with clang-tidy-21 driven by the in-tree .clang-tidy and warnings-as-errors, on the same compile command the build uses: the previous head reports that one error and nothing else in the file, the size_t counter variant reports the implicit-conversion error, and this change reports nothing. The change is behaviour-neutral, so 03444, 03950, 04635, 04092, 03780, 03781, 04202 and 04264 produce byte-identical output before and after.
|
Heads-up on a one-line overlap: I opened #112469 to fix an unrelated abort where |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 64/83 (77.11%) · Uncovered code |
CI finish ledger - 5d4dc1aCI is fully finished on this head and there are no failures to own: 174 check-runs, all completed, 0 failure / 0 timed_out / 0 action_required. The The only CIDB Session id: cron:our-pr-ci-monitor:20260730-030000 |
alexey-milovidov
left a comment
There was a problem hiding this comment.
It looks good to me right now.
The one-line `useDefaultImplementationForVariant` override this branch added landed on master independently through ClickHouse#110694 (which needed the same override to keep the `Geometry` custom type name on the passthrough column). Master's wording of the accompanying comment is kept verbatim, so `src/Functions/identity.h` is now byte-identical to master and this branch contributes only the regression test.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
flipCoordinateslosing theGeometrytype: for aGeometryargument the result is now typedGeometryagain instead of the underlyingVariant(...), so it can be passed directly to functions likeareaCartesian.Description
Closes: #110680
flipCoordinates(g)over aGeometryargument returned the underlyingVariant(LineString, MultiLineString, MultiPolygon, Point, Polygon, Ring)type instead ofGeometry. The flipped value could not be passed to functions requiring a geometry type, e.g.areaCartesian(flipCoordinates(g))failed withCode: 43 ILLEGAL_TYPE_OF_ARGUMENT.Root cause: the function did not override
useDefaultImplementationForVariant()(defaulttrue), soFunctionBaseVariantAdaptorintercepted the namedGeometryVariant, executed per subtype, and reassembled a bareVariant, dropping the custom name.getReturnTypeImplreturnsarguments[0], but it never ran for the Variant path.Fix:
flipCoordinatesnow handles the Variant argument itself (useDefaultImplementationForVariant()returnsfalse). It flips each variant sub-column in place and reassembles aColumnVariantwith the original discriminators, offsets, and local-to-global mapping.flipCoordinatespreserves geometry structure (Point stays Point, Polygon stays Polygon, ...), so no rows move between variants and the result column matches the input Variant type. This makesarguments[0](the custom-namedGeometry) the correct result type.Verified:
toTypeName(flipCoordinates(g)) = Geometry,areaCartesian(flipCoordinates(g))returns the area, mixed-typeGeometrycolumns and rows containingNULLkeep theGeometrytype, and rawPoint/Ring/Polygon/MultiPolygoninputs are unchanged. Regression assertions added to03444_flip_coordinates.Version info
26.8.1.465(included in26.8and later)