Skip to content

Commit 550e11a

Browse files
authored
Fix typmod for multiple concat of binaries (babelfish-for-postgresql#4429)
This commit will fix the typmod for multiple concatenation of binaries for both base and domain binary types Task: BABEL-6296 Signed-off-by: Yashneet Vinayak
1 parent 67248ef commit 550e11a

8 files changed

+471
-4
lines changed

contrib/babelfishpg_tsql/src/hooks.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8478,8 +8478,8 @@ tsql_set_typmod_op_expr(ParseState *pstate, Node *OpExp, Node *lexpr, Node* rexp
84788478
lopr = exprType(lexpr);
84798479
ropr = exprType(rexpr);
84808480
if (strncmp(opname, "+", 1) == 0 &&
8481-
(*common_utility_plugin_ptr->is_tsql_sys_binary_datatype) (lopr) &&
8482-
(*common_utility_plugin_ptr->is_tsql_sys_binary_datatype) (ropr))
8481+
(*common_utility_plugin_ptr->is_tsql_binary_datatype) (getBaseType(lopr)) &&
8482+
(*common_utility_plugin_ptr->is_tsql_binary_datatype) (getBaseType(ropr)))
84838483
{
84848484
int32 typmod1 = exprTypmod(lexpr),
84858485
typmod2 = exprTypmod(rexpr),
@@ -8538,10 +8538,10 @@ pltsql_post_transform_expr_recurse(ParseState *pstate, Node *expr)
85388538
* RelabelType with typmod = -1, so we need to look through to the underlying
85398539
* expression to get the actual typmod value.
85408540
*/
8541-
while (lexpr && IsA(lexpr, RelabelType))
8541+
while (lexpr && IsA(lexpr, RelabelType) && exprTypmod(lexpr) == -1)
85428542
lexpr = (Node *) ((RelabelType *) lexpr)->arg;
85438543

8544-
while (rexpr && IsA(rexpr, RelabelType))
8544+
while (rexpr && IsA(rexpr, RelabelType) && exprTypmod(rexpr) == -1)
85458545
rexpr = (Node *) ((RelabelType *) rexpr)->arg;
85468546

85478547
expr = tsql_set_typmod_op_expr(pstate, expr, lexpr, rexpr);

test/JDBC/expected/babel_binary-before-17_5-or-16_9-vu-verify.out

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,3 +1413,80 @@ int
14131413
1
14141414
~~END~~
14151415

1416+
1417+
declare @bin1 binary(3) = 0x4142
1418+
declare @bin2 binary(4) = 0x4344
1419+
declare @bin3 binary(1) = 0x4546
1420+
declare @bin4 binary(6) = 0x4748
1421+
select (@bin1 + @bin2) + (@bin3 + @bin4) as NestedConcat
1422+
GO
1423+
~~START~~
1424+
binary
1425+
4142004344000045474800000000
1426+
~~END~~
1427+
1428+
1429+
declare @bin1 binary(5) = 0x4142
1430+
declare @bin2 binary(9) = 0x4344
1431+
declare @bin3 binary(12) = 0x4546
1432+
declare @bin4 binary = 0x4748
1433+
select (@bin1 + @bin2) + (@bin3 + @bin4) as NestedConcat
1434+
GO
1435+
~~START~~
1436+
binary
1437+
414200000043440000000000000045460000000000000000000047
1438+
~~END~~
1439+
1440+
1441+
declare @empty1 binary(15) = 0x00
1442+
declare @empty2 binary(1) = 0x00
1443+
declare @empty3 binary(6) = 0x00
1444+
select @empty1 + @empty2 + @empty3 as multiple_empty_concat
1445+
GO
1446+
~~START~~
1447+
binary
1448+
00000000000000000000000000000000000000000000
1449+
~~END~~
1450+
1451+
1452+
create table binary_test_table (
1453+
id int identity(1,1),
1454+
bin_col1 binary(5),
1455+
bin_col2 binary(8),
1456+
varbin_col1 varbinary(10),
1457+
varbin_col2 varbinary(6)
1458+
)
1459+
GO
1460+
1461+
-- Insert test data
1462+
insert into binary_test_table (bin_col1, bin_col2, varbin_col1, varbin_col2) values
1463+
(0x4142434445, 0x4647484950, 0x5152535455, 0x5657585960),
1464+
(0x6162636465, 0x6667686970, 0x7172737475, 0x7677787980),
1465+
(0x, 0x4142434445, 0x, 0x4647484950),
1466+
(NULL, 0x4142434445, NULL, 0x4647484950)
1467+
GO
1468+
~~ROW COUNT: 4~~
1469+
1470+
1471+
with binary_cte as (
1472+
select id,
1473+
bin_col1 + bin_col2 as concat_result,
1474+
datalength(bin_col1 + bin_col2) as concat_len
1475+
from binary_test_table
1476+
where bin_col1 is not null and bin_col2 is not null
1477+
)
1478+
select concat_result,
1479+
concat_len,
1480+
concat_result + cast(0x9999 as binary(2)) as extended_concat
1481+
from binary_cte
1482+
GO
1483+
~~START~~
1484+
binary#!#int#!#binary
1485+
41424344454647484950000000#!#13#!#414243444546474849500000009999
1486+
61626364656667686970000000#!#13#!#616263646566676869700000009999
1487+
00000000004142434445000000#!#13#!#000000000041424344450000009999
1488+
~~END~~
1489+
1490+
1491+
DROP table binary_test_table
1492+
GO

test/JDBC/expected/babel_binary-before-17_7-or-16_11-vu-verify.out

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,3 +1413,80 @@ int
14131413
1
14141414
~~END~~
14151415

1416+
1417+
declare @bin1 binary(3) = 0x4142
1418+
declare @bin2 binary(4) = 0x4344
1419+
declare @bin3 binary(1) = 0x4546
1420+
declare @bin4 binary(6) = 0x4748
1421+
select (@bin1 + @bin2) + (@bin3 + @bin4) as NestedConcat
1422+
GO
1423+
~~START~~
1424+
binary
1425+
4142004344000045474800000000
1426+
~~END~~
1427+
1428+
1429+
declare @bin1 binary(5) = 0x4142
1430+
declare @bin2 binary(9) = 0x4344
1431+
declare @bin3 binary(12) = 0x4546
1432+
declare @bin4 binary = 0x4748
1433+
select (@bin1 + @bin2) + (@bin3 + @bin4) as NestedConcat
1434+
GO
1435+
~~START~~
1436+
binary
1437+
414200000043440000000000000045460000000000000000000047
1438+
~~END~~
1439+
1440+
1441+
declare @empty1 binary(15) = 0x00
1442+
declare @empty2 binary(1) = 0x00
1443+
declare @empty3 binary(6) = 0x00
1444+
select @empty1 + @empty2 + @empty3 as multiple_empty_concat
1445+
GO
1446+
~~START~~
1447+
binary
1448+
00000000000000000000000000000000000000000000
1449+
~~END~~
1450+
1451+
1452+
create table binary_test_table (
1453+
id int identity(1,1),
1454+
bin_col1 binary(5),
1455+
bin_col2 binary(8),
1456+
varbin_col1 varbinary(10),
1457+
varbin_col2 varbinary(6)
1458+
)
1459+
GO
1460+
1461+
-- Insert test data
1462+
insert into binary_test_table (bin_col1, bin_col2, varbin_col1, varbin_col2) values
1463+
(0x4142434445, 0x4647484950, 0x5152535455, 0x5657585960),
1464+
(0x6162636465, 0x6667686970, 0x7172737475, 0x7677787980),
1465+
(0x, 0x4142434445, 0x, 0x4647484950),
1466+
(NULL, 0x4142434445, NULL, 0x4647484950)
1467+
GO
1468+
~~ROW COUNT: 4~~
1469+
1470+
1471+
with binary_cte as (
1472+
select id,
1473+
bin_col1 + bin_col2 as concat_result,
1474+
datalength(bin_col1 + bin_col2) as concat_len
1475+
from binary_test_table
1476+
where bin_col1 is not null and bin_col2 is not null
1477+
)
1478+
select concat_result,
1479+
concat_len,
1480+
concat_result + cast(0x9999 as binary(2)) as extended_concat
1481+
from binary_cte
1482+
GO
1483+
~~START~~
1484+
binary#!#int#!#binary
1485+
41424344454647484950000000#!#13#!#414243444546474849500000009999
1486+
61626364656667686970000000#!#13#!#616263646566676869700000009999
1487+
00000000004142434445000000#!#13#!#000000000041424344450000009999
1488+
~~END~~
1489+
1490+
1491+
DROP table binary_test_table
1492+
GO

test/JDBC/expected/babel_binary-vu-verify.out

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,3 +1414,80 @@ int
14141414
1
14151415
~~END~~
14161416

1417+
1418+
declare @bin1 binary(3) = 0x4142
1419+
declare @bin2 binary(4) = 0x4344
1420+
declare @bin3 binary(1) = 0x4546
1421+
declare @bin4 binary(6) = 0x4748
1422+
select (@bin1 + @bin2) + (@bin3 + @bin4) as NestedConcat
1423+
GO
1424+
~~START~~
1425+
binary
1426+
4142004344000045474800000000
1427+
~~END~~
1428+
1429+
1430+
declare @bin1 binary(5) = 0x4142
1431+
declare @bin2 binary(9) = 0x4344
1432+
declare @bin3 binary(12) = 0x4546
1433+
declare @bin4 binary = 0x4748
1434+
select (@bin1 + @bin2) + (@bin3 + @bin4) as NestedConcat
1435+
GO
1436+
~~START~~
1437+
binary
1438+
414200000043440000000000000045460000000000000000000047
1439+
~~END~~
1440+
1441+
1442+
declare @empty1 binary(15) = 0x00
1443+
declare @empty2 binary(1) = 0x00
1444+
declare @empty3 binary(6) = 0x00
1445+
select @empty1 + @empty2 + @empty3 as multiple_empty_concat
1446+
GO
1447+
~~START~~
1448+
binary
1449+
00000000000000000000000000000000000000000000
1450+
~~END~~
1451+
1452+
1453+
create table binary_test_table (
1454+
id int identity(1,1),
1455+
bin_col1 binary(5),
1456+
bin_col2 binary(8),
1457+
varbin_col1 varbinary(10),
1458+
varbin_col2 varbinary(6)
1459+
)
1460+
GO
1461+
1462+
-- Insert test data
1463+
insert into binary_test_table (bin_col1, bin_col2, varbin_col1, varbin_col2) values
1464+
(0x4142434445, 0x4647484950, 0x5152535455, 0x5657585960),
1465+
(0x6162636465, 0x6667686970, 0x7172737475, 0x7677787980),
1466+
(0x, 0x4142434445, 0x, 0x4647484950),
1467+
(NULL, 0x4142434445, NULL, 0x4647484950)
1468+
GO
1469+
~~ROW COUNT: 4~~
1470+
1471+
1472+
with binary_cte as (
1473+
select id,
1474+
bin_col1 + bin_col2 as concat_result,
1475+
datalength(bin_col1 + bin_col2) as concat_len
1476+
from binary_test_table
1477+
where bin_col1 is not null and bin_col2 is not null
1478+
)
1479+
select concat_result,
1480+
concat_len,
1481+
concat_result + cast(0x9999 as binary(2)) as extended_concat
1482+
from binary_cte
1483+
GO
1484+
~~START~~
1485+
binary#!#int#!#binary
1486+
41424344454647484950000000#!#13#!#414243444546474849500000009999
1487+
61626364656667686970000000#!#13#!#616263646566676869700000009999
1488+
00000000004142434445000000#!#13#!#000000000041424344450000009999
1489+
~~END~~
1490+
1491+
1492+
DROP table binary_test_table
1493+
GO

test/JDBC/expected/parallel_query/babel_binary-vu-verify.out

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,3 +1432,80 @@ int
14321432
1
14331433
~~END~~
14341434

1435+
1436+
declare @bin1 binary(3) = 0x4142
1437+
declare @bin2 binary(4) = 0x4344
1438+
declare @bin3 binary(1) = 0x4546
1439+
declare @bin4 binary(6) = 0x4748
1440+
select (@bin1 + @bin2) + (@bin3 + @bin4) as NestedConcat
1441+
GO
1442+
~~START~~
1443+
binary
1444+
4142004344000045474800000000
1445+
~~END~~
1446+
1447+
1448+
declare @bin1 binary(5) = 0x4142
1449+
declare @bin2 binary(9) = 0x4344
1450+
declare @bin3 binary(12) = 0x4546
1451+
declare @bin4 binary = 0x4748
1452+
select (@bin1 + @bin2) + (@bin3 + @bin4) as NestedConcat
1453+
GO
1454+
~~START~~
1455+
binary
1456+
414200000043440000000000000045460000000000000000000047
1457+
~~END~~
1458+
1459+
1460+
declare @empty1 binary(15) = 0x00
1461+
declare @empty2 binary(1) = 0x00
1462+
declare @empty3 binary(6) = 0x00
1463+
select @empty1 + @empty2 + @empty3 as multiple_empty_concat
1464+
GO
1465+
~~START~~
1466+
binary
1467+
00000000000000000000000000000000000000000000
1468+
~~END~~
1469+
1470+
1471+
create table binary_test_table (
1472+
id int identity(1,1),
1473+
bin_col1 binary(5),
1474+
bin_col2 binary(8),
1475+
varbin_col1 varbinary(10),
1476+
varbin_col2 varbinary(6)
1477+
)
1478+
GO
1479+
1480+
-- Insert test data
1481+
insert into binary_test_table (bin_col1, bin_col2, varbin_col1, varbin_col2) values
1482+
(0x4142434445, 0x4647484950, 0x5152535455, 0x5657585960),
1483+
(0x6162636465, 0x6667686970, 0x7172737475, 0x7677787980),
1484+
(0x, 0x4142434445, 0x, 0x4647484950),
1485+
(NULL, 0x4142434445, NULL, 0x4647484950)
1486+
GO
1487+
~~ROW COUNT: 4~~
1488+
1489+
1490+
with binary_cte as (
1491+
select id,
1492+
bin_col1 + bin_col2 as concat_result,
1493+
datalength(bin_col1 + bin_col2) as concat_len
1494+
from binary_test_table
1495+
where bin_col1 is not null and bin_col2 is not null
1496+
)
1497+
select concat_result,
1498+
concat_len,
1499+
concat_result + cast(0x9999 as binary(2)) as extended_concat
1500+
from binary_cte
1501+
GO
1502+
~~START~~
1503+
binary#!#int#!#binary
1504+
41424344454647484950000000#!#13#!#414243444546474849500000009999
1505+
61626364656667686970000000#!#13#!#616263646566676869700000009999
1506+
00000000004142434445000000#!#13#!#000000000041424344450000009999
1507+
~~END~~
1508+
1509+
1510+
DROP table binary_test_table
1511+
GO

0 commit comments

Comments
 (0)