Skip to content

Commit

Permalink
Added hook to set common typmod for case expression (#452)
Browse files Browse the repository at this point in the history
Issue:

\Return type of overall CASE expression when branch expression is of String Datatype, is evaluated to give typmod error message if the common type is CHAR/NCHAR.

eg. If the Common type evaluated is CHAR/NCHAR

SELECT CASE 1
    WHEN 1 THEN NULL
    WHEN 2 THEN CAST('char' AS CHAR(10))
END
GO

Output: Msg 33557097, Level 16, State 1, Server BABELFISH, Line 1
The string size for the given CHAR/NCHAR data is not defined. Please use an explicit CAST or CONVERT to CHAR(n)/NCHAR(n).

Expected output:
CHAR
NULL

Changes made to fix the issues:

Implementing a hook that calculates the resultant typmod for the CASE expression, and also set typmod for all the CASE branches when .

Task: BABEL-5103, BABEL-4332
Signed-off-by: Yashneet Vinayak <[email protected]>
  • Loading branch information
Yvinayak07 authored Sep 30, 2024
1 parent f5394b4 commit 1cc5129
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/backend/parser/parse_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ static List *ExpandChecksumStar(ParseState *pstate, FuncCall *fn, int location);

lookup_param_hook_type lookup_param_hook = NULL;
handle_constant_literals_hook_type handle_constant_literals_hook = NULL;
set_common_typmod_case_expr_hook_type set_common_typmod_case_expr_hook = NULL;
/*
* transformExpr -
* Analyze and transform expressions. Type checking and type casting is
Expand Down Expand Up @@ -1834,6 +1835,10 @@ transformCaseExpr(ParseState *pstate, CaseExpr *c)

newc->location = c->location;

/* Following hook will be used to set the typmod of all the CASE Branches. */
if (sql_dialect == SQL_DIALECT_TSQL && set_common_typmod_case_expr_hook)
(*set_common_typmod_case_expr_hook)(pstate, resultexprs, newc);

return (Node *) newc;
}

Expand Down
3 changes: 3 additions & 0 deletions src/include/parser/parse_coerce.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,7 @@ typedef int32 (*select_common_typmod_hook_type) (ParseState *pstate, List *exprs
typedef Node *(*handle_constant_literals_hook_type) (ParseState *pstate, Node *e);
extern PGDLLEXPORT handle_constant_literals_hook_type handle_constant_literals_hook;

typedef void (*set_common_typmod_case_expr_hook_type) (ParseState *pstate, List *exprs, CaseExpr *newc);
extern PGDLLIMPORT set_common_typmod_case_expr_hook_type set_common_typmod_case_expr_hook;

#endif /* PARSE_COERCE_H */

0 comments on commit 1cc5129

Please sign in to comment.