Skip to content

Preserve Geometry type name in flipCoordinates - #110694

Merged
alexey-milovidov merged 9 commits into
ClickHouse:masterfrom
groeneai:fix-flipcoordinates-geometry-type-110680
Jul 30, 2026
Merged

alexey-milovidov merged 9 commits into
ClickHouse:masterfrom
groeneai:fix-flipcoordinates-geometry-type-110680

Conversation

@groeneai

@groeneai groeneai commented Jul 16, 2026 •

Copy link
Copy Markdown
Collaborator

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 flipCoordinates losing the Geometry type: for a Geometry argument the result is now typed Geometry again instead of the underlying Variant(...), so it can be passed directly to functions like areaCartesian.

Description

Closes: #110680

flipCoordinates(g) over a Geometry argument returned the underlying Variant(LineString, MultiLineString, MultiPolygon, Point, Polygon, Ring) type instead of Geometry. The flipped value could not be passed to functions requiring a geometry type, e.g. areaCartesian(flipCoordinates(g)) failed with Code: 43 ILLEGAL_TYPE_OF_ARGUMENT.

Root cause: the function did not override useDefaultImplementationForVariant() (default true), so FunctionBaseVariantAdaptor intercepted the named Geometry Variant, executed per subtype, and reassembled a bare Variant, dropping the custom name. getReturnTypeImpl returns arguments[0], but it never ran for the Variant path.

Fix: flipCoordinates now handles the Variant argument itself (useDefaultImplementationForVariant() returns false). It flips each variant sub-column in place and reassembles a ColumnVariant with the original discriminators, offsets, and local-to-global mapping. flipCoordinates preserves geometry structure (Point stays Point, Polygon stays Polygon, ...), so no rows move between variants and the result column matches the input Variant type. This makes arguments[0] (the custom-named Geometry) the correct result type.

Verified: toTypeName(flipCoordinates(g)) = Geometry, areaCartesian(flipCoordinates(g)) returns the area, mixed-type Geometry columns and rows containing NULL keep the Geometry type, and raw Point/Ring/Polygon/MultiPolygon inputs are unchanged. Regression assertions added to 03444_flip_coordinates.

Version info

  • Merged into: 26.8.1.465 (included in 26.8 and later)

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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. WITH readWKT('POLYGON((0 0,4 0,4 3,0 3,0 0))') AS g SELECT toTypeName(flipCoordinates(g)) returns Variant(...) and areaCartesian(flipCoordinates(g)) fails Code 43, every run.
b Root cause explained? flipCoordinates did not override useDefaultImplementationForVariant() (default true), so FunctionBaseVariantAdaptor intercepted the named Geometry Variant, executed per subtype, and reassembled a bare Variant, dropping the custom name; getReturnTypeImpl's arguments[0] never ran for the Variant path.
c Fix matches root cause? Yes. Disables the adaptor for this function and flips each variant sub-column in place, reassembling a ColumnVariant with the original discriminators/offsets/mapping so arguments[0] (custom Geometry) is the result type.
d Test intent preserved / new tests added? Existing 03444_flip_coordinates values unchanged; added assertions for toTypeName(flipCoordinates(g))=Geometry, areaCartesian(flipCoordinates(g)), and a mixed-type Geometry column.
e Both directions demonstrated? Yes. Reverted-source binary reproduces Variant(...) + Code 43; fixed binary returns Geometry + area 12. 03444 reference diffs exactly on those lines.
f Fix is general across code paths? flipCoordinates has a single execute path; the Variant branch covers all Geometry subtypes (Point/Ring/Polygon/MultiPolygon/LineString/MultiLineString) recursively via the existing Tuple/Array handlers. No sibling function shares this path.
g Fix generalizes across inputs (params/datatypes/wrappers)? Verified across all Geometry subtypes, a mixed-type Geometry column, rows containing NULL (kept as NULL, type Geometry), and raw Point/Ring/Polygon/MultiPolygon inputs (unchanged). flipCoordinates takes exactly one argument, no settings.
h Backward compatible? (maintainer-approved exception only) Yes. Return type moves from bare Variant(...) to the equivalent custom-named Geometry (a strict fix of the reported bug); flipped values are byte-identical. No setting/format/on-disk change.
i Invariants and contracts preserved? Result ColumnVariant reuses the input's local_discriminators, offsets, and local_to_global mapping; only the sub-columns are swapped x<->y, preserving Variant structure invariants (discriminator/offset alignment, per-variant sizes).

Session id: cron:clickhouse-worker-slot-0:20260716-125100

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Avogar @scanhex12 — could you review? Preserves the Geometry custom type name through flipCoordinates by handling the Variant argument directly (disabling the generic Variant adaptor which rebuilds a bare Variant).

@clickhouse-gh

clickhouse-gh Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5d4dc1a]

Summary: ✅


AI Review

Summary

This PR fixes the original flipCoordinates(Geometry) contract by preserving the Geometry custom type name, keeps ordinary Variant(...) inputs on the existing adaptor path, and applies the same passthrough fix to identity. I reviewed the current diff, the touched code paths, and the prior PR discussion against the current code and did not find any remaining correctness, compatibility, or coverage issues that warrant an inline review comment.

Final Verdict

✅ No findings.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 16, 2026
@PedroTadim

Copy link
Copy Markdown
Member

@groeneai can you check if there are more functions not overriding useDefaultImplementationForVariant when they should?

@Avogar Avogar self-assigned this Jul 16, 2026
Comment thread src/Functions/flipCoordinates.cpp Resolved
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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

@PedroTadim Audited src/Functions/ for functions that should override useDefaultImplementationForVariant but don't.

Scope: Geometry is the only custom-named Variant type in the codebase (DataTypeCustomGeo.cpp), so this bug can only surface for functions that accept Geometry. The corruption happens when a function's result type echoes the input's exact per-subtype type (adaptor rebuilds a bare Variant). A function is safe if it (a) overrides the flag, (b) overrides useDefaultImplementationForNulls=false (the flag defaults to it, e.g. materialize), or (c) returns a fixed type name for every subtype.

Findings:

  • identity / __scalarSubqueryResult (FunctionIdentityBase): affected. Pure passthrough returning arguments.front(), no override -> identity(g) returned Variant(...), areaCartesian(identity(g)) failed with Code 43. Fixed in 966f86c (same one-line override, mirroring the existing LowCardinality one in that class) + regression test 03950.
  • Dedicated geo functions are already safe: area*/perimeter*/geometryIntersect/FunctionGeometry override the flag; MVTEncodeGeom returns a fixed Geometry name (adaptor's all-same path preserves it); wkt/wkb/svg return String.
  • reverse(g) returns a different type but that is correct: it transforms content (Point -> swapped Tuple), not a name-dropping bug.
  • materialize/if/ifNull/coalesce/any already preserve Geometry.

Both fixes verified in both directions (fails without, passes with) on debug builds.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate — identity() commit 966f86c (click to expand)
# Question Answer
a Deterministic repro? Yes. WITH readWKT('POLYGON((0 0,4 0,4 3,0 3,0 0))') AS g SELECT toTypeName(identity(g)) returns Variant(...) (should be Geometry); areaCartesian(identity(g)) fails with Code 43.
b Root cause explained? identity/__scalarSubqueryResult (FunctionIdentityBase) return arguments.front() and the column verbatim, but did not override useDefaultImplementationForVariant (defaults to useDefaultImplementationForNulls, also not overridden). So FunctionBaseVariantAdaptor intercepts the named Geometry Variant, runs per subtype (each returning its bare subtype), and reassembles a bare Variant, dropping the custom name.
c Fix matches root cause? Yes. Override useDefaultImplementationForVariant()=false so the pure passthrough keeps the exact argument type — the direct mechanism from (b), mirroring the existing LowCardinality override in the same class.
d Test intent preserved / new tests added? New regression test 03950_identity_preserves_geometry_type asserts type preservation, areaCartesian(identity(g)), mixed-subtype round-trip, and unchanged plain-Variant/scalar passthrough. No existing test weakened.
e Both directions demonstrated? Yes. Reverted-source debug binary reproduces Variant(...) + Code 43 (test diffs); fixed binary returns Geometry + area, values round-trip, test passes.
f Fix is general across code paths? Yes. Audited all arguments[0]-passthrough functions; identity and __scalarSubqueryResult share FunctionIdentityBase, so the single override covers both. materialize/if/ifNull/coalesce already preserve Geometry; reverse legitimately transforms content.
g Fix generalizes across inputs? Yes. Geometry is the only custom-named Variant type in the codebase, so it is the only affected input; verified plain scalars, Nullable, LowCardinality, and plain Variant(...) passthrough are unchanged (type and value).
h Backward compatible? Yes. No setting/format/default change. identity now returns the exact input type, which is its documented contract ("returns the input value unchanged"); the old bare-Variant type was the bug.
i Invariants and contracts preserved? Yes. getReturnTypeImpl returning the argument type now matches executeImpl returning the argument column verbatim — the type/column-identity invariant is strengthened, not weakened. Bypassing the adaptor is correct for a verbatim passthrough.

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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pushed d47a726 addressing the empty-arm review finding.

Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. SET allow_suspicious_variant_types=1; SELECT flipCoordinates(CAST(CAST((1.0,2.0),'Point') AS Variant(Point, String))); throws Code 43 on the unfixed binary, every time.
b Root cause explained? executeForVariant iterated all num_variants declared arms. ColumnVariant keeps an empty subcolumn per declared alternative, so an unused non-geometry arm (String) was passed to the Tuple/Array flip logic and hit the ILLEGAL_TYPE_OF_ARGUMENT else-branch, even though no row uses it.
c Fix matches root cause? Yes. executeForVariant now skips arms whose subcolumn is empty in the current block (pushes the original subcolumn through unchanged), flipping only populated arms.
d Test intent preserved / new tests? Existing 03444 assertions unchanged. Added cases: Variant(Point, String) value and column carrying only Point rows -> flipped Point returned, Variant(Point, String) type preserved.
e Demonstrated both directions? Yes. Reverted source (BuildID f99261f4) throws Code 43 on the String arm; fixed (BuildID bc6448d4) returns the flipped Point and preserves the type.
f General, not a narrow patch? The empty-arm skip is generic over all declared arms and all Variant/Geometry inputs, not just the reported String case. flipCoordinates is the only geo function reassembling a Variant this way; sibling geo funcs return fixed/String types.
g Generalizes across inputs? Verified: empty arm as String; only-Point populated arm; mixed Geometry column (Point+LineString+Polygon); single-value and multi-row columns. All preserve the input type; empty arms of any non-geometry type are left untouched.
h Backward compatible? Yes. No setting/format/wire change. Only widens which valid inputs succeed; no previously-working input changes behavior.
i Invariants preserved? Yes. Reassembled ColumnVariant reuses the original local_discriminators, offsets, and local_to_global mapping; empty arms keep their exact original subcolumn, so column sizes and discriminator layout stay consistent.

Session id: cron:clickhouse-worker-slot-8:20260716-141100

Comment thread src/Functions/flipCoordinates.cpp Resolved
groeneai and others added 2 commits July 16, 2026 15:46
… 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]>
Comment thread src/Functions/flipCoordinates.cpp Resolved

@Avogar Avogar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Done, implemented exactly that in fb97639. Added useDefaultImplementationForVariantWithCustomName() (defaults to useDefaultImplementationForVariant()); IFunctionOverloadResolver::build() consults it for a Variant argument that hasCustomName(). flipCoordinates returns false only there, so a Geometry keeps its custom name via the manual path and an ordinary Variant(...) goes through FunctionBaseVariantAdaptor unchanged.

On the mismatch setting: for an ordinary Variant, flipCoordinates now behaves identically to master. Note master already throws ILLEGAL_TYPE_OF_ARGUMENT for a populated unsupported arm even with variant_throw_on_type_mismatch = 0, because flipCoordinates validates the arm type at execute time (not build time), and the adaptor only nulls build-time mismatches. I verified this against a pristine master build. Updated the regression test accordingly (03444), and re-ran 03780/03781/04202/04264 which all pass.

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — fb97639

Every 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 CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Integration tests (amd_tsan, 3/6) / test_replicated_database::test_replicated_table_structure_alter flaky (71 master hits + 30+ unrelated PRs in 30d; unrelated to this PR's flipCoordinates.cpp change) #110030 (ours, open)
PR (aggregate) rollup of the flaky integration failure above #110030 (ours, open)

Session id: cron:our-pr-ci-monitor:20260717-030000

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — fb97639

Every failure below has an owner: a fixing PR, or a full-effort fix task. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Integration tests (amd_tsan, 3/6) / test_replicated_database::test_replicated_table_structure_alter flaky (chronic; 71 master hits + 20+ unrelated PRs in 30d; our diff only touches flipCoordinates.cpp + identity.h) #110030 (ours, open)

Session id: cron:our-pr-ci-monitor:20260717-033000

Avogar
Avogar previously requested changes Jul 17, 2026
Comment thread src/Functions/IFunction.cpp Outdated
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()))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!base && (useDefaultImplementationForVariant() || useDefaultImplementationForVariantWithCustomName()))
if (!base && (useDefaultImplementationForVariant()))

I think if useDefaultImplementationForVariant is disabled, we should not check for useDefaultImplementationForVariantWithCustomName

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/Functions/IFunction.cpp Outdated
Comment on lines +808 to +812
const bool use_adaptor = arg.type->hasCustomName()
? useDefaultImplementationForVariantWithCustomName()
: useDefaultImplementationForVariant();
if (!use_adaptor)
break;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const bool use_adaptor = arg.type->hasCustomName()
? useDefaultImplementationForVariantWithCustomName()
: useDefaultImplementationForVariant();
if (!use_adaptor)
break;
if (arg.type->hasCustomName() && !useDefaultImplementationForVariantWithCustomName())
break;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/Functions/flipCoordinates.cpp Outdated
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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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??

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5ac29b5. The local is gone; the loop reads column_variant->globalDiscriminatorByLocal(local_discr) directly and the reassembly passes column_variant->getLocalToGlobalDiscriminatorsMapping().

Comment thread src/Functions/flipCoordinates.cpp Outdated
Comment on lines +123 to +126
/// 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code should process only Geometry type. Not any other Variant with unsopported types

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 56d3ec3

CI 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.

Check / test Reason Owner / fixing PR
Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) / 01666_merge_tree_max_query_limit Lane-specific flaky, not PR-caused. Failure mode here is result differs with reference on the throttle assertions: on the ParallelReplicas lane the per-table throttle is keyed on query id and the secondary replica executors send an empty one, so the holder query competes against itself and the expected 202 is not raised. 13 master hits in the last 14 days. #112385 (open, authored by me)

This PR's diff is flipCoordinates / identity Variant handling plus its own two stateless tests; it does not touch the query-throttle or parallel-replicas read paths.

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.
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 5ac29b5

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Build (arm_tidy) / Build ClickHouse PR-caused: 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] on the loop this diff adds fixed in this PR
Finish Workflow / Post Hooks consequence of the row above: the failed Build (arm_tidy) DROPPED every Build (*) and all three Bugfix validation jobs, so new_tests_check.py reports that no per-arch validator measured the bug fixed in this PR

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.
@groeneai

Copy link
Copy Markdown
Collaborator Author

Heads-up on a one-line overlap: I opened #112469 to fix an unrelated abort where identity reports a Variant type that does not match the column it passes through, and its fix is the same useDefaultImplementationForVariant opt-out on FunctionIdentityBase that this PR already adds at src/Functions/identity.h:49. The statement is byte-identical, so whichever of the two merges second needs a one-line resolution (keep one copy). The two changes fix different defects and the tests do not overlap: the tests here contain no GROUP BY, which this abort needs, and mine contain no Geometry.

@clickhouse-gh

clickhouse-gh Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.30% 86.30% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.50% 78.50% +0.00%

Changed lines: Changed C/C++ lines covered: 64/83 (77.11%) · Uncovered code

Full report · Diff report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 5d4dc1a

CI 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. Config Workflow and Finish Workflow are both green.

The arm_tidy break and the Finish Workflow / Post Hooks red carried from the previous head are both cleared by the bound cast in this commit; the Post Hooks row was a consequence of the tidy failure dropping every Build (*) and Bugfix validation node, so one fix discharged both.

The only CIDB FAIL rows on this commit are Bugfix validation (functional tests, amd64/aarch64) for 04092_geometry_cast_custom_type_name. Those are the master-binary side of the bugfix contract, which must fail on master HEAD for the new test to count as a regression test; both jobs report success and the test passes on this head.

Session id: cron:our-pr-ci-monitor:20260730-030000

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good to me right now.

@alexey-milovidov
alexey-milovidov requested a review from Avogar July 30, 2026 21:19
@alexey-milovidov alexey-milovidov self-assigned this Jul 30, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 30, 2026
Merged via the queue into ClickHouse:master with commit b51d942 Jul 30, 2026
179 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 30, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jul 31, 2026
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.
@groeneai groeneai mentioned this pull request Jul 31, 2026
1 task done
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 pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

flipCoordinates returns the raw Variant implementation instead of preserving the Geometry type

5 participants