📄 setrefs.c
字号:
rtable, outer_itlist); } else if (IsA(inner_plan, BitmapAnd)) { /* All we need do here is recurse */ BitmapAnd *innerscan = (BitmapAnd *) inner_plan; ListCell *l; foreach(l, innerscan->bitmapplans) { set_inner_join_references((Plan *) lfirst(l), rtable, outer_itlist); } } else if (IsA(inner_plan, BitmapOr)) { /* All we need do here is recurse */ BitmapOr *innerscan = (BitmapOr *) inner_plan; ListCell *l; foreach(l, innerscan->bitmapplans) { set_inner_join_references((Plan *) lfirst(l), rtable, outer_itlist); } } else if (IsA(inner_plan, TidScan)) { TidScan *innerscan = (TidScan *) inner_plan; Index innerrel = innerscan->scan.scanrelid; innerscan->tideval = join_references(innerscan->tideval, rtable, outer_itlist, NULL, innerrel); }}/* * set_uppernode_references * Update the targetlist and quals of an upper-level plan node * to refer to the tuples returned by its lefttree subplan. * * This is used for single-input plan types like Agg, Group, Result. * * 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. */static voidset_uppernode_references(Plan *plan, Index subvarno){ Plan *subplan = plan->lefttree; indexed_tlist *subplan_itlist; List *output_targetlist; ListCell *l; if (subplan != NULL) subplan_itlist = build_tlist_index(subplan->targetlist); else subplan_itlist = build_tlist_index(NIL); output_targetlist = NIL; foreach(l, plan->targetlist) { TargetEntry *tle = (TargetEntry *) lfirst(l); Node *newexpr; newexpr = replace_vars_with_subplan_refs((Node *) tle->expr, subplan_itlist, subvarno); tle = flatCopyTargetEntry(tle); tle->expr = (Expr *) newexpr; output_targetlist = lappend(output_targetlist, tle); } plan->targetlist = output_targetlist; plan->qual = (List *) replace_vars_with_subplan_refs((Node *) plan->qual, subplan_itlist, subvarno); pfree(subplan_itlist);}/* * build_tlist_index --- build an index data structure for a child tlist * * In most cases, subplan tlists will be "flat" tlists with only Vars, * so we try to optimize that case by extracting information about Vars * in advance. Matching a parent tlist to a child is still an O(N^2) * operation, but at least with a much smaller constant factor than plain * tlist_member() searches. * * The result of this function is an indexed_tlist struct to pass to * search_indexed_tlist_for_var() or search_indexed_tlist_for_non_var(). * When done, the indexed_tlist may be freed with a single pfree(). */static indexed_tlist *build_tlist_index(List *tlist){ indexed_tlist *itlist; tlist_vinfo *vinfo; ListCell *l; /* Create data structure with enough slots for all tlist entries */ itlist = (indexed_tlist *) palloc(offsetof(indexed_tlist, vars) + list_length(tlist) * sizeof(tlist_vinfo)); itlist->tlist = tlist; itlist->has_non_vars = false; /* Find the Vars and fill in the index array */ vinfo = itlist->vars; foreach(l, tlist) { TargetEntry *tle = (TargetEntry *) lfirst(l); if (tle->expr && IsA(tle->expr, Var)) { Var *var = (Var *) tle->expr; vinfo->varno = var->varno; vinfo->varattno = var->varattno; vinfo->resno = tle->resno; vinfo++; } else itlist->has_non_vars = true; } itlist->num_vars = (vinfo - itlist->vars); return itlist;}/* * search_indexed_tlist_for_var --- find a Var in an indexed tlist * * If a match is found, return a copy of the given Var with suitably * modified varno/varattno (to wit, newvarno and the resno of the TLE entry). * If no match, return NULL. */static Var *search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist, Index newvarno){ Index varno = var->varno; AttrNumber varattno = var->varattno; tlist_vinfo *vinfo; int i; vinfo = itlist->vars; i = itlist->num_vars; while (i-- > 0) { if (vinfo->varno == varno && vinfo->varattno == varattno) { /* Found a match */ Var *newvar = (Var *) copyObject(var); newvar->varno = newvarno; newvar->varattno = vinfo->resno; return newvar; } vinfo++; } return NULL; /* no match */}/* * search_indexed_tlist_for_non_var --- find a non-Var in an indexed tlist * * If a match is found, return a Var constructed to reference the tlist item. * If no match, return NULL. * * NOTE: it is a waste of time to call this if !itlist->has_non_vars */static Var *search_indexed_tlist_for_non_var(Node *node, indexed_tlist *itlist, Index newvarno){ TargetEntry *tle; tle = tlist_member(node, itlist->tlist); if (tle) { /* Found a matching subplan output expression */ Var *newvar; newvar = makeVar(newvarno, tle->resno, exprType((Node *) tle->expr), exprTypmod((Node *) tle->expr), 0); newvar->varnoold = 0; /* wasn't ever a plain Var */ newvar->varoattno = 0; return newvar; } return NULL; /* no match */}/* * join_references * Creates a new set of targetlist entries or join qual clauses by * changing the varno/varattno values of variables in the clauses * to reference target list values from the outer and inner join * relation target lists. * * This is used in two different scenarios: a normal join clause, where * all the Vars in the clause *must* be replaced by OUTER or INNER references; * and an indexscan being used on the inner side of a nestloop join. * In the latter case we want to replace the outer-relation Vars by OUTER * references, but not touch the Vars of the inner relation. * * For a normal join, acceptable_rel should be zero so that any failure to * match a Var will be reported as an error. For the indexscan case, * pass inner_itlist = NULL and acceptable_rel = the ID of the inner relation. * * 'clauses' is the targetlist or list of join clauses * 'rtable' is the current range table * 'outer_itlist' is the indexed target list of the outer join relation * 'inner_itlist' is the indexed target list of the inner join relation, * or NULL * 'acceptable_rel' is either zero or the rangetable index of a relation * whose Vars may appear in the clause without provoking an error. * * Returns the new expression tree. The original clause structure is * not modified. */static List *join_references(List *clauses, List *rtable, indexed_tlist *outer_itlist, indexed_tlist *inner_itlist, Index acceptable_rel){ join_references_context context; context.rtable = rtable; context.outer_itlist = outer_itlist; context.inner_itlist = inner_itlist; context.acceptable_rel = acceptable_rel; return (List *) join_references_mutator((Node *) clauses, &context);}static Node *join_references_mutator(Node *node, join_references_context *context){ Var *newvar; if (node == NULL) return NULL; if (IsA(node, Var)) { Var *var = (Var *) node; /* First look for the var in the input tlists */ newvar = search_indexed_tlist_for_var(var, context->outer_itlist, OUTER); if (newvar) return (Node *) newvar; if (context->inner_itlist) { newvar = search_indexed_tlist_for_var(var, context->inner_itlist, INNER); if (newvar) return (Node *) newvar; } /* Return the Var unmodified, if it's for acceptable_rel */ if (var->varno == context->acceptable_rel) return (Node *) copyObject(var); /* No referent found for Var */ elog(ERROR, "variable not found in subplan target lists"); } /* Try matching more complex expressions too, if tlists have any */ if (context->outer_itlist->has_non_vars) { newvar = search_indexed_tlist_for_non_var(node, context->outer_itlist, OUTER); if (newvar) return (Node *) newvar; } if (context->inner_itlist && context->inner_itlist->has_non_vars) { newvar = search_indexed_tlist_for_non_var(node, context->inner_itlist, INNER); if (newvar) return (Node *) newvar; } return expression_tree_mutator(node, join_references_mutator, (void *) context);}/* * replace_vars_with_subplan_refs * This routine modifies an expression tree so that all Var nodes * reference target nodes of a subplan. It is used to fix up * target and qual expressions of non-join upper-level plan nodes. * * 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. * * 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 * expressions that were already computed by the subplan. (This is relatively * expensive, so we don't want to try it in the common case where the * subplan tlist is just a flattened list of Vars.) * * 'node': the tree to be fixed (a target item or qual) * 'subplan_itlist': indexed target list for subplan * 'subvarno': varno to be assigned to all Vars * * The resulting tree is a copy of the original in which all Var nodes have * varno = subvarno, varattno = resno of corresponding subplan target. * The original tree is not modified. */static Node *replace_vars_with_subplan_refs(Node *node, indexed_tlist *subplan_itlist, Index subvarno){ replace_vars_with_subplan_refs_context context; context.subplan_itlist = subplan_itlist; context.subvarno = subvarno; return replace_vars_with_subplan_refs_mutator(node, &context);}static Node *replace_vars_with_subplan_refs_mutator(Node *node, replace_vars_with_subplan_refs_context *context){ Var *newvar; if (node == NULL) return NULL; if (IsA(node, Var)) { Var *var = (Var *) node; newvar = search_indexed_tlist_for_var(var, context->subplan_itlist, context->subvarno); if (!newvar) elog(ERROR, "variable not found in subplan target list"); return (Node *) newvar; } /* Try matching more complex expressions too, if tlist has any */ if (context->subplan_itlist->has_non_vars) { newvar = search_indexed_tlist_for_non_var(node, context->subplan_itlist, context->subvarno); if (newvar) return (Node *) newvar; } return expression_tree_mutator(node, replace_vars_with_subplan_refs_mutator, (void *) context);}/***************************************************************************** * OPERATOR REGPROC LOOKUP *****************************************************************************//* * fix_opfuncids * Calculate opfuncid field from opno for each OpExpr node in given tree. * The given tree can be anything expression_tree_walker handles. * * The argument is modified in-place. (This is OK since we'd want the * same change for any node, even if it gets visited more than once due to * shared structure.) */voidfix_opfuncids(Node *node){ /* This tree walk requires no special setup, so away we go... */ fix_opfuncids_walker(node, NULL);}static boolfix_opfuncids_walker(Node *node, void *context){ if (node == NULL) return false; if (IsA(node, OpExpr)) set_opfuncid((OpExpr *) node); else if (IsA(node, DistinctExpr)) set_opfuncid((OpExpr *) node); /* rely on struct equivalence */ else if (IsA(node, ScalarArrayOpExpr)) set_sa_opfuncid((ScalarArrayOpExpr *) node); else if (IsA(node, NullIfExpr)) set_opfuncid((OpExpr *) node); /* rely on struct equivalence */ return expression_tree_walker(node, fix_opfuncids_walker, context);}/* * set_opfuncid * Set the opfuncid (procedure OID) in an OpExpr node, * if it hasn't been set already. * * Because of struct equivalence, this can also be used for * DistinctExpr and NullIfExpr nodes. */voidset_opfuncid(OpExpr *opexpr){ if (opexpr->opfuncid == InvalidOid) opexpr->opfuncid = get_opcode(opexpr->opno);}/* * set_sa_opfuncid * As above, for ScalarArrayOpExpr nodes. */static voidset_sa_opfuncid(ScalarArrayOpExpr *opexpr){ if (opexpr->opfuncid == InvalidOid) opexpr->opfuncid = get_opcode(opexpr->opno);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -