Skip to content

Commit

Permalink
Replace varcharTypeName->typeOid with baseTypeId in tsql_coerce_strin…
Browse files Browse the repository at this point in the history
…g_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 <[email protected]>
  • Loading branch information
basasairohan authored Jan 10, 2024
1 parent ee5e599 commit 16a43e5
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 6 deletions.
2 changes: 1 addition & 1 deletion contrib/babelfishpg_tsql/src/pltsql_coerce.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions test/JDBC/expected/babel-4688-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions test/JDBC/expected/babel-4688-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions test/JDBC/expected/babel-4688-vu-verify.out
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions test/JDBC/input/babel-4688-vu-cleanup.sql
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions test/JDBC/input/babel-4688-vu-prepare.sql
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions test/JDBC/input/babel-4688-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/JDBC/upgrade/latest/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,4 @@ BABEL-4231
sys_asymmetric_keys
sys_certificates
sys_database_permissions
babel-4688
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 16a43e5

Please sign in to comment.