Skip to content

Commit d4d2385

Browse files
committed
If the alternatives for a CASE construct all have the same typmod,
use that typmod not -1 as the typmod of the CASE result. Part of response to bug#513.
1 parent e433bf5 commit d4d2385

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/backend/parser/parse_expr.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.104 2001/10/25 05:49:39 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/parser/parse_expr.c,v 1.105 2001/11/12 20:05:24 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -806,6 +806,37 @@ exprTypmod(Node *expr)
806806
case T_RelabelType:
807807
return ((RelabelType *) expr)->resulttypmod;
808808
break;
809+
case T_CaseExpr:
810+
{
811+
/*
812+
* If all the alternatives agree on type/typmod, return
813+
* that typmod, else use -1
814+
*/
815+
CaseExpr *cexpr = (CaseExpr *) expr;
816+
Oid casetype = cexpr->casetype;
817+
int32 typmod;
818+
List *arg;
819+
820+
if (!cexpr->defresult)
821+
return -1;
822+
if (exprType(cexpr->defresult) != casetype)
823+
return -1;
824+
typmod = exprTypmod(cexpr->defresult);
825+
if (typmod < 0)
826+
return -1; /* no point in trying harder */
827+
foreach(arg, cexpr->args)
828+
{
829+
CaseWhen *w = (CaseWhen *) lfirst(arg);
830+
831+
Assert(IsA(w, CaseWhen));
832+
if (exprType(w->result) != casetype)
833+
return -1;
834+
if (exprTypmod(w->result) != typmod)
835+
return -1;
836+
}
837+
return typmod;
838+
}
839+
break;
809840
default:
810841
break;
811842
}

0 commit comments

Comments
 (0)