PostgreSQL Source Code git master
Loading...
Searching...
No Matches
var.c File Reference
#include "postgres.h"
#include "access/sysattr.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/optimizer.h"
#include "optimizer/placeholder.h"
#include "optimizer/prep.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteManip.h"
Include dependency graph for var.c:

Go to the source code of this file.

Data Structures

struct  pull_varnos_context
 
struct  pull_varattnos_context
 
struct  pull_vars_context
 
struct  locate_var_of_level_context
 
struct  pull_var_clause_context
 
struct  flatten_join_alias_vars_context
 

Functions

static bool pull_varnos_walker (Node *node, pull_varnos_context *context)
 
static bool pull_varattnos_walker (Node *node, pull_varattnos_context *context)
 
static bool pull_vars_walker (Node *node, pull_vars_context *context)
 
static bool contain_var_clause_walker (Node *node, void *context)
 
static bool contain_vars_of_level_walker (Node *node, int *sublevels_up)
 
static bool contain_vars_returning_old_or_new_walker (Node *node, void *context)
 
static bool locate_var_of_level_walker (Node *node, locate_var_of_level_context *context)
 
static bool pull_var_clause_walker (Node *node, pull_var_clause_context *context)
 
static Nodeflatten_join_alias_vars_mutator (Node *node, flatten_join_alias_vars_context *context)
 
static Nodeflatten_group_exprs_mutator (Node *node, flatten_join_alias_vars_context *context)
 
static Nodemark_nullable_by_grouping (PlannerInfo *root, Node *newnode, Var *oldvar)
 
static Nodeadd_nullingrels_if_needed (PlannerInfo *root, Node *newnode, Var *oldvar)
 
static bool is_standard_join_alias_expression (Node *newnode, Var *oldvar)
 
static void adjust_standard_join_alias_expression (Node *newnode, Var *oldvar)
 
static Relids alias_relid_set (Query *query, Relids relids)
 
Relids pull_varnos (PlannerInfo *root, Node *node)
 
Relids pull_varnos_of_level (PlannerInfo *root, Node *node, int levelsup)
 
void pull_varattnos (Node *node, Index varno, Bitmapset **varattnos)
 
Listpull_vars_of_level (Node *node, int levelsup)
 
bool contain_var_clause (Node *node)
 
bool contain_vars_of_level (Node *node, int levelsup)
 
bool contain_vars_returning_old_or_new (Node *node)
 
int locate_var_of_level (Node *node, int levelsup)
 
Listpull_var_clause (Node *node, int flags)
 
Nodeflatten_join_alias_vars (PlannerInfo *root, Query *query, Node *node)
 
Nodeflatten_join_alias_for_parser (Query *query, Node *node, int sublevels_up)
 
Nodeflatten_group_exprs (PlannerInfo *root, Query *query, Node *node)
 

Function Documentation

◆ add_nullingrels_if_needed()

static Node * add_nullingrels_if_needed ( PlannerInfo root,
Node newnode,
Var oldvar 
)
static

Definition at line 1207 of file var.c.

1208{
1209 if (oldvar->varnullingrels == NULL)
1210 return newnode; /* nothing to do */
1211 /* If possible, do it by adding to existing nullingrel fields */
1214 else if (root)
1215 {
1216 /*
1217 * We can insert a PlaceHolderVar to carry the nullingrels. However,
1218 * deciding where to evaluate the PHV is slightly tricky. We first
1219 * try to evaluate it at the natural semantic level of the new
1220 * expression; but if that expression is variable-free, fall back to
1221 * evaluating it at the join that the oldvar is an alias Var for.
1222 */
1224 Index levelsup = oldvar->varlevelsup;
1226
1227 if (bms_is_empty(phrels)) /* variable-free? */
1228 {
1229 if (levelsup != 0) /* this won't work otherwise */
1230 elog(ERROR, "unsupported join alias expression");
1231 phrels = get_relids_for_join(root->parse, oldvar->varno);
1232 /* If it's an outer join, eval below not above the join */
1235 }
1237 /* newphv has zero phlevelsup and NULL phnullingrels; fix it */
1238 newphv->phlevelsup = levelsup;
1239 newphv->phnullingrels = bms_copy(oldvar->varnullingrels);
1240 newnode = (Node *) newphv;
1241 }
1242 else
1243 {
1244 /* ooops, we're missing support for something the parser can make */
1245 elog(ERROR, "unsupported join alias expression");
1246 }
1247 return newnode;
1248}
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition bitmapset.c:852
Bitmapset * bms_copy(const Bitmapset *a)
Definition bitmapset.c:122
#define bms_is_empty(a)
Definition bitmapset.h:118
#define Assert(condition)
Definition c.h:906
unsigned int Index
Definition c.h:661
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
PlaceHolderVar * make_placeholder_expr(PlannerInfo *root, Expr *expr, Relids phrels)
Definition placeholder.c:54
Relids get_relids_for_join(Query *query, int joinrelid)
static int fb(int x)
tree ctl root
Definition radixtree.h:1857
Definition nodes.h:135
Relids pull_varnos_of_level(PlannerInfo *root, Node *node, int levelsup)
Definition var.c:140
static bool is_standard_join_alias_expression(Node *newnode, Var *oldvar)
Definition var.c:1259
static void adjust_standard_join_alias_expression(Node *newnode, Var *oldvar)
Definition var.c:1333

References adjust_standard_join_alias_expression(), Assert, bms_copy(), bms_del_member(), bms_is_empty, elog, ERROR, fb(), get_relids_for_join(), is_standard_join_alias_expression(), make_placeholder_expr(), pull_varnos_of_level(), and root.

Referenced by flatten_join_alias_vars_mutator().

◆ adjust_standard_join_alias_expression()

static void adjust_standard_join_alias_expression ( Node newnode,
Var oldvar 
)
static

Definition at line 1333 of file var.c.

1334{
1335 if (IsA(newnode, Var) &&
1336 ((Var *) newnode)->varlevelsup == oldvar->varlevelsup)
1337 {
1338 Var *newvar = (Var *) newnode;
1339
1340 newvar->varnullingrels = bms_add_members(newvar->varnullingrels,
1341 oldvar->varnullingrels);
1342 }
1343 else if (IsA(newnode, PlaceHolderVar) &&
1344 ((PlaceHolderVar *) newnode)->phlevelsup == oldvar->varlevelsup)
1345 {
1347
1348 newphv->phnullingrels = bms_add_members(newphv->phnullingrels,
1349 oldvar->varnullingrels);
1350 }
1351 else if (IsA(newnode, FuncExpr))
1352 {
1354
1356 }
1357 else if (IsA(newnode, RelabelType))
1358 {
1360
1362 }
1363 else if (IsA(newnode, CoerceViaIO))
1364 {
1365 CoerceViaIO *iocoerce = (CoerceViaIO *) newnode;
1366
1368 }
1369 else if (IsA(newnode, ArrayCoerceExpr))
1370 {
1372
1374 }
1375 else if (IsA(newnode, CoalesceExpr))
1376 {
1377 CoalesceExpr *cexpr = (CoalesceExpr *) newnode;
1378 ListCell *lc;
1379
1380 Assert(cexpr->args != NIL);
1381 foreach(lc, cexpr->args)
1382 {
1384 }
1385 }
1386 else
1387 Assert(false);
1388}
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:901
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define lfirst(lc)
Definition pg_list.h:172
#define NIL
Definition pg_list.h:68
#define linitial(l)
Definition pg_list.h:178
Relids phnullingrels
Definition pathnodes.h:3113

References adjust_standard_join_alias_expression(), CoerceViaIO::arg, CoalesceExpr::args, Assert, bms_add_members(), fb(), IsA, lfirst, linitial, NIL, and PlaceHolderVar::phnullingrels.

Referenced by add_nullingrels_if_needed(), and adjust_standard_join_alias_expression().

◆ alias_relid_set()

static Relids alias_relid_set ( Query query,
Relids  relids 
)
static

Definition at line 1395 of file var.c.

1396{
1397 Relids result = NULL;
1398 int rtindex;
1399
1400 rtindex = -1;
1401 while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
1402 {
1403 RangeTblEntry *rte = rt_fetch(rtindex, query->rtable);
1404
1405 if (rte->rtekind == RTE_JOIN)
1406 result = bms_join(result, get_relids_for_join(query, rtindex));
1407 else
1408 result = bms_add_member(result, rtindex);
1409 }
1410 return result;
1411}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition bitmapset.c:1214
@ RTE_JOIN
#define rt_fetch(rangetable_index, rangetable)
Definition parsetree.h:31
List * rtable
Definition parsenodes.h:175

References bms_add_member(), bms_join(), bms_next_member(), fb(), get_relids_for_join(), rt_fetch, Query::rtable, and RTE_JOIN.

Referenced by flatten_join_alias_vars_mutator().

◆ contain_var_clause()

◆ contain_var_clause_walker()

static bool contain_var_clause_walker ( Node node,
void context 
)
static

Definition at line 412 of file var.c.

413{
414 if (node == NULL)
415 return false;
416 if (IsA(node, Var))
417 {
418 if (((Var *) node)->varlevelsup == 0)
419 return true; /* abort the tree traversal and return true */
420 return false;
421 }
422 if (IsA(node, CurrentOfExpr))
423 return true;
424 if (IsA(node, PlaceHolderVar))
425 {
426 if (((PlaceHolderVar *) node)->phlevelsup == 0)
427 return true; /* abort the tree traversal and return true */
428 /* else fall through to check the contained expr */
429 }
431}
#define expression_tree_walker(n, w, c)
Definition nodeFuncs.h:153

References contain_var_clause_walker(), expression_tree_walker, fb(), and IsA.

Referenced by contain_var_clause(), and contain_var_clause_walker().

◆ contain_vars_of_level()

bool contain_vars_of_level ( Node node,
int  levelsup 
)

◆ contain_vars_of_level_walker()

static bool contain_vars_of_level_walker ( Node node,
int sublevels_up 
)
static

Definition at line 455 of file var.c.

456{
457 if (node == NULL)
458 return false;
459 if (IsA(node, Var))
460 {
461 if (((Var *) node)->varlevelsup == *sublevels_up)
462 return true; /* abort tree traversal and return true */
463 return false;
464 }
465 if (IsA(node, CurrentOfExpr))
466 {
467 if (*sublevels_up == 0)
468 return true;
469 return false;
470 }
471 if (IsA(node, PlaceHolderVar))
472 {
473 if (((PlaceHolderVar *) node)->phlevelsup == *sublevels_up)
474 return true; /* abort the tree traversal and return true */
475 /* else fall through to check the contained expr */
476 }
477 if (IsA(node, Query))
478 {
479 /* Recurse into subselects */
480 bool result;
481
482 (*sublevels_up)++;
483 result = query_tree_walker((Query *) node,
485 sublevels_up,
486 0);
487 (*sublevels_up)--;
488 return result;
489 }
490 return expression_tree_walker(node,
492 sublevels_up);
493}
#define query_tree_walker(q, w, c, f)
Definition nodeFuncs.h:158

References contain_vars_of_level_walker(), expression_tree_walker, fb(), IsA, and query_tree_walker.

Referenced by contain_vars_of_level(), and contain_vars_of_level_walker().

◆ contain_vars_returning_old_or_new()

bool contain_vars_returning_old_or_new ( Node node)

Definition at line 511 of file var.c.

512{
514}
static bool contain_vars_returning_old_or_new_walker(Node *node, void *context)
Definition var.c:517

References contain_vars_returning_old_or_new_walker(), and fb().

Referenced by make_modifytable().

◆ contain_vars_returning_old_or_new_walker()

static bool contain_vars_returning_old_or_new_walker ( Node node,
void context 
)
static

Definition at line 517 of file var.c.

518{
519 if (node == NULL)
520 return false;
521 if (IsA(node, Var))
522 {
523 if (((Var *) node)->varlevelsup == 0 &&
524 ((Var *) node)->varreturningtype != VAR_RETURNING_DEFAULT)
525 return true; /* abort the tree traversal and return true */
526 return false;
527 }
528 if (IsA(node, ReturningExpr))
529 {
530 if (((ReturningExpr *) node)->retlevelsup == 0)
531 return true; /* abort the tree traversal and return true */
532 return false;
533 }
535 context);
536}
@ VAR_RETURNING_DEFAULT
Definition primnodes.h:257

References contain_vars_returning_old_or_new_walker(), expression_tree_walker, fb(), IsA, and VAR_RETURNING_DEFAULT.

Referenced by contain_vars_returning_old_or_new(), and contain_vars_returning_old_or_new_walker().

◆ flatten_group_exprs()

Node * flatten_group_exprs ( PlannerInfo root,
Query query,
Node node 
)

Definition at line 1002 of file var.c.

1003{
1005
1006 /*
1007 * We do not expect this to be applied to the whole Query, only to
1008 * expressions or LATERAL subqueries. Hence, if the top node is a Query,
1009 * it's okay to immediately increment sublevels_up.
1010 */
1011 Assert(node != (Node *) query);
1012
1013 context.root = root;
1014 context.query = query;
1015 context.sublevels_up = 0;
1016 /* flag whether grouping expressions could possibly contain SubLinks */
1017 context.possible_sublink = query->hasSubLinks;
1018 /* if hasSubLinks is already true, no need to work hard */
1019 context.inserted_sublink = query->hasSubLinks;
1020
1021 return flatten_group_exprs_mutator(node, &context);
1022}
static Node * flatten_group_exprs_mutator(Node *node, flatten_join_alias_vars_context *context)
Definition var.c:1025

References Assert, flatten_group_exprs_mutator(), flatten_join_alias_vars_context::inserted_sublink, flatten_join_alias_vars_context::possible_sublink, flatten_join_alias_vars_context::query, flatten_join_alias_vars_context::root, root, and flatten_join_alias_vars_context::sublevels_up.

Referenced by check_output_expressions(), get_query_def(), and subquery_planner().

◆ flatten_group_exprs_mutator()

static Node * flatten_group_exprs_mutator ( Node node,
flatten_join_alias_vars_context context 
)
static

Definition at line 1025 of file var.c.

1027{
1028 if (node == NULL)
1029 return NULL;
1030 if (IsA(node, Var))
1031 {
1032 Var *var = (Var *) node;
1034 Node *newvar;
1035
1036 /* No change unless Var belongs to the GROUP of the target level */
1037 if (var->varlevelsup != context->sublevels_up)
1038 return node; /* no need to copy, really */
1039 rte = rt_fetch(var->varno, context->query->rtable);
1040 if (rte->rtekind != RTE_GROUP)
1041 return node;
1042
1043 /* Expand group exprs reference */
1044 Assert(var->varattno > 0);
1045 newvar = (Node *) list_nth(rte->groupexprs, var->varattno - 1);
1046 Assert(newvar != NULL);
1048
1049 /*
1050 * If we are expanding an expr carried down from an upper query, must
1051 * adjust its varlevelsup fields.
1052 */
1053 if (context->sublevels_up != 0)
1055
1056 /* Preserve original Var's location, if possible */
1057 if (IsA(newvar, Var))
1058 ((Var *) newvar)->location = var->location;
1059
1060 /* Detect if we are adding a sublink to query */
1061 if (context->possible_sublink && !context->inserted_sublink)
1063
1064 /* Lastly, add any varnullingrels to the replacement expression */
1065 return mark_nullable_by_grouping(context->root, newvar, var);
1066 }
1067
1068 if (IsA(node, Aggref))
1069 {
1070 Aggref *agg = (Aggref *) node;
1071
1072 if ((int) agg->agglevelsup == context->sublevels_up)
1073 {
1074 /*
1075 * If we find an aggregate call of the original level, do not
1076 * recurse into its normal arguments, ORDER BY arguments, or
1077 * filter; there are no grouped vars there. But we should check
1078 * direct arguments as though they weren't in an aggregate.
1079 */
1080 agg = copyObject(agg);
1081 agg->aggdirectargs = (List *)
1082 flatten_group_exprs_mutator((Node *) agg->aggdirectargs, context);
1083
1084 return (Node *) agg;
1085 }
1086
1087 /*
1088 * We can skip recursing into aggregates of higher levels altogether,
1089 * since they could not possibly contain Vars of concern to us (see
1090 * transformAggregateCall). We do need to look at aggregates of lower
1091 * levels, however.
1092 */
1093 if ((int) agg->agglevelsup > context->sublevels_up)
1094 return node;
1095 }
1096
1097 if (IsA(node, GroupingFunc))
1098 {
1099 GroupingFunc *grp = (GroupingFunc *) node;
1100
1101 /*
1102 * If we find a GroupingFunc node of the original or higher level, do
1103 * not recurse into its arguments; there are no grouped vars there.
1104 */
1105 if ((int) grp->agglevelsup >= context->sublevels_up)
1106 return node;
1107 }
1108
1109 if (IsA(node, Query))
1110 {
1111 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
1112 Query *newnode;
1114
1115 context->sublevels_up++;
1117 context->inserted_sublink = ((Query *) node)->hasSubLinks;
1118 newnode = query_tree_mutator((Query *) node,
1120 context,
1122 newnode->hasSubLinks |= context->inserted_sublink;
1124 context->sublevels_up--;
1125 return (Node *) newnode;
1126 }
1127
1129 context);
1130}
#define expression_tree_mutator(n, m, c)
Definition nodeFuncs.h:155
#define QTW_IGNORE_GROUPEXPRS
Definition nodeFuncs.h:32
#define query_tree_mutator(q, m, c, f)
Definition nodeFuncs.h:160
#define copyObject(obj)
Definition nodes.h:232
@ RTE_GROUP
static void * list_nth(const List *list, int n)
Definition pg_list.h:299
bool checkExprHasSubLink(Node *node)
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition pg_list.h:54
ParseLoc location
Definition primnodes.h:311
AttrNumber varattno
Definition primnodes.h:275
int varno
Definition primnodes.h:270
Index varlevelsup
Definition primnodes.h:295
static Node * mark_nullable_by_grouping(PlannerInfo *root, Node *newnode, Var *oldvar)
Definition var.c:1137

References Assert, checkExprHasSubLink(), copyObject, expression_tree_mutator, fb(), flatten_group_exprs_mutator(), IncrementVarSublevelsUp(), flatten_join_alias_vars_context::inserted_sublink, IsA, list_nth(), Var::location, mark_nullable_by_grouping(), flatten_join_alias_vars_context::possible_sublink, QTW_IGNORE_GROUPEXPRS, flatten_join_alias_vars_context::query, query_tree_mutator, flatten_join_alias_vars_context::root, rt_fetch, Query::rtable, RTE_GROUP, flatten_join_alias_vars_context::sublevels_up, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by flatten_group_exprs(), and flatten_group_exprs_mutator().

◆ flatten_join_alias_for_parser()

Node * flatten_join_alias_for_parser ( Query query,
Node node,
int  sublevels_up 
)

Definition at line 819 of file var.c.

820{
822
823 /*
824 * We do not expect this to be applied to the whole Query, only to
825 * expressions or LATERAL subqueries. Hence, if the top node is a Query,
826 * it's okay to immediately increment sublevels_up.
827 */
828 Assert(node != (Node *) query);
829
830 context.root = NULL;
831 context.query = query;
832 context.sublevels_up = sublevels_up;
833 /* flag whether join aliases could possibly contain SubLinks */
834 context.possible_sublink = query->hasSubLinks;
835 /* if hasSubLinks is already true, no need to work hard */
836 context.inserted_sublink = query->hasSubLinks;
837
838 return flatten_join_alias_vars_mutator(node, &context);
839}
static Node * flatten_join_alias_vars_mutator(Node *node, flatten_join_alias_vars_context *context)
Definition var.c:842

References Assert, fb(), flatten_join_alias_vars_mutator(), flatten_join_alias_vars_context::inserted_sublink, flatten_join_alias_vars_context::possible_sublink, flatten_join_alias_vars_context::query, flatten_join_alias_vars_context::root, and flatten_join_alias_vars_context::sublevels_up.

Referenced by finalize_grouping_exprs_walker(), and parseCheckAggregates().

◆ flatten_join_alias_vars()

Node * flatten_join_alias_vars ( PlannerInfo root,
Query query,
Node node 
)

Definition at line 781 of file var.c.

782{
784
785 /*
786 * We do not expect this to be applied to the whole Query, only to
787 * expressions or LATERAL subqueries. Hence, if the top node is a Query,
788 * it's okay to immediately increment sublevels_up.
789 */
790 Assert(node != (Node *) query);
791
792 context.root = root;
793 context.query = query;
794 context.sublevels_up = 0;
795 /* flag whether join aliases could possibly contain SubLinks */
796 context.possible_sublink = query->hasSubLinks;
797 /* if hasSubLinks is already true, no need to work hard */
798 context.inserted_sublink = query->hasSubLinks;
799
800 return flatten_join_alias_vars_mutator(node, &context);
801}

References Assert, flatten_join_alias_vars_mutator(), flatten_join_alias_vars_context::inserted_sublink, flatten_join_alias_vars_context::possible_sublink, flatten_join_alias_vars_context::query, flatten_join_alias_vars_context::root, root, and flatten_join_alias_vars_context::sublevels_up.

Referenced by preprocess_expression(), pull_up_simple_subquery(), and subquery_planner().

◆ flatten_join_alias_vars_mutator()

static Node * flatten_join_alias_vars_mutator ( Node node,
flatten_join_alias_vars_context context 
)
static

Definition at line 842 of file var.c.

844{
845 if (node == NULL)
846 return NULL;
847 if (IsA(node, Var))
848 {
849 Var *var = (Var *) node;
851 Node *newvar;
852
853 /* No change unless Var belongs to a JOIN of the target level */
854 if (var->varlevelsup != context->sublevels_up)
855 return node; /* no need to copy, really */
856 rte = rt_fetch(var->varno, context->query->rtable);
857 if (rte->rtekind != RTE_JOIN)
858 return node;
859 if (var->varattno == InvalidAttrNumber)
860 {
861 /* Must expand whole-row reference */
862 RowExpr *rowexpr;
863 List *fields = NIL;
864 List *colnames = NIL;
865 ListCell *lv;
866 ListCell *ln;
867
868 Assert(list_length(rte->joinaliasvars) == list_length(rte->eref->colnames));
869 forboth(lv, rte->joinaliasvars, ln, rte->eref->colnames)
870 {
871 newvar = (Node *) lfirst(lv);
872 /* Ignore dropped columns */
873 if (newvar == NULL)
874 continue;
876
877 /*
878 * If we are expanding an alias carried down from an upper
879 * query, must adjust its varlevelsup fields.
880 */
881 if (context->sublevels_up != 0)
883 /* Preserve original Var's location, if possible */
884 if (IsA(newvar, Var))
885 ((Var *) newvar)->location = var->location;
886 /* Recurse in case join input is itself a join */
887 /* (also takes care of setting inserted_sublink if needed) */
889 fields = lappend(fields, newvar);
890 /* We need the names of non-dropped columns, too */
891 colnames = lappend(colnames, copyObject((Node *) lfirst(ln)));
892 }
893 rowexpr = makeNode(RowExpr);
894 rowexpr->args = fields;
895 rowexpr->row_typeid = var->vartype;
896 rowexpr->row_format = COERCE_IMPLICIT_CAST;
897 /* vartype will always be RECORDOID, so we always need colnames */
898 rowexpr->colnames = colnames;
899 rowexpr->location = var->location;
900
901 /* Lastly, add any varnullingrels to the replacement expression */
902 return add_nullingrels_if_needed(context->root, (Node *) rowexpr,
903 var);
904 }
905
906 /* Expand join alias reference */
907 Assert(var->varattno > 0);
908 newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
909 Assert(newvar != NULL);
911
912 /*
913 * If we are expanding an alias carried down from an upper query, must
914 * adjust its varlevelsup fields.
915 */
916 if (context->sublevels_up != 0)
918
919 /* Preserve original Var's location, if possible */
920 if (IsA(newvar, Var))
921 ((Var *) newvar)->location = var->location;
922
923 /* Recurse in case join input is itself a join */
925
926 /* Detect if we are adding a sublink to query */
927 if (context->possible_sublink && !context->inserted_sublink)
929
930 /* Lastly, add any varnullingrels to the replacement expression */
931 return add_nullingrels_if_needed(context->root, newvar, var);
932 }
933 if (IsA(node, PlaceHolderVar))
934 {
935 /* Copy the PlaceHolderVar node with correct mutation of subnodes */
937
940 context);
941 /* now fix PlaceHolderVar's relid sets */
942 if (phv->phlevelsup == context->sublevels_up)
943 {
944 phv->phrels = alias_relid_set(context->query,
945 phv->phrels);
946 /* we *don't* change phnullingrels */
947 }
948 return (Node *) phv;
949 }
950
951 if (IsA(node, Query))
952 {
953 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
954 Query *newnode;
956
957 context->sublevels_up++;
959 context->inserted_sublink = ((Query *) node)->hasSubLinks;
962 context,
964 newnode->hasSubLinks |= context->inserted_sublink;
966 context->sublevels_up--;
967 return (Node *) newnode;
968 }
969 /* Already-planned tree not supported */
970 Assert(!IsA(node, SubPlan));
972 /* Shouldn't need to handle these planner auxiliary nodes here */
973 Assert(!IsA(node, SpecialJoinInfo));
974 Assert(!IsA(node, PlaceHolderInfo));
975 Assert(!IsA(node, MinMaxAggInfo));
976
978}
#define InvalidAttrNumber
Definition attnum.h:23
List * lappend(List *list, void *datum)
Definition list.c:339
#define QTW_IGNORE_JOINALIASES
Definition nodeFuncs.h:25
#define makeNode(_type_)
Definition nodes.h:161
static int list_length(const List *l)
Definition pg_list.h:152
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:518
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:769
List * args
Definition primnodes.h:1449
ParseLoc location
Definition primnodes.h:1473
static Node * add_nullingrels_if_needed(PlannerInfo *root, Node *newnode, Var *oldvar)
Definition var.c:1207
static Relids alias_relid_set(Query *query, Relids relids)
Definition var.c:1395

References add_nullingrels_if_needed(), alias_relid_set(), RowExpr::args, Assert, checkExprHasSubLink(), COERCE_IMPLICIT_CAST, copyObject, expression_tree_mutator, fb(), flatten_join_alias_vars_mutator(), forboth, IncrementVarSublevelsUp(), flatten_join_alias_vars_context::inserted_sublink, InvalidAttrNumber, IsA, lappend(), lfirst, list_length(), list_nth(), Var::location, RowExpr::location, makeNode, NIL, flatten_join_alias_vars_context::possible_sublink, QTW_IGNORE_JOINALIASES, flatten_join_alias_vars_context::query, query_tree_mutator, flatten_join_alias_vars_context::root, rt_fetch, Query::rtable, RTE_JOIN, flatten_join_alias_vars_context::sublevels_up, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by flatten_join_alias_for_parser(), flatten_join_alias_vars(), and flatten_join_alias_vars_mutator().

◆ is_standard_join_alias_expression()

static bool is_standard_join_alias_expression ( Node newnode,
Var oldvar 
)
static

Definition at line 1259 of file var.c.

1260{
1261 if (newnode == NULL)
1262 return false;
1263 if (IsA(newnode, Var) &&
1264 ((Var *) newnode)->varlevelsup == oldvar->varlevelsup)
1265 return true;
1266 else if (IsA(newnode, PlaceHolderVar) &&
1267 ((PlaceHolderVar *) newnode)->phlevelsup == oldvar->varlevelsup)
1268 return true;
1269 else if (IsA(newnode, FuncExpr))
1270 {
1272
1273 /*
1274 * We need to assume that the function wouldn't produce non-NULL from
1275 * NULL, which is reasonable for implicit coercions but otherwise not
1276 * so much. (Looking at its strictness is likely overkill, and anyway
1277 * it would cause us to fail if someone forgot to mark an implicit
1278 * coercion as strict.)
1279 */
1280 if (fexpr->funcformat != COERCE_IMPLICIT_CAST ||
1281 fexpr->args == NIL)
1282 return false;
1283
1284 /*
1285 * Examine only the first argument --- coercions might have additional
1286 * arguments that are constants.
1287 */
1289 }
1290 else if (IsA(newnode, RelabelType))
1291 {
1293
1294 /* This definitely won't produce non-NULL from NULL */
1296 }
1297 else if (IsA(newnode, CoerceViaIO))
1298 {
1299 CoerceViaIO *iocoerce = (CoerceViaIO *) newnode;
1300
1301 /* This definitely won't produce non-NULL from NULL */
1302 return is_standard_join_alias_expression((Node *) iocoerce->arg, oldvar);
1303 }
1304 else if (IsA(newnode, ArrayCoerceExpr))
1305 {
1307
1308 /* This definitely won't produce non-NULL from NULL (at array level) */
1310 }
1311 else if (IsA(newnode, CoalesceExpr))
1312 {
1313 CoalesceExpr *cexpr = (CoalesceExpr *) newnode;
1314 ListCell *lc;
1315
1316 Assert(cexpr->args != NIL);
1317 foreach(lc, cexpr->args)
1318 {
1320 return false;
1321 }
1322 return true;
1323 }
1324 else
1325 return false;
1326}

References CoerceViaIO::arg, CoalesceExpr::args, Assert, COERCE_IMPLICIT_CAST, fb(), is_standard_join_alias_expression(), IsA, lfirst, linitial, and NIL.

Referenced by add_nullingrels_if_needed(), and is_standard_join_alias_expression().

◆ locate_var_of_level()

int locate_var_of_level ( Node node,
int  levelsup 
)

Definition at line 555 of file var.c.

556{
558
559 context.var_location = -1; /* in case we find nothing */
560 context.sublevels_up = levelsup;
561
564 &context,
565 0);
566
567 return context.var_location;
568}
static bool locate_var_of_level_walker(Node *node, locate_var_of_level_context *context)
Definition var.c:571

References fb(), locate_var_of_level_walker(), query_or_expression_tree_walker, locate_var_of_level_context::sublevels_up, and locate_var_of_level_context::var_location.

Referenced by check_agg_arguments(), checkExprIsVarFree(), and transformSetOperationTree().

◆ locate_var_of_level_walker()

static bool locate_var_of_level_walker ( Node node,
locate_var_of_level_context context 
)
static

Definition at line 571 of file var.c.

573{
574 if (node == NULL)
575 return false;
576 if (IsA(node, Var))
577 {
578 Var *var = (Var *) node;
579
580 if (var->varlevelsup == context->sublevels_up &&
581 var->location >= 0)
582 {
583 context->var_location = var->location;
584 return true; /* abort tree traversal and return true */
585 }
586 return false;
587 }
588 if (IsA(node, CurrentOfExpr))
589 {
590 /* since CurrentOfExpr doesn't carry location, nothing we can do */
591 return false;
592 }
593 /* No extra code needed for PlaceHolderVar; just look in contained expr */
594 if (IsA(node, Query))
595 {
596 /* Recurse into subselects */
597 bool result;
598
599 context->sublevels_up++;
600 result = query_tree_walker((Query *) node,
602 context,
603 0);
604 context->sublevels_up--;
605 return result;
606 }
607 return expression_tree_walker(node,
609 context);
610}

References expression_tree_walker, fb(), IsA, locate_var_of_level_walker(), Var::location, query_tree_walker, locate_var_of_level_context::sublevels_up, locate_var_of_level_context::var_location, and Var::varlevelsup.

Referenced by locate_var_of_level(), and locate_var_of_level_walker().

◆ mark_nullable_by_grouping()

static Node * mark_nullable_by_grouping ( PlannerInfo root,
Node newnode,
Var oldvar 
)
static

Definition at line 1137 of file var.c.

1138{
1139 Relids relids;
1140
1141 if (root == NULL)
1142 return newnode;
1143 if (oldvar->varnullingrels == NULL)
1144 return newnode; /* nothing to do */
1145
1146 Assert(bms_equal(oldvar->varnullingrels,
1147 bms_make_singleton(root->group_rtindex)));
1148
1149 relids = pull_varnos_of_level(root, newnode, oldvar->varlevelsup);
1150
1151 if (!bms_is_empty(relids))
1152 {
1153 /*
1154 * If the newnode is not variable-free, we set the nullingrels of Vars
1155 * or PHVs that are contained in the expression. This is not really
1156 * 'correct' in theory, because it is the whole expression that can be
1157 * nullable by grouping sets, not its individual vars. But it works
1158 * in practice, because what we need is that the expression can be
1159 * somehow distinguished from the same expression in ECs, and marking
1160 * its vars is sufficient for this purpose.
1161 */
1163 relids,
1164 oldvar->varnullingrels);
1165 }
1166 else /* variable-free? */
1167 {
1168 /*
1169 * If the newnode is variable-free and does not contain volatile
1170 * functions or set-returning functions, it can be treated as a member
1171 * of EC that is redundant. So wrap it in a new PlaceHolderVar to
1172 * carry the nullingrels. Otherwise we do not bother to make any
1173 * changes.
1174 *
1175 * Aggregate functions and window functions are not allowed in
1176 * grouping expressions.
1177 */
1180
1183 {
1185 Relids phrels;
1186
1187 phrels = get_relids_in_jointree((Node *) root->parse->jointree,
1188 true, false);
1190
1192 /* newphv has zero phlevelsup and NULL phnullingrels; fix it */
1193 newphv->phlevelsup = oldvar->varlevelsup;
1194 newphv->phnullingrels = bms_copy(oldvar->varnullingrels);
1195 newnode = (Node *) newphv;
1196 }
1197 }
1198
1199 return newnode;
1200}
Bitmapset * bms_make_singleton(int x)
Definition bitmapset.c:216
bool bms_equal(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:142
bool contain_agg_clause(Node *clause)
Definition clauses.c:190
bool contain_window_function(Node *clause)
Definition clauses.c:227
bool contain_volatile_functions(Node *clause)
Definition clauses.c:547
bool expression_returns_set(Node *clause)
Definition nodeFuncs.c:763
Relids get_relids_in_jointree(Node *jtnode, bool include_outer_joins, bool include_inner_joins)
Node * add_nulling_relids(Node *node, const Bitmapset *target_relids, const Bitmapset *added_relids)

References add_nulling_relids(), Assert, bms_copy(), bms_equal(), bms_is_empty, bms_make_singleton(), contain_agg_clause(), contain_volatile_functions(), contain_window_function(), expression_returns_set(), fb(), get_relids_in_jointree(), make_placeholder_expr(), pull_varnos_of_level(), and root.

Referenced by flatten_group_exprs_mutator().

◆ pull_var_clause()

List * pull_var_clause ( Node node,
int  flags 
)

Definition at line 653 of file var.c.

654{
656
657 /* Assert that caller has not specified inconsistent flags */
664
665 context.varlist = NIL;
666 context.flags = flags;
667
668 pull_var_clause_walker(node, &context);
669 return context.varlist;
670}
#define PVC_RECURSE_AGGREGATES
Definition optimizer.h:189
#define PVC_RECURSE_PLACEHOLDERS
Definition optimizer.h:193
#define PVC_RECURSE_WINDOWFUNCS
Definition optimizer.h:191
#define PVC_INCLUDE_WINDOWFUNCS
Definition optimizer.h:190
#define PVC_INCLUDE_PLACEHOLDERS
Definition optimizer.h:192
#define PVC_INCLUDE_AGGREGATES
Definition optimizer.h:188
static bool pull_var_clause_walker(Node *node, pull_var_clause_context *context)
Definition var.c:673

References Assert, pull_var_clause_context::flags, NIL, pull_var_clause_walker(), PVC_INCLUDE_AGGREGATES, PVC_INCLUDE_PLACEHOLDERS, PVC_INCLUDE_WINDOWFUNCS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_PLACEHOLDERS, PVC_RECURSE_WINDOWFUNCS, and pull_var_clause_context::varlist.

Referenced by add_paths_with_pathkeys_for_rel(), AddRelationNewConstraints(), build_base_rel_tlists(), build_remote_returning(), build_tlist_to_deparse(), create_agg_clause_infos(), CreateTriggerFiringOn(), distribute_qual_to_rels(), estimate_num_groups(), find_computable_ec_member(), find_placeholders_in_expr(), fix_placeholder_input_needed_levels(), foreign_grouping_ok(), generate_base_implied_equalities_no_const(), is_var_in_aggref_only(), make_group_input_target(), make_partial_grouping_target(), make_sort_input_target(), make_window_input_target(), preprocess_targetlist(), process_implied_equality(), qual_is_pushdown_safe(), rebuild_eclass_attr_needed(), rebuild_joinclause_attr_needed(), rebuild_placeholder_attr_needed(), semijoin_target_ok(), and StoreRelCheck().

◆ pull_var_clause_walker()

static bool pull_var_clause_walker ( Node node,
pull_var_clause_context context 
)
static

Definition at line 673 of file var.c.

674{
675 if (node == NULL)
676 return false;
677 if (IsA(node, Var))
678 {
679 if (((Var *) node)->varlevelsup != 0)
680 elog(ERROR, "Upper-level Var found where not expected");
681 context->varlist = lappend(context->varlist, node);
682 return false;
683 }
684 else if (IsA(node, Aggref))
685 {
686 if (((Aggref *) node)->agglevelsup != 0)
687 elog(ERROR, "Upper-level Aggref found where not expected");
688 if (context->flags & PVC_INCLUDE_AGGREGATES)
689 {
690 context->varlist = lappend(context->varlist, node);
691 /* we do NOT descend into the contained expression */
692 return false;
693 }
694 else if (context->flags & PVC_RECURSE_AGGREGATES)
695 {
696 /* fall through to recurse into the aggregate's arguments */
697 }
698 else
699 elog(ERROR, "Aggref found where not expected");
700 }
701 else if (IsA(node, GroupingFunc))
702 {
703 if (((GroupingFunc *) node)->agglevelsup != 0)
704 elog(ERROR, "Upper-level GROUPING found where not expected");
705 if (context->flags & PVC_INCLUDE_AGGREGATES)
706 {
707 context->varlist = lappend(context->varlist, node);
708 /* we do NOT descend into the contained expression */
709 return false;
710 }
711 else if (context->flags & PVC_RECURSE_AGGREGATES)
712 {
713 /* fall through to recurse into the GroupingFunc's arguments */
714 }
715 else
716 elog(ERROR, "GROUPING found where not expected");
717 }
718 else if (IsA(node, WindowFunc))
719 {
720 /* WindowFuncs have no levelsup field to check ... */
721 if (context->flags & PVC_INCLUDE_WINDOWFUNCS)
722 {
723 context->varlist = lappend(context->varlist, node);
724 /* we do NOT descend into the contained expressions */
725 return false;
726 }
727 else if (context->flags & PVC_RECURSE_WINDOWFUNCS)
728 {
729 /* fall through to recurse into the windowfunc's arguments */
730 }
731 else
732 elog(ERROR, "WindowFunc found where not expected");
733 }
734 else if (IsA(node, PlaceHolderVar))
735 {
736 if (((PlaceHolderVar *) node)->phlevelsup != 0)
737 elog(ERROR, "Upper-level PlaceHolderVar found where not expected");
738 if (context->flags & PVC_INCLUDE_PLACEHOLDERS)
739 {
740 context->varlist = lappend(context->varlist, node);
741 /* we do NOT descend into the contained expression */
742 return false;
743 }
744 else if (context->flags & PVC_RECURSE_PLACEHOLDERS)
745 {
746 /* fall through to recurse into the placeholder's expression */
747 }
748 else
749 elog(ERROR, "PlaceHolderVar found where not expected");
750 }
751 return expression_tree_walker(node, pull_var_clause_walker, context);
752}

References elog, ERROR, expression_tree_walker, fb(), pull_var_clause_context::flags, IsA, lappend(), pull_var_clause_walker(), PVC_INCLUDE_AGGREGATES, PVC_INCLUDE_PLACEHOLDERS, PVC_INCLUDE_WINDOWFUNCS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_PLACEHOLDERS, PVC_RECURSE_WINDOWFUNCS, and pull_var_clause_context::varlist.

Referenced by pull_var_clause(), and pull_var_clause_walker().

◆ pull_varattnos()

◆ pull_varattnos_walker()

static bool pull_varattnos_walker ( Node node,
pull_varattnos_context context 
)
static

Definition at line 309 of file var.c.

310{
311 if (node == NULL)
312 return false;
313 if (IsA(node, Var))
314 {
315 Var *var = (Var *) node;
316
317 if (var->varno == context->varno && var->varlevelsup == 0)
318 context->varattnos =
319 bms_add_member(context->varattnos,
321 return false;
322 }
323
324 /* Should not find an unplanned subquery */
325 Assert(!IsA(node, Query));
326
327 return expression_tree_walker(node, pull_varattnos_walker, context);
328}
#define FirstLowInvalidHeapAttributeNumber
Definition sysattr.h:27

References Assert, bms_add_member(), expression_tree_walker, fb(), FirstLowInvalidHeapAttributeNumber, IsA, pull_varattnos_walker(), Var::varattno, pull_varattnos_context::varattnos, Var::varlevelsup, pull_varattnos_context::varno, and Var::varno.

Referenced by pull_varattnos(), and pull_varattnos_walker().

◆ pull_varnos()

Relids pull_varnos ( PlannerInfo root,
Node node 
)

Definition at line 114 of file var.c.

115{
116 pull_varnos_context context;
117
118 context.varnos = NULL;
119 context.root = root;
120 context.sublevels_up = 0;
121
122 /*
123 * Must be prepared to start with a Query or a bare expression tree; if
124 * it's a Query, we don't want to increment sublevels_up.
125 */
128 &context,
129 0);
130
131 return context.varnos;
132}
PlannerInfo * root
Definition var.c:36
Relids varnos
Definition var.c:35
static bool pull_varnos_walker(Node *node, pull_varnos_context *context)
Definition var.c:161

References fb(), pull_varnos_walker(), query_or_expression_tree_walker, pull_varnos_context::root, root, pull_varnos_context::sublevels_up, and pull_varnos_context::varnos.

Referenced by compute_semijoin_info(), convert_ANY_sublink_to_join(), convert_EXISTS_sublink_to_join(), cost_incremental_sort(), create_agg_clause_infos(), distribute_qual_to_rels(), examine_variable(), expand_indexqual_rowcompare(), extract_lateral_vars_from_PHVs(), find_placeholder_info(), get_eclass_for_sort_expr(), is_pseudo_constant_for_index(), IsTidEqualAnyClause(), join_is_removable(), make_outerjoininfo(), make_plain_restrictinfo(), match_rowcompare_to_indexcol(), match_saopclause_to_indexcol(), NumRelids(), pg_get_expr_worker(), process_implied_equality(), pullup_replace_vars_callback(), remove_leftjoinrel_from_query(), and subquery_planner().

◆ pull_varnos_of_level()

Relids pull_varnos_of_level ( PlannerInfo root,
Node node,
int  levelsup 
)

Definition at line 140 of file var.c.

141{
142 pull_varnos_context context;
143
144 context.varnos = NULL;
145 context.root = root;
146 context.sublevels_up = levelsup;
147
148 /*
149 * Must be prepared to start with a Query or a bare expression tree; if
150 * it's a Query, we don't want to increment sublevels_up.
151 */
154 &context,
155 0);
156
157 return context.varnos;
158}

References fb(), pull_varnos_walker(), query_or_expression_tree_walker, pull_varnos_context::root, root, pull_varnos_context::sublevels_up, and pull_varnos_context::varnos.

Referenced by add_nullingrels_if_needed(), convert_ANY_sublink_to_join(), is_simple_subquery(), jointree_contains_lateral_outer_refs(), and mark_nullable_by_grouping().

◆ pull_varnos_walker()

static bool pull_varnos_walker ( Node node,
pull_varnos_context context 
)
static

Definition at line 161 of file var.c.

162{
163 if (node == NULL)
164 return false;
165 if (IsA(node, Var))
166 {
167 Var *var = (Var *) node;
168
169 if (var->varlevelsup == context->sublevels_up)
170 {
171 context->varnos = bms_add_member(context->varnos, var->varno);
172 context->varnos = bms_add_members(context->varnos,
173 var->varnullingrels);
174 }
175 return false;
176 }
177 if (IsA(node, CurrentOfExpr))
178 {
179 CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
180
181 if (context->sublevels_up == 0)
182 context->varnos = bms_add_member(context->varnos, cexpr->cvarno);
183 return false;
184 }
185 if (IsA(node, PlaceHolderVar))
186 {
188
189 /*
190 * If a PlaceHolderVar is not of the target query level, ignore it,
191 * instead recursing into its expression to see if it contains any
192 * vars that are of the target level. We'll also do that when the
193 * caller doesn't pass a "root" pointer. (We probably shouldn't see
194 * PlaceHolderVars at all in such cases, but if we do, this is a
195 * reasonable behavior.)
196 */
197 if (phv->phlevelsup == context->sublevels_up &&
198 context->root != NULL)
199 {
200 /*
201 * Ideally, the PHV's contribution to context->varnos is its
202 * ph_eval_at set. However, this code can be invoked before
203 * that's been computed. If we cannot find a PlaceHolderInfo,
204 * fall back to the conservative assumption that the PHV will be
205 * evaluated at its syntactic level (phv->phrels).
206 *
207 * Another problem is that a PlaceHolderVar can appear in quals or
208 * tlists that have been translated for use in a child appendrel.
209 * Typically such a PHV is a parameter expression sourced by some
210 * other relation, so that the translation from parent appendrel
211 * to child doesn't change its phrels, and we should still take
212 * ph_eval_at at face value. But in corner cases, the PHV's
213 * original phrels can include the parent appendrel itself, in
214 * which case the translated PHV will have the child appendrel in
215 * phrels, and we must translate ph_eval_at to match.
216 */
218
219 if (phv->phlevelsup == 0)
220 {
221 if (phv->phid < context->root->placeholder_array_size)
222 phinfo = context->root->placeholder_array[phv->phid];
223 }
224 if (phinfo == NULL)
225 {
226 /* No PlaceHolderInfo yet, use phrels */
227 context->varnos = bms_add_members(context->varnos,
228 phv->phrels);
229 }
230 else if (bms_equal(phv->phrels, phinfo->ph_var->phrels))
231 {
232 /* Normal case: use ph_eval_at */
233 context->varnos = bms_add_members(context->varnos,
234 phinfo->ph_eval_at);
235 }
236 else
237 {
238 /* Translated PlaceHolderVar: translate ph_eval_at to match */
240 delta;
241
242 /* remove what was removed from phv->phrels ... */
243 delta = bms_difference(phinfo->ph_var->phrels, phv->phrels);
244 newevalat = bms_difference(phinfo->ph_eval_at, delta);
245 /* ... then if that was in fact part of ph_eval_at ... */
246 if (!bms_equal(newevalat, phinfo->ph_eval_at))
247 {
248 /* ... add what was added */
249 delta = bms_difference(phv->phrels, phinfo->ph_var->phrels);
250 newevalat = bms_join(newevalat, delta);
251 }
252 context->varnos = bms_join(context->varnos,
253 newevalat);
254 }
255
256 /*
257 * In all three cases, include phnullingrels in the result. We
258 * don't worry about possibly needing to translate it, because
259 * appendrels only translate varnos of baserels, not outer joins.
260 */
261 context->varnos = bms_add_members(context->varnos,
262 phv->phnullingrels);
263 return false; /* don't recurse into expression */
264 }
265 }
266 else if (IsA(node, Query))
267 {
268 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
269 bool result;
270
271 context->sublevels_up++;
272 result = query_tree_walker((Query *) node, pull_varnos_walker,
273 context, 0);
274 context->sublevels_up--;
275 return result;
276 }
277 return expression_tree_walker(node, pull_varnos_walker, context);
278}
Bitmapset * bms_difference(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:346

References bms_add_member(), bms_add_members(), bms_difference(), bms_equal(), bms_join(), CurrentOfExpr::cvarno, expression_tree_walker, fb(), IsA, pull_varnos_walker(), query_tree_walker, pull_varnos_context::root, pull_varnos_context::sublevels_up, Var::varlevelsup, Var::varno, and pull_varnos_context::varnos.

Referenced by pull_varnos(), pull_varnos_of_level(), and pull_varnos_walker().

◆ pull_vars_of_level()

List * pull_vars_of_level ( Node node,
int  levelsup 
)

Definition at line 339 of file var.c.

340{
341 pull_vars_context context;
342
343 context.vars = NIL;
344 context.sublevels_up = levelsup;
345
346 /*
347 * Must be prepared to start with a Query or a bare expression tree; if
348 * it's a Query, we don't want to increment sublevels_up.
349 */
352 &context,
353 0);
354
355 return context.vars;
356}
int sublevels_up
Definition var.c:49
List * vars
Definition var.c:48
static bool pull_vars_walker(Node *node, pull_vars_context *context)
Definition var.c:359

References NIL, pull_vars_walker(), query_or_expression_tree_walker, pull_vars_context::sublevels_up, and pull_vars_context::vars.

Referenced by extract_lateral_references(), and extract_lateral_vars_from_PHVs().

◆ pull_vars_walker()

static bool pull_vars_walker ( Node node,
pull_vars_context context 
)
static

Definition at line 359 of file var.c.

360{
361 if (node == NULL)
362 return false;
363 if (IsA(node, Var))
364 {
365 Var *var = (Var *) node;
366
367 if (var->varlevelsup == context->sublevels_up)
368 context->vars = lappend(context->vars, var);
369 return false;
370 }
371 if (IsA(node, PlaceHolderVar))
372 {
374
375 if (phv->phlevelsup == context->sublevels_up)
376 context->vars = lappend(context->vars, phv);
377 /* we don't want to look into the contained expression */
378 return false;
379 }
380 if (IsA(node, Query))
381 {
382 /* Recurse into RTE subquery or not-yet-planned sublink subquery */
383 bool result;
384
385 context->sublevels_up++;
386 result = query_tree_walker((Query *) node, pull_vars_walker,
387 context, 0);
388 context->sublevels_up--;
389 return result;
390 }
391 return expression_tree_walker(node, pull_vars_walker, context);
392}

References expression_tree_walker, fb(), IsA, lappend(), pull_vars_walker(), query_tree_walker, pull_vars_context::sublevels_up, Var::varlevelsup, and pull_vars_context::vars.

Referenced by pull_vars_of_level(), and pull_vars_walker().