Repository navigation
Closed
Copy link
Description
Description
ST_FrechetDistance 和 ST_HausdorffDistance 忽略 geometry 的 SRID 4326,始终用平面 math.Hypot 计算坐标差。地理坐标相差 1° 时,两者返回 1,与 SRID 0 的结果完全相同,而正确结果应使用地理 SRS 的长度单位,约为 111 km。
Environment
- Branch:
main - Commit:
e7bb0572235ec4bac81eb098eb0ad8900f0065ab - Deployment: macOS arm64,单节点 MatrixOne,默认 launch 配置
- Date: 2026-09-05
Steps to reproduce
SELECT ST_FrechetDistance(
ST_GeomFromText('LINESTRING(0 0,1 0)', 4326),
ST_GeomFromText('LINESTRING(0 1,1 1)', 4326)
);
SELECT ST_HausdorffDistance(
ST_GeomFromText('LINESTRING(0 0,1 0)', 4326),
ST_GeomFromText('LINESTRING(0 1,1 1)', 4326)
);
-- Cartesian control
SELECT ST_FrechetDistance(
ST_GeomFromText('LINESTRING(0 0,1 0)', 0),
ST_GeomFromText('LINESTRING(0 1,1 1)', 0)
);Actual behavior
Frechet, SRID 4326 = 1
Hausdorff, SRID 4326 = 1
Frechet, SRID 0 = 1
Expected behavior
SRID 4326 应走 geodetic distance kernel,并按 SRS 长度单位返回结果。MySQL 8.3.0 对两个 4326 查询均返回 111319.49079326246,SRID 0 控制仍返回 1。
Stability and controls
- 常量表达式各重复 3 次,结果稳定。
- 使用两个
geometry srid 4326typed columns 存储相同 LineString 后再次计算,MO 仍返回1,MySQL 仍返回111319.49079326246。 - SRID 0 平面场景 MO 与 MySQL 均为 1。
ST_Distance、ST_Length已对 SRID 4326 分派到测地内核,说明离散距离是漏接 SRID 分派,而不是系统统一采用角度单位。
Evidence and code analysis
- Exact main commit: e7bb057
- SQL wrappers decode geometry but never inspect type SRID:
matrixone/pkg/sql/plan/function/func_binary.go
Lines 9728 to 9784 in e7bb057
// StFrechetDistance returns the discrete Fréchet distance (planar) between two // geometries' vertex sequences. func StFrechetDistance(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int, selectList *FunctionSelectList) error { return stFrechetDistance[float64](ivecs, result, proc, length, selectList) } // StFrechetDistance32 is the GEOMETRY32 overload of ST_FrechetDistance. func StFrechetDistance32(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int, selectList *FunctionSelectList) error { return stFrechetDistance[float32](ivecs, result, proc, length, selectList) } func stFrechetDistance[T float32 | float64](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int, selectList *FunctionSelectList) error { return opBinaryBytesBytesToFixedWithErrorCheck[T](ivecs, result, proc, length, func(v1, v2 []byte) (T, error) { a, err := decodeGeoGeometry(v1) if err != nil { return 0, err } b, err := decodeGeoGeometry(v2) if err != nil { return 0, err } d, ok := geo.FrechetDistance(a, b) if !ok { return 0, moerr.NewInvalidInputNoCtx("ST_FrechetDistance: empty geometry") } return T(d), nil }, selectList) } // StHausdorffDistance returns the discrete Hausdorff distance (planar) between // two geometries' vertex sets. func StHausdorffDistance(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int, selectList *FunctionSelectList) error { return stHausdorffDistance[float64](ivecs, result, proc, length, selectList) } // StHausdorffDistance32 is the GEOMETRY32 overload of ST_HausdorffDistance. func StHausdorffDistance32(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int, selectList *FunctionSelectList) error { return stHausdorffDistance[float32](ivecs, result, proc, length, selectList) } func stHausdorffDistance[T float32 | float64](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int, selectList *FunctionSelectList) error { return opBinaryBytesBytesToFixedWithErrorCheck[T](ivecs, result, proc, length, func(v1, v2 []byte) (T, error) { a, err := decodeGeoGeometry(v1) if err != nil { return 0, err } b, err := decodeGeoGeometry(v2) if err != nil { return 0, err } d, ok := geo.HausdorffDistance(a, b) if !ok { return 0, moerr.NewInvalidInputNoCtx("ST_HausdorffDistance: empty geometry") } return T(d), nil }, selectList) } - Both core algorithms use planar
math.Hypot:Lines 26 to 76 in e7bb057
// HausdorffDistance returns the discrete Hausdorff distance (planar) between the // vertex sets of two geometries: the larger of the two directed distances. func HausdorffDistance(a, b Geometry) (float64, bool) { pa, pb := coordsOf(a), coordsOf(b) if len(pa) == 0 || len(pb) == 0 { return 0, false } return math.Max(directedHausdorff(pa, pb), directedHausdorff(pb, pa)), true } func directedHausdorff(pa, pb []Coord) float64 { maxMin := 0.0 for _, a := range pa { minD := math.Inf(1) for _, b := range pb { if d := math.Hypot(a.X-b.X, a.Y-b.Y); d < minD { minD = d } } if minD > maxMin { maxMin = minD } } return maxMin } // FrechetDistance returns the discrete Fréchet distance (planar) between the // vertex sequences of two geometries, via the Eiter-Mannila dynamic program. func FrechetDistance(a, b Geometry) (float64, bool) { pa, pb := coordsOf(a), coordsOf(b) n, m := len(pa), len(pb) if n == 0 || m == 0 { return 0, false } ca := make([][]float64, n) for i := range ca { ca[i] = make([]float64, m) } for i := 0; i < n; i++ { for j := 0; j < m; j++ { d := math.Hypot(pa[i].X-pb[j].X, pa[i].Y-pb[j].Y) switch { case i == 0 && j == 0: ca[i][j] = d case i == 0: ca[i][j] = math.Max(ca[0][j-1], d) case j == 0: ca[i][j] = math.Max(ca[i-1][0], d) default: prev := math.Min(ca[i-1][j], math.Min(ca[i-1][j-1], ca[i][j-1])) ca[i][j] = math.Max(prev, d) - Existing SRID-aware distance dispatch for comparison:
matrixone/pkg/sql/plan/function/func_binary.go
Lines 10114 to 10144 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) } func StDistance(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int, selectList *FunctionSelectList) error { return stDistance[float64](ivecs, result, proc, length, selectList) } // StDistance32 is the GEOMETRY32 overload of ST_Distance (returns float32). func StDistance32(ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int, selectList *FunctionSelectList) error { return stDistance[float32](ivecs, result, proc, length, selectList) } func stDistance[T float32 | float64](ivecs []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int, selectList *FunctionSelectList) error { if err := checkBinaryGeometryTypeSRID("ST_DISTANCE", ivecs); err != nil { return err } srid := sridFromTypeWidth(ivecs[0].GetType().Width) return opBinaryBytesBytesToFixedWithErrorCheck[T](ivecs, result, proc, length, func(v1, v2 []byte) (T, error) { d, err := geometryDistanceBySRID(v1, v2, srid) return T(d), err }, selectList)
Regression coverage
修复时应覆盖 SRID 0/4326 的 LineString,赤道/高纬度/反经线、参数交换、typed columns、GEOMETRY32、越界经纬度,以及与 ST_Distance 的单位一致性。
Metadata
Metadata
Assignees
Labels
kind/bugSomething isn't working
Activity
fix(spatial): support geodetic discrete distance units (#28830)
Ariznawlll commentedon Sep 21, 2026
Verified on current main 57c280e.\n\nST_FrechetDistance and ST_HausdorffDistance now dispatch SRID 4326 inputs to the geodetic path rather than treating them as planar coordinates. The one-degree LINESTRING fixture returns 111195.080234 m, while the legacy SRID-0 operation remains planar. Three MOTR runs passed.\n\nThe implemented geodetic model is intentionally spherical, so its numeric result differs from MySQL's ellipsoidal reference; that model choice is documented and outside this issue's original SRID-dispatch defect.\n\nCoverage: matrixorigin/motr#193,\nsuites/14_issue_regression/t/issue_28200_28194_geodetic_distance_units.test.\n\nClosing as fixed.