⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setrefs.c

📁 PostgreSQL7.4.6 for Linux
💻 C
📖 第 1 页 / 共 2 页
字号:
				if (NumRelids((Node *) inner_plan->qual) > 1)					inner_plan->qual = join_references(inner_plan->qual,													   rtable,													   outer_tlist,													   NIL,													   innerrel,												   tlists_have_non_vars);			}		}		else if (IsA(inner_plan, TidScan))		{			TidScan    *innerscan = (TidScan *) inner_plan;			Index		innerrel = innerscan->scan.scanrelid;			innerscan->tideval = join_references(innerscan->tideval,												 rtable,												 outer_tlist,												 NIL,												 innerrel,												 tlists_have_non_vars);		}	}	else if (IsA(join, MergeJoin))	{		MergeJoin  *mj = (MergeJoin *) join;		mj->mergeclauses = join_references(mj->mergeclauses,										   rtable,										   outer_tlist,										   inner_tlist,										   (Index) 0,										   tlists_have_non_vars);	}	else if (IsA(join, HashJoin))	{		HashJoin   *hj = (HashJoin *) join;		hj->hashclauses = join_references(hj->hashclauses,										  rtable,										  outer_tlist,										  inner_tlist,										  (Index) 0,										  tlists_have_non_vars);	}}/* * 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;	List	   *subplan_targetlist,			   *output_targetlist,			   *l;	bool		tlist_has_non_vars;	if (subplan != NULL)		subplan_targetlist = subplan->targetlist;	else		subplan_targetlist = NIL;	tlist_has_non_vars = targetlist_has_non_vars(subplan_targetlist);	output_targetlist = NIL;	foreach(l, plan->targetlist)	{		TargetEntry *tle = (TargetEntry *) lfirst(l);		Node	   *newexpr;		newexpr = replace_vars_with_subplan_refs((Node *) tle->expr,												 subvarno,												 subplan_targetlist,												 tlist_has_non_vars);		output_targetlist = lappend(output_targetlist,									makeTargetEntry(tle->resdom,													(Expr *) newexpr));	}	plan->targetlist = output_targetlist;	plan->qual = (List *)		replace_vars_with_subplan_refs((Node *) plan->qual,									   subvarno,									   subplan_targetlist,									   tlist_has_non_vars);}/* * targetlist_has_non_vars --- are there any non-Var entries in tlist? * * In most cases, subplan tlists will be "flat" tlists with only Vars. * Checking for this allows us to save comparisons in common cases. */static booltargetlist_has_non_vars(List *tlist){	List	   *l;	foreach(l, tlist)	{		TargetEntry *tle = (TargetEntry *) lfirst(l);		if (tle->expr && !IsA(tle->expr, Var))			return true;	}	return false;}/* * 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_tlist = NIL 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_tlist' is the target list of the outer join relation * 'inner_tlist' is the target list of the inner join relation, or NIL * '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,				List *outer_tlist,				List *inner_tlist,				Index acceptable_rel,				bool tlists_have_non_vars){	join_references_context context;	context.rtable = rtable;	context.outer_tlist = outer_tlist;	context.inner_tlist = inner_tlist;	context.acceptable_rel = acceptable_rel;	context.tlists_have_non_vars = tlists_have_non_vars;	return (List *) join_references_mutator((Node *) clauses, &context);}static Node *join_references_mutator(Node *node,						join_references_context *context){	if (node == NULL)		return NULL;	if (IsA(node, Var))	{		Var		   *var = (Var *) node;		Resdom	   *resdom;		/* First look for the var in the input tlists */		resdom = tlist_member((Node *) var, context->outer_tlist);		if (resdom)		{			Var		   *newvar = (Var *) copyObject(var);			newvar->varno = OUTER;			newvar->varattno = resdom->resno;			return (Node *) newvar;		}		resdom = tlist_member((Node *) var, context->inner_tlist);		if (resdom)		{			Var		   *newvar = (Var *) copyObject(var);			newvar->varno = INNER;			newvar->varattno = resdom->resno;			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->tlists_have_non_vars)	{		Resdom	   *resdom;		resdom = tlist_member(node, context->outer_tlist);		if (resdom)		{			/* Found a matching subplan output expression */			Var		   *newvar;			newvar = makeVar(OUTER,							 resdom->resno,							 resdom->restype,							 resdom->restypmod,							 0);			newvar->varnoold = 0;		/* wasn't ever a plain Var */			newvar->varoattno = 0;			return (Node *) newvar;		}		resdom = tlist_member(node, context->inner_tlist);		if (resdom)		{			/* Found a matching subplan output expression */			Var		   *newvar;			newvar = makeVar(INNER,							 resdom->resno,							 resdom->restype,							 resdom->restypmod,							 0);			newvar->varnoold = 0;		/* wasn't ever a plain Var */			newvar->varoattno = 0;			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 tlist_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) * 'subvarno': varno to be assigned to all Vars * 'subplan_targetlist': target list for subplan * 'tlist_has_non_vars': true if subplan_targetlist contains non-Var exprs * * 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,							   Index subvarno,							   List *subplan_targetlist,							   bool tlist_has_non_vars){	replace_vars_with_subplan_refs_context context;	context.subvarno = subvarno;	context.subplan_targetlist = subplan_targetlist;	context.tlist_has_non_vars = tlist_has_non_vars;	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){	if (node == NULL)		return NULL;	if (IsA(node, Var))	{		Var		   *var = (Var *) node;		Resdom	   *resdom;		Var		   *newvar;		resdom = tlist_member((Node *) var, context->subplan_targetlist);		if (!resdom)			elog(ERROR, "variable not found in subplan target list");		newvar = (Var *) copyObject(var);		newvar->varno = context->subvarno;		newvar->varattno = resdom->resno;		return (Node *) newvar;	}	/* Try matching more complex expressions too, if tlist has any */	if (context->tlist_has_non_vars)	{		Resdom	   *resdom;		resdom = tlist_member(node, context->subplan_targetlist);		if (resdom)		{			/* Found a matching subplan output expression */			Var		   *newvar;			newvar = makeVar(context->subvarno,							 resdom->resno,							 resdom->restype,							 resdom->restypmod,							 0);			newvar->varnoold = 0;		/* wasn't ever a plain Var */			newvar->varoattno = 0;			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 + -