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

Commitdcc55dd

Browse files
committed
Rename ResolveNew() to ReplaceVarsFromTargetList(), and tweak its API.
This function currently lacks the option to throw error if the providedtargetlist doesn't have any matching entry for a Var to be replaced.Two of the four existing call sites would be better off with an error,as would the usage in the pending auto-updatable-views patch, so it seemspast time to extend the API to support that. To do so, replace the "event"parameter (historically of type CmdType, though it was declared plain int)with a special-purpose enum type.It's unclear whether this function might be called by third-party code.Since many C compilers wouldn't warn about a call site continuing to usethe old calling convention, rename the function to forcibly break anysuch code that hasn't been updated. The old name was none too well chosenanyhow.
1 parent75af5ae commitdcc55dd

File tree

4 files changed

+106
-76
lines changed

4 files changed

+106
-76
lines changed

‎src/backend/optimizer/path/allpaths.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,10 +1835,10 @@ subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
18351835
* This step also ensures that when we are pushing into a setop tree,
18361836
* each component query gets its own copy of the qual.
18371837
*/
1838-
qual=ResolveNew(qual,rti,0,rte,
1839-
subquery->targetList,
1840-
CMD_SELECT,0,
1841-
&subquery->hasSubLinks);
1838+
qual=ReplaceVarsFromTargetList(qual,rti,0,rte,
1839+
subquery->targetList,
1840+
REPLACEVARS_REPORT_ERROR,0,
1841+
&subquery->hasSubLinks);
18421842

18431843
/*
18441844
* Now attach the qual to the proper place: normally WHERE, but if the

‎src/backend/rewrite/rewriteHandler.c

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -502,25 +502,27 @@ rewriteRuleAction(Query *parsetree,
502502
AddQual(sub_action,parsetree->jointree->quals);
503503

504504
/*
505-
* Rewrite new.attributew/ right hand side of target-list entry for
505+
* Rewrite new.attributewith right hand side of target-list entry for
506506
* appropriate field name in insert/update.
507507
*
508-
* KLUGE ALERT: sinceResolveNew returns a mutated copy, we can't just
509-
* apply it to sub_action; we have to remember to update the sublink
510-
* inside rule_action, too.
508+
* KLUGE ALERT: sinceReplaceVarsFromTargetList returns a mutated copy, we
509+
*can't justapply it to sub_action; we have to remember to update the
510+
*sublinkinside rule_action, too.
511511
*/
512512
if ((event==CMD_INSERT||event==CMD_UPDATE)&&
513513
sub_action->commandType!=CMD_UTILITY)
514514
{
515-
sub_action= (Query*)ResolveNew((Node*)sub_action,
516-
new_varno,
517-
0,
518-
rt_fetch(new_varno,
519-
sub_action->rtable),
520-
parsetree->targetList,
521-
event,
522-
current_varno,
523-
NULL);
515+
sub_action= (Query*)
516+
ReplaceVarsFromTargetList((Node*)sub_action,
517+
new_varno,
518+
0,
519+
rt_fetch(new_varno,sub_action->rtable),
520+
parsetree->targetList,
521+
(event==CMD_UPDATE) ?
522+
REPLACEVARS_CHANGE_VARNO :
523+
REPLACEVARS_SUBSTITUTE_NULL,
524+
current_varno,
525+
NULL);
524526
if (sub_action_ptr)
525527
*sub_action_ptr=sub_action;
526528
else
@@ -543,15 +545,15 @@ rewriteRuleAction(Query *parsetree,
543545
errmsg("cannot have RETURNING lists in multiple rules")));
544546
*returning_flag= true;
545547
rule_action->returningList= (List*)
546-
ResolveNew((Node*)parsetree->returningList,
547-
parsetree->resultRelation,
548-
0,
549-
rt_fetch(parsetree->resultRelation,
550-
parsetree->rtable),
551-
rule_action->returningList,
552-
CMD_SELECT,
553-
0,
554-
&rule_action->hasSubLinks);
548+
ReplaceVarsFromTargetList((Node*)parsetree->returningList,
549+
parsetree->resultRelation,
550+
0,
551+
rt_fetch(parsetree->resultRelation,
552+
parsetree->rtable),
553+
rule_action->returningList,
554+
REPLACEVARS_REPORT_ERROR,
555+
0,
556+
&rule_action->hasSubLinks);
555557

556558
/*
557559
* There could have been some SubLinks in parsetree's returningList,
@@ -1703,14 +1705,17 @@ CopyAndAddInvertedQual(Query *parsetree,
17031705
ChangeVarNodes(new_qual,PRS2_OLD_VARNO,rt_index,0);
17041706
/* Fix references to NEW */
17051707
if (event==CMD_INSERT||event==CMD_UPDATE)
1706-
new_qual=ResolveNew(new_qual,
1707-
PRS2_NEW_VARNO,
1708-
0,
1709-
rt_fetch(rt_index,parsetree->rtable),
1710-
parsetree->targetList,
1711-
event,
1712-
rt_index,
1713-
&parsetree->hasSubLinks);
1708+
new_qual=ReplaceVarsFromTargetList(new_qual,
1709+
PRS2_NEW_VARNO,
1710+
0,
1711+
rt_fetch(rt_index,
1712+
parsetree->rtable),
1713+
parsetree->targetList,
1714+
(event==CMD_UPDATE) ?
1715+
REPLACEVARS_CHANGE_VARNO :
1716+
REPLACEVARS_SUBSTITUTE_NULL,
1717+
rt_index,
1718+
&parsetree->hasSubLinks);
17141719
/* And attach the fixed qual */
17151720
AddInvertedQual(parsetree,new_qual);
17161721

‎src/backend/rewrite/rewriteManip.c

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,12 +1323,16 @@ map_variable_attnos(Node *node,
13231323

13241324

13251325
/*
1326-
*ResolveNew - replace Vars with corresponding items from a targetlist
1326+
*ReplaceVarsFromTargetList - replace Vars with items from a targetlist
13271327
*
13281328
* Vars matching target_varno and sublevels_up are replaced by the
13291329
* entry with matching resno from targetlist, if there is one.
1330-
* If not, we either change the unmatched Var's varno to update_varno
1331-
* (when event == CMD_UPDATE) or replace it with a constant NULL.
1330+
*
1331+
* If there is no matching resno for such a Var, the action depends on the
1332+
* nomatch_option:
1333+
*REPLACEVARS_REPORT_ERROR: throw an error
1334+
*REPLACEVARS_CHANGE_VARNO: change Var's varno to nomatch_varno
1335+
*REPLACEVARS_SUBSTITUTE_NULL: replace Var with a NULL Const of same type
13321336
*
13331337
* The caller must also provide target_rte, the RTE describing the target
13341338
* relation. This is needed to handle whole-row Vars referencing the target.
@@ -1341,15 +1345,15 @@ typedef struct
13411345
{
13421346
RangeTblEntry*target_rte;
13431347
List*targetlist;
1344-
intevent;
1345-
intupdate_varno;
1346-
}ResolveNew_context;
1348+
ReplaceVarsNoMatchOptionnomatch_option;
1349+
intnomatch_varno;
1350+
}ReplaceVarsFromTargetList_context;
13471351

13481352
staticNode*
1349-
ResolveNew_callback(Var*var,
1350-
replace_rte_variables_context*context)
1353+
ReplaceVarsFromTargetList_callback(Var*var,
1354+
replace_rte_variables_context*context)
13511355
{
1352-
ResolveNew_context*rcon= (ResolveNew_context*)context->callback_arg;
1356+
ReplaceVarsFromTargetList_context*rcon= (ReplaceVarsFromTargetList_context*)context->callback_arg;
13531357
TargetEntry*tle;
13541358

13551359
if (var->varattno==InvalidAttrNumber)
@@ -1388,29 +1392,37 @@ ResolveNew_callback(Var *var,
13881392

13891393
if (tle==NULL||tle->resjunk)
13901394
{
1391-
/* Failed to find column in insert/update tlist */
1392-
if (rcon->event==CMD_UPDATE)
1393-
{
1394-
/* For update, just change unmatched var's varno */
1395-
var= (Var*)copyObject(var);
1396-
var->varno=rcon->update_varno;
1397-
var->varnoold=rcon->update_varno;
1398-
return (Node*)var;
1399-
}
1400-
else
1395+
/* Failed to find column in targetlist */
1396+
switch (rcon->nomatch_option)
14011397
{
1402-
/* Otherwise replace unmatched var with a null */
1403-
/* need coerce_to_domain in case of NOT NULL domain constraint */
1404-
returncoerce_to_domain((Node*)makeNullConst(var->vartype,
1405-
var->vartypmod,
1406-
var->varcollid),
1407-
InvalidOid,-1,
1408-
var->vartype,
1409-
COERCE_IMPLICIT_CAST,
1410-
-1,
1411-
false,
1412-
false);
1398+
caseREPLACEVARS_REPORT_ERROR:
1399+
/* fall through, throw error below */
1400+
break;
1401+
1402+
caseREPLACEVARS_CHANGE_VARNO:
1403+
var= (Var*)copyObject(var);
1404+
var->varno=rcon->nomatch_varno;
1405+
var->varnoold=rcon->nomatch_varno;
1406+
return (Node*)var;
1407+
1408+
caseREPLACEVARS_SUBSTITUTE_NULL:
1409+
/*
1410+
* If Var is of domain type, we should add a CoerceToDomain
1411+
* node, in case there is a NOT NULL domain constraint.
1412+
*/
1413+
returncoerce_to_domain((Node*)makeNullConst(var->vartype,
1414+
var->vartypmod,
1415+
var->varcollid),
1416+
InvalidOid,-1,
1417+
var->vartype,
1418+
COERCE_IMPLICIT_CAST,
1419+
-1,
1420+
false,
1421+
false);
14131422
}
1423+
elog(ERROR,"could not find replacement targetlist entry for attno %d",
1424+
var->varattno);
1425+
returnNULL;/* keep compiler quiet */
14141426
}
14151427
else
14161428
{
@@ -1426,20 +1438,23 @@ ResolveNew_callback(Var *var,
14261438
}
14271439

14281440
Node*
1429-
ResolveNew(Node*node,inttarget_varno,intsublevels_up,
1430-
RangeTblEntry*target_rte,
1431-
List*targetlist,intevent,intupdate_varno,
1432-
bool*outer_hasSubLinks)
1441+
ReplaceVarsFromTargetList(Node*node,
1442+
inttarget_varno,intsublevels_up,
1443+
RangeTblEntry*target_rte,
1444+
List*targetlist,
1445+
ReplaceVarsNoMatchOptionnomatch_option,
1446+
intnomatch_varno,
1447+
bool*outer_hasSubLinks)
14331448
{
1434-
ResolveNew_contextcontext;
1449+
ReplaceVarsFromTargetList_contextcontext;
14351450

14361451
context.target_rte=target_rte;
14371452
context.targetlist=targetlist;
1438-
context.event=event;
1439-
context.update_varno=update_varno;
1453+
context.nomatch_option=nomatch_option;
1454+
context.nomatch_varno=nomatch_varno;
14401455

14411456
returnreplace_rte_variables(node,target_varno,sublevels_up,
1442-
ResolveNew_callback,
1457+
ReplaceVarsFromTargetList_callback,
14431458
(void*)&context,
14441459
outer_hasSubLinks);
14451460
}

‎src/include/rewrite/rewriteManip.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ struct replace_rte_variables_context
3131
boolinserted_sublink;/* have we inserted a SubLink? */
3232
};
3333

34+
typedefenumReplaceVarsNoMatchOption
35+
{
36+
REPLACEVARS_REPORT_ERROR,/* throw error if no match */
37+
REPLACEVARS_CHANGE_VARNO,/* change the Var's varno, nothing else */
38+
REPLACEVARS_SUBSTITUTE_NULL/* replace with a NULL Const */
39+
}ReplaceVarsNoMatchOption;
40+
3441

3542
externvoidOffsetVarNodes(Node*node,intoffset,intsublevels_up);
3643
externvoidChangeVarNodes(Node*node,intold_varno,intnew_varno,
@@ -69,9 +76,12 @@ extern Node *map_variable_attnos(Node *node,
6976
constAttrNumber*attno_map,intmap_length,
7077
bool*found_whole_row);
7178

72-
externNode*ResolveNew(Node*node,inttarget_varno,intsublevels_up,
73-
RangeTblEntry*target_rte,
74-
List*targetlist,intevent,intupdate_varno,
75-
bool*outer_hasSubLinks);
79+
externNode*ReplaceVarsFromTargetList(Node*node,
80+
inttarget_varno,intsublevels_up,
81+
RangeTblEntry*target_rte,
82+
List*targetlist,
83+
ReplaceVarsNoMatchOptionnomatch_option,
84+
intnomatch_varno,
85+
bool*outer_hasSubLinks);
7686

7787
#endif/* REWRITEMANIP_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp