Skip to content

Commit

Permalink
Resolved Comments
Browse files Browse the repository at this point in the history
Signed-off-by: Anikait Agrawal <[email protected]>
  • Loading branch information
Anikait Agrawal committed Oct 18, 2023
1 parent 824fb0a commit acb1f45
Show file tree
Hide file tree
Showing 23 changed files with 630 additions and 1,103 deletions.
4 changes: 2 additions & 2 deletions contrib/babelfishpg_common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ endif

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

#include ../Makefile.common

Expand Down
143 changes: 51 additions & 92 deletions contrib/babelfishpg_common/sql/geography.sql
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,10 @@ CREATE OR REPLACE FUNCTION sys.GEOGRAPHY(sys.GEOGRAPHY, integer, boolean)

CREATE CAST (sys.GEOGRAPHY AS sys.GEOGRAPHY) WITH FUNCTION sys.GEOGRAPHY(sys.GEOGRAPHY, integer, boolean) AS IMPLICIT;

CREATE OR REPLACE FUNCTION sys.GEOGRAPHY(bytea)
RETURNS sys.GEOGRAPHY
AS $$
DECLARE
len integer;
varBin bytea;
geomType bytea;
srid integer;
newVarBin bytea;
lat float8;
byte_position integer := 6;
coord_NaN bytea := E'\\x000000000000f87f';
input_coord bytea;
isNaN integer = 0;
CREATE OR REPLACE FUNCTION sys.get_valid_srids()
RETURNS integer[]
AS $$
DECLARE
valid_srids integer[] := ARRAY[
4120, 4121, 4122, 4123, 4124, 4127, 4128, 4129, 4130, 4131, 4132, 4133, 4134, 4135, 4136, 4137, 4138, 4139, 4141,
4142, 4143, 4144, 4145, 4146, 4147, 4148, 4149, 4150, 4151, 4152, 4153, 4154, 4155, 4156, 4157, 4158, 4159, 4160,
Expand All @@ -92,14 +82,43 @@ CREATE OR REPLACE FUNCTION sys.GEOGRAPHY(bytea)
4941, 4943, 4945, 4947, 4949, 4951, 4953, 4955, 4957, 4959, 4961, 4963, 4965, 4967, 4971, 4973, 4975, 4977, 4979,
4981, 4983, 4985, 4987, 4989, 4991, 4993, 4995, 4997, 4999, 7843, 7844, 104001
];
BEGIN
RETURN valid_srids;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

CREATE OR REPLACE FUNCTION sys.GEOGRAPHY(bytea)
RETURNS sys.GEOGRAPHY
AS $$
DECLARE
len integer;
varBin bytea;
geomType bytea;
srid integer;
newVarBin bytea;
lat float8;
byte_position integer := 6;
coord_NaN bytea := E'\\x000000000000f87f';
input_coord bytea;
reversed_bytea bytea := E'\\x';
i integer := 14;
isNaN integer = 0;
valid_srids integer[];
BEGIN
-- Call the function to retrieve the valid SRIDs
SELECT sys.get_valid_srids() INTO valid_srids;
varBin := $1;
len := LENGTH(varBin);
IF len >= 22 THEN
-- We are preprocessing it by removing 2 constant Geometry Type bytes -> 01 0c (currently for Point Type)
-- Then adding 5 Bytes -> 01 (little endianess) + 4 Bytes (Geography Type)
-- General Logic: We are preprocessing it by removing 2 constant Geometry Type bytes -> 01 0c (currently for Point Type) Then adding 5 Bytes -> 01 (little endianess) + 4 Bytes (Geography Type). It is expected by the driver
-- Here we are calculating SRID which is initially in little endian order
srid := (get_byte(varBin, 3) << 24) | (get_byte(varBin, 2) << 16) | (get_byte(varBin, 1) << 8) | get_byte(varBin, 0);
lat := varbinaryfloat8(CAST ((substring(varBin from 14 for 1) || substring(varBin from 13 for 1) || substring(varBin from 12 for 1) || substring(varBin from 11 for 1) || substring(varBin from 10 for 1) || substring(varBin from 9 for 1) || substring(varBin from 8 for 1) || substring(varBin from 7 for 1)) AS bbf_varbinary));
-- Here we are calculating value of latitude which is initially in little endian order to check if it lies in the range [-90, 90]
WHILE i > 6 LOOP
reversed_bytea := reversed_bytea || substring(varBin from i for 1);
i = i - 1;
END LOOP;
lat := varbinaryfloat8(CAST (reversed_bytea AS bbf_varbinary));
WHILE byte_position < len LOOP
-- Get the coordinate to check if it is NaN
input_coord := substring(varBin from byte_position + 1 for 8);
Expand All @@ -118,7 +137,7 @@ CREATE OR REPLACE FUNCTION sys.GEOGRAPHY(bytea)
RAISE EXCEPTION 'Error converting data type varbinary to geography.';
END IF;
ELSE
RAISE EXCEPTION 'Currently only Point Type(without Z and M flags) is Supported in Babelfish';
RAISE EXCEPTION 'Currently only Point Type(without Z and M flags) is Supported';
END IF;
ELSE
RAISE EXCEPTION 'Error converting data type varbinary to geography.';
Expand Down Expand Up @@ -185,31 +204,11 @@ CREATE OR REPLACE FUNCTION sys.Geography__stgeomfromtext(text, integer)
srid integer;
Geomtype text;
geom sys.GEOGRAPHY;
valid_srids integer[] := ARRAY[
4120, 4121, 4122, 4123, 4124, 4127, 4128, 4129, 4130, 4131, 4132, 4133, 4134, 4135, 4136, 4137, 4138, 4139, 4141,
4142, 4143, 4144, 4145, 4146, 4147, 4148, 4149, 4150, 4151, 4152, 4153, 4154, 4155, 4156, 4157, 4158, 4159, 4160,
4161, 4162, 4163, 4164, 4165, 4166, 4167, 4168, 4169, 4170, 4171, 4173, 4174, 4175, 4176, 4178, 4179, 4180, 4181,
4182, 4183, 4184, 4188, 4189, 4190, 4191, 4192, 4193, 4194, 4195, 4196, 4197, 4198, 4199, 4200, 4201, 4202, 4203,
4204, 4205, 4206, 4207, 4208, 4209, 4210, 4211, 4212, 4213, 4214, 4215, 4216, 4218, 4219, 4220, 4221, 4222, 4223,
4224, 4225, 4227, 4229, 4230, 4231, 4232, 4236, 4237, 4238, 4239, 4240, 4241, 4242, 4243, 4244, 4245, 4246, 4247,
4248, 4249, 4250, 4251, 4252, 4253, 4254, 4255, 4256, 4257, 4258, 4259, 4261, 4262, 4263, 4265, 4266, 4267, 4268,
4269, 4270, 4271, 4272, 4273, 4274, 4275, 4276, 4277, 4278, 4279, 4280, 4281, 4282, 4283, 4284, 4285, 4286, 4288,
4289, 4292, 4293, 4295, 4297, 4298, 4299, 4300, 4301, 4302, 4303, 4304, 4306, 4307, 4308, 4309, 4310, 4311, 4312,
4313, 4314, 4315, 4316, 4317, 4318, 4319, 4322, 4324, 4326, 4600, 4601, 4602, 4603, 4604, 4605, 4606, 4607, 4608,
4609, 4610, 4611, 4612, 4613, 4614, 4615, 4616, 4617, 4618, 4619, 4620, 4621, 4622, 4623, 4624, 4625, 4626, 4627,
4628, 4629, 4630, 4632, 4633, 4636, 4637, 4638, 4639, 4640, 4641, 4642, 4643, 4644, 4646, 4657, 4658, 4659, 4660,
4661, 4662, 4663, 4664, 4665, 4666, 4667, 4668, 4669, 4670, 4671, 4672, 4673, 4674, 4675, 4676, 4677, 4678, 4679,
4680, 4682, 4683, 4684, 4686, 4687, 4688, 4689, 4690, 4691, 4692, 4693, 4694, 4695, 4696, 4697, 4698, 4699, 4700,
4701, 4702, 4703, 4704, 4705, 4706, 4707, 4708, 4709, 4710, 4711, 4712, 4713, 4714, 4715, 4716, 4717, 4718, 4719,
4720, 4721, 4722, 4723, 4724, 4725, 4726, 4727, 4728, 4729, 4730, 4732, 4733, 4734, 4735, 4736, 4737, 4738, 4739,
4740, 4741, 4742, 4743, 4744, 4745, 4746, 4747, 4748, 4749, 4750, 4751, 4752, 4753, 4754, 4755, 4756, 4757, 4758,
4801, 4802, 4803, 4804, 4805, 4806, 4807, 4808, 4809, 4810, 4811, 4813, 4814, 4815, 4816, 4817, 4818, 4820, 4821,
4895, 4898, 4900, 4901, 4902, 4903, 4904, 4907, 4909, 4921, 4923, 4925, 4927, 4929, 4931, 4933, 4935, 4937, 4939,
4941, 4943, 4945, 4947, 4949, 4951, 4953, 4955, 4957, 4959, 4961, 4963, 4965, 4967, 4971, 4973, 4975, 4977, 4979,
4981, 4983, 4985, 4987, 4989, 4991, 4993, 4995, 4997, 4999, 7843, 7844, 104001
];
valid_srids integer[];
lat float8;
BEGIN
BEGIN
-- Call the function to retrieve the valid SRIDs
SELECT sys.get_valid_srids() INTO valid_srids;
srid := $2;
-- Here we are flipping the coordinates since Geography Datatype stores the point from STGeomFromText and STPointFromText in Reverse Order i.e. (long, lat)
geom = (SELECT sys.stgeogfromtext_helper($1, $2));
Expand All @@ -220,7 +219,7 @@ CREATE OR REPLACE FUNCTION sys.Geography__stgeomfromtext(text, integer)
-- Call the underlying function after preprocessing
-- Here we are flipping the coordinates since Geography Datatype stores the point from STGeomFromText and STPointFromText in Reverse Order i.e. (long, lat)
IF (SELECT sys.ST_Zmflag(geom)) = 1 OR (SELECT sys.ST_Zmflag(geom)) = 2 OR (SELECT sys.ST_Zmflag(geom)) = 3 THEN
RAISE EXCEPTION 'Z and M flags are currently not supported in Babelfish';
RAISE EXCEPTION 'Z and M flags are currently not supported';
ELSE
RETURN (SELECT sys.Geography__STFlipCoordinates(geom));
END IF;
Expand All @@ -230,7 +229,7 @@ CREATE OR REPLACE FUNCTION sys.Geography__stgeomfromtext(text, integer)
RAISE EXCEPTION 'Inavalid SRID';
END IF;
ELSE
RAISE EXCEPTION '% is currently not supported in Babelfish', Geomtype;
RAISE EXCEPTION '% is currently not supported', Geomtype;
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
Expand All @@ -256,30 +255,10 @@ CREATE OR REPLACE FUNCTION sys.Geography__Point(float8, float8, srid integer)
DECLARE
srid integer;
lat float8;
valid_srids integer[] := ARRAY[
4120, 4121, 4122, 4123, 4124, 4127, 4128, 4129, 4130, 4131, 4132, 4133, 4134, 4135, 4136, 4137, 4138, 4139, 4141,
4142, 4143, 4144, 4145, 4146, 4147, 4148, 4149, 4150, 4151, 4152, 4153, 4154, 4155, 4156, 4157, 4158, 4159, 4160,
4161, 4162, 4163, 4164, 4165, 4166, 4167, 4168, 4169, 4170, 4171, 4173, 4174, 4175, 4176, 4178, 4179, 4180, 4181,
4182, 4183, 4184, 4188, 4189, 4190, 4191, 4192, 4193, 4194, 4195, 4196, 4197, 4198, 4199, 4200, 4201, 4202, 4203,
4204, 4205, 4206, 4207, 4208, 4209, 4210, 4211, 4212, 4213, 4214, 4215, 4216, 4218, 4219, 4220, 4221, 4222, 4223,
4224, 4225, 4227, 4229, 4230, 4231, 4232, 4236, 4237, 4238, 4239, 4240, 4241, 4242, 4243, 4244, 4245, 4246, 4247,
4248, 4249, 4250, 4251, 4252, 4253, 4254, 4255, 4256, 4257, 4258, 4259, 4261, 4262, 4263, 4265, 4266, 4267, 4268,
4269, 4270, 4271, 4272, 4273, 4274, 4275, 4276, 4277, 4278, 4279, 4280, 4281, 4282, 4283, 4284, 4285, 4286, 4288,
4289, 4292, 4293, 4295, 4297, 4298, 4299, 4300, 4301, 4302, 4303, 4304, 4306, 4307, 4308, 4309, 4310, 4311, 4312,
4313, 4314, 4315, 4316, 4317, 4318, 4319, 4322, 4324, 4326, 4600, 4601, 4602, 4603, 4604, 4605, 4606, 4607, 4608,
4609, 4610, 4611, 4612, 4613, 4614, 4615, 4616, 4617, 4618, 4619, 4620, 4621, 4622, 4623, 4624, 4625, 4626, 4627,
4628, 4629, 4630, 4632, 4633, 4636, 4637, 4638, 4639, 4640, 4641, 4642, 4643, 4644, 4646, 4657, 4658, 4659, 4660,
4661, 4662, 4663, 4664, 4665, 4666, 4667, 4668, 4669, 4670, 4671, 4672, 4673, 4674, 4675, 4676, 4677, 4678, 4679,
4680, 4682, 4683, 4684, 4686, 4687, 4688, 4689, 4690, 4691, 4692, 4693, 4694, 4695, 4696, 4697, 4698, 4699, 4700,
4701, 4702, 4703, 4704, 4705, 4706, 4707, 4708, 4709, 4710, 4711, 4712, 4713, 4714, 4715, 4716, 4717, 4718, 4719,
4720, 4721, 4722, 4723, 4724, 4725, 4726, 4727, 4728, 4729, 4730, 4732, 4733, 4734, 4735, 4736, 4737, 4738, 4739,
4740, 4741, 4742, 4743, 4744, 4745, 4746, 4747, 4748, 4749, 4750, 4751, 4752, 4753, 4754, 4755, 4756, 4757, 4758,
4801, 4802, 4803, 4804, 4805, 4806, 4807, 4808, 4809, 4810, 4811, 4813, 4814, 4815, 4816, 4817, 4818, 4820, 4821,
4895, 4898, 4900, 4901, 4902, 4903, 4904, 4907, 4909, 4921, 4923, 4925, 4927, 4929, 4931, 4933, 4935, 4937, 4939,
4941, 4943, 4945, 4947, 4949, 4951, 4953, 4955, 4957, 4959, 4961, 4963, 4965, 4967, 4971, 4973, 4975, 4977, 4979,
4981, 4983, 4985, 4987, 4989, 4991, 4993, 4995, 4997, 4999, 7843, 7844, 104001
];
valid_srids integer[];
BEGIN
-- Call the function to retrieve the valid SRIDs
SELECT sys.get_valid_srids() INTO valid_srids;
srid := $3;
lat := $1;
IF srid = ANY(valid_srids) AND lat >= -90.0 AND lat <= 90.0 THEN
Expand Down Expand Up @@ -310,31 +289,11 @@ CREATE OR REPLACE FUNCTION sys.Geography__STPointFromText(text, integer)
srid integer;
Geomtype text;
geom sys.GEOGRAPHY;
valid_srids integer[] := ARRAY[
4120, 4121, 4122, 4123, 4124, 4127, 4128, 4129, 4130, 4131, 4132, 4133, 4134, 4135, 4136, 4137, 4138, 4139, 4141,
4142, 4143, 4144, 4145, 4146, 4147, 4148, 4149, 4150, 4151, 4152, 4153, 4154, 4155, 4156, 4157, 4158, 4159, 4160,
4161, 4162, 4163, 4164, 4165, 4166, 4167, 4168, 4169, 4170, 4171, 4173, 4174, 4175, 4176, 4178, 4179, 4180, 4181,
4182, 4183, 4184, 4188, 4189, 4190, 4191, 4192, 4193, 4194, 4195, 4196, 4197, 4198, 4199, 4200, 4201, 4202, 4203,
4204, 4205, 4206, 4207, 4208, 4209, 4210, 4211, 4212, 4213, 4214, 4215, 4216, 4218, 4219, 4220, 4221, 4222, 4223,
4224, 4225, 4227, 4229, 4230, 4231, 4232, 4236, 4237, 4238, 4239, 4240, 4241, 4242, 4243, 4244, 4245, 4246, 4247,
4248, 4249, 4250, 4251, 4252, 4253, 4254, 4255, 4256, 4257, 4258, 4259, 4261, 4262, 4263, 4265, 4266, 4267, 4268,
4269, 4270, 4271, 4272, 4273, 4274, 4275, 4276, 4277, 4278, 4279, 4280, 4281, 4282, 4283, 4284, 4285, 4286, 4288,
4289, 4292, 4293, 4295, 4297, 4298, 4299, 4300, 4301, 4302, 4303, 4304, 4306, 4307, 4308, 4309, 4310, 4311, 4312,
4313, 4314, 4315, 4316, 4317, 4318, 4319, 4322, 4324, 4326, 4600, 4601, 4602, 4603, 4604, 4605, 4606, 4607, 4608,
4609, 4610, 4611, 4612, 4613, 4614, 4615, 4616, 4617, 4618, 4619, 4620, 4621, 4622, 4623, 4624, 4625, 4626, 4627,
4628, 4629, 4630, 4632, 4633, 4636, 4637, 4638, 4639, 4640, 4641, 4642, 4643, 4644, 4646, 4657, 4658, 4659, 4660,
4661, 4662, 4663, 4664, 4665, 4666, 4667, 4668, 4669, 4670, 4671, 4672, 4673, 4674, 4675, 4676, 4677, 4678, 4679,
4680, 4682, 4683, 4684, 4686, 4687, 4688, 4689, 4690, 4691, 4692, 4693, 4694, 4695, 4696, 4697, 4698, 4699, 4700,
4701, 4702, 4703, 4704, 4705, 4706, 4707, 4708, 4709, 4710, 4711, 4712, 4713, 4714, 4715, 4716, 4717, 4718, 4719,
4720, 4721, 4722, 4723, 4724, 4725, 4726, 4727, 4728, 4729, 4730, 4732, 4733, 4734, 4735, 4736, 4737, 4738, 4739,
4740, 4741, 4742, 4743, 4744, 4745, 4746, 4747, 4748, 4749, 4750, 4751, 4752, 4753, 4754, 4755, 4756, 4757, 4758,
4801, 4802, 4803, 4804, 4805, 4806, 4807, 4808, 4809, 4810, 4811, 4813, 4814, 4815, 4816, 4817, 4818, 4820, 4821,
4895, 4898, 4900, 4901, 4902, 4903, 4904, 4907, 4909, 4921, 4923, 4925, 4927, 4929, 4931, 4933, 4935, 4937, 4939,
4941, 4943, 4945, 4947, 4949, 4951, 4953, 4955, 4957, 4959, 4961, 4963, 4965, 4967, 4971, 4973, 4975, 4977, 4979,
4981, 4983, 4985, 4987, 4989, 4991, 4993, 4995, 4997, 4999, 7843, 7844, 104001
];
valid_srids integer[];
lat float8;
BEGIN
-- Call the function to retrieve the valid SRIDs
SELECT sys.get_valid_srids() INTO valid_srids;
srid := $2;
-- Here we are flipping the coordinates since Geography Datatype stores the point from STGeomFromText and STPointFromText in Reverse Order i.e. (long, lat)
geom = (SELECT sys.stgeogfromtext_helper($1, $2));
Expand All @@ -345,7 +304,7 @@ CREATE OR REPLACE FUNCTION sys.Geography__STPointFromText(text, integer)
-- Call the underlying function after preprocessing
-- Here we are flipping the coordinates since Geography Datatype stores the point from STGeomFromText and STPointFromText in Reverse Order i.e. (long, lat)
IF (SELECT sys.ST_Zmflag(geom)) = 1 OR (SELECT sys.ST_Zmflag(geom)) = 2 OR (SELECT sys.ST_Zmflag(geom)) = 3 THEN
RAISE EXCEPTION 'Z and M flags are currently not supported in Babelfish';
RAISE EXCEPTION 'Z and M flags are currently not supported';
ELSE
RETURN (SELECT sys.Geography__STFlipCoordinates(geom));
END IF;
Expand All @@ -355,7 +314,7 @@ CREATE OR REPLACE FUNCTION sys.Geography__STPointFromText(text, integer)
RAISE EXCEPTION 'Inavalid SRID';
END IF;
ELSE
RAISE EXCEPTION '% is currently not supported in Babelfish', Geomtype;
RAISE EXCEPTION '% is currently not supported', Geomtype;
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;
Expand Down
10 changes: 5 additions & 5 deletions contrib/babelfishpg_common/sql/geometry.sql
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ CREATE OR REPLACE FUNCTION sys.Geometry__stgeomfromtext(text, integer)
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 'Z and M flags are currently not supported in Babelfish';
RAISE EXCEPTION 'Z and M flags are currently not supported';
ELSE
RETURN geom;
END IF;
ELSE
RAISE EXCEPTION '% is currently not supported in Babelfish', Geomtype;
RAISE EXCEPTION '% is currently not supported', Geomtype;
END IF;
ELSE
RAISE EXCEPTION 'SRID value should be between 0 and 999999';
Expand Down Expand Up @@ -141,7 +141,7 @@ CREATE OR REPLACE FUNCTION sys.GEOMETRY(bytea)
IF encode(geomType, 'hex') = encode(E'\\x010c', 'hex') THEN
newVarBin := E'\\x0101000020' || varBin;
ELSE
RAISE EXCEPTION 'Currently only Point Type(without Z and M flags) is Supported in Babelfish';
RAISE EXCEPTION 'Currently only Point Type(without Z and M flags) is Supported';
END IF;
ELSE
RAISE EXCEPTION 'Error converting data type varbinary to geometry.';
Expand Down Expand Up @@ -225,12 +225,12 @@ CREATE OR REPLACE FUNCTION sys.Geometry__STPointFromText(text, integer)
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 'Z and M flags are currently not supported in Babelfish';
RAISE EXCEPTION 'Z and M flags are currently not supported';
ELSE
RETURN geom;
END IF;
ELSE
RAISE EXCEPTION '% is currently not supported in Babelfish', Geomtype;
RAISE EXCEPTION '% is currently not supported', Geomtype;
END IF;
ELSE
RAISE EXCEPTION 'SRID value should be between 0 and 999999';
Expand Down
Loading

0 comments on commit acb1f45

Please sign in to comment.