Repository navigation
Description
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:
Lines 492 to 510 in e7bb057
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:
matrixone/pkg/geo/overlay_connect.go
Lines 293 to 329 in e7bb057
// 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 增加“部分重叠且一侧共线”的双向参数用例,同时断言:
- 对称差结果有效;
- 面积恒等式成立;
A xor B与B xor A拓扑等价;- 结果保持为两个互不相交的面组件。
并在 test/distributed/cases/geo/geo_overlay.sql 增加相同 SQL 回归。
Related
- Existing GIS implementation PR (not a duplicate): Gis geometry engine #24777
Metadata
Metadata
Assignees
Labels
kind/bugSomething isn't working
Activity
XuPeng-SH commentedon Sep 13, 2026
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/nextPoschose 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.go vetwith the repository vettool) — PASS; incremental golangci-lint — PASS, 0 issues;git diff --check— PASS.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.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.
fix(geo): preserve point-touching components in ST_SymDifference (#28818
Ariznawlll commentedon Sep 14, 2026
测试版本
原问题验证
Happy Path 覆盖
Unhappy Path 覆盖
白盒验证
回归测试
测试证据
测试结论
测试通过。
fix(geo): preserve point-touching components in ST_SymDifference (mat…
fix(geo): preserve point-touching components in ST_SymDifference (mat…
fix(geo): preserve point-touching components in ST_SymDifference (mat…