Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions contrib/babelfishpg_common/sql/geography.sql
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,19 @@ CREATE OR REPLACE FUNCTION sys.STDimension(geom sys.GEOGRAPHY)
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

--STNumPoints
CREATE OR REPLACE FUNCTION sys.STNumPoints(geog sys.GEOGRAPHY)
RETURNS integer
AS $$
BEGIN
IF sys.STIsValid(geog) = 0 THEN
RAISE EXCEPTION 'The geography instance is not valid';
ELSE
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 geography 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

RETURN sys.STNumPoints_helper(geog);
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

-- STDisjoint
-- Checks if two geometries have no points in common
CREATE OR REPLACE FUNCTION sys.STDisjoint(geom1 sys.GEOGRAPHY, geom2 sys.GEOGRAPHY)
Expand Down Expand Up @@ -633,6 +646,11 @@ CREATE OR REPLACE FUNCTION sys.STDimension_helper(sys.GEOGRAPHY)
AS '$libdir/postgis-3','LWGEOM_dimension'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STNumPoints_helper(sys.GEOGRAPHY)
RETURNS integer
AS '$libdir/postgis-3', 'LWGEOM_npoints'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STIntersects_helper(geom1 sys.GEOGRAPHY, geom2 sys.GEOGRAPHY)
RETURNS sys.BIT
AS '$libdir/postgis-3','ST_Intersects'
Expand Down
18 changes: 18 additions & 0 deletions contrib/babelfishpg_common/sql/geometry.sql
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,19 @@ CREATE OR REPLACE FUNCTION sys.STDimension(geom sys.GEOMETRY)
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

--STNumPoints
CREATE OR REPLACE FUNCTION sys.STNumPoints(geom sys.GEOMETRY)
RETURNS integer
AS $$
BEGIN
IF STIsValid(geom) = 0 THEN
RAISE EXCEPTION 'The geometry instance is not valid';
ELSE
RETURN sys.STNumPoints_helper(geom);
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 geometry 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

END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

-- STDisjoint
-- Checks if two geometries have no points in common
CREATE OR REPLACE FUNCTION sys.STDisjoint(geom1 sys.GEOMETRY, geom2 sys.GEOMETRY)
Expand Down Expand Up @@ -622,6 +635,11 @@ CREATE OR REPLACE FUNCTION sys.STDimension_helper(sys.GEOMETRY)
RETURNS integer
AS '$libdir/postgis-3','LWGEOM_dimension'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STNumPoints_helper(sys.GEOMETRY)
RETURNS integer
AS '$libdir/postgis-3','LWGEOM_npoints'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STIntersects_helper(geom1 sys.GEOMETRY, geom2 sys.GEOMETRY)
RETURNS sys.BIT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
-------------------------------------------------------
---- Include changes related to spatial types here ----
-------------------------------------------------------

--STNumPoints
-- geometry

CREATE OR REPLACE FUNCTION sys.STNumPoints(geom sys.GEOMETRY)
RETURNS integer
AS $$
BEGIN
IF STIsValid(geom) = 0 THEN
RAISE EXCEPTION 'The geometry instance is not valid';
ELSE
RETURN sys.STNumPoints_helper(geom);
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STNumPoints_helper(sys.GEOMETRY)
RETURNS integer
AS '$libdir/postgis-3','LWGEOM_npoints'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

-- Geography

CREATE OR REPLACE FUNCTION sys.STNumPoints(geog sys.GEOGRAPHY)
RETURNS integer
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
Copy Markdown
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
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

ELSE
RETURN sys.STNumPoints_helper(geog);
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STNumPoints_helper(sys.GEOGRAPHY)
RETURNS integer
AS '$libdir/postgis-3', 'LWGEOM_npoints'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;
1 change: 1 addition & 0 deletions contrib/babelfishpg_tsql/antlr/TSqlLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,7 @@ STISCLOSED: 'STIsClosed';
STISEMPTY: 'STIsEmpty';
STISVALID: 'STIsValid';
STLINEFROMTEXT: 'STLineFromText';
STNUMPOINTS: 'STNumPoints';
STOP: S T O P;
STOPAT: S T O P A T;
STOPATMARK: S T O P A T M A R K;
Expand Down
2 changes: 2 additions & 0 deletions contrib/babelfishpg_tsql/antlr/TSqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -3945,6 +3945,7 @@ geospatial_func_no_arg
| STISCLOSED
| STISEMPTY
| STISVALID
| STNUMPOINTS
;

geospatial_func_arg
Expand Down Expand Up @@ -5086,6 +5087,7 @@ keyword
| STISEMPTY
| STISVALID
| STLINEFROMTEXT
| STNUMPOINTS
| STOP
| STOPAT
| STOPATMARK
Expand Down
56 changes: 32 additions & 24 deletions test/JDBC/expected/Numeric_Decimal_tests.out
Original file line number Diff line number Diff line change
Expand Up @@ -4884,34 +4884,38 @@ GO
-- GEOMETRY -> NUMERIC
SELECT CAST(GEOMETRY::STGeomFromText('LINESTRING(0 0, 1 1, 2 2)', 0).STNumPoints() AS NUMERIC(10,0));
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: syntax error at or near ".")~~
~~START~~
numeric
3
~~END~~


-- GEOMETRY -> DECIMAL
SELECT CAST(GEOMETRY::STGeomFromText('LINESTRING(0 0, 1 1, 2 2)', 0).STNumPoints() AS DECIMAL(10,0));
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: syntax error at or near ".")~~
~~START~~
numeric
3
~~END~~


-- GEOGRAPHY -> NUMERIC/DECIMAL (using STNumPoints() method)
-- GEOGRAPHY -> NUMERIC
SELECT CAST(GEOGRAPHY::STGeomFromText('LINESTRING(-122.34 47.65, -122.35 47.66)', 4326).STNumPoints() AS NUMERIC(10,0));
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: syntax error at or near ".")~~
~~START~~
numeric
2
~~END~~


-- GEOGRAPHY -> DECIMAL
SELECT CAST(GEOGRAPHY::STGeomFromText('LINESTRING(-122.34 47.65, -122.35 47.66)', 4326).STNumPoints() AS DECIMAL(10,0));
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: syntax error at or near ".")~~
~~START~~
numeric
2
~~END~~


-- ROWVERSION/TIMESTAMP -> NUMERIC/DECIMAL
Expand Down Expand Up @@ -5807,33 +5811,37 @@ GO
-- GEOMETRY -> NUMERIC using CONVERT
SELECT CONVERT(NUMERIC(10,0), GEOMETRY::STGeomFromText('LINESTRING(0 0, 1 1, 2 2)', 0).STNumPoints());
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: syntax error at or near ".")~~
~~START~~
numeric
3
~~END~~


-- GEOMETRY -> DECIMAL using CONVERT
SELECT CONVERT(DECIMAL(10,0), GEOMETRY::STGeomFromText('LINESTRING(0 0, 1 1, 2 2)', 0).STNumPoints());
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: syntax error at or near ".")~~
~~START~~
numeric
3
~~END~~


-- GEOGRAPHY -> NUMERIC using CONVERT
SELECT CONVERT(NUMERIC(10,0), GEOGRAPHY::STGeomFromText('LINESTRING(-122.34 47.65, -122.35 47.66)', 4326).STNumPoints());
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: syntax error at or near ".")~~
~~START~~
numeric
2
~~END~~


-- GEOGRAPHY -> DECIMAL using CONVERT
SELECT CONVERT(DECIMAL(10,0), GEOGRAPHY::STGeomFromText('LINESTRING(-122.34 47.65, -122.35 47.66)', 4326).STNumPoints());
GO
~~ERROR (Code: 33557097)~~

~~ERROR (Message: syntax error at or near ".")~~
~~START~~
numeric
2
~~END~~


-- ROWVERSION/TIMESTAMP -> NUMERIC/DECIMAL using CONVERT
Expand Down
13 changes: 13 additions & 0 deletions test/JDBC/expected/Test-spatial-functions-3-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
USE TestSTNumPoints_DB

USE MASTER

DROP VIEW STNumPoints_geom_view;

DROP VIEW STNumPoints_geog_view;

DROP TABLE STNumPoints_geom_test;

DROP TABLE STNumPoints_geog_test;

DROP DATABASE TestSTNumPoints_DB
98 changes: 98 additions & 0 deletions test/JDBC/expected/Test-spatial-functions-3-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
CREATE DATABASE TestSTNumPoints_DB;

USE TestSTNumPoints_DB;

USE MASTER

CREATE TABLE STNumPoints_geom_test ( ID INT PRIMARY KEY, geom_type VARCHAR(50), geom geometry );

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (1, 'Point', geometry::STPointFromText('POINT(0 0)', 0));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (2, 'Point with SRID', geometry::STPointFromText('POINT(-122.349 47.651)', 4326));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (3, 'Point constructor', geometry::Point(3.0, 4.0, 4326));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (4, 'LineString 2pts', geometry::STGeomFromText('LINESTRING(0 0, 1 1)', 0));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (5, 'LineString 3pts', geometry::STGeomFromText('LINESTRING(0 0, 1 1, 2 2)', 0));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (6, 'LineString 5pts', geometry::STGeomFromText('LINESTRING(0 0, 1 1, 2 2, 3 3, 4 4)', 0));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (7, 'Polygon triangle', geometry::STGeomFromText('POLYGON((0 0, 1 0, 0 1, 0 0))', 0));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (8, 'Polygon square', geometry::STGeomFromText('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))', 0));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (9, 'Polygon with hole', geometry::STGeomFromText('POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (2 2, 8 2, 8 8, 2 8, 2 2))', 0));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (10, 'Empty Point', geometry::STGeomFromText('POINT EMPTY', 0));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (11, 'Empty LineString', geometry::STGeomFromText('LINESTRING EMPTY', 0));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (12, 'Empty Polygon', geometry::STGeomFromText('POLYGON EMPTY', 0));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (13, 'Point Z', geometry::STGeomFromText('POINT Z(1 2 3)', 0));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (14, 'Empty Point', geometry::STGeomFromText('POINT EMPTY', 0));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (15, 'Empty LineString', geometry::STGeomFromText('LINESTRING EMPTY', 0));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geom_test (ID, geom_type, geom) VALUES (16, 'Empty Polygon', geometry::STGeomFromText('POLYGON EMPTY', 0));
~~ROW COUNT: 1~~


CREATE TABLE STNumPoints_geog_test ( ID INT PRIMARY KEY, geog_type VARCHAR(50), geog geography );
INSERT INTO STNumPoints_geog_test (ID, geog_type, geog) VALUES (1, 'Point', geography::STPointFromText('POINT(-122.349 47.651)', 4326));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geog_test (ID, geog_type, geog) VALUES (2, 'Point constructor', geography::Point(47.651, -122.349, 4326));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geog_test (ID, geog_type, geog) VALUES (3, 'LineString 2pts', geography::STGeomFromText('LINESTRING(-122.36 47.65, -122.34 47.66)', 4326));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geog_test (ID, geog_type, geog) VALUES (4, 'LineString 3pts', geography::STGeomFromText('LINESTRING(-122.36 47.65, -122.34 47.66, -122.32 47.67)', 4326));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geog_test (ID, geog_type, geog) VALUES (5, 'LineString 5pts', geography::STGeomFromText('LINESTRING(-122.36 47.65, -122.35 47.66, -122.34 47.67, -122.33 47.68, -122.32 47.69)', 4326));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geog_test (ID, geog_type, geog) VALUES (6, 'Polygon triangle', geography::STGeomFromText('POLYGON((-122.36 47.65, -122.34 47.65, -122.35 47.67, -122.36 47.65))', 4326));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geog_test (ID, geog_type, geog) VALUES (7, 'Polygon square', geography::STGeomFromText('POLYGON((-122.36 47.65, -122.34 47.65, -122.34 47.67, -122.36 47.67, -122.36 47.65))', 4326));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geog_test (ID, geog_type, geog) VALUES (8, 'Empty Point', geography::STGeomFromText('POINT EMPTY', 4326));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geog_test (ID, geog_type, geog) VALUES (9, 'Empty LineString', geography::STGeomFromText('LINESTRING EMPTY', 4326));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geog_test (ID, geog_type, geog) VALUES (10, 'Point Z', geography::STGeomFromText('POINT Z(-122.349 47.651 100)', 4326));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geog_test (ID, geog_type, geog) VALUES (11, 'Empty Point', geography::STGeomFromText('POINT EMPTY', 4326));
~~ROW COUNT: 1~~

INSERT INTO STNumPoints_geog_test (ID, geog_type, geog) VALUES (12, 'Empty LineString', geography::STGeomFromText('LINESTRING EMPTY', 4326));
~~ROW COUNT: 1~~


CREATE VIEW STNumPoints_geom_view AS SELECT ID, geom_type, geom.STNumPoints() AS num_points FROM STNumPoints_geom_test;

CREATE VIEW STNumPoints_geog_view AS SELECT ID, geog_type, geog.STNumPoints() AS num_points FROM STNumPoints_geog_test;
Loading