Skip to content

[Bug]: ST_FrechetDistance and ST_HausdorffDistance ignore geographic SRID 4326 #28194

Closed

Description

@Ariznawlll

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。

Reference: https://dev.mysql.com/doc/refman/8.0/en/spatial-relation-functions-object-shapes.html

Stability and controls

  • 常量表达式各重复 3 次,结果稳定。
  • 使用两个 geometry srid 4326 typed 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:
    // 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:
    // 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:
    // 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 的单位一致性。

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. added a commit that references this issue on Sep 20, 2026
    5e1e93a
  6. Ariznawlll commented on Sep 21, 2026

    @Ariznawlll
    CollaboratorAuthor

    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.

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