Skip to content

Commit

Permalink
Restrict SQL_VARIANT datatype in Partition Function (#2821)
Browse files Browse the repository at this point in the history
In TSQL, range values for the SQL_VARIANT parameter type in Partition Functions can be of different data types. However, the current implementation in Babelfish does not handle this scenario correctly. Due to this limitation, we are temporarily restricting the complete usage of the SQL_VARIANT data type as a parameter for Partition Functions in Babelfish.

Signed-off-by: Sumit Jaiswal <[email protected]>
Co-authored-by: Sumit Jaiswal <[email protected]>
  • Loading branch information
sumitj824 and Sumit Jaiswal authored Aug 5, 2024
1 parent e760636 commit f7aac56
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 521 deletions.
1 change: 1 addition & 0 deletions contrib/babelfishpg_common/src/babelfishpg_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ get_common_utility_plugin(void)
common_utility_plugin_var.is_tsql_smalldatetime_datatype = &is_tsql_smalldatetime_datatype;
common_utility_plugin_var.is_tsql_datetimeoffset_datatype = &is_tsql_datetimeoffset_datatype;
common_utility_plugin_var.is_tsql_decimal_datatype = &is_tsql_decimal_datatype;
common_utility_plugin_var.is_tsql_sqlvariant_datatype = &is_tsql_sqlvariant_datatype;
common_utility_plugin_var.is_tsql_rowversion_or_timestamp_datatype = &is_tsql_rowversion_or_timestamp_datatype;
common_utility_plugin_var.datetime_in_str = &datetime_in_str;
common_utility_plugin_var.datetime2sqlvariant = &datetime2sqlvariant;
Expand Down
1 change: 1 addition & 0 deletions contrib/babelfishpg_common/src/babelfishpg_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ typedef struct common_utility_plugin
bool (*is_tsql_smalldatetime_datatype) (Oid oid);
bool (*is_tsql_datetimeoffset_datatype) (Oid oid);
bool (*is_tsql_decimal_datatype) (Oid oid);
bool (*is_tsql_sqlvariant_datatype) (Oid oid);
bool (*is_tsql_rowversion_or_timestamp_datatype) (Oid oid);
Datum (*datetime_in_str) (char *str, Node *escontext);
Datum (*datetime2sqlvariant) (PG_FUNCTION_ARGS);
Expand Down
9 changes: 9 additions & 0 deletions contrib/babelfishpg_common/src/typecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ Oid tsql_datetime2_oid = InvalidOid;
Oid tsql_smalldatetime_oid = InvalidOid;
Oid tsql_datetimeoffset_oid = InvalidOid;
Oid tsql_decimal_oid = InvalidOid;
Oid tsql_sqlvariant_oid = InvalidOid;
Oid tsql_geography_oid = InvalidOid;
Oid tsql_geometry_oid = InvalidOid;

Expand Down Expand Up @@ -474,6 +475,14 @@ is_tsql_decimal_datatype(Oid oid)
return tsql_decimal_oid == oid;
}

bool
is_tsql_sqlvariant_datatype(Oid oid)
{
if (tsql_sqlvariant_oid == InvalidOid)
tsql_sqlvariant_oid = lookup_tsql_datatype_oid("sql_variant");
return tsql_sqlvariant_oid == oid;
}

/*
* handle_type_and_collation - is implemented to handle the domain id and
* collation id assigned to FuncExpr of the target column. (Maily for target types
Expand Down
1 change: 1 addition & 0 deletions contrib/babelfishpg_common/src/typecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ extern bool is_tsql_datetime2_datatype(Oid oid);
extern bool is_tsql_smalldatetime_datatype(Oid oid);
extern bool is_tsql_datetimeoffset_datatype(Oid oid);
extern bool is_tsql_decimal_datatype(Oid oid);
extern bool is_tsql_sqlvariant_datatype(Oid oid);

extern void handle_type_and_collation(struct Node *node, Oid typid, Oid collationid);
extern bool check_target_type_is_sys_varchar(Oid funcid);
Expand Down
4 changes: 4 additions & 0 deletions contrib/babelfishpg_tsql/src/pl_exec-2.c
Original file line number Diff line number Diff line change
Expand Up @@ -4240,6 +4240,10 @@ exec_stmt_partition_function(PLtsql_execstate *estate, PLtsql_stmt_partition_fun
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("The type '%s(max)' is not valid for this operation.", tsql_typename)));
}
else if ((*common_utility_plugin_ptr->is_tsql_sqlvariant_datatype) (typ->typoid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("The type '%s' is not yet supported for partition function in Babelfish.", tsql_typename)));

/* check if the given number of boundaries are exceeding allowed limit */
nargs = list_length(arg);
Expand Down
12 changes: 0 additions & 12 deletions test/JDBC/expected/PARTITION-vu-cleanup.out
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ GO
--------------------------------------------------
--- DROP Partitioned Index
--------------------------------------------------
DROP INDEX partition_vu_prepare_sqlvariant_index ON partition_vu_prepare_sqlvariant_table;
GO

DROP INDEX partition_vu_prepare_uniqueidentifier_index ON partition_vu_prepare_uniqueidentifier_table;
GO

Expand Down Expand Up @@ -161,9 +158,6 @@ GO
--------------------------------------------------
--- DROP Partitioned Table
--------------------------------------------------
DROP TABLE partition_vu_prepare_sqlvariant_table;
GO

DROP TABLE partition_vu_prepare_uniqueidentifier_table;
GO

Expand Down Expand Up @@ -310,9 +304,6 @@ GO
DROP PARTITION SCHEME UniqueIdentifierPartitionScheme
GO

DROP PARTITION SCHEME SqlVariantPartitionScheme
GO

DROP PARTITION SCHEME PartitionSchemeNameGreaterThan64AndLessThan128abcdefghijklmnopqrstuvwxyz
GO

Expand Down Expand Up @@ -394,9 +385,6 @@ GO
DROP PARTITION FUNCTION UniqueIdentifierPartitionFunction
GO

DROP PARTITION FUNCTION SqlVariantPartitionFunction
GO

DROP PARTITION FUNCTION PartitionFunctionNameGreaterThan64AndLessThan128abcdefghijklmnopqrstuvwxyz
GO

Expand Down
47 changes: 0 additions & 47 deletions test/JDBC/expected/PARTITION-vu-prepare.out
Original file line number Diff line number Diff line change
Expand Up @@ -341,16 +341,6 @@ CREATE PARTITION FUNCTION UniqueIdentifierPartitionFunction (uniqueidentifier)
AS RANGE RIGHT FOR VALUES ('00000000-0000-0000-0000-000000000000', 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF', '6F9619FF-8B86-D011-B42D-00C04FC964FF');
GO

CREATE PARTITION FUNCTION SqlVariantPartitionFunction (sql_variant)
AS RANGE RIGHT
FOR VALUES (
CAST('abc' AS char(5)),
CAST(N'xyz' AS nchar(5)),
CAST('Apple' AS VARCHAR(10)),
CAST(N'Banana' AS NVARCHAR(10)),
CAST('Some text' AS SQL_VARIANT)
);
go

-- identifier length greater than 64
CREATE PARTITION FUNCTION PartitionFunctionNameGreaterThan64AndLessThan128abcdefghijklmnopqrstuvwxyz (bigint)
Expand Down Expand Up @@ -474,11 +464,6 @@ PARTITION UniqueIdentifierPartitionFunction
ALL TO ([PRIMARY]);
GO

CREATE PARTITION SCHEME SqlVariantPartitionScheme AS
PARTITION SqlVariantPartitionFunction
ALL TO ([PRIMARY]);
GO

-- identifier length greater than 64
CREATE PARTITION SCHEME PartitionSchemeNameGreaterThan64AndLessThan128abcdefghijklmnopqrstuvwxyz AS
PARTITION PartitionFunctionNameGreaterThan64AndLessThan128abcdefghijklmnopqrstuvwxyz
Expand Down Expand Up @@ -832,12 +817,6 @@ CREATE TABLE partition_vu_prepare_uniqueidentifier_table (
) ON UniqueIdentifierPartitionScheme(Id);
GO

-- sql_variant data type
CREATE TABLE partition_vu_prepare_sqlvariant_table (
Id SQL_VARIANT,
Value sys.varchar(50)
) ON SqlVariantPartitionScheme(Id);
GO

-- identifier length greater than 64
CREATE TABLE PartitionTableNameGreaterThan64AndLessThan128abcdefghijklmnopqrstuvwxyz (
Expand Down Expand Up @@ -961,11 +940,6 @@ ON partition_vu_prepare_uniqueidentifier_table(Id)
ON UniqueIdentifierPartitionScheme (Id);
GO

CREATE INDEX partition_vu_prepare_sqlvariant_index
ON partition_vu_prepare_sqlvariant_table(Id)
ON SqlVariantPartitionScheme (Id);
GO

CREATE VIEW GetPartitionedIndexListFromSysIndexesView
AS
(
Expand Down Expand Up @@ -1301,27 +1275,6 @@ GO
~~ROW COUNT: 1~~


-- Insert values into partition_vu_prepare_sqlvariant_table
INSERT INTO partition_vu_prepare_sqlvariant_table (Id, Value) VALUES (CAST('abc' AS char(5)), 'Value abc');
INSERT INTO partition_vu_prepare_sqlvariant_table (Id, Value) VALUES (CAST(N'xyz' AS nchar(5)), N'Value xyz');
INSERT INTO partition_vu_prepare_sqlvariant_table (Id, Value) VALUES (CAST('Apple' AS VARCHAR(10)), 'Value Apple');
INSERT INTO partition_vu_prepare_sqlvariant_table (Id, Value) VALUES (CAST(N'Banana' AS NVARCHAR(10)), N'Value Banana');
INSERT INTO partition_vu_prepare_sqlvariant_table (Id, Value) VALUES (CAST('Some text' AS SQL_VARIANT), 'Value Some text');
INSERT INTO partition_vu_prepare_sqlvariant_table (Id, Value) VALUES (NULL, 'Value NULL');
GO
~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~

~~ROW COUNT: 1~~


---------------------------------------------------
--- Check for inconsistent metadata before upgrade
----------------------------------------------------
Expand Down
Loading

0 comments on commit f7aac56

Please sign in to comment.