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 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 (Query *query, Node *node)
 

Function Documentation

◆ alias_relid_set()

static Relids alias_relid_set ( Query query,
Relids  relids 
)
static

Definition at line 880 of file var.c.

881 {
882  Relids result = NULL;
883  int rtindex;
884 
885  rtindex = -1;
886  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
887  {
888  RangeTblEntry *rte = rt_fetch(rtindex, query->rtable);
889 
890  if (rte->rtekind == RTE_JOIN)
891  result = bms_join(result, get_relids_for_join(query, rtindex));
892  else
893  result = bms_add_member(result, rtindex);
894  }
895  return result;
896 }
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:953
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1047
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:739
@ RTE_JOIN
Definition: parsenodes.h:1008
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
Relids get_relids_for_join(Query *query, int joinrelid)
List * rtable
Definition: parsenodes.h:168
RTEKind rtekind
Definition: parsenodes.h:1025

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()

bool contain_var_clause ( Node node)

◆ contain_var_clause_walker()

static bool contain_var_clause_walker ( Node node,
void *  context 
)
static

Definition at line 399 of file var.c.

400 {
401  if (node == NULL)
402  return false;
403  if (IsA(node, Var))
404  {
405  if (((Var *) node)->varlevelsup == 0)
406  return true; /* abort the tree traversal and return true */
407  return false;
408  }
409  if (IsA(node, CurrentOfExpr))
410  return true;
411  if (IsA(node, PlaceHolderVar))
412  {
413  if (((PlaceHolderVar *) node)->phlevelsup == 0)
414  return true; /* abort the tree traversal and return true */
415  /* else fall through to check the contained expr */
416  }
417  return expression_tree_walker(node, contain_var_clause_walker, context);
418 }
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:151
#define IsA(nodeptr, _type_)
Definition: nodes.h:168
Definition: primnodes.h:215

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

432 {
433  int sublevels_up = levelsup;
434 
437  (void *) &sublevels_up,
438  0);
439 }
#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:442

References contain_vars_of_level_walker(), and query_or_expression_tree_walker.

Referenced by apply_child_basequals(), checkExprIsVarFree(), convert_ANY_sublink_to_join(), convert_EXISTS_sublink_to_join(), convert_EXISTS_to_ANY(), pull_up_simple_values(), pullup_replace_vars_callback(), transformAExprIn(), 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 442 of file var.c.

443 {
444  if (node == NULL)
445  return false;
446  if (IsA(node, Var))
447  {
448  if (((Var *) node)->varlevelsup == *sublevels_up)
449  return true; /* abort tree traversal and return true */
450  return false;
451  }
452  if (IsA(node, CurrentOfExpr))
453  {
454  if (*sublevels_up == 0)
455  return true;
456  return false;
457  }
458  if (IsA(node, PlaceHolderVar))
459  {
460  if (((PlaceHolderVar *) node)->phlevelsup == *sublevels_up)
461  return true; /* abort the tree traversal and return true */
462  /* else fall through to check the contained expr */
463  }
464  if (IsA(node, Query))
465  {
466  /* Recurse into subselects */
467  bool result;
468 
469  (*sublevels_up)++;
470  result = query_tree_walker((Query *) node,
472  (void *) sublevels_up,
473  0);
474  (*sublevels_up)--;
475  return result;
476  }
477  return expression_tree_walker(node,
479  (void *) sublevels_up);
480 }
#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 ( Query query,
Node node 
)

Definition at line 726 of file var.c.

727 {
729 
730  context.query = query;
731  context.sublevels_up = 0;
732  /* flag whether join aliases could possibly contain SubLinks */
733  context.possible_sublink = query->hasSubLinks;
734  /* if hasSubLinks is already true, no need to work hard */
735  context.inserted_sublink = query->hasSubLinks;
736 
737  return flatten_join_alias_vars_mutator(node, &context);
738 }
bool hasSubLinks
Definition: parsenodes.h:152
static Node * flatten_join_alias_vars_mutator(Node *node, flatten_join_alias_vars_context *context)
Definition: var.c:741

References flatten_join_alias_vars_mutator(), Query::hasSubLinks, flatten_join_alias_vars_context::inserted_sublink, flatten_join_alias_vars_context::possible_sublink, flatten_join_alias_vars_context::query, and flatten_join_alias_vars_context::sublevels_up.

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

743 {
744  if (node == NULL)
745  return NULL;
746  if (IsA(node, Var))
747  {
748  Var *var = (Var *) node;
749  RangeTblEntry *rte;
750  Node *newvar;
751 
752  /* No change unless Var belongs to a JOIN of the target level */
753  if (var->varlevelsup != context->sublevels_up)
754  return node; /* no need to copy, really */
755  rte = rt_fetch(var->varno, context->query->rtable);
756  if (rte->rtekind != RTE_JOIN)
757  return node;
758  if (var->varattno == InvalidAttrNumber)
759  {
760  /* Must expand whole-row reference */
761  RowExpr *rowexpr;
762  List *fields = NIL;
763  List *colnames = NIL;
764  ListCell *lv;
765  ListCell *ln;
766 
768  forboth(lv, rte->joinaliasvars, ln, rte->eref->colnames)
769  {
770  newvar = (Node *) lfirst(lv);
771  /* Ignore dropped columns */
772  if (newvar == NULL)
773  continue;
774  newvar = copyObject(newvar);
775 
776  /*
777  * If we are expanding an alias carried down from an upper
778  * query, must adjust its varlevelsup fields.
779  */
780  if (context->sublevels_up != 0)
781  IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
782  /* Preserve original Var's location, if possible */
783  if (IsA(newvar, Var))
784  ((Var *) newvar)->location = var->location;
785  /* Recurse in case join input is itself a join */
786  /* (also takes care of setting inserted_sublink if needed) */
787  newvar = flatten_join_alias_vars_mutator(newvar, context);
788  fields = lappend(fields, newvar);
789  /* We need the names of non-dropped columns, too */
790  colnames = lappend(colnames, copyObject((Node *) lfirst(ln)));
791  }
792  rowexpr = makeNode(RowExpr);
793  rowexpr->args = fields;
794  rowexpr->row_typeid = var->vartype;
795  rowexpr->row_format = COERCE_IMPLICIT_CAST;
796  /* vartype will always be RECORDOID, so we always need colnames */
797  rowexpr->colnames = colnames;
798  rowexpr->location = var->location;
799 
800  return (Node *) rowexpr;
801  }
802 
803  /* Expand join alias reference */
804  Assert(var->varattno > 0);
805  newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
806  Assert(newvar != NULL);
807  newvar = copyObject(newvar);
808 
809  /*
810  * If we are expanding an alias carried down from an upper query, must
811  * adjust its varlevelsup fields.
812  */
813  if (context->sublevels_up != 0)
814  IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
815 
816  /* Preserve original Var's location, if possible */
817  if (IsA(newvar, Var))
818  ((Var *) newvar)->location = var->location;
819 
820  /* Recurse in case join input is itself a join */
821  newvar = flatten_join_alias_vars_mutator(newvar, context);
822 
823  /* Detect if we are adding a sublink to query */
824  if (context->possible_sublink && !context->inserted_sublink)
825  context->inserted_sublink = checkExprHasSubLink(newvar);
826 
827  return newvar;
828  }
829  if (IsA(node, PlaceHolderVar))
830  {
831  /* Copy the PlaceHolderVar node with correct mutation of subnodes */
832  PlaceHolderVar *phv;
833 
836  (void *) context);
837  /* now fix PlaceHolderVar's relid sets */
838  if (phv->phlevelsup == context->sublevels_up)
839  {
840  phv->phrels = alias_relid_set(context->query,
841  phv->phrels);
842  }
843  return (Node *) phv;
844  }
845 
846  if (IsA(node, Query))
847  {
848  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
849  Query *newnode;
850  bool save_inserted_sublink;
851 
852  context->sublevels_up++;
853  save_inserted_sublink = context->inserted_sublink;
854  context->inserted_sublink = ((Query *) node)->hasSubLinks;
855  newnode = query_tree_mutator((Query *) node,
857  (void *) context,
859  newnode->hasSubLinks |= context->inserted_sublink;
860  context->inserted_sublink = save_inserted_sublink;
861  context->sublevels_up--;
862  return (Node *) newnode;
863  }
864  /* Already-planned tree not supported */
865  Assert(!IsA(node, SubPlan));
866  /* Shouldn't need to handle these planner auxiliary nodes here */
867  Assert(!IsA(node, SpecialJoinInfo));
868  Assert(!IsA(node, PlaceHolderInfo));
869  Assert(!IsA(node, MinMaxAggInfo));
870 
872  (void *) context);
873 }
#define InvalidAttrNumber
Definition: attnum.h:23
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:338
#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:233
#define makeNode(_type_)
Definition: nodes.h:165
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:467
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:628
bool checkExprHasSubLink(Node *node)
Definition: rewriteManip.c:277
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:809
List * colnames
Definition: primnodes.h:43
Definition: pg_list.h:54
Definition: nodes.h:118
Index phlevelsup
Definition: pathnodes.h:2661
Alias * eref
Definition: parsenodes.h:1184
List * joinaliasvars
Definition: parsenodes.h:1113
Oid row_typeid
Definition: primnodes.h:1291
int location
Definition: primnodes.h:1312
List * args
Definition: primnodes.h:1288
CoercionForm row_format
Definition: primnodes.h:1307
List * colnames
Definition: primnodes.h:1310
Oid vartype
Definition: primnodes.h:230
AttrNumber varattno
Definition: primnodes.h:227
int varno
Definition: primnodes.h:222
Index varlevelsup
Definition: primnodes.h:240
int location
Definition: primnodes.h:253
static Relids alias_relid_set(Query *query, Relids relids)
Definition: var.c:880

References alias_relid_set(), RowExpr::args, Assert(), checkExprHasSubLink(), COERCE_IMPLICIT_CAST, Alias::colnames, RowExpr::colnames, copyObject, RangeTblEntry::eref, expression_tree_mutator, forboth, Query::hasSubLinks, IncrementVarSublevelsUp(), flatten_join_alias_vars_context::inserted_sublink, InvalidAttrNumber, IsA, RangeTblEntry::joinaliasvars, lappend(), lfirst, list_length(), list_nth(), Var::location, RowExpr::location, makeNode, NIL, PlaceHolderVar::phlevelsup, flatten_join_alias_vars_context::possible_sublink, QTW_IGNORE_JOINALIASES, flatten_join_alias_vars_context::query, query_tree_mutator, RowExpr::row_format, RowExpr::row_typeid, rt_fetch, Query::rtable, RTE_JOIN, RangeTblEntry::rtekind, flatten_join_alias_vars_context::sublevels_up, Var::varattno, Var::varlevelsup, Var::varno, and Var::vartype.

Referenced by flatten_join_alias_vars().

◆ locate_var_of_level()

int locate_var_of_level ( Node node,
int  levelsup 
)

Definition at line 499 of file var.c.

500 {
502 
503  context.var_location = -1; /* in case we find nothing */
504  context.sublevels_up = levelsup;
505 
508  (void *) &context,
509  0);
510 
511  return context.var_location;
512 }
static bool locate_var_of_level_walker(Node *node, locate_var_of_level_context *context)
Definition: var.c:515

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

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

References expression_tree_walker, IsA, 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().

◆ pull_var_clause()

List* pull_var_clause ( Node node,
int  flags 
)

Definition at line 597 of file var.c.

598 {
599  pull_var_clause_context context;
600 
601  /* Assert that caller has not specified inconsistent flags */
608 
609  context.varlist = NIL;
610  context.flags = flags;
611 
612  pull_var_clause_walker(node, &context);
613  return context.varlist;
614 }
#define PVC_RECURSE_AGGREGATES
Definition: optimizer.h:184
#define PVC_RECURSE_PLACEHOLDERS
Definition: optimizer.h:188
#define PVC_RECURSE_WINDOWFUNCS
Definition: optimizer.h:186
#define PVC_INCLUDE_WINDOWFUNCS
Definition: optimizer.h:185
#define PVC_INCLUDE_PLACEHOLDERS
Definition: optimizer.h:187
#define PVC_INCLUDE_AGGREGATES
Definition: optimizer.h:183
static bool pull_var_clause_walker(Node *node, pull_var_clause_context *context)
Definition: var.c:617

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(), CreateTriggerFiringOn(), dependency_is_compatible_expression(), 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(), and StoreRelCheck().

◆ pull_var_clause_walker()

static bool pull_var_clause_walker ( Node node,
pull_var_clause_context context 
)
static

Definition at line 617 of file var.c.

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

References elog(), ERROR, expression_tree_walker, pull_var_clause_context::flags, IsA, lappend(), 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().

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

295 {
296  if (node == NULL)
297  return false;
298  if (IsA(node, Var))
299  {
300  Var *var = (Var *) node;
301 
302  if (var->varno == context->varno && var->varlevelsup == 0)
303  context->varattnos =
304  bms_add_member(context->varattnos,
306  return false;
307  }
308 
309  /* Should not find an unplanned subquery */
310  Assert(!IsA(node, Query));
311 
313  (void *) context);
314 }
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

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

Referenced by pull_varattnos().

◆ pull_varnos()

Relids pull_varnos ( PlannerInfo root,
Node node 
)

Definition at line 100 of file var.c.

101 {
102  pull_varnos_context context;
103 
104  context.varnos = NULL;
105  context.root = root;
106  context.sublevels_up = 0;
107 
108  /*
109  * Must be prepared to start with a Query or a bare expression tree; if
110  * it's a Query, we don't want to increment sublevels_up.
111  */
114  (void *) &context,
115  0);
116 
117  return context.varnos;
118 }
PlannerInfo * root
Definition: var.c:35
int sublevels_up
Definition: var.c:36
Relids varnos
Definition: var.c:34
static bool pull_varnos_walker(Node *node, pull_varnos_context *context)
Definition: var.c:147

References pull_varnos_walker(), query_or_expression_tree_walker, pull_varnos_context::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(), 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 pullup_replace_vars_callback().

◆ pull_varnos_of_level()

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

Definition at line 126 of file var.c.

127 {
128  pull_varnos_context context;
129 
130  context.varnos = NULL;
131  context.root = root;
132  context.sublevels_up = levelsup;
133 
134  /*
135  * Must be prepared to start with a Query or a bare expression tree; if
136  * it's a Query, we don't want to increment sublevels_up.
137  */
140  (void *) &context,
141  0);
142 
143  return context.varnos;
144 }

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

Referenced by 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 147 of file var.c.

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

References bms_add_member(), bms_add_members(), bms_difference(), bms_equal(), bms_join(), CurrentOfExpr::cvarno, expression_tree_walker, IsA, PlaceHolderInfo::ph_eval_at, PlaceHolderInfo::ph_var, PlaceHolderVar::phid, PlaceHolderVar::phlevelsup, 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(), and pull_varnos_of_level().

◆ pull_vars_of_level()

List* pull_vars_of_level ( Node node,
int  levelsup 
)

Definition at line 325 of file var.c.

326 {
327  pull_vars_context context;
328 
329  context.vars = NIL;
330  context.sublevels_up = levelsup;
331 
332  /*
333  * Must be prepared to start with a Query or a bare expression tree; if
334  * it's a Query, we don't want to increment sublevels_up.
335  */
338  (void *) &context,
339  0);
340 
341  return context.vars;
342 }
int sublevels_up
Definition: var.c:48
List * vars
Definition: var.c:47
static bool pull_vars_walker(Node *node, pull_vars_context *context)
Definition: var.c:345

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().

◆ pull_vars_walker()

static bool pull_vars_walker ( Node node,
pull_vars_context context 
)
static

Definition at line 345 of file var.c.

346 {
347  if (node == NULL)
348  return false;
349  if (IsA(node, Var))
350  {
351  Var *var = (Var *) node;
352 
353  if (var->varlevelsup == context->sublevels_up)
354  context->vars = lappend(context->vars, var);
355  return false;
356  }
357  if (IsA(node, PlaceHolderVar))
358  {
359  PlaceHolderVar *phv = (PlaceHolderVar *) node;
360 
361  if (phv->phlevelsup == context->sublevels_up)
362  context->vars = lappend(context->vars, phv);
363  /* we don't want to look into the contained expression */
364  return false;
365  }
366  if (IsA(node, Query))
367  {
368  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
369  bool result;
370 
371  context->sublevels_up++;
372  result = query_tree_walker((Query *) node, pull_vars_walker,
373  (void *) context, 0);
374  context->sublevels_up--;
375  return result;
376  }
378  (void *) context);
379 }

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

Referenced by pull_vars_of_level().