Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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;

--Parse
CREATE OR REPLACE FUNCTION sys.Geography__Parse(geography_tagged_text sys.NVARCHAR)
RETURNS sys.GEOGRAPHY
Expand Down Expand Up @@ -665,6 +678,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 @@ -462,6 +462,19 @@ CREATE OR REPLACE FUNCTION sys.Geometry__Parse(geometry_tagged_text sys.NVARCHAR
END;
$$ LANGUAGE plpgsql STRICT IMMUTABLE 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 @@ -656,6 +669,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,6 +1,45 @@
-------------------------------------------------------
---- 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';
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;
--parse
--Geometry
CREATE OR REPLACE FUNCTION sys.Geometry__Parse(geometry_tagged_text sys.NVARCHAR)
Expand Down
1 change: 1 addition & 0 deletions contrib/babelfishpg_tsql/antlr/TSqlLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,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 @@ -3946,6 +3946,7 @@ geospatial_func_no_arg
| STISCLOSED
| STISEMPTY
| STISVALID
| STNUMPOINTS
;

geospatial_func_arg
Expand Down Expand Up @@ -5088,6 +5089,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
18 changes: 18 additions & 0 deletions test/JDBC/expected/Test-spatial-functions-3-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
USE TestSTNumPoints_DB;

DROP VIEW STNumPoints_geog_view_db;
DROP VIEW STNumPoints_geom_view_db;

DROP TABLE STNumPoints_geog_test_db;
DROP TABLE STNumPoints_geom_test_db;

USE MASTER;

DROP VIEW STNumPoints_geog_view;
DROP VIEW STNumPoints_geom_view;

DROP TABLE STNumPoints_geog_test;
DROP TABLE STNumPoints_geom_test;

DROP DATABASE TestSTNumPoints_DB;

USE TestGeospatialParse_DB

DROP TABLE TestGeospatialParse_GeometryTable3
Expand Down
Loading