Skip to content

Implement STGeometryType for geometry and geography types#4509

Merged
shardgupta merged 6 commits intobabelfish-for-postgresql:BABEL_5_X_DEVfrom
Gopalverma062:BABEL_6310_STGEOMETRYTYPE
Feb 23, 2026
Merged

Implement STGeometryType for geometry and geography types#4509
shardgupta merged 6 commits intobabelfish-for-postgresql:BABEL_5_X_DEVfrom
Gopalverma062:BABEL_6310_STGEOMETRYTYPE

Conversation

@Gopalverma062
Copy link
Copy Markdown
Contributor

@Gopalverma062 Gopalverma062 commented Feb 1, 2026

Description

Implement STGeometryType for Geometry and Geography Types**

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

Key Features:

  • Input Validation: Validates geometry/geography instances using STIsValid() before processing
  • Error Handling: Throws appropriate exceptions for invalid instances
  • SQL Server Compatibility: Returns sys.NVARCHAR type and matches TSQL naming conventions
  • NULL Handling: Proper handling of NULL inputs with STRICT function attribute
  • Thread Safety: Implemented with PARALLEL SAFE attribute

Examples

For STGeometryType (Geometry):

DECLARE @geom geometry;
SET @geom = geometry::STGeomFromText('POINT(1 2)', 4326);
SELECT @geom.STGeometryType();

Output: Point

For STGeometryType (Geography):

DECLARE @geog geography;
SET @geog = geography::STGeomFromText('LINESTRING(-122.36 47.65, -122.34 47.66)', 4326);
SELECT @geog.STGeometryType();

Output: LineString

Table Operations:

SELECT ID, GeomColumn.STGeometryType() AS GeometryType 
FROM TestTable 
WHERE GeomColumn.STGeometryType() = 'Point';

Files Modified

  • geometry.sql - Added STGeometryType for geometry type
  • geography.sql - Added STGeometryType for geography type
  • spatial_types--5.5.0--5.6.0.sql - Upgrade script
  • Test files with comprehensive coverage

Issues Resolved

BABEL-6330 - Implement STGeometryType for Geometry and Geography Types

Signed-off by: gopalgv gopalgv@amazon.com

Authored by: gopalgv gopalgv@amazon.com


Checklist

  • Commits are signed per the DCO using --signoff
  • Contribution is under Apache 2.0 and PostgreSQL licenses

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.

@coveralls
Copy link
Copy Markdown
Collaborator

coveralls commented Feb 1, 2026

Pull Request Test Coverage Report for Build 22112542606

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 1081 unchanged lines in 9 files lost coverage.
  • Overall coverage decreased (-0.001%) to 76.99%

Files with Coverage Reduction New Missed Lines %
contrib/babelfishpg_tsql/src/hooks.c 1 85.82%
contrib/babelfishpg_tds/src/backend/tds/tdsresponse.c 2 80.9%
contrib/babelfishpg_tsql/src/session.c 4 96.62%
contrib/babelfishpg_tds/src/backend/tds/tdslogin.c 31 77.42%
contrib/babelfishpg_tsql/src/collation.c 32 80.88%
contrib/babelfishpg_tds/src/include/tds_request.h 36 76.22%
contrib/babelfishpg_tds/src/backend/tds/tdstypeio.c 103 87.44%
contrib/babelfishpg_tsql/src/pl_handler.c 215 90.82%
contrib/babelfishpg_tsql/src/pl_exec.c 657 44.52%
Totals Coverage Status
Change from base Build 21506975134: -0.001%
Covered Lines: 52718
Relevant Lines: 68474

💛 - Coveralls

@rishabhtanwar29 rishabhtanwar29 changed the title BABEL_6310: Implement STGeometryType for geometry and geography types Implement STGeometryType for geometry and geography types Feb 2, 2026

--Geometry
CREATE OR REPLACE FUNCTION sys.STGeometryType(geom sys.GEOMETRY)
RETURNS sys.NVARCHAR
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation says that it should be nvarchar(4000) so we should stick to that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, i have fixed it

RETURN substr(geom_type, 4);
END IF;

RETURN geom_type;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should raise exception in case geom_type is not in 'ST_%' format.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, i have raised the exception

--Geography

CREATE OR REPLACE FUNCTION sys.STGeometryType(geog sys.GEOGRAPHY)
RETURNS sys.NVARCHAR
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, it should be nvarchar(4000)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done ,i have fixed it

RETURN substr(geom_type, 4);
END IF;

RAISE EXCEPTION 'Unexpected geometry type format: %. Expected ST_* prefix.', geom_type;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this ? it can be handled by ST_GeometryType internally right ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rishabh asked me to raise the exception for this

END IF;
geom_type := sys.ST_GeometryType(geog);

IF geom_type LIKE 'ST_%' THEN
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'' is a wildcard in LIKE operator. Using 'ST%' means any string of the form 'ST<any_single_character>' will be valid here. I do not think we want that. Please use something like:

geom_type LIKE 'ST\_%' ESCAPE '\'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have fixed this

IF STIsValid(geog) = 0 THEN
RAISE EXCEPTION 'This operation cannot be completed because the instance is not valid';
END IF;
geom_type := sys.ST_GeometryType(geog);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to check whether it is empty or not?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually it is handled by the c functions

$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

--STGeomType
CREATE OR REPLACE FUNCTION sys.STGeometryType(geom sys.GEOMETRY)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as previous comments

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually it is handled by the c functions

$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

--STGeomType
CREATE OR REPLACE FUNCTION sys.STGeometryType(geog sys.GEOGRAPHY)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the name of the function suggests geometry whereas the input is of type geography?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is named STGeometryType to match T-SQL documentation

@@ -0,0 +1,13 @@
USE TestGeospatialMethods3_DB
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the test file is .txt and not .sql?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, we may add special test directives like prepst#!# thats why it is the .txt not .sql

@@ -0,0 +1,13 @@
USE TestGeospatialMethods3_DB

USE MASTER
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of creating TestGeospatialMethods3_DB db?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed The database now contains the test tables and corresponding tests have been added in the verify file.

@@ -0,0 +1,32 @@
CREATE DATABASE TestGeospatialMethods3_DB;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above^

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have added tables and corrosponding testcases in verify.sql file

@@ -0,0 +1,231 @@
SELECT geometry::STGeomFromText('POINT(1 2)', 4326).STGeometryType();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add comments stating what are we testing: null input, empty input, negative test case, invalid input etc

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes i have mark the comments in test files

@shardgupta shardgupta merged commit b5bc2fa into babelfish-for-postgresql:BABEL_5_X_DEV Feb 23, 2026
47 of 48 checks passed
Gopalverma062 added a commit to Gopalverma062/babelfish_extensions that referenced this pull request Mar 19, 2026
…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 gopalgv@amazon.com
Gopalverma062 added a commit to Gopalverma062/babelfish_extensions that referenced this pull request Mar 19, 2026
…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 gopalgv@amazon.com
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.

6 participants