Skip to content

Commit fcc7c28

Browse files
authored
Fix behavioural differences due to return type and handle constant string literal inputs differently for T-SQL COALESCE function (#345)
This commit contains the following changes : Whenever the caller is T-SQL COALESCE, then call the select_common_type_hook with 'TSQL_COALESCE' context. If the input is a constant string literal, first we convert the UNKNOWN node to VARCHAR node and then coerce it to the common type. This change also handles empty or white space strings according to the way T-SQL handles. Issues Resolved [BABEL-726] Extension PR : babelfish-for-postgresql/babelfish_extensions#2507 Signed-off-by: Sai Rohan Basa [email protected]
1 parent 49e23fb commit fcc7c28

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/backend/parser/parse_expr.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ static Node *make_nulltest_from_distinct(ParseState *pstate,
100100
static List *ExpandChecksumStar(ParseState *pstate, FuncCall *fn, int location);
101101

102102
lookup_param_hook_type lookup_param_hook = NULL;
103+
handle_constant_literals_hook_type handle_constant_literals_hook = NULL;
103104
/*
104105
* transformExpr -
105106
* Analyze and transform expressions. Type checking and type casting is
@@ -2316,6 +2317,8 @@ transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c)
23162317

23172318
if (sql_dialect == SQL_DIALECT_TSQL && select_common_type_hook && c->tsql_is_null)
23182319
newc->coalescetype = select_common_type(pstate, newargs, "ISNULL", NULL);
2320+
else if(sql_dialect == SQL_DIALECT_TSQL && select_common_type_hook)
2321+
newc->coalescetype = select_common_type(pstate, newargs, "TSQL_COALESCE", NULL);
23192322
else
23202323
newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
23212324
/* coalescecollid will be set by parse_collate.c */
@@ -2326,6 +2329,15 @@ transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c)
23262329
Node *e = (Node *) lfirst(args);
23272330
Node *newe;
23282331

2332+
/*
2333+
* T-SQL treats constant string literals as VARCHAR. Hence,
2334+
* coercing into VARCHAR before coercing it to the common type.
2335+
*/
2336+
if (sql_dialect == SQL_DIALECT_TSQL && !c->tsql_is_null && handle_constant_literals_hook)
2337+
{
2338+
e = (*handle_constant_literals_hook)(pstate, e);
2339+
}
2340+
23292341
newe = coerce_to_common_type(pstate, e,
23302342
newc->coalescetype,
23312343
"COALESCE");

src/include/parser/parse_coerce.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,7 @@ typedef Oid (*select_common_type_hook_type) (ParseState *pstate, List *exprs, co
135135
extern PGDLLEXPORT select_common_type_hook_type select_common_type_hook;
136136
typedef int32 (*select_common_typmod_hook_type) (ParseState *pstate, List *exprs, Oid common_type);
137137

138+
typedef Node *(*handle_constant_literals_hook_type) (ParseState *pstate, Node *e);
139+
extern PGDLLEXPORT handle_constant_literals_hook_type handle_constant_literals_hook;
140+
138141
#endif /* PARSE_COERCE_H */

0 commit comments

Comments
 (0)