Repository navigation
Description
Description
SRID 4326 的 ST_Distance、ST_Length 和 ST_Area 未验证 longitude/latitude 的有效范围。经度 181°、纬度 91° 会被 S2 球面内核规范化后返回看似有效的距离、长度和面积,而不是拒绝无效地理坐标。
同一代码库中的 ST_Distance_Sphere 已明确要求 longitude [-180,180]、latitude [-90,90],但 SRID 4326 的通用测量路径绕过了该校验。
Environment
- Branch:
main - Commit:
e7bb0572235ec4bac81eb098eb0ad8900f0065ab - Deployment: macOS arm64,单节点 MatrixOne,默认 launch 配置
- Date: 2026-09-05
Steps to reproduce
SELECT ST_Distance(
ST_GeomFromText('POINT(181 0)', 4326),
ST_GeomFromText('POINT(0 0)', 4326)
);
SELECT ST_Distance(
ST_GeomFromText('POINT(0 91)', 4326),
ST_GeomFromText('POINT(0 0)', 4326)
);
SELECT ST_Length(ST_GeomFromText('LINESTRING(0 0,181 0)', 4326));
SELECT ST_Area(
ST_GeomFromText('POLYGON((0 0,1 0,1 91,0 91,0 0))', 4326)
);
-- control: the sibling spherical-distance function rejects the same domains
SELECT ST_Distance_Sphere(ST_GeomFromText('POINT(181 0)'), ST_GeomFromText('POINT(0 0)'));
SELECT ST_Distance_Sphere(ST_GeomFromText('POINT(0 91)'), ST_GeomFromText('POINT(0 0)'));Actual behavior
distance from POINT(181 0) = 19903919.361802418
distance from POINT(0 91) = 10118752.301251495
length = 19903919.361802388
area = 708316943627.4152
ST_Distance_Sphere longitude 181 -> explicit out-of-range error
ST_Distance_Sphere latitude 91 -> explicit out-of-range error
Expected behavior
SRID 4326 的 longitude 必须在 [-180,180],latitude 必须在 [-90,90],且坐标必须有限。越界坐标不应被球面内核隐式折叠到另一个地理位置;构造或进入 geodetic measurement kernel 时应返回明确错误。
Stability and controls
- 四个越界测量场景各重复 3 次,返回数值稳定。
- 合法控制:赤道 1° 距离约 111195.08 m;反经线
179° -> -179°取约 222390.16 m 的短弧;顺/逆时针 1° 方形面积相同。 ST_Distance_Sphere对完全相同的 181°/91° 越界域稳定拒绝,证明已有可复用的范围规则。- NaN WKT 被存储校验拒绝,问题集中在“有限但越界”的坐标。
Evidence and code analysis
- Exact main commit: e7bb057
- Geodetic area/length wrappers decode and call the S2-backed kernel without coordinate validation:
matrixone/pkg/sql/plan/function/func_unary.go
Lines 1661 to 1692 in e7bb057
// geodeticArea returns the WGS 84 geodesic area (square meters) of a polygon or // multipolygon. func geodeticArea(payload []byte) (float64, error) { typeName, err := geometryTypeNameFromPayload(payload) if err != nil { return 0, err } if typeName != "POLYGON" && typeName != "MULTIPOLYGON" { return 0, moerr.NewInvalidInputNoCtx("geometry is not a POLYGON or MULTIPOLYGON") } g, err := decodeGeoGeometry(payload) if err != nil { return 0, err } return geo.AreaSquareMeters(g), nil } // geodeticLength returns the WGS 84 geodesic length (meters) of a linestring or // multilinestring. func geodeticLength(payload []byte) (float64, error) { typeName, err := geometryTypeNameFromPayload(payload) if err != nil { return 0, err } if typeName != "LINESTRING" && typeName != "MULTILINESTRING" { return 0, moerr.NewInvalidInputNoCtx("geometry is not a LINESTRING or MULTILINESTRING") } g, err := decodeGeoGeometry(payload) if err != nil { return 0, err } return geo.LengthMeters(g), nil - SRID 4326 distance dispatch likewise has no coordinate-domain guard:
matrixone/pkg/sql/plan/function/func_binary.go
Lines 10114 to 10124 in e7bb057
// geometryDistanceBySRID computes the distance between two geometries in the // coordinate system selected by srid: geodesic meters for SRID 4326, Cartesian // otherwise. func geometryDistanceBySRID(left, right []byte, srid uint32) (float64, error) { if err := validateComputationSRID(srid); err != nil { return 0, err } if srid == geo.SRIDWGS84 { return geodeticDistance(left, right) } return geometryDistance(left, right) ST_Distance_Sphererange validation already implemented:matrixone/pkg/sql/plan/function/func_binary.go
Lines 10074 to 10111 in e7bb057
return 0, moerr.NewInvalidInputNoCtx("invalid geometry payload") } return T(d), nil }, selectList) } // validateDistanceSphereGeometry enforces the ST_Distance_Sphere input contract: // only POINT and MULTIPOINT geometries are accepted, and every coordinate must // lie within longitude [-180, 180] and latitude [-90, 90]. func validateDistanceSphereGeometry(g geo.Geometry) error { checkCoord := func(x, y float64) error { if x < -180 || x > 180 { return moerr.NewInvalidInputNoCtxf("ST_DISTANCE_SPHERE longitude %v is out of range [-180, 180]", x) } if y < -90 || y > 90 { return moerr.NewInvalidInputNoCtxf("ST_DISTANCE_SPHERE latitude %v is out of range [-90, 90]", y) } return nil } switch v := g.(type) { case geo.Point: if v.IsEmpty { return nil } return checkCoord(v.X, v.Y) case geo.MultiPoint: for _, p := range v.Points { if p.IsEmpty { continue } if err := checkCoord(p.X, p.Y); err != nil { return err } } return nil default: return moerr.NewInvalidInputNoCtx("ST_DISTANCE_SPHERE only supports POINT and MULTIPOINT inputs") }
Regression coverage
修复时应覆盖 Point/LineString/Polygon/Multi* 的合法边界、刚越界、NaN/Inf;覆盖 ST_Distance、ST_Length、ST_Area 的类型 SRID 与显式 SRID overload,以及 geography 列、常量、表列和 Prepared Statement 路径。
Activity
XuPeng-SH commentedon Sep 13, 2026
修复已提交为 PR #28815,commit
5f6603fe2bc6c26b5e8da52d5c935a04f57e8fcb。Root cause: SRID 4326 的
ST_Distance、ST_Length、ST_Area把坐标交给 S2;s2.LatLngFromDegrees会规范化有限但越界的经纬度,measurement wrapper 之前没有在 S2 前拒绝它们。因此查询会返回看似有效但错误的数值。基线 commit67a9a4b130388956971888a67fc626784deade48上,issue 的四个 SQL-evaluator regression 都稳定返回成功而非错误。**修复边界:*新增共享经纬度遍历校验,在有效 SRID 为 4326 的 area/length/distance 内核调用前执行;Distance 左右操作数在 containment/零距离 shortcut 前都完整校验。坐标必须有限,longitude 在
[-180,180]、latitude 在[-90,90]。显式 SRID 仍覆盖列/值类型:SRID 0 仍走 Cartesian;构造/存储不提前拒绝坐标;ST_DISTANCE_SPHERE继续使用其独立 Point/MultiPoint 契约。覆盖 GEOMETRY32、holes/Multi、NULL/masked/zero-row、empty sentinel、边界和早退反例。验证:
pkg/geo全包 UT、pkg/sql/plan/function全包 CGo UT、go vet、增量 golangci-lint(0 issues)、gofmt 和 diff 检查均通过。临时 M4 microbenchmark 测得校验额外耗时约 point distance 1%、4096 点 line length 5.3%、4096 点 polygon area 2%,额外分配为 0。新增 SQL BVT 覆盖常量、显式 SRID、GEOGRAPHY/GEOGRAPHY32 列及 prepared valid→invalid→valid;没有针对其他版本的本地服务运行 BVT,需由 PR CI 验证。创建 PR 时 CI preflight 已通过,shared build 正在运行,UT/SCA 已排队;尚未等待 CI 结果。GPT‑6 medium 设计门和最终精确 diff 源码审查均为 PASS(最终 diff SHA-256
9cdf1bfe3e78670737fea26ff9c8b86285910f62e11ab281a48c3729500f292e;调用配置gpt-6-astra,medium,审查 agent 不能独立认证运行时变体/effort)。请在 exact-head CI/BVT 通过后再验收并关闭 issue。fix(geo): reject out-of-range geodetic coordinates (#28815)
Ariznawlll commentedon Sep 14, 2026
测试版本
原问题验证
Happy Path 覆盖
Unhappy Path 覆盖
白盒验证
回归测试
测试证据
测试结论
测试通过。
fix(geo): reject out-of-range geodetic coordinates (matrixorigin#28815)
fix(geo): reject out-of-range geodetic coordinates (matrixorigin#28815)
fix(geo): reject out-of-range geodetic coordinates (matrixorigin#28815)