Skip to content

Commit

Permalink
Implemented TDS Receiver Support for Spatial Datatypes and Tested bot…
Browse files Browse the repository at this point in the history
…h Sender and Receiver functions with JDBC, ODBC and DOTNET Drivers (babelfish-for-postgresql#1921)

This commit contains Receiver Support for GEOGRAPHY and GEOGRAPHY datatype for Point Type and also tested the Sender and Receiver functions with JDBC, ODBC and DOTNET drivers.

Added CAST (Varbinary to Geometry/Geography and vice-versa) required by JDBC driver to receiver Geometry/Geography as Varbinary.

Introduced appropriate error messages when an unsupported functionality(such as LINESTRING) is queried.

Added Receiver function required by DOTNET driver to receiver Geometry/Geography as CLRUDT types.

Added new tests in TestSpatialPoint-vu-* to test receiver function for JDBC driver.

Added logic to parse the geometry/geography inputs in JDBCPreparedStatement.java file.

Added new tests TestPoint.txt to test Sender and Receiver functions for DOTNET driver.

Added logic to parse the geometry/geography inputs in PrepareExecBinding.cs file.

Added new tests geometry.cpp and geography.cpp to test Sender and Receiver functions for ODBC driver.

Added some sender function tests in test_data_types.cpp file for ODBC driver.

Tested the changes with valgrind and didn't see any memory issues.

Task: BABEL- 4369
Authored-by: Anikait Agrawal [email protected]
Signed-off-by: Anikait Agrawal [email protected]
  • Loading branch information
Anikait143 authored Oct 23, 2023
1 parent 2ad7351 commit 3a0259b
Show file tree
Hide file tree
Showing 25 changed files with 1,748 additions and 722 deletions.
14 changes: 8 additions & 6 deletions contrib/babelfishpg_common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ include $(PGXS)

MODULEPATH = $$libdir/$(EXTENSION)-$(BBFPGCMN_MAJOR_VERSION)

UPGRADES = $(patsubst sql/upgrades/%.sql,sql/%.sql,$(wildcard sql/upgrades/*.sql))
UPGRADES = $(patsubst sql/upgrades/babelfishpg_common--%.sql,sql/babelfishpg_common--%.sql,$(wildcard sql/upgrades/babelfishpg_common--*.sql))
GENERATED_UPGRADES = sql/$(EXTENSION)--3.2.0--3.3.0.sql

ifdef PREV_EXTVERSION
DATA = sql/$(EXTENSION)--$(PREV_EXTVERSION).sql
endif

DATA_built = \
$(EXTENSION).control \
sql/$(EXTENSION)--$(EXTVERSION).sql $(UPGRADES)
sql/$(EXTENSION)--$(EXTVERSION).sql $(UPGRADES) \
$(GENERATED_UPGRADES)

#include ../Makefile.common

Expand All @@ -78,7 +80,7 @@ include $(PGXS)

ifeq ($(GE91),yes)
all: sql/$(EXTENSION)--$(EXTVERSION).sql $(UPGRADES)
all: sql/upgrades/$(EXTENSION)--3.2.0--3.3.0.sql sql/upgrades/babelfishpg_upgrades.in
all: $(GENERATED_UPGRADES)
endif

$(EXTENSION).control: $(EXTENSION).control.in
Expand All @@ -96,18 +98,18 @@ ifeq (,$(filter $(FLAG_TO_CHECK),$(PG_CPPFLAGS)))
sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).in
cpp $< | sed 's/^# /-- /g' > $@

sql/upgrades/$(EXTENSION)--3.2.0--3.3.0.sql: sql/upgrades/babelfishpg_upgrades.in
sql/$(EXTENSION)--3.2.0--3.3.0.sql: sql/upgrades/babelfishpg_upgrades.in
cpp -D PREV_VERSION=3.2.0 -D CUR_VERSION=3.3.0 $< | sed 's/^# /-- /g' > $@
else
# The flag is present build the .in file including the spatial type files
sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).in
cpp -D ENABLE_SPATIAL_TYPES=1 $< | sed 's/^# /-- /g' > $@

sql/upgrades/$(EXTENSION)--3.2.0--3.3.0.sql: sql/upgrades/babelfishpg_upgrades.in
sql/$(EXTENSION)--3.2.0--3.3.0.sql: sql/upgrades/babelfishpg_upgrades.in
cpp -D ENABLE_SPATIAL_TYPES=1 -D PREV_VERSION=3.2.0 -D CUR_VERSION=3.3.0 $< | sed 's/^# /-- /g' > $@
endif

sql/%.sql: sql/upgrades/%.sql
sql/babelfishpg_common--%.sql: sql/upgrades/babelfishpg_common--%.sql
cp $< $@


Expand Down
274 changes: 181 additions & 93 deletions contrib/babelfishpg_common/sql/geography.sql

Large diffs are not rendered by default.

110 changes: 105 additions & 5 deletions contrib/babelfishpg_common/sql/geometry.sql
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,23 @@ CREATE OR REPLACE FUNCTION sys.Geometry__stgeomfromtext(text, integer)
AS $$
DECLARE
srid integer;
Geomtype text;
geom sys.GEOMETRY;
BEGIN
srid := $2;
IF srid >= 0 AND srid <= 999999 THEN
-- Call the underlying function after preprocessing
RETURN (SELECT sys.stgeomfromtext_helper($1, $2));
geom = (SELECT sys.stgeomfromtext_helper($1, $2));
Geomtype = (SELECT sys.ST_GeometryType(geom));
IF Geomtype = 'ST_Point' THEN
IF (SELECT sys.ST_Zmflag(geom)) = 1 OR (SELECT sys.ST_Zmflag(geom)) = 2 OR (SELECT sys.ST_Zmflag(geom)) = 3 THEN
RAISE EXCEPTION 'Unsupported flags';
ELSE
RETURN geom;
END IF;
ELSE
RAISE EXCEPTION '% is not supported', Geomtype;
END IF;
ELSE
RAISE EXCEPTION 'SRID value should be between 0 and 999999';
END IF;
Expand All @@ -96,15 +108,74 @@ CREATE OR REPLACE FUNCTION sys.text(sys.GEOMETRY)
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.GEOMETRY(bytea)
RETURNS sys.GEOMETRY
AS '$libdir/postgis-3','LWGEOM_from_bytea'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;
RETURNS sys.GEOMETRY
AS $$
DECLARE
len integer;
varBin bytea;
geomType bytea;
srid integer;
byte_position integer := 6;
coord_NaN bytea := E'\\x000000000000f87f';
input_coord bytea;
isNaN integer = 0;
newVarBin bytea;
BEGIN
varBin := $1;
len := LENGTH(varBin);
IF len >= 22 THEN
-- We are preprocessing it by removing 2 constant Geometry Type bytes -> 01 0c (for 2-D Point Type)
-- Then adding 5 Bytes -> 01 (little endianess) + 4 Bytes (Geometry Type)
srid := (get_byte(varBin, 3) << 24) | (get_byte(varBin, 2) << 16) | (get_byte(varBin, 1) << 8) | get_byte(varBin, 0);
WHILE byte_position < len LOOP
-- Get the coordinate to check if it is NaN
input_coord := substring(varBin from byte_position + 1 for 8);
IF encode(input_coord, 'hex') = encode(coord_NaN, 'hex') THEN
isNaN := 1;
END IF;
byte_position := byte_position + 8;
END LOOP;
geomType := substring(varBin from 5 for 2);
varBin := substring(varBin from 1 for 4) || substring(varBin from 7);
IF srid >= 0 AND srid <= 999999 AND isNaN = 0 THEN
IF encode(geomType, 'hex') = encode(E'\\x010c', 'hex') THEN
newVarBin := E'\\x0101000020' || varBin;
ELSE
RAISE EXCEPTION 'Unsupported geometry type';
END IF;
ELSE
RAISE EXCEPTION 'Error converting data type varbinary to geometry.';
END IF;
-- Call the underlying function after preprocessing
RETURN (SELECT sys.GEOMETRY_helper(newVarBin));
ELSE
RAISE EXCEPTION 'Invalid Geometry';
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

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

CREATE OR REPLACE FUNCTION sys.GEOMETRY(sys.bbf_varbinary)
RETURNS sys.GEOMETRY
AS $$
DECLARE
varBin bytea;
BEGIN
varBin := (SELECT CAST ($1 AS bytea));
-- Call the underlying function after preprocessing
RETURN (SELECT CAST (varBin AS GEOMETRY));
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

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

CREATE OR REPLACE FUNCTION sys.GEOMETRY(text)
RETURNS sys.GEOMETRY
AS '$libdir/postgis-3','parse_WKT_lwgeom'
Expand All @@ -114,6 +185,8 @@ CREATE CAST (text AS sys.GEOMETRY) WITH FUNCTION sys.GEOMETRY(text) AS IMPLICIT;
CREATE CAST (sys.GEOMETRY AS text) WITH FUNCTION sys.text(sys.GEOMETRY) AS IMPLICIT;
CREATE CAST (bytea AS sys.GEOMETRY) WITH FUNCTION sys.GEOMETRY(bytea) AS IMPLICIT;
CREATE CAST (sys.GEOMETRY AS bytea) WITH FUNCTION sys.bytea(sys.GEOMETRY) AS IMPLICIT;
CREATE CAST (sys.bbf_varbinary AS sys.GEOMETRY) WITH FUNCTION sys.GEOMETRY(sys.bbf_varbinary) AS IMPLICIT;
CREATE CAST (sys.GEOMETRY AS sys.bbf_varbinary) WITH FUNCTION sys.bbf_varbinary(sys.GEOMETRY) AS IMPLICIT;

-- Availability: 3.2.0 current supported in APG
CREATE OR REPLACE FUNCTION sys.Geometry__Point(float8, float8, srid integer)
Expand Down Expand Up @@ -142,17 +215,39 @@ CREATE OR REPLACE FUNCTION sys.Geometry__STPointFromText(text, integer)
AS $$
DECLARE
srid integer;
Geomtype text;
geom sys.GEOMETRY;
BEGIN
srid := $2;
IF srid >= 0 AND srid <= 999999 THEN
-- Call the underlying function after preprocessing
RETURN (SELECT sys.stgeomfromtext_helper($1, $2));
geom = (SELECT sys.stgeomfromtext_helper($1, $2));
Geomtype = (SELECT sys.ST_GeometryType(geom));
IF Geomtype = 'ST_Point' THEN
IF (SELECT sys.ST_Zmflag(geom)) = 1 OR (SELECT sys.ST_Zmflag(geom)) = 2 OR (SELECT sys.ST_Zmflag(geom)) = 3 THEN
RAISE EXCEPTION 'Unsupported flags';
ELSE
RETURN geom;
END IF;
ELSE
RAISE EXCEPTION '% is not supported', Geomtype;
END IF;
ELSE
RAISE EXCEPTION 'SRID value should be between 0 and 999999';
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

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

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

-- Minimum distance. 2D only.
CREATE OR REPLACE FUNCTION sys.STDistance(geom1 sys.GEOMETRY, geom2 sys.GEOMETRY)
RETURNS float8
Expand Down Expand Up @@ -180,3 +275,8 @@ CREATE OR REPLACE FUNCTION sys.GeomPoint_helper(float8, float8, srid integer)
AS '$libdir/postgis-3', 'ST_Point'
LANGUAGE 'c' IMMUTABLE STRICT PARALLEL SAFE;

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

Loading

0 comments on commit 3a0259b

Please sign in to comment.