Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
USE TestGeospatialMethods_DB

DROP TABLE TestGeospatialMethods_YourTable1Temp

DROP TABLE TestGeospatialMethods_YourTable3Temp

USE MASTER

DROP TABLE TestGeospatialMethods_YourTable2Temp

DROP TABLE TestGeospatialMethods_TableATemp

DROP TABLE TestGeospatialMethods_TableBTemp

DROP VIEW TestGeospatialMethods_ValFromGeomTemp

DROP VIEW TestGeospatialMethods_TextFromGeogTemp

DROP VIEW TestGeospatialMethods_point_intersect1Temp

DROP VIEW TestGeospatialMethods_disjointTemp

DROP VIEW TestGeospatialMethods_point_intersectTemp

DROP VIEW TestGeospatialMethods_point_disjointTemp

DROP TABLE TestGeospatialMethods_YourTableTemp

DROP VIEW TestGeospatialMethods_valid_ValFromGeomTemp

DROP VIEW TestGeospatialMethods_valid_TextFromGeogTemp

DROP VIEW TestGeospatialMethods_closed_ValFromGeomTemp

DROP VIEW TestGeospatialMethods_closed_TextFromGeogTemp

DROP VIEW TestGeospatialMethods_empty_ValFromGeomTemp

DROP VIEW TestGeospatialMethods_empty_TextFromGeomTemp

DROP TABLE TestGeospatialMethods_SPATIALPOINTGEOM_dttemp

DROP TABLE TestGeospatialMethods_SPATIALPOINTGEOG_dttemp

DROP DATABASE TestGeospatialMethods_DB
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# This file tests support for these functions: STDimension, STDisjoint, STIntersects, STIsClosed, STIsEmpty, STIsValid

CREATE DATABASE TestGeospatialMethods_DB;

USE TestGeospatialMethods_DB;

CREATE TABLE TestGeospatialMethods_YourTable1Temp ( ID INT PRIMARY KEY, PointColumn geometry );
INSERT INTO TestGeospatialMethods_YourTable1Temp (ID, PointColumn) VALUES (1, geometry::Point(3.0, 4.0, 4326)), (2, geometry::Point(5.0, 6.0, 4326)), (3, geometry::Point(3.0, 4.0, 0)), (4, geometry::STPointFromText('POINT EMPTY', 4123));
~~ROW COUNT: 4~~


CREATE TABLE TestGeospatialMethods_YourTable3Temp ( ID INT PRIMARY KEY, PointColumn geography );
INSERT INTO TestGeospatialMethods_YourTable3Temp (ID, PointColumn) VALUES (1, geography::Point(3.0, 4.0, 4326)), (2, geography::Point(5.0, 6.0, 4121)), (3, geography::Point(3.0, 4.0, 4122)), (4, geography::STPointFromText('POINT EMPTY', 4123));
~~ROW COUNT: 4~~


USE MASTER

CREATE TABLE TestGeospatialMethods_YourTableTemp ( ID INT PRIMARY KEY, PointColumn geometry );
INSERT INTO TestGeospatialMethods_YourTableTemp (ID, PointColumn) VALUES (1, geometry::Point(3.0, 4.0, 4326)), (2, geometry::Point(5.0, 6.0, 4326)), (3, geometry::Point(3.0, 4.0, 0));
~~ROW COUNT: 3~~


CREATE TABLE TestGeospatialMethods_YourTable2Temp ( ID INT PRIMARY KEY, PointColumn1 geometry, PointColumn2 geometry );
INSERT INTO TestGeospatialMethods_YourTable2Temp (ID, PointColumn1, PointColumn2) VALUES (1, geometry::Point(3.0, 4.0, 4326), geometry::Point(3.0, 4.0, 4326));
~~ROW COUNT: 1~~


CREATE TABLE TestGeospatialMethods_TableATemp (ID INT PRIMARY KEY, PointA geometry);
INSERT INTO TestGeospatialMethods_TableATemp (ID, PointA) VALUES (1, geometry::Point(1.0, 2.0, 4326));
~~ROW COUNT: 1~~


CREATE TABLE TestGeospatialMethods_TableBTemp (ID INT PRIMARY KEY, PointB geometry);
INSERT INTO TestGeospatialMethods_TableBTemp (ID, PointB) VALUES (1, geometry::Point(3.0, 4.0, 4326));
~~ROW COUNT: 1~~


CREATE TABLE TestGeospatialMethods_SPATIALPOINTGEOG_dttemp (location geography);
INSERT INTO TestGeospatialMethods_SPATIALPOINTGEOG_dttemp (location) VALUES ( geography::STGeomFromText('Point(47.65100 -22.34900)', 4326) );
~~ROW COUNT: 1~~

INSERT INTO TestGeospatialMethods_SPATIALPOINTGEOG_dttemp (location) VALUES ( geography::STGeomFromText('Point(1.0 2.0)', 4326) );
~~ROW COUNT: 1~~

INSERT INTO TestGeospatialMethods_SPATIALPOINTGEOG_dttemp (location) VALUES ( geography::STGeomFromText('Point(1.0 2.0)', 4326) );
~~ROW COUNT: 1~~

INSERT INTO TestGeospatialMethods_SPATIALPOINTGEOG_dttemp (location) VALUES ( geography::STPointFromText('Point(1.0 2.0)', 4326) );
~~ROW COUNT: 1~~


CREATE TABLE TestGeospatialMethods_SPATIALPOINTGEOM_dttemp (location geometry);
INSERT INTO TestGeospatialMethods_SPATIALPOINTGEOM_dttemp (location) VALUES ( geometry::STGeomFromText('Point(47.65100 -22.34900)', 4326) );
~~ROW COUNT: 1~~

INSERT INTO TestGeospatialMethods_SPATIALPOINTGEOM_dttemp (location) VALUES ( geometry::STGeomFromText('Point(1.0 2.0)', 4326) );
~~ROW COUNT: 1~~

INSERT INTO TestGeospatialMethods_SPATIALPOINTGEOM_dttemp (location) VALUES ( geometry::STGeomFromText('Point(47.65100 -22.34900)', 0) );
~~ROW COUNT: 1~~

INSERT INTO TestGeospatialMethods_SPATIALPOINTGEOM_dttemp (location) VALUES ( geometry::STPointFromText('Point(1.0 2.0)', 4326) );
~~ROW COUNT: 1~~

INSERT INTO TestGeospatialMethods_SPATIALPOINTGEOM_dttemp (location) VALUES ( geometry::Point(47.65100, -22.34900, 4326) );
~~ROW COUNT: 1~~


CREATE VIEW TestGeospatialMethods_point_intersect1Temp AS SELECT p1.location.STIntersects(p2.location) AS intersection FROM TestGeospatialMethods_SPATIALPOINTGEOM_dttemp p1 CROSS JOIN TestGeospatialMethods_SPATIALPOINTGEOM_dttemp p2;

CREATE VIEW TestGeospatialMethods_disjointTemp AS SELECT p1.location.STDisjoint(p2.location) AS isIN FROM TestGeospatialMethods_SPATIALPOINTGEOM_dttemp p1 CROSS JOIN TestGeospatialMethods_SPATIALPOINTGEOM_dttemp p2 ORDER BY p1.location.STX;

CREATE VIEW TestGeospatialMethods_ValFromGeomTemp AS SELECT location.STDimension() FROM TestGeospatialMethods_SPATIALPOINTGEOM_dttemp ORDER BY location.STX;

CREATE VIEW TestGeospatialMethods_TextFromGeogTemp AS SELECT location.STDimension() AS Dimension FROM TestGeospatialMethods_SPATIALPOINTGEOG_dttemp ORDER BY location.Lat;

CREATE VIEW TestGeospatialMethods_point_intersectTemp AS SELECT p1.location.STIntersects(p2.location) AS Intersection FROM TestGeospatialMethods_SPATIALPOINTGEOG_dttemp p1 CROSS JOIN TestGeospatialMethods_SPATIALPOINTGEOG_dttemp p2 ORDER BY p1.location.Lat;

CREATE VIEW TestGeospatialMethods_point_disjointTemp AS SELECT p1.location.STDisjoint(p2.location) AS disjoint FROM TestGeospatialMethods_SPATIALPOINTGEOG_dttemp p1 CROSS JOIN TestGeospatialMethods_SPATIALPOINTGEOG_dttemp p2 ORDER BY p1.location.Lat;

CREATE VIEW TestGeospatialMethods_valid_ValFromGeomTemp AS SELECT location.STIsValid() FROM TestGeospatialMethods_SPATIALPOINTGEOM_dttemp ORDER BY location.STX;

CREATE VIEW TestGeospatialMethods_valid_TextFromGeogTemp AS SELECT location.STIsValid() AS Dimension FROM TestGeospatialMethods_SPATIALPOINTGEOG_dttemp ORDER BY location.Lat;

CREATE VIEW TestGeospatialMethods_closed_ValFromGeomTemp AS SELECT location.STIsClosed() FROM TestGeospatialMethods_SPATIALPOINTGEOM_dttemp ORDER BY location.STX;

CREATE VIEW TestGeospatialMethods_closed_TextFromGeogTemp AS SELECT location.STIsClosed() AS Dimension FROM TestGeospatialMethods_SPATIALPOINTGEOG_dttemp ORDER BY location.Lat;

CREATE VIEW TestGeospatialMethods_empty_ValFromGeomTemp AS SELECT location.STIsEmpty() FROM TestGeospatialMethods_SPATIALPOINTGEOM_dttemp ORDER BY location.STX;

CREATE VIEW TestGeospatialMethods_empty_TextFromGeomTemp AS SELECT location.STIsEmpty() AS Dimension FROM TestGeospatialMethods_SPATIALPOINTGEOG_dttemp ORDER BY location.Lat;
Loading