Skip to content

Commit 367ce92

Browse files
Gopalverma062Gopal Verma
andcommitted
Supported PostGIS Function: STNumPoints for Geospatial datatypes (#4510)
Implemented the STNumPoints GeoSpatial TSQL function that was previously unsupported in Babelfish. BABEL-6329 --------- Co-authored-by: Gopal Verma <gopalgv@amazon.com>
1 parent ddfccc9 commit 367ce92

14 files changed

+1748
-115
lines changed

contrib/babelfishpg_common/sql/geography.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,19 @@ CREATE OR REPLACE FUNCTION sys.STDimension(geom sys.GEOGRAPHY)
433433
END;
434434
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
435435

436+
--STNumPoints
437+
CREATE OR REPLACE FUNCTION sys.STNumPoints(geog sys.GEOGRAPHY)
438+
RETURNS integer
439+
AS $$
440+
BEGIN
441+
IF sys.STIsValid(geog) = 0 THEN
442+
RAISE EXCEPTION 'The geography instance is not valid';
443+
ELSE
444+
RETURN sys.STNumPoints_helper(geog);
445+
END IF;
446+
END;
447+
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
448+
436449
--Parse
437450
CREATE OR REPLACE FUNCTION sys.Geography__Parse(geography_tagged_text sys.NVARCHAR)
438451
RETURNS sys.GEOGRAPHY
@@ -665,6 +678,11 @@ CREATE OR REPLACE FUNCTION sys.STDimension_helper(sys.GEOGRAPHY)
665678
AS '$libdir/postgis-3','LWGEOM_dimension'
666679
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;
667680
681+
CREATE OR REPLACE FUNCTION sys.STNumPoints_helper(sys.GEOGRAPHY)
682+
RETURNS integer
683+
AS '$libdir/postgis-3', 'LWGEOM_npoints'
684+
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;
685+
668686
CREATE OR REPLACE FUNCTION sys.STIntersects_helper(geom1 sys.GEOGRAPHY, geom2 sys.GEOGRAPHY)
669687
RETURNS sys.BIT
670688
AS '$libdir/postgis-3','ST_Intersects'

contrib/babelfishpg_common/sql/geometry.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,19 @@ CREATE OR REPLACE FUNCTION sys.Geometry__Parse(geometry_tagged_text sys.NVARCHAR
462462
END;
463463
$$ LANGUAGE plpgsql STRICT IMMUTABLE PARALLEL SAFE;
464464
465+
--STNumPoints
466+
CREATE OR REPLACE FUNCTION sys.STNumPoints(geom sys.GEOMETRY)
467+
RETURNS integer
468+
AS $$
469+
BEGIN
470+
IF STIsValid(geom) = 0 THEN
471+
RAISE EXCEPTION 'The geometry instance is not valid';
472+
ELSE
473+
RETURN sys.STNumPoints_helper(geom);
474+
END IF;
475+
END;
476+
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
477+
465478
-- STDisjoint
466479
-- Checks if two geometries have no points in common
467480
CREATE OR REPLACE FUNCTION sys.STDisjoint(geom1 sys.GEOMETRY, geom2 sys.GEOMETRY)
@@ -656,6 +669,11 @@ CREATE OR REPLACE FUNCTION sys.STDimension_helper(sys.GEOMETRY)
656669
RETURNS integer
657670
AS '$libdir/postgis-3','LWGEOM_dimension'
658671
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;
672+
673+
CREATE OR REPLACE FUNCTION sys.STNumPoints_helper(sys.GEOMETRY)
674+
RETURNS integer
675+
AS '$libdir/postgis-3','LWGEOM_npoints'
676+
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;
659677
660678
CREATE OR REPLACE FUNCTION sys.STIntersects_helper(geom1 sys.GEOMETRY, geom2 sys.GEOMETRY)
661679
RETURNS sys.BIT

contrib/babelfishpg_common/sql/upgrades/spatial_types--6.0.0--6.1.0.sql

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,45 @@
11
-------------------------------------------------------
22
---- Include changes related to spatial types here ----
33
-------------------------------------------------------
4+
5+
--STNumPoints
6+
-- geometry
7+
8+
CREATE OR REPLACE FUNCTION sys.STNumPoints(geom sys.GEOMETRY)
9+
RETURNS integer
10+
AS $$
11+
BEGIN
12+
IF STIsValid(geom) = 0 THEN
13+
RAISE EXCEPTION 'The geometry instance is not valid';
14+
ELSE
15+
RETURN sys.STNumPoints_helper(geom);
16+
END IF;
17+
END;
18+
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
19+
20+
CREATE OR REPLACE FUNCTION sys.STNumPoints_helper(sys.GEOMETRY)
21+
RETURNS integer
22+
AS '$libdir/postgis-3','LWGEOM_npoints'
23+
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;
24+
25+
-- Geography
26+
27+
CREATE OR REPLACE FUNCTION sys.STNumPoints(geog sys.GEOGRAPHY)
28+
RETURNS integer
29+
AS $$
30+
BEGIN
31+
IF sys.STIsValid(geog) = 0 THEN
32+
RAISE EXCEPTION 'The geography instance is not valid';
33+
ELSE
34+
RETURN sys.STNumPoints_helper(geog);
35+
END IF;
36+
END;
37+
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
38+
39+
CREATE OR REPLACE FUNCTION sys.STNumPoints_helper(sys.GEOGRAPHY)
40+
RETURNS integer
41+
AS '$libdir/postgis-3', 'LWGEOM_npoints'
42+
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;
443
--parse
544
--Geometry
645
CREATE OR REPLACE FUNCTION sys.Geometry__Parse(geometry_tagged_text sys.NVARCHAR)

contrib/babelfishpg_tsql/antlr/TSqlLexer.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@ STISCLOSED: 'STIsClosed';
986986
STISEMPTY: 'STIsEmpty';
987987
STISVALID: 'STIsValid';
988988
STLINEFROMTEXT: 'STLineFromText';
989+
STNUMPOINTS: 'STNumPoints';
989990
STOP: S T O P;
990991
STOPAT: S T O P A T;
991992
STOPATMARK: S T O P A T M A R K;

contrib/babelfishpg_tsql/antlr/TSqlParser.g4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3946,6 +3946,7 @@ geospatial_func_no_arg
39463946
| STISCLOSED
39473947
| STISEMPTY
39483948
| STISVALID
3949+
| STNUMPOINTS
39493950
;
39503951

39513952
geospatial_func_arg
@@ -5088,6 +5089,7 @@ keyword
50885089
| STISEMPTY
50895090
| STISVALID
50905091
| STLINEFROMTEXT
5092+
| STNUMPOINTS
50915093
| STOP
50925094
| STOPAT
50935095
| STOPATMARK

test/JDBC/expected/Numeric_Decimal_tests.out

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4884,34 +4884,38 @@ GO
48844884
-- GEOMETRY -> NUMERIC
48854885
SELECT CAST(GEOMETRY::STGeomFromText('LINESTRING(0 0, 1 1, 2 2)', 0).STNumPoints() AS NUMERIC(10,0));
48864886
GO
4887-
~~ERROR (Code: 33557097)~~
4888-
4889-
~~ERROR (Message: syntax error at or near ".")~~
4887+
~~START~~
4888+
numeric
4889+
3
4890+
~~END~~
48904891

48914892

48924893
-- GEOMETRY -> DECIMAL
48934894
SELECT CAST(GEOMETRY::STGeomFromText('LINESTRING(0 0, 1 1, 2 2)', 0).STNumPoints() AS DECIMAL(10,0));
48944895
GO
4895-
~~ERROR (Code: 33557097)~~
4896-
4897-
~~ERROR (Message: syntax error at or near ".")~~
4896+
~~START~~
4897+
numeric
4898+
3
4899+
~~END~~
48984900

48994901

49004902
-- GEOGRAPHY -> NUMERIC/DECIMAL (using STNumPoints() method)
49014903
-- GEOGRAPHY -> NUMERIC
49024904
SELECT CAST(GEOGRAPHY::STGeomFromText('LINESTRING(-122.34 47.65, -122.35 47.66)', 4326).STNumPoints() AS NUMERIC(10,0));
49034905
GO
4904-
~~ERROR (Code: 33557097)~~
4905-
4906-
~~ERROR (Message: syntax error at or near ".")~~
4906+
~~START~~
4907+
numeric
4908+
2
4909+
~~END~~
49074910

49084911

49094912
-- GEOGRAPHY -> DECIMAL
49104913
SELECT CAST(GEOGRAPHY::STGeomFromText('LINESTRING(-122.34 47.65, -122.35 47.66)', 4326).STNumPoints() AS DECIMAL(10,0));
49114914
GO
4912-
~~ERROR (Code: 33557097)~~
4913-
4914-
~~ERROR (Message: syntax error at or near ".")~~
4915+
~~START~~
4916+
numeric
4917+
2
4918+
~~END~~
49154919

49164920

49174921
-- ROWVERSION/TIMESTAMP -> NUMERIC/DECIMAL
@@ -5807,33 +5811,37 @@ GO
58075811
-- GEOMETRY -> NUMERIC using CONVERT
58085812
SELECT CONVERT(NUMERIC(10,0), GEOMETRY::STGeomFromText('LINESTRING(0 0, 1 1, 2 2)', 0).STNumPoints());
58095813
GO
5810-
~~ERROR (Code: 33557097)~~
5811-
5812-
~~ERROR (Message: syntax error at or near ".")~~
5814+
~~START~~
5815+
numeric
5816+
3
5817+
~~END~~
58135818

58145819

58155820
-- GEOMETRY -> DECIMAL using CONVERT
58165821
SELECT CONVERT(DECIMAL(10,0), GEOMETRY::STGeomFromText('LINESTRING(0 0, 1 1, 2 2)', 0).STNumPoints());
58175822
GO
5818-
~~ERROR (Code: 33557097)~~
5819-
5820-
~~ERROR (Message: syntax error at or near ".")~~
5823+
~~START~~
5824+
numeric
5825+
3
5826+
~~END~~
58215827

58225828

58235829
-- GEOGRAPHY -> NUMERIC using CONVERT
58245830
SELECT CONVERT(NUMERIC(10,0), GEOGRAPHY::STGeomFromText('LINESTRING(-122.34 47.65, -122.35 47.66)', 4326).STNumPoints());
58255831
GO
5826-
~~ERROR (Code: 33557097)~~
5827-
5828-
~~ERROR (Message: syntax error at or near ".")~~
5832+
~~START~~
5833+
numeric
5834+
2
5835+
~~END~~
58295836

58305837

58315838
-- GEOGRAPHY -> DECIMAL using CONVERT
58325839
SELECT CONVERT(DECIMAL(10,0), GEOGRAPHY::STGeomFromText('LINESTRING(-122.34 47.65, -122.35 47.66)', 4326).STNumPoints());
58335840
GO
5834-
~~ERROR (Code: 33557097)~~
5835-
5836-
~~ERROR (Message: syntax error at or near ".")~~
5841+
~~START~~
5842+
numeric
5843+
2
5844+
~~END~~
58375845

58385846

58395847
-- ROWVERSION/TIMESTAMP -> NUMERIC/DECIMAL using CONVERT

test/JDBC/expected/Test-spatial-functions-3-vu-cleanup.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
USE TestSTNumPoints_DB;
2+
3+
DROP VIEW STNumPoints_geog_view_db;
4+
DROP VIEW STNumPoints_geom_view_db;
5+
6+
DROP TABLE STNumPoints_geog_test_db;
7+
DROP TABLE STNumPoints_geom_test_db;
8+
9+
USE MASTER;
10+
11+
DROP VIEW STNumPoints_geog_view;
12+
DROP VIEW STNumPoints_geom_view;
13+
14+
DROP TABLE STNumPoints_geog_test;
15+
DROP TABLE STNumPoints_geom_test;
16+
17+
DROP DATABASE TestSTNumPoints_DB;
18+
119
USE TestGeospatialParse_DB
220

321
DROP TABLE TestGeospatialParse_GeometryTable3

0 commit comments

Comments
 (0)