var.c

来自「postgresql8.3.4源码,开源数据库」· C语言 代码 · 共 710 行 · 第 1/2 页

C
710
字号
/*------------------------------------------------------------------------- * * var.c *	  Var node manipulation routines * * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION *	  $PostgreSQL: pgsql/src/backend/optimizer/util/var.c,v 1.73 2008/01/01 19:45:50 momjian Exp $ * *------------------------------------------------------------------------- */#include "postgres.h"#include "access/htup.h"#include "optimizer/clauses.h"#include "optimizer/prep.h"#include "optimizer/var.h"#include "parser/parsetree.h"#include "rewrite/rewriteManip.h"typedef struct{	Relids		varnos;	int			sublevels_up;} pull_varnos_context;typedef struct{	int			varno;	int			varattno;	int			sublevels_up;} contain_var_reference_context;typedef struct{	int			min_varlevel;	int			sublevels_up;} find_minimum_var_level_context;typedef struct{	List	   *varlist;	bool		includeUpperVars;} pull_var_clause_context;typedef struct{	PlannerInfo *root;	int			sublevels_up;} flatten_join_alias_vars_context;static bool pull_varnos_walker(Node *node,				   pull_varnos_context *context);static bool pull_varattnos_walker(Node *node, Bitmapset **varattnos);static bool contain_var_reference_walker(Node *node,							 contain_var_reference_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 contain_vars_above_level_walker(Node *node, int *sublevels_up);static bool find_minimum_var_level_walker(Node *node,							  find_minimum_var_level_context *context);static bool pull_var_clause_walker(Node *node,					   pull_var_clause_context *context);static Node *flatten_join_alias_vars_mutator(Node *node,								flatten_join_alias_vars_context *context);static Relids alias_relid_set(PlannerInfo *root, Relids relids);/* * pull_varnos *		Create a set of all the distinct varnos present in a parsetree. *		Only varnos that reference level-zero rtable entries are considered. * * NOTE: this is used on not-yet-planned expressions.  It may therefore find * bare SubLinks, and if so it needs to recurse into them to look for uplevel * references to the desired rtable level!	But when we find a completed * SubPlan, we only need to look at the parameters passed to the subplan. */Relidspull_varnos(Node *node){	pull_varnos_context context;	context.varnos = NULL;	context.sublevels_up = 0;	/*	 * Must be prepared to start with a Query or a bare expression tree; if	 * it's a Query, we don't want to increment sublevels_up.	 */	query_or_expression_tree_walker(node,									pull_varnos_walker,									(void *) &context,									0);	return context.varnos;}static boolpull_varnos_walker(Node *node, pull_varnos_context *context){	if (node == NULL)		return false;	if (IsA(node, Var))	{		Var		   *var = (Var *) node;		if (var->varlevelsup == context->sublevels_up)			context->varnos = bms_add_member(context->varnos, var->varno);		return false;	}	if (IsA(node, CurrentOfExpr))	{		CurrentOfExpr *cexpr = (CurrentOfExpr *) node;		if (context->sublevels_up == 0)			context->varnos = bms_add_member(context->varnos, cexpr->cvarno);		return false;	}	if (IsA(node, Query))	{		/* Recurse into RTE subquery or not-yet-planned sublink subquery */		bool		result;		context->sublevels_up++;		result = query_tree_walker((Query *) node, pull_varnos_walker,								   (void *) context, 0);		context->sublevels_up--;		return result;	}	return expression_tree_walker(node, pull_varnos_walker,								  (void *) context);}/* * pull_varattnos *		Find all the distinct attribute numbers present in an expression tree, *		and add them to the initial contents of *varattnos. *		Only Vars that reference RTE 1 of rtable level zero are considered. * * Attribute numbers are offset by FirstLowInvalidHeapAttributeNumber so that * we can include system attributes (e.g., OID) in the bitmap representation. * * Currently, this does not support subqueries nor expressions containing * references to multiple tables; not needed since it's only applied to * index expressions and predicates. */voidpull_varattnos(Node *node, Bitmapset **varattnos){	(void) pull_varattnos_walker(node, varattnos);}static boolpull_varattnos_walker(Node *node, Bitmapset **varattnos){	if (node == NULL)		return false;	if (IsA(node, Var))	{		Var		   *var = (Var *) node;		Assert(var->varno == 1);		*varattnos = bms_add_member(*varattnos,						 var->varattno - FirstLowInvalidHeapAttributeNumber);		return false;	}	/* Should not find a subquery or subplan */	Assert(!IsA(node, Query));	Assert(!is_subplan(node));	return expression_tree_walker(node, pull_varattnos_walker,								  (void *) varattnos);}/* *		contain_var_reference * *		Detect whether a parsetree contains any references to a specified *		attribute of a specified rtable entry. * * NOTE: this is used on not-yet-planned expressions.  It may therefore find * bare SubLinks, and if so it needs to recurse into them to look for uplevel * references to the desired rtable entry!	But when we find a completed * SubPlan, we only need to look at the parameters passed to the subplan. */boolcontain_var_reference(Node *node, int varno, int varattno, int levelsup){	contain_var_reference_context context;	context.varno = varno;	context.varattno = varattno;	context.sublevels_up = levelsup;	/*	 * Must be prepared to start with a Query or a bare expression tree; if	 * it's a Query, we don't want to increment sublevels_up.	 */	return query_or_expression_tree_walker(node,										   contain_var_reference_walker,										   (void *) &context,										   0);}static boolcontain_var_reference_walker(Node *node,							 contain_var_reference_context *context){	if (node == NULL)		return false;	if (IsA(node, Var))	{		Var		   *var = (Var *) node;		if (var->varno == context->varno &&			var->varattno == context->varattno &&			var->varlevelsup == context->sublevels_up)			return true;		return false;	}	if (IsA(node, Query))	{		/* Recurse into RTE subquery or not-yet-planned sublink subquery */		bool		result;		context->sublevels_up++;		result = query_tree_walker((Query *) node,								   contain_var_reference_walker,								   (void *) context, 0);		context->sublevels_up--;		return result;	}	return expression_tree_walker(node, contain_var_reference_walker,								  (void *) context);}/* * contain_var_clause *	  Recursively scan a clause to discover whether it contains any Var nodes *	  (of the current query level). * *	  Returns true if any varnode found. * * Does not examine subqueries, therefore must only be used after reduction * of sublinks to subplans! */boolcontain_var_clause(Node *node){	return contain_var_clause_walker(node, NULL);}static boolcontain_var_clause_walker(Node *node, void *context){	if (node == NULL)		return false;	if (IsA(node, Var))	{		if (((Var *) node)->varlevelsup == 0)			return true;		/* abort the tree traversal and return true */		return false;	}	if (IsA(node, CurrentOfExpr))		return true;	return expression_tree_walker(node, contain_var_clause_walker, context);}/* * contain_vars_of_level *	  Recursively scan a clause to discover whether it contains any Var nodes *	  of the specified query level. * *	  Returns true if any such Var found. * * Will recurse into sublinks.	Also, may be invoked directly on a Query. */boolcontain_vars_of_level(Node *node, int levelsup){	int			sublevels_up = levelsup;	return query_or_expression_tree_walker(node,										   contain_vars_of_level_walker,										   (void *) &sublevels_up,										   0);}static boolcontain_vars_of_level_walker(Node *node, int *sublevels_up){	if (node == NULL)		return false;	if (IsA(node, Var))	{		if (((Var *) node)->varlevelsup == *sublevels_up)			return true;		/* abort tree traversal and return true */		return false;	}	if (IsA(node, CurrentOfExpr))	{		if (*sublevels_up == 0)			return true;		return false;	}	if (IsA(node, Query))	{		/* Recurse into subselects */		bool		result;		(*sublevels_up)++;		result = query_tree_walker((Query *) node,								   contain_vars_of_level_walker,								   (void *) sublevels_up,								   0);		(*sublevels_up)--;		return result;	}	return expression_tree_walker(node,								  contain_vars_of_level_walker,								  (void *) sublevels_up);}/* * contain_vars_above_level *	  Recursively scan a clause to discover whether it contains any Var nodes *	  above the specified query level.	(For example, pass zero to detect *	  all nonlocal Vars.) * *	  Returns true if any such Var found. * * Will recurse into sublinks.	Also, may be invoked directly on a Query. */boolcontain_vars_above_level(Node *node, int levelsup){	int			sublevels_up = levelsup;	return query_or_expression_tree_walker(node,										   contain_vars_above_level_walker,										   (void *) &sublevels_up,										   0);}static boolcontain_vars_above_level_walker(Node *node, int *sublevels_up){	if (node == NULL)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?