Skip to content

Supported PostGIS Function: STNumPoints for Geospatial datatypes#4510

Merged
jsudrik merged 18 commits intobabelfish-for-postgresql:BABEL_5_X_DEVfrom
Gopalverma062:BABEL_6310_STNUMPOINTS
Mar 3, 2026
Merged

Supported PostGIS Function: STNumPoints for Geospatial datatypes#4510
jsudrik merged 18 commits intobabelfish-for-postgresql:BABEL_5_X_DEVfrom
Gopalverma062:BABEL_6310_STNUMPOINTS

Conversation

@Gopalverma062
Copy link
Contributor

@Gopalverma062 Gopalverma062 commented Feb 1, 2026

Implement STNumPoints GeoSpatial SQL Function

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


What's Changed

Function Implementation

  • Added sys.STNumPoints(geog sys.GEOGRAPHY) function in geography.sql
  • Delegates to sys.STNumPoints_geography_helper() for valid, non-empty instances
  • Function is marked as IMMUTABLE STRICT PARALLEL SAFE for optimal performance

Error Handling

  • Returns exception for invalid geography instances
  • Returns 0 for empty geography instances
  • Returns actual point count for valid instances

Function Behavior

Input Output
Valid geography instance Integer count of points
Empty geography instance 0
Invalid geography instance Exception

Examples

Point

DECLARE @point geography;
SET @point = geography::STPointFromText('POINT(-122.34900 47.65100)', 4326);
SELECT @point.STNumPoints() AS NumPoints;
-- Output: 1

LineString

DECLARE @line geography;
SET @line = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656, -122.310 47.690)', 4326);
SELECT @line.STNumPoints() AS NumPoints;
-- Output: 3

Polygon

DECLARE @polygon geography;
SET @polygon = geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326);
SELECT @polygon.STNumPoints() AS NumPoints;
-- Output: 5

Empty Instance

DECLARE @empty geography;
SET @empty = geography::STGeomFromText('POINT EMPTY', 4326);
SELECT @empty.STNumPoints() AS NumPoints;
-- Output: 0

Issues Resolved

Task: BABEL-6329

Signed-off by: Gopal verma [email protected]

Authored by: Gopal verma [email protected]


Test Coverage

  • Use case based
  • Boundary conditions
  • Arbitrary inputs
  • Negative test cases

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
Collaborator

coveralls commented Feb 2, 2026

Pull Request Test Coverage Report for Build 22109069228

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


geom_type := ST_GeometryType(geom);

IF geom_type = 'ST_Point' THEN
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean Postgis is not able to handle empty geometry and Point?

Copy link
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


IF geom_type = 'ST_Point' THEN
RETURN 1;
ELSIF geom_type IN ('ST_LineString', 'ST_Polygon', 'ST_MultiLineString', 'ST_MultiPolygon') THEN
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this condition because postgis only supports these geometries or is it that we support these for now? If later than IMO we should instead directly call STNumPoints_helper for all geometry types as otherwise we would need to update this function each time we support a new geometry type.

Copy link
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

Copy link
Contributor

Choose a reason for hiding this comment

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

IMO, we should create a new set of test files now, something like Test-spatial-functions-3-vu-*

Copy link
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 created

AS $$
BEGIN
IF sys.STIsValid(geog) = 0 THEN
RAISE EXCEPTION 'The geography instance is not valid. Use MakeValid() to convert to valid geography.';
Copy link
Contributor

Choose a reason for hiding this comment

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

The error message is not same as in geography.sql. Please check!

Copy link
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

Function sys.stintersects_helper(sys.geometry,sys.geometry)
Function sys.stisclosed_helper(sys.geography)
Function sys.stisclosed_helper(sys.geometry)
Function sys.stnumpoints_helper(sys.geography)
Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove this by creating a dependency for STNumPoints function eg. CREATE VIEW. Please verify this for all functions.

BEGIN
IF sys.STIsValid(geog) = 0 THEN
RAISE EXCEPTION 'The geography instance is not valid';
ELSE
Copy link
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 geography is empty or not?

Copy link
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

IF STIsValid(geom) = 0 THEN
RAISE EXCEPTION 'The geometry instance is not valid';
ELSE
RETURN sys.STNumPoints_helper(geom);
Copy link
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 geometry is empty or not?

Copy link
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

@@ -0,0 +1,42 @@
CREATE DATABASE TestSTNumPoints_DB;
Copy link
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This allows us to add JDBC test directives (like prepst#!# for prepared statement testing)


USE TestSTNumPoints_DB;

USE MASTER
Copy link
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 TestSTNumPoints_DB? We are simply creating the db and not using it. All the objects are created inside master db. So what's the point?

Copy link
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,161 @@
--STNumPoints()
Copy link
Contributor

@ahmed-shameem ahmed-shameem Feb 16, 2026

Choose a reason for hiding this comment

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

Please add some comments on the test cases as to what we are testing. For eg: invalid input, null input, empty input, negative tests etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done Added comments

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

USE MASTER
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, what's the point of creating TestSTNumPoints_DB?

Copy link
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.

rishabhtanwar29
rishabhtanwar29 previously approved these changes Mar 2, 2026
Copy link
Contributor

@jsudrik jsudrik left a comment

Choose a reason for hiding this comment

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

Approved.

@jsudrik jsudrik merged commit 4c4798e into babelfish-for-postgresql:BABEL_5_X_DEV Mar 3, 2026
47 checks passed
Gopalverma062 added a commit to Gopalverma062/babelfish_extensions that referenced this pull request Mar 19, 2026
…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]>
Gopalverma062 added a commit to Gopalverma062/babelfish_extensions that referenced this pull request Mar 20, 2026
…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]>
Gopalverma062 added a commit to Gopalverma062/babelfish_extensions that referenced this pull request Mar 20, 2026
…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]>
rishabhtanwar29 pushed a commit that referenced this pull request Mar 20, 2026
…) (#4676)

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

## What's Changed

### Function Implementation
- Added `sys.STNumPoints(geog sys.GEOGRAPHY)` function in `geography.sql`
- Delegates to `sys.STNumPoints_geography_helper()` for valid, non-empty instances
- Function is marked as `IMMUTABLE STRICT PARALLEL SAFE` for optimal performance

### Error Handling
- Returns exception for invalid geography instances
- Returns 0 for empty geography instances
- Returns actual point count for valid instances

---

## Function Behavior

| Input | Output |
|-------|--------|
| Valid geography instance | Integer count of points |
| Empty geography instance | 0 |
| Invalid geography instance | Exception |

---

## Examples

**Point**

    DECLARE @point geography;
    SET @point = geography::STPointFromText('POINT(-122.34900 47.65100)', 4326);
    SELECT @point.STNumPoints() AS NumPoints;
    -- Output: 1

**LineString**

    DECLARE @line geography;
    SET @line = geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656, -122.310 47.690)', 4326);
    SELECT @line.STNumPoints() AS NumPoints;
    -- Output: 3

**Polygon**

    DECLARE @polygon geography;
    SET @polygon = geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326);
    SELECT @polygon.STNumPoints() AS NumPoints;
    -- Output: 5

**Empty Instance**

    DECLARE @empty geography;
    SET @empty = geography::STGeomFromText('POINT EMPTY', 4326);
    SELECT @empty.STNumPoints() AS NumPoints;
    -- Output: 0

Task: BABEL-6329
Signed-off by: Gopal verma <[email protected]>
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