Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbedd04a

Browse files
author
Thomas G. Lockhart
committed
Implement CASE expression.
1 parent19740e2 commitbedd04a

File tree

17 files changed

+777
-108
lines changed

17 files changed

+777
-108
lines changed

‎src/backend/executor/execQual.c‎

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/executor/execQual.c,v 1.38 1998/11/27 19:52:00 vadim Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/executor/execQual.c,v 1.39 1998/12/04 15:33:19 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -20,7 +20,7 @@
2020
* NOTES
2121
*ExecEvalExpr() and ExecEvalVar() are hotspots.making these faster
2222
*will speed up the entire system. Unfortunately they are currently
23-
*implemented recursively.. Eliminating the recursion is bound to
23+
*implemented recursively. Eliminating the recursion is bound to
2424
*improve the speed of the executor.
2525
*
2626
*ExecTargetList() is used to make tuple projections. Rather then
@@ -205,7 +205,7 @@ ExecEvalAggreg(Aggreg *agg, ExprContext *econtext, bool *isNull)
205205
*variable with respect to given expression context.
206206
*
207207
*
208-
*As an entry condition, we expect that thethedatatype the
208+
*As an entry condition, we expect that the datatype the
209209
*plan expects to get (as told by our "variable" argument) is in
210210
*fact the datatype of the attribute the plan says to fetch (as
211211
*seen in the current context, identified by our "econtext"
@@ -1124,6 +1124,79 @@ ExecEvalAnd(Expr *andExpr, ExprContext *econtext, bool *isNull)
11241124
returnconst_value;
11251125
}
11261126

1127+
/* ----------------------------------------------------------------
1128+
*ExecEvalCase
1129+
*
1130+
*Evaluate a CASE clause. Will have boolean expressions
1131+
*inside the WHEN clauses, and will have constants
1132+
*for results.
1133+
*- thomas 1998-11-09
1134+
* ----------------------------------------------------------------
1135+
*/
1136+
staticDatum
1137+
ExecEvalCase(CaseExpr*caseExpr,ExprContext*econtext,bool*isNull)
1138+
{
1139+
List*clauses;
1140+
List*clause;
1141+
CaseWhen*wclause;
1142+
Datumconst_value=0;
1143+
boolisDone;
1144+
1145+
clauses=caseExpr->args;
1146+
1147+
/******************
1148+
*we evaluate each of the WHEN clauses in turn,
1149+
*as soon as one is true we return the corresponding
1150+
*result.If none are true then we return the value
1151+
*of the default clause, or NULL.
1152+
******************
1153+
*/
1154+
foreach(clause,clauses)
1155+
{
1156+
1157+
/******************
1158+
*We don't iterate over sets in the quals, so pass in an isDone
1159+
*flag, but ignore it.
1160+
******************
1161+
*/
1162+
1163+
wclause=lfirst(clause);
1164+
const_value=ExecEvalExpr((Node*)wclause->expr,
1165+
econtext,
1166+
isNull,
1167+
&isDone);
1168+
1169+
/******************
1170+
* if we have a true test, then we return the result,
1171+
* since the case statement is satisfied.
1172+
******************
1173+
*/
1174+
if (DatumGetInt32(const_value)!=0)
1175+
{
1176+
const_value=ExecEvalExpr((Node*)wclause->result,
1177+
econtext,
1178+
isNull,
1179+
&isDone);
1180+
return (Datum)const_value;
1181+
1182+
}
1183+
}
1184+
1185+
if (caseExpr->defresult)
1186+
{
1187+
const_value=ExecEvalExpr((Node*)caseExpr->defresult,
1188+
econtext,
1189+
isNull,
1190+
&isDone);
1191+
}
1192+
else
1193+
{
1194+
*isNull= true;
1195+
}
1196+
1197+
returnconst_value;
1198+
}
1199+
11271200
/* ----------------------------------------------------------------
11281201
*ExecEvalExpr
11291202
*
@@ -1236,13 +1309,18 @@ ExecEvalExpr(Node *expression,
12361309
}
12371310
break;
12381311
}
1312+
caseT_CaseExpr:
1313+
retDatum= (Datum)ExecEvalCase((CaseExpr*)expression,econtext,isNull);
1314+
break;
1315+
12391316
default:
12401317
elog(ERROR,"ExecEvalExpr: unknown expression type %d",nodeTag(expression));
12411318
break;
12421319
}
12431320

12441321
returnretDatum;
1245-
}
1322+
}/* ExecEvalExpr() */
1323+
12461324

12471325
/* ----------------------------------------------------------------
12481326
* ExecQual / ExecTargetList
@@ -1642,3 +1720,4 @@ ExecProject(ProjectionInfo *projInfo, bool *isDone)
16421720
InvalidBuffer,/* tuple has no buffer */
16431721
true);
16441722
}
1723+

‎src/backend/nodes/copyfuncs.c‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.50 1998/11/22 10:48:38 vadim Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.51 1998/12/04 15:33:33 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -943,6 +943,47 @@ _copySubLink(SubLink *from)
943943
returnnewnode;
944944
}
945945

946+
/* ----------------
947+
*_copyCaseExpr
948+
* ----------------
949+
*/
950+
staticCaseExpr*
951+
_copyCaseExpr(CaseExpr*from)
952+
{
953+
CaseExpr*newnode=makeNode(CaseExpr);
954+
955+
/* ----------------
956+
*copy remainder of node
957+
* ----------------
958+
*/
959+
newnode->casetype=from->casetype;
960+
961+
Node_Copy(from,newnode,arg);
962+
Node_Copy(from,newnode,args);
963+
Node_Copy(from,newnode,defresult);
964+
965+
returnnewnode;
966+
}
967+
968+
/* ----------------
969+
*_copyCaseWhen
970+
* ----------------
971+
*/
972+
staticCaseWhen*
973+
_copyCaseWhen(CaseWhen*from)
974+
{
975+
CaseWhen*newnode=makeNode(CaseWhen);
976+
977+
/* ----------------
978+
*copy remainder of node
979+
* ----------------
980+
*/
981+
Node_Copy(from,newnode,expr);
982+
Node_Copy(from,newnode,result);
983+
984+
returnnewnode;
985+
}
986+
946987
staticArray*
947988
_copyArray(Array*from)
948989
{
@@ -1734,6 +1775,12 @@ copyObject(void *from)
17341775
caseT_SubLink:
17351776
retval=_copySubLink(from);
17361777
break;
1778+
caseT_CaseExpr:
1779+
retval=_copyCaseExpr(from);
1780+
break;
1781+
caseT_CaseWhen:
1782+
retval=_copyCaseWhen(from);
1783+
break;
17371784

17381785
/*
17391786
* RELATION NODES

‎src/backend/nodes/outfuncs.c‎

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.48 1998/11/22 10:48:39 vadim Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.49 1998/12/04 15:33:33 thomas Exp $
1111
*
1212
* NOTES
1313
* Every (plan) node in POSTGRES has an associated "out" routine which
@@ -1635,6 +1635,82 @@ _outAConst(StringInfo str, A_Const *node)
16351635
return;
16361636
}
16371637

1638+
staticvoid
1639+
_outConstraint(StringInfostr,Constraint*node)
1640+
{
1641+
charbuf[500];
1642+
1643+
sprintf(buf," %s :type",
1644+
((node->name!=NULL)?node->name:"<>"));
1645+
appendStringInfo(str,buf);
1646+
1647+
switch (node->contype)
1648+
{
1649+
caseCONSTR_PRIMARY:
1650+
sprintf(buf," PRIMARY KEY ");
1651+
appendStringInfo(str,buf);
1652+
_outNode(str,node->keys);
1653+
break;
1654+
1655+
caseCONSTR_CHECK:
1656+
sprintf(buf," CHECK ");
1657+
appendStringInfo(str,buf);
1658+
appendStringInfo(str,node->def);
1659+
break;
1660+
1661+
caseCONSTR_DEFAULT:
1662+
sprintf(buf," DEFAULT ");
1663+
appendStringInfo(str,buf);
1664+
appendStringInfo(str,node->def);
1665+
break;
1666+
1667+
caseCONSTR_NOTNULL:
1668+
sprintf(buf," NOT NULL ");
1669+
appendStringInfo(str,buf);
1670+
break;
1671+
1672+
caseCONSTR_UNIQUE:
1673+
sprintf(buf," UNIQUE ");
1674+
appendStringInfo(str,buf);
1675+
_outNode(str,node->keys);
1676+
break;
1677+
1678+
default:
1679+
sprintf(buf,"<unrecognized constraint>");
1680+
appendStringInfo(str,buf);
1681+
break;
1682+
}
1683+
return;
1684+
}
1685+
1686+
staticvoid
1687+
_outCaseExpr(StringInfostr,CaseExpr*node)
1688+
{
1689+
charbuf[500];
1690+
1691+
sprintf(buf,"CASE ");
1692+
appendStringInfo(str,buf);
1693+
_outNode(str,node->args);
1694+
sprintf(buf," :default ");
1695+
appendStringInfo(str,buf);
1696+
_outNode(str,node->defresult);
1697+
return;
1698+
}
1699+
1700+
staticvoid
1701+
_outCaseWhen(StringInfostr,CaseWhen*node)
1702+
{
1703+
charbuf[500];
1704+
1705+
sprintf(buf," :when ");
1706+
appendStringInfo(str,buf);
1707+
_outNode(str,node->expr);
1708+
sprintf(buf," :then ");
1709+
appendStringInfo(str,buf);
1710+
_outNode(str,node->result);
1711+
return;
1712+
}
1713+
16381714
/*
16391715
* _outNode -
16401716
* converts a Node into ascii string and append it to 'str'
@@ -1861,6 +1937,15 @@ _outNode(StringInfo str, void *obj)
18611937
caseT_A_Const:
18621938
_outAConst(str,obj);
18631939
break;
1940+
caseT_Constraint:
1941+
_outConstraint(str,obj);
1942+
break;
1943+
caseT_CaseExpr:
1944+
_outCaseExpr(str,obj);
1945+
break;
1946+
caseT_CaseWhen:
1947+
_outCaseWhen(str,obj);
1948+
break;
18641949
default:
18651950
elog(NOTICE,"_outNode: don't know how to print type %d ",
18661951
nodeTag(obj));

‎src/backend/optimizer/plan/createplan.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.33 1998/11/22 10:48:43 vadim Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.34 1998/12/04 15:34:05 thomas Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -437,7 +437,7 @@ create_nestloop_node(JoinPath *best_path,
437437
* used in the inner scan path, so we need only consider the first
438438
* set of qualifications in indxqual.
439439
*
440-
* But there may be more than oneclauses in this "first set" in the
440+
* But there may be more than oneclause in this "first set" in the
441441
* case of multi-column indices. - vadim 03/18/97
442442
*/
443443

@@ -735,6 +735,11 @@ fix_indxqual_references(Node *clause, Path *index_path)
735735
else
736736
returnclause;
737737
}
738+
elseif (IsA(clause,CaseExpr))
739+
{
740+
elog(NOTICE,"optimizer: fix_indxqual_references sees CaseExpr");
741+
returnclause;
742+
}
738743
else
739744
{
740745
List*oldclauses= (List*)clause;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp