From 16a43e53e78918ed14bde0d317e4cec0f14f44f6 Mon Sep 17 00:00:00 2001 From: Sai Rohan Basa <108261379+basasairohan@users.noreply.github.com> Date: Wed, 10 Jan 2024 18:18:02 +0530 Subject: [PATCH] Replace varcharTypeName->typeOid with baseTypeId in tsql_coerce_string_literal_hook() to get the correct typeId (#2235) Currently, in tsql_coerce_string_literal_hook(), we are using varcharTypeName->typeOid without actually being set. This leads to error while dumping objects containing cast from string literal to binary type during MVU. For example: -- T-SQL endpoint 1> create view babel_4475_v1 as select cast(cast('a' as binary(2)) as varchar(2)) 2> go -- PSQL endpoint jdbc_testdb=# select pg_catalog.pg_get_viewdef(oid, true) from pg_class where relname = 'babel_4475_v1'; ERROR: cache lookup failed for type 0 This same also being done during dump & restore. This commit aims to fix this issue by appropriately setting base type id in tsql_coerce_string_literal_hook. Task: BABEL-4688 Signed-off-by: Sai Rohan Basa --- contrib/babelfishpg_tsql/src/pltsql_coerce.c | 2 +- test/JDBC/expected/babel-4688-vu-cleanup.out | 11 ++++++ test/JDBC/expected/babel-4688-vu-prepare.out | 17 +++++++++ test/JDBC/expected/babel-4688-vu-verify.out | 37 +++++++++++++++++++ test/JDBC/input/babel-4688-vu-cleanup.sql | 11 ++++++ test/JDBC/input/babel-4688-vu-prepare.sql | 17 +++++++++ test/JDBC/input/babel-4688-vu-verify.sql | 17 +++++++++ test/JDBC/upgrade/latest/schedule | 1 + .../expected_dependency.out | 5 --- 9 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 test/JDBC/expected/babel-4688-vu-cleanup.out create mode 100644 test/JDBC/expected/babel-4688-vu-prepare.out create mode 100644 test/JDBC/expected/babel-4688-vu-verify.out create mode 100644 test/JDBC/input/babel-4688-vu-cleanup.sql create mode 100644 test/JDBC/input/babel-4688-vu-prepare.sql create mode 100644 test/JDBC/input/babel-4688-vu-verify.sql diff --git a/contrib/babelfishpg_tsql/src/pltsql_coerce.c b/contrib/babelfishpg_tsql/src/pltsql_coerce.c index e1e36c977b..c5af2cc0b8 100644 --- a/contrib/babelfishpg_tsql/src/pltsql_coerce.c +++ b/contrib/babelfishpg_tsql/src/pltsql_coerce.c @@ -1086,7 +1086,7 @@ tsql_coerce_string_literal_hook(ParseCallbackState *pcbstate, Oid targetTypeId, typenameTypeIdAndMod(NULL, (const TypeName *)varcharTypeName, &baseTypeId, &baseTypeMod); - tempcon = makeConst(varcharTypeName->typeOid, -1, + tempcon = makeConst(baseTypeId, -1, tsql_get_server_collation_oid_internal(false), -1, PointerGetDatum(cstring_to_text(value)), false, false); diff --git a/test/JDBC/expected/babel-4688-vu-cleanup.out b/test/JDBC/expected/babel-4688-vu-cleanup.out new file mode 100644 index 0000000000..f1e76e84b1 --- /dev/null +++ b/test/JDBC/expected/babel-4688-vu-cleanup.out @@ -0,0 +1,11 @@ +drop view babel_4688_v1 +go + +drop view babel_4688_v2 +go + +drop view babel_4688_v3 +go + +drop view babel_4688_v4 +go diff --git a/test/JDBC/expected/babel-4688-vu-prepare.out b/test/JDBC/expected/babel-4688-vu-prepare.out new file mode 100644 index 0000000000..32019eb36f --- /dev/null +++ b/test/JDBC/expected/babel-4688-vu-prepare.out @@ -0,0 +1,17 @@ +create view babel_4688_v1 as select cast(cast('a' as binary(2)) as varchar(2)) +go + +create view babel_4688_v2 as select cast(cast('a' as binary(2)) as pg_catalog.varchar(2)) +go + +EXEC sp_babelfish_configure 'babelfishpg_tsql.escape_hatch_rowversion', 'ignore'; +go + +create view babel_4688_v3 as select cast(cast('ab' as rowversion) as varchar(2)) +go + +create view babel_4688_v4 as select cast(cast('ab' as rowversion) as pg_catalog.varchar(2)) +go + +EXEC sp_babelfish_configure 'babelfishpg_tsql.escape_hatch_rowversion', 'strict'; +go diff --git a/test/JDBC/expected/babel-4688-vu-verify.out b/test/JDBC/expected/babel-4688-vu-verify.out new file mode 100644 index 0000000000..be1b0a26a4 --- /dev/null +++ b/test/JDBC/expected/babel-4688-vu-verify.out @@ -0,0 +1,37 @@ +select * from babel_4688_v1 +go +~~START~~ +varchar +a +~~END~~ + + +select * from babel_4688_v2 +go +~~START~~ +varchar +a +~~END~~ + + +EXEC sp_babelfish_configure 'babelfishpg_tsql.escape_hatch_rowversion', 'ignore'; +go + +select * from babel_4688_v3 +go +~~START~~ +varchar +ab +~~END~~ + + +select * from babel_4688_v4 +go +~~START~~ +varchar +ab +~~END~~ + + +EXEC sp_babelfish_configure 'babelfishpg_tsql.escape_hatch_rowversion', 'strict'; +go diff --git a/test/JDBC/input/babel-4688-vu-cleanup.sql b/test/JDBC/input/babel-4688-vu-cleanup.sql new file mode 100644 index 0000000000..f1e76e84b1 --- /dev/null +++ b/test/JDBC/input/babel-4688-vu-cleanup.sql @@ -0,0 +1,11 @@ +drop view babel_4688_v1 +go + +drop view babel_4688_v2 +go + +drop view babel_4688_v3 +go + +drop view babel_4688_v4 +go diff --git a/test/JDBC/input/babel-4688-vu-prepare.sql b/test/JDBC/input/babel-4688-vu-prepare.sql new file mode 100644 index 0000000000..32019eb36f --- /dev/null +++ b/test/JDBC/input/babel-4688-vu-prepare.sql @@ -0,0 +1,17 @@ +create view babel_4688_v1 as select cast(cast('a' as binary(2)) as varchar(2)) +go + +create view babel_4688_v2 as select cast(cast('a' as binary(2)) as pg_catalog.varchar(2)) +go + +EXEC sp_babelfish_configure 'babelfishpg_tsql.escape_hatch_rowversion', 'ignore'; +go + +create view babel_4688_v3 as select cast(cast('ab' as rowversion) as varchar(2)) +go + +create view babel_4688_v4 as select cast(cast('ab' as rowversion) as pg_catalog.varchar(2)) +go + +EXEC sp_babelfish_configure 'babelfishpg_tsql.escape_hatch_rowversion', 'strict'; +go diff --git a/test/JDBC/input/babel-4688-vu-verify.sql b/test/JDBC/input/babel-4688-vu-verify.sql new file mode 100644 index 0000000000..852aee2cb0 --- /dev/null +++ b/test/JDBC/input/babel-4688-vu-verify.sql @@ -0,0 +1,17 @@ +select * from babel_4688_v1 +go + +select * from babel_4688_v2 +go + +EXEC sp_babelfish_configure 'babelfishpg_tsql.escape_hatch_rowversion', 'ignore'; +go + +select * from babel_4688_v3 +go + +select * from babel_4688_v4 +go + +EXEC sp_babelfish_configure 'babelfishpg_tsql.escape_hatch_rowversion', 'strict'; +go diff --git a/test/JDBC/upgrade/latest/schedule b/test/JDBC/upgrade/latest/schedule index 27c4509273..e0c209d4a2 100644 --- a/test/JDBC/upgrade/latest/schedule +++ b/test/JDBC/upgrade/latest/schedule @@ -419,3 +419,4 @@ BABEL-4231 sys_asymmetric_keys sys_certificates sys_database_permissions +babel-4688 diff --git a/test/python/expected/upgrade_validation/expected_dependency.out b/test/python/expected/upgrade_validation/expected_dependency.out index 0a210dd4de..44efa1a522 100644 --- a/test/python/expected/upgrade_validation/expected_dependency.out +++ b/test/python/expected/upgrade_validation/expected_dependency.out @@ -237,8 +237,6 @@ Function sys.binaryint2(sys.bbf_binary) Function sys.binaryint4(sys.bbf_binary) Function sys.binaryint8(sys.bbf_binary) Function sys.binaryrowversion(sys.bbf_binary,integer,boolean) -Function sys.binarysysvarchar(sys.bbf_binary) -Function sys.binaryvarchar(sys.bbf_binary) Function sys.bit2fixeddec(sys."bit") Function sys.bit2int2(sys."bit") Function sys.bit2int4(sys."bit") @@ -523,9 +521,7 @@ Function sys.rowversionbinary(sys.rowversion,integer,boolean) Function sys.rowversionint2(sys.rowversion) Function sys.rowversionint4(sys.rowversion) Function sys.rowversionint8(sys.rowversion) -Function sys.rowversionsysvarchar(sys.rowversion) Function sys.rowversionvarbinary(sys.rowversion,integer,boolean) -Function sys.rowversionvarchar(sys.rowversion) Function sys.schema_name() Function sys.scope_identity() Function sys.servername() @@ -694,7 +690,6 @@ Function sys.varchar_sqlvariant(sys."varchar") Function sys.varchar_to_name(character varying) Function sys.varchar_to_name(sys."varchar") Function sys.varcharbinary(character varying,integer,boolean) -Function sys.varcharbinary(sys."varchar",integer,boolean) Function sys.varcharcmp(sys."varchar",sys."varchar") Function sys.varcharrowversion(character varying,integer,boolean) Function sys.varcharrowversion(sys."varchar",integer,boolean)