Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/BABEL_3_X_DEV' into JIRA-BABEL…
Browse files Browse the repository at this point in the history
…-803
  • Loading branch information
Vaibhav Agarwal committed Oct 24, 2023
2 parents 81a6a5e + 8739a1e commit c8ef3c2
Show file tree
Hide file tree
Showing 101 changed files with 6,514 additions and 1,430 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;

16 changes: 3 additions & 13 deletions contrib/babelfishpg_common/sql/string_operators.sql
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
-- Wrap built-in CONCAT function to accept two text arguments.
-- This is necessary because CONCAT accepts arguments of type VARIADIC "any".
-- CONCAT also automatically handles NULL which || does not.
CREATE OR REPLACE FUNCTION sys.babelfish_concat_wrapper(leftarg text, rightarg text) RETURNS TEXT AS
$$
SELECT
CASE WHEN (current_setting('babelfishpg_tsql.concat_null_yields_null') = 'on') THEN
CASE
WHEN leftarg IS NULL OR rightarg IS NULL THEN NULL
ELSE CONCAT(leftarg, rightarg)
END
ELSE
CONCAT(leftarg, rightarg)
END
$$
LANGUAGE SQL STABLE;
CREATE OR REPLACE FUNCTION sys.babelfish_concat_wrapper(leftarg text, rightarg text) RETURNS TEXT
AS 'babelfishpg_tsql', 'babelfish_concat_wrapper'
LANGUAGE C STABLE PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.babelfish_concat_wrapper_outer(leftarg text, rightarg text) RETURNS sys.varchar(8000) AS
$$
Expand Down
Loading

0 comments on commit c8ef3c2

Please sign in to comment.