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

889 {
890  Relids result = NULL;
891  int rtindex;
892 
893  rtindex = -1;
894  while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
895  {
896  RangeTblEntry *rte = rt_fetch(rtindex, query->rtable);
897 
898  if (rte->rtekind == RTE_JOIN)
899  result = bms_join(result, get_relids_for_join(query, rtindex));
900  else
901  result = bms_add_member(result, rtindex);
902  }
903  return result;
904 }
Bitmapset * bms_join(Bitmapset *a, Bitmapset *b)
Definition: bitmapset.c:951
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1045
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:738
@ RTE_JOIN
Definition: parsenodes.h:991
#define rt_fetch(rangetable_index, rangetable)
Definition: parsetree.h:31
Relids get_relids_for_join(Query *query, int joinrelid)
List * rtable
Definition: parsenodes.h:146
RTEKind rtekind
Definition: parsenodes.h:1006

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 }
bool expression_tree_walker(Node *node, bool(*walker)(), void *context)
Definition: nodeFuncs.c:1904
#define IsA(nodeptr, _type_)
Definition: nodes.h:589
Definition: primnodes.h:187

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 }
bool query_or_expression_tree_walker(Node *node, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:3461
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 }
bool query_tree_walker(Query *query, bool(*walker)(), void *context, int flags)
Definition: nodeFuncs.c:2355

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

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

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

749 {
750  if (node == NULL)
751  return NULL;
752  if (IsA(node, Var))
753  {
754  Var *var = (Var *) node;
755  RangeTblEntry *rte;
756  Node *newvar;
757 
758  /* No change unless Var belongs to a JOIN of the target level */
759  if (var->varlevelsup != context->sublevels_up)
760  return node; /* no need to copy, really */
761  rte = rt_fetch(var->varno, context->query->rtable);
762  if (rte->rtekind != RTE_JOIN)
763  return node;
764  if (var->varattno == InvalidAttrNumber)
765  {
766  /* Must expand whole-row reference */
767  RowExpr *rowexpr;
768  List *fields = NIL;
769  List *colnames = NIL;
771  ListCell *lv;
772  ListCell *ln;
773 
774  attnum = 0;
776  forboth(lv, rte->joinaliasvars, ln, rte->eref->colnames)
777  {
778  newvar = (Node *) lfirst(lv);
779  attnum++;
780  /* Ignore dropped columns */
781  if (newvar == NULL)
782  continue;
783  newvar = copyObject(newvar);
784 
785  /*
786  * If we are expanding an alias carried down from an upper
787  * query, must adjust its varlevelsup fields.
788  */
789  if (context->sublevels_up != 0)
790  IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
791  /* Preserve original Var's location, if possible */
792  if (IsA(newvar, Var))
793  ((Var *) newvar)->location = var->location;
794  /* Recurse in case join input is itself a join */
795  /* (also takes care of setting inserted_sublink if needed) */
796  newvar = flatten_join_alias_vars_mutator(newvar, context);
797  fields = lappend(fields, newvar);
798  /* We need the names of non-dropped columns, too */
799  colnames = lappend(colnames, copyObject((Node *) lfirst(ln)));
800  }
801  rowexpr = makeNode(RowExpr);
802  rowexpr->args = fields;
803  rowexpr->row_typeid = var->vartype;
804  rowexpr->row_format = COERCE_IMPLICIT_CAST;
805  rowexpr->colnames = colnames;
806  rowexpr->location = var->location;
807 
808  return (Node *) rowexpr;
809  }
810 
811  /* Expand join alias reference */
812  Assert(var->varattno > 0);
813  newvar = (Node *) list_nth(rte->joinaliasvars, var->varattno - 1);
814  Assert(newvar != NULL);
815  newvar = copyObject(newvar);
816 
817  /*
818  * If we are expanding an alias carried down from an upper query, must
819  * adjust its varlevelsup fields.
820  */
821  if (context->sublevels_up != 0)
822  IncrementVarSublevelsUp(newvar, context->sublevels_up, 0);
823 
824  /* Preserve original Var's location, if possible */
825  if (IsA(newvar, Var))
826  ((Var *) newvar)->location = var->location;
827 
828  /* Recurse in case join input is itself a join */
829  newvar = flatten_join_alias_vars_mutator(newvar, context);
830 
831  /* Detect if we are adding a sublink to query */
832  if (context->possible_sublink && !context->inserted_sublink)
833  context->inserted_sublink = checkExprHasSubLink(newvar);
834 
835  return newvar;
836  }
837  if (IsA(node, PlaceHolderVar))
838  {
839  /* Copy the PlaceHolderVar node with correct mutation of subnodes */
840  PlaceHolderVar *phv;
841 
844  (void *) context);
845  /* now fix PlaceHolderVar's relid sets */
846  if (phv->phlevelsup == context->sublevels_up)
847  {
848  phv->phrels = alias_relid_set(context->query,
849  phv->phrels);
850  }
851  return (Node *) phv;
852  }
853 
854  if (IsA(node, Query))
855  {
856  /* Recurse into RTE subquery or not-yet-planned sublink subquery */
857  Query *newnode;
858  bool save_inserted_sublink;
859 
860  context->sublevels_up++;
861  save_inserted_sublink = context->inserted_sublink;
862  context->inserted_sublink = ((Query *) node)->hasSubLinks;
863  newnode = query_tree_mutator((Query *) node,
865  (void *) context,
867  newnode->hasSubLinks |= context->inserted_sublink;
868  context->inserted_sublink = save_inserted_sublink;
869  context->sublevels_up--;
870  return (Node *) newnode;
871  }
872  /* Already-planned tree not supported */
873  Assert(!IsA(node, SubPlan));
874  /* Shouldn't need to handle these planner auxiliary nodes here */
875  Assert(!IsA(node, SpecialJoinInfo));
876  Assert(!IsA(node, PlaceHolderInfo));
877  Assert(!IsA(node, MinMaxAggInfo));
878 
880  (void *) context);
881 }
int16 AttrNumber
Definition: attnum.h:21
#define InvalidAttrNumber
Definition: attnum.h:23
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
Definition: list.c:336
Query * query_tree_mutator(Query *query, Node *(*mutator)(), void *context, int flags)
Definition: nodeFuncs.c:3300
Node * expression_tree_mutator(Node *node, Node *(*mutator)(), void *context)
Definition: nodeFuncs.c:2598
#define QTW_IGNORE_JOINALIASES
Definition: nodeFuncs.h:23
#define copyObject(obj)
Definition: nodes.h:654
#define makeNode(_type_)
Definition: nodes.h:586
int16 attnum
Definition: pg_attribute.h:83
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
#define NIL
Definition: pg_list.h:65
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:446
static void * list_nth(const List *list, int n)
Definition: pg_list.h:278
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:485
bool checkExprHasSubLink(Node *node)
Definition: rewriteManip.c:277
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
Definition: rewriteManip.c:776
List * colnames
Definition: primnodes.h:43
Definition: pg_list.h:51
Definition: nodes.h:539
Index phlevelsup
Definition: pathnodes.h:2187
Alias * eref
Definition: parsenodes.h:1152
List * joinaliasvars
Definition: parsenodes.h:1081
Oid row_typeid
Definition: primnodes.h:1069
int location
Definition: primnodes.h:1085
List * args
Definition: primnodes.h:1068
CoercionForm row_format
Definition: primnodes.h:1083
List * colnames
Definition: primnodes.h:1084
Oid vartype
Definition: primnodes.h:193
AttrNumber varattno
Definition: primnodes.h:191
int varno
Definition: primnodes.h:189
Index varlevelsup
Definition: primnodes.h:196
int location
Definition: primnodes.h:201
static Relids alias_relid_set(Query *query, Relids relids)
Definition: var.c:888

References alias_relid_set(), RowExpr::args, Assert(), attnum, 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, PlaceHolderVar::phrels, 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: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: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 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  /*
658  * We do NOT descend into the contained expression, even if the
659  * caller asked for it, because we never actually evaluate it -
660  * the result is driven entirely off the associated GROUP BY
661  * clause, so we never need to extract the actual Vars here.
662  */
663  return false;
664  }
665  else
666  elog(ERROR, "GROUPING found where not expected");
667  }
668  else if (IsA(node, WindowFunc))
669  {
670  /* WindowFuncs have no levelsup field to check ... */
671  if (context->flags & PVC_INCLUDE_WINDOWFUNCS)
672  {
673  context->varlist = lappend(context->varlist, node);
674  /* we do NOT descend into the contained expressions */
675  return false;
676  }
677  else if (context->flags & PVC_RECURSE_WINDOWFUNCS)
678  {
679  /* fall through to recurse into the windowfunc's arguments */
680  }
681  else
682  elog(ERROR, "WindowFunc found where not expected");
683  }
684  else if (IsA(node, PlaceHolderVar))
685  {
686  if (((PlaceHolderVar *) node)->phlevelsup != 0)
687  elog(ERROR, "Upper-level PlaceHolderVar found where not expected");
688  if (context->flags & PVC_INCLUDE_PLACEHOLDERS)
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_PLACEHOLDERS)
695  {
696  /* fall through to recurse into the placeholder's expression */
697  }
698  else
699  elog(ERROR, "PlaceHolderVar found where not expected");
700  }
702  (void *) context);
703 }
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218

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 
)

Definition at line 281 of file var.c.

282 {
283  pull_varattnos_context context;
284 
285  context.varattnos = *varattnos;
286  context.varno = varno;
287 
288  (void) pull_varattnos_walker(node, &context);
289 
290  *varattnos = context.varattnos;
291 }
Bitmapset * varattnos
Definition: var.c:41
static bool pull_varattnos_walker(Node *node, pull_varattnos_context *context)
Definition: var.c:294

References pull_varattnos_walker(), pull_varattnos_context::varattnos, and pull_varattnos_context::varno.

Referenced by check_index_only(), check_selective_binary_conversion(), ComputePartitionAttrs(), create_foreignscan_plan(), CreateStatistics(), DefineIndex(), deparseReturningList(), fill_extraUpdatedCols(), has_partition_attrs(), postgresGetForeignRelSize(), RelationGetIndexAttrBitmap(), remove_unused_subquery_outputs(), and statext_is_compatible_clause().

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

98 {
99  pull_varnos_context context;
100 
101  context.varnos = NULL;
102  context.root = root;
103  context.sublevels_up = 0;
104 
105  /*
106  * Must be prepared to start with a Query or a bare expression tree; if
107  * it's a Query, we don't want to increment sublevels_up.
108  */
111  (void *) &context,
112  0);
113 
114  return context.varnos;
115 }
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: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 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(), process_implied_equality(), pullup_replace_vars_callback(), and reduce_outer_joins_pass2().

◆ pull_varnos_of_level()

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

Definition at line 123 of file var.c.

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

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

145 {
146  if (node == NULL)
147  return false;
148  if (IsA(node, Var))
149  {
150  Var *var = (Var *) node;
151 
152  if (var->varlevelsup == context->sublevels_up)
153  context->varnos = bms_add_member(context->varnos, var->varno);
154  return false;
155  }
156  if (IsA(node, CurrentOfExpr))
157  {
158  CurrentOfExpr *cexpr = (CurrentOfExpr *) node;
159 
160  if (context->sublevels_up == 0)
161  context->varnos = bms_add_member(context->varnos, cexpr->cvarno);
162  return false;
163  }
164  if (IsA(node, PlaceHolderVar))
165  {
166  PlaceHolderVar *phv = (PlaceHolderVar *) node;
167 
168  /*
169  * If a PlaceHolderVar is not of the target query level, ignore it,
170  * instead recursing into its expression to see if it contains any
171  * vars that are of the target level.
172  */
173  if (phv->phlevelsup == context->sublevels_up)
174  {
175  /*
176  * Ideally, the PHV's contribution to context->varnos is its
177  * ph_eval_at set. However, this code can be invoked before
178  * that's been computed. If we cannot find a PlaceHolderInfo,
179  * fall back to the conservative assumption that the PHV will be
180  * evaluated at its syntactic level (phv->phrels).
181  *
182  * There is a second hazard: this code is also used to examine
183  * qual clauses during deconstruct_jointree, when we may have a
184  * PlaceHolderInfo but its ph_eval_at value is not yet final, so
185  * that theoretically we could obtain a relid set that's smaller
186  * than we'd see later on. That should never happen though,
187  * because we deconstruct the jointree working upwards. Any outer
188  * join that forces delay of evaluation of a given qual clause
189  * will be processed before we examine that clause here, so the
190  * ph_eval_at value should have been updated to include it.
191  *
192  * Another problem is that a PlaceHolderVar can appear in quals or
193  * tlists that have been translated for use in a child appendrel.
194  * Typically such a PHV is a parameter expression sourced by some
195  * other relation, so that the translation from parent appendrel
196  * to child doesn't change its phrels, and we should still take
197  * ph_eval_at at face value. But in corner cases, the PHV's
198  * original phrels can include the parent appendrel itself, in
199  * which case the translated PHV will have the child appendrel in
200  * phrels, and we must translate ph_eval_at to match.
201  */
202  PlaceHolderInfo *phinfo = NULL;
203 
204  if (phv->phlevelsup == 0)
205  {
206  ListCell *lc;
207 
208  foreach(lc, context->root->placeholder_list)
209  {
210  phinfo = (PlaceHolderInfo *) lfirst(lc);
211  if (phinfo->phid == phv->phid)
212  break;
213  phinfo = NULL;
214  }
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:291
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:795
Relids ph_eval_at
Definition: pathnodes.h:2416
PlaceHolderVar * ph_var
Definition: pathnodes.h:2415
List * placeholder_list
Definition: pathnodes.h:290

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