clauses.c 68.5 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * clauses.c
4
 *	  routines to manipulate qualification clauses
5
 *
Bruce Momjian's avatar
Bruce Momjian committed
6
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $Header: /cvsroot/pgsql/src/backend/optimizer/util/clauses.c,v 1.109 2002/09/11 14:48:54 tgl Exp $
12 13
 *
 * HISTORY
14 15
 *	  AUTHOR			DATE			MAJOR EVENT
 *	  Andrew Yu			Nov 3, 1994		clause.c and clauses.c combined
16 17 18 19 20 21
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

22
#include "catalog/pg_operator.h"
23 24 25
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "executor/executor.h"
26 27 28
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
29
#include "optimizer/tlist.h"
30
#include "optimizer/var.h"
31
#include "parser/parsetree.h"
32
#include "utils/datum.h"
Bruce Momjian's avatar
Bruce Momjian committed
33
#include "utils/lsyscache.h"
34
#include "utils/syscache.h"
35

36

37 38 39 40 41
/* note that pg_type.h hardwires size of bool as 1 ... duplicate it */
#define MAKEBOOLCONST(val,isnull) \
	((Node *) makeConst(BOOLOID, 1, (Datum) (val), \
						(isnull), true, false, false))

42 43 44 45
typedef struct
{
	Query	   *query;
	List	   *groupClauses;
Bruce Momjian's avatar
Bruce Momjian committed
46
} check_subplans_for_ungrouped_vars_context;
47

48
static bool contain_agg_clause_walker(Node *node, void *context);
49
static bool pull_agg_clause_walker(Node *node, List **listptr);
50
static bool expression_returns_set_walker(Node *node, void *context);
51 52
static bool contain_subplans_walker(Node *node, void *context);
static bool pull_subplans_walker(Node *node, List **listptr);
53
static bool check_subplans_for_ungrouped_vars_walker(Node *node,
Bruce Momjian's avatar
Bruce Momjian committed
54
					 check_subplans_for_ungrouped_vars_context *context);
55 56
static bool contain_mutable_functions_walker(Node *node, void *context);
static bool contain_volatile_functions_walker(Node *node, void *context);
57
static Node *eval_const_expressions_mutator(Node *node, void *context);
58
static Expr *simplify_op_or_func(Expr *expr, List *args);
59

60

61
Expr *
62
make_clause(int type, Node *oper, List *args)
63
{
64 65 66
	Expr	   *expr = makeNode(Expr);

	switch (type)
67
	{
68 69 70 71 72 73
		case AND_EXPR:
		case OR_EXPR:
		case NOT_EXPR:
			expr->typeOid = BOOLOID;
			break;
		case OP_EXPR:
74
		case DISTINCT_EXPR:
75 76 77
			expr->typeOid = ((Oper *) oper)->opresulttype;
			break;
		case FUNC_EXPR:
78
			expr->typeOid = ((Func *) oper)->funcresulttype;
79 80 81 82
			break;
		default:
			elog(ERROR, "make_clause: unsupported type %d", type);
			break;
83
	}
84 85 86 87
	expr->opType = type;
	expr->oper = oper;			/* ignored for AND, OR, NOT */
	expr->args = args;
	return expr;
88 89 90 91
}


/*****************************************************************************
92
 *		OPERATOR clause functions
93 94 95
 *****************************************************************************/


96
/*
97
 * is_opclause
98
 *
99
 * Returns t iff the clause is an operator clause:
100
 *				(op expr expr) or (op expr).
101 102
 *
 * [historical note: is_clause has the exact functionality and is used
103 104
 *		throughout the code. They're renamed to is_opclause for clarity.
 *												- ay 10/94.]
105 106
 */
bool
107
is_opclause(Node *clause)
108
{
109
	return (clause != NULL &&
110
			IsA(clause, Expr) &&
111 112
			((((Expr *) clause)->opType == OP_EXPR) ||
			 ((Expr *) clause)->opType == DISTINCT_EXPR));
113 114
}

115
/*
116
 * make_opclause
117 118 119
 *	  Creates a clause given its operator left operand and right
 *	  operand (if it is non-null).
 *
120
 */
121
Expr *
122
make_opclause(Oper *op, Var *leftop, Var *rightop)
123
{
124
	Expr	   *expr = makeNode(Expr);
125

126
	expr->typeOid = op->opresulttype;
127 128
	expr->opType = OP_EXPR;
	expr->oper = (Node *) op;
129
	if (rightop)
130
		expr->args = makeList2(leftop, rightop);
131
	else
132
		expr->args = makeList1(leftop);
133
	return expr;
134 135
}

136
/*
137
 * get_leftop
138
 *
139
 * Returns the left operand of a clause of the form (op expr expr)
140
 *		or (op expr)
141 142 143 144
 *
 * NB: for historical reasons, the result is declared Var *, even
 * though many callers can cope with results that are not Vars.
 * The result really ought to be declared Expr * or Node *.
145
 */
146
Var *
147
get_leftop(Expr *clause)
148
{
149
	if (clause->args != NULL)
150
		return lfirst(clause->args);
151 152
	else
		return NULL;
153 154
}

155
/*
156
 * get_rightop
157
 *
158
 * Returns the right operand in a clause of the form (op expr expr).
159
 * NB: result will be NULL if applied to a unary op clause.
160
 */
161
Var *
162
get_rightop(Expr *clause)
163
{
164
	if (clause->args != NULL && lnext(clause->args) != NULL)
165
		return lfirst(lnext(clause->args));
166 167
	else
		return NULL;
168 169 170
}

/*****************************************************************************
171
 *		FUNC clause functions
172 173
 *****************************************************************************/

174
/*
175
 * is_funcclause
176
 *
177
 * Returns t iff the clause is a function clause: (func { expr }).
178
 *
179 180
 */
bool
181
is_funcclause(Node *clause)
182
{
183
	return (clause != NULL &&
184
			IsA(clause, Expr) &&
185
			((Expr *) clause)->opType == FUNC_EXPR);
186 187
}

188
/*
189
 * make_funcclause
190
 *
191 192
 * Creates a function clause given the FUNC node and the functional
 * arguments.
193
 *
194
 */
195
Expr *
196
make_funcclause(Func *func, List *funcargs)
197
{
198
	Expr	   *expr = makeNode(Expr);
199

200
	expr->typeOid = func->funcresulttype;
201 202 203 204
	expr->opType = FUNC_EXPR;
	expr->oper = (Node *) func;
	expr->args = funcargs;
	return expr;
205 206 207
}

/*****************************************************************************
208
 *		OR clause functions
209 210
 *****************************************************************************/

211
/*
212
 * or_clause
213
 *
214
 * Returns t iff the clause is an 'or' clause: (OR { expr }).
215
 *
216 217
 */
bool
218
or_clause(Node *clause)
219
{
220 221 222
	return (clause != NULL &&
			IsA(clause, Expr) &&
			((Expr *) clause)->opType == OR_EXPR);
223 224
}

225
/*
226
 * make_orclause
227
 *
228
 * Creates an 'or' clause given a list of its subclauses.
229
 *
230
 */
231
Expr *
232
make_orclause(List *orclauses)
233
{
234
	Expr	   *expr = makeNode(Expr);
235

236
	expr->typeOid = BOOLOID;
237 238 239 240
	expr->opType = OR_EXPR;
	expr->oper = NULL;
	expr->args = orclauses;
	return expr;
241 242 243
}

/*****************************************************************************
244
 *		NOT clause functions
245 246
 *****************************************************************************/

247
/*
248
 * not_clause
249
 *
250
 * Returns t iff this is a 'not' clause: (NOT expr).
251
 *
252 253
 */
bool
254
not_clause(Node *clause)
255
{
256
	return (clause != NULL &&
257
			IsA(clause, Expr) &&
258
			((Expr *) clause)->opType == NOT_EXPR);
259 260
}

261
/*
262
 * make_notclause
263
 *
264
 * Create a 'not' clause given the expression to be negated.
265
 *
266
 */
267
Expr *
268
make_notclause(Expr *notclause)
269
{
270
	Expr	   *expr = makeNode(Expr);
271

272
	expr->typeOid = BOOLOID;
273 274
	expr->opType = NOT_EXPR;
	expr->oper = NULL;
275
	expr->args = makeList1(notclause);
276
	return expr;
277 278
}

279
/*
280
 * get_notclausearg
281
 *
282
 * Retrieve the clause within a 'not' clause
283
 *
284
 */
285
Expr *
286
get_notclausearg(Expr *notclause)
287
{
288
	return lfirst(notclause->args);
289 290 291
}

/*****************************************************************************
292
 *		AND clause functions
293 294 295
 *****************************************************************************/


296
/*
297
 * and_clause
298
 *
299
 * Returns t iff its argument is an 'and' clause: (AND { expr }).
300
 *
301 302
 */
bool
303
and_clause(Node *clause)
304
{
305
	return (clause != NULL &&
306
			IsA(clause, Expr) &&
307
			((Expr *) clause)->opType == AND_EXPR);
308
}
309 310

/*
311
 * make_andclause
312
 *
313 314
 * Create an 'and' clause given its arguments in a list.
 */
315
Expr *
316
make_andclause(List *andclauses)
317
{
318
	Expr	   *expr = makeNode(Expr);
319

320
	expr->typeOid = BOOLOID;
321 322 323 324
	expr->opType = AND_EXPR;
	expr->oper = NULL;
	expr->args = andclauses;
	return expr;
325 326
}

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
/*
 * make_and_qual
 *
 * Variant of make_andclause for ANDing two qual conditions together.
 * Qual conditions have the property that a NULL nodetree is interpreted
 * as 'true'.
 */
Node *
make_and_qual(Node *qual1, Node *qual2)
{
	if (qual1 == NULL)
		return qual2;
	if (qual2 == NULL)
		return qual1;
	return (Node *) make_andclause(makeList2(qual1, qual2));
}

344
/*
345 346 347 348 349 350 351
 * Sometimes (such as in the result of canonicalize_qual or the input of
 * ExecQual), we use lists of expression nodes with implicit AND semantics.
 *
 * These functions convert between an AND-semantics expression list and the
 * ordinary representation of a boolean expression.
 *
 * Note that an empty list is considered equivalent to TRUE.
352 353 354 355 356
 */
Expr *
make_ands_explicit(List *andclauses)
{
	if (andclauses == NIL)
357 358
		return (Expr *) MAKEBOOLCONST(true, false);
	else if (lnext(andclauses) == NIL)
359 360 361 362
		return (Expr *) lfirst(andclauses);
	else
		return make_andclause(andclauses);
}
363

364 365 366
List *
make_ands_implicit(Expr *clause)
{
367 368 369
	/*
	 * NB: because the parser sets the qual field to NULL in a query that
	 * has no WHERE clause, we must consider a NULL input clause as TRUE,
370 371
	 * even though one might more reasonably think it FALSE.  Grumble. If
	 * this causes trouble, consider changing the parser's behavior.
372
	 */
373
	if (clause == NULL)
374
		return NIL;				/* NULL -> NIL list == TRUE */
375 376
	else if (and_clause((Node *) clause))
		return clause->args;
377
	else if (IsA(clause, Const) &&
378
			 !((Const *) clause)->constisnull &&
379
			 DatumGetBool(((Const *) clause)->constvalue))
380
		return NIL;				/* constant TRUE input -> NIL list */
381
	else
382
		return makeList1(clause);
383 384
}

385

386
/*****************************************************************************
387
 *		Aggregate-function clause manipulation
388 389
 *****************************************************************************/

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
/*
 * contain_agg_clause
 *	  Recursively search for Aggref nodes within a clause.
 *
 *	  Returns true if any aggregate found.
 */
bool
contain_agg_clause(Node *clause)
{
	return contain_agg_clause_walker(clause, NULL);
}

static bool
contain_agg_clause_walker(Node *node, void *context)
{
	if (node == NULL)
		return false;
	if (IsA(node, Aggref))
408 409
		return true;			/* abort the tree traversal and return
								 * true */
410 411 412
	return expression_tree_walker(node, contain_agg_clause_walker, context);
}

413 414 415 416 417 418
/*
 * pull_agg_clause
 *	  Recursively pulls all Aggref nodes from an expression tree.
 *
 *	  Returns list of Aggref nodes found.  Note the nodes themselves are not
 *	  copied, only referenced.
419 420
 *
 *	  Note: this also checks for nested aggregates, which are an error.
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
 */
List *
pull_agg_clause(Node *clause)
{
	List	   *result = NIL;

	pull_agg_clause_walker(clause, &result);
	return result;
}

static bool
pull_agg_clause_walker(Node *node, List **listptr)
{
	if (node == NULL)
		return false;
	if (IsA(node, Aggref))
	{
		*listptr = lappend(*listptr, node);
439

440 441 442
		/*
		 * Complain if the aggregate's argument contains any aggregates;
		 * nested agg functions are semantically nonsensical.
443
		 */
444 445
		if (contain_agg_clause(((Aggref *) node)->target))
			elog(ERROR, "Aggregate function calls may not be nested");
446

447 448 449 450
		/*
		 * Having checked that, we need not recurse into the argument.
		 */
		return false;
451 452 453 454 455
	}
	return expression_tree_walker(node, pull_agg_clause_walker,
								  (void *) listptr);
}

456

457
/*****************************************************************************
458
 *		Support for expressions returning sets
459 460 461
 *****************************************************************************/

/*
462
 * expression_returns_set
463
 *	  Test whether an expression returns a set result.
464
 *
465 466 467
 * Because we use expression_tree_walker(), this can also be applied to
 * whole targetlists; it'll produce TRUE if any one of the tlist items
 * returns a set.
468 469
 */
bool
470
expression_returns_set(Node *clause)
471
{
472
	return expression_returns_set_walker(clause, NULL);
473 474 475
}

static bool
476
expression_returns_set_walker(Node *node, void *context)
477 478 479
{
	if (node == NULL)
		return false;
480 481
	if (IsA(node, Expr))
	{
Bruce Momjian's avatar
Bruce Momjian committed
482
		Expr	   *expr = (Expr *) node;
483 484 485 486

		switch (expr->opType)
		{
			case OP_EXPR:
487
			case DISTINCT_EXPR:
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
				if (((Oper *) expr->oper)->opretset)
					return true;
				/* else fall through to check args */
				break;
			case FUNC_EXPR:
				if (((Func *) expr->oper)->funcretset)
					return true;
				/* else fall through to check args */
				break;
			case OR_EXPR:
			case AND_EXPR:
			case NOT_EXPR:
				/* Booleans can't return a set, so no need to recurse */
				return false;
			case SUBPLAN_EXPR:
				/* Subplans can't presently return sets either */
				return false;
		}
	}
	/* Avoid recursion for some other cases that can't return a set */
	if (IsA(node, Aggref))
		return false;
	if (IsA(node, SubLink))
		return false;
	return expression_tree_walker(node, expression_returns_set_walker,
								  context);
514 515
}

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
/*****************************************************************************
 *		Subplan clause manipulation
 *****************************************************************************/

/*
 * contain_subplans
 *	  Recursively search for subplan nodes within a clause.
 *
 * If we see a SubLink node, we will return TRUE.  This is only possible if
 * the expression tree hasn't yet been transformed by subselect.c.  We do not
 * know whether the node will produce a true subplan or just an initplan,
 * but we make the conservative assumption that it will be a subplan.
 *
 * Returns true if any subplan found.
 */
bool
contain_subplans(Node *clause)
{
	return contain_subplans_walker(clause, NULL);
}

static bool
contain_subplans_walker(Node *node, void *context)
{
	if (node == NULL)
		return false;
	if (is_subplan(node) || IsA(node, SubLink))
543 544
		return true;			/* abort the tree traversal and return
								 * true */
545 546 547 548 549 550 551
	return expression_tree_walker(node, contain_subplans_walker, context);
}

/*
 * pull_subplans
 *	  Recursively pulls all subplans from an expression tree.
 *
552
 *	  Returns list of subplan nodes found.	Note the nodes themselves are not
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
 *	  copied, only referenced.
 */
List *
pull_subplans(Node *clause)
{
	List	   *result = NIL;

	pull_subplans_walker(clause, &result);
	return result;
}

static bool
pull_subplans_walker(Node *node, List **listptr)
{
	if (node == NULL)
		return false;
	if (is_subplan(node))
	{
		*listptr = lappend(*listptr, ((Expr *) node)->oper);
		/* fall through to check args to subplan */
	}
	return expression_tree_walker(node, pull_subplans_walker,
								  (void *) listptr);
}

578 579 580
/*
 * check_subplans_for_ungrouped_vars
 *		Check for subplans that are being passed ungrouped variables as
581
 *		parameters; generate an error message if any are found.
582 583
 *
 * In most contexts, ungrouped variables will be detected by the parser (see
584 585
 * parse_agg.c, check_ungrouped_columns()). But that routine currently does
 * not check subplans, because the necessary info is not computed until the
586 587
 * planner runs.  So we do it here, after we have processed sublinks into
 * subplans.  This ought to be cleaned up someday.
588
 *
589 590
 * A deficiency in this scheme is that any outer reference var must be
 * grouped by itself; we don't recognize groupable expressions within
591
 * subselects.	For example, consider
592 593 594 595 596
 *		SELECT
 *			(SELECT x FROM bar where y = (foo.a + foo.b))
 *		FROM foo
 *		GROUP BY a + b;
 * This query will be rejected although it could be allowed.
597
 */
598
void
599
check_subplans_for_ungrouped_vars(Query *query)
600
{
601
	check_subplans_for_ungrouped_vars_context context;
602
	List	   *gl;
603 604

	context.query = query;
605

606
	/*
607 608
	 * Build a list of the acceptable GROUP BY expressions for use in the
	 * walker (to avoid repeated scans of the targetlist within the
609 610 611 612 613 614 615 616 617 618 619 620
	 * recursive routine).
	 */
	context.groupClauses = NIL;
	foreach(gl, query->groupClause)
	{
		GroupClause *grpcl = lfirst(gl);
		Node	   *expr;

		expr = get_sortgroupclause_expr(grpcl, query->targetList);
		context.groupClauses = lcons(expr, context.groupClauses);
	}

621
	/*
622 623 624
	 * Recursively scan the targetlist and the HAVING clause. WHERE and
	 * JOIN/ON conditions are not examined, since they are evaluated
	 * before grouping.
625
	 */
626 627 628 629 630 631
	check_subplans_for_ungrouped_vars_walker((Node *) query->targetList,
											 &context);
	check_subplans_for_ungrouped_vars_walker(query->havingQual,
											 &context);

	freeList(context.groupClauses);
632 633 634 635
}

static bool
check_subplans_for_ungrouped_vars_walker(Node *node,
Bruce Momjian's avatar
Bruce Momjian committed
636
					  check_subplans_for_ungrouped_vars_context *context)
637
{
638 639
	List	   *gl;

640 641
	if (node == NULL)
		return false;
642
	if (IsA(node, Const) ||IsA(node, Param))
643
		return false;			/* constants are always acceptable */
644

645 646
	/*
	 * If we find an aggregate function, do not recurse into its
647
	 * arguments.  Subplans invoked within aggregate calls are allowed to
648 649
	 * receive ungrouped variables.  (This test and the next one should
	 * match the logic in parse_agg.c's check_ungrouped_columns().)
650 651 652 653
	 */
	if (IsA(node, Aggref))
		return false;

654 655 656 657 658 659 660 661 662 663 664
	/*
	 * Check to see if subexpression as a whole matches any GROUP BY item.
	 * We need to do this at every recursion level so that we recognize
	 * GROUPed-BY expressions before reaching variables within them.
	 */
	foreach(gl, context->groupClauses)
	{
		if (equal(node, lfirst(gl)))
			return false;		/* acceptable, do not descend more */
	}

665
	/*
666 667
	 * We can ignore Vars other than in subplan args lists, since the
	 * parser already checked 'em.
668 669 670 671 672 673 674
	 */
	if (is_subplan(node))
	{
		/*
		 * The args list of the subplan node represents attributes from
		 * outside passed into the sublink.
		 */
675
		List	   *t;
676 677 678 679

		foreach(t, ((Expr *) node)->args)
		{
			Node	   *thisarg = lfirst(t);
680 681
			Var		   *var;
			bool		contained_in_group_clause;
682

683 684 685
			/*
			 * We do not care about args that are not local variables;
			 * params or outer-level vars are not our responsibility to
686 687
			 * check.  (The outer-level query passing them to us needs to
			 * worry, instead.)
688
			 */
689
			if (!IsA(thisarg, Var))
690 691 692 693 694 695 696 697 698
				continue;
			var = (Var *) thisarg;
			if (var->varlevelsup > 0)
				continue;

			/*
			 * Else, see if it is a grouping column.
			 */
			contained_in_group_clause = false;
699
			foreach(gl, context->groupClauses)
700
			{
701
				if (equal(thisarg, lfirst(gl)))
702 703 704 705 706 707 708
				{
					contained_in_group_clause = true;
					break;
				}
			}

			if (!contained_in_group_clause)
709 710
			{
				/* Found an ungrouped argument.  Complain. */
711 712
				RangeTblEntry *rte;
				char	   *attname;
713 714

				Assert(var->varno > 0 &&
715
					 (int) var->varno <= length(context->query->rtable));
716
				rte = rt_fetch(var->varno, context->query->rtable);
717
				attname = get_rte_attribute_name(rte, var->varattno);
718
				elog(ERROR, "Sub-SELECT uses un-GROUPed attribute %s.%s from outer query",
719
					 rte->eref->aliasname, attname);
720
			}
721 722 723
		}
	}
	return expression_tree_walker(node,
724
								check_subplans_for_ungrouped_vars_walker,
725 726 727 728
								  (void *) context);
}


729
/*****************************************************************************
730
 *		Check clauses for mutable functions
731 732
 *****************************************************************************/

733
/*
734 735
 * contain_mutable_functions
 *	  Recursively search for mutable functions within a clause.
736
 *
737 738
 * Returns true if any mutable function (or operator implemented by a
 * mutable function) is found.	This test is needed so that we don't
739 740 741 742
 * mistakenly think that something like "WHERE random() < 0.5" can be treated
 * as a constant qualification.
 *
 * XXX we do not examine sublinks/subplans to see if they contain uses of
743
 * mutable functions.  It's not real clear if that is correct or not...
744 745
 */
bool
746
contain_mutable_functions(Node *clause)
747
{
748
	return contain_mutable_functions_walker(clause, NULL);
749 750 751
}

static bool
752
contain_mutable_functions_walker(Node *node, void *context)
753 754 755 756 757 758 759 760 761 762
{
	if (node == NULL)
		return false;
	if (IsA(node, Expr))
	{
		Expr	   *expr = (Expr *) node;

		switch (expr->opType)
		{
			case OP_EXPR:
763
			case DISTINCT_EXPR:
764
				if (op_volatile(((Oper *) expr->oper)->opno) != PROVOLATILE_IMMUTABLE)
765 766 767
					return true;
				break;
			case FUNC_EXPR:
768
				if (func_volatile(((Func *) expr->oper)->funcid) != PROVOLATILE_IMMUTABLE)
769 770 771 772 773 774
					return true;
				break;
			default:
				break;
		}
	}
775 776 777 778 779 780 781 782 783 784 785 786 787 788
	return expression_tree_walker(node, contain_mutable_functions_walker,
								  context);
}


/*****************************************************************************
 *		Check clauses for volatile functions
 *****************************************************************************/

/*
 * contain_volatile_functions
 *	  Recursively search for volatile functions within a clause.
 *
 * Returns true if any volatile function (or operator implemented by a
Bruce Momjian's avatar
Bruce Momjian committed
789
 * volatile function) is found. This test prevents invalid conversions
790 791 792
 * of volatile expressions into indexscan quals.
 *
 * XXX we do not examine sublinks/subplans to see if they contain uses of
Bruce Momjian's avatar
Bruce Momjian committed
793
 * volatile functions.	It's not real clear if that is correct or not...
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
 */
bool
contain_volatile_functions(Node *clause)
{
	return contain_volatile_functions_walker(clause, NULL);
}

static bool
contain_volatile_functions_walker(Node *node, void *context)
{
	if (node == NULL)
		return false;
	if (IsA(node, Expr))
	{
		Expr	   *expr = (Expr *) node;

		switch (expr->opType)
		{
			case OP_EXPR:
813
			case DISTINCT_EXPR:
814 815 816 817 818 819 820 821 822 823 824 825
				if (op_volatile(((Oper *) expr->oper)->opno) == PROVOLATILE_VOLATILE)
					return true;
				break;
			case FUNC_EXPR:
				if (func_volatile(((Func *) expr->oper)->funcid) == PROVOLATILE_VOLATILE)
					return true;
				break;
			default:
				break;
		}
	}
	return expression_tree_walker(node, contain_volatile_functions_walker,
826 827 828 829 830 831 832
								  context);
}


/*****************************************************************************
 *		Check for "pseudo-constant" clauses
 *****************************************************************************/
833 834

/*
835 836
 * is_pseudo_constant_clause
 *	  Detect whether a clause is "constant", ie, it contains no variables
837
 *	  of the current query level and no uses of volatile functions.
838
 *	  Such a clause is not necessarily a true constant: it can still contain
839
 *	  Params and outer-level Vars, not to mention functions whose results
Bruce Momjian's avatar
Bruce Momjian committed
840
 *	  may vary from one statement to the next.	However, the clause's value
841 842 843
 *	  will be constant over any one scan of the current query, so it can be
 *	  used as an indexscan key or (if a top-level qual) can be pushed up to
 *	  become a gating qual.
844 845 846 847 848 849
 */
bool
is_pseudo_constant_clause(Node *clause)
{
	/*
	 * We could implement this check in one recursive scan.  But since the
850
	 * check for volatile functions is both moderately expensive and
851
	 * unlikely to fail, it seems better to look for Vars first and only
852
	 * check for volatile functions if we find no Vars.
853 854
	 */
	if (!contain_var_clause(clause) &&
855
		!contain_volatile_functions(clause))
856 857 858 859
		return true;
	return false;
}

860
/*
861
 * pull_constant_clauses
862 863
 *		Scan through a list of qualifications and separate "constant" quals
 *		from those that are not.
864
 *
865 866
 * Returns a list of the pseudo-constant clauses in constantQual and the
 * remaining quals as the return value.
867 868
 */
List *
869
pull_constant_clauses(List *quals, List **constantQual)
870 871
{
	List	   *constqual = NIL;
872 873
	List	   *restqual = NIL;
	List	   *q;
874 875 876

	foreach(q, quals)
	{
877
		Node	   *qual = (Node *) lfirst(q);
878

879
		if (is_pseudo_constant_clause(qual))
880
			constqual = lappend(constqual, qual);
881 882
		else
			restqual = lappend(restqual, qual);
883 884
	}
	*constantQual = constqual;
885
	return restqual;
886 887 888
}


889 890 891 892 893 894 895 896
/*****************************************************************************
 *		Tests on clauses of queries
 *
 * Possibly this code should go someplace else, since this isn't quite the
 * same meaning of "clause" as is used elsewhere in this module.  But I can't
 * think of a better place for it...
 *****************************************************************************/

897 898 899 900 901 902 903 904 905 906
/*
 * Test whether a sort/group reference value appears in the given list of
 * SortClause (or GroupClause) nodes.
 *
 * Because GroupClause is typedef'd as SortClause, either kind of
 * node list can be passed without casting.
 */
static bool
sortgroupref_is_present(Index sortgroupref, List *clauselist)
{
907
	List	   *clause;
908 909 910 911 912 913 914 915 916 917 918

	foreach(clause, clauselist)
	{
		SortClause *scl = (SortClause *) lfirst(clause);

		if (scl->tleSortGroupRef == sortgroupref)
			return true;
	}
	return false;
}

919 920
/*
 * Test whether a query uses DISTINCT ON, ie, has a distinct-list that is
921
 * not the same as the set of output columns.
922 923 924 925
 */
bool
has_distinct_on_clause(Query *query)
{
926
	List	   *targetList;
927 928 929 930

	/* Is there a DISTINCT clause at all? */
	if (query->distinctClause == NIL)
		return false;
931

932
	/*
933 934
	 * If the DISTINCT list contains all the nonjunk targetlist items, and
	 * nothing else (ie, no junk tlist items), then it's a simple
935 936
	 * DISTINCT, else it's DISTINCT ON.  We do not require the lists to be
	 * in the same order (since the parser may have adjusted the DISTINCT
937 938 939 940
	 * clause ordering to agree with ORDER BY).  Furthermore, a
	 * non-DISTINCT junk tlist item that is in the sortClause is also
	 * evidence of DISTINCT ON, since we don't allow ORDER BY on junk
	 * tlist items when plain DISTINCT is used.
941 942 943
	 *
	 * This code assumes that the DISTINCT list is valid, ie, all its entries
	 * match some entry of the tlist.
944 945 946 947
	 */
	foreach(targetList, query->targetList)
	{
		TargetEntry *tle = (TargetEntry *) lfirst(targetList);
948
		Index		ressortgroupref = tle->resdom->ressortgroupref;
949 950

		if (ressortgroupref == 0)
951 952 953
		{
			if (tle->resdom->resjunk)
				continue;		/* we can ignore unsorted junk cols */
954
			return true;		/* definitely not in DISTINCT list */
955 956
		}
		if (sortgroupref_is_present(ressortgroupref, query->distinctClause))
957
		{
958 959 960 961 962 963 964 965 966 967 968 969
			if (tle->resdom->resjunk)
				return true;	/* junk TLE in DISTINCT means DISTINCT ON */
			/* else this TLE is okay, keep looking */
		}
		else
		{
			/* This TLE is not in DISTINCT list */
			if (!tle->resdom->resjunk)
				return true;	/* non-junk, non-DISTINCT, so DISTINCT ON */
			if (sortgroupref_is_present(ressortgroupref, query->sortClause))
				return true;	/* sorted, non-distinct junk */
			/* unsorted junk is okay, keep looking */
970 971 972 973 974 975 976
		}
	}
	/* It's a simple DISTINCT */
	return false;
}


977 978 979 980 981 982
/*****************************************************************************
 *																			 *
 *		General clause-manipulating routines								 *
 *																			 *
 *****************************************************************************/

983
/*
984
 * clause_get_relids_vars
985 986 987 988 989 990 991 992
 *	  Retrieves distinct relids and vars appearing within a clause.
 *
 * '*relids' is set to an integer list of all distinct "varno"s appearing
 *		in Vars within the clause.
 * '*vars' is set to a list of all distinct Vars appearing within the clause.
 *		Var nodes are considered distinct if they have different varno
 *		or varattno values.  If there are several occurrences of the same
 *		varno/varattno, you get a randomly chosen one...
993 994 995
 *
 * Note that upper-level vars are ignored, since they normally will
 * become Params with respect to this query level.
996 997
 */
void
998
clause_get_relids_vars(Node *clause, Relids *relids, List **vars)
999
{
1000
	List	   *clvars = pull_var_clause(clause, false);
1001
	List	   *varno_list = NIL;
1002
	List	   *var_list = NIL;
1003
	List	   *i;
1004

1005
	foreach(i, clvars)
1006
	{
1007 1008
		Var		   *var = (Var *) lfirst(i);
		List	   *vi;
1009 1010

		if (!intMember(var->varno, varno_list))
1011
			varno_list = lconsi(var->varno, varno_list);
1012 1013
		foreach(vi, var_list)
		{
1014
			Var		   *in_list = (Var *) lfirst(vi);
1015

1016
			if (in_list->varno == var->varno &&
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
1017
				in_list->varattno == var->varattno)
1018 1019 1020
				break;
		}
		if (vi == NIL)
1021
			var_list = lcons(var, var_list);
1022
	}
1023
	freeList(clvars);
1024

1025 1026
	*relids = varno_list;
	*vars = var_list;
1027 1028
}

1029
/*
1030 1031
 * NumRelids
 *		(formerly clause_relids)
1032
 *
1033 1034 1035
 * Returns the number of different relations referenced in 'clause'.
 */
int
1036
NumRelids(Node *clause)
1037
{
1038 1039
	List	   *varno_list = pull_varnos(clause);
	int			result = length(varno_list);
1040

1041
	freeList(varno_list);
1042
	return result;
1043 1044
}

1045 1046
/*--------------------
 * CommuteClause: commute a binary operator clause
1047 1048
 *
 * XXX the clause is destructively modified!
1049 1050
 *--------------------
 */
1051
void
1052
CommuteClause(Expr *clause)
1053
{
1054 1055
	Oid			opoid;
	HeapTuple	optup;
1056 1057 1058
	Form_pg_operator commuTup;
	Oper	   *commu;
	Node	   *temp;
1059

1060 1061 1062
	if (!is_opclause((Node *) clause) ||
		length(clause->args) != 2)
		elog(ERROR, "CommuteClause: applied to non-binary-operator clause");
1063

1064
	opoid = ((Oper *) clause->oper)->opno;
1065

1066 1067 1068 1069 1070
	optup = SearchSysCache(OPEROID,
						   ObjectIdGetDatum(get_commutator(opoid)),
						   0, 0, 0);
	if (!HeapTupleIsValid(optup))
		elog(ERROR, "CommuteClause: no commutator for operator %u", opoid);
1071

1072
	commuTup = (Form_pg_operator) GETSTRUCT(optup);
1073

1074
	commu = makeOper(HeapTupleGetOid(optup),
1075
					 commuTup->oprcode,
1076 1077
					 commuTup->oprresult,
					 ((Oper *) clause->oper)->opretset);
1078

1079 1080
	ReleaseSysCache(optup);

1081
	/*
1082
	 * re-form the clause in-place!
1083
	 */
1084 1085 1086 1087
	clause->oper = (Node *) commu;
	temp = lfirst(clause->args);
	lfirst(clause->args) = lsecond(clause->args);
	lsecond(clause->args) = temp;
1088
}
1089 1090


1091 1092 1093 1094 1095 1096 1097
/*--------------------
 * eval_const_expressions
 *
 * Reduce any recognizably constant subexpressions of the given
 * expression tree, for example "2 + 2" => "4".  More interestingly,
 * we can reduce certain boolean expressions even when they contain
 * non-constant subexpressions: "x OR true" => "true" no matter what
1098
 * the subexpression x is.	(XXX We assume that no such subexpression
1099 1100 1101 1102 1103 1104
 * will have important side-effects, which is not necessarily a good
 * assumption in the presence of user-defined functions; do we need a
 * pg_proc flag that prevents discarding the execution of a function?)
 *
 * We do understand that certain functions may deliver non-constant
 * results even with constant inputs, "nextval()" being the classic
1105
 * example.  Functions that are not marked "immutable" in pg_proc
1106
 * will not be pre-evaluated here, although we will reduce their
1107
 * arguments as far as possible.
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
 *
 * We assume that the tree has already been type-checked and contains
 * only operators and functions that are reasonable to try to execute.
 *
 * This routine should be invoked before converting sublinks to subplans
 * (subselect.c's SS_process_sublinks()).  The converted form contains
 * bogus "Const" nodes that are actually placeholders where the executor
 * will insert values from the inner plan, and obviously we mustn't try
 * to reduce the expression as though these were really constants.
 * As a safeguard, if we happen to find an already-converted SubPlan node,
 * we will return it unchanged rather than recursing into it.
 *--------------------
 */
Node *
eval_const_expressions(Node *node)
{
	/* no context or special setup needed, so away we go... */
	return eval_const_expressions_mutator(node, NULL);
}

static Node *
1129
eval_const_expressions_mutator(Node *node, void *context)
1130 1131 1132 1133 1134 1135 1136 1137
{
	if (node == NULL)
		return NULL;
	if (IsA(node, Expr))
	{
		Expr	   *expr = (Expr *) node;
		List	   *args;
		Const	   *const_input;
1138
		Expr	   *newexpr;
1139 1140 1141

		/*
		 * Reduce constants in the Expr's arguments.  We know args is
1142 1143
		 * either NIL or a List node, so we can call
		 * expression_tree_mutator directly rather than recursing to self.
1144 1145
		 */
		args = (List *) expression_tree_mutator((Node *) expr->args,
Bruce Momjian's avatar
Bruce Momjian committed
1146
										  eval_const_expressions_mutator,
1147 1148 1149 1150 1151 1152
												(void *) context);

		switch (expr->opType)
		{
			case OP_EXPR:
			case FUNC_EXPR:
1153

1154
				/*
1155 1156
				 * Code for op/func case is pretty bulky, so split it out
				 * as a separate function.
1157
				 */
1158 1159 1160
				newexpr = simplify_op_or_func(expr, args);
				if (newexpr)	/* successfully simplified it */
					return (Node *) newexpr;
1161

1162
				/*
1163 1164
				 * else fall out to build new Expr node with simplified
				 * args
1165
				 */
1166
				break;
1167 1168
			case DISTINCT_EXPR:
				{
Bruce Momjian's avatar
Bruce Momjian committed
1169 1170 1171 1172
					List	   *arg;
					bool		has_null_input = false;
					bool		all_null_input = true;
					bool		has_nonconst_input = false;
1173 1174

					/*
Bruce Momjian's avatar
Bruce Momjian committed
1175 1176
					 * Check for constant inputs and especially
					 * constant-NULL inputs.
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
					 */
					Assert(length(args) == 2);
					foreach(arg, args)
					{
						if (IsA(lfirst(arg), Const))
						{
							has_null_input |= ((Const *) lfirst(arg))->constisnull;
							all_null_input &= ((Const *) lfirst(arg))->constisnull;
						}
						else
							has_nonconst_input = true;
					}
					/* all nulls? then not distinct */
					if (all_null_input)
						return MAKEBOOLCONST(false, false);

					/* one null? then distinct */
					if (has_null_input)
						return MAKEBOOLCONST(true, false);

					/* all constants? then optimize this out */
					if (!has_nonconst_input)
					{
						Oid			result_typeid;
						int16		resultTypLen;
						bool		resultTypByVal;
						ExprContext *econtext;
						Datum		const_val;
						bool		const_is_null;

						Oper	   *oper = (Oper *) expr->oper;
Bruce Momjian's avatar
Bruce Momjian committed
1208 1209 1210

						replace_opid(oper);		/* OK to scribble on input
												 * to this extent */
1211 1212 1213
						result_typeid = oper->opresulttype;

						/*
Bruce Momjian's avatar
Bruce Momjian committed
1214 1215
						 * OK, looks like we can simplify this
						 * operator/function.
1216
						 *
Bruce Momjian's avatar
Bruce Momjian committed
1217 1218 1219
						 * We use the executor's routine ExecEvalExpr() to
						 * avoid duplication of code and ensure we get the
						 * same result as the executor would get.
1220
						 *
Bruce Momjian's avatar
Bruce Momjian committed
1221 1222 1223 1224
						 * Build a new Expr node containing the
						 * already-simplified arguments. The only other
						 * setup needed here is the replace_opid() that we
						 * already did for the OP_EXPR case.
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
						 */
						newexpr = makeNode(Expr);
						newexpr->typeOid = expr->typeOid;
						newexpr->opType = expr->opType;
						newexpr->oper = expr->oper;
						newexpr->args = args;

						/* Get info needed about result datatype */
						get_typlenbyval(result_typeid, &resultTypLen, &resultTypByVal);

						/*
Bruce Momjian's avatar
Bruce Momjian committed
1236 1237 1238 1239 1240 1241
						 * It is OK to pass a dummy econtext because none
						 * of the ExecEvalExpr() code used in this
						 * situation will use econtext.  That might seem
						 * fortuitous, but it's not so unreasonable --- a
						 * constant expression does not depend on context,
						 * by definition, n'est ce pas?
1242 1243 1244 1245
						 */
						econtext = MakeExprContext(NULL, CurrentMemoryContext);

						const_val = ExecEvalExprSwitchContext((Node *) newexpr, econtext,
Bruce Momjian's avatar
Bruce Momjian committed
1246
												   &const_is_null, NULL);
1247

Bruce Momjian's avatar
Bruce Momjian committed
1248 1249 1250 1251
						/*
						 * Must copy result out of sub-context used by
						 * expression eval
						 */
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
						if (!const_is_null)
							const_val = datumCopy(const_val, resultTypByVal, resultTypLen);

						FreeExprContext(econtext);
						pfree(newexpr);

						/*
						 * Make the constant result node.
						 */
						return (Node *) makeConst(result_typeid, resultTypLen,
Bruce Momjian's avatar
Bruce Momjian committed
1262 1263
												const_val, const_is_null,
										   resultTypByVal, false, false);
1264 1265 1266
					}
					break;
				}
1267
			case OR_EXPR:
1268
				{
1269

1270 1271 1272 1273 1274 1275 1276 1277 1278
					/*----------
					 * OR arguments are handled as follows:
					 *	non constant: keep
					 *	FALSE: drop (does not affect result)
					 *	TRUE: force result to TRUE
					 *	NULL: keep only one
					 * We keep one NULL input because ExecEvalOr returns NULL
					 * when no input is TRUE and at least one is NULL.
					 *----------
1279 1280 1281 1282 1283 1284 1285
					 */
					List	   *newargs = NIL;
					List	   *arg;
					bool		haveNull = false;
					bool		forceTrue = false;

					foreach(arg, args)
1286
					{
1287 1288 1289 1290 1291 1292 1293 1294
						if (!IsA(lfirst(arg), Const))
						{
							newargs = lappend(newargs, lfirst(arg));
							continue;
						}
						const_input = (Const *) lfirst(arg);
						if (const_input->constisnull)
							haveNull = true;
1295
						else if (DatumGetBool(const_input->constvalue))
1296 1297
							forceTrue = true;
						/* otherwise, we can drop the constant-false input */
1298
					}
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318

					/*
					 * We could return TRUE before falling out of the
					 * loop, but this coding method will be easier to
					 * adapt if we ever add a notion of non-removable
					 * functions. We'd need to check all the inputs for
					 * non-removability.
					 */
					if (forceTrue)
						return MAKEBOOLCONST(true, false);
					if (haveNull)
						newargs = lappend(newargs, MAKEBOOLCONST(false, true));
					/* If all the inputs are FALSE, result is FALSE */
					if (newargs == NIL)
						return MAKEBOOLCONST(false, false);
					/* If only one nonconst-or-NULL input, it's the result */
					if (lnext(newargs) == NIL)
						return (Node *) lfirst(newargs);
					/* Else we still need an OR node */
					return (Node *) make_orclause(newargs);
1319 1320 1321
				}
			case AND_EXPR:
				{
1322

1323 1324 1325 1326 1327 1328 1329
					/*----------
					 * AND arguments are handled as follows:
					 *	non constant: keep
					 *	TRUE: drop (does not affect result)
					 *	FALSE: force result to FALSE
					 *	NULL: keep only one
					 * We keep one NULL input because ExecEvalAnd returns NULL
1330
					 * when no input is FALSE and at least one is NULL.
1331
					 *----------
1332 1333 1334 1335 1336 1337 1338
					 */
					List	   *newargs = NIL;
					List	   *arg;
					bool		haveNull = false;
					bool		forceFalse = false;

					foreach(arg, args)
1339
					{
1340 1341 1342 1343 1344 1345 1346 1347
						if (!IsA(lfirst(arg), Const))
						{
							newargs = lappend(newargs, lfirst(arg));
							continue;
						}
						const_input = (Const *) lfirst(arg);
						if (const_input->constisnull)
							haveNull = true;
1348
						else if (!DatumGetBool(const_input->constvalue))
1349 1350
							forceFalse = true;
						/* otherwise, we can drop the constant-true input */
1351
					}
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371

					/*
					 * We could return FALSE before falling out of the
					 * loop, but this coding method will be easier to
					 * adapt if we ever add a notion of non-removable
					 * functions. We'd need to check all the inputs for
					 * non-removability.
					 */
					if (forceFalse)
						return MAKEBOOLCONST(false, false);
					if (haveNull)
						newargs = lappend(newargs, MAKEBOOLCONST(false, true));
					/* If all the inputs are TRUE, result is TRUE */
					if (newargs == NIL)
						return MAKEBOOLCONST(true, false);
					/* If only one nonconst-or-NULL input, it's the result */
					if (lnext(newargs) == NIL)
						return (Node *) lfirst(newargs);
					/* Else we still need an AND node */
					return (Node *) make_andclause(newargs);
1372 1373 1374
				}
			case NOT_EXPR:
				Assert(length(args) == 1);
1375
				if (!IsA(lfirst(args), Const))
1376 1377 1378 1379 1380 1381
					break;
				const_input = (Const *) lfirst(args);
				/* NOT NULL => NULL */
				if (const_input->constisnull)
					return MAKEBOOLCONST(false, true);
				/* otherwise pretty easy */
1382
				return MAKEBOOLCONST(!DatumGetBool(const_input->constvalue),
1383 1384
									 false);
			case SUBPLAN_EXPR:
1385

1386 1387
				/*
				 * Safety measure per notes at head of this routine:
1388
				 * return a SubPlan unchanged.	Too late to do anything
1389
				 * with it.  The arglist simplification above was wasted
1390 1391
				 * work (the list probably only contains Var nodes
				 * anyway).
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
				 */
				return (Node *) expr;
			default:
				elog(ERROR, "eval_const_expressions: unexpected opType %d",
					 (int) expr->opType);
				break;
		}

		/*
		 * If we break out of the above switch on opType, then the
1402 1403 1404 1405 1406
		 * expression cannot be simplified any further, so build and
		 * return a replacement Expr node using the possibly-simplified
		 * arguments and the original oper node. Can't use make_clause()
		 * here because we want to be sure the typeOid field is
		 * preserved...
1407 1408
		 */
		newexpr = makeNode(Expr);
1409 1410 1411 1412 1413
		newexpr->typeOid = expr->typeOid;
		newexpr->opType = expr->opType;
		newexpr->oper = expr->oper;
		newexpr->args = args;
		return (Node *) newexpr;
1414
	}
1415 1416 1417 1418
	if (IsA(node, RelabelType))
	{
		/*
		 * If we can simplify the input to a constant, then we don't need
1419
		 * the RelabelType node anymore: just change the type field of the
1420
		 * Const node.	Otherwise, must copy the RelabelType node.
1421 1422 1423 1424 1425
		 */
		RelabelType *relabel = (RelabelType *) node;
		Node	   *arg;

		arg = eval_const_expressions_mutator(relabel->arg, context);
1426 1427

		/*
1428 1429
		 * If we find stacked RelabelTypes (eg, from foo :: int :: oid) we
		 * can discard all but the top one.
1430 1431 1432 1433
		 */
		while (arg && IsA(arg, RelabelType))
			arg = ((RelabelType *) arg)->arg;

1434 1435
		if (arg && IsA(arg, Const))
		{
1436
			Const	   *con = (Const *) arg;
1437 1438

			con->consttype = relabel->resulttype;
1439

1440 1441 1442
			/*
			 * relabel's resulttypmod is discarded, which is OK for now;
			 * if the type actually needs a runtime length coercion then
1443 1444
			 * there should be a function call to do it just above this
			 * node.
1445
			 */
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
			return (Node *) con;
		}
		else
		{
			RelabelType *newrelabel = makeNode(RelabelType);

			newrelabel->arg = arg;
			newrelabel->resulttype = relabel->resulttype;
			newrelabel->resulttypmod = relabel->resulttypmod;
			return (Node *) newrelabel;
		}
	}
1458 1459
	if (IsA(node, CaseExpr))
	{
1460

1461
		/*----------
1462
		 * CASE expressions can be simplified if there are constant
1463 1464 1465 1466 1467 1468 1469 1470
		 * condition clauses:
		 *		FALSE (or NULL): drop the alternative
		 *		TRUE: drop all remaining alternatives
		 * If the first non-FALSE alternative is a constant TRUE, we can
		 * simplify the entire CASE to that alternative's expression.
		 * If there are no non-FALSE alternatives, we simplify the entire
		 * CASE to the default result (ELSE result).
		 *----------
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
		 */
		CaseExpr   *caseexpr = (CaseExpr *) node;
		CaseExpr   *newcase;
		List	   *newargs = NIL;
		Node	   *defresult;
		Const	   *const_input;
		List	   *arg;

		foreach(arg, caseexpr->args)
		{
			/* Simplify this alternative's condition and result */
			CaseWhen   *casewhen = (CaseWhen *)
1483 1484 1485 1486
			expression_tree_mutator((Node *) lfirst(arg),
									eval_const_expressions_mutator,
									(void *) context);

1487 1488
			Assert(IsA(casewhen, CaseWhen));
			if (casewhen->expr == NULL ||
1489
				!IsA(casewhen->expr, Const))
1490 1491 1492 1493 1494 1495
			{
				newargs = lappend(newargs, casewhen);
				continue;
			}
			const_input = (Const *) casewhen->expr;
			if (const_input->constisnull ||
1496
				!DatumGetBool(const_input->constvalue))
1497
				continue;		/* drop alternative with FALSE condition */
1498

1499
			/*
1500
			 * Found a TRUE condition.	If it's the first (un-dropped)
1501 1502 1503 1504
			 * alternative, the CASE reduces to just this alternative.
			 */
			if (newargs == NIL)
				return casewhen->result;
1505

1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
			/*
			 * Otherwise, add it to the list, and drop all the rest.
			 */
			newargs = lappend(newargs, casewhen);
			break;
		}

		/* Simplify the default result */
		defresult = eval_const_expressions_mutator(caseexpr->defresult,
												   context);
1516 1517 1518 1519 1520

		/*
		 * If no non-FALSE alternatives, CASE reduces to the default
		 * result
		 */
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
		if (newargs == NIL)
			return defresult;
		/* Otherwise we need a new CASE node */
		newcase = makeNode(CaseExpr);
		newcase->casetype = caseexpr->casetype;
		newcase->arg = NULL;
		newcase->args = newargs;
		newcase->defresult = defresult;
		return (Node *) newcase;
	}
1531

1532 1533
	/*
	 * For any node type not handled above, we recurse using
1534 1535 1536 1537
	 * expression_tree_mutator, which will copy the node unchanged but try
	 * to simplify its arguments (if any) using this routine. For example:
	 * we cannot eliminate an ArrayRef node, but we might be able to
	 * simplify constant expressions in its subscripts.
1538 1539 1540 1541 1542
	 */
	return expression_tree_mutator(node, eval_const_expressions_mutator,
								   (void *) context);
}

1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
/*
 * Subroutine for eval_const_expressions: try to evaluate an op or func
 *
 * Inputs are the op or func Expr node, and the pre-simplified argument list.
 * Returns a simplified expression if successful, or NULL if cannot
 * simplify the op/func.
 *
 * XXX Possible future improvement: if the func is SQL-language, and its
 * definition is simply "SELECT expression", we could parse and substitute
 * the expression here.  This would avoid much runtime overhead, and perhaps
 * expose opportunities for constant-folding within the expression even if
 * not all the func's input args are constants.  It'd be appropriate to do
1555
 * that here, not in the parser, since we wouldn't want it to happen until
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
 * after rule substitution/rewriting.
 */
static Expr *
simplify_op_or_func(Expr *expr, List *args)
{
	List	   *arg;
	Oid			funcid;
	Oid			result_typeid;
	HeapTuple	func_tuple;
	Form_pg_proc funcform;
1566
	char		provolatile;
1567 1568 1569
	bool		proisstrict;
	bool		proretset;
	int16		resultTypLen;
1570
	bool		resultTypByVal;
1571
	Expr	   *newexpr;
1572
	ExprContext *econtext;
1573
	Datum		const_val;
1574 1575
	bool		has_nonconst_input = false;
	bool		has_null_input = false;
1576 1577 1578
	bool		const_is_null;

	/*
1579
	 * Check for constant inputs and especially constant-NULL inputs.
1580 1581 1582
	 */
	foreach(arg, args)
	{
1583 1584 1585 1586
		if (IsA(lfirst(arg), Const))
			has_null_input |= ((Const *) lfirst(arg))->constisnull;
		else
			has_nonconst_input = true;
1587
	}
1588

1589 1590 1591 1592
	/*
	 * If the function is strict and has a constant-NULL input, it will
	 * never be called at all, so we can replace the call by a NULL
	 * constant even if there are other inputs that aren't constant.
1593 1594
	 * Otherwise, we can only simplify if all inputs are constants. We can
	 * skip the function lookup if neither case applies.
1595 1596 1597 1598
	 */
	if (has_nonconst_input && !has_null_input)
		return NULL;

1599
	/*
1600
	 * Get the function procedure's OID and look to see whether it is
1601
	 * marked immutable.
1602
	 *
1603 1604 1605
	 * Note we take the result type from the Oper or Func node, not the
	 * pg_proc tuple; probably necessary for binary-compatibility cases.
	 *
1606 1607 1608
	 */
	if (expr->opType == OP_EXPR)
	{
1609
		Oper	   *oper = (Oper *) expr->oper;
1610 1611 1612 1613 1614 1615 1616

		replace_opid(oper);		/* OK to scribble on input to this extent */
		funcid = oper->opid;
		result_typeid = oper->opresulttype;
	}
	else
	{
1617
		Func	   *func = (Func *) expr->oper;
1618 1619

		funcid = func->funcid;
1620
		result_typeid = func->funcresulttype;
1621
	}
1622

1623
	/*
1624
	 * we could use func_volatile() here, but we need several fields out
1625
	 * of the func tuple, so might as well just look it up once.
1626
	 */
1627 1628 1629
	func_tuple = SearchSysCache(PROCOID,
								ObjectIdGetDatum(funcid),
								0, 0, 0);
1630 1631 1632
	if (!HeapTupleIsValid(func_tuple))
		elog(ERROR, "Function OID %u does not exist", funcid);
	funcform = (Form_pg_proc) GETSTRUCT(func_tuple);
1633
	provolatile = funcform->provolatile;
1634 1635 1636 1637
	proisstrict = funcform->proisstrict;
	proretset = funcform->proretset;
	ReleaseSysCache(func_tuple);

1638
	if (provolatile != PROVOLATILE_IMMUTABLE)
1639
		return NULL;
1640

1641 1642 1643
	/*
	 * Also check to make sure it doesn't return a set.
	 */
1644
	if (proretset)
1645
		return NULL;
1646

1647 1648 1649 1650
	/*
	 * Now that we know if the function is strict, we can finish the
	 * checks for simplifiable inputs that we started above.
	 */
1651
	if (proisstrict && has_null_input)
1652 1653 1654 1655 1656
	{
		/*
		 * It's strict and has NULL input, so must produce NULL output.
		 * Return a NULL constant of the right type.
		 */
1657
		return (Expr *) makeNullConst(result_typeid);
1658 1659 1660
	}

	/*
1661 1662 1663
	 * Otherwise, can simplify only if all inputs are constants. (For a
	 * non-strict function, constant NULL inputs are treated the same as
	 * constant non-NULL inputs.)
1664 1665 1666 1667
	 */
	if (has_nonconst_input)
		return NULL;

1668 1669 1670 1671 1672 1673
	/*
	 * OK, looks like we can simplify this operator/function.
	 *
	 * We use the executor's routine ExecEvalExpr() to avoid duplication of
	 * code and ensure we get the same result as the executor would get.
	 *
1674 1675
	 * Build a new Expr node containing the already-simplified arguments. The
	 * only other setup needed here is the replace_opid() that we already
1676 1677 1678 1679 1680 1681 1682
	 * did for the OP_EXPR case.
	 */
	newexpr = makeNode(Expr);
	newexpr->typeOid = expr->typeOid;
	newexpr->opType = expr->opType;
	newexpr->oper = expr->oper;
	newexpr->args = args;
1683

1684
	/* Get info needed about result datatype */
1685
	get_typlenbyval(result_typeid, &resultTypLen, &resultTypByVal);
1686

1687
	/*
1688 1689 1690 1691
	 * It is OK to pass a dummy econtext because none of the
	 * ExecEvalExpr() code used in this situation will use econtext.  That
	 * might seem fortuitous, but it's not so unreasonable --- a constant
	 * expression does not depend on context, by definition, n'est ce pas?
1692
	 */
1693 1694 1695
	econtext = MakeExprContext(NULL, CurrentMemoryContext);

	const_val = ExecEvalExprSwitchContext((Node *) newexpr, econtext,
1696
										  &const_is_null, NULL);
1697 1698

	/* Must copy result out of sub-context used by expression eval */
1699 1700
	if (!const_is_null)
		const_val = datumCopy(const_val, resultTypByVal, resultTypLen);
1701 1702

	FreeExprContext(econtext);
1703
	pfree(newexpr);
1704

1705 1706 1707
	/*
	 * Make the constant result node.
	 */
1708
	return (Expr *) makeConst(result_typeid, resultTypLen,
1709
							  const_val, const_is_null,
1710
							  resultTypByVal, false, false);
1711 1712 1713
}


1714
/*
1715 1716 1717 1718 1719 1720 1721 1722 1723
 * Standard expression-tree walking support
 *
 * We used to have near-duplicate code in many different routines that
 * understood how to recurse through an expression node tree.  That was
 * a pain to maintain, and we frequently had bugs due to some particular
 * routine neglecting to support a particular node type.  In most cases,
 * these routines only actually care about certain node types, and don't
 * care about other types except insofar as they have to recurse through
 * non-primitive node types.  Therefore, we now provide generic tree-walking
1724 1725 1726 1727 1728
 * logic to consolidate the redundant "boilerplate" code.  There are
 * two versions: expression_tree_walker() and expression_tree_mutator().
 */

/*--------------------
1729 1730
 * expression_tree_walker() is designed to support routines that traverse
 * a tree in a read-only fashion (although it will also work for routines
1731 1732
 * that modify nodes in-place but never add/delete/replace nodes).
 * A walker routine should look like this:
1733 1734 1735 1736 1737
 *
 * bool my_walker (Node *node, my_struct *context)
 * {
 *		if (node == NULL)
 *			return false;
Bruce Momjian's avatar
Bruce Momjian committed
1738
 *		// check for nodes that special work is required for, eg:
1739 1740 1741 1742 1743 1744 1745 1746
 *		if (IsA(node, Var))
 *		{
 *			... do special actions for Var nodes
 *		}
 *		else if (IsA(node, ...))
 *		{
 *			... do special actions for other node types
 *		}
Bruce Momjian's avatar
Bruce Momjian committed
1747
 *		// for any node type not specially processed, do:
1748 1749 1750 1751
 *		return expression_tree_walker(node, my_walker, (void *) context);
 * }
 *
 * The "context" argument points to a struct that holds whatever context
1752 1753
 * information the walker routine needs --- it can be used to return data
 * gathered by the walker, too.  This argument is not touched by
1754 1755 1756 1757 1758 1759 1760
 * expression_tree_walker, but it is passed down to recursive sub-invocations
 * of my_walker.  The tree walk is started from a setup routine that
 * fills in the appropriate context struct, calls my_walker with the top-level
 * node of the tree, and then examines the results.
 *
 * The walker routine should return "false" to continue the tree walk, or
 * "true" to abort the walk and immediately return "true" to the top-level
1761 1762
 * caller.	This can be used to short-circuit the traversal if the walker
 * has found what it came for.	"false" is returned to the top-level caller
1763
 * iff no invocation of the walker returned "true".
1764 1765 1766 1767 1768 1769 1770
 *
 * The node types handled by expression_tree_walker include all those
 * normally found in target lists and qualifier clauses during the planning
 * stage.  In particular, it handles List nodes since a cnf-ified qual clause
 * will have List structure at the top level, and it handles TargetEntry nodes
 * so that a scan of a target list can be handled without additional code.
 * (But only the "expr" part of a TargetEntry is examined, unless the walker
1771
 * chooses to process TargetEntry nodes specially.)  Also, RangeTblRef,
1772 1773
 * FromExpr, JoinExpr, and SetOperationStmt nodes are handled, so that query
 * jointrees and setOperation trees can be processed without additional code.
1774 1775 1776 1777 1778 1779 1780
 *
 * expression_tree_walker will handle SubLink and SubPlan nodes by recursing
 * normally into the "lefthand" arguments (which belong to the outer plan).
 * It will also call the walker on the sub-Query node; however, when
 * expression_tree_walker itself is called on a Query node, it does nothing
 * and returns "false".  The net effect is that unless the walker does
 * something special at a Query node, sub-selects will not be visited
1781
 * during an expression tree walk.	This is exactly the behavior wanted
1782 1783
 * in many cases --- and for those walkers that do want to recurse into
 * sub-selects, special behavior is typically needed anyway at the entry
1784
 * to a sub-select (such as incrementing a depth counter).	A walker that
1785 1786 1787 1788 1789
 * wants to examine sub-selects should include code along the lines of:
 *
 *		if (IsA(node, Query))
 *		{
 *			adjust context for subquery;
1790
 *			result = query_tree_walker((Query *) node, my_walker, context,
1791
 *									   0); // to visit rtable items too
1792 1793 1794
 *			restore context if needed;
 *			return result;
 *		}
1795
 *
1796 1797
 * query_tree_walker is a convenience routine (see below) that calls the
 * walker on all the expression subtrees of the given Query node.
1798
 *
1799 1800 1801 1802
 * NOTE: currently, because make_subplan() clears the subselect link in
 * a SubLink node, it is not actually possible to recurse into subselects
 * of an already-planned expression tree.  This is OK for current uses,
 * but ought to be cleaned up when we redesign querytree processing.
1803 1804 1805 1806
 *--------------------
 */

bool
1807 1808 1809
expression_tree_walker(Node *node,
					   bool (*walker) (),
					   void *context)
1810 1811 1812 1813
{
	List	   *temp;

	/*
1814 1815
	 * The walker has already visited the current node, and so we need
	 * only recurse into any sub-nodes it has.
1816
	 *
1817 1818
	 * We assume that the walker is not interested in List nodes per se, so
	 * when we expect a List we just recurse directly to self without
1819 1820 1821 1822 1823 1824 1825 1826 1827
	 * bothering to call the walker.
	 */
	if (node == NULL)
		return false;
	switch (nodeTag(node))
	{
		case T_Const:
		case T_Var:
		case T_Param:
1828
		case T_RangeTblRef:
1829 1830
			/* primitive node types with no subnodes */
			break;
1831 1832
		case T_Expr:
			{
1833
				Expr	   *expr = (Expr *) node;
1834

1835 1836
				if (expr->opType == SUBPLAN_EXPR)
				{
1837 1838 1839
					/* recurse to the SubLink node (skipping SubPlan!) */
					if (walker((Node *) ((SubPlan *) expr->oper)->sublink,
							   context))
1840 1841
						return true;
				}
1842 1843 1844 1845
				/* for all Expr node types, examine args list */
				if (expression_tree_walker((Node *) expr->args,
										   walker, context))
					return true;
1846 1847 1848 1849 1850 1851 1852
			}
			break;
		case T_Aggref:
			return walker(((Aggref *) node)->target, context);
		case T_ArrayRef:
			{
				ArrayRef   *aref = (ArrayRef *) node;
1853

1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
				/* recurse directly for upper/lower array index lists */
				if (expression_tree_walker((Node *) aref->refupperindexpr,
										   walker, context))
					return true;
				if (expression_tree_walker((Node *) aref->reflowerindexpr,
										   walker, context))
					return true;
				/* walker must see the refexpr and refassgnexpr, however */
				if (walker(aref->refexpr, context))
					return true;
				if (walker(aref->refassgnexpr, context))
					return true;
			}
			break;
1868 1869
		case T_FieldSelect:
			return walker(((FieldSelect *) node)->arg, context);
1870 1871
		case T_RelabelType:
			return walker(((RelabelType *) node)->arg, context);
1872 1873 1874
		case T_CaseExpr:
			{
				CaseExpr   *caseexpr = (CaseExpr *) node;
1875

1876 1877 1878 1879
				/* we assume walker doesn't care about CaseWhens, either */
				foreach(temp, caseexpr->args)
				{
					CaseWhen   *when = (CaseWhen *) lfirst(temp);
1880

1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
					Assert(IsA(when, CaseWhen));
					if (walker(when->expr, context))
						return true;
					if (walker(when->result, context))
						return true;
				}
				/* caseexpr->arg should be null, but we'll check it anyway */
				if (walker(caseexpr->arg, context))
					return true;
				if (walker(caseexpr->defresult, context))
					return true;
			}
			break;
1894 1895 1896 1897
		case T_NullTest:
			return walker(((NullTest *) node)->arg, context);
		case T_BooleanTest:
			return walker(((BooleanTest *) node)->arg, context);
1898 1899 1900 1901
		case T_ConstraintTest:
			if (walker(((ConstraintTest *) node)->arg, context))
				return true;
			return walker(((ConstraintTest *) node)->check_expr, context);
1902
		case T_SubLink:
1903
			{
1904
				SubLink    *sublink = (SubLink *) node;
1905

1906 1907
				/*
				 * If the SubLink has already been processed by
1908
				 * subselect.c, it will have lefthand=NIL, and we need to
1909 1910 1911 1912
				 * scan the oper list.	Otherwise we only need to look at
				 * the lefthand list (the incomplete Oper nodes in the
				 * oper list are deemed uninteresting, perhaps even
				 * confusing).
1913 1914
				 */
				if (sublink->lefthand)
1915 1916 1917 1918
				{
					if (walker((Node *) sublink->lefthand, context))
						return true;
				}
1919
				else
1920 1921 1922 1923
				{
					if (walker((Node *) sublink->oper, context))
						return true;
				}
1924

1925
				/*
1926 1927
				 * Also invoke the walker on the sublink's Query node, so
				 * it can recurse into the sub-query if it wants to.
1928 1929
				 */
				return walker(sublink->subselect, context);
1930 1931
			}
			break;
1932 1933 1934
		case T_Query:
			/* Do nothing with a sub-Query, per discussion above */
			break;
1935 1936 1937 1938 1939 1940 1941 1942 1943
		case T_List:
			foreach(temp, (List *) node)
			{
				if (walker((Node *) lfirst(temp), context))
					return true;
			}
			break;
		case T_TargetEntry:
			return walker(((TargetEntry *) node)->expr, context);
1944 1945
		case T_FromExpr:
			{
1946
				FromExpr   *from = (FromExpr *) node;
1947 1948 1949 1950 1951 1952 1953

				if (walker(from->fromlist, context))
					return true;
				if (walker(from->quals, context))
					return true;
			}
			break;
1954 1955
		case T_JoinExpr:
			{
1956
				JoinExpr   *join = (JoinExpr *) node;
1957 1958 1959 1960 1961 1962 1963

				if (walker(join->larg, context))
					return true;
				if (walker(join->rarg, context))
					return true;
				if (walker(join->quals, context))
					return true;
Bruce Momjian's avatar
Bruce Momjian committed
1964

1965
				/*
1966
				 * alias clause, using list are deemed uninteresting.
1967 1968 1969
				 */
			}
			break;
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
		case T_SetOperationStmt:
			{
				SetOperationStmt *setop = (SetOperationStmt *) node;

				if (walker(setop->larg, context))
					return true;
				if (walker(setop->rarg, context))
					return true;
			}
			break;
1980 1981 1982 1983 1984 1985 1986
		default:
			elog(ERROR, "expression_tree_walker: Unexpected node type %d",
				 nodeTag(node));
			break;
	}
	return false;
}
1987

1988 1989 1990 1991 1992 1993 1994 1995
/*
 * query_tree_walker --- initiate a walk of a Query's expressions
 *
 * This routine exists just to reduce the number of places that need to know
 * where all the expression subtrees of a Query are.  Note it can be used
 * for starting a walk at top level of a Query regardless of whether the
 * walker intends to descend into subqueries.  It is also useful for
 * descending into subqueries within a walker.
1996
 *
1997 1998 1999 2000 2001
 * Some callers want to suppress visitation of certain items in the sub-Query,
 * typically because they need to process them specially, or don't actually
 * want to recurse into subqueries.  This is supported by the flags argument,
 * which is the bitwise OR of flag values to suppress visitation of
 * indicated items.  (More flag bits may be added as needed.)
2002 2003 2004 2005
 */
bool
query_tree_walker(Query *query,
				  bool (*walker) (),
2006
				  void *context,
2007
				  int flags)
2008
{
2009 2010
	List	   *rt;

2011 2012 2013 2014
	Assert(query != NULL && IsA(query, Query));

	if (walker((Node *) query->targetList, context))
		return true;
2015
	if (walker((Node *) query->jointree, context))
2016
		return true;
2017 2018
	if (walker(query->setOperations, context))
		return true;
2019 2020
	if (walker(query->havingQual, context))
		return true;
2021
	foreach(rt, query->rtable)
2022
	{
2023
		RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
2024

2025
		switch (rte->rtekind)
2026
		{
2027 2028 2029 2030 2031
			case RTE_RELATION:
			case RTE_SPECIAL:
				/* nothing to do */
				break;
			case RTE_SUBQUERY:
2032
				if (! (flags & QTW_IGNORE_SUBQUERIES))
2033 2034 2035 2036
					if (walker(rte->subquery, context))
						return true;
				break;
			case RTE_JOIN:
2037 2038 2039
				if (! (flags & QTW_IGNORE_JOINALIASES))
					if (walker(rte->joinaliasvars, context))
						return true;
2040
				break;
2041 2042 2043 2044
			case RTE_FUNCTION:
				if (walker(rte->funcexpr, context))
					return true;
				break;
2045 2046
		}
	}
2047 2048 2049 2050
	return false;
}


2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
/*--------------------
 * expression_tree_mutator() is designed to support routines that make a
 * modified copy of an expression tree, with some nodes being added,
 * removed, or replaced by new subtrees.  The original tree is (normally)
 * not changed.  Each recursion level is responsible for returning a copy of
 * (or appropriately modified substitute for) the subtree it is handed.
 * A mutator routine should look like this:
 *
 * Node * my_mutator (Node *node, my_struct *context)
 * {
 *		if (node == NULL)
 *			return NULL;
Bruce Momjian's avatar
Bruce Momjian committed
2063
 *		// check for nodes that special work is required for, eg:
2064 2065 2066 2067 2068 2069 2070 2071
 *		if (IsA(node, Var))
 *		{
 *			... create and return modified copy of Var node
 *		}
 *		else if (IsA(node, ...))
 *		{
 *			... do special transformations of other node types
 *		}
Bruce Momjian's avatar
Bruce Momjian committed
2072
 *		// for any node type not specially processed, do:
2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
 *		return expression_tree_mutator(node, my_mutator, (void *) context);
 * }
 *
 * The "context" argument points to a struct that holds whatever context
 * information the mutator routine needs --- it can be used to return extra
 * data gathered by the mutator, too.  This argument is not touched by
 * expression_tree_mutator, but it is passed down to recursive sub-invocations
 * of my_mutator.  The tree walk is started from a setup routine that
 * fills in the appropriate context struct, calls my_mutator with the
 * top-level node of the tree, and does any required post-processing.
 *
 * Each level of recursion must return an appropriately modified Node.
 * If expression_tree_mutator() is called, it will make an exact copy
 * of the given Node, but invoke my_mutator() to copy the sub-node(s)
 * of that Node.  In this way, my_mutator() has full control over the
 * copying process but need not directly deal with expression trees
 * that it has no interest in.
 *
 * Just as for expression_tree_walker, the node types handled by
 * expression_tree_mutator include all those normally found in target lists
 * and qualifier clauses during the planning stage.
 *
 * expression_tree_mutator will handle a SUBPLAN_EXPR node by recursing into
 * the args and slink->oper lists (which belong to the outer plan), but it
 * will simply copy the link to the inner plan, since that's typically what
 * expression tree mutators want.  A mutator that wants to modify the subplan
2099 2100
 * can force appropriate behavior by recognizing subplan expression nodes
 * and doing the right thing.
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
 *
 * Bare SubLink nodes (without a SUBPLAN_EXPR) are handled by recursing into
 * the "lefthand" argument list only.  (A bare SubLink should be seen only if
 * the tree has not yet been processed by subselect.c.)  Again, this can be
 * overridden by the mutator, but it seems to be the most useful default
 * behavior.
 *--------------------
 */

Node *
2111 2112 2113
expression_tree_mutator(Node *node,
						Node *(*mutator) (),
						void *context)
2114 2115
{
	/*
2116 2117
	 * The mutator has already decided not to modify the current node, but
	 * we must call the mutator for any sub-nodes.
2118 2119 2120 2121 2122 2123
	 */

#define FLATCOPY(newnode, node, nodetype)  \
	( (newnode) = makeNode(nodetype), \
	  memcpy((newnode), (node), sizeof(nodetype)) )

2124
#define CHECKFLATCOPY(newnode, node, nodetype)	\
2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
	( AssertMacro(IsA((node), nodetype)), \
	  (newnode) = makeNode(nodetype), \
	  memcpy((newnode), (node), sizeof(nodetype)) )

#define MUTATE(newfield, oldfield, fieldtype)  \
		( (newfield) = (fieldtype) mutator((Node *) (oldfield), context) )

	if (node == NULL)
		return NULL;
	switch (nodeTag(node))
	{
		case T_Const:
		case T_Var:
		case T_Param:
2139
		case T_RangeTblRef:
2140 2141
			/* primitive node types with no subnodes */
			return (Node *) copyObject(node);
2142 2143
		case T_Expr:
			{
2144 2145
				Expr	   *expr = (Expr *) node;
				Expr	   *newnode;
2146 2147 2148 2149 2150

				FLATCOPY(newnode, expr, Expr);

				if (expr->opType == SUBPLAN_EXPR)
				{
2151 2152
					SubLink    *oldsublink = ((SubPlan *) expr->oper)->sublink;
					SubPlan    *newsubplan;
2153 2154 2155 2156 2157 2158

					/* flat-copy the oper node, which is a SubPlan */
					CHECKFLATCOPY(newsubplan, expr->oper, SubPlan);
					newnode->oper = (Node *) newsubplan;
					/* likewise its SubLink node */
					CHECKFLATCOPY(newsubplan->sublink, oldsublink, SubLink);
2159 2160 2161 2162 2163

					/*
					 * transform args list (params to be passed to
					 * subplan)
					 */
2164 2165
					MUTATE(newnode->args, expr->args, List *);
					/* transform sublink's oper list as well */
2166 2167 2168 2169 2170 2171
					MUTATE(newsubplan->sublink->oper, oldsublink->oper, List *);

					/*
					 * but not the subplan itself, which is referenced
					 * as-is
					 */
2172 2173 2174
				}
				else
				{
2175 2176 2177
					/*
					 * for other Expr node types, just transform args
					 * list, linking to original oper node (OK?)
2178 2179 2180 2181 2182 2183 2184 2185
					 */
					MUTATE(newnode->args, expr->args, List *);
				}
				return (Node *) newnode;
			}
			break;
		case T_Aggref:
			{
2186 2187
				Aggref	   *aggref = (Aggref *) node;
				Aggref	   *newnode;
2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210

				FLATCOPY(newnode, aggref, Aggref);
				MUTATE(newnode->target, aggref->target, Node *);
				return (Node *) newnode;
			}
			break;
		case T_ArrayRef:
			{
				ArrayRef   *arrayref = (ArrayRef *) node;
				ArrayRef   *newnode;

				FLATCOPY(newnode, arrayref, ArrayRef);
				MUTATE(newnode->refupperindexpr, arrayref->refupperindexpr,
					   List *);
				MUTATE(newnode->reflowerindexpr, arrayref->reflowerindexpr,
					   List *);
				MUTATE(newnode->refexpr, arrayref->refexpr,
					   Node *);
				MUTATE(newnode->refassgnexpr, arrayref->refassgnexpr,
					   Node *);
				return (Node *) newnode;
			}
			break;
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
		case T_FieldSelect:
			{
				FieldSelect *fselect = (FieldSelect *) node;
				FieldSelect *newnode;

				FLATCOPY(newnode, fselect, FieldSelect);
				MUTATE(newnode->arg, fselect->arg, Node *);
				return (Node *) newnode;
			}
			break;
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230
		case T_RelabelType:
			{
				RelabelType *relabel = (RelabelType *) node;
				RelabelType *newnode;

				FLATCOPY(newnode, relabel, RelabelType);
				MUTATE(newnode->arg, relabel->arg, Node *);
				return (Node *) newnode;
			}
			break;
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254
		case T_CaseExpr:
			{
				CaseExpr   *caseexpr = (CaseExpr *) node;
				CaseExpr   *newnode;

				FLATCOPY(newnode, caseexpr, CaseExpr);
				MUTATE(newnode->args, caseexpr->args, List *);
				/* caseexpr->arg should be null, but we'll check it anyway */
				MUTATE(newnode->arg, caseexpr->arg, Node *);
				MUTATE(newnode->defresult, caseexpr->defresult, Node *);
				return (Node *) newnode;
			}
			break;
		case T_CaseWhen:
			{
				CaseWhen   *casewhen = (CaseWhen *) node;
				CaseWhen   *newnode;

				FLATCOPY(newnode, casewhen, CaseWhen);
				MUTATE(newnode->expr, casewhen->expr, Node *);
				MUTATE(newnode->result, casewhen->result, Node *);
				return (Node *) newnode;
			}
			break;
2255 2256
		case T_NullTest:
			{
2257 2258
				NullTest   *ntest = (NullTest *) node;
				NullTest   *newnode;
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274

				FLATCOPY(newnode, ntest, NullTest);
				MUTATE(newnode->arg, ntest->arg, Node *);
				return (Node *) newnode;
			}
			break;
		case T_BooleanTest:
			{
				BooleanTest *btest = (BooleanTest *) node;
				BooleanTest *newnode;

				FLATCOPY(newnode, btest, BooleanTest);
				MUTATE(newnode->arg, btest->arg, Node *);
				return (Node *) newnode;
			}
			break;
2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
		case T_ConstraintTest:
			{
				ConstraintTest *ctest = (ConstraintTest *) node;
				ConstraintTest *newnode;

				FLATCOPY(newnode, ctest, ConstraintTest);
				MUTATE(newnode->arg, ctest->arg, Node *);
				MUTATE(newnode->check_expr, ctest->check_expr, Node *);
				return (Node *) newnode;
			}
			break;
2286 2287
		case T_SubLink:
			{
2288 2289 2290 2291
				/*
				 * A "bare" SubLink (note we will not come here if we
				 * found a SUBPLAN_EXPR node above it).  Transform the
				 * lefthand side, but not the oper list nor the subquery.
2292
				 */
2293 2294
				SubLink    *sublink = (SubLink *) node;
				SubLink    *newnode;
2295 2296 2297 2298 2299 2300 2301 2302

				FLATCOPY(newnode, sublink, SubLink);
				MUTATE(newnode->lefthand, sublink->lefthand, List *);
				return (Node *) newnode;
			}
			break;
		case T_List:
			{
2303 2304 2305 2306 2307
				/*
				 * We assume the mutator isn't interested in the list
				 * nodes per se, so just invoke it on each list element.
				 * NOTE: this would fail badly on a list with integer
				 * elements!
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322
				 */
				List	   *resultlist = NIL;
				List	   *temp;

				foreach(temp, (List *) node)
				{
					resultlist = lappend(resultlist,
										 mutator((Node *) lfirst(temp),
												 context));
				}
				return (Node *) resultlist;
			}
			break;
		case T_TargetEntry:
			{
2323 2324 2325 2326 2327 2328
				/*
				 * We mutate the expression, but not the resdom, by
				 * default.
				 */
				TargetEntry *targetentry = (TargetEntry *) node;
				TargetEntry *newnode;
2329 2330 2331 2332 2333 2334

				FLATCOPY(newnode, targetentry, TargetEntry);
				MUTATE(newnode->expr, targetentry->expr, Node *);
				return (Node *) newnode;
			}
			break;
2335 2336
		case T_FromExpr:
			{
2337 2338
				FromExpr   *from = (FromExpr *) node;
				FromExpr   *newnode;
2339 2340 2341 2342 2343 2344 2345

				FLATCOPY(newnode, from, FromExpr);
				MUTATE(newnode->fromlist, from->fromlist, List *);
				MUTATE(newnode->quals, from->quals, Node *);
				return (Node *) newnode;
			}
			break;
2346 2347
		case T_JoinExpr:
			{
2348 2349
				JoinExpr   *join = (JoinExpr *) node;
				JoinExpr   *newnode;
2350 2351 2352 2353 2354

				FLATCOPY(newnode, join, JoinExpr);
				MUTATE(newnode->larg, join->larg, Node *);
				MUTATE(newnode->rarg, join->rarg, Node *);
				MUTATE(newnode->quals, join->quals, Node *);
2355
				/* We do not mutate alias or using by default */
2356 2357 2358
				return (Node *) newnode;
			}
			break;
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
		case T_SetOperationStmt:
			{
				SetOperationStmt *setop = (SetOperationStmt *) node;
				SetOperationStmt *newnode;

				FLATCOPY(newnode, setop, SetOperationStmt);
				MUTATE(newnode->larg, setop->larg, Node *);
				MUTATE(newnode->rarg, setop->rarg, Node *);
				return (Node *) newnode;
			}
			break;
2370 2371 2372 2373 2374 2375 2376 2377
		default:
			elog(ERROR, "expression_tree_mutator: Unexpected node type %d",
				 nodeTag(node));
			break;
	}
	/* can't get here, but keep compiler happy */
	return NULL;
}
2378 2379 2380 2381 2382 2383 2384 2385


/*
 * query_tree_mutator --- initiate modification of a Query's expressions
 *
 * This routine exists just to reduce the number of places that need to know
 * where all the expression subtrees of a Query are.  Note it can be used
 * for starting a walk at top level of a Query regardless of whether the
2386
 * mutator intends to descend into subqueries.	It is also useful for
2387 2388 2389 2390 2391 2392
 * descending into subqueries within a mutator.
 *
 * The specified Query node is modified-in-place; do a FLATCOPY() beforehand
 * if you don't want to change the original.  All substructure is safely
 * copied, however.
 *
2393 2394 2395 2396 2397
 * Some callers want to suppress mutating of certain items in the sub-Query,
 * typically because they need to process them specially, or don't actually
 * want to recurse into subqueries.  This is supported by the flags argument,
 * which is the bitwise OR of flag values to suppress mutating of
 * indicated items.  (More flag bits may be added as needed.)
2398 2399 2400 2401 2402
 */
void
query_tree_mutator(Query *query,
				   Node *(*mutator) (),
				   void *context,
2403
				   int flags)
2404
{
2405 2406 2407
	List	   *newrt = NIL;
	List	   *rt;

2408 2409 2410 2411 2412 2413
	Assert(query != NULL && IsA(query, Query));

	MUTATE(query->targetList, query->targetList, List *);
	MUTATE(query->jointree, query->jointree, FromExpr *);
	MUTATE(query->setOperations, query->setOperations, Node *);
	MUTATE(query->havingQual, query->havingQual, Node *);
2414
	foreach(rt, query->rtable)
2415
	{
2416 2417
		RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
		RangeTblEntry *newrte;
2418

2419
		switch (rte->rtekind)
2420
		{
2421 2422 2423 2424 2425
			case RTE_RELATION:
			case RTE_SPECIAL:
				/* nothing to do, don't bother to make a copy */
				break;
			case RTE_SUBQUERY:
2426
				if (! (flags & QTW_IGNORE_SUBQUERIES))
2427 2428 2429 2430 2431 2432 2433 2434
				{
					FLATCOPY(newrte, rte, RangeTblEntry);
					CHECKFLATCOPY(newrte->subquery, rte->subquery, Query);
					MUTATE(newrte->subquery, newrte->subquery, Query *);
					rte = newrte;
				}
				break;
			case RTE_JOIN:
2435 2436 2437 2438 2439 2440
				if (! (flags & QTW_IGNORE_JOINALIASES))
				{
					FLATCOPY(newrte, rte, RangeTblEntry);
					MUTATE(newrte->joinaliasvars, rte->joinaliasvars, List *);
					rte = newrte;
				}
2441
				break;
2442 2443 2444 2445 2446
			case RTE_FUNCTION:
				FLATCOPY(newrte, rte, RangeTblEntry);
				MUTATE(newrte->funcexpr, rte->funcexpr, Node *);
				rte = newrte;
				break;
2447
		}
2448
		newrt = lappend(newrt, rte);
2449
	}
2450
	query->rtable = newrt;
2451
}