Skip to content

[Bug]: ST_SymDifference creates an invalid polygon and wrong area for collinear partial overlap #28183

Closed

Description

@Ariznawlll

Description

ST_SymDifference 在两个轴对齐矩形部分重叠且共享一段共线边界时,会把本应互不相交的两个剩余区域连接成一个自相交 POLYGON。返回几何无效,且面积不满足对称差恒等式。

Environment

  • Branch: main
  • Commit: e7bb0572235ec4bac81eb098eb0ad8900f0065ab
  • Deployment: macOS arm64,单节点 MatrixOne,默认 launch 配置
  • Date: 2026-09-05

Steps to reproduce

SET @a = ST_GeomFromText('POLYGON((0 0,4 0,4 2,0 2,0 0))');
SET @b = ST_GeomFromText('POLYGON((-2 1,1 1,1 2,-2 2,-2 1))');

SELECT
  ST_AsText(ST_SymDifference(@a, @b)) AS result_wkt,
  ST_Area(ST_SymDifference(@a, @b)) AS actual_area,
  ST_Area(@a) + ST_Area(@b)
    - 2 * ST_Area(ST_Intersection(@a, @b)) AS expected_area,
  ST_IsValid(ST_SymDifference(@a, @b)) AS result_valid;

Actual behavior

result_wkt = POLYGON((-2 1,-2 2,0 2,0 1,0 0,4 0,4 2,1 2,1 1,0 1,-2 1))
actual_area = 5
expected_area = 9
result_valid = 0

结果环在交点附近重复穿越,将两个应独立的区域错误连接为一个无效 Polygon。

Expected behavior

返回有效的两个面区域(通常表示为 MULTIPOLYGON),面积为 9,并满足:

area(A xor B) = area(A) + area(B) - 2 * area(A intersection B)

MySQL 8.3.0 对相同输入返回有效 MULTIPOLYGON,面积为 9。

Stability and controls

  • Reproducer: 原参数顺序和交换参数顺序各执行 3 次,共 6/6 次稳定返回面积 5、ST_IsValid=0。
  • Control: 普通角部重叠矩形 3/3 次返回面积 24,与恒等式一致;互不相交矩形 3/3 次返回面积 8,与恒等式一致。
  • Extended sample: 120 组固定种子的随机矩形对中,19 组 ST_SymDifference 面积与 MySQL 及对称差恒等式不一致;同组输入的 ST_Union 面积 120/120 一致,ST_Intersection 和 ST_Difference 在可比较的面结果上也一致。

Evidence

  • Exact main commit: e7bb057
  • Relevant XOR edge selection:
    func (o *overlay) inResult(e *ovEvent) bool {
    switch e.kind {
    case edgeNormal:
    switch o.op {
    case OpIntersection:
    return !e.otherInOut
    case OpUnion:
    return e.otherInOut
    case OpDifference:
    return (e.subject && e.otherInOut) || (!e.subject && !e.otherInOut)
    case OpXOR:
    return true
    }
    case edgeSameTransition:
    return o.op == OpIntersection || o.op == OpUnion
    case edgeDifferentTransition:
    return o.op == OpDifference
    case edgeNonContributing:
    return false
  • Overlay ring assembly path:
    // Overlay computes a Boolean operation between two areal geometries.
    func Overlay(a, b Geometry, op BoolOp) (Geometry, error) {
    ra, err := polygonRings(a)
    if err != nil {
    return nil, err
    }
    rb, err := polygonRings(b)
    if err != nil {
    return nil, err
    }
    o := &overlay{op: op}
    for _, r := range ra {
    o.addRing(r, true)
    }
    for _, r := range rb {
    o.addRing(r, false)
    }
    // Trivial cases: an empty operand.
    if len(ra) == 0 || len(rb) == 0 {
    switch op {
    case OpUnion, OpXOR:
    if len(ra) == 0 {
    return b, nil
    }
    return a, nil
    case OpDifference:
    return a, nil
    case OpIntersection:
    return Polygon{}, nil
    }
    }
    processed := o.run()
    sortedEvents := make([]*ovEvent, 0, len(processed)*2)
    for _, e := range processed {
    sortedEvents = append(sortedEvents, e, e.other)

Code analysis

ST_SymDifference 进入 geo.Overlay(..., OpXOR)。扫描线阶段把 edgeNormal 全部标记为 XOR 结果边,随后 connectEdges 负责把结果边连接为环。当前最小输入的 Union、Intersection、Difference 均正确,错误集中在 XOR 的结果边连接:两个本应独立的环被连接为包含重复交点的单环。上述代码位置是已确认的数据流;具体需要调整事件分类还是环连接规则仍需实现侧进一步定位。

Regression coverage

修复时应在 pkg/geo/overlay_test.go 增加“部分重叠且一侧共线”的双向参数用例,同时断言:

  1. 对称差结果有效;
  2. 面积恒等式成立;
  3. A xor B 与 B xor A 拓扑等价;
  4. 结果保持为两个互不相交的面组件。

并在 test/distributed/cases/geo/geo_overlay.sql 增加相同 SQL 回归。

Related

Activity

  1. added this to the MO-202609 milestone on Sep 5, 2026
  2. added
    kind/bug
    on Sep 5, 2026
  3. added theissue type on Sep 5, 2026
  4. self-assigned this
    on Sep 5, 2026
  5. XuPeng-SH commented on Sep 13, 2026

    @XuPeng-SH
    Contributor

    Implementation update — PR #28818

    The issue is implemented in PR #28818.

    The source-level root cause is in result-boundary assembly, not XOR edge selection: the old connectEdges/nextPos chose a successor by sweep-event order at a multi-edge vertex. For this input it crossed the point-touching result sectors and joined two polygon components into one self-intersecting polygon (area 5 instead of 9).

    The replacement orients selected edges with result interior on the left, pairs branch incidences by angular order, enforces one-to-one successor/predecessor and closed-cycle invariants, and handles repeated branch vertices without synthetic closure. It keeps a no-sort path for ordinary degree-two vertices.

    Validation on the PR head:

    • GOWORK=off go test -mod=readonly -count=1 -timeout=180s ./pkg/geo — PASS.
    • molint (go vet with the repository vettool) — PASS; incremental golangci-lint — PASS, 0 issues; git diff --check — PASS.
    • UTs cover both operand orders, ring reversal/rotation, a sheared equivalent, topology and area invariants, shared/touching boundaries, holes, multipolygons, and 120 deterministic rectangle pairs.
    • The public SQL BVT checks both operand orders, MULTIPOLYGON, two components, area 9, and validity of each polygon component. It was added but not run locally because this host has another active MatrixOne version; that service was not contacted or modified.
    • Apple M4 synthetic union benchmarks show about 3–9% higher latency across the short runs, while allocated bytes and allocation counts are slightly lower. The exact measurements and limitations are recorded in the PR body.
    • GPT-6 Astra medium design review: GO. GPT-6 Astra medium final review: PASS on diff SHA-256 73b72f78e7cca902781894a753d0b2c10e103f9d5f93f197237a5cdf01bd1321.

    At the latest check, PR preflight passed, but MatrixOne ALL CI was still pending/in progress and CodeQL Go/C++/Java analysis had not completed. No overall CI pass is claimed or awaited here.

  6. added a commit that references this issue on Sep 13, 2026
    6b948a9
  7. Ariznawlll commented on Sep 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    原问题验证

    • ST_SymDifference 对共线部分重叠不再产生非法 polygon 或错误面积。

    Happy Path 覆盖

    • collinear partial-overlap、boundary-contact 与矩形不变量均通过。

    Unhappy Path 覆盖

    • 覆盖接触边界及随机矩形不变量,避免退化 overlay。

    白盒验证

    • pkg/geo TestOverlaySymDifferenceCollinearPartialOverlap、BoundaryContacts、RectangleInvariants 连续 3 次通过。

    回归测试

    测试证据

    • 独立环境三轮输出字节一致;服务日志未见 panic/hang,测试后已清理服务。

    测试结论

    测试通过。

  8. added 3 commits that reference this issue on Sep 17, 2026
    925d997
    df41c59
    71ca737
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions