Skip to content

fix: enforce geometry subtype validation at INSERT/UPDATE bind time - #24353

Merged
mergify[bot] merged 8 commits into
matrixorigin:3.0-devfrom
ck89119:issue-24350
May 12, 2026
Merged

mergify[bot] merged 8 commits into
matrixorigin:3.0-devfrom
ck89119:issue-24350

Conversation

@ck89119

@ck89119 ck89119 commented May 12, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #24350

What this PR does / why we need it:

Ports the cast_geometry_to_subtype runtime function from main to 3.0-dev so that INSERT/UPDATE operations validate the WKT geometry subtype and SRID against the column declaration. Previously on 3.0-dev, a column declared as POINT could silently accept a LINESTRING value.

Changes:

  • Register CAST_GEOMETRY_TO_SUBTYPE function ID and built-in overload
  • Implement CastGeometryToSubtype runtime function with full WKT structure validation
  • Add funcCastForGeometryType planner function that wraps geometry expressions with subtype/SRID validation
  • Wire geometry cast into all INSERT/UPDATE bind paths (bind_insert, bind_update, build_update, build_constraint_util, make.go)
  • Add unit tests covering subtype match/reject, SRID mismatch, malformed structure, and non-finite coordinates
  • Add T_geometry support to the function test framework

🤖 Generated with Claude Code

…atrixorigin#24350)

Port cast_geometry_to_subtype runtime function from main to 3.0-dev so
that INSERT/UPDATE operations validate the WKT subtype and SRID against
the column declaration, rejecting mismatches at plan time.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@ck89119
ck89119 requested review from aunjgr and ouyuanning as code owners May 12, 2026 07:41
@qodo-code-review

Copy link
Copy Markdown
ⓘ You've reached your Qodo monthly free-tier limit. Reviews pause until next month — upgrade your plan to continue now, or link your paid account if you already have one.

1. bind_insert.go:173 and build_constraint_util.go:717 now route
   geometry columns through funcCastForGeometryType instead of plain
   forceCastExpr, closing the ON DUPLICATE KEY UPDATE loophole.

2. Move geometry cast check before isSameColumnType() in
   makePlan2CastExpr so that two T_geometry types with different
   Enumvalues (subtype metadata) still get validated.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@matrix-meow matrix-meow added the size/XL Denotes a PR that changes [1000, 1999] lines label May 12, 2026
1. ODKU ON UPDATE path now validates geometry via funcCastForGeometryType.
2. All DEFAULT value paths (getDefaultExpr + *tree.DefaultVal) in
   bind_insert.go and build_constraint_util.go now pass geometry
   columns through subtype validation.
3. Expand test suite: all geometry subtypes, SRID matching/mismatch,
   EMPTY geometries, malformed structures, null propagation, and
   GEOMETRY(...) wrapper bypass.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Add tests for all type switch branches in maxPointsInGeometryLimit,
point count exceeded path, multipoint/multilinestring/multipolygon
count helpers, generic geometry content validation, collection nesting
depth limit, encode/decode payload helpers, and split/parse utilities.

All functions in func_geometry.go now exceed 75% coverage.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Cover funcCastForGeometryType, isGeometryNullLiteralExpr,
isGeometryPlanType, geometrySubtypeName, and normalizeGeometrySubtype
to satisfy CI coverage check on new lines.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Clean port of geometry subtype/SRID validation from main to 3.0-dev.

Design

funcCastForGeometryType wraps geometry column expressions with a cast_geometry_to_subtype(metadata, value) runtime function at bind time. The metadata string carries "POINT;SRID=4326" (subtype + SRID), and the runtime function validates:

  1. WKT type name matches column subtype (POINT/LINESTRING/POLYGON/etc.)
  2. SRID matches column SRID (with assignment semantics when value has no SRID)
  3. WKT structural validity (parenthesis balance, coordinate pair count, ring closure)
  4. Finite coordinates (rejects NaN/Inf)
  5. Point count cap via max_points_in_geometry session variable

Correctness verified

  • All INSERT/UPDATE bind paths covered: 5 call sites in bind_insert.go, 4 in bind_update.go/build_update.go, 6 in build_constraint_util.go, plus make.go:577 for general cast wrapping. ON DUPLICATE KEY UPDATE path covered (commit bdd8ab11).
  • Short-circuits correct: T_any type and NULL literal expressions skip validation (line 84-87). Same-type same-metadata passes through without re-validation (line 89-91).
  • Security: splitTopLevelGeometryItemsStrict tracks parenthesis depth, rejects unbalanced/empty items. maxGeometryCollectionNestingDepth=64 caps recursion depth. maxPointsInGeometry defaults to 65536. No injection risk — the metadata string is always from the catalog Enumvalues field.
  • GEOMETRY column (base type) correctly accepts all subtypes at line 63: columnSubtype != "GEOMETRY" means a GEOMETRY column with no explicit subtype doesn't reject any valid geometry.

Test coverage

  • func_geometry_test.go (759 lines): 11 test functions covering subtype match/reject, SRID match/mismatch/assign, malformed WKT, NULL handling, all geometry types (MULTIPOINT, MULTILINESTRING, MULTIPOLYGON, GEOMETRYCOLLECTION), point count limits, and helper functions.
  • mysql_special_types_test.go (152 lines): covers funcCastForGeometryType, isGeometryNullLiteralExpr, type predicates, and subtype normalization.

Minor observation (not blocking)

validateFiniteCoordinatesInGeometryText tokenizes the entire WKT and attempts ParseFloat on every token including type names ("POINT", "LINESTRING"). Non-numeric tokens fail ParseFloat but the function only rejects when err == nil && (IsNaN || IsInf) — so type-name tokens are silently skipped. This is functionally correct but slightly wasteful. A prefix check for digit/sign/dot before ParseFloat would avoid the failed parse attempts. Not blocking — it's validation-path, not hot-loop.

CI still running (SCA + checks passed). Merge after green.

@mergify

mergify Bot commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-05-12 14:16 UTC · Rule: release-3.0
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-05-12 15:16 UTC · at f0e8dd7cee511ce5f6a8fde40cfaba2d186a5c5e · squash

This pull request spent 1 hour 8 seconds in the queue, including 59 minutes 38 seconds running CI.

Required conditions to merge
  • #approved-reviews-by >= 1 [🛡 GitHub branch protection]
  • #changes-requested-reviews-by = 0 [🛡 GitHub branch protection]
  • branch-protection-review-decision = APPROVED [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI (3.0) / Coverage
    • check-neutral = Matrixone Utils CI (3.0) / Coverage
    • check-skipped = Matrixone Utils CI (3.0) / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI (3.0) / SCA Test on Ubuntu/x86
    • check-neutral = Matrixone CI (3.0) / SCA Test on Ubuntu/x86
    • check-skipped = Matrixone CI (3.0) / SCA Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI (3.0) / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI (3.0) / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI (3.0) / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-neutral = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-skipped = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(Optimistic/PUSH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-neutral = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI (3.0) / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI (3.0) / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-neutral = Matrixone Standlone CI (3.0) / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI (3.0) / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-neutral = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI (3.0) / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Upgrade CI (3.0) / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-neutral = Matrixone Upgrade CI (3.0) / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI (3.0) / Compatibility Test With Target on Linux/x64(LAUNCH)

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants