Skip to content

[DO NOT SQUASH MERGE] Geospatial functions (STGeometryType, Parse, STNumPoints, MakeValid)#4667

Open
Gopalverma062 wants to merge 4 commits intobabelfish-for-postgresql:BABEL_6_X_DEVfrom
Gopalverma062:BABEL_6_X_STGEOMETRYTYPE
Open

[DO NOT SQUASH MERGE] Geospatial functions (STGeometryType, Parse, STNumPoints, MakeValid)#4667
Gopalverma062 wants to merge 4 commits intobabelfish-for-postgresql:BABEL_6_X_DEVfrom
Gopalverma062:BABEL_6_X_STGEOMETRYTYPE

Conversation

@Gopalverma062
Copy link
Contributor

Description

This PR cherry-picks four geospatial feature commits from the BABEL_5_X development branch into BABEL_6_X.

Currently, Babelfish on the 6.x branch does not support several T-SQL geospatial methods (STGeometryType(), Parse(), STNumPoints(), MakeValid()) for sys.GEOMETRY and sys.GEOGRAPHY types.

With this change, the following geospatial functions are now available on the BABEL_6_X branch:

  1. STGeometryType() — Returns the OGC type name (Point, LineString, Polygon, etc.) for geometry and geography instances. Strips the ST_ prefix from PostGIS output to match T-SQL format.
  2. Parse() — Parses WKT (Well-Known Text) string representations into geometry and geography instances, matching T-SQL GEOMETRY::Parse() and GEOGRAPHY::Parse() behavior.
  3. STNumPoints() — Returns the total number of points in a geometry or geography instance, consistent with T-SQL S behavior.
  4. MakeValid() — Converts invalid geometry and geography instances into valid ones, leveraging PostGIS ST_MakeValid with T-SQL-compatible behavior.

Why: These are essential T-SQL geospatial methods required for SQL compatibility. Cherry-picking into BABEL_6_X ensures the 6.x release branch includes this geospatial functionality.

Cherry-picked commits:

  • Implement STGeometryType for geometry and geography types (#4509)
  • Implementation of parse geography and geometry datatype (#4518)
  • Supported PostGIS Function: STNumPoints for Geospatial datatypes (#4510)
  • MakeValid implementation for geometry and geography (#4513)

Issues Resolved

  • BABEL-6310 — Implement STGeometryType ,Parse, STNumPoints, and MakeValid implementations
    for geometry and geography types

Test Scenarios Covered

  • Use case based —

    • STGeometryType() on all OGC types (Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection)
    • Parse() with valid WKT strings for both geometry and geography
    • STNumPoints() on single and multi-part geometries/geographies
    • MakeValid() on invalid geometries (e.g., self-intersecting polygons, bowtie polygons)
  • Boundary conditions —

    • NULL inputs return NULL for all functions
    • Empty geometries handled correctly
    • Geometries with zero points for STNumPoints()
  • Arbitrary inputs —

    • Various valid WKT representations across all supported geometry types
  • Negative test cases —

    • Invalid or malformed WKT strings for Parse()
    • Calling functions on non-spatial types rejected appropriately
  • Minor version upgrade tests —

    • Verified compatibility on BABEL_6_X branch
  • Major version upgrade tests —

    • N/A (cherry-pick of already tested commits)
  • Performance tests —

    • N/A (lightweight wrappers around PostGIS functions)
  • Tooling impact —

    • No tooling changes required
  • Client tests —

    • Output verified to match T-SQL Server behavior for all four functions

Check List

  • Commits are signed per the DCO using --signoff

Signed-off-by: gopalgv <[email protected]>

By submitting this pull request, I confirm that my contribution is under the terms of the Apache 2.0 and PostgreSQL licenses, and grant any person obtaining a copy of the contribution permission to relicense all or a portion of my contribution to the PostgreSQL License solely to contribute all or a portion of my contribution to the PostgreSQL open source project.

Gopalverma062 and others added 4 commits March 19, 2026 06:08
…for-postgresql#4509)

Implemented the STGeometryType() method for both sys.GEOMETRY and sys.GEOGRAPHY types, extending the existing geospatial functionality in Babelfish. This function returns the geometry type name (Point, LineString, Polygon, etc.) with full TSQL compatibility.
Changes Made

Function Implementations:

    STGeometryType for sys.GEOMETRY - Returns geometry type name with proper validation
    STGeometryType for sys.GEOGRAPHY - Returns geography type name with same behavior
    Utilizes PostGIS functions with necessary adjustments for TSQL compatibility
    Strips 'ST_' prefix from PostGIS type names to match TSQL Server format

Task: BABEL-6310

Signed-off by: gopalgv [email protected]
…-postgresql#4518)

Implemented Parse function support for GeoSpatial TSQL data types that was previously unsupported in Babelfish. The Parse function has been added for both geometry and geography data types.

Changes include:

Implemented geometry::Parse() and geography::Parse() functions by utilizing PostGIS parsing capabilities with necessary adjustments to ensure TSQL compatibility. Added Parse function definitions to the TSQL parser to recognize these new static methods. Ensured proper syntax handling for Parse function, including NVARCHAR parameter types and return values.

Examples:

DECLARE @geomText NVARCHAR(MAX);
SET @geomText = 'POINT(1.0 2.0)';
SELECT geometry::Parse(@geomText).STAsText() AS ParsedGeometry;
GO

ParsedGeometry
--------------
POINT(1 2)


DECLARE @geogText NVARCHAR(MAX);
SET @geogText = 'LINESTRING(0 0, 1 1, 2 2)';
SELECT geography::Parse(@geogText).STAsText() AS ParsedGeography;
GO

ParsedGeography
---------------
LINESTRING(0 0,1 1,2 2)

Task: BABEL-6310

Signed-off by: Gopalgv [email protected]
…elfish-for-postgresql#4510)

Implemented the STNumPoints GeoSpatial TSQL function that was previously unsupported in Babelfish.

BABEL-6329

---------

Co-authored-by: Gopal Verma <[email protected]>
…stgresql#4513)

Implemented the MakeValid GeoSpatial TSQL function that was previously unsupported in Babelfish.

Added sys.MakeValid(geom sys.GEOMETRY) function in geometry.sql
Added sys.MakeValid(geog sys.GEOGRAPHY) function in geography.sql

Delegates to respective helper functions for processing invalid geometries. Functions are marked as IMMUTABLE STRICT PARALLEL SAFE for optimal performance.

Task: BABEL-6310

Signed-off-by: Gopal Verma <[email protected]>
@Gopalverma062 Gopalverma062 changed the title Babel 6 x stgeometrytype Cherry-pick: Geospatial functions (STGeometryType, Parse, STNumPoints, MakeValid) Mar 19, 2026
@Gopalverma062 Gopalverma062 changed the title Cherry-pick: Geospatial functions (STGeometryType, Parse, STNumPoints, MakeValid) Geospatial functions (STGeometryType, Parse, STNumPoints, MakeValid) Mar 19, 2026
@Gopalverma062 Gopalverma062 changed the title Geospatial functions (STGeometryType, Parse, STNumPoints, MakeValid) [DO NOT SQUASH MERGE] Geospatial functions (STGeometryType, Parse, STNumPoints, MakeValid) Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant