PostgreSQL Source Code  git master
var.c File Reference
#include "postgres.h"
#include "access/sysattr.h"
#include "nodes/nodeFuncs.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 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 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)
 
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)
 

Function Documentation

◆ add_nullingrels_if_needed()

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

Definition at line 910 of file var.c.

911 {
912  if (oldvar->varnullingrels == NULL)
913  return newnode; /* nothing to do */
914  /* If possible, do it by adding to existing nullingrel fields */
915  if (is_standard_join_alias_expression(newnode, oldvar))
916  adjust_standard_join_alias_expression(newnode, oldvar);
917  else if (root)
918  {
919  /*
920  * We can insert a PlaceHolderVar to carry the nullingrels. However,
921  * deciding where to evaluate the PHV is slightly tricky. We first
922  * try to evaluate it at the natural semantic level of the new
923  * expression; but if that expression is variable-free, fall back to
924  * evaluating it at the join that the oldvar is an alias Var for.
925  */
926  PlaceHolderVar *newphv;
927  Index levelsup = oldvar->varlevelsup;
928  Relids phrels = pull_varnos_of_level(root, newnode, levelsup);
929 
930  if (bms_is_empty(phrels)) /* variable-free? */
931  {
932  if (levelsup != 0) /* this won't work otherwise */
933  elog(ERROR, "unsupported join alias expression");
934  phrels = get_relids_for_join(root->parse, oldvar->varno);
935  /* If it's an outer join, eval below not above the join */
936  phrels = bms_del_member(phrels, oldvar->varno);
937  Assert(!bms_is_empty(phrels));
938  }
939  newphv = make_placeholder_expr(root, (Expr *) newnode, phrels);
940  /* newphv has zero phlevelsup and NULL phnullingrels; fix it */
941  newphv->phlevelsup = levelsup;
942  newphv->phnullingrels = bms_copy(oldvar->varnullingrels);
943  newnode = (Node *) newphv;
944  }
945  else
946  {
947  /* ooops, we're missing support for something the parser can make */
948  elog(ERROR, "unsupported join alias expression");
949  }
950  return newnode;
951 }
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:868
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:858
unsigned int Index
Definition: c.h:614
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
PlaceHolderVar * make_placeholder_expr(PlannerInfo *root, Expr *expr, Relids phrels)
Definition: placeholder.c:54
Relids get_relids_for_join(Query *query, int joinrelid)
tree ctl root
Definition: radixtree.h:1880
Definition: nodes.h:129
Relids phnullingrels
Definition: pathnodes.h:2779
Index phlevelsup
Definition: pathnodes.h:2785
int varno
Definition: primnodes.h:255
Index varlevelsup
Definition: primnodes.h:280
Relids pull_varnos_of_level(PlannerInfo *root, Node *node, int levelsup)
Definition: var.c:134
static bool is_standard_join_alias_expression(Node *newnode, Var *oldvar)
Definition: var.c:962
static void adjust_standard_join_alias_expression(Node *newnode, Var *oldvar)
Definition: var.c:1036

References adjust_standard_join_alias_expression(), Assert, bms_copy(), bms_del_member(), bms_is_empty, elog, ERROR, get_relids_for_join(), is_standard_join_alias_expression(), make_placeholder_expr(), PlaceHolderVar::phlevelsup, PlaceHolderVar::phnullingrels, pull_varnos_of_level(), root, Var::varlevelsup, and Var::varno.

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 1036 of file var.c.

1037 {
1038  if (IsA(newnode, Var) &&
1039  ((Var *) newnode)->varlevelsup == oldvar->varlevelsup)
1040  {
1041  Var *newvar = (Var *) newnode;
1042 
1043  newvar->varnullingrels = bms_add_members(newvar->varnullingrels,
1044  oldvar->varnullingrels);
1045  }
1046  else if (IsA(newnode, PlaceHolderVar) &&
1047  ((PlaceHolderVar *) newnode)->phlevelsup == oldvar->varlevelsup)
1048  {
1049  PlaceHolderVar *newphv = (PlaceHolderVar *) newnode;
1050 
1051  newphv->phnullingrels = bms_add_members(newphv->phnullingrels,
1052  oldvar->varnullingrels);
1053  }
1054  else if (IsA(newnode, FuncExpr))
1055  {
1056  FuncExpr *fexpr = (FuncExpr *) newnode;
1057 
1059  }
1060  else if (IsA(newnode, RelabelType))
1061  {
1062  RelabelType *relabel = (RelabelType *) newnode;
1063 
1064  adjust_standard_join_alias_expression((Node *) relabel->arg, oldvar);
1065  }
1066  else if (IsA(newnode, CoerceViaIO))
1067  {
1068  CoerceViaIO *iocoerce = (CoerceViaIO *) newnode;
1069 
1070  adjust_standard_join_alias_expression((Node *) iocoerce->arg, oldvar);
1071  }
1072  else if (IsA(newnode, ArrayCoerceExpr))
1073  {
1074  ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) newnode;
1075 
1076  adjust_standard_join_alias_expression((Node *) acoerce->arg, oldvar);
1077  }
1078  else if (IsA(newnode, CoalesceExpr))
1079  {
1080  CoalesceExpr *cexpr = (CoalesceExpr *) newnode;
1081  ListCell *lc;
1082 
1083  Assert(cexpr->args != NIL);
1084  foreach(lc, cexpr->args)
1085  {
1087  }
1088  }
1089  else
1090  Assert(false);
1091 }
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:917
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
#define linitial(l)
Definition: pg_list.h:178
List * args
Definition: primnodes.h:1462
Expr * arg
Definition: primnodes.h:1177
List * args
Definition: primnodes.h:738
Expr * arg
Definition: primnodes.h:1154
Definition: primnodes.h:248

References RelabelType::arg, CoerceViaIO::arg, ArrayCoerceExpr::arg, FuncExpr::args, CoalesceExpr::args, Assert, bms_add_members(), IsA, lfirst, linitial, NIL, PlaceHolderVar::phnullingrels, and Var::varlevelsup.

Referenced by add_nullingrels_if_needed().

◆ alias_relid_set()

static Relids alias_relid_set ( Query query,
Relids  relids 
)
static

Definition at line 1098 of file var.c.

1099 {
1100  Relids result = NULL;
1101  int rtindex;
1102 
1103  rtindex = -1;
1104  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
1105  {
1106  RangeTblEntry *rte = rt_fetch(rtindex, query->rtable);
1107 
1108  if (rte->rtekind == RTE_JOIN)
1109  result = bms_join(result, get_relids_for_join(query, rtindex));
1110  else
1111  result = bms_add_member(result, rtindex);
1112  }
1113  return result;
1114 }
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:1230
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
@ RTE_JOIN
Definition: parsenodes.h:1030
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
List * rtable
Definition: parsenodes.h:168
RTEKind rtekind
Definition: parsenodes.h:1057

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

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 409 of file var.c.

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

References context, expression_tree_walker, and IsA.

Referenced by contain_var_clause().

◆ contain_vars_of_level()

bool contain_vars_of_level ( Node node,
int  levelsup 
)

Definition at line 441 of file var.c.

442 {
443  int sublevels_up = levelsup;
444 
447  (void *) &sublevels_up,
448  0);
449 }
#define query_or_expression_tree_walker(n, w, c, f)
Definition: nodeFuncs.h:169
static bool contain_vars_of_level_walker(Node *node, int *sublevels_up)
Definition: var.c:452

References contain_vars_of_level_walker(), and query_or_expression_tree_walker.

Referenced by apply_child_basequals(), checkExprIsVarFree(), convert_EXISTS_sublink_to_join(), convert_EXISTS_to_ANY(), pull_up_simple_values(), rewriteRuleAction(), transformAExprIn(), transformJsonTable(), transformRangeFunction(), transformRangeTableFunc(), transformSetOperationTree(), and transformValuesClause().

◆ contain_vars_of_level_walker()

static bool contain_vars_of_level_walker ( Node node,
int *  sublevels_up 
)
static

Definition at line 452 of file var.c.

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

References expression_tree_walker, IsA, and query_tree_walker.

Referenced by contain_vars_of_level().

◆ flatten_join_alias_vars()

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

Definition at line 744 of file var.c.

745 {
747 
748  /*
749  * We do not expect this to be applied to the whole Query, only to
750  * expressions or LATERAL subqueries. Hence, if the top node is a Query,
751  * it's okay to immediately increment sublevels_up.
752  */
753  Assert(node != (Node *) query);
754 
755  context.root = root;
756  context.query = query;
757  context.sublevels_up = 0;
758  /* flag whether join aliases could possibly contain SubLinks */
759  context.possible_sublink = query->hasSubLinks;
760  /* if hasSubLinks is already true, no need to work hard */
761  context.inserted_sublink = query->hasSubLinks;
762 
764 }
static Node * flatten_join_alias_vars_mutator(Node *node, flatten_join_alias_vars_context *context)
Definition: var.c:767

References Assert, context, flatten_join_alias_vars_mutator(), and root.

Referenced by finalize_grouping_exprs_walker(), parseCheckAggregates(), 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 767 of file var.c.

769 {
770  if (node == NULL)
771  return NULL;
772  if (IsA(node, Var))
773  {
774  Var *var = (Var *) node;
775  RangeTblEntry *rte;
776  Node *newvar;
777 
778  /* No change unless Var belongs to a JOIN of the target level */
779  if (var->varlevelsup != context->sublevels_up)
780  return node; /* no need to copy, really */
781  rte = rt_fetch(var->varno, context->query->rtable);
782  if (rte->rtekind != RTE_JOIN)
783  return node;
784  if (var->varattno == InvalidAttrNumber)
785  {
786  /* Must expand whole-row reference */
787  RowExpr *rowexpr;
788  List *fields = NIL;
789  List *colnames = NIL;
790  ListCell *lv;
791  ListCell *ln;
792 
793  Assert(list_length(rte->joinaliasvars) == list_length(rte->eref->colnames));
794  forboth(lv, rte->joinaliasvars, ln, rte->eref->colnames)
795  {
796  newvar = (Node *) lfirst(lv);
797  /* Ignore dropped columns */
798  if (newvar == NULL)
799  continue;
800  newvar = copyObject(newvar);
801 
802  /*
803  * If we are expanding an alias carried down from an upper
804  * query, must adjust its varlevelsup fields.
805  */
806  if (context->sublevels_up != 0)
807  IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
808  /* Preserve original Var's location, if possible */
809  if (IsA(newvar, Var))
810  ((Var *) newvar)->location = var->location;
811  /* Recurse in case join input is itself a join */
812  /* (also takes care of setting inserted_sublink if needed) */
813  newvar = flatten_join_alias_vars_mutator(newvar, context);
814  fields = lappend(fields, newvar);
815  /* We need the names of non-dropped columns, too */
816  colnames = lappend(colnames, copyObject((Node *) lfirst(ln)));
817  }
818  rowexpr = makeNode(RowExpr);
819  rowexpr->args = fields;
820  rowexpr->row_typeid = var->vartype;
821  rowexpr->row_format = COERCE_IMPLICIT_CAST;
822  /* vartype will always be RECORDOID, so we always need colnames */
823  rowexpr->colnames = colnames;
824  rowexpr->location = var->location;
825 
826  /* Lastly, add any varnullingrels to the replacement expression */
827  return add_nullingrels_if_needed(context->root, (Node *) rowexpr,
828  var);
829  }
830 
831  /* Expand join alias reference */
832  Assert(var->varattno > 0);
833  newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
834  Assert(newvar != NULL);
835  newvar = copyObject(newvar);
836 
837  /*
838  * If we are expanding an alias carried down from an upper query, must
839  * adjust its varlevelsup fields.
840  */
841  if (context->sublevels_up != 0)
842  IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
843 
844  /* Preserve original Var's location, if possible */
845  if (IsA(newvar, Var))
846  ((Var *) newvar)->location = var->location;
847 
848  /* Recurse in case join input is itself a join */
849  newvar = flatten_join_alias_vars_mutator(newvar, context);
850 
851  /* Detect if we are adding a sublink to query */
852  if (context->possible_sublink && !context->inserted_sublink)
853  context->inserted_sublink = checkExprHasSubLink(newvar);
854 
855  /* Lastly, add any varnullingrels to the replacement expression */
856  return add_nullingrels_if_needed(context->root, newvar, var);
857  }
858  if (IsA(node, PlaceHolderVar))
859  {
860  /* Copy the PlaceHolderVar node with correct mutation of subnodes */
861  PlaceHolderVar *phv;
862 
865  (void *) context);
866  /* now fix PlaceHolderVar's relid sets */
867  if (phv->phlevelsup == context->sublevels_up)
868  {
869  phv->phrels = alias_relid_set(context->query,
870  phv->phrels);
871  /* we *don't* change phnullingrels */
872  }
873  return (Node *) phv;
874  }
875 
876  if (IsA(node, Query))
877  {
878  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
879  Query *newnode;
880  bool save_inserted_sublink;
881 
882  context->sublevels_up++;
883  save_inserted_sublink = context->inserted_sublink;
884  context->inserted_sublink = ((Query *) node)->hasSubLinks;
885  newnode = query_tree_mutator((Query *) node,
887  (void *) context,
889  newnode->hasSubLinks |= context->inserted_sublink;
890  context->inserted_sublink = save_inserted_sublink;
891  context->sublevels_up--;
892  return (Node *) newnode;
893  }
894  /* Already-planned tree not supported */
895  Assert(!IsA(node, SubPlan));
896  /* Shouldn't need to handle these planner auxiliary nodes here */
897  Assert(!IsA(node, SpecialJoinInfo));
898  Assert(!IsA(node, PlaceHolderInfo));
899  Assert(!IsA(node, MinMaxAggInfo));
900 
902  (void *) context);
903 }
#define InvalidAttrNumber
Definition: attnum.h:23
List * lappend(List *list, void *datum)
Definition: list.c:339
#define expression_tree_mutator(n, m, c)
Definition: nodeFuncs.h:153
#define query_tree_mutator(q, m, c, f)
Definition: nodeFuncs.h:158
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:25
#define copyObject(obj)
Definition: nodes.h:224
#define makeNode(_type_)
Definition: nodes.h:155
static int list_length(const List *l)
Definition: pg_list.h:152
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:706
bool checkExprHasSubLink(Node *node)
Definition: rewriteManip.c:295
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:849
Definition: pg_list.h:54
List * args
Definition: primnodes.h:1381
ParseLoc location
Definition: primnodes.h:1405
ParseLoc location
Definition: primnodes.h:293
AttrNumber varattno
Definition: primnodes.h:260
static Node * add_nullingrels_if_needed(PlannerInfo *root, Node *newnode, Var *oldvar)
Definition: var.c:910
static Relids alias_relid_set(Query *query, Relids relids)
Definition: var.c:1098

References add_nullingrels_if_needed(), alias_relid_set(), RowExpr::args, Assert, checkExprHasSubLink(), COERCE_IMPLICIT_CAST, context, copyObject, expression_tree_mutator, forboth, IncrementVarSublevelsUp(), InvalidAttrNumber, IsA, lappend(), lfirst, list_length(), list_nth(), Var::location, RowExpr::location, makeNode, NIL, PlaceHolderVar::phlevelsup, QTW_IGNORE_JOINALIASES, query_tree_mutator, rt_fetch, RTE_JOIN, RangeTblEntry::rtekind, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by flatten_join_alias_vars().

◆ is_standard_join_alias_expression()

static bool is_standard_join_alias_expression ( Node newnode,
Var oldvar 
)
static

Definition at line 962 of file var.c.

963 {
964  if (newnode == NULL)
965  return false;
966  if (IsA(newnode, Var) &&
967  ((Var *) newnode)->varlevelsup == oldvar->varlevelsup)
968  return true;
969  else if (IsA(newnode, PlaceHolderVar) &&
970  ((PlaceHolderVar *) newnode)->phlevelsup == oldvar->varlevelsup)
971  return true;
972  else if (IsA(newnode, FuncExpr))
973  {
974  FuncExpr *fexpr = (FuncExpr *) newnode;
975 
976  /*
977  * We need to assume that the function wouldn't produce non-NULL from
978  * NULL, which is reasonable for implicit coercions but otherwise not
979  * so much. (Looking at its strictness is likely overkill, and anyway
980  * it would cause us to fail if someone forgot to mark an implicit
981  * coercion as strict.)
982  */
983  if (fexpr->funcformat != COERCE_IMPLICIT_CAST ||
984  fexpr->args == NIL)
985  return false;
986 
987  /*
988  * Examine only the first argument --- coercions might have additional
989  * arguments that are constants.
990  */
991  return is_standard_join_alias_expression(linitial(fexpr->args), oldvar);
992  }
993  else if (IsA(newnode, RelabelType))
994  {
995  RelabelType *relabel = (RelabelType *) newnode;
996 
997  /* This definitely won't produce non-NULL from NULL */
998  return is_standard_join_alias_expression((Node *) relabel->arg, oldvar);
999  }
1000  else if (IsA(newnode, CoerceViaIO))
1001  {
1002  CoerceViaIO *iocoerce = (CoerceViaIO *) newnode;
1003 
1004  /* This definitely won't produce non-NULL from NULL */
1005  return is_standard_join_alias_expression((Node *) iocoerce->arg, oldvar);
1006  }
1007  else if (IsA(newnode, ArrayCoerceExpr))
1008  {
1009  ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) newnode;
1010 
1011  /* This definitely won't produce non-NULL from NULL (at array level) */
1012  return is_standard_join_alias_expression((Node *) acoerce->arg, oldvar);
1013  }
1014  else if (IsA(newnode, CoalesceExpr))
1015  {
1016  CoalesceExpr *cexpr = (CoalesceExpr *) newnode;
1017  ListCell *lc;
1018 
1019  Assert(cexpr->args != NIL);
1020  foreach(lc, cexpr->args)
1021  {
1022  if (!is_standard_join_alias_expression(lfirst(lc), oldvar))
1023  return false;
1024  }
1025  return true;
1026  }
1027  else
1028  return false;
1029 }

References RelabelType::arg, CoerceViaIO::arg, ArrayCoerceExpr::arg, FuncExpr::args, CoalesceExpr::args, Assert, COERCE_IMPLICIT_CAST, IsA, lfirst, linitial, NIL, and Var::varlevelsup.

Referenced by add_nullingrels_if_needed().

◆ locate_var_of_level()

int locate_var_of_level ( Node node,
int  levelsup 
)

Definition at line 509 of file var.c.

510 {
512 
513  context.var_location = -1; /* in case we find nothing */
514  context.sublevels_up = levelsup;
515 
518  (void *) &context,
519  0);
520 
521  return context.var_location;
522 }
static bool locate_var_of_level_walker(Node *node, locate_var_of_level_context *context)
Definition: var.c:525

References context, locate_var_of_level_walker(), and query_or_expression_tree_walker.

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 525 of file var.c.

527 {
528  if (node == NULL)
529  return false;
530  if (IsA(node, Var))
531  {
532  Var *var = (Var *) node;
533 
534  if (var->varlevelsup == context->sublevels_up &&
535  var->location >= 0)
536  {
537  context->var_location = var->location;
538  return true; /* abort tree traversal and return true */
539  }
540  return false;
541  }
542  if (IsA(node, CurrentOfExpr))
543  {
544  /* since CurrentOfExpr doesn't carry location, nothing we can do */
545  return false;
546  }
547  /* No extra code needed for PlaceHolderVar; just look in contained expr */
548  if (IsA(node, Query))
549  {
550  /* Recurse into subselects */
551  bool result;
552 
553  context->sublevels_up++;
554  result = query_tree_walker((Query *) node,
556  (void *) context,
557  0);
558  context->sublevels_up--;
559  return result;
560  }
561  return expression_tree_walker(node,
563  (void *) context);
564 }

References context, expression_tree_walker, IsA, Var::location, query_tree_walker, and Var::varlevelsup.

Referenced by locate_var_of_level().

◆ pull_var_clause()

List* pull_var_clause ( Node node,
int  flags 
)

Definition at line 607 of file var.c.

608 {
610 
611  /* Assert that caller has not specified inconsistent flags */
618 
619  context.varlist = NIL;
620  context.flags = flags;
621 
623  return context.varlist;
624 }
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:187
#define PVC_RECURSE_PLACEHOLDERS
Definition: optimizer.h:191
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:189
#define PVC_INCLUDE_WINDOWFUNCS
Definition: optimizer.h:188
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:190
#define PVC_INCLUDE_AGGREGATES
Definition: optimizer.h:186
static bool pull_var_clause_walker(Node *node, pull_var_clause_context *context)
Definition: var.c:627

References Assert, context, NIL, pull_var_clause_walker(), PVC_INCLUDE_AGGREGATES, PVC_INCLUDE_PLACEHOLDERS, PVC_INCLUDE_WINDOWFUNCS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_PLACEHOLDERS, and PVC_RECURSE_WINDOWFUNCS.

Referenced by add_paths_with_pathkeys_for_rel(), AddRelationNewConstraints(), build_base_rel_tlists(), build_remote_returning(), build_tlist_to_deparse(), 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(), 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(), 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 627 of file var.c.

628 {
629  if (node == NULL)
630  return false;
631  if (IsA(node, Var))
632  {
633  if (((Var *) node)->varlevelsup != 0)
634  elog(ERROR, "Upper-level Var found where not expected");
635  context->varlist = lappend(context->varlist, node);
636  return false;
637  }
638  else if (IsA(node, Aggref))
639  {
640  if (((Aggref *) node)->agglevelsup != 0)
641  elog(ERROR, "Upper-level Aggref found where not expected");
642  if (context->flags & PVC_INCLUDE_AGGREGATES)
643  {
644  context->varlist = lappend(context->varlist, node);
645  /* we do NOT descend into the contained expression */
646  return false;
647  }
648  else if (context->flags & PVC_RECURSE_AGGREGATES)
649  {
650  /* fall through to recurse into the aggregate's arguments */
651  }
652  else
653  elog(ERROR, "Aggref found where not expected");
654  }
655  else if (IsA(node, GroupingFunc))
656  {
657  if (((GroupingFunc *) node)->agglevelsup != 0)
658  elog(ERROR, "Upper-level GROUPING found where not expected");
659  if (context->flags & PVC_INCLUDE_AGGREGATES)
660  {
661  context->varlist = lappend(context->varlist, node);
662  /* we do NOT descend into the contained expression */
663  return false;
664  }
665  else if (context->flags & PVC_RECURSE_AGGREGATES)
666  {
667  /* fall through to recurse into the GroupingFunc's arguments */
668  }
669  else
670  elog(ERROR, "GROUPING found where not expected");
671  }
672  else if (IsA(node, WindowFunc))
673  {
674  /* WindowFuncs have no levelsup field to check ... */
675  if (context->flags & PVC_INCLUDE_WINDOWFUNCS)
676  {
677  context->varlist = lappend(context->varlist, node);
678  /* we do NOT descend into the contained expressions */
679  return false;
680  }
681  else if (context->flags & PVC_RECURSE_WINDOWFUNCS)
682  {
683  /* fall through to recurse into the windowfunc's arguments */
684  }
685  else
686  elog(ERROR, "WindowFunc found where not expected");
687  }
688  else if (IsA(node, PlaceHolderVar))
689  {
690  if (((PlaceHolderVar *) node)->phlevelsup != 0)
691  elog(ERROR, "Upper-level PlaceHolderVar found where not expected");
692  if (context->flags & PVC_INCLUDE_PLACEHOLDERS)
693  {
694  context->varlist = lappend(context->varlist, node);
695  /* we do NOT descend into the contained expression */
696  return false;
697  }
698  else if (context->flags & PVC_RECURSE_PLACEHOLDERS)
699  {
700  /* fall through to recurse into the placeholder's expression */
701  }
702  else
703  elog(ERROR, "PlaceHolderVar found where not expected");
704  }
706  (void *) context);
707 }

References context, elog, ERROR, expression_tree_walker, IsA, lappend(), PVC_INCLUDE_AGGREGATES, PVC_INCLUDE_PLACEHOLDERS, PVC_INCLUDE_WINDOWFUNCS, PVC_RECURSE_AGGREGATES, PVC_RECURSE_PLACEHOLDERS, and PVC_RECURSE_WINDOWFUNCS.

Referenced by pull_var_clause().

◆ pull_varattnos()

void pull_varattnos ( Node node,
Index  varno,
Bitmapset **  varattnos 
)

◆ pull_varattnos_walker()

static bool pull_varattnos_walker ( Node node,
pull_varattnos_context context 
)
static

Definition at line 304 of file var.c.

305 {
306  if (node == NULL)
307  return false;
308  if (IsA(node, Var))
309  {
310  Var *var = (Var *) node;
311 
312  if (var->varno == context->varno && var->varlevelsup == 0)
313  context->varattnos =
314  bms_add_member(context->varattnos,
316  return false;
317  }
318 
319  /* Should not find an unplanned subquery */
320  Assert(!IsA(node, Query));
321 
323  (void *) context);
324 }
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

References Assert, bms_add_member(), context, expression_tree_walker, FirstLowInvalidHeapAttributeNumber, IsA, Var::varattno, Var::varlevelsup, and Var::varno.

Referenced by pull_varattnos().

◆ pull_varnos()

Relids pull_varnos ( PlannerInfo root,
Node node 
)

Definition at line 108 of file var.c.

109 {
111 
112  context.varnos = NULL;
113  context.root = root;
114  context.sublevels_up = 0;
115 
116  /*
117  * Must be prepared to start with a Query or a bare expression tree; if
118  * it's a Query, we don't want to increment sublevels_up.
119  */
122  (void *) &context,
123  0);
124 
125  return context.varnos;
126 }
static bool pull_varnos_walker(Node *node, pull_varnos_context *context)
Definition: var.c:155

References context, pull_varnos_walker(), query_or_expression_tree_walker, and root.

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

◆ pull_varnos_of_level()

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

Definition at line 134 of file var.c.

135 {
137 
138  context.varnos = NULL;
139  context.root = root;
140  context.sublevels_up = levelsup;
141 
142  /*
143  * Must be prepared to start with a Query or a bare expression tree; if
144  * it's a Query, we don't want to increment sublevels_up.
145  */
148  (void *) &context,
149  0);
150 
151  return context.varnos;
152 }

References context, pull_varnos_walker(), query_or_expression_tree_walker, and root.

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

◆ pull_varnos_walker()

static bool pull_varnos_walker ( Node node,
pull_varnos_context context 
)
static

Definition at line 155 of file var.c.

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

References bms_add_member(), bms_add_members(), bms_difference(), bms_equal(), bms_join(), context, CurrentOfExpr::cvarno, expression_tree_walker, IsA, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_var, PlaceHolderVar::phid, PlaceHolderVar::phlevelsup, PlaceHolderVar::phnullingrels, query_tree_walker, Var::varlevelsup, and Var::varno.

Referenced by pull_varnos(), and pull_varnos_of_level().

◆ pull_vars_of_level()

List* pull_vars_of_level ( Node node,
int  levelsup 
)

Definition at line 335 of file var.c.

336 {
338 
339  context.vars = NIL;
340  context.sublevels_up = levelsup;
341 
342  /*
343  * Must be prepared to start with a Query or a bare expression tree; if
344  * it's a Query, we don't want to increment sublevels_up.
345  */
348  (void *) &context,
349  0);
350 
351  return context.vars;
352 }
static bool pull_vars_walker(Node *node, pull_vars_context *context)
Definition: var.c:355

References context, NIL, pull_vars_walker(), and query_or_expression_tree_walker.

Referenced by extract_lateral_references().

◆ pull_vars_walker()

static bool pull_vars_walker ( Node node,
pull_vars_context context 
)
static

Definition at line 355 of file var.c.

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

References context, expression_tree_walker, IsA, lappend(), PlaceHolderVar::phlevelsup, query_tree_walker, and Var::varlevelsup.

Referenced by pull_vars_of_level().