Skip to content

Commit

Permalink
Supported STContains, STEquals, STArea PostGIS functions for Geospati…
Browse files Browse the repository at this point in the history
…al datatypes (#2803)

Implemented Some of the GeoSpatial datatypes like STContains, STEquals, STArea and STSrid. STSrid was important as it was important to cover the edge cases of STContains and STEquals as STContains or STEquals is performed with Geometry/Geography gives Null as per documentation but Was giving error in our case. So to check the SRID of the Object to cover the edge cases STSrid was implemented.

Issues Resolved
Task: BABEL-5101

Signed-off-by: yashneet vinayak <[email protected]>
Co-authored-by: yashneet vinayak <[email protected]>
  • Loading branch information
Yvinayak07 and yashneet vinayak authored Aug 5, 2024
1 parent f7aac56 commit 37a7838
Show file tree
Hide file tree
Showing 15 changed files with 2,647 additions and 58 deletions.
90 changes: 68 additions & 22 deletions contrib/babelfishpg_common/sql/geography.sql
Original file line number Diff line number Diff line change
Expand Up @@ -443,18 +443,53 @@ CREATE OR REPLACE FUNCTION sys.ST_zmflag(sys.GEOGRAPHY)
AS '$libdir/postgis-3', 'LWGEOM_zmflag'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION sys.ST_Equals(leftarg sys.GEOGRAPHY, rightarg sys.GEOGRAPHY)
RETURNS boolean
AS $$
DECLARE
leftvarBin sys.bbf_varbinary;
rightvarBin sys.bbf_varbinary;
BEGIN
leftvarBin := (SELECT sys.bbf_varbinary($1));
rightvarBin := (SELECT sys.bbf_varbinary($2));
RETURN (SELECT sys.varbinary_eq(leftvarBin, rightvarBin));
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
CREATE OR REPLACE FUNCTION sys.STArea(sys.GEOGRAPHY)
RETURNS float8
AS '$libdir/postgis-3','ST_Area'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

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

CREATE OR REPLACE FUNCTION sys.STEquals(geom1 sys.GEOGRAPHY, geom2 sys.GEOGRAPHY)
RETURNS sys.BIT
AS $$
BEGIN
IF STSrid(geom1) != STSrid(geom2) THEN
RETURN NULL;
ELSE
Return sys.STEquals_helper($1,$2);
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STContains(geom1 sys.GEOGRAPHY, geom2 sys.GEOGRAPHY)
RETURNS sys.BIT
AS $$
BEGIN
IF STSrid(geom1) != STSrid(geom2) THEN
RETURN NULL;
ELSE
Return sys.STContains_helper($1,$2);
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.ST_Equals(leftarg sys.GEOGRAPHY, rightarg sys.GEOGRAPHY)
RETURNS boolean
AS $$
DECLARE
Result integer;
BEGIN
Result := STEquals(leftarg,rightarg);
IF Result IS NULL THEN
RETURN false;
END IF;
RETURN Result;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OPERATOR sys.= (
LEFTARG = sys.GEOGRAPHY,
Expand All @@ -464,18 +499,19 @@ CREATE OPERATOR sys.= (
RESTRICT = eqsel
);

CREATE FUNCTION sys.ST_NotEquals(leftarg sys.GEOGRAPHY, rightarg sys.GEOGRAPHY)
CREATE OR REPLACE FUNCTION sys.ST_NotEquals(leftarg sys.GEOGRAPHY, rightarg sys.GEOGRAPHY)
RETURNS boolean
AS $$
DECLARE
leftvarBin sys.bbf_varbinary;
rightvarBin sys.bbf_varbinary;
BEGIN
leftvarBin := (SELECT sys.bbf_varbinary($1));
rightvarBin := (SELECT sys.bbf_varbinary($2));
RETURN (SELECT sys.varbinary_neq(leftvarBin, rightvarBin));
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
DECLARE
Result integer;
BEGIN
Result := STEquals(leftarg,rightarg);
IF Result IS NULL THEN
RETURN true;
END IF;
RETURN 1 - Result;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OPERATOR sys.<> (
LEFTARG = sys.GEOGRAPHY,
Expand Down Expand Up @@ -512,6 +548,16 @@ CREATE OR REPLACE FUNCTION sys.ST_Transform(sys.GEOGRAPHY, integer)
LANGUAGE 'c' IMMUTABLE STRICT;

-- Helper functions for main T-SQL functions
CREATE OR REPLACE FUNCTION sys.STEquals_helper(geom1 sys.GEOGRAPHY, geom2 sys.GEOGRAPHY)
RETURNS sys.BIT
AS '$libdir/postgis-3','ST_Equals'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STContains_helper(geom1 sys.GEOGRAPHY, geom2 sys.GEOGRAPHY)
RETURNS sys.BIT
AS '$libdir/postgis-3','within'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.stgeogfromtext_helper(text, integer)
RETURNS sys.GEOGRAPHY
AS '$libdir/postgis-3','LWGEOM_from_text'
Expand Down
88 changes: 67 additions & 21 deletions contrib/babelfishpg_common/sql/geometry.sql
Original file line number Diff line number Diff line change
Expand Up @@ -338,18 +338,53 @@ CREATE OR REPLACE FUNCTION sys.ST_zmflag(sys.GEOMETRY)
AS '$libdir/postgis-3', 'LWGEOM_zmflag'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION sys.ST_Equals(leftarg sys.GEOMETRY, rightarg sys.GEOMETRY)
CREATE OR REPLACE FUNCTION sys.STArea(sys.GEOMETRY)
RETURNS float8
AS '$libdir/postgis-3','ST_Area'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

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

CREATE OR REPLACE FUNCTION sys.STEquals(geom1 sys.GEOMETRY, geom2 sys.GEOMETRY)
RETURNS sys.BIT
AS $$
BEGIN
IF STSrid(geom1) != STSrid(geom2) THEN
RETURN NULL;
ELSE
Return sys.STEquals_helper($1,$2);
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STContains(geom1 sys.GEOMETRY, geom2 sys.GEOMETRY)
RETURNS sys.BIT
AS $$
BEGIN
IF STSrid(geom1) != STSrid(geom2) THEN
RETURN NULL;
ELSE
Return sys.STContains_helper($1,$2);
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.ST_Equals(leftarg sys.GEOMETRY, rightarg sys.GEOMETRY)
RETURNS boolean
AS $$
DECLARE
leftvarBin sys.bbf_varbinary;
rightvarBin sys.bbf_varbinary;
BEGIN
leftvarBin := (SELECT sys.bbf_varbinary($1));
rightvarBin := (SELECT sys.bbf_varbinary($2));
RETURN (SELECT sys.varbinary_eq(leftvarBin, rightvarBin));
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
AS $$
DECLARE
Result integer;
BEGIN
Result := STEquals(leftarg,rightarg);
IF Result IS NULL THEN
RETURN false;
END IF;
RETURN Result;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OPERATOR sys.= (
LEFTARG = sys.GEOMETRY,
Expand All @@ -359,18 +394,19 @@ CREATE OPERATOR sys.= (
RESTRICT = eqsel
);

CREATE FUNCTION sys.ST_NotEquals(leftarg sys.GEOMETRY, rightarg sys.GEOMETRY)
CREATE OR REPLACE FUNCTION sys.ST_NotEquals(leftarg sys.GEOMETRY, rightarg sys.GEOMETRY)
RETURNS boolean
AS $$
DECLARE
leftvarBin sys.bbf_varbinary;
rightvarBin sys.bbf_varbinary;
BEGIN
leftvarBin := (SELECT sys.bbf_varbinary($1));
rightvarBin := (SELECT sys.bbf_varbinary($2));
RETURN (SELECT sys.varbinary_neq(leftvarBin, rightvarBin));
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
DECLARE
Result integer;
BEGIN
Result := STEquals(leftarg,rightarg);
IF Result IS NULL THEN
RETURN true;
END IF;
RETURN 1 - Result;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OPERATOR sys.<> (
LEFTARG = sys.GEOMETRY,
Expand All @@ -396,6 +432,16 @@ CREATE OR REPLACE FUNCTION sys.sty(sys.GEOMETRY)
LANGUAGE 'c' IMMUTABLE STRICT;

-- Helper functions for main T-SQL functions
CREATE OR REPLACE FUNCTION sys.STContains_helper(geom1 sys.GEOMETRY, geom2 sys.GEOMETRY)
RETURNS sys.BIT
AS '$libdir/postgis-3','within'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STEquals_helper(geom1 sys.GEOMETRY, geom2 sys.GEOMETRY)
RETURNS sys.BIT
AS '$libdir/postgis-3','ST_Equals'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.stgeomfromtext_helper(text, integer)
RETURNS sys.GEOMETRY
AS '$libdir/postgis-3','LWGEOM_from_text'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,147 @@
-------------------------------------------------------
---- Include changes related to spatial types here ----
-------------------------------------------------------
CREATE OR REPLACE FUNCTION sys.STArea(sys.GEOMETRY)
RETURNS float8
AS '$libdir/postgis-3','ST_Area'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

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

CREATE OR REPLACE FUNCTION sys.STEquals(geom1 sys.GEOMETRY, geom2 sys.GEOMETRY)
RETURNS sys.BIT
AS $$
BEGIN
IF STSrid(geom1) != STSrid(geom2) THEN
RETURN NULL;
ELSE
Return sys.STEquals_helper($1,$2);
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STContains(geom1 sys.GEOMETRY, geom2 sys.GEOMETRY)
RETURNS sys.BIT
AS $$
DECLARE
BEGIN
IF STSrid(geom1) != STSrid(geom2) THEN
RETURN NULL;
ELSE
Return sys.STContains_helper($1,$2);
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.ST_Equals(leftarg sys.GEOMETRY, rightarg sys.GEOMETRY)
RETURNS boolean
AS $$
DECLARE
Result integer;
BEGIN
Result := STEquals(leftarg,rightarg);
IF Result IS NULL THEN
RETURN false;
END IF;
RETURN Result;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.ST_NotEquals(leftarg sys.GEOMETRY, rightarg sys.GEOMETRY)
RETURNS boolean
AS $$
DECLARE
Result integer;
BEGIN
Result := STEquals(leftarg,rightarg);
IF Result IS NULL THEN
RETURN true;
END IF;
RETURN 1 - Result;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STContains_helper(geom1 sys.GEOMETRY, geom2 sys.GEOMETRY)
RETURNS sys.BIT
AS '$libdir/postgis-3','within'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STEquals_helper(geom1 sys.GEOMETRY, geom2 sys.GEOMETRY)
RETURNS sys.BIT
AS '$libdir/postgis-3','ST_Equals'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

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

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

CREATE OR REPLACE FUNCTION sys.STEquals(geom1 sys.GEOGRAPHY, geom2 sys.GEOGRAPHY)
RETURNS sys.BIT
AS $$
BEGIN
IF STSrid(geom1) != STSrid(geom2) THEN
RETURN NULL;
ELSE
Return sys.STEquals_helper($1,$2);
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STContains(geom1 sys.GEOGRAPHY, geom2 sys.GEOGRAPHY)
RETURNS sys.BIT
AS $$
BEGIN
IF STSrid(geom1) != STSrid(geom2) THEN
RETURN NULL;
ELSE
Return sys.STContains_helper($1,$2);
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.ST_Equals(leftarg sys.GEOGRAPHY, rightarg sys.GEOGRAPHY)
RETURNS boolean
AS $$
DECLARE
Result integer;
BEGIN
Result := STEquals(leftarg,rightarg);
IF Result IS NULL THEN
RETURN false;
END IF;
RETURN Result;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.ST_NotEquals(leftarg sys.GEOGRAPHY, rightarg sys.GEOGRAPHY)
RETURNS boolean
AS $$
DECLARE
Result integer;
BEGIN
Result := STEquals(leftarg,rightarg);
IF Result IS NULL THEN
RETURN true;
END IF;
RETURN 1 - Result;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STEquals_helper(geom1 sys.GEOGRAPHY, geom2 sys.GEOGRAPHY)
RETURNS sys.BIT
AS '$libdir/postgis-3','ST_Equals'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.STContains_helper(geom1 sys.GEOGRAPHY, geom2 sys.GEOGRAPHY)
RETURNS sys.BIT
AS '$libdir/postgis-3','within'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;
Loading

0 comments on commit 37a7838

Please sign in to comment.