Skip to content

Commit

Permalink
Added support for STDistance, STAsText, STAsBinary functions
Browse files Browse the repository at this point in the history
Signed-off-by: Anikait Agrawal <[email protected]>
  • Loading branch information
Anikait Agrawal committed Nov 18, 2023
1 parent c77d348 commit afc89da
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 35 deletions.
6 changes: 3 additions & 3 deletions contrib/babelfishpg_common/sql/geography.sql
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ CREATE OR REPLACE FUNCTION sys.Geography__stgeomfromtext(text, integer)
ELSEIF lat < -90.0 OR lat > 90.0 THEN
RAISE EXCEPTION 'Latitude values must be between -90 and 90 degrees';
ELSE
RAISE EXCEPTION 'Inavalid SRID';
RAISE EXCEPTION 'The spatial reference identifier (SRID) is not valid.';
END IF;
ELSE
RAISE EXCEPTION '% is not supported', Geomtype;
Expand Down Expand Up @@ -358,7 +358,7 @@ CREATE OR REPLACE FUNCTION sys.Geography__Point(float8, float8, srid integer)
ELSEIF lat < -90.0 OR lat > 90.0 THEN
RAISE EXCEPTION 'Latitude values must be between -90 and 90 degrees';
ELSE
RAISE EXCEPTION 'Inavalid SRID';
RAISE EXCEPTION 'The spatial reference identifier (SRID) is not valid.';
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
Expand Down Expand Up @@ -402,7 +402,7 @@ CREATE OR REPLACE FUNCTION sys.Geography__STPointFromText(text, integer)
ELSEIF lat < -90.0 OR lat > 90.0 THEN
RAISE EXCEPTION 'Latitude values must be between -90 and 90 degrees';
ELSE
RAISE EXCEPTION 'Inavalid SRID';
RAISE EXCEPTION 'The spatial reference identifier (SRID) is not valid.';
END IF;
ELSE
RAISE EXCEPTION '% is not supported', Geomtype;
Expand Down
6 changes: 3 additions & 3 deletions contrib/babelfishpg_common/sql/geometry.sql
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ CREATE OR REPLACE FUNCTION sys.Geometry__stgeomfromtext(text, integer)
RAISE EXCEPTION '% is not supported', Geomtype;
END IF;
ELSE
RAISE EXCEPTION 'SRID value should be between 0 and 999999';
RAISE EXCEPTION 'The spatial reference identifier (SRID) is not valid. SRIDs must be between 0 and 999999.';
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
Expand Down Expand Up @@ -261,7 +261,7 @@ CREATE OR REPLACE FUNCTION sys.Geometry__Point(float8, float8, srid integer)
-- Call the underlying function after preprocessing
RETURN (SELECT sys.GeomPoint_helper($1, $2, $3));
ELSE
RAISE EXCEPTION 'SRID value should be between 0 and 999999';
RAISE EXCEPTION 'The spatial reference identifier (SRID) is not valid. SRIDs must be between 0 and 999999.';
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
Expand Down Expand Up @@ -294,7 +294,7 @@ CREATE OR REPLACE FUNCTION sys.Geometry__STPointFromText(text, integer)
RAISE EXCEPTION '% is not supported', Geomtype;
END IF;
ELSE
RAISE EXCEPTION 'SRID value should be between 0 and 999999';
RAISE EXCEPTION 'The spatial reference identifier (SRID) is not valid. SRIDs must be between 0 and 999999.';
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ CREATE OR REPLACE FUNCTION sys.Geometry__stgeomfromtext(text, integer)
RAISE EXCEPTION '% is not supported', Geomtype;
END IF;
ELSE
RAISE EXCEPTION 'SRID value should be between 0 and 999999';
RAISE EXCEPTION 'The spatial reference identifier (SRID) is not valid. SRIDs must be between 0 and 999999.';
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
Expand Down Expand Up @@ -265,7 +265,7 @@ CREATE OR REPLACE FUNCTION sys.Geometry__Point(float8, float8, srid integer)
-- Call the underlying function after preprocessing
RETURN (SELECT sys.GeomPoint_helper($1, $2, $3));
ELSE
RAISE EXCEPTION 'SRID value should be between 0 and 999999';
RAISE EXCEPTION 'The spatial reference identifier (SRID) is not valid. SRIDs must be between 0 and 999999.';
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
Expand Down Expand Up @@ -298,7 +298,7 @@ CREATE OR REPLACE FUNCTION sys.Geometry__STPointFromText(text, integer)
RAISE EXCEPTION '% is not supported', Geomtype;
END IF;
ELSE
RAISE EXCEPTION 'SRID value should be between 0 and 999999';
RAISE EXCEPTION 'The spatial reference identifier (SRID) is not valid. SRIDs must be between 0 and 999999.';
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
Expand Down Expand Up @@ -710,7 +710,7 @@ CREATE OR REPLACE FUNCTION sys.Geography__stgeomfromtext(text, integer)
ELSEIF lat < -90.0 OR lat > 90.0 THEN
RAISE EXCEPTION 'Latitude values must be between -90 and 90 degrees';
ELSE
RAISE EXCEPTION 'Inavalid SRID';
RAISE EXCEPTION 'The spatial reference identifier (SRID) is not valid.';
END IF;
ELSE
RAISE EXCEPTION '% is not supported', Geomtype;
Expand Down Expand Up @@ -751,7 +751,7 @@ CREATE OR REPLACE FUNCTION sys.Geography__Point(float8, float8, srid integer)
ELSEIF lat < -90.0 OR lat > 90.0 THEN
RAISE EXCEPTION 'Latitude values must be between -90 and 90 degrees';
ELSE
RAISE EXCEPTION 'Inavalid SRID';
RAISE EXCEPTION 'The spatial reference identifier (SRID) is not valid.';
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
Expand Down Expand Up @@ -795,7 +795,7 @@ CREATE OR REPLACE FUNCTION sys.Geography__STPointFromText(text, integer)
ELSEIF lat < -90.0 OR lat > 90.0 THEN
RAISE EXCEPTION 'Latitude values must be between -90 and 90 degrees';
ELSE
RAISE EXCEPTION 'Inavalid SRID';
RAISE EXCEPTION 'The spatial reference identifier (SRID) is not valid.';
END IF;
ELSE
RAISE EXCEPTION '% is not supported', Geomtype;
Expand Down
75 changes: 75 additions & 0 deletions contrib/babelfishpg_tsql/src/pl_comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ static void add_dummy_return(PLtsql_function *function);
static void add_decl_table(PLtsql_function *function, int tbl_dno, char *tbl_typ);
static Node *pltsql_pre_column_ref(ParseState *pstate, ColumnRef *cref);
static Node *pltsql_post_column_ref(ParseState *pstate, ColumnRef *cref, Node *var);
static List *pltsql_post_func_ref(ParseState *pstate, FuncCall *fn, List *fargs);
static void pltsql_post_expand_star(ParseState *pstate, ColumnRef *cref, List *l);
static Node *pltsql_param_ref(ParseState *pstate, ParamRef *pref);
static Node *resolve_column_ref(ParseState *pstate, PLtsql_expr *expr,
Expand All @@ -148,6 +149,7 @@ static void pltsql_HashTableInsert(PLtsql_function *function,
static void pltsql_HashTableDelete(PLtsql_function *function);
static void delete_function(PLtsql_function *func);
static Node *resolve_geospatial_func_ref(ParseState *pstate, ColumnRef *cref);
static List *resolve_geospatial_func_ref_2(ParseState *pstate, FuncCall *fn, List *fargs);
static char *resolve_schema_name(char *db_name, char *schema_name, MigrationMode mode);

extern Portal ActivePortal;
Expand Down Expand Up @@ -1486,6 +1488,7 @@ pltsql_parser_setup(struct ParseState *pstate, PLtsql_expr *expr)
{
pstate->p_pre_columnref_hook = pltsql_pre_column_ref;
pstate->p_post_columnref_hook = pltsql_post_column_ref;
pstate->p_post_funcref_hook = pltsql_post_func_ref;
pstate->p_post_expand_star_hook = pltsql_post_expand_star;
pstate->p_paramref_hook = pltsql_param_ref;
/* no need to use p_coerce_param_hook */
Expand Down Expand Up @@ -1654,6 +1657,18 @@ pltsql_param_ref(ParseState *pstate, ParamRef *pref)
return make_datum_param(expr, nse->itemno, pref->location);
}

static List *
pltsql_post_func_ref(ParseState *pstate, FuncCall *fn, List *fargs)
{
List *ret = resolve_geospatial_func_ref_2(pstate, fn, fargs);
if (ret)
{
elog(WARNING, "got some rets %d", ret->length);
return ret;
}
return fargs;
}

static char *
resolve_schema_name(char *db_name, char *schema_name, MigrationMode mode)
{
Expand Down Expand Up @@ -1751,6 +1766,66 @@ resolve_geospatial_func_ref(ParseState *pstate, ColumnRef *cref)
return NULL;
}

static List *resolve_geospatial_func_ref_2(ParseState *pstate, FuncCall *fn, List *fargs)
{
Node *fname;

if(list_length(fn->funcname) > 1 && list_length(fn->args) <= 1 && fn->agg_order == NULL && fn->agg_filter == NULL
&& !fn->agg_within_group && !fn->agg_star && !fn->agg_distinct && !fn->func_variadic)
{
Node *col;
bool flag = true;
fname = (Node *) llast(fn->funcname);

if (IsA(fname, String) && !isGeospatialFunction &&
((pg_strcasecmp(strVal(fname), "stdistance") == 0 && list_length(fn->args) == 1) ||
(pg_strcasecmp(strVal(fname), "stastext") == 0 && list_length(fn->args) == 0) ||
(pg_strcasecmp(strVal(fname), "stasbinary") == 0 && list_length(fn->args) == 0)))
{
ColumnRef *cref = makeNode(ColumnRef);
cref->fields = list_copy(fn->funcname);
cref->fields = list_delete_last(cref->fields);

isGeospatialFunction = true;
if(list_length(cref->fields) == 1)
{
Node *schema = (Node *) linitial(cref->fields); // a for wrong, master_a for right
char *cur_db = get_cur_db_name(); //master
String *new_schema;

if (!is_shared_schema(strVal(schema)))
{
new_schema = makeString(resolve_schema_name(cur_db, strVal(schema), get_migration_mode()));
cref->fields = list_delete_first(cref->fields);
cref->fields = lcons(new_schema, cref->fields);
}
}

PG_TRY();
{
col = transformExpr(pstate, (Node *) cref, pstate->p_expr_kind);
}
PG_CATCH();
{
flag = false;
}
PG_END_TRY();

if(flag)
{
fn->args = lcons(cref, fn->args);
fargs = lcons(col, fargs);
fn->funcname = list_delete_first(fn->funcname);
isGeospatialFunction = false;
return fargs;
}
}
}
isGeospatialFunction = false;

return NULL;
}

/*
* resolve_column_ref attempt to resolve a ColumnRef as a pltsql var
*
Expand Down
10 changes: 5 additions & 5 deletions test/JDBC/expected/TestSpatialPoint-vu-prepare.out
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ INSERT INTO SPATIALPOINTGEOM_dt (location) VALUES ( geometry::STGeomFromText('Po
INSERT INTO SPATIALPOINTGEOM_dt (location) VALUES ( geometry::STGeomFromText('Point(47.65100 -22.34900)', 1000000000 ) )
~~ERROR (Code: 33557097)~~

~~ERROR (Message: SRID value should be between 0 and 999999)~~
~~ERROR (Message: The spatial reference identifier (SRID) is not valid. SRIDs must be between 0 and 999999.)~~


#Negative Test for STGeomFromText with SRID < 0
#SRID should be between 0 to 999999
INSERT INTO SPATIALPOINTGEOM_dt (location) VALUES ( geometry::STGeomFromText('Point(47.65100 -22.34900)', -1) )
~~ERROR (Code: 33557097)~~

~~ERROR (Message: SRID value should be between 0 and 999999)~~
~~ERROR (Message: The spatial reference identifier (SRID) is not valid. SRIDs must be between 0 and 999999.)~~


#Negative Test for STGeomFromText when a coordinate is missing
Expand Down Expand Up @@ -191,7 +191,7 @@ INSERT INTO SPATIALPOINTGEOG_dt (location) VALUES ( geography::STGeomFromText('P
INSERT INTO SPATIALPOINTGEOG_dt (location) VALUES ( geography::STGeomFromText('Point(47.65100 -22.34900)', 0) )
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Inavalid SRID)~~
~~ERROR (Message: The spatial reference identifier (SRID) is not valid.)~~


#Negative Test for STGeomFromText for Geography when lat > 90 or < -90
Expand Down Expand Up @@ -222,14 +222,14 @@ INSERT INTO SPATIALPOINTGEOG_dt (location) VALUES ( STGeomFromText('Point(47.651
INSERT INTO SPATIALPOINTGEOG_dt (location) VALUES ( geography::STGeomFromText('Point(47.65100 -22.34900)', 1000000000 ) )
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Inavalid SRID)~~
~~ERROR (Message: The spatial reference identifier (SRID) is not valid.)~~


#Negative Test for STGeomFromText with SRID < 0
INSERT INTO SPATIALPOINTGEOG_dt (location) VALUES ( geography::STGeomFromText('Point(47.65100 -22.34900)', -1) )
~~ERROR (Code: 33557097)~~

~~ERROR (Message: Inavalid SRID)~~
~~ERROR (Message: The spatial reference identifier (SRID) is not valid.)~~


#Negative Test for STGeomFromText when a coordinate is missing
Expand Down
12 changes: 6 additions & 6 deletions test/JDBC/expected/TestSpatialPoint-vu-verify.out
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DECLARE @point geometry;
SET @point = geometry::STPointFromText('POINT(-122.34900 47.65100)', 4326);
SELECT STAsText(@point);
SELECT @point.STAsText();
Go
~~START~~
text
Expand All @@ -10,7 +10,7 @@ POINT(-122.349 47.651)

DECLARE @point geometry;
SET @point = geometry::POINT(22.34900, -47.65100, 4326);
SELECT STAsText(@point);
SELECT @point.STAsText();
Go
~~START~~
text
Expand Down Expand Up @@ -53,7 +53,7 @@ float
DECLARE @point1 geometry, @point2 geometry;
SET @point1 = geometry::STPointFromText('POINT(-122.34900 47.65100)', 4326);
SET @point2 = geometry::STGeomFromText('POINT(-122.35000 47.65000)', 4326);
SELECT STDistance(@point1, @point2);
SELECT @point1.STDistance(@point2);
Go
~~START~~
float
Expand Down Expand Up @@ -446,7 +446,7 @@ E6100000010C17D9CEF753D34740D34D6210585936C0

DECLARE @point geography;
SET @point = geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326);
SELECT STAsText(@point);
SELECT @point.STAsText();
Go
~~START~~
text
Expand All @@ -456,7 +456,7 @@ POINT(-122.349 47.651)

DECLARE @point geography;
SET @point = geography::POINT(22.34900, -47.65100, 4326);
SELECT STAsText(@point);
SELECT @point.STAsText();
Go
~~START~~
text
Expand Down Expand Up @@ -499,7 +499,7 @@ float
DECLARE @point1 geography, @point2 geography;
SET @point1 = geography::STPointFromText('POINT(-122.34900 47.65100)', 4326);
SET @point2 = geography::STGeomFromText('POINT(-122.35000 47.65000)', 4326);
SELECT STDistance(@point1, @point2);
SELECT @point1.STDistance(@point2);
Go
~~START~~
float
Expand Down
12 changes: 6 additions & 6 deletions test/JDBC/input/datatypes/TestSpatialPoint-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
DECLARE @point geometry;
SET @point = geometry::STPointFromText('POINT(-122.34900 47.65100)', 4326);
SELECT STAsText(@point);
SELECT @point.STAsText();
Go

DECLARE @point geometry;
SET @point = geometry::POINT(22.34900, -47.65100, 4326);
SELECT STAsText(@point);
SELECT @point.STAsText();
Go

DECLARE @point geometry;
Expand All @@ -23,7 +23,7 @@ Go
DECLARE @point1 geometry, @point2 geometry;
SET @point1 = geometry::STPointFromText('POINT(-122.34900 47.65100)', 4326);
SET @point2 = geometry::STGeomFromText('POINT(-122.35000 47.65000)', 4326);
SELECT STDistance(@point1, @point2);
SELECT @point1.STDistance(@point2);
Go

SELECT * FROM TextFromGeom;
Expand Down Expand Up @@ -138,12 +138,12 @@ GO

DECLARE @point geography;
SET @point = geography::STGeomFromText('POINT(-122.34900 47.65100)', 4326);
SELECT STAsText(@point);
SELECT @point.STAsText();
Go

DECLARE @point geography;
SET @point = geography::POINT(22.34900, -47.65100, 4326);
SELECT STAsText(@point);
SELECT @point.STAsText();
Go

DECLARE @point geography;
Expand All @@ -161,7 +161,7 @@ Go
DECLARE @point1 geography, @point2 geography;
SET @point1 = geography::STPointFromText('POINT(-122.34900 47.65100)', 4326);
SET @point2 = geography::STGeomFromText('POINT(-122.35000 47.65000)', 4326);
SELECT STDistance(@point1, @point2);
SELECT @point1.STDistance(@point2);
Go

SELECT * FROM TextFromGeog;
Expand Down
6 changes: 3 additions & 3 deletions test/dotnet/ExpectedOutput/TestPoint.out
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#Q#INSERT INTO POINTGEOG_dt(location) values(geography::STGeomFromText('Point(47.65100 -22.34900)', 4326))
#Q#INSERT INTO POINTGEOG_dt(location) values(geography::STPointFromText('Point(47.65100 -22.34900)', 4326))
#Q#INSERT INTO POINTGEOG_dt(location) values(geography::Point(47.65100, -22.34900, 4326))
#Q#SELECT STAsText(location) FROM POINTGEOM_dt;
#Q#SELECT location.STAsText() FROM POINTGEOM_dt;
#D#text
POINT(47.651 -22.349)
POINT(1 2)
Expand All @@ -33,14 +33,14 @@ POINT(47.651 -22.349)
POINT(47.651 -22.349)
POINT(47.651 -22.349)
POINT(47.651 -22.349)
#Q#SELECT STAsText(location) FROM POINTGEOG_dt;
#Q#SELECT location.STAsText() FROM POINTGEOG_dt;
#D#text
POINT(47.651 -22.349)
POINT(1 2)
POINT(47.651 -22.349)
POINT(47.651 -22.349)
POINT(-22.349 47.651)
#Q#SELECT STAsText(geom), STAsText(geog) FROM POINT_dt;
#Q#SELECT geom.STAsText(), geog.STAsText() FROM POINT_dt;
#D#text#!#text
POINT(47.651 -22.349)#!#
#!#POINT(47.651 -22.349)
Expand Down
Loading

0 comments on commit afc89da

Please sign in to comment.