1 /*-------------------------------------------------------------------------
2  *
3  * parse_expr.c
4  *	  handle expressions in parser
5  *
6  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *	  src/backend/parser/parse_expr.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include "catalog/pg_type.h"
19 #include "commands/dbcommands.h"
20 #include "miscadmin.h"
21 #include "nodes/makefuncs.h"
22 #include "nodes/nodeFuncs.h"
23 #include "optimizer/optimizer.h"
24 #include "parser/analyze.h"
25 #include "parser/parse_agg.h"
26 #include "parser/parse_clause.h"
27 #include "parser/parse_coerce.h"
28 #include "parser/parse_collate.h"
29 #include "parser/parse_expr.h"
30 #include "parser/parse_func.h"
31 #include "parser/parse_oper.h"
32 #include "parser/parse_relation.h"
33 #include "parser/parse_target.h"
34 #include "parser/parse_type.h"
35 #include "utils/builtins.h"
36 #include "utils/date.h"
37 #include "utils/lsyscache.h"
38 #include "utils/timestamp.h"
39 #include "utils/xml.h"
40 
41 /* GUC parameters */
42 bool		Transform_null_equals = false;
43 
44 
45 static Node *transformExprRecurse(ParseState *pstate, Node *expr);
46 static Node *transformParamRef(ParseState *pstate, ParamRef *pref);
47 static Node *transformAExprOp(ParseState *pstate, A_Expr *a);
48 static Node *transformAExprOpAny(ParseState *pstate, A_Expr *a);
49 static Node *transformAExprOpAll(ParseState *pstate, A_Expr *a);
50 static Node *transformAExprDistinct(ParseState *pstate, A_Expr *a);
51 static Node *transformAExprNullIf(ParseState *pstate, A_Expr *a);
52 static Node *transformAExprIn(ParseState *pstate, A_Expr *a);
53 static Node *transformAExprBetween(ParseState *pstate, A_Expr *a);
54 static Node *transformBoolExpr(ParseState *pstate, BoolExpr *a);
55 static Node *transformFuncCall(ParseState *pstate, FuncCall *fn);
56 static Node *transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref);
57 static Node *transformCaseExpr(ParseState *pstate, CaseExpr *c);
58 static Node *transformSubLink(ParseState *pstate, SubLink *sublink);
59 static Node *transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
60 								Oid array_type, Oid element_type, int32 typmod);
61 static Node *transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault);
62 static Node *transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c);
63 static Node *transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m);
64 static Node *transformSQLValueFunction(ParseState *pstate,
65 									   SQLValueFunction *svf);
66 static Node *transformXmlExpr(ParseState *pstate, XmlExpr *x);
67 static Node *transformXmlSerialize(ParseState *pstate, XmlSerialize *xs);
68 static Node *transformBooleanTest(ParseState *pstate, BooleanTest *b);
69 static Node *transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr);
70 static Node *transformColumnRef(ParseState *pstate, ColumnRef *cref);
71 static Node *transformWholeRowRef(ParseState *pstate,
72 								  ParseNamespaceItem *nsitem,
73 								  int sublevels_up, int location);
74 static Node *transformIndirection(ParseState *pstate, A_Indirection *ind);
75 static Node *transformTypeCast(ParseState *pstate, TypeCast *tc);
76 static Node *transformCollateClause(ParseState *pstate, CollateClause *c);
77 static Node *make_row_comparison_op(ParseState *pstate, List *opname,
78 									List *largs, List *rargs, int location);
79 static Node *make_row_distinct_op(ParseState *pstate, List *opname,
80 								  RowExpr *lrow, RowExpr *rrow, int location);
81 static Expr *make_distinct_op(ParseState *pstate, List *opname,
82 							  Node *ltree, Node *rtree, int location);
83 static Node *make_nulltest_from_distinct(ParseState *pstate,
84 										 A_Expr *distincta, Node *arg);
85 
86 
87 /*
88  * transformExpr -
89  *	  Analyze and transform expressions. Type checking and type casting is
90  *	  done here.  This processing converts the raw grammar output into
91  *	  expression trees with fully determined semantics.
92  */
93 Node *
transformExpr(ParseState * pstate,Node * expr,ParseExprKind exprKind)94 transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
95 {
96 	Node	   *result;
97 	ParseExprKind sv_expr_kind;
98 
99 	/* Save and restore identity of expression type we're parsing */
100 	Assert(exprKind != EXPR_KIND_NONE);
101 	sv_expr_kind = pstate->p_expr_kind;
102 	pstate->p_expr_kind = exprKind;
103 
104 	result = transformExprRecurse(pstate, expr);
105 
106 	pstate->p_expr_kind = sv_expr_kind;
107 
108 	return result;
109 }
110 
111 static Node *
transformExprRecurse(ParseState * pstate,Node * expr)112 transformExprRecurse(ParseState *pstate, Node *expr)
113 {
114 	Node	   *result;
115 
116 	if (expr == NULL)
117 		return NULL;
118 
119 	/* Guard against stack overflow due to overly complex expressions */
120 	check_stack_depth();
121 
122 	switch (nodeTag(expr))
123 	{
124 		case T_ColumnRef:
125 			result = transformColumnRef(pstate, (ColumnRef *) expr);
126 			break;
127 
128 		case T_ParamRef:
129 			result = transformParamRef(pstate, (ParamRef *) expr);
130 			break;
131 
132 		case T_A_Const:
133 			{
134 				A_Const    *con = (A_Const *) expr;
135 				Value	   *val = &con->val;
136 
137 				result = (Node *) make_const(pstate, val, con->location);
138 				break;
139 			}
140 
141 		case T_A_Indirection:
142 			result = transformIndirection(pstate, (A_Indirection *) expr);
143 			break;
144 
145 		case T_A_ArrayExpr:
146 			result = transformArrayExpr(pstate, (A_ArrayExpr *) expr,
147 										InvalidOid, InvalidOid, -1);
148 			break;
149 
150 		case T_TypeCast:
151 			result = transformTypeCast(pstate, (TypeCast *) expr);
152 			break;
153 
154 		case T_CollateClause:
155 			result = transformCollateClause(pstate, (CollateClause *) expr);
156 			break;
157 
158 		case T_A_Expr:
159 			{
160 				A_Expr	   *a = (A_Expr *) expr;
161 
162 				switch (a->kind)
163 				{
164 					case AEXPR_OP:
165 						result = transformAExprOp(pstate, a);
166 						break;
167 					case AEXPR_OP_ANY:
168 						result = transformAExprOpAny(pstate, a);
169 						break;
170 					case AEXPR_OP_ALL:
171 						result = transformAExprOpAll(pstate, a);
172 						break;
173 					case AEXPR_DISTINCT:
174 					case AEXPR_NOT_DISTINCT:
175 						result = transformAExprDistinct(pstate, a);
176 						break;
177 					case AEXPR_NULLIF:
178 						result = transformAExprNullIf(pstate, a);
179 						break;
180 					case AEXPR_IN:
181 						result = transformAExprIn(pstate, a);
182 						break;
183 					case AEXPR_LIKE:
184 					case AEXPR_ILIKE:
185 					case AEXPR_SIMILAR:
186 						/* we can transform these just like AEXPR_OP */
187 						result = transformAExprOp(pstate, a);
188 						break;
189 					case AEXPR_BETWEEN:
190 					case AEXPR_NOT_BETWEEN:
191 					case AEXPR_BETWEEN_SYM:
192 					case AEXPR_NOT_BETWEEN_SYM:
193 						result = transformAExprBetween(pstate, a);
194 						break;
195 					default:
196 						elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
197 						result = NULL;	/* keep compiler quiet */
198 						break;
199 				}
200 				break;
201 			}
202 
203 		case T_BoolExpr:
204 			result = transformBoolExpr(pstate, (BoolExpr *) expr);
205 			break;
206 
207 		case T_FuncCall:
208 			result = transformFuncCall(pstate, (FuncCall *) expr);
209 			break;
210 
211 		case T_MultiAssignRef:
212 			result = transformMultiAssignRef(pstate, (MultiAssignRef *) expr);
213 			break;
214 
215 		case T_GroupingFunc:
216 			result = transformGroupingFunc(pstate, (GroupingFunc *) expr);
217 			break;
218 
219 		case T_NamedArgExpr:
220 			{
221 				NamedArgExpr *na = (NamedArgExpr *) expr;
222 
223 				na->arg = (Expr *) transformExprRecurse(pstate, (Node *) na->arg);
224 				result = expr;
225 				break;
226 			}
227 
228 		case T_SubLink:
229 			result = transformSubLink(pstate, (SubLink *) expr);
230 			break;
231 
232 		case T_CaseExpr:
233 			result = transformCaseExpr(pstate, (CaseExpr *) expr);
234 			break;
235 
236 		case T_RowExpr:
237 			result = transformRowExpr(pstate, (RowExpr *) expr, false);
238 			break;
239 
240 		case T_CoalesceExpr:
241 			result = transformCoalesceExpr(pstate, (CoalesceExpr *) expr);
242 			break;
243 
244 		case T_MinMaxExpr:
245 			result = transformMinMaxExpr(pstate, (MinMaxExpr *) expr);
246 			break;
247 
248 		case T_SQLValueFunction:
249 			result = transformSQLValueFunction(pstate,
250 											   (SQLValueFunction *) expr);
251 			break;
252 
253 		case T_XmlExpr:
254 			result = transformXmlExpr(pstate, (XmlExpr *) expr);
255 			break;
256 
257 		case T_XmlSerialize:
258 			result = transformXmlSerialize(pstate, (XmlSerialize *) expr);
259 			break;
260 
261 		case T_NullTest:
262 			{
263 				NullTest   *n = (NullTest *) expr;
264 
265 				n->arg = (Expr *) transformExprRecurse(pstate, (Node *) n->arg);
266 				/* the argument can be any type, so don't coerce it */
267 				n->argisrow = type_is_rowtype(exprType((Node *) n->arg));
268 				result = expr;
269 				break;
270 			}
271 
272 		case T_BooleanTest:
273 			result = transformBooleanTest(pstate, (BooleanTest *) expr);
274 			break;
275 
276 		case T_CurrentOfExpr:
277 			result = transformCurrentOfExpr(pstate, (CurrentOfExpr *) expr);
278 			break;
279 
280 			/*
281 			 * In all places where DEFAULT is legal, the caller should have
282 			 * processed it rather than passing it to transformExpr().
283 			 */
284 		case T_SetToDefault:
285 			ereport(ERROR,
286 					(errcode(ERRCODE_SYNTAX_ERROR),
287 					 errmsg("DEFAULT is not allowed in this context"),
288 					 parser_errposition(pstate,
289 										((SetToDefault *) expr)->location)));
290 			break;
291 
292 			/*
293 			 * CaseTestExpr doesn't require any processing; it is only
294 			 * injected into parse trees in a fully-formed state.
295 			 *
296 			 * Ordinarily we should not see a Var here, but it is convenient
297 			 * for transformJoinUsingClause() to create untransformed operator
298 			 * trees containing already-transformed Vars.  The best
299 			 * alternative would be to deconstruct and reconstruct column
300 			 * references, which seems expensively pointless.  So allow it.
301 			 */
302 		case T_CaseTestExpr:
303 		case T_Var:
304 			{
305 				result = (Node *) expr;
306 				break;
307 			}
308 
309 		default:
310 			/* should not reach here */
311 			elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
312 			result = NULL;		/* keep compiler quiet */
313 			break;
314 	}
315 
316 	return result;
317 }
318 
319 /*
320  * helper routine for delivering "column does not exist" error message
321  *
322  * (Usually we don't have to work this hard, but the general case of field
323  * selection from an arbitrary node needs it.)
324  */
325 static void
unknown_attribute(ParseState * pstate,Node * relref,const char * attname,int location)326 unknown_attribute(ParseState *pstate, Node *relref, const char *attname,
327 				  int location)
328 {
329 	RangeTblEntry *rte;
330 
331 	if (IsA(relref, Var) &&
332 		((Var *) relref)->varattno == InvalidAttrNumber)
333 	{
334 		/* Reference the RTE by alias not by actual table name */
335 		rte = GetRTEByRangeTablePosn(pstate,
336 									 ((Var *) relref)->varno,
337 									 ((Var *) relref)->varlevelsup);
338 		ereport(ERROR,
339 				(errcode(ERRCODE_UNDEFINED_COLUMN),
340 				 errmsg("column %s.%s does not exist",
341 						rte->eref->aliasname, attname),
342 				 parser_errposition(pstate, location)));
343 	}
344 	else
345 	{
346 		/* Have to do it by reference to the type of the expression */
347 		Oid			relTypeId = exprType(relref);
348 
349 		if (ISCOMPLEX(relTypeId))
350 			ereport(ERROR,
351 					(errcode(ERRCODE_UNDEFINED_COLUMN),
352 					 errmsg("column \"%s\" not found in data type %s",
353 							attname, format_type_be(relTypeId)),
354 					 parser_errposition(pstate, location)));
355 		else if (relTypeId == RECORDOID)
356 			ereport(ERROR,
357 					(errcode(ERRCODE_UNDEFINED_COLUMN),
358 					 errmsg("could not identify column \"%s\" in record data type",
359 							attname),
360 					 parser_errposition(pstate, location)));
361 		else
362 			ereport(ERROR,
363 					(errcode(ERRCODE_WRONG_OBJECT_TYPE),
364 					 errmsg("column notation .%s applied to type %s, "
365 							"which is not a composite type",
366 							attname, format_type_be(relTypeId)),
367 					 parser_errposition(pstate, location)));
368 	}
369 }
370 
371 static Node *
transformIndirection(ParseState * pstate,A_Indirection * ind)372 transformIndirection(ParseState *pstate, A_Indirection *ind)
373 {
374 	Node	   *last_srf = pstate->p_last_srf;
375 	Node	   *result = transformExprRecurse(pstate, ind->arg);
376 	List	   *subscripts = NIL;
377 	int			location = exprLocation(result);
378 	ListCell   *i;
379 
380 	/*
381 	 * We have to split any field-selection operations apart from
382 	 * subscripting.  Adjacent A_Indices nodes have to be treated as a single
383 	 * multidimensional subscript operation.
384 	 */
385 	foreach(i, ind->indirection)
386 	{
387 		Node	   *n = lfirst(i);
388 
389 		if (IsA(n, A_Indices))
390 			subscripts = lappend(subscripts, n);
391 		else if (IsA(n, A_Star))
392 		{
393 			ereport(ERROR,
394 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
395 					 errmsg("row expansion via \"*\" is not supported here"),
396 					 parser_errposition(pstate, location)));
397 		}
398 		else
399 		{
400 			Node	   *newresult;
401 
402 			Assert(IsA(n, String));
403 
404 			/* process subscripts before this field selection */
405 			if (subscripts)
406 				result = (Node *) transformContainerSubscripts(pstate,
407 															   result,
408 															   exprType(result),
409 															   exprTypmod(result),
410 															   subscripts,
411 															   false);
412 			subscripts = NIL;
413 
414 			newresult = ParseFuncOrColumn(pstate,
415 										  list_make1(n),
416 										  list_make1(result),
417 										  last_srf,
418 										  NULL,
419 										  false,
420 										  location);
421 			if (newresult == NULL)
422 				unknown_attribute(pstate, result, strVal(n), location);
423 			result = newresult;
424 		}
425 	}
426 	/* process trailing subscripts, if any */
427 	if (subscripts)
428 		result = (Node *) transformContainerSubscripts(pstate,
429 													   result,
430 													   exprType(result),
431 													   exprTypmod(result),
432 													   subscripts,
433 													   false);
434 
435 	return result;
436 }
437 
438 /*
439  * Transform a ColumnRef.
440  *
441  * If you find yourself changing this code, see also ExpandColumnRefStar.
442  */
443 static Node *
transformColumnRef(ParseState * pstate,ColumnRef * cref)444 transformColumnRef(ParseState *pstate, ColumnRef *cref)
445 {
446 	Node	   *node = NULL;
447 	char	   *nspname = NULL;
448 	char	   *relname = NULL;
449 	char	   *colname = NULL;
450 	ParseNamespaceItem *nsitem;
451 	int			levels_up;
452 	enum
453 	{
454 		CRERR_NO_COLUMN,
455 		CRERR_NO_RTE,
456 		CRERR_WRONG_DB,
457 		CRERR_TOO_MANY
458 	}			crerr = CRERR_NO_COLUMN;
459 	const char *err;
460 
461 	/*
462 	 * Check to see if the column reference is in an invalid place within the
463 	 * query.  We allow column references in most places, except in default
464 	 * expressions and partition bound expressions.
465 	 */
466 	err = NULL;
467 	switch (pstate->p_expr_kind)
468 	{
469 		case EXPR_KIND_NONE:
470 			Assert(false);		/* can't happen */
471 			break;
472 		case EXPR_KIND_OTHER:
473 		case EXPR_KIND_JOIN_ON:
474 		case EXPR_KIND_JOIN_USING:
475 		case EXPR_KIND_FROM_SUBSELECT:
476 		case EXPR_KIND_FROM_FUNCTION:
477 		case EXPR_KIND_WHERE:
478 		case EXPR_KIND_POLICY:
479 		case EXPR_KIND_HAVING:
480 		case EXPR_KIND_FILTER:
481 		case EXPR_KIND_WINDOW_PARTITION:
482 		case EXPR_KIND_WINDOW_ORDER:
483 		case EXPR_KIND_WINDOW_FRAME_RANGE:
484 		case EXPR_KIND_WINDOW_FRAME_ROWS:
485 		case EXPR_KIND_WINDOW_FRAME_GROUPS:
486 		case EXPR_KIND_SELECT_TARGET:
487 		case EXPR_KIND_INSERT_TARGET:
488 		case EXPR_KIND_UPDATE_SOURCE:
489 		case EXPR_KIND_UPDATE_TARGET:
490 		case EXPR_KIND_GROUP_BY:
491 		case EXPR_KIND_ORDER_BY:
492 		case EXPR_KIND_DISTINCT_ON:
493 		case EXPR_KIND_LIMIT:
494 		case EXPR_KIND_OFFSET:
495 		case EXPR_KIND_RETURNING:
496 		case EXPR_KIND_VALUES:
497 		case EXPR_KIND_VALUES_SINGLE:
498 		case EXPR_KIND_CHECK_CONSTRAINT:
499 		case EXPR_KIND_DOMAIN_CHECK:
500 		case EXPR_KIND_FUNCTION_DEFAULT:
501 		case EXPR_KIND_INDEX_EXPRESSION:
502 		case EXPR_KIND_INDEX_PREDICATE:
503 		case EXPR_KIND_STATS_EXPRESSION:
504 		case EXPR_KIND_ALTER_COL_TRANSFORM:
505 		case EXPR_KIND_EXECUTE_PARAMETER:
506 		case EXPR_KIND_TRIGGER_WHEN:
507 		case EXPR_KIND_PARTITION_EXPRESSION:
508 		case EXPR_KIND_CALL_ARGUMENT:
509 		case EXPR_KIND_COPY_WHERE:
510 		case EXPR_KIND_GENERATED_COLUMN:
511 		case EXPR_KIND_CYCLE_MARK:
512 			/* okay */
513 			break;
514 
515 		case EXPR_KIND_COLUMN_DEFAULT:
516 			err = _("cannot use column reference in DEFAULT expression");
517 			break;
518 		case EXPR_KIND_PARTITION_BOUND:
519 			err = _("cannot use column reference in partition bound expression");
520 			break;
521 
522 			/*
523 			 * There is intentionally no default: case here, so that the
524 			 * compiler will warn if we add a new ParseExprKind without
525 			 * extending this switch.  If we do see an unrecognized value at
526 			 * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
527 			 * which is sane anyway.
528 			 */
529 	}
530 	if (err)
531 		ereport(ERROR,
532 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
533 				 errmsg_internal("%s", err),
534 				 parser_errposition(pstate, cref->location)));
535 
536 	/*
537 	 * Give the PreParseColumnRefHook, if any, first shot.  If it returns
538 	 * non-null then that's all, folks.
539 	 */
540 	if (pstate->p_pre_columnref_hook != NULL)
541 	{
542 		node = pstate->p_pre_columnref_hook(pstate, cref);
543 		if (node != NULL)
544 			return node;
545 	}
546 
547 	/*----------
548 	 * The allowed syntaxes are:
549 	 *
550 	 * A		First try to resolve as unqualified column name;
551 	 *			if no luck, try to resolve as unqualified table name (A.*).
552 	 * A.B		A is an unqualified table name; B is either a
553 	 *			column or function name (trying column name first).
554 	 * A.B.C	schema A, table B, col or func name C.
555 	 * A.B.C.D	catalog A, schema B, table C, col or func D.
556 	 * A.*		A is an unqualified table name; means whole-row value.
557 	 * A.B.*	whole-row value of table B in schema A.
558 	 * A.B.C.*	whole-row value of table C in schema B in catalog A.
559 	 *
560 	 * We do not need to cope with bare "*"; that will only be accepted by
561 	 * the grammar at the top level of a SELECT list, and transformTargetList
562 	 * will take care of it before it ever gets here.  Also, "A.*" etc will
563 	 * be expanded by transformTargetList if they appear at SELECT top level,
564 	 * so here we are only going to see them as function or operator inputs.
565 	 *
566 	 * Currently, if a catalog name is given then it must equal the current
567 	 * database name; we check it here and then discard it.
568 	 *----------
569 	 */
570 	switch (list_length(cref->fields))
571 	{
572 		case 1:
573 			{
574 				Node	   *field1 = (Node *) linitial(cref->fields);
575 
576 				Assert(IsA(field1, String));
577 				colname = strVal(field1);
578 
579 				/* Try to identify as an unqualified column */
580 				node = colNameToVar(pstate, colname, false, cref->location);
581 
582 				if (node == NULL)
583 				{
584 					/*
585 					 * Not known as a column of any range-table entry.
586 					 *
587 					 * Try to find the name as a relation.  Note that only
588 					 * relations already entered into the rangetable will be
589 					 * recognized.
590 					 *
591 					 * This is a hack for backwards compatibility with
592 					 * PostQUEL-inspired syntax.  The preferred form now is
593 					 * "rel.*".
594 					 */
595 					nsitem = refnameNamespaceItem(pstate, NULL, colname,
596 												  cref->location,
597 												  &levels_up);
598 					if (nsitem)
599 						node = transformWholeRowRef(pstate, nsitem, levels_up,
600 													cref->location);
601 				}
602 				break;
603 			}
604 		case 2:
605 			{
606 				Node	   *field1 = (Node *) linitial(cref->fields);
607 				Node	   *field2 = (Node *) lsecond(cref->fields);
608 
609 				Assert(IsA(field1, String));
610 				relname = strVal(field1);
611 
612 				/* Locate the referenced nsitem */
613 				nsitem = refnameNamespaceItem(pstate, nspname, relname,
614 											  cref->location,
615 											  &levels_up);
616 				if (nsitem == NULL)
617 				{
618 					crerr = CRERR_NO_RTE;
619 					break;
620 				}
621 
622 				/* Whole-row reference? */
623 				if (IsA(field2, A_Star))
624 				{
625 					node = transformWholeRowRef(pstate, nsitem, levels_up,
626 												cref->location);
627 					break;
628 				}
629 
630 				Assert(IsA(field2, String));
631 				colname = strVal(field2);
632 
633 				/* Try to identify as a column of the nsitem */
634 				node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
635 										   cref->location);
636 				if (node == NULL)
637 				{
638 					/* Try it as a function call on the whole row */
639 					node = transformWholeRowRef(pstate, nsitem, levels_up,
640 												cref->location);
641 					node = ParseFuncOrColumn(pstate,
642 											 list_make1(makeString(colname)),
643 											 list_make1(node),
644 											 pstate->p_last_srf,
645 											 NULL,
646 											 false,
647 											 cref->location);
648 				}
649 				break;
650 			}
651 		case 3:
652 			{
653 				Node	   *field1 = (Node *) linitial(cref->fields);
654 				Node	   *field2 = (Node *) lsecond(cref->fields);
655 				Node	   *field3 = (Node *) lthird(cref->fields);
656 
657 				Assert(IsA(field1, String));
658 				nspname = strVal(field1);
659 				Assert(IsA(field2, String));
660 				relname = strVal(field2);
661 
662 				/* Locate the referenced nsitem */
663 				nsitem = refnameNamespaceItem(pstate, nspname, relname,
664 											  cref->location,
665 											  &levels_up);
666 				if (nsitem == NULL)
667 				{
668 					crerr = CRERR_NO_RTE;
669 					break;
670 				}
671 
672 				/* Whole-row reference? */
673 				if (IsA(field3, A_Star))
674 				{
675 					node = transformWholeRowRef(pstate, nsitem, levels_up,
676 												cref->location);
677 					break;
678 				}
679 
680 				Assert(IsA(field3, String));
681 				colname = strVal(field3);
682 
683 				/* Try to identify as a column of the nsitem */
684 				node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
685 										   cref->location);
686 				if (node == NULL)
687 				{
688 					/* Try it as a function call on the whole row */
689 					node = transformWholeRowRef(pstate, nsitem, levels_up,
690 												cref->location);
691 					node = ParseFuncOrColumn(pstate,
692 											 list_make1(makeString(colname)),
693 											 list_make1(node),
694 											 pstate->p_last_srf,
695 											 NULL,
696 											 false,
697 											 cref->location);
698 				}
699 				break;
700 			}
701 		case 4:
702 			{
703 				Node	   *field1 = (Node *) linitial(cref->fields);
704 				Node	   *field2 = (Node *) lsecond(cref->fields);
705 				Node	   *field3 = (Node *) lthird(cref->fields);
706 				Node	   *field4 = (Node *) lfourth(cref->fields);
707 				char	   *catname;
708 
709 				Assert(IsA(field1, String));
710 				catname = strVal(field1);
711 				Assert(IsA(field2, String));
712 				nspname = strVal(field2);
713 				Assert(IsA(field3, String));
714 				relname = strVal(field3);
715 
716 				/*
717 				 * We check the catalog name and then ignore it.
718 				 */
719 				if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
720 				{
721 					crerr = CRERR_WRONG_DB;
722 					break;
723 				}
724 
725 				/* Locate the referenced nsitem */
726 				nsitem = refnameNamespaceItem(pstate, nspname, relname,
727 											  cref->location,
728 											  &levels_up);
729 				if (nsitem == NULL)
730 				{
731 					crerr = CRERR_NO_RTE;
732 					break;
733 				}
734 
735 				/* Whole-row reference? */
736 				if (IsA(field4, A_Star))
737 				{
738 					node = transformWholeRowRef(pstate, nsitem, levels_up,
739 												cref->location);
740 					break;
741 				}
742 
743 				Assert(IsA(field4, String));
744 				colname = strVal(field4);
745 
746 				/* Try to identify as a column of the nsitem */
747 				node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
748 										   cref->location);
749 				if (node == NULL)
750 				{
751 					/* Try it as a function call on the whole row */
752 					node = transformWholeRowRef(pstate, nsitem, levels_up,
753 												cref->location);
754 					node = ParseFuncOrColumn(pstate,
755 											 list_make1(makeString(colname)),
756 											 list_make1(node),
757 											 pstate->p_last_srf,
758 											 NULL,
759 											 false,
760 											 cref->location);
761 				}
762 				break;
763 			}
764 		default:
765 			crerr = CRERR_TOO_MANY; /* too many dotted names */
766 			break;
767 	}
768 
769 	/*
770 	 * Now give the PostParseColumnRefHook, if any, a chance.  We pass the
771 	 * translation-so-far so that it can throw an error if it wishes in the
772 	 * case that it has a conflicting interpretation of the ColumnRef. (If it
773 	 * just translates anyway, we'll throw an error, because we can't undo
774 	 * whatever effects the preceding steps may have had on the pstate.) If it
775 	 * returns NULL, use the standard translation, or throw a suitable error
776 	 * if there is none.
777 	 */
778 	if (pstate->p_post_columnref_hook != NULL)
779 	{
780 		Node	   *hookresult;
781 
782 		hookresult = pstate->p_post_columnref_hook(pstate, cref, node);
783 		if (node == NULL)
784 			node = hookresult;
785 		else if (hookresult != NULL)
786 			ereport(ERROR,
787 					(errcode(ERRCODE_AMBIGUOUS_COLUMN),
788 					 errmsg("column reference \"%s\" is ambiguous",
789 							NameListToString(cref->fields)),
790 					 parser_errposition(pstate, cref->location)));
791 	}
792 
793 	/*
794 	 * Throw error if no translation found.
795 	 */
796 	if (node == NULL)
797 	{
798 		switch (crerr)
799 		{
800 			case CRERR_NO_COLUMN:
801 				errorMissingColumn(pstate, relname, colname, cref->location);
802 				break;
803 			case CRERR_NO_RTE:
804 				errorMissingRTE(pstate, makeRangeVar(nspname, relname,
805 													 cref->location));
806 				break;
807 			case CRERR_WRONG_DB:
808 				ereport(ERROR,
809 						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
810 						 errmsg("cross-database references are not implemented: %s",
811 								NameListToString(cref->fields)),
812 						 parser_errposition(pstate, cref->location)));
813 				break;
814 			case CRERR_TOO_MANY:
815 				ereport(ERROR,
816 						(errcode(ERRCODE_SYNTAX_ERROR),
817 						 errmsg("improper qualified name (too many dotted names): %s",
818 								NameListToString(cref->fields)),
819 						 parser_errposition(pstate, cref->location)));
820 				break;
821 		}
822 	}
823 
824 	return node;
825 }
826 
827 static Node *
transformParamRef(ParseState * pstate,ParamRef * pref)828 transformParamRef(ParseState *pstate, ParamRef *pref)
829 {
830 	Node	   *result;
831 
832 	/*
833 	 * The core parser knows nothing about Params.  If a hook is supplied,
834 	 * call it.  If not, or if the hook returns NULL, throw a generic error.
835 	 */
836 	if (pstate->p_paramref_hook != NULL)
837 		result = pstate->p_paramref_hook(pstate, pref);
838 	else
839 		result = NULL;
840 
841 	if (result == NULL)
842 		ereport(ERROR,
843 				(errcode(ERRCODE_UNDEFINED_PARAMETER),
844 				 errmsg("there is no parameter $%d", pref->number),
845 				 parser_errposition(pstate, pref->location)));
846 
847 	return result;
848 }
849 
850 /* Test whether an a_expr is a plain NULL constant or not */
851 static bool
exprIsNullConstant(Node * arg)852 exprIsNullConstant(Node *arg)
853 {
854 	if (arg && IsA(arg, A_Const))
855 	{
856 		A_Const    *con = (A_Const *) arg;
857 
858 		if (con->val.type == T_Null)
859 			return true;
860 	}
861 	return false;
862 }
863 
864 static Node *
transformAExprOp(ParseState * pstate,A_Expr * a)865 transformAExprOp(ParseState *pstate, A_Expr *a)
866 {
867 	Node	   *lexpr = a->lexpr;
868 	Node	   *rexpr = a->rexpr;
869 	Node	   *result;
870 
871 	/*
872 	 * Special-case "foo = NULL" and "NULL = foo" for compatibility with
873 	 * standards-broken products (like Microsoft's).  Turn these into IS NULL
874 	 * exprs. (If either side is a CaseTestExpr, then the expression was
875 	 * generated internally from a CASE-WHEN expression, and
876 	 * transform_null_equals does not apply.)
877 	 */
878 	if (Transform_null_equals &&
879 		list_length(a->name) == 1 &&
880 		strcmp(strVal(linitial(a->name)), "=") == 0 &&
881 		(exprIsNullConstant(lexpr) || exprIsNullConstant(rexpr)) &&
882 		(!IsA(lexpr, CaseTestExpr) && !IsA(rexpr, CaseTestExpr)))
883 	{
884 		NullTest   *n = makeNode(NullTest);
885 
886 		n->nulltesttype = IS_NULL;
887 		n->location = a->location;
888 
889 		if (exprIsNullConstant(lexpr))
890 			n->arg = (Expr *) rexpr;
891 		else
892 			n->arg = (Expr *) lexpr;
893 
894 		result = transformExprRecurse(pstate, (Node *) n);
895 	}
896 	else if (lexpr && IsA(lexpr, RowExpr) &&
897 			 rexpr && IsA(rexpr, SubLink) &&
898 			 ((SubLink *) rexpr)->subLinkType == EXPR_SUBLINK)
899 	{
900 		/*
901 		 * Convert "row op subselect" into a ROWCOMPARE sublink. Formerly the
902 		 * grammar did this, but now that a row construct is allowed anywhere
903 		 * in expressions, it's easier to do it here.
904 		 */
905 		SubLink    *s = (SubLink *) rexpr;
906 
907 		s->subLinkType = ROWCOMPARE_SUBLINK;
908 		s->testexpr = lexpr;
909 		s->operName = a->name;
910 		s->location = a->location;
911 		result = transformExprRecurse(pstate, (Node *) s);
912 	}
913 	else if (lexpr && IsA(lexpr, RowExpr) &&
914 			 rexpr && IsA(rexpr, RowExpr))
915 	{
916 		/* ROW() op ROW() is handled specially */
917 		lexpr = transformExprRecurse(pstate, lexpr);
918 		rexpr = transformExprRecurse(pstate, rexpr);
919 
920 		result = make_row_comparison_op(pstate,
921 										a->name,
922 										castNode(RowExpr, lexpr)->args,
923 										castNode(RowExpr, rexpr)->args,
924 										a->location);
925 	}
926 	else
927 	{
928 		/* Ordinary scalar operator */
929 		Node	   *last_srf = pstate->p_last_srf;
930 
931 		lexpr = transformExprRecurse(pstate, lexpr);
932 		rexpr = transformExprRecurse(pstate, rexpr);
933 
934 		result = (Node *) make_op(pstate,
935 								  a->name,
936 								  lexpr,
937 								  rexpr,
938 								  last_srf,
939 								  a->location);
940 	}
941 
942 	return result;
943 }
944 
945 static Node *
transformAExprOpAny(ParseState * pstate,A_Expr * a)946 transformAExprOpAny(ParseState *pstate, A_Expr *a)
947 {
948 	Node	   *lexpr = transformExprRecurse(pstate, a->lexpr);
949 	Node	   *rexpr = transformExprRecurse(pstate, a->rexpr);
950 
951 	return (Node *) make_scalar_array_op(pstate,
952 										 a->name,
953 										 true,
954 										 lexpr,
955 										 rexpr,
956 										 a->location);
957 }
958 
959 static Node *
transformAExprOpAll(ParseState * pstate,A_Expr * a)960 transformAExprOpAll(ParseState *pstate, A_Expr *a)
961 {
962 	Node	   *lexpr = transformExprRecurse(pstate, a->lexpr);
963 	Node	   *rexpr = transformExprRecurse(pstate, a->rexpr);
964 
965 	return (Node *) make_scalar_array_op(pstate,
966 										 a->name,
967 										 false,
968 										 lexpr,
969 										 rexpr,
970 										 a->location);
971 }
972 
973 static Node *
transformAExprDistinct(ParseState * pstate,A_Expr * a)974 transformAExprDistinct(ParseState *pstate, A_Expr *a)
975 {
976 	Node	   *lexpr = a->lexpr;
977 	Node	   *rexpr = a->rexpr;
978 	Node	   *result;
979 
980 	/*
981 	 * If either input is an undecorated NULL literal, transform to a NullTest
982 	 * on the other input. That's simpler to process than a full DistinctExpr,
983 	 * and it avoids needing to require that the datatype have an = operator.
984 	 */
985 	if (exprIsNullConstant(rexpr))
986 		return make_nulltest_from_distinct(pstate, a, lexpr);
987 	if (exprIsNullConstant(lexpr))
988 		return make_nulltest_from_distinct(pstate, a, rexpr);
989 
990 	lexpr = transformExprRecurse(pstate, lexpr);
991 	rexpr = transformExprRecurse(pstate, rexpr);
992 
993 	if (lexpr && IsA(lexpr, RowExpr) &&
994 		rexpr && IsA(rexpr, RowExpr))
995 	{
996 		/* ROW() op ROW() is handled specially */
997 		result = make_row_distinct_op(pstate, a->name,
998 									  (RowExpr *) lexpr,
999 									  (RowExpr *) rexpr,
1000 									  a->location);
1001 	}
1002 	else
1003 	{
1004 		/* Ordinary scalar operator */
1005 		result = (Node *) make_distinct_op(pstate,
1006 										   a->name,
1007 										   lexpr,
1008 										   rexpr,
1009 										   a->location);
1010 	}
1011 
1012 	/*
1013 	 * If it's NOT DISTINCT, we first build a DistinctExpr and then stick a
1014 	 * NOT on top.
1015 	 */
1016 	if (a->kind == AEXPR_NOT_DISTINCT)
1017 		result = (Node *) makeBoolExpr(NOT_EXPR,
1018 									   list_make1(result),
1019 									   a->location);
1020 
1021 	return result;
1022 }
1023 
1024 static Node *
transformAExprNullIf(ParseState * pstate,A_Expr * a)1025 transformAExprNullIf(ParseState *pstate, A_Expr *a)
1026 {
1027 	Node	   *lexpr = transformExprRecurse(pstate, a->lexpr);
1028 	Node	   *rexpr = transformExprRecurse(pstate, a->rexpr);
1029 	OpExpr	   *result;
1030 
1031 	result = (OpExpr *) make_op(pstate,
1032 								a->name,
1033 								lexpr,
1034 								rexpr,
1035 								pstate->p_last_srf,
1036 								a->location);
1037 
1038 	/*
1039 	 * The comparison operator itself should yield boolean ...
1040 	 */
1041 	if (result->opresulttype != BOOLOID)
1042 		ereport(ERROR,
1043 				(errcode(ERRCODE_DATATYPE_MISMATCH),
1044 				 errmsg("NULLIF requires = operator to yield boolean"),
1045 				 parser_errposition(pstate, a->location)));
1046 	if (result->opretset)
1047 		ereport(ERROR,
1048 				(errcode(ERRCODE_DATATYPE_MISMATCH),
1049 		/* translator: %s is name of a SQL construct, eg NULLIF */
1050 				 errmsg("%s must not return a set", "NULLIF"),
1051 				 parser_errposition(pstate, a->location)));
1052 
1053 	/*
1054 	 * ... but the NullIfExpr will yield the first operand's type.
1055 	 */
1056 	result->opresulttype = exprType((Node *) linitial(result->args));
1057 
1058 	/*
1059 	 * We rely on NullIfExpr and OpExpr being the same struct
1060 	 */
1061 	NodeSetTag(result, T_NullIfExpr);
1062 
1063 	return (Node *) result;
1064 }
1065 
1066 static Node *
transformAExprIn(ParseState * pstate,A_Expr * a)1067 transformAExprIn(ParseState *pstate, A_Expr *a)
1068 {
1069 	Node	   *result = NULL;
1070 	Node	   *lexpr;
1071 	List	   *rexprs;
1072 	List	   *rvars;
1073 	List	   *rnonvars;
1074 	bool		useOr;
1075 	ListCell   *l;
1076 
1077 	/*
1078 	 * If the operator is <>, combine with AND not OR.
1079 	 */
1080 	if (strcmp(strVal(linitial(a->name)), "<>") == 0)
1081 		useOr = false;
1082 	else
1083 		useOr = true;
1084 
1085 	/*
1086 	 * We try to generate a ScalarArrayOpExpr from IN/NOT IN, but this is only
1087 	 * possible if there is a suitable array type available.  If not, we fall
1088 	 * back to a boolean condition tree with multiple copies of the lefthand
1089 	 * expression.  Also, any IN-list items that contain Vars are handled as
1090 	 * separate boolean conditions, because that gives the planner more scope
1091 	 * for optimization on such clauses.
1092 	 *
1093 	 * First step: transform all the inputs, and detect whether any contain
1094 	 * Vars.
1095 	 */
1096 	lexpr = transformExprRecurse(pstate, a->lexpr);
1097 	rexprs = rvars = rnonvars = NIL;
1098 	foreach(l, (List *) a->rexpr)
1099 	{
1100 		Node	   *rexpr = transformExprRecurse(pstate, lfirst(l));
1101 
1102 		rexprs = lappend(rexprs, rexpr);
1103 		if (contain_vars_of_level(rexpr, 0))
1104 			rvars = lappend(rvars, rexpr);
1105 		else
1106 			rnonvars = lappend(rnonvars, rexpr);
1107 	}
1108 
1109 	/*
1110 	 * ScalarArrayOpExpr is only going to be useful if there's more than one
1111 	 * non-Var righthand item.
1112 	 */
1113 	if (list_length(rnonvars) > 1)
1114 	{
1115 		List	   *allexprs;
1116 		Oid			scalar_type;
1117 		Oid			array_type;
1118 
1119 		/*
1120 		 * Try to select a common type for the array elements.  Note that
1121 		 * since the LHS' type is first in the list, it will be preferred when
1122 		 * there is doubt (eg, when all the RHS items are unknown literals).
1123 		 *
1124 		 * Note: use list_concat here not lcons, to avoid damaging rnonvars.
1125 		 */
1126 		allexprs = list_concat(list_make1(lexpr), rnonvars);
1127 		scalar_type = select_common_type(pstate, allexprs, NULL, NULL);
1128 
1129 		/*
1130 		 * Do we have an array type to use?  Aside from the case where there
1131 		 * isn't one, we don't risk using ScalarArrayOpExpr when the common
1132 		 * type is RECORD, because the RowExpr comparison logic below can cope
1133 		 * with some cases of non-identical row types.
1134 		 */
1135 		if (OidIsValid(scalar_type) && scalar_type != RECORDOID)
1136 			array_type = get_array_type(scalar_type);
1137 		else
1138 			array_type = InvalidOid;
1139 		if (array_type != InvalidOid)
1140 		{
1141 			/*
1142 			 * OK: coerce all the right-hand non-Var inputs to the common type
1143 			 * and build an ArrayExpr for them.
1144 			 */
1145 			List	   *aexprs;
1146 			ArrayExpr  *newa;
1147 
1148 			aexprs = NIL;
1149 			foreach(l, rnonvars)
1150 			{
1151 				Node	   *rexpr = (Node *) lfirst(l);
1152 
1153 				rexpr = coerce_to_common_type(pstate, rexpr,
1154 											  scalar_type,
1155 											  "IN");
1156 				aexprs = lappend(aexprs, rexpr);
1157 			}
1158 			newa = makeNode(ArrayExpr);
1159 			newa->array_typeid = array_type;
1160 			/* array_collid will be set by parse_collate.c */
1161 			newa->element_typeid = scalar_type;
1162 			newa->elements = aexprs;
1163 			newa->multidims = false;
1164 			newa->location = -1;
1165 
1166 			result = (Node *) make_scalar_array_op(pstate,
1167 												   a->name,
1168 												   useOr,
1169 												   lexpr,
1170 												   (Node *) newa,
1171 												   a->location);
1172 
1173 			/* Consider only the Vars (if any) in the loop below */
1174 			rexprs = rvars;
1175 		}
1176 	}
1177 
1178 	/*
1179 	 * Must do it the hard way, ie, with a boolean expression tree.
1180 	 */
1181 	foreach(l, rexprs)
1182 	{
1183 		Node	   *rexpr = (Node *) lfirst(l);
1184 		Node	   *cmp;
1185 
1186 		if (IsA(lexpr, RowExpr) &&
1187 			IsA(rexpr, RowExpr))
1188 		{
1189 			/* ROW() op ROW() is handled specially */
1190 			cmp = make_row_comparison_op(pstate,
1191 										 a->name,
1192 										 copyObject(((RowExpr *) lexpr)->args),
1193 										 ((RowExpr *) rexpr)->args,
1194 										 a->location);
1195 		}
1196 		else
1197 		{
1198 			/* Ordinary scalar operator */
1199 			cmp = (Node *) make_op(pstate,
1200 								   a->name,
1201 								   copyObject(lexpr),
1202 								   rexpr,
1203 								   pstate->p_last_srf,
1204 								   a->location);
1205 		}
1206 
1207 		cmp = coerce_to_boolean(pstate, cmp, "IN");
1208 		if (result == NULL)
1209 			result = cmp;
1210 		else
1211 			result = (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
1212 										   list_make2(result, cmp),
1213 										   a->location);
1214 	}
1215 
1216 	return result;
1217 }
1218 
1219 static Node *
transformAExprBetween(ParseState * pstate,A_Expr * a)1220 transformAExprBetween(ParseState *pstate, A_Expr *a)
1221 {
1222 	Node	   *aexpr;
1223 	Node	   *bexpr;
1224 	Node	   *cexpr;
1225 	Node	   *result;
1226 	Node	   *sub1;
1227 	Node	   *sub2;
1228 	List	   *args;
1229 
1230 	/* Deconstruct A_Expr into three subexprs */
1231 	aexpr = a->lexpr;
1232 	args = castNode(List, a->rexpr);
1233 	Assert(list_length(args) == 2);
1234 	bexpr = (Node *) linitial(args);
1235 	cexpr = (Node *) lsecond(args);
1236 
1237 	/*
1238 	 * Build the equivalent comparison expression.  Make copies of
1239 	 * multiply-referenced subexpressions for safety.  (XXX this is really
1240 	 * wrong since it results in multiple runtime evaluations of what may be
1241 	 * volatile expressions ...)
1242 	 *
1243 	 * Ideally we would not use hard-wired operators here but instead use
1244 	 * opclasses.  However, mixed data types and other issues make this
1245 	 * difficult:
1246 	 * http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
1247 	 */
1248 	switch (a->kind)
1249 	{
1250 		case AEXPR_BETWEEN:
1251 			args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1252 											   aexpr, bexpr,
1253 											   a->location),
1254 							  makeSimpleA_Expr(AEXPR_OP, "<=",
1255 											   copyObject(aexpr), cexpr,
1256 											   a->location));
1257 			result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1258 			break;
1259 		case AEXPR_NOT_BETWEEN:
1260 			args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1261 											   aexpr, bexpr,
1262 											   a->location),
1263 							  makeSimpleA_Expr(AEXPR_OP, ">",
1264 											   copyObject(aexpr), cexpr,
1265 											   a->location));
1266 			result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1267 			break;
1268 		case AEXPR_BETWEEN_SYM:
1269 			args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1270 											   aexpr, bexpr,
1271 											   a->location),
1272 							  makeSimpleA_Expr(AEXPR_OP, "<=",
1273 											   copyObject(aexpr), cexpr,
1274 											   a->location));
1275 			sub1 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1276 			args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1277 											   copyObject(aexpr), copyObject(cexpr),
1278 											   a->location),
1279 							  makeSimpleA_Expr(AEXPR_OP, "<=",
1280 											   copyObject(aexpr), copyObject(bexpr),
1281 											   a->location));
1282 			sub2 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1283 			args = list_make2(sub1, sub2);
1284 			result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1285 			break;
1286 		case AEXPR_NOT_BETWEEN_SYM:
1287 			args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1288 											   aexpr, bexpr,
1289 											   a->location),
1290 							  makeSimpleA_Expr(AEXPR_OP, ">",
1291 											   copyObject(aexpr), cexpr,
1292 											   a->location));
1293 			sub1 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1294 			args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1295 											   copyObject(aexpr), copyObject(cexpr),
1296 											   a->location),
1297 							  makeSimpleA_Expr(AEXPR_OP, ">",
1298 											   copyObject(aexpr), copyObject(bexpr),
1299 											   a->location));
1300 			sub2 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1301 			args = list_make2(sub1, sub2);
1302 			result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1303 			break;
1304 		default:
1305 			elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
1306 			result = NULL;		/* keep compiler quiet */
1307 			break;
1308 	}
1309 
1310 	return transformExprRecurse(pstate, result);
1311 }
1312 
1313 static Node *
transformBoolExpr(ParseState * pstate,BoolExpr * a)1314 transformBoolExpr(ParseState *pstate, BoolExpr *a)
1315 {
1316 	List	   *args = NIL;
1317 	const char *opname;
1318 	ListCell   *lc;
1319 
1320 	switch (a->boolop)
1321 	{
1322 		case AND_EXPR:
1323 			opname = "AND";
1324 			break;
1325 		case OR_EXPR:
1326 			opname = "OR";
1327 			break;
1328 		case NOT_EXPR:
1329 			opname = "NOT";
1330 			break;
1331 		default:
1332 			elog(ERROR, "unrecognized boolop: %d", (int) a->boolop);
1333 			opname = NULL;		/* keep compiler quiet */
1334 			break;
1335 	}
1336 
1337 	foreach(lc, a->args)
1338 	{
1339 		Node	   *arg = (Node *) lfirst(lc);
1340 
1341 		arg = transformExprRecurse(pstate, arg);
1342 		arg = coerce_to_boolean(pstate, arg, opname);
1343 		args = lappend(args, arg);
1344 	}
1345 
1346 	return (Node *) makeBoolExpr(a->boolop, args, a->location);
1347 }
1348 
1349 static Node *
transformFuncCall(ParseState * pstate,FuncCall * fn)1350 transformFuncCall(ParseState *pstate, FuncCall *fn)
1351 {
1352 	Node	   *last_srf = pstate->p_last_srf;
1353 	List	   *targs;
1354 	ListCell   *args;
1355 
1356 	/* Transform the list of arguments ... */
1357 	targs = NIL;
1358 	foreach(args, fn->args)
1359 	{
1360 		targs = lappend(targs, transformExprRecurse(pstate,
1361 													(Node *) lfirst(args)));
1362 	}
1363 
1364 	/*
1365 	 * When WITHIN GROUP is used, we treat its ORDER BY expressions as
1366 	 * additional arguments to the function, for purposes of function lookup
1367 	 * and argument type coercion.  So, transform each such expression and add
1368 	 * them to the targs list.  We don't explicitly mark where each argument
1369 	 * came from, but ParseFuncOrColumn can tell what's what by reference to
1370 	 * list_length(fn->agg_order).
1371 	 */
1372 	if (fn->agg_within_group)
1373 	{
1374 		Assert(fn->agg_order != NIL);
1375 		foreach(args, fn->agg_order)
1376 		{
1377 			SortBy	   *arg = (SortBy *) lfirst(args);
1378 
1379 			targs = lappend(targs, transformExpr(pstate, arg->node,
1380 												 EXPR_KIND_ORDER_BY));
1381 		}
1382 	}
1383 
1384 	/* ... and hand off to ParseFuncOrColumn */
1385 	return ParseFuncOrColumn(pstate,
1386 							 fn->funcname,
1387 							 targs,
1388 							 last_srf,
1389 							 fn,
1390 							 false,
1391 							 fn->location);
1392 }
1393 
1394 static Node *
transformMultiAssignRef(ParseState * pstate,MultiAssignRef * maref)1395 transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref)
1396 {
1397 	SubLink    *sublink;
1398 	RowExpr    *rexpr;
1399 	Query	   *qtree;
1400 	TargetEntry *tle;
1401 
1402 	/* We should only see this in first-stage processing of UPDATE tlists */
1403 	Assert(pstate->p_expr_kind == EXPR_KIND_UPDATE_SOURCE);
1404 
1405 	/* We only need to transform the source if this is the first column */
1406 	if (maref->colno == 1)
1407 	{
1408 		/*
1409 		 * For now, we only allow EXPR SubLinks and RowExprs as the source of
1410 		 * an UPDATE multiassignment.  This is sufficient to cover interesting
1411 		 * cases; at worst, someone would have to write (SELECT * FROM expr)
1412 		 * to expand a composite-returning expression of another form.
1413 		 */
1414 		if (IsA(maref->source, SubLink) &&
1415 			((SubLink *) maref->source)->subLinkType == EXPR_SUBLINK)
1416 		{
1417 			/* Relabel it as a MULTIEXPR_SUBLINK */
1418 			sublink = (SubLink *) maref->source;
1419 			sublink->subLinkType = MULTIEXPR_SUBLINK;
1420 			/* And transform it */
1421 			sublink = (SubLink *) transformExprRecurse(pstate,
1422 													   (Node *) sublink);
1423 
1424 			qtree = castNode(Query, sublink->subselect);
1425 
1426 			/* Check subquery returns required number of columns */
1427 			if (count_nonjunk_tlist_entries(qtree->targetList) != maref->ncolumns)
1428 				ereport(ERROR,
1429 						(errcode(ERRCODE_SYNTAX_ERROR),
1430 						 errmsg("number of columns does not match number of values"),
1431 						 parser_errposition(pstate, sublink->location)));
1432 
1433 			/*
1434 			 * Build a resjunk tlist item containing the MULTIEXPR SubLink,
1435 			 * and add it to pstate->p_multiassign_exprs, whence it will later
1436 			 * get appended to the completed targetlist.  We needn't worry
1437 			 * about selecting a resno for it; transformUpdateStmt will do
1438 			 * that.
1439 			 */
1440 			tle = makeTargetEntry((Expr *) sublink, 0, NULL, true);
1441 			pstate->p_multiassign_exprs = lappend(pstate->p_multiassign_exprs,
1442 												  tle);
1443 
1444 			/*
1445 			 * Assign a unique-within-this-targetlist ID to the MULTIEXPR
1446 			 * SubLink.  We can just use its position in the
1447 			 * p_multiassign_exprs list.
1448 			 */
1449 			sublink->subLinkId = list_length(pstate->p_multiassign_exprs);
1450 		}
1451 		else if (IsA(maref->source, RowExpr))
1452 		{
1453 			/* Transform the RowExpr, allowing SetToDefault items */
1454 			rexpr = (RowExpr *) transformRowExpr(pstate,
1455 												 (RowExpr *) maref->source,
1456 												 true);
1457 
1458 			/* Check it returns required number of columns */
1459 			if (list_length(rexpr->args) != maref->ncolumns)
1460 				ereport(ERROR,
1461 						(errcode(ERRCODE_SYNTAX_ERROR),
1462 						 errmsg("number of columns does not match number of values"),
1463 						 parser_errposition(pstate, rexpr->location)));
1464 
1465 			/*
1466 			 * Temporarily append it to p_multiassign_exprs, so we can get it
1467 			 * back when we come back here for additional columns.
1468 			 */
1469 			tle = makeTargetEntry((Expr *) rexpr, 0, NULL, true);
1470 			pstate->p_multiassign_exprs = lappend(pstate->p_multiassign_exprs,
1471 												  tle);
1472 		}
1473 		else
1474 			ereport(ERROR,
1475 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1476 					 errmsg("source for a multiple-column UPDATE item must be a sub-SELECT or ROW() expression"),
1477 					 parser_errposition(pstate, exprLocation(maref->source))));
1478 	}
1479 	else
1480 	{
1481 		/*
1482 		 * Second or later column in a multiassignment.  Re-fetch the
1483 		 * transformed SubLink or RowExpr, which we assume is still the last
1484 		 * entry in p_multiassign_exprs.
1485 		 */
1486 		Assert(pstate->p_multiassign_exprs != NIL);
1487 		tle = (TargetEntry *) llast(pstate->p_multiassign_exprs);
1488 	}
1489 
1490 	/*
1491 	 * Emit the appropriate output expression for the current column
1492 	 */
1493 	if (IsA(tle->expr, SubLink))
1494 	{
1495 		Param	   *param;
1496 
1497 		sublink = (SubLink *) tle->expr;
1498 		Assert(sublink->subLinkType == MULTIEXPR_SUBLINK);
1499 		qtree = castNode(Query, sublink->subselect);
1500 
1501 		/* Build a Param representing the current subquery output column */
1502 		tle = (TargetEntry *) list_nth(qtree->targetList, maref->colno - 1);
1503 		Assert(!tle->resjunk);
1504 
1505 		param = makeNode(Param);
1506 		param->paramkind = PARAM_MULTIEXPR;
1507 		param->paramid = (sublink->subLinkId << 16) | maref->colno;
1508 		param->paramtype = exprType((Node *) tle->expr);
1509 		param->paramtypmod = exprTypmod((Node *) tle->expr);
1510 		param->paramcollid = exprCollation((Node *) tle->expr);
1511 		param->location = exprLocation((Node *) tle->expr);
1512 
1513 		return (Node *) param;
1514 	}
1515 
1516 	if (IsA(tle->expr, RowExpr))
1517 	{
1518 		Node	   *result;
1519 
1520 		rexpr = (RowExpr *) tle->expr;
1521 
1522 		/* Just extract and return the next element of the RowExpr */
1523 		result = (Node *) list_nth(rexpr->args, maref->colno - 1);
1524 
1525 		/*
1526 		 * If we're at the last column, delete the RowExpr from
1527 		 * p_multiassign_exprs; we don't need it anymore, and don't want it in
1528 		 * the finished UPDATE tlist.  We assume this is still the last entry
1529 		 * in p_multiassign_exprs.
1530 		 */
1531 		if (maref->colno == maref->ncolumns)
1532 			pstate->p_multiassign_exprs =
1533 				list_delete_last(pstate->p_multiassign_exprs);
1534 
1535 		return result;
1536 	}
1537 
1538 	elog(ERROR, "unexpected expr type in multiassign list");
1539 	return NULL;				/* keep compiler quiet */
1540 }
1541 
1542 static Node *
transformCaseExpr(ParseState * pstate,CaseExpr * c)1543 transformCaseExpr(ParseState *pstate, CaseExpr *c)
1544 {
1545 	CaseExpr   *newc = makeNode(CaseExpr);
1546 	Node	   *last_srf = pstate->p_last_srf;
1547 	Node	   *arg;
1548 	CaseTestExpr *placeholder;
1549 	List	   *newargs;
1550 	List	   *resultexprs;
1551 	ListCell   *l;
1552 	Node	   *defresult;
1553 	Oid			ptype;
1554 
1555 	/* transform the test expression, if any */
1556 	arg = transformExprRecurse(pstate, (Node *) c->arg);
1557 
1558 	/* generate placeholder for test expression */
1559 	if (arg)
1560 	{
1561 		/*
1562 		 * If test expression is an untyped literal, force it to text. We have
1563 		 * to do something now because we won't be able to do this coercion on
1564 		 * the placeholder.  This is not as flexible as what was done in 7.4
1565 		 * and before, but it's good enough to handle the sort of silly coding
1566 		 * commonly seen.
1567 		 */
1568 		if (exprType(arg) == UNKNOWNOID)
1569 			arg = coerce_to_common_type(pstate, arg, TEXTOID, "CASE");
1570 
1571 		/*
1572 		 * Run collation assignment on the test expression so that we know
1573 		 * what collation to mark the placeholder with.  In principle we could
1574 		 * leave it to parse_collate.c to do that later, but propagating the
1575 		 * result to the CaseTestExpr would be unnecessarily complicated.
1576 		 */
1577 		assign_expr_collations(pstate, arg);
1578 
1579 		placeholder = makeNode(CaseTestExpr);
1580 		placeholder->typeId = exprType(arg);
1581 		placeholder->typeMod = exprTypmod(arg);
1582 		placeholder->collation = exprCollation(arg);
1583 	}
1584 	else
1585 		placeholder = NULL;
1586 
1587 	newc->arg = (Expr *) arg;
1588 
1589 	/* transform the list of arguments */
1590 	newargs = NIL;
1591 	resultexprs = NIL;
1592 	foreach(l, c->args)
1593 	{
1594 		CaseWhen   *w = lfirst_node(CaseWhen, l);
1595 		CaseWhen   *neww = makeNode(CaseWhen);
1596 		Node	   *warg;
1597 
1598 		warg = (Node *) w->expr;
1599 		if (placeholder)
1600 		{
1601 			/* shorthand form was specified, so expand... */
1602 			warg = (Node *) makeSimpleA_Expr(AEXPR_OP, "=",
1603 											 (Node *) placeholder,
1604 											 warg,
1605 											 w->location);
1606 		}
1607 		neww->expr = (Expr *) transformExprRecurse(pstate, warg);
1608 
1609 		neww->expr = (Expr *) coerce_to_boolean(pstate,
1610 												(Node *) neww->expr,
1611 												"CASE/WHEN");
1612 
1613 		warg = (Node *) w->result;
1614 		neww->result = (Expr *) transformExprRecurse(pstate, warg);
1615 		neww->location = w->location;
1616 
1617 		newargs = lappend(newargs, neww);
1618 		resultexprs = lappend(resultexprs, neww->result);
1619 	}
1620 
1621 	newc->args = newargs;
1622 
1623 	/* transform the default clause */
1624 	defresult = (Node *) c->defresult;
1625 	if (defresult == NULL)
1626 	{
1627 		A_Const    *n = makeNode(A_Const);
1628 
1629 		n->val.type = T_Null;
1630 		n->location = -1;
1631 		defresult = (Node *) n;
1632 	}
1633 	newc->defresult = (Expr *) transformExprRecurse(pstate, defresult);
1634 
1635 	/*
1636 	 * Note: default result is considered the most significant type in
1637 	 * determining preferred type. This is how the code worked before, but it
1638 	 * seems a little bogus to me --- tgl
1639 	 */
1640 	resultexprs = lcons(newc->defresult, resultexprs);
1641 
1642 	ptype = select_common_type(pstate, resultexprs, "CASE", NULL);
1643 	Assert(OidIsValid(ptype));
1644 	newc->casetype = ptype;
1645 	/* casecollid will be set by parse_collate.c */
1646 
1647 	/* Convert default result clause, if necessary */
1648 	newc->defresult = (Expr *)
1649 		coerce_to_common_type(pstate,
1650 							  (Node *) newc->defresult,
1651 							  ptype,
1652 							  "CASE/ELSE");
1653 
1654 	/* Convert when-clause results, if necessary */
1655 	foreach(l, newc->args)
1656 	{
1657 		CaseWhen   *w = (CaseWhen *) lfirst(l);
1658 
1659 		w->result = (Expr *)
1660 			coerce_to_common_type(pstate,
1661 								  (Node *) w->result,
1662 								  ptype,
1663 								  "CASE/WHEN");
1664 	}
1665 
1666 	/* if any subexpression contained a SRF, complain */
1667 	if (pstate->p_last_srf != last_srf)
1668 		ereport(ERROR,
1669 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1670 		/* translator: %s is name of a SQL construct, eg GROUP BY */
1671 				 errmsg("set-returning functions are not allowed in %s",
1672 						"CASE"),
1673 				 errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
1674 				 parser_errposition(pstate,
1675 									exprLocation(pstate->p_last_srf))));
1676 
1677 	newc->location = c->location;
1678 
1679 	return (Node *) newc;
1680 }
1681 
1682 static Node *
transformSubLink(ParseState * pstate,SubLink * sublink)1683 transformSubLink(ParseState *pstate, SubLink *sublink)
1684 {
1685 	Node	   *result = (Node *) sublink;
1686 	Query	   *qtree;
1687 	const char *err;
1688 
1689 	/*
1690 	 * Check to see if the sublink is in an invalid place within the query. We
1691 	 * allow sublinks everywhere in SELECT/INSERT/UPDATE/DELETE, but generally
1692 	 * not in utility statements.
1693 	 */
1694 	err = NULL;
1695 	switch (pstate->p_expr_kind)
1696 	{
1697 		case EXPR_KIND_NONE:
1698 			Assert(false);		/* can't happen */
1699 			break;
1700 		case EXPR_KIND_OTHER:
1701 			/* Accept sublink here; caller must throw error if wanted */
1702 			break;
1703 		case EXPR_KIND_JOIN_ON:
1704 		case EXPR_KIND_JOIN_USING:
1705 		case EXPR_KIND_FROM_SUBSELECT:
1706 		case EXPR_KIND_FROM_FUNCTION:
1707 		case EXPR_KIND_WHERE:
1708 		case EXPR_KIND_POLICY:
1709 		case EXPR_KIND_HAVING:
1710 		case EXPR_KIND_FILTER:
1711 		case EXPR_KIND_WINDOW_PARTITION:
1712 		case EXPR_KIND_WINDOW_ORDER:
1713 		case EXPR_KIND_WINDOW_FRAME_RANGE:
1714 		case EXPR_KIND_WINDOW_FRAME_ROWS:
1715 		case EXPR_KIND_WINDOW_FRAME_GROUPS:
1716 		case EXPR_KIND_SELECT_TARGET:
1717 		case EXPR_KIND_INSERT_TARGET:
1718 		case EXPR_KIND_UPDATE_SOURCE:
1719 		case EXPR_KIND_UPDATE_TARGET:
1720 		case EXPR_KIND_GROUP_BY:
1721 		case EXPR_KIND_ORDER_BY:
1722 		case EXPR_KIND_DISTINCT_ON:
1723 		case EXPR_KIND_LIMIT:
1724 		case EXPR_KIND_OFFSET:
1725 		case EXPR_KIND_RETURNING:
1726 		case EXPR_KIND_VALUES:
1727 		case EXPR_KIND_VALUES_SINGLE:
1728 		case EXPR_KIND_CYCLE_MARK:
1729 			/* okay */
1730 			break;
1731 		case EXPR_KIND_CHECK_CONSTRAINT:
1732 		case EXPR_KIND_DOMAIN_CHECK:
1733 			err = _("cannot use subquery in check constraint");
1734 			break;
1735 		case EXPR_KIND_COLUMN_DEFAULT:
1736 		case EXPR_KIND_FUNCTION_DEFAULT:
1737 			err = _("cannot use subquery in DEFAULT expression");
1738 			break;
1739 		case EXPR_KIND_INDEX_EXPRESSION:
1740 			err = _("cannot use subquery in index expression");
1741 			break;
1742 		case EXPR_KIND_INDEX_PREDICATE:
1743 			err = _("cannot use subquery in index predicate");
1744 			break;
1745 		case EXPR_KIND_STATS_EXPRESSION:
1746 			err = _("cannot use subquery in statistics expression");
1747 			break;
1748 		case EXPR_KIND_ALTER_COL_TRANSFORM:
1749 			err = _("cannot use subquery in transform expression");
1750 			break;
1751 		case EXPR_KIND_EXECUTE_PARAMETER:
1752 			err = _("cannot use subquery in EXECUTE parameter");
1753 			break;
1754 		case EXPR_KIND_TRIGGER_WHEN:
1755 			err = _("cannot use subquery in trigger WHEN condition");
1756 			break;
1757 		case EXPR_KIND_PARTITION_BOUND:
1758 			err = _("cannot use subquery in partition bound");
1759 			break;
1760 		case EXPR_KIND_PARTITION_EXPRESSION:
1761 			err = _("cannot use subquery in partition key expression");
1762 			break;
1763 		case EXPR_KIND_CALL_ARGUMENT:
1764 			err = _("cannot use subquery in CALL argument");
1765 			break;
1766 		case EXPR_KIND_COPY_WHERE:
1767 			err = _("cannot use subquery in COPY FROM WHERE condition");
1768 			break;
1769 		case EXPR_KIND_GENERATED_COLUMN:
1770 			err = _("cannot use subquery in column generation expression");
1771 			break;
1772 
1773 			/*
1774 			 * There is intentionally no default: case here, so that the
1775 			 * compiler will warn if we add a new ParseExprKind without
1776 			 * extending this switch.  If we do see an unrecognized value at
1777 			 * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
1778 			 * which is sane anyway.
1779 			 */
1780 	}
1781 	if (err)
1782 		ereport(ERROR,
1783 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1784 				 errmsg_internal("%s", err),
1785 				 parser_errposition(pstate, sublink->location)));
1786 
1787 	pstate->p_hasSubLinks = true;
1788 
1789 	/*
1790 	 * OK, let's transform the sub-SELECT.
1791 	 */
1792 	qtree = parse_sub_analyze(sublink->subselect, pstate, NULL, false, true);
1793 
1794 	/*
1795 	 * Check that we got a SELECT.  Anything else should be impossible given
1796 	 * restrictions of the grammar, but check anyway.
1797 	 */
1798 	if (!IsA(qtree, Query) ||
1799 		qtree->commandType != CMD_SELECT)
1800 		elog(ERROR, "unexpected non-SELECT command in SubLink");
1801 
1802 	sublink->subselect = (Node *) qtree;
1803 
1804 	if (sublink->subLinkType == EXISTS_SUBLINK)
1805 	{
1806 		/*
1807 		 * EXISTS needs no test expression or combining operator. These fields
1808 		 * should be null already, but make sure.
1809 		 */
1810 		sublink->testexpr = NULL;
1811 		sublink->operName = NIL;
1812 	}
1813 	else if (sublink->subLinkType == EXPR_SUBLINK ||
1814 			 sublink->subLinkType == ARRAY_SUBLINK)
1815 	{
1816 		/*
1817 		 * Make sure the subselect delivers a single column (ignoring resjunk
1818 		 * targets).
1819 		 */
1820 		if (count_nonjunk_tlist_entries(qtree->targetList) != 1)
1821 			ereport(ERROR,
1822 					(errcode(ERRCODE_SYNTAX_ERROR),
1823 					 errmsg("subquery must return only one column"),
1824 					 parser_errposition(pstate, sublink->location)));
1825 
1826 		/*
1827 		 * EXPR and ARRAY need no test expression or combining operator. These
1828 		 * fields should be null already, but make sure.
1829 		 */
1830 		sublink->testexpr = NULL;
1831 		sublink->operName = NIL;
1832 	}
1833 	else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
1834 	{
1835 		/* Same as EXPR case, except no restriction on number of columns */
1836 		sublink->testexpr = NULL;
1837 		sublink->operName = NIL;
1838 	}
1839 	else
1840 	{
1841 		/* ALL, ANY, or ROWCOMPARE: generate row-comparing expression */
1842 		Node	   *lefthand;
1843 		List	   *left_list;
1844 		List	   *right_list;
1845 		ListCell   *l;
1846 
1847 		/*
1848 		 * If the source was "x IN (select)", convert to "x = ANY (select)".
1849 		 */
1850 		if (sublink->operName == NIL)
1851 			sublink->operName = list_make1(makeString("="));
1852 
1853 		/*
1854 		 * Transform lefthand expression, and convert to a list
1855 		 */
1856 		lefthand = transformExprRecurse(pstate, sublink->testexpr);
1857 		if (lefthand && IsA(lefthand, RowExpr))
1858 			left_list = ((RowExpr *) lefthand)->args;
1859 		else
1860 			left_list = list_make1(lefthand);
1861 
1862 		/*
1863 		 * Build a list of PARAM_SUBLINK nodes representing the output columns
1864 		 * of the subquery.
1865 		 */
1866 		right_list = NIL;
1867 		foreach(l, qtree->targetList)
1868 		{
1869 			TargetEntry *tent = (TargetEntry *) lfirst(l);
1870 			Param	   *param;
1871 
1872 			if (tent->resjunk)
1873 				continue;
1874 
1875 			param = makeNode(Param);
1876 			param->paramkind = PARAM_SUBLINK;
1877 			param->paramid = tent->resno;
1878 			param->paramtype = exprType((Node *) tent->expr);
1879 			param->paramtypmod = exprTypmod((Node *) tent->expr);
1880 			param->paramcollid = exprCollation((Node *) tent->expr);
1881 			param->location = -1;
1882 
1883 			right_list = lappend(right_list, param);
1884 		}
1885 
1886 		/*
1887 		 * We could rely on make_row_comparison_op to complain if the list
1888 		 * lengths differ, but we prefer to generate a more specific error
1889 		 * message.
1890 		 */
1891 		if (list_length(left_list) < list_length(right_list))
1892 			ereport(ERROR,
1893 					(errcode(ERRCODE_SYNTAX_ERROR),
1894 					 errmsg("subquery has too many columns"),
1895 					 parser_errposition(pstate, sublink->location)));
1896 		if (list_length(left_list) > list_length(right_list))
1897 			ereport(ERROR,
1898 					(errcode(ERRCODE_SYNTAX_ERROR),
1899 					 errmsg("subquery has too few columns"),
1900 					 parser_errposition(pstate, sublink->location)));
1901 
1902 		/*
1903 		 * Identify the combining operator(s) and generate a suitable
1904 		 * row-comparison expression.
1905 		 */
1906 		sublink->testexpr = make_row_comparison_op(pstate,
1907 												   sublink->operName,
1908 												   left_list,
1909 												   right_list,
1910 												   sublink->location);
1911 	}
1912 
1913 	return result;
1914 }
1915 
1916 /*
1917  * transformArrayExpr
1918  *
1919  * If the caller specifies the target type, the resulting array will
1920  * be of exactly that type.  Otherwise we try to infer a common type
1921  * for the elements using select_common_type().
1922  */
1923 static Node *
transformArrayExpr(ParseState * pstate,A_ArrayExpr * a,Oid array_type,Oid element_type,int32 typmod)1924 transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
1925 				   Oid array_type, Oid element_type, int32 typmod)
1926 {
1927 	ArrayExpr  *newa = makeNode(ArrayExpr);
1928 	List	   *newelems = NIL;
1929 	List	   *newcoercedelems = NIL;
1930 	ListCell   *element;
1931 	Oid			coerce_type;
1932 	bool		coerce_hard;
1933 
1934 	/*
1935 	 * Transform the element expressions
1936 	 *
1937 	 * Assume that the array is one-dimensional unless we find an array-type
1938 	 * element expression.
1939 	 */
1940 	newa->multidims = false;
1941 	foreach(element, a->elements)
1942 	{
1943 		Node	   *e = (Node *) lfirst(element);
1944 		Node	   *newe;
1945 
1946 		/*
1947 		 * If an element is itself an A_ArrayExpr, recurse directly so that we
1948 		 * can pass down any target type we were given.
1949 		 */
1950 		if (IsA(e, A_ArrayExpr))
1951 		{
1952 			newe = transformArrayExpr(pstate,
1953 									  (A_ArrayExpr *) e,
1954 									  array_type,
1955 									  element_type,
1956 									  typmod);
1957 			/* we certainly have an array here */
1958 			Assert(array_type == InvalidOid || array_type == exprType(newe));
1959 			newa->multidims = true;
1960 		}
1961 		else
1962 		{
1963 			newe = transformExprRecurse(pstate, e);
1964 
1965 			/*
1966 			 * Check for sub-array expressions, if we haven't already found
1967 			 * one.
1968 			 */
1969 			if (!newa->multidims && type_is_array(exprType(newe)))
1970 				newa->multidims = true;
1971 		}
1972 
1973 		newelems = lappend(newelems, newe);
1974 	}
1975 
1976 	/*
1977 	 * Select a target type for the elements.
1978 	 *
1979 	 * If we haven't been given a target array type, we must try to deduce a
1980 	 * common type based on the types of the individual elements present.
1981 	 */
1982 	if (OidIsValid(array_type))
1983 	{
1984 		/* Caller must ensure array_type matches element_type */
1985 		Assert(OidIsValid(element_type));
1986 		coerce_type = (newa->multidims ? array_type : element_type);
1987 		coerce_hard = true;
1988 	}
1989 	else
1990 	{
1991 		/* Can't handle an empty array without a target type */
1992 		if (newelems == NIL)
1993 			ereport(ERROR,
1994 					(errcode(ERRCODE_INDETERMINATE_DATATYPE),
1995 					 errmsg("cannot determine type of empty array"),
1996 					 errhint("Explicitly cast to the desired type, "
1997 							 "for example ARRAY[]::integer[]."),
1998 					 parser_errposition(pstate, a->location)));
1999 
2000 		/* Select a common type for the elements */
2001 		coerce_type = select_common_type(pstate, newelems, "ARRAY", NULL);
2002 
2003 		if (newa->multidims)
2004 		{
2005 			array_type = coerce_type;
2006 			element_type = get_element_type(array_type);
2007 			if (!OidIsValid(element_type))
2008 				ereport(ERROR,
2009 						(errcode(ERRCODE_UNDEFINED_OBJECT),
2010 						 errmsg("could not find element type for data type %s",
2011 								format_type_be(array_type)),
2012 						 parser_errposition(pstate, a->location)));
2013 		}
2014 		else
2015 		{
2016 			element_type = coerce_type;
2017 			array_type = get_array_type(element_type);
2018 			if (!OidIsValid(array_type))
2019 				ereport(ERROR,
2020 						(errcode(ERRCODE_UNDEFINED_OBJECT),
2021 						 errmsg("could not find array type for data type %s",
2022 								format_type_be(element_type)),
2023 						 parser_errposition(pstate, a->location)));
2024 		}
2025 		coerce_hard = false;
2026 	}
2027 
2028 	/*
2029 	 * Coerce elements to target type
2030 	 *
2031 	 * If the array has been explicitly cast, then the elements are in turn
2032 	 * explicitly coerced.
2033 	 *
2034 	 * If the array's type was merely derived from the common type of its
2035 	 * elements, then the elements are implicitly coerced to the common type.
2036 	 * This is consistent with other uses of select_common_type().
2037 	 */
2038 	foreach(element, newelems)
2039 	{
2040 		Node	   *e = (Node *) lfirst(element);
2041 		Node	   *newe;
2042 
2043 		if (coerce_hard)
2044 		{
2045 			newe = coerce_to_target_type(pstate, e,
2046 										 exprType(e),
2047 										 coerce_type,
2048 										 typmod,
2049 										 COERCION_EXPLICIT,
2050 										 COERCE_EXPLICIT_CAST,
2051 										 -1);
2052 			if (newe == NULL)
2053 				ereport(ERROR,
2054 						(errcode(ERRCODE_CANNOT_COERCE),
2055 						 errmsg("cannot cast type %s to %s",
2056 								format_type_be(exprType(e)),
2057 								format_type_be(coerce_type)),
2058 						 parser_errposition(pstate, exprLocation(e))));
2059 		}
2060 		else
2061 			newe = coerce_to_common_type(pstate, e,
2062 										 coerce_type,
2063 										 "ARRAY");
2064 		newcoercedelems = lappend(newcoercedelems, newe);
2065 	}
2066 
2067 	newa->array_typeid = array_type;
2068 	/* array_collid will be set by parse_collate.c */
2069 	newa->element_typeid = element_type;
2070 	newa->elements = newcoercedelems;
2071 	newa->location = a->location;
2072 
2073 	return (Node *) newa;
2074 }
2075 
2076 static Node *
transformRowExpr(ParseState * pstate,RowExpr * r,bool allowDefault)2077 transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
2078 {
2079 	RowExpr    *newr;
2080 	char		fname[16];
2081 	int			fnum;
2082 
2083 	newr = makeNode(RowExpr);
2084 
2085 	/* Transform the field expressions */
2086 	newr->args = transformExpressionList(pstate, r->args,
2087 										 pstate->p_expr_kind, allowDefault);
2088 
2089 	/* Barring later casting, we consider the type RECORD */
2090 	newr->row_typeid = RECORDOID;
2091 	newr->row_format = COERCE_IMPLICIT_CAST;
2092 
2093 	/* ROW() has anonymous columns, so invent some field names */
2094 	newr->colnames = NIL;
2095 	for (fnum = 1; fnum <= list_length(newr->args); fnum++)
2096 	{
2097 		snprintf(fname, sizeof(fname), "f%d", fnum);
2098 		newr->colnames = lappend(newr->colnames, makeString(pstrdup(fname)));
2099 	}
2100 
2101 	newr->location = r->location;
2102 
2103 	return (Node *) newr;
2104 }
2105 
2106 static Node *
transformCoalesceExpr(ParseState * pstate,CoalesceExpr * c)2107 transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c)
2108 {
2109 	CoalesceExpr *newc = makeNode(CoalesceExpr);
2110 	Node	   *last_srf = pstate->p_last_srf;
2111 	List	   *newargs = NIL;
2112 	List	   *newcoercedargs = NIL;
2113 	ListCell   *args;
2114 
2115 	foreach(args, c->args)
2116 	{
2117 		Node	   *e = (Node *) lfirst(args);
2118 		Node	   *newe;
2119 
2120 		newe = transformExprRecurse(pstate, e);
2121 		newargs = lappend(newargs, newe);
2122 	}
2123 
2124 	newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
2125 	/* coalescecollid will be set by parse_collate.c */
2126 
2127 	/* Convert arguments if necessary */
2128 	foreach(args, newargs)
2129 	{
2130 		Node	   *e = (Node *) lfirst(args);
2131 		Node	   *newe;
2132 
2133 		newe = coerce_to_common_type(pstate, e,
2134 									 newc->coalescetype,
2135 									 "COALESCE");
2136 		newcoercedargs = lappend(newcoercedargs, newe);
2137 	}
2138 
2139 	/* if any subexpression contained a SRF, complain */
2140 	if (pstate->p_last_srf != last_srf)
2141 		ereport(ERROR,
2142 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2143 		/* translator: %s is name of a SQL construct, eg GROUP BY */
2144 				 errmsg("set-returning functions are not allowed in %s",
2145 						"COALESCE"),
2146 				 errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
2147 				 parser_errposition(pstate,
2148 									exprLocation(pstate->p_last_srf))));
2149 
2150 	newc->args = newcoercedargs;
2151 	newc->location = c->location;
2152 	return (Node *) newc;
2153 }
2154 
2155 static Node *
transformMinMaxExpr(ParseState * pstate,MinMaxExpr * m)2156 transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
2157 {
2158 	MinMaxExpr *newm = makeNode(MinMaxExpr);
2159 	List	   *newargs = NIL;
2160 	List	   *newcoercedargs = NIL;
2161 	const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
2162 	ListCell   *args;
2163 
2164 	newm->op = m->op;
2165 	foreach(args, m->args)
2166 	{
2167 		Node	   *e = (Node *) lfirst(args);
2168 		Node	   *newe;
2169 
2170 		newe = transformExprRecurse(pstate, e);
2171 		newargs = lappend(newargs, newe);
2172 	}
2173 
2174 	newm->minmaxtype = select_common_type(pstate, newargs, funcname, NULL);
2175 	/* minmaxcollid and inputcollid will be set by parse_collate.c */
2176 
2177 	/* Convert arguments if necessary */
2178 	foreach(args, newargs)
2179 	{
2180 		Node	   *e = (Node *) lfirst(args);
2181 		Node	   *newe;
2182 
2183 		newe = coerce_to_common_type(pstate, e,
2184 									 newm->minmaxtype,
2185 									 funcname);
2186 		newcoercedargs = lappend(newcoercedargs, newe);
2187 	}
2188 
2189 	newm->args = newcoercedargs;
2190 	newm->location = m->location;
2191 	return (Node *) newm;
2192 }
2193 
2194 static Node *
transformSQLValueFunction(ParseState * pstate,SQLValueFunction * svf)2195 transformSQLValueFunction(ParseState *pstate, SQLValueFunction *svf)
2196 {
2197 	/*
2198 	 * All we need to do is insert the correct result type and (where needed)
2199 	 * validate the typmod, so we just modify the node in-place.
2200 	 */
2201 	switch (svf->op)
2202 	{
2203 		case SVFOP_CURRENT_DATE:
2204 			svf->type = DATEOID;
2205 			break;
2206 		case SVFOP_CURRENT_TIME:
2207 			svf->type = TIMETZOID;
2208 			break;
2209 		case SVFOP_CURRENT_TIME_N:
2210 			svf->type = TIMETZOID;
2211 			svf->typmod = anytime_typmod_check(true, svf->typmod);
2212 			break;
2213 		case SVFOP_CURRENT_TIMESTAMP:
2214 			svf->type = TIMESTAMPTZOID;
2215 			break;
2216 		case SVFOP_CURRENT_TIMESTAMP_N:
2217 			svf->type = TIMESTAMPTZOID;
2218 			svf->typmod = anytimestamp_typmod_check(true, svf->typmod);
2219 			break;
2220 		case SVFOP_LOCALTIME:
2221 			svf->type = TIMEOID;
2222 			break;
2223 		case SVFOP_LOCALTIME_N:
2224 			svf->type = TIMEOID;
2225 			svf->typmod = anytime_typmod_check(false, svf->typmod);
2226 			break;
2227 		case SVFOP_LOCALTIMESTAMP:
2228 			svf->type = TIMESTAMPOID;
2229 			break;
2230 		case SVFOP_LOCALTIMESTAMP_N:
2231 			svf->type = TIMESTAMPOID;
2232 			svf->typmod = anytimestamp_typmod_check(false, svf->typmod);
2233 			break;
2234 		case SVFOP_CURRENT_ROLE:
2235 		case SVFOP_CURRENT_USER:
2236 		case SVFOP_USER:
2237 		case SVFOP_SESSION_USER:
2238 		case SVFOP_CURRENT_CATALOG:
2239 		case SVFOP_CURRENT_SCHEMA:
2240 			svf->type = NAMEOID;
2241 			break;
2242 	}
2243 
2244 	return (Node *) svf;
2245 }
2246 
2247 static Node *
transformXmlExpr(ParseState * pstate,XmlExpr * x)2248 transformXmlExpr(ParseState *pstate, XmlExpr *x)
2249 {
2250 	XmlExpr    *newx;
2251 	ListCell   *lc;
2252 	int			i;
2253 
2254 	newx = makeNode(XmlExpr);
2255 	newx->op = x->op;
2256 	if (x->name)
2257 		newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
2258 	else
2259 		newx->name = NULL;
2260 	newx->xmloption = x->xmloption;
2261 	newx->type = XMLOID;		/* this just marks the node as transformed */
2262 	newx->typmod = -1;
2263 	newx->location = x->location;
2264 
2265 	/*
2266 	 * gram.y built the named args as a list of ResTarget.  Transform each,
2267 	 * and break the names out as a separate list.
2268 	 */
2269 	newx->named_args = NIL;
2270 	newx->arg_names = NIL;
2271 
2272 	foreach(lc, x->named_args)
2273 	{
2274 		ResTarget  *r = lfirst_node(ResTarget, lc);
2275 		Node	   *expr;
2276 		char	   *argname;
2277 
2278 		expr = transformExprRecurse(pstate, r->val);
2279 
2280 		if (r->name)
2281 			argname = map_sql_identifier_to_xml_name(r->name, false, false);
2282 		else if (IsA(r->val, ColumnRef))
2283 			argname = map_sql_identifier_to_xml_name(FigureColname(r->val),
2284 													 true, false);
2285 		else
2286 		{
2287 			ereport(ERROR,
2288 					(errcode(ERRCODE_SYNTAX_ERROR),
2289 					 x->op == IS_XMLELEMENT
2290 					 ? errmsg("unnamed XML attribute value must be a column reference")
2291 					 : errmsg("unnamed XML element value must be a column reference"),
2292 					 parser_errposition(pstate, r->location)));
2293 			argname = NULL;		/* keep compiler quiet */
2294 		}
2295 
2296 		/* reject duplicate argnames in XMLELEMENT only */
2297 		if (x->op == IS_XMLELEMENT)
2298 		{
2299 			ListCell   *lc2;
2300 
2301 			foreach(lc2, newx->arg_names)
2302 			{
2303 				if (strcmp(argname, strVal(lfirst(lc2))) == 0)
2304 					ereport(ERROR,
2305 							(errcode(ERRCODE_SYNTAX_ERROR),
2306 							 errmsg("XML attribute name \"%s\" appears more than once",
2307 									argname),
2308 							 parser_errposition(pstate, r->location)));
2309 			}
2310 		}
2311 
2312 		newx->named_args = lappend(newx->named_args, expr);
2313 		newx->arg_names = lappend(newx->arg_names, makeString(argname));
2314 	}
2315 
2316 	/* The other arguments are of varying types depending on the function */
2317 	newx->args = NIL;
2318 	i = 0;
2319 	foreach(lc, x->args)
2320 	{
2321 		Node	   *e = (Node *) lfirst(lc);
2322 		Node	   *newe;
2323 
2324 		newe = transformExprRecurse(pstate, e);
2325 		switch (x->op)
2326 		{
2327 			case IS_XMLCONCAT:
2328 				newe = coerce_to_specific_type(pstate, newe, XMLOID,
2329 											   "XMLCONCAT");
2330 				break;
2331 			case IS_XMLELEMENT:
2332 				/* no coercion necessary */
2333 				break;
2334 			case IS_XMLFOREST:
2335 				newe = coerce_to_specific_type(pstate, newe, XMLOID,
2336 											   "XMLFOREST");
2337 				break;
2338 			case IS_XMLPARSE:
2339 				if (i == 0)
2340 					newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2341 												   "XMLPARSE");
2342 				else
2343 					newe = coerce_to_boolean(pstate, newe, "XMLPARSE");
2344 				break;
2345 			case IS_XMLPI:
2346 				newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2347 											   "XMLPI");
2348 				break;
2349 			case IS_XMLROOT:
2350 				if (i == 0)
2351 					newe = coerce_to_specific_type(pstate, newe, XMLOID,
2352 												   "XMLROOT");
2353 				else if (i == 1)
2354 					newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2355 												   "XMLROOT");
2356 				else
2357 					newe = coerce_to_specific_type(pstate, newe, INT4OID,
2358 												   "XMLROOT");
2359 				break;
2360 			case IS_XMLSERIALIZE:
2361 				/* not handled here */
2362 				Assert(false);
2363 				break;
2364 			case IS_DOCUMENT:
2365 				newe = coerce_to_specific_type(pstate, newe, XMLOID,
2366 											   "IS DOCUMENT");
2367 				break;
2368 		}
2369 		newx->args = lappend(newx->args, newe);
2370 		i++;
2371 	}
2372 
2373 	return (Node *) newx;
2374 }
2375 
2376 static Node *
transformXmlSerialize(ParseState * pstate,XmlSerialize * xs)2377 transformXmlSerialize(ParseState *pstate, XmlSerialize *xs)
2378 {
2379 	Node	   *result;
2380 	XmlExpr    *xexpr;
2381 	Oid			targetType;
2382 	int32		targetTypmod;
2383 
2384 	xexpr = makeNode(XmlExpr);
2385 	xexpr->op = IS_XMLSERIALIZE;
2386 	xexpr->args = list_make1(coerce_to_specific_type(pstate,
2387 													 transformExprRecurse(pstate, xs->expr),
2388 													 XMLOID,
2389 													 "XMLSERIALIZE"));
2390 
2391 	typenameTypeIdAndMod(pstate, xs->typeName, &targetType, &targetTypmod);
2392 
2393 	xexpr->xmloption = xs->xmloption;
2394 	xexpr->location = xs->location;
2395 	/* We actually only need these to be able to parse back the expression. */
2396 	xexpr->type = targetType;
2397 	xexpr->typmod = targetTypmod;
2398 
2399 	/*
2400 	 * The actual target type is determined this way.  SQL allows char and
2401 	 * varchar as target types.  We allow anything that can be cast implicitly
2402 	 * from text.  This way, user-defined text-like data types automatically
2403 	 * fit in.
2404 	 */
2405 	result = coerce_to_target_type(pstate, (Node *) xexpr,
2406 								   TEXTOID, targetType, targetTypmod,
2407 								   COERCION_IMPLICIT,
2408 								   COERCE_IMPLICIT_CAST,
2409 								   -1);
2410 	if (result == NULL)
2411 		ereport(ERROR,
2412 				(errcode(ERRCODE_CANNOT_COERCE),
2413 				 errmsg("cannot cast XMLSERIALIZE result to %s",
2414 						format_type_be(targetType)),
2415 				 parser_errposition(pstate, xexpr->location)));
2416 	return result;
2417 }
2418 
2419 static Node *
transformBooleanTest(ParseState * pstate,BooleanTest * b)2420 transformBooleanTest(ParseState *pstate, BooleanTest *b)
2421 {
2422 	const char *clausename;
2423 
2424 	switch (b->booltesttype)
2425 	{
2426 		case IS_TRUE:
2427 			clausename = "IS TRUE";
2428 			break;
2429 		case IS_NOT_TRUE:
2430 			clausename = "IS NOT TRUE";
2431 			break;
2432 		case IS_FALSE:
2433 			clausename = "IS FALSE";
2434 			break;
2435 		case IS_NOT_FALSE:
2436 			clausename = "IS NOT FALSE";
2437 			break;
2438 		case IS_UNKNOWN:
2439 			clausename = "IS UNKNOWN";
2440 			break;
2441 		case IS_NOT_UNKNOWN:
2442 			clausename = "IS NOT UNKNOWN";
2443 			break;
2444 		default:
2445 			elog(ERROR, "unrecognized booltesttype: %d",
2446 				 (int) b->booltesttype);
2447 			clausename = NULL;	/* keep compiler quiet */
2448 	}
2449 
2450 	b->arg = (Expr *) transformExprRecurse(pstate, (Node *) b->arg);
2451 
2452 	b->arg = (Expr *) coerce_to_boolean(pstate,
2453 										(Node *) b->arg,
2454 										clausename);
2455 
2456 	return (Node *) b;
2457 }
2458 
2459 static Node *
transformCurrentOfExpr(ParseState * pstate,CurrentOfExpr * cexpr)2460 transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr)
2461 {
2462 	/* CURRENT OF can only appear at top level of UPDATE/DELETE */
2463 	Assert(pstate->p_target_nsitem != NULL);
2464 	cexpr->cvarno = pstate->p_target_nsitem->p_rtindex;
2465 
2466 	/*
2467 	 * Check to see if the cursor name matches a parameter of type REFCURSOR.
2468 	 * If so, replace the raw name reference with a parameter reference. (This
2469 	 * is a hack for the convenience of plpgsql.)
2470 	 */
2471 	if (cexpr->cursor_name != NULL) /* in case already transformed */
2472 	{
2473 		ColumnRef  *cref = makeNode(ColumnRef);
2474 		Node	   *node = NULL;
2475 
2476 		/* Build an unqualified ColumnRef with the given name */
2477 		cref->fields = list_make1(makeString(cexpr->cursor_name));
2478 		cref->location = -1;
2479 
2480 		/* See if there is a translation available from a parser hook */
2481 		if (pstate->p_pre_columnref_hook != NULL)
2482 			node = pstate->p_pre_columnref_hook(pstate, cref);
2483 		if (node == NULL && pstate->p_post_columnref_hook != NULL)
2484 			node = pstate->p_post_columnref_hook(pstate, cref, NULL);
2485 
2486 		/*
2487 		 * XXX Should we throw an error if we get a translation that isn't a
2488 		 * refcursor Param?  For now it seems best to silently ignore false
2489 		 * matches.
2490 		 */
2491 		if (node != NULL && IsA(node, Param))
2492 		{
2493 			Param	   *p = (Param *) node;
2494 
2495 			if (p->paramkind == PARAM_EXTERN &&
2496 				p->paramtype == REFCURSOROID)
2497 			{
2498 				/* Matches, so convert CURRENT OF to a param reference */
2499 				cexpr->cursor_name = NULL;
2500 				cexpr->cursor_param = p->paramid;
2501 			}
2502 		}
2503 	}
2504 
2505 	return (Node *) cexpr;
2506 }
2507 
2508 /*
2509  * Construct a whole-row reference to represent the notation "relation.*".
2510  */
2511 static Node *
transformWholeRowRef(ParseState * pstate,ParseNamespaceItem * nsitem,int sublevels_up,int location)2512 transformWholeRowRef(ParseState *pstate, ParseNamespaceItem *nsitem,
2513 					 int sublevels_up, int location)
2514 {
2515 	/*
2516 	 * Build the appropriate referencing node.  Normally this can be a
2517 	 * whole-row Var, but if the nsitem is a JOIN USING alias then it contains
2518 	 * only a subset of the columns of the underlying join RTE, so that will
2519 	 * not work.  Instead we immediately expand the reference into a RowExpr.
2520 	 * Since the JOIN USING's common columns are fully determined at this
2521 	 * point, there seems no harm in expanding it now rather than during
2522 	 * planning.
2523 	 *
2524 	 * Note that if the RTE is a function returning scalar, we create just a
2525 	 * plain reference to the function value, not a composite containing a
2526 	 * single column.  This is pretty inconsistent at first sight, but it's
2527 	 * what we've done historically.  One argument for it is that "rel" and
2528 	 * "rel.*" mean the same thing for composite relations, so why not for
2529 	 * scalar functions...
2530 	 */
2531 	if (nsitem->p_names == nsitem->p_rte->eref)
2532 	{
2533 		Var		   *result;
2534 
2535 		result = makeWholeRowVar(nsitem->p_rte, nsitem->p_rtindex,
2536 								 sublevels_up, true);
2537 
2538 		/* location is not filled in by makeWholeRowVar */
2539 		result->location = location;
2540 
2541 		/* mark relation as requiring whole-row SELECT access */
2542 		markVarForSelectPriv(pstate, result);
2543 
2544 		return (Node *) result;
2545 	}
2546 	else
2547 	{
2548 		RowExpr    *rowexpr;
2549 		List	   *fields;
2550 
2551 		/*
2552 		 * We want only as many columns as are listed in p_names->colnames,
2553 		 * and we should use those names not whatever possibly-aliased names
2554 		 * are in the RTE.  We needn't worry about marking the RTE for SELECT
2555 		 * access, as the common columns are surely so marked already.
2556 		 */
2557 		expandRTE(nsitem->p_rte, nsitem->p_rtindex,
2558 				  sublevels_up, location, false,
2559 				  NULL, &fields);
2560 		rowexpr = makeNode(RowExpr);
2561 		rowexpr->args = list_truncate(fields,
2562 									  list_length(nsitem->p_names->colnames));
2563 		rowexpr->row_typeid = RECORDOID;
2564 		rowexpr->row_format = COERCE_IMPLICIT_CAST;
2565 		rowexpr->colnames = copyObject(nsitem->p_names->colnames);
2566 		rowexpr->location = location;
2567 
2568 		return (Node *) rowexpr;
2569 	}
2570 }
2571 
2572 /*
2573  * Handle an explicit CAST construct.
2574  *
2575  * Transform the argument, look up the type name, and apply any necessary
2576  * coercion function(s).
2577  */
2578 static Node *
transformTypeCast(ParseState * pstate,TypeCast * tc)2579 transformTypeCast(ParseState *pstate, TypeCast *tc)
2580 {
2581 	Node	   *result;
2582 	Node	   *arg = tc->arg;
2583 	Node	   *expr;
2584 	Oid			inputType;
2585 	Oid			targetType;
2586 	int32		targetTypmod;
2587 	int			location;
2588 
2589 	/* Look up the type name first */
2590 	typenameTypeIdAndMod(pstate, tc->typeName, &targetType, &targetTypmod);
2591 
2592 	/*
2593 	 * If the subject of the typecast is an ARRAY[] construct and the target
2594 	 * type is an array type, we invoke transformArrayExpr() directly so that
2595 	 * we can pass down the type information.  This avoids some cases where
2596 	 * transformArrayExpr() might not infer the correct type.  Otherwise, just
2597 	 * transform the argument normally.
2598 	 */
2599 	if (IsA(arg, A_ArrayExpr))
2600 	{
2601 		Oid			targetBaseType;
2602 		int32		targetBaseTypmod;
2603 		Oid			elementType;
2604 
2605 		/*
2606 		 * If target is a domain over array, work with the base array type
2607 		 * here.  Below, we'll cast the array type to the domain.  In the
2608 		 * usual case that the target is not a domain, the remaining steps
2609 		 * will be a no-op.
2610 		 */
2611 		targetBaseTypmod = targetTypmod;
2612 		targetBaseType = getBaseTypeAndTypmod(targetType, &targetBaseTypmod);
2613 		elementType = get_element_type(targetBaseType);
2614 		if (OidIsValid(elementType))
2615 		{
2616 			expr = transformArrayExpr(pstate,
2617 									  (A_ArrayExpr *) arg,
2618 									  targetBaseType,
2619 									  elementType,
2620 									  targetBaseTypmod);
2621 		}
2622 		else
2623 			expr = transformExprRecurse(pstate, arg);
2624 	}
2625 	else
2626 		expr = transformExprRecurse(pstate, arg);
2627 
2628 	inputType = exprType(expr);
2629 	if (inputType == InvalidOid)
2630 		return expr;			/* do nothing if NULL input */
2631 
2632 	/*
2633 	 * Location of the coercion is preferentially the location of the :: or
2634 	 * CAST symbol, but if there is none then use the location of the type
2635 	 * name (this can happen in TypeName 'string' syntax, for instance).
2636 	 */
2637 	location = tc->location;
2638 	if (location < 0)
2639 		location = tc->typeName->location;
2640 
2641 	result = coerce_to_target_type(pstate, expr, inputType,
2642 								   targetType, targetTypmod,
2643 								   COERCION_EXPLICIT,
2644 								   COERCE_EXPLICIT_CAST,
2645 								   location);
2646 	if (result == NULL)
2647 		ereport(ERROR,
2648 				(errcode(ERRCODE_CANNOT_COERCE),
2649 				 errmsg("cannot cast type %s to %s",
2650 						format_type_be(inputType),
2651 						format_type_be(targetType)),
2652 				 parser_coercion_errposition(pstate, location, expr)));
2653 
2654 	return result;
2655 }
2656 
2657 /*
2658  * Handle an explicit COLLATE clause.
2659  *
2660  * Transform the argument, and look up the collation name.
2661  */
2662 static Node *
transformCollateClause(ParseState * pstate,CollateClause * c)2663 transformCollateClause(ParseState *pstate, CollateClause *c)
2664 {
2665 	CollateExpr *newc;
2666 	Oid			argtype;
2667 
2668 	newc = makeNode(CollateExpr);
2669 	newc->arg = (Expr *) transformExprRecurse(pstate, c->arg);
2670 
2671 	argtype = exprType((Node *) newc->arg);
2672 
2673 	/*
2674 	 * The unknown type is not collatable, but coerce_type() takes care of it
2675 	 * separately, so we'll let it go here.
2676 	 */
2677 	if (!type_is_collatable(argtype) && argtype != UNKNOWNOID)
2678 		ereport(ERROR,
2679 				(errcode(ERRCODE_DATATYPE_MISMATCH),
2680 				 errmsg("collations are not supported by type %s",
2681 						format_type_be(argtype)),
2682 				 parser_errposition(pstate, c->location)));
2683 
2684 	newc->collOid = LookupCollation(pstate, c->collname, c->location);
2685 	newc->location = c->location;
2686 
2687 	return (Node *) newc;
2688 }
2689 
2690 /*
2691  * Transform a "row compare-op row" construct
2692  *
2693  * The inputs are lists of already-transformed expressions.
2694  * As with coerce_type, pstate may be NULL if no special unknown-Param
2695  * processing is wanted.
2696  *
2697  * The output may be a single OpExpr, an AND or OR combination of OpExprs,
2698  * or a RowCompareExpr.  In all cases it is guaranteed to return boolean.
2699  * The AND, OR, and RowCompareExpr cases further imply things about the
2700  * behavior of the operators (ie, they behave as =, <>, or < <= > >=).
2701  */
2702 static Node *
make_row_comparison_op(ParseState * pstate,List * opname,List * largs,List * rargs,int location)2703 make_row_comparison_op(ParseState *pstate, List *opname,
2704 					   List *largs, List *rargs, int location)
2705 {
2706 	RowCompareExpr *rcexpr;
2707 	RowCompareType rctype;
2708 	List	   *opexprs;
2709 	List	   *opnos;
2710 	List	   *opfamilies;
2711 	ListCell   *l,
2712 			   *r;
2713 	List	  **opinfo_lists;
2714 	Bitmapset  *strats;
2715 	int			nopers;
2716 	int			i;
2717 
2718 	nopers = list_length(largs);
2719 	if (nopers != list_length(rargs))
2720 		ereport(ERROR,
2721 				(errcode(ERRCODE_SYNTAX_ERROR),
2722 				 errmsg("unequal number of entries in row expressions"),
2723 				 parser_errposition(pstate, location)));
2724 
2725 	/*
2726 	 * We can't compare zero-length rows because there is no principled basis
2727 	 * for figuring out what the operator is.
2728 	 */
2729 	if (nopers == 0)
2730 		ereport(ERROR,
2731 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2732 				 errmsg("cannot compare rows of zero length"),
2733 				 parser_errposition(pstate, location)));
2734 
2735 	/*
2736 	 * Identify all the pairwise operators, using make_op so that behavior is
2737 	 * the same as in the simple scalar case.
2738 	 */
2739 	opexprs = NIL;
2740 	forboth(l, largs, r, rargs)
2741 	{
2742 		Node	   *larg = (Node *) lfirst(l);
2743 		Node	   *rarg = (Node *) lfirst(r);
2744 		OpExpr	   *cmp;
2745 
2746 		cmp = castNode(OpExpr, make_op(pstate, opname, larg, rarg,
2747 									   pstate->p_last_srf, location));
2748 
2749 		/*
2750 		 * We don't use coerce_to_boolean here because we insist on the
2751 		 * operator yielding boolean directly, not via coercion.  If it
2752 		 * doesn't yield bool it won't be in any index opfamilies...
2753 		 */
2754 		if (cmp->opresulttype != BOOLOID)
2755 			ereport(ERROR,
2756 					(errcode(ERRCODE_DATATYPE_MISMATCH),
2757 					 errmsg("row comparison operator must yield type boolean, "
2758 							"not type %s",
2759 							format_type_be(cmp->opresulttype)),
2760 					 parser_errposition(pstate, location)));
2761 		if (expression_returns_set((Node *) cmp))
2762 			ereport(ERROR,
2763 					(errcode(ERRCODE_DATATYPE_MISMATCH),
2764 					 errmsg("row comparison operator must not return a set"),
2765 					 parser_errposition(pstate, location)));
2766 		opexprs = lappend(opexprs, cmp);
2767 	}
2768 
2769 	/*
2770 	 * If rows are length 1, just return the single operator.  In this case we
2771 	 * don't insist on identifying btree semantics for the operator (but we
2772 	 * still require it to return boolean).
2773 	 */
2774 	if (nopers == 1)
2775 		return (Node *) linitial(opexprs);
2776 
2777 	/*
2778 	 * Now we must determine which row comparison semantics (= <> < <= > >=)
2779 	 * apply to this set of operators.  We look for btree opfamilies
2780 	 * containing the operators, and see which interpretations (strategy
2781 	 * numbers) exist for each operator.
2782 	 */
2783 	opinfo_lists = (List **) palloc(nopers * sizeof(List *));
2784 	strats = NULL;
2785 	i = 0;
2786 	foreach(l, opexprs)
2787 	{
2788 		Oid			opno = ((OpExpr *) lfirst(l))->opno;
2789 		Bitmapset  *this_strats;
2790 		ListCell   *j;
2791 
2792 		opinfo_lists[i] = get_op_btree_interpretation(opno);
2793 
2794 		/*
2795 		 * convert strategy numbers into a Bitmapset to make the intersection
2796 		 * calculation easy.
2797 		 */
2798 		this_strats = NULL;
2799 		foreach(j, opinfo_lists[i])
2800 		{
2801 			OpBtreeInterpretation *opinfo = lfirst(j);
2802 
2803 			this_strats = bms_add_member(this_strats, opinfo->strategy);
2804 		}
2805 		if (i == 0)
2806 			strats = this_strats;
2807 		else
2808 			strats = bms_int_members(strats, this_strats);
2809 		i++;
2810 	}
2811 
2812 	/*
2813 	 * If there are multiple common interpretations, we may use any one of
2814 	 * them ... this coding arbitrarily picks the lowest btree strategy
2815 	 * number.
2816 	 */
2817 	i = bms_first_member(strats);
2818 	if (i < 0)
2819 	{
2820 		/* No common interpretation, so fail */
2821 		ereport(ERROR,
2822 				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2823 				 errmsg("could not determine interpretation of row comparison operator %s",
2824 						strVal(llast(opname))),
2825 				 errhint("Row comparison operators must be associated with btree operator families."),
2826 				 parser_errposition(pstate, location)));
2827 	}
2828 	rctype = (RowCompareType) i;
2829 
2830 	/*
2831 	 * For = and <> cases, we just combine the pairwise operators with AND or
2832 	 * OR respectively.
2833 	 */
2834 	if (rctype == ROWCOMPARE_EQ)
2835 		return (Node *) makeBoolExpr(AND_EXPR, opexprs, location);
2836 	if (rctype == ROWCOMPARE_NE)
2837 		return (Node *) makeBoolExpr(OR_EXPR, opexprs, location);
2838 
2839 	/*
2840 	 * Otherwise we need to choose exactly which opfamily to associate with
2841 	 * each operator.
2842 	 */
2843 	opfamilies = NIL;
2844 	for (i = 0; i < nopers; i++)
2845 	{
2846 		Oid			opfamily = InvalidOid;
2847 		ListCell   *j;
2848 
2849 		foreach(j, opinfo_lists[i])
2850 		{
2851 			OpBtreeInterpretation *opinfo = lfirst(j);
2852 
2853 			if (opinfo->strategy == rctype)
2854 			{
2855 				opfamily = opinfo->opfamily_id;
2856 				break;
2857 			}
2858 		}
2859 		if (OidIsValid(opfamily))
2860 			opfamilies = lappend_oid(opfamilies, opfamily);
2861 		else					/* should not happen */
2862 			ereport(ERROR,
2863 					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2864 					 errmsg("could not determine interpretation of row comparison operator %s",
2865 							strVal(llast(opname))),
2866 					 errdetail("There are multiple equally-plausible candidates."),
2867 					 parser_errposition(pstate, location)));
2868 	}
2869 
2870 	/*
2871 	 * Now deconstruct the OpExprs and create a RowCompareExpr.
2872 	 *
2873 	 * Note: can't just reuse the passed largs/rargs lists, because of
2874 	 * possibility that make_op inserted coercion operations.
2875 	 */
2876 	opnos = NIL;
2877 	largs = NIL;
2878 	rargs = NIL;
2879 	foreach(l, opexprs)
2880 	{
2881 		OpExpr	   *cmp = (OpExpr *) lfirst(l);
2882 
2883 		opnos = lappend_oid(opnos, cmp->opno);
2884 		largs = lappend(largs, linitial(cmp->args));
2885 		rargs = lappend(rargs, lsecond(cmp->args));
2886 	}
2887 
2888 	rcexpr = makeNode(RowCompareExpr);
2889 	rcexpr->rctype = rctype;
2890 	rcexpr->opnos = opnos;
2891 	rcexpr->opfamilies = opfamilies;
2892 	rcexpr->inputcollids = NIL; /* assign_expr_collations will fix this */
2893 	rcexpr->largs = largs;
2894 	rcexpr->rargs = rargs;
2895 
2896 	return (Node *) rcexpr;
2897 }
2898 
2899 /*
2900  * Transform a "row IS DISTINCT FROM row" construct
2901  *
2902  * The input RowExprs are already transformed
2903  */
2904 static Node *
make_row_distinct_op(ParseState * pstate,List * opname,RowExpr * lrow,RowExpr * rrow,int location)2905 make_row_distinct_op(ParseState *pstate, List *opname,
2906 					 RowExpr *lrow, RowExpr *rrow,
2907 					 int location)
2908 {
2909 	Node	   *result = NULL;
2910 	List	   *largs = lrow->args;
2911 	List	   *rargs = rrow->args;
2912 	ListCell   *l,
2913 			   *r;
2914 
2915 	if (list_length(largs) != list_length(rargs))
2916 		ereport(ERROR,
2917 				(errcode(ERRCODE_SYNTAX_ERROR),
2918 				 errmsg("unequal number of entries in row expressions"),
2919 				 parser_errposition(pstate, location)));
2920 
2921 	forboth(l, largs, r, rargs)
2922 	{
2923 		Node	   *larg = (Node *) lfirst(l);
2924 		Node	   *rarg = (Node *) lfirst(r);
2925 		Node	   *cmp;
2926 
2927 		cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg, location);
2928 		if (result == NULL)
2929 			result = cmp;
2930 		else
2931 			result = (Node *) makeBoolExpr(OR_EXPR,
2932 										   list_make2(result, cmp),
2933 										   location);
2934 	}
2935 
2936 	if (result == NULL)
2937 	{
2938 		/* zero-length rows?  Generate constant FALSE */
2939 		result = makeBoolConst(false, false);
2940 	}
2941 
2942 	return result;
2943 }
2944 
2945 /*
2946  * make the node for an IS DISTINCT FROM operator
2947  */
2948 static Expr *
make_distinct_op(ParseState * pstate,List * opname,Node * ltree,Node * rtree,int location)2949 make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree,
2950 				 int location)
2951 {
2952 	Expr	   *result;
2953 
2954 	result = make_op(pstate, opname, ltree, rtree,
2955 					 pstate->p_last_srf, location);
2956 	if (((OpExpr *) result)->opresulttype != BOOLOID)
2957 		ereport(ERROR,
2958 				(errcode(ERRCODE_DATATYPE_MISMATCH),
2959 				 errmsg("IS DISTINCT FROM requires = operator to yield boolean"),
2960 				 parser_errposition(pstate, location)));
2961 	if (((OpExpr *) result)->opretset)
2962 		ereport(ERROR,
2963 				(errcode(ERRCODE_DATATYPE_MISMATCH),
2964 		/* translator: %s is name of a SQL construct, eg NULLIF */
2965 				 errmsg("%s must not return a set", "IS DISTINCT FROM"),
2966 				 parser_errposition(pstate, location)));
2967 
2968 	/*
2969 	 * We rely on DistinctExpr and OpExpr being same struct
2970 	 */
2971 	NodeSetTag(result, T_DistinctExpr);
2972 
2973 	return result;
2974 }
2975 
2976 /*
2977  * Produce a NullTest node from an IS [NOT] DISTINCT FROM NULL construct
2978  *
2979  * "arg" is the untransformed other argument
2980  */
2981 static Node *
make_nulltest_from_distinct(ParseState * pstate,A_Expr * distincta,Node * arg)2982 make_nulltest_from_distinct(ParseState *pstate, A_Expr *distincta, Node *arg)
2983 {
2984 	NullTest   *nt = makeNode(NullTest);
2985 
2986 	nt->arg = (Expr *) transformExprRecurse(pstate, arg);
2987 	/* the argument can be any type, so don't coerce it */
2988 	if (distincta->kind == AEXPR_NOT_DISTINCT)
2989 		nt->nulltesttype = IS_NULL;
2990 	else
2991 		nt->nulltesttype = IS_NOT_NULL;
2992 	/* argisrow = false is correct whether or not arg is composite */
2993 	nt->argisrow = false;
2994 	nt->location = distincta->location;
2995 	return (Node *) nt;
2996 }
2997 
2998 /*
2999  * Produce a string identifying an expression by kind.
3000  *
3001  * Note: when practical, use a simple SQL keyword for the result.  If that
3002  * doesn't work well, check call sites to see whether custom error message
3003  * strings are required.
3004  */
3005 const char *
ParseExprKindName(ParseExprKind exprKind)3006 ParseExprKindName(ParseExprKind exprKind)
3007 {
3008 	switch (exprKind)
3009 	{
3010 		case EXPR_KIND_NONE:
3011 			return "invalid expression context";
3012 		case EXPR_KIND_OTHER:
3013 			return "extension expression";
3014 		case EXPR_KIND_JOIN_ON:
3015 			return "JOIN/ON";
3016 		case EXPR_KIND_JOIN_USING:
3017 			return "JOIN/USING";
3018 		case EXPR_KIND_FROM_SUBSELECT:
3019 			return "sub-SELECT in FROM";
3020 		case EXPR_KIND_FROM_FUNCTION:
3021 			return "function in FROM";
3022 		case EXPR_KIND_WHERE:
3023 			return "WHERE";
3024 		case EXPR_KIND_POLICY:
3025 			return "POLICY";
3026 		case EXPR_KIND_HAVING:
3027 			return "HAVING";
3028 		case EXPR_KIND_FILTER:
3029 			return "FILTER";
3030 		case EXPR_KIND_WINDOW_PARTITION:
3031 			return "window PARTITION BY";
3032 		case EXPR_KIND_WINDOW_ORDER:
3033 			return "window ORDER BY";
3034 		case EXPR_KIND_WINDOW_FRAME_RANGE:
3035 			return "window RANGE";
3036 		case EXPR_KIND_WINDOW_FRAME_ROWS:
3037 			return "window ROWS";
3038 		case EXPR_KIND_WINDOW_FRAME_GROUPS:
3039 			return "window GROUPS";
3040 		case EXPR_KIND_SELECT_TARGET:
3041 			return "SELECT";
3042 		case EXPR_KIND_INSERT_TARGET:
3043 			return "INSERT";
3044 		case EXPR_KIND_UPDATE_SOURCE:
3045 		case EXPR_KIND_UPDATE_TARGET:
3046 			return "UPDATE";
3047 		case EXPR_KIND_GROUP_BY:
3048 			return "GROUP BY";
3049 		case EXPR_KIND_ORDER_BY:
3050 			return "ORDER BY";
3051 		case EXPR_KIND_DISTINCT_ON:
3052 			return "DISTINCT ON";
3053 		case EXPR_KIND_LIMIT:
3054 			return "LIMIT";
3055 		case EXPR_KIND_OFFSET:
3056 			return "OFFSET";
3057 		case EXPR_KIND_RETURNING:
3058 			return "RETURNING";
3059 		case EXPR_KIND_VALUES:
3060 		case EXPR_KIND_VALUES_SINGLE:
3061 			return "VALUES";
3062 		case EXPR_KIND_CHECK_CONSTRAINT:
3063 		case EXPR_KIND_DOMAIN_CHECK:
3064 			return "CHECK";
3065 		case EXPR_KIND_COLUMN_DEFAULT:
3066 		case EXPR_KIND_FUNCTION_DEFAULT:
3067 			return "DEFAULT";
3068 		case EXPR_KIND_INDEX_EXPRESSION:
3069 			return "index expression";
3070 		case EXPR_KIND_INDEX_PREDICATE:
3071 			return "index predicate";
3072 		case EXPR_KIND_STATS_EXPRESSION:
3073 			return "statistics expression";
3074 		case EXPR_KIND_ALTER_COL_TRANSFORM:
3075 			return "USING";
3076 		case EXPR_KIND_EXECUTE_PARAMETER:
3077 			return "EXECUTE";
3078 		case EXPR_KIND_TRIGGER_WHEN:
3079 			return "WHEN";
3080 		case EXPR_KIND_PARTITION_BOUND:
3081 			return "partition bound";
3082 		case EXPR_KIND_PARTITION_EXPRESSION:
3083 			return "PARTITION BY";
3084 		case EXPR_KIND_CALL_ARGUMENT:
3085 			return "CALL";
3086 		case EXPR_KIND_COPY_WHERE:
3087 			return "WHERE";
3088 		case EXPR_KIND_GENERATED_COLUMN:
3089 			return "GENERATED AS";
3090 		case EXPR_KIND_CYCLE_MARK:
3091 			return "CYCLE";
3092 
3093 			/*
3094 			 * There is intentionally no default: case here, so that the
3095 			 * compiler will warn if we add a new ParseExprKind without
3096 			 * extending this switch.  If we do see an unrecognized value at
3097 			 * runtime, we'll fall through to the "unrecognized" return.
3098 			 */
3099 	}
3100 	return "unrecognized expression kind";
3101 }
3102