From 1cc51292f7357a87ac3439b3a53217992744a3f9 Mon Sep 17 00:00:00 2001 From: Yvinayak07 <100944709+Yvinayak07@users.noreply.github.com> Date: Mon, 30 Sep 2024 15:48:58 +0530 Subject: [PATCH] Added hook to set common typmod for case expression (#452) 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 --- src/backend/parser/parse_expr.c | 5 +++++ src/include/parser/parse_coerce.h | 3 +++ 2 files changed, 8 insertions(+) diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index aa7524701ea..02560b40e82 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -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 @@ -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; } diff --git a/src/include/parser/parse_coerce.h b/src/include/parser/parse_coerce.h index a30dd7d8799..64b5fe90ce7 100644 --- a/src/include/parser/parse_coerce.h +++ b/src/include/parser/parse_coerce.h @@ -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 */