Skip to content

Commit a06a095

Browse files
sharathbpSharath BP
authored and
Sharath BP
committed
Add coalesce node support for computed columns (babelfish-for-postgresql#3552)
We invoke sys.tsql_get_expr function for getting the column definition in sys.computed_columns. At present tsql_get_expr function doesn't support Coalesce type in computed columns and return null when we try to obtain computed column expression containing Coalesce expression.Adding support for Coalesce node types for computed column expressions. Task: Babel-5460 Signed-off-by: Sharath BP <[email protected]> (cherry picked from commit ad1d762)
1 parent 33e9cdf commit a06a095

5 files changed

+20
-5
lines changed

contrib/babelfishpg_tsql/src/pltsql_ruleutils.c

+10
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,17 @@ get_rule_expr(Node *node, deparse_context *context,
14541454
appendStringInfoChar(buf, ')');
14551455
}
14561456
break;
1457+
1458+
case T_CoalesceExpr:
1459+
{
1460+
CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
14571461

1462+
appendStringInfoString(buf, "COALESCE(");
1463+
get_rule_expr((Node *) coalesceexpr->args, context, true);
1464+
appendStringInfoChar(buf, ')');
1465+
}
1466+
break;
1467+
14581468
case T_SetToDefault:
14591469
appendStringInfoString(buf, "DEFAULT");
14601470
break;

test/JDBC/expected/sys-computed_columns-vu-prepare.out

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ GO
44
CREATE TABLE sys_computed_columns_vu_prepare_t1 (
55
scc_first_number smallint,
66
scc_second_number money,
7-
scc_multiplied AS scc_first_number * scc_second_number
7+
scc_multiplied AS scc_first_number * scc_second_number,
8+
scc_coalesce_col AS COALESCE(scc_first_number, 10) PERSISTED
89
)
910
GO

test/JDBC/expected/sys-computed_columns-vu-verify.out

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ scc_multiplied#!#(scc_first_number * scc_second_number)
2525
~~END~~
2626

2727

28-
Select sys.tsql_get_expr(adbin, adrelid) FROM pg_attrdef WHERE adrelid = (select oid from pg_class where relname='sys_computed_columns_vu_prepare_t1')
28+
Select sys.tsql_get_expr(adbin, adrelid) as text FROM pg_attrdef WHERE adrelid = (select oid from pg_class where relname='sys_computed_columns_vu_prepare_t1') ORDER BY text
2929
GO
3030
~~START~~
3131
text
3232
(scc_first_number * scc_second_number)
33+
COALESCE(CAST((scc_first_number) AS int), 10)
3334
~~END~~
3435

3536

@@ -64,3 +65,4 @@ text
6465
<NULL>
6566
~~END~~
6667

68+

test/JDBC/input/views/sys-computed_columns-vu-prepare.sql

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ GO
44
CREATE TABLE sys_computed_columns_vu_prepare_t1 (
55
scc_first_number smallint,
66
scc_second_number money,
7-
scc_multiplied AS scc_first_number * scc_second_number
7+
scc_multiplied AS scc_first_number * scc_second_number,
8+
scc_coalesce_col AS COALESCE(scc_first_number, 10) PERSISTED
89
)
910
GO

test/JDBC/input/views/sys-computed_columns-vu-verify.sql

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ WHERE name in ('scc_first_number', 'scc_second_number', 'scc_multiplied')
1010
ORDER BY name
1111
GO
1212

13-
Select sys.tsql_get_expr(adbin, adrelid) FROM pg_attrdef WHERE adrelid = (select oid from pg_class where relname='sys_computed_columns_vu_prepare_t1')
13+
Select sys.tsql_get_expr(adbin, adrelid) as text FROM pg_attrdef WHERE adrelid = (select oid from pg_class where relname='sys_computed_columns_vu_prepare_t1') ORDER BY text
1414
GO
1515

1616
Select sys.tsql_get_expr('scc_second_number',123)
@@ -23,4 +23,5 @@ Select sys.tsql_get_expr('abc',0)
2323
GO
2424

2525
Select sys.tsql_get_expr(null, null)
26-
GO
26+
GO
27+

0 commit comments

Comments
 (0)