tlist = add_to_flat_tlist(tlist, foreignrel->reltarget.exprs);
tlist = add_to_flat_tlist(tlist,
pull_var_clause((Node *) fpinfo->local_conds,
- PVC_REJECT_AGGREGATES,
PVC_RECURSE_PLACEHOLDERS));
return tlist;
* in check constraints; it would fail to examine the contents of
* subselects.
*/
- varList = pull_var_clause(expr,
- PVC_REJECT_AGGREGATES,
- PVC_REJECT_PLACEHOLDERS);
+ varList = pull_var_clause(expr, 0);
keycount = list_length(varList);
if (keycount > 0)
List *vars;
char *colname;
- vars = pull_var_clause(expr,
- PVC_REJECT_AGGREGATES,
- PVC_REJECT_PLACEHOLDERS);
+ vars = pull_var_clause(expr, 0);
/* eliminate duplicates */
vars = list_union(NIL, vars);
* subselects in WHEN clauses; it would fail to examine the contents
* of subselects.
*/
- varList = pull_var_clause(whenClause,
- PVC_REJECT_AGGREGATES,
- PVC_REJECT_PLACEHOLDERS);
+ varList = pull_var_clause(whenClause, 0);
foreach(lc, varList)
{
Var *var = (Var *) lfirst(lc);
* Examine all Vars used in clause; since it's a restriction clause, all
* such Vars must refer to subselect output columns.
*/
- vars = pull_var_clause(qual,
- PVC_REJECT_AGGREGATES,
- PVC_INCLUDE_PLACEHOLDERS);
+ vars = pull_var_clause(qual, PVC_INCLUDE_PLACEHOLDERS);
foreach(vl, vars)
{
Var *var = (Var *) lfirst(vl);
{
EquivalenceMember *cur_em = (EquivalenceMember *) lfirst(lc);
List *vars = pull_var_clause((Node *) cur_em->em_expr,
- PVC_RECURSE_AGGREGATES,
+ PVC_RECURSE_AGGREGATES |
PVC_INCLUDE_PLACEHOLDERS);
add_vars_to_targetlist(root, vars, ec->ec_relids, false);
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "optimizer/tlist.h"
+#include "optimizer/var.h"
#include "utils/lsyscache.h"
/* local functions */
sortexpr = em->em_expr;
exprvars = pull_var_clause((Node *) sortexpr,
- PVC_INCLUDE_AGGREGATES,
+ PVC_INCLUDE_AGGREGATES |
PVC_INCLUDE_PLACEHOLDERS);
foreach(k, exprvars)
{
build_base_rel_tlists(PlannerInfo *root, List *final_tlist)
{
List *tlist_vars = pull_var_clause((Node *) final_tlist,
- PVC_RECURSE_AGGREGATES,
+ PVC_RECURSE_AGGREGATES |
PVC_INCLUDE_PLACEHOLDERS);
if (tlist_vars != NIL)
if (root->parse->havingQual)
{
List *having_vars = pull_var_clause(root->parse->havingQual,
- PVC_RECURSE_AGGREGATES,
+ PVC_RECURSE_AGGREGATES |
PVC_INCLUDE_PLACEHOLDERS);
if (having_vars != NIL)
if (bms_membership(relids) == BMS_MULTIPLE)
{
List *vars = pull_var_clause(clause,
- PVC_RECURSE_AGGREGATES,
+ PVC_RECURSE_AGGREGATES |
PVC_INCLUDE_PLACEHOLDERS);
add_vars_to_targetlist(root, vars, relids, false);
#include "optimizer/prep.h"
#include "optimizer/subselect.h"
#include "optimizer/tlist.h"
+#include "optimizer/var.h"
#include "parser/analyze.h"
#include "parser/parsetree.h"
#include "parser/parse_agg.h"
* pulled out here, too.
*/
non_group_vars = pull_var_clause((Node *) non_group_cols,
- PVC_RECURSE_AGGREGATES,
+ PVC_RECURSE_AGGREGATES |
PVC_INCLUDE_PLACEHOLDERS);
sub_tlist = add_to_flat_tlist(sub_tlist, non_group_vars);
* at higher levels.
*/
flattenable_vars = pull_var_clause((Node *) flattenable_cols,
- PVC_INCLUDE_AGGREGATES,
+ PVC_INCLUDE_AGGREGATES |
PVC_INCLUDE_PLACEHOLDERS);
new_tlist = add_to_flat_tlist(new_tlist, flattenable_vars);
*
* In most cases, we have to match up individual Vars in the tlist and
* qual expressions with elements of the subplan's tlist (which was
- * generated by flatten_tlist() from these selfsame expressions, so it
- * should have all the required variables). There is an important exception,
- * however: GROUP BY and ORDER BY expressions will have been pushed into the
- * subplan tlist unflattened. If these values are also needed in the output
- * then we want to reference the subplan tlist element rather than recomputing
- * the expression.
+ * generated by flattening these selfsame expressions, so it should have all
+ * the required variables). There is an important exception, however:
+ * depending on where we are in the plan tree, sort/group columns may have
+ * been pushed into the subplan tlist unflattened. If these values are also
+ * needed in the output then we want to reference the subplan tlist element
+ * rather than recomputing the expression.
*/
static void
set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset)
*
* An error is raised if no matching var can be found in the subplan tlist
* --- so this routine should only be applied to nodes whose subplans'
- * targetlists were generated via flatten_tlist() or some such method.
+ * targetlists were generated by flattening the expressions used in the
+ * parent node.
*
* If itlist->has_non_vars is true, then we try to match whole subexpressions
* against elements of the subplan tlist, so that we can avoid recomputing
#include "optimizer/prep.h"
#include "optimizer/subselect.h"
#include "optimizer/tlist.h"
+#include "optimizer/var.h"
#include "parser/parse_relation.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteManip.h"
#include "nodes/makefuncs.h"
#include "optimizer/prep.h"
#include "optimizer/tlist.h"
+#include "optimizer/var.h"
#include "parser/parsetree.h"
#include "parser/parse_coerce.h"
#include "utils/rel.h"
ListCell *l;
vars = pull_var_clause((Node *) parse->returningList,
- PVC_RECURSE_AGGREGATES,
+ PVC_RECURSE_AGGREGATES |
PVC_INCLUDE_PLACEHOLDERS);
foreach(l, vars)
{
* convenient to use.
*/
vars = pull_var_clause(expr,
- PVC_RECURSE_AGGREGATES,
+ PVC_RECURSE_AGGREGATES |
PVC_INCLUDE_PLACEHOLDERS);
foreach(vl, vars)
{
{
PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
List *vars = pull_var_clause((Node *) phinfo->ph_var->phexpr,
- PVC_RECURSE_AGGREGATES,
+ PVC_RECURSE_AGGREGATES |
PVC_INCLUDE_PLACEHOLDERS);
add_vars_to_targetlist(root, vars, phinfo->ph_eval_at, false);
return NULL;
}
-/*
- * flatten_tlist
- * Create a target list that only contains unique variables.
- *
- * Aggrefs and PlaceHolderVars in the input are treated according to
- * aggbehavior and phbehavior, for which see pull_var_clause().
- *
- * 'tlist' is the current target list
- *
- * Returns the "flattened" new target list.
- *
- * The result is entirely new structure sharing no nodes with the original.
- * Copying the Var nodes is probably overkill, but be safe for now.
- */
-List *
-flatten_tlist(List *tlist, PVCAggregateBehavior aggbehavior,
- PVCPlaceHolderBehavior phbehavior)
-{
- List *vlist = pull_var_clause((Node *) tlist,
- aggbehavior,
- phbehavior);
- List *new_tlist;
-
- new_tlist = add_to_flat_tlist(NIL, vlist);
- list_free(vlist);
- return new_tlist;
-}
-
/*
* add_to_flat_tlist
* Add more items to a flattened tlist (if they're not already in it)
typedef struct
{
List *varlist;
- PVCAggregateBehavior aggbehavior;
- PVCPlaceHolderBehavior phbehavior;
+ int flags;
} pull_var_clause_context;
typedef struct
* pull_var_clause
* Recursively pulls all Var nodes from an expression clause.
*
- * Aggrefs are handled according to 'aggbehavior':
- * PVC_REJECT_AGGREGATES throw error if Aggref found
+ * Aggrefs are handled according to these bits in 'flags':
* PVC_INCLUDE_AGGREGATES include Aggrefs in output list
* PVC_RECURSE_AGGREGATES recurse into Aggref arguments
- * Vars within an Aggref's expression are included only in the last case.
+ * neither flag throw error if Aggref found
+ * Vars within an Aggref's expression are included in the result only
+ * when PVC_RECURSE_AGGREGATES is specified.
*
- * PlaceHolderVars are handled according to 'phbehavior':
- * PVC_REJECT_PLACEHOLDERS throw error if PlaceHolderVar found
+ * PlaceHolderVars are handled according to these bits in 'flags':
* PVC_INCLUDE_PLACEHOLDERS include PlaceHolderVars in output list
* PVC_RECURSE_PLACEHOLDERS recurse into PlaceHolderVar arguments
- * Vars within a PHV's expression are included only in the last case.
+ * neither flag throw error if PlaceHolderVar found
+ * Vars within a PHV's expression are included in the result only
+ * when PVC_RECURSE_PLACEHOLDERS is specified.
+ *
+ * GroupingFuncs are treated mostly like Aggrefs, and so do not need
+ * their own flag bits.
*
* CurrentOfExpr nodes are ignored in all cases.
*
* of sublinks to subplans!
*/
List *
-pull_var_clause(Node *node, PVCAggregateBehavior aggbehavior,
- PVCPlaceHolderBehavior phbehavior)
+pull_var_clause(Node *node, int flags)
{
pull_var_clause_context context;
+ /* Assert that caller has not specified inconsistent flags */
+ Assert((flags & (PVC_INCLUDE_AGGREGATES | PVC_RECURSE_AGGREGATES))
+ != (PVC_INCLUDE_AGGREGATES | PVC_RECURSE_AGGREGATES));
+ Assert((flags & (PVC_INCLUDE_PLACEHOLDERS | PVC_RECURSE_PLACEHOLDERS))
+ != (PVC_INCLUDE_PLACEHOLDERS | PVC_RECURSE_PLACEHOLDERS));
+
context.varlist = NIL;
- context.aggbehavior = aggbehavior;
- context.phbehavior = phbehavior;
+ context.flags = flags;
pull_var_clause_walker(node, &context);
return context.varlist;
{
if (((Aggref *) node)->agglevelsup != 0)
elog(ERROR, "Upper-level Aggref found where not expected");
- switch (context->aggbehavior)
+ if (context->flags & PVC_INCLUDE_AGGREGATES)
+ {
+ context->varlist = lappend(context->varlist, node);
+ /* we do NOT descend into the contained expression */
+ return false;
+ }
+ else if (context->flags & PVC_RECURSE_AGGREGATES)
{
- case PVC_REJECT_AGGREGATES:
- elog(ERROR, "Aggref found where not expected");
- break;
- case PVC_INCLUDE_AGGREGATES:
- context->varlist = lappend(context->varlist, node);
- /* we do NOT descend into the contained expression */
- return false;
- case PVC_RECURSE_AGGREGATES:
- /* ignore the aggregate, look at its argument instead */
- break;
+ /* fall through to recurse into the aggregate's arguments */
}
+ else
+ elog(ERROR, "Aggref found where not expected");
}
else if (IsA(node, GroupingFunc))
{
if (((GroupingFunc *) node)->agglevelsup != 0)
elog(ERROR, "Upper-level GROUPING found where not expected");
- switch (context->aggbehavior)
+ if (context->flags & PVC_INCLUDE_AGGREGATES)
{
- case PVC_REJECT_AGGREGATES:
- elog(ERROR, "GROUPING found where not expected");
- break;
- case PVC_INCLUDE_AGGREGATES:
- context->varlist = lappend(context->varlist, node);
- /* we do NOT descend into the contained expression */
- return false;
- case PVC_RECURSE_AGGREGATES:
-
- /*
- * we do NOT descend into the contained expression, even if
- * the caller asked for it, because we never actually evaluate
- * it - the result is driven entirely off the associated GROUP
- * BY clause, so we never need to extract the actual Vars
- * here.
- */
- return false;
+ context->varlist = lappend(context->varlist, node);
+ /* we do NOT descend into the contained expression */
+ return false;
+ }
+ else if (context->flags & PVC_RECURSE_AGGREGATES)
+ {
+ /*
+ * We do NOT descend into the contained expression, even if the
+ * caller asked for it, because we never actually evaluate it -
+ * the result is driven entirely off the associated GROUP BY
+ * clause, so we never need to extract the actual Vars here.
+ */
+ return false;
}
+ else
+ elog(ERROR, "GROUPING found where not expected");
}
else if (IsA(node, PlaceHolderVar))
{
if (((PlaceHolderVar *) node)->phlevelsup != 0)
elog(ERROR, "Upper-level PlaceHolderVar found where not expected");
- switch (context->phbehavior)
+ if (context->flags & PVC_INCLUDE_PLACEHOLDERS)
{
- case PVC_REJECT_PLACEHOLDERS:
- elog(ERROR, "PlaceHolderVar found where not expected");
- break;
- case PVC_INCLUDE_PLACEHOLDERS:
- context->varlist = lappend(context->varlist, node);
- /* we do NOT descend into the contained expression */
- return false;
- case PVC_RECURSE_PLACEHOLDERS:
- /* ignore the placeholder, look at its argument instead */
- break;
+ context->varlist = lappend(context->varlist, node);
+ /* we do NOT descend into the contained expression */
+ return false;
}
+ else if (context->flags & PVC_RECURSE_PLACEHOLDERS)
+ {
+ /* fall through to recurse into the placeholder's expression */
+ }
+ else
+ elog(ERROR, "PlaceHolderVar found where not expected");
}
return expression_tree_walker(node, pull_var_clause_walker,
(void *) context);
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/tlist.h"
+#include "optimizer/var.h"
#include "parser/parse_agg.h"
#include "parser/parse_clause.h"
#include "parser/parse_coerce.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/tlist.h"
+#include "optimizer/var.h"
#include "parser/analyze.h"
#include "parser/parsetree.h"
#include "parser/parser.h"
* down to ignoring the possible addition of nulls to the result set).
*/
varshere = pull_var_clause(groupexpr,
- PVC_RECURSE_AGGREGATES,
+ PVC_RECURSE_AGGREGATES |
PVC_RECURSE_PLACEHOLDERS);
/*
#ifndef TLIST_H
#define TLIST_H
-#include "optimizer/var.h"
+#include "nodes/relation.h"
extern TargetEntry *tlist_member(Node *node, List *targetlist);
extern TargetEntry *tlist_member_ignore_relabel(Node *node, List *targetlist);
-extern List *flatten_tlist(List *tlist, PVCAggregateBehavior aggbehavior,
- PVCPlaceHolderBehavior phbehavior);
extern List *add_to_flat_tlist(List *tlist, List *exprs);
extern List *get_tlist_exprs(List *tlist, bool includeJunk);
#include "nodes/relation.h"
-typedef enum
-{
- PVC_REJECT_AGGREGATES, /* throw error if Aggref found */
- PVC_INCLUDE_AGGREGATES, /* include Aggrefs in output list */
- PVC_RECURSE_AGGREGATES /* recurse into Aggref arguments */
-} PVCAggregateBehavior;
+/* Bits that can be OR'd into the flags argument of pull_var_clause() */
+#define PVC_INCLUDE_AGGREGATES 0x0001 /* include Aggrefs in output list */
+#define PVC_RECURSE_AGGREGATES 0x0002 /* recurse into Aggref arguments */
+#define PVC_INCLUDE_PLACEHOLDERS 0x0004 /* include PlaceHolderVars in
+ * output list */
+#define PVC_RECURSE_PLACEHOLDERS 0x0008 /* recurse into PlaceHolderVar
+ * arguments */
-typedef enum
-{
- PVC_REJECT_PLACEHOLDERS, /* throw error if PlaceHolderVar found */
- PVC_INCLUDE_PLACEHOLDERS, /* include PlaceHolderVars in output list */
- PVC_RECURSE_PLACEHOLDERS /* recurse into PlaceHolderVar arguments */
-} PVCPlaceHolderBehavior;
extern Relids pull_varnos(Node *node);
extern Relids pull_varnos_of_level(Node *node, int levelsup);
extern bool contain_var_clause(Node *node);
extern bool contain_vars_of_level(Node *node, int levelsup);
extern int locate_var_of_level(Node *node, int levelsup);
-extern List *pull_var_clause(Node *node, PVCAggregateBehavior aggbehavior,
- PVCPlaceHolderBehavior phbehavior);
+extern List *pull_var_clause(Node *node, int flags);
extern Node *flatten_join_alias_vars(PlannerInfo *root, Node *node);
#endif /* VAR_H */