doc: Meson is not experimental on Windows
[pgsql.git] / src / backend / parser / parse_expr.c
blobbad1df732ea4de3df3e7ec623e6168072fdb8d70
1 /*-------------------------------------------------------------------------
3 * parse_expr.c
4 * handle expressions in parser
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * src/backend/parser/parse_expr.c
13 *-------------------------------------------------------------------------
16 #include "postgres.h"
18 #include "catalog/pg_aggregate.h"
19 #include "catalog/pg_type.h"
20 #include "commands/dbcommands.h"
21 #include "miscadmin.h"
22 #include "nodes/makefuncs.h"
23 #include "nodes/nodeFuncs.h"
24 #include "optimizer/optimizer.h"
25 #include "parser/analyze.h"
26 #include "parser/parse_agg.h"
27 #include "parser/parse_clause.h"
28 #include "parser/parse_coerce.h"
29 #include "parser/parse_collate.h"
30 #include "parser/parse_expr.h"
31 #include "parser/parse_func.h"
32 #include "parser/parse_oper.h"
33 #include "parser/parse_relation.h"
34 #include "parser/parse_target.h"
35 #include "parser/parse_type.h"
36 #include "utils/builtins.h"
37 #include "utils/date.h"
38 #include "utils/fmgroids.h"
39 #include "utils/lsyscache.h"
40 #include "utils/timestamp.h"
41 #include "utils/xml.h"
43 /* GUC parameters */
44 bool Transform_null_equals = false;
47 static Node *transformExprRecurse(ParseState *pstate, Node *expr);
48 static Node *transformParamRef(ParseState *pstate, ParamRef *pref);
49 static Node *transformAExprOp(ParseState *pstate, A_Expr *a);
50 static Node *transformAExprOpAny(ParseState *pstate, A_Expr *a);
51 static Node *transformAExprOpAll(ParseState *pstate, A_Expr *a);
52 static Node *transformAExprDistinct(ParseState *pstate, A_Expr *a);
53 static Node *transformAExprNullIf(ParseState *pstate, A_Expr *a);
54 static Node *transformAExprIn(ParseState *pstate, A_Expr *a);
55 static Node *transformAExprBetween(ParseState *pstate, A_Expr *a);
56 static Node *transformMergeSupportFunc(ParseState *pstate, MergeSupportFunc *f);
57 static Node *transformBoolExpr(ParseState *pstate, BoolExpr *a);
58 static Node *transformFuncCall(ParseState *pstate, FuncCall *fn);
59 static Node *transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref);
60 static Node *transformCaseExpr(ParseState *pstate, CaseExpr *c);
61 static Node *transformSubLink(ParseState *pstate, SubLink *sublink);
62 static Node *transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
63 Oid array_type, Oid element_type, int32 typmod);
64 static Node *transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault);
65 static Node *transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c);
66 static Node *transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m);
67 static Node *transformSQLValueFunction(ParseState *pstate,
68 SQLValueFunction *svf);
69 static Node *transformXmlExpr(ParseState *pstate, XmlExpr *x);
70 static Node *transformXmlSerialize(ParseState *pstate, XmlSerialize *xs);
71 static Node *transformBooleanTest(ParseState *pstate, BooleanTest *b);
72 static Node *transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr);
73 static Node *transformColumnRef(ParseState *pstate, ColumnRef *cref);
74 static Node *transformWholeRowRef(ParseState *pstate,
75 ParseNamespaceItem *nsitem,
76 int sublevels_up, int location);
77 static Node *transformIndirection(ParseState *pstate, A_Indirection *ind);
78 static Node *transformTypeCast(ParseState *pstate, TypeCast *tc);
79 static Node *transformCollateClause(ParseState *pstate, CollateClause *c);
80 static Node *transformJsonObjectConstructor(ParseState *pstate,
81 JsonObjectConstructor *ctor);
82 static Node *transformJsonArrayConstructor(ParseState *pstate,
83 JsonArrayConstructor *ctor);
84 static Node *transformJsonArrayQueryConstructor(ParseState *pstate,
85 JsonArrayQueryConstructor *ctor);
86 static Node *transformJsonObjectAgg(ParseState *pstate, JsonObjectAgg *agg);
87 static Node *transformJsonArrayAgg(ParseState *pstate, JsonArrayAgg *agg);
88 static Node *transformJsonIsPredicate(ParseState *pstate, JsonIsPredicate *pred);
89 static Node *transformJsonParseExpr(ParseState *pstate, JsonParseExpr *jsexpr);
90 static Node *transformJsonScalarExpr(ParseState *pstate, JsonScalarExpr *jsexpr);
91 static Node *transformJsonSerializeExpr(ParseState *pstate,
92 JsonSerializeExpr *expr);
93 static Node *transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func);
94 static void transformJsonPassingArgs(ParseState *pstate, const char *constructName,
95 JsonFormatType format, List *args,
96 List **passing_values, List **passing_names);
97 static JsonBehavior *transformJsonBehavior(ParseState *pstate, JsonBehavior *behavior,
98 JsonBehaviorType default_behavior,
99 JsonReturning *returning);
100 static Node *GetJsonBehaviorConst(JsonBehaviorType btype, int location);
101 static Node *make_row_comparison_op(ParseState *pstate, List *opname,
102 List *largs, List *rargs, int location);
103 static Node *make_row_distinct_op(ParseState *pstate, List *opname,
104 RowExpr *lrow, RowExpr *rrow, int location);
105 static Expr *make_distinct_op(ParseState *pstate, List *opname,
106 Node *ltree, Node *rtree, int location);
107 static Node *make_nulltest_from_distinct(ParseState *pstate,
108 A_Expr *distincta, Node *arg);
112 * transformExpr -
113 * Analyze and transform expressions. Type checking and type casting is
114 * done here. This processing converts the raw grammar output into
115 * expression trees with fully determined semantics.
117 Node *
118 transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
120 Node *result;
121 ParseExprKind sv_expr_kind;
123 /* Save and restore identity of expression type we're parsing */
124 Assert(exprKind != EXPR_KIND_NONE);
125 sv_expr_kind = pstate->p_expr_kind;
126 pstate->p_expr_kind = exprKind;
128 result = transformExprRecurse(pstate, expr);
130 pstate->p_expr_kind = sv_expr_kind;
132 return result;
135 static Node *
136 transformExprRecurse(ParseState *pstate, Node *expr)
138 Node *result;
140 if (expr == NULL)
141 return NULL;
143 /* Guard against stack overflow due to overly complex expressions */
144 check_stack_depth();
146 switch (nodeTag(expr))
148 case T_ColumnRef:
149 result = transformColumnRef(pstate, (ColumnRef *) expr);
150 break;
152 case T_ParamRef:
153 result = transformParamRef(pstate, (ParamRef *) expr);
154 break;
156 case T_A_Const:
157 result = (Node *) make_const(pstate, (A_Const *) expr);
158 break;
160 case T_A_Indirection:
161 result = transformIndirection(pstate, (A_Indirection *) expr);
162 break;
164 case T_A_ArrayExpr:
165 result = transformArrayExpr(pstate, (A_ArrayExpr *) expr,
166 InvalidOid, InvalidOid, -1);
167 break;
169 case T_TypeCast:
170 result = transformTypeCast(pstate, (TypeCast *) expr);
171 break;
173 case T_CollateClause:
174 result = transformCollateClause(pstate, (CollateClause *) expr);
175 break;
177 case T_A_Expr:
179 A_Expr *a = (A_Expr *) expr;
181 switch (a->kind)
183 case AEXPR_OP:
184 result = transformAExprOp(pstate, a);
185 break;
186 case AEXPR_OP_ANY:
187 result = transformAExprOpAny(pstate, a);
188 break;
189 case AEXPR_OP_ALL:
190 result = transformAExprOpAll(pstate, a);
191 break;
192 case AEXPR_DISTINCT:
193 case AEXPR_NOT_DISTINCT:
194 result = transformAExprDistinct(pstate, a);
195 break;
196 case AEXPR_NULLIF:
197 result = transformAExprNullIf(pstate, a);
198 break;
199 case AEXPR_IN:
200 result = transformAExprIn(pstate, a);
201 break;
202 case AEXPR_LIKE:
203 case AEXPR_ILIKE:
204 case AEXPR_SIMILAR:
205 /* we can transform these just like AEXPR_OP */
206 result = transformAExprOp(pstate, a);
207 break;
208 case AEXPR_BETWEEN:
209 case AEXPR_NOT_BETWEEN:
210 case AEXPR_BETWEEN_SYM:
211 case AEXPR_NOT_BETWEEN_SYM:
212 result = transformAExprBetween(pstate, a);
213 break;
214 default:
215 elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
216 result = NULL; /* keep compiler quiet */
217 break;
219 break;
222 case T_BoolExpr:
223 result = transformBoolExpr(pstate, (BoolExpr *) expr);
224 break;
226 case T_FuncCall:
227 result = transformFuncCall(pstate, (FuncCall *) expr);
228 break;
230 case T_MultiAssignRef:
231 result = transformMultiAssignRef(pstate, (MultiAssignRef *) expr);
232 break;
234 case T_GroupingFunc:
235 result = transformGroupingFunc(pstate, (GroupingFunc *) expr);
236 break;
238 case T_MergeSupportFunc:
239 result = transformMergeSupportFunc(pstate,
240 (MergeSupportFunc *) expr);
241 break;
243 case T_NamedArgExpr:
245 NamedArgExpr *na = (NamedArgExpr *) expr;
247 na->arg = (Expr *) transformExprRecurse(pstate, (Node *) na->arg);
248 result = expr;
249 break;
252 case T_SubLink:
253 result = transformSubLink(pstate, (SubLink *) expr);
254 break;
256 case T_CaseExpr:
257 result = transformCaseExpr(pstate, (CaseExpr *) expr);
258 break;
260 case T_RowExpr:
261 result = transformRowExpr(pstate, (RowExpr *) expr, false);
262 break;
264 case T_CoalesceExpr:
265 result = transformCoalesceExpr(pstate, (CoalesceExpr *) expr);
266 break;
268 case T_MinMaxExpr:
269 result = transformMinMaxExpr(pstate, (MinMaxExpr *) expr);
270 break;
272 case T_SQLValueFunction:
273 result = transformSQLValueFunction(pstate,
274 (SQLValueFunction *) expr);
275 break;
277 case T_XmlExpr:
278 result = transformXmlExpr(pstate, (XmlExpr *) expr);
279 break;
281 case T_XmlSerialize:
282 result = transformXmlSerialize(pstate, (XmlSerialize *) expr);
283 break;
285 case T_NullTest:
287 NullTest *n = (NullTest *) expr;
289 n->arg = (Expr *) transformExprRecurse(pstate, (Node *) n->arg);
290 /* the argument can be any type, so don't coerce it */
291 n->argisrow = type_is_rowtype(exprType((Node *) n->arg));
292 result = expr;
293 break;
296 case T_BooleanTest:
297 result = transformBooleanTest(pstate, (BooleanTest *) expr);
298 break;
300 case T_CurrentOfExpr:
301 result = transformCurrentOfExpr(pstate, (CurrentOfExpr *) expr);
302 break;
305 * In all places where DEFAULT is legal, the caller should have
306 * processed it rather than passing it to transformExpr().
308 case T_SetToDefault:
309 ereport(ERROR,
310 (errcode(ERRCODE_SYNTAX_ERROR),
311 errmsg("DEFAULT is not allowed in this context"),
312 parser_errposition(pstate,
313 ((SetToDefault *) expr)->location)));
314 break;
317 * CaseTestExpr doesn't require any processing; it is only
318 * injected into parse trees in a fully-formed state.
320 * Ordinarily we should not see a Var here, but it is convenient
321 * for transformJoinUsingClause() to create untransformed operator
322 * trees containing already-transformed Vars. The best
323 * alternative would be to deconstruct and reconstruct column
324 * references, which seems expensively pointless. So allow it.
326 case T_CaseTestExpr:
327 case T_Var:
329 result = (Node *) expr;
330 break;
333 case T_JsonObjectConstructor:
334 result = transformJsonObjectConstructor(pstate, (JsonObjectConstructor *) expr);
335 break;
337 case T_JsonArrayConstructor:
338 result = transformJsonArrayConstructor(pstate, (JsonArrayConstructor *) expr);
339 break;
341 case T_JsonArrayQueryConstructor:
342 result = transformJsonArrayQueryConstructor(pstate, (JsonArrayQueryConstructor *) expr);
343 break;
345 case T_JsonObjectAgg:
346 result = transformJsonObjectAgg(pstate, (JsonObjectAgg *) expr);
347 break;
349 case T_JsonArrayAgg:
350 result = transformJsonArrayAgg(pstate, (JsonArrayAgg *) expr);
351 break;
353 case T_JsonIsPredicate:
354 result = transformJsonIsPredicate(pstate, (JsonIsPredicate *) expr);
355 break;
357 case T_JsonParseExpr:
358 result = transformJsonParseExpr(pstate, (JsonParseExpr *) expr);
359 break;
361 case T_JsonScalarExpr:
362 result = transformJsonScalarExpr(pstate, (JsonScalarExpr *) expr);
363 break;
365 case T_JsonSerializeExpr:
366 result = transformJsonSerializeExpr(pstate, (JsonSerializeExpr *) expr);
367 break;
369 case T_JsonFuncExpr:
370 result = transformJsonFuncExpr(pstate, (JsonFuncExpr *) expr);
371 break;
373 default:
374 /* should not reach here */
375 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
376 result = NULL; /* keep compiler quiet */
377 break;
380 return result;
384 * helper routine for delivering "column does not exist" error message
386 * (Usually we don't have to work this hard, but the general case of field
387 * selection from an arbitrary node needs it.)
389 static void
390 unknown_attribute(ParseState *pstate, Node *relref, const char *attname,
391 int location)
393 RangeTblEntry *rte;
395 if (IsA(relref, Var) &&
396 ((Var *) relref)->varattno == InvalidAttrNumber)
398 /* Reference the RTE by alias not by actual table name */
399 rte = GetRTEByRangeTablePosn(pstate,
400 ((Var *) relref)->varno,
401 ((Var *) relref)->varlevelsup);
402 ereport(ERROR,
403 (errcode(ERRCODE_UNDEFINED_COLUMN),
404 errmsg("column %s.%s does not exist",
405 rte->eref->aliasname, attname),
406 parser_errposition(pstate, location)));
408 else
410 /* Have to do it by reference to the type of the expression */
411 Oid relTypeId = exprType(relref);
413 if (ISCOMPLEX(relTypeId))
414 ereport(ERROR,
415 (errcode(ERRCODE_UNDEFINED_COLUMN),
416 errmsg("column \"%s\" not found in data type %s",
417 attname, format_type_be(relTypeId)),
418 parser_errposition(pstate, location)));
419 else if (relTypeId == RECORDOID)
420 ereport(ERROR,
421 (errcode(ERRCODE_UNDEFINED_COLUMN),
422 errmsg("could not identify column \"%s\" in record data type",
423 attname),
424 parser_errposition(pstate, location)));
425 else
426 ereport(ERROR,
427 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
428 errmsg("column notation .%s applied to type %s, "
429 "which is not a composite type",
430 attname, format_type_be(relTypeId)),
431 parser_errposition(pstate, location)));
435 static Node *
436 transformIndirection(ParseState *pstate, A_Indirection *ind)
438 Node *last_srf = pstate->p_last_srf;
439 Node *result = transformExprRecurse(pstate, ind->arg);
440 List *subscripts = NIL;
441 int location = exprLocation(result);
442 ListCell *i;
445 * We have to split any field-selection operations apart from
446 * subscripting. Adjacent A_Indices nodes have to be treated as a single
447 * multidimensional subscript operation.
449 foreach(i, ind->indirection)
451 Node *n = lfirst(i);
453 if (IsA(n, A_Indices))
454 subscripts = lappend(subscripts, n);
455 else if (IsA(n, A_Star))
457 ereport(ERROR,
458 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
459 errmsg("row expansion via \"*\" is not supported here"),
460 parser_errposition(pstate, location)));
462 else
464 Node *newresult;
466 Assert(IsA(n, String));
468 /* process subscripts before this field selection */
469 if (subscripts)
470 result = (Node *) transformContainerSubscripts(pstate,
471 result,
472 exprType(result),
473 exprTypmod(result),
474 subscripts,
475 false);
476 subscripts = NIL;
478 newresult = ParseFuncOrColumn(pstate,
479 list_make1(n),
480 list_make1(result),
481 last_srf,
482 NULL,
483 false,
484 location);
485 if (newresult == NULL)
486 unknown_attribute(pstate, result, strVal(n), location);
487 result = newresult;
490 /* process trailing subscripts, if any */
491 if (subscripts)
492 result = (Node *) transformContainerSubscripts(pstate,
493 result,
494 exprType(result),
495 exprTypmod(result),
496 subscripts,
497 false);
499 return result;
503 * Transform a ColumnRef.
505 * If you find yourself changing this code, see also ExpandColumnRefStar.
507 static Node *
508 transformColumnRef(ParseState *pstate, ColumnRef *cref)
510 Node *node = NULL;
511 char *nspname = NULL;
512 char *relname = NULL;
513 char *colname = NULL;
514 ParseNamespaceItem *nsitem;
515 int levels_up;
516 enum
518 CRERR_NO_COLUMN,
519 CRERR_NO_RTE,
520 CRERR_WRONG_DB,
521 CRERR_TOO_MANY
522 } crerr = CRERR_NO_COLUMN;
523 const char *err;
526 * Check to see if the column reference is in an invalid place within the
527 * query. We allow column references in most places, except in default
528 * expressions and partition bound expressions.
530 err = NULL;
531 switch (pstate->p_expr_kind)
533 case EXPR_KIND_NONE:
534 Assert(false); /* can't happen */
535 break;
536 case EXPR_KIND_OTHER:
537 case EXPR_KIND_JOIN_ON:
538 case EXPR_KIND_JOIN_USING:
539 case EXPR_KIND_FROM_SUBSELECT:
540 case EXPR_KIND_FROM_FUNCTION:
541 case EXPR_KIND_WHERE:
542 case EXPR_KIND_POLICY:
543 case EXPR_KIND_HAVING:
544 case EXPR_KIND_FILTER:
545 case EXPR_KIND_WINDOW_PARTITION:
546 case EXPR_KIND_WINDOW_ORDER:
547 case EXPR_KIND_WINDOW_FRAME_RANGE:
548 case EXPR_KIND_WINDOW_FRAME_ROWS:
549 case EXPR_KIND_WINDOW_FRAME_GROUPS:
550 case EXPR_KIND_SELECT_TARGET:
551 case EXPR_KIND_INSERT_TARGET:
552 case EXPR_KIND_UPDATE_SOURCE:
553 case EXPR_KIND_UPDATE_TARGET:
554 case EXPR_KIND_MERGE_WHEN:
555 case EXPR_KIND_GROUP_BY:
556 case EXPR_KIND_ORDER_BY:
557 case EXPR_KIND_DISTINCT_ON:
558 case EXPR_KIND_LIMIT:
559 case EXPR_KIND_OFFSET:
560 case EXPR_KIND_RETURNING:
561 case EXPR_KIND_MERGE_RETURNING:
562 case EXPR_KIND_VALUES:
563 case EXPR_KIND_VALUES_SINGLE:
564 case EXPR_KIND_CHECK_CONSTRAINT:
565 case EXPR_KIND_DOMAIN_CHECK:
566 case EXPR_KIND_FUNCTION_DEFAULT:
567 case EXPR_KIND_INDEX_EXPRESSION:
568 case EXPR_KIND_INDEX_PREDICATE:
569 case EXPR_KIND_STATS_EXPRESSION:
570 case EXPR_KIND_ALTER_COL_TRANSFORM:
571 case EXPR_KIND_EXECUTE_PARAMETER:
572 case EXPR_KIND_TRIGGER_WHEN:
573 case EXPR_KIND_PARTITION_EXPRESSION:
574 case EXPR_KIND_CALL_ARGUMENT:
575 case EXPR_KIND_COPY_WHERE:
576 case EXPR_KIND_GENERATED_COLUMN:
577 case EXPR_KIND_CYCLE_MARK:
578 /* okay */
579 break;
581 case EXPR_KIND_COLUMN_DEFAULT:
582 err = _("cannot use column reference in DEFAULT expression");
583 break;
584 case EXPR_KIND_PARTITION_BOUND:
585 err = _("cannot use column reference in partition bound expression");
586 break;
589 * There is intentionally no default: case here, so that the
590 * compiler will warn if we add a new ParseExprKind without
591 * extending this switch. If we do see an unrecognized value at
592 * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
593 * which is sane anyway.
596 if (err)
597 ereport(ERROR,
598 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
599 errmsg_internal("%s", err),
600 parser_errposition(pstate, cref->location)));
603 * Give the PreParseColumnRefHook, if any, first shot. If it returns
604 * non-null then that's all, folks.
606 if (pstate->p_pre_columnref_hook != NULL)
608 node = pstate->p_pre_columnref_hook(pstate, cref);
609 if (node != NULL)
610 return node;
613 /*----------
614 * The allowed syntaxes are:
616 * A First try to resolve as unqualified column name;
617 * if no luck, try to resolve as unqualified table name (A.*).
618 * A.B A is an unqualified table name; B is either a
619 * column or function name (trying column name first).
620 * A.B.C schema A, table B, col or func name C.
621 * A.B.C.D catalog A, schema B, table C, col or func D.
622 * A.* A is an unqualified table name; means whole-row value.
623 * A.B.* whole-row value of table B in schema A.
624 * A.B.C.* whole-row value of table C in schema B in catalog A.
626 * We do not need to cope with bare "*"; that will only be accepted by
627 * the grammar at the top level of a SELECT list, and transformTargetList
628 * will take care of it before it ever gets here. Also, "A.*" etc will
629 * be expanded by transformTargetList if they appear at SELECT top level,
630 * so here we are only going to see them as function or operator inputs.
632 * Currently, if a catalog name is given then it must equal the current
633 * database name; we check it here and then discard it.
634 *----------
636 switch (list_length(cref->fields))
638 case 1:
640 Node *field1 = (Node *) linitial(cref->fields);
642 colname = strVal(field1);
644 /* Try to identify as an unqualified column */
645 node = colNameToVar(pstate, colname, false, cref->location);
647 if (node == NULL)
650 * Not known as a column of any range-table entry.
652 * Try to find the name as a relation. Note that only
653 * relations already entered into the rangetable will be
654 * recognized.
656 * This is a hack for backwards compatibility with
657 * PostQUEL-inspired syntax. The preferred form now is
658 * "rel.*".
660 nsitem = refnameNamespaceItem(pstate, NULL, colname,
661 cref->location,
662 &levels_up);
663 if (nsitem)
664 node = transformWholeRowRef(pstate, nsitem, levels_up,
665 cref->location);
667 break;
669 case 2:
671 Node *field1 = (Node *) linitial(cref->fields);
672 Node *field2 = (Node *) lsecond(cref->fields);
674 relname = strVal(field1);
676 /* Locate the referenced nsitem */
677 nsitem = refnameNamespaceItem(pstate, nspname, relname,
678 cref->location,
679 &levels_up);
680 if (nsitem == NULL)
682 crerr = CRERR_NO_RTE;
683 break;
686 /* Whole-row reference? */
687 if (IsA(field2, A_Star))
689 node = transformWholeRowRef(pstate, nsitem, levels_up,
690 cref->location);
691 break;
694 colname = strVal(field2);
696 /* Try to identify as a column of the nsitem */
697 node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
698 cref->location);
699 if (node == NULL)
701 /* Try it as a function call on the whole row */
702 node = transformWholeRowRef(pstate, nsitem, levels_up,
703 cref->location);
704 node = ParseFuncOrColumn(pstate,
705 list_make1(makeString(colname)),
706 list_make1(node),
707 pstate->p_last_srf,
708 NULL,
709 false,
710 cref->location);
712 break;
714 case 3:
716 Node *field1 = (Node *) linitial(cref->fields);
717 Node *field2 = (Node *) lsecond(cref->fields);
718 Node *field3 = (Node *) lthird(cref->fields);
720 nspname = strVal(field1);
721 relname = strVal(field2);
723 /* Locate the referenced nsitem */
724 nsitem = refnameNamespaceItem(pstate, nspname, relname,
725 cref->location,
726 &levels_up);
727 if (nsitem == NULL)
729 crerr = CRERR_NO_RTE;
730 break;
733 /* Whole-row reference? */
734 if (IsA(field3, A_Star))
736 node = transformWholeRowRef(pstate, nsitem, levels_up,
737 cref->location);
738 break;
741 colname = strVal(field3);
743 /* Try to identify as a column of the nsitem */
744 node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
745 cref->location);
746 if (node == NULL)
748 /* Try it as a function call on the whole row */
749 node = transformWholeRowRef(pstate, nsitem, levels_up,
750 cref->location);
751 node = ParseFuncOrColumn(pstate,
752 list_make1(makeString(colname)),
753 list_make1(node),
754 pstate->p_last_srf,
755 NULL,
756 false,
757 cref->location);
759 break;
761 case 4:
763 Node *field1 = (Node *) linitial(cref->fields);
764 Node *field2 = (Node *) lsecond(cref->fields);
765 Node *field3 = (Node *) lthird(cref->fields);
766 Node *field4 = (Node *) lfourth(cref->fields);
767 char *catname;
769 catname = strVal(field1);
770 nspname = strVal(field2);
771 relname = strVal(field3);
774 * We check the catalog name and then ignore it.
776 if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
778 crerr = CRERR_WRONG_DB;
779 break;
782 /* Locate the referenced nsitem */
783 nsitem = refnameNamespaceItem(pstate, nspname, relname,
784 cref->location,
785 &levels_up);
786 if (nsitem == NULL)
788 crerr = CRERR_NO_RTE;
789 break;
792 /* Whole-row reference? */
793 if (IsA(field4, A_Star))
795 node = transformWholeRowRef(pstate, nsitem, levels_up,
796 cref->location);
797 break;
800 colname = strVal(field4);
802 /* Try to identify as a column of the nsitem */
803 node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
804 cref->location);
805 if (node == NULL)
807 /* Try it as a function call on the whole row */
808 node = transformWholeRowRef(pstate, nsitem, levels_up,
809 cref->location);
810 node = ParseFuncOrColumn(pstate,
811 list_make1(makeString(colname)),
812 list_make1(node),
813 pstate->p_last_srf,
814 NULL,
815 false,
816 cref->location);
818 break;
820 default:
821 crerr = CRERR_TOO_MANY; /* too many dotted names */
822 break;
826 * Now give the PostParseColumnRefHook, if any, a chance. We pass the
827 * translation-so-far so that it can throw an error if it wishes in the
828 * case that it has a conflicting interpretation of the ColumnRef. (If it
829 * just translates anyway, we'll throw an error, because we can't undo
830 * whatever effects the preceding steps may have had on the pstate.) If it
831 * returns NULL, use the standard translation, or throw a suitable error
832 * if there is none.
834 if (pstate->p_post_columnref_hook != NULL)
836 Node *hookresult;
838 hookresult = pstate->p_post_columnref_hook(pstate, cref, node);
839 if (node == NULL)
840 node = hookresult;
841 else if (hookresult != NULL)
842 ereport(ERROR,
843 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
844 errmsg("column reference \"%s\" is ambiguous",
845 NameListToString(cref->fields)),
846 parser_errposition(pstate, cref->location)));
850 * Throw error if no translation found.
852 if (node == NULL)
854 switch (crerr)
856 case CRERR_NO_COLUMN:
857 errorMissingColumn(pstate, relname, colname, cref->location);
858 break;
859 case CRERR_NO_RTE:
860 errorMissingRTE(pstate, makeRangeVar(nspname, relname,
861 cref->location));
862 break;
863 case CRERR_WRONG_DB:
864 ereport(ERROR,
865 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
866 errmsg("cross-database references are not implemented: %s",
867 NameListToString(cref->fields)),
868 parser_errposition(pstate, cref->location)));
869 break;
870 case CRERR_TOO_MANY:
871 ereport(ERROR,
872 (errcode(ERRCODE_SYNTAX_ERROR),
873 errmsg("improper qualified name (too many dotted names): %s",
874 NameListToString(cref->fields)),
875 parser_errposition(pstate, cref->location)));
876 break;
880 return node;
883 static Node *
884 transformParamRef(ParseState *pstate, ParamRef *pref)
886 Node *result;
889 * The core parser knows nothing about Params. If a hook is supplied,
890 * call it. If not, or if the hook returns NULL, throw a generic error.
892 if (pstate->p_paramref_hook != NULL)
893 result = pstate->p_paramref_hook(pstate, pref);
894 else
895 result = NULL;
897 if (result == NULL)
898 ereport(ERROR,
899 (errcode(ERRCODE_UNDEFINED_PARAMETER),
900 errmsg("there is no parameter $%d", pref->number),
901 parser_errposition(pstate, pref->location)));
903 return result;
906 /* Test whether an a_expr is a plain NULL constant or not */
907 static bool
908 exprIsNullConstant(Node *arg)
910 if (arg && IsA(arg, A_Const))
912 A_Const *con = (A_Const *) arg;
914 if (con->isnull)
915 return true;
917 return false;
920 static Node *
921 transformAExprOp(ParseState *pstate, A_Expr *a)
923 Node *lexpr = a->lexpr;
924 Node *rexpr = a->rexpr;
925 Node *result;
928 * Special-case "foo = NULL" and "NULL = foo" for compatibility with
929 * standards-broken products (like Microsoft's). Turn these into IS NULL
930 * exprs. (If either side is a CaseTestExpr, then the expression was
931 * generated internally from a CASE-WHEN expression, and
932 * transform_null_equals does not apply.)
934 if (Transform_null_equals &&
935 list_length(a->name) == 1 &&
936 strcmp(strVal(linitial(a->name)), "=") == 0 &&
937 (exprIsNullConstant(lexpr) || exprIsNullConstant(rexpr)) &&
938 (!IsA(lexpr, CaseTestExpr) && !IsA(rexpr, CaseTestExpr)))
940 NullTest *n = makeNode(NullTest);
942 n->nulltesttype = IS_NULL;
943 n->location = a->location;
945 if (exprIsNullConstant(lexpr))
946 n->arg = (Expr *) rexpr;
947 else
948 n->arg = (Expr *) lexpr;
950 result = transformExprRecurse(pstate, (Node *) n);
952 else if (lexpr && IsA(lexpr, RowExpr) &&
953 rexpr && IsA(rexpr, SubLink) &&
954 ((SubLink *) rexpr)->subLinkType == EXPR_SUBLINK)
957 * Convert "row op subselect" into a ROWCOMPARE sublink. Formerly the
958 * grammar did this, but now that a row construct is allowed anywhere
959 * in expressions, it's easier to do it here.
961 SubLink *s = (SubLink *) rexpr;
963 s->subLinkType = ROWCOMPARE_SUBLINK;
964 s->testexpr = lexpr;
965 s->operName = a->name;
966 s->location = a->location;
967 result = transformExprRecurse(pstate, (Node *) s);
969 else if (lexpr && IsA(lexpr, RowExpr) &&
970 rexpr && IsA(rexpr, RowExpr))
972 /* ROW() op ROW() is handled specially */
973 lexpr = transformExprRecurse(pstate, lexpr);
974 rexpr = transformExprRecurse(pstate, rexpr);
976 result = make_row_comparison_op(pstate,
977 a->name,
978 castNode(RowExpr, lexpr)->args,
979 castNode(RowExpr, rexpr)->args,
980 a->location);
982 else
984 /* Ordinary scalar operator */
985 Node *last_srf = pstate->p_last_srf;
987 lexpr = transformExprRecurse(pstate, lexpr);
988 rexpr = transformExprRecurse(pstate, rexpr);
990 result = (Node *) make_op(pstate,
991 a->name,
992 lexpr,
993 rexpr,
994 last_srf,
995 a->location);
998 return result;
1001 static Node *
1002 transformAExprOpAny(ParseState *pstate, A_Expr *a)
1004 Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1005 Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1007 return (Node *) make_scalar_array_op(pstate,
1008 a->name,
1009 true,
1010 lexpr,
1011 rexpr,
1012 a->location);
1015 static Node *
1016 transformAExprOpAll(ParseState *pstate, A_Expr *a)
1018 Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1019 Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1021 return (Node *) make_scalar_array_op(pstate,
1022 a->name,
1023 false,
1024 lexpr,
1025 rexpr,
1026 a->location);
1029 static Node *
1030 transformAExprDistinct(ParseState *pstate, A_Expr *a)
1032 Node *lexpr = a->lexpr;
1033 Node *rexpr = a->rexpr;
1034 Node *result;
1037 * If either input is an undecorated NULL literal, transform to a NullTest
1038 * on the other input. That's simpler to process than a full DistinctExpr,
1039 * and it avoids needing to require that the datatype have an = operator.
1041 if (exprIsNullConstant(rexpr))
1042 return make_nulltest_from_distinct(pstate, a, lexpr);
1043 if (exprIsNullConstant(lexpr))
1044 return make_nulltest_from_distinct(pstate, a, rexpr);
1046 lexpr = transformExprRecurse(pstate, lexpr);
1047 rexpr = transformExprRecurse(pstate, rexpr);
1049 if (lexpr && IsA(lexpr, RowExpr) &&
1050 rexpr && IsA(rexpr, RowExpr))
1052 /* ROW() op ROW() is handled specially */
1053 result = make_row_distinct_op(pstate, a->name,
1054 (RowExpr *) lexpr,
1055 (RowExpr *) rexpr,
1056 a->location);
1058 else
1060 /* Ordinary scalar operator */
1061 result = (Node *) make_distinct_op(pstate,
1062 a->name,
1063 lexpr,
1064 rexpr,
1065 a->location);
1069 * If it's NOT DISTINCT, we first build a DistinctExpr and then stick a
1070 * NOT on top.
1072 if (a->kind == AEXPR_NOT_DISTINCT)
1073 result = (Node *) makeBoolExpr(NOT_EXPR,
1074 list_make1(result),
1075 a->location);
1077 return result;
1080 static Node *
1081 transformAExprNullIf(ParseState *pstate, A_Expr *a)
1083 Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1084 Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1085 OpExpr *result;
1087 result = (OpExpr *) make_op(pstate,
1088 a->name,
1089 lexpr,
1090 rexpr,
1091 pstate->p_last_srf,
1092 a->location);
1095 * The comparison operator itself should yield boolean ...
1097 if (result->opresulttype != BOOLOID)
1098 ereport(ERROR,
1099 (errcode(ERRCODE_DATATYPE_MISMATCH),
1100 /* translator: %s is name of a SQL construct, eg NULLIF */
1101 errmsg("%s requires = operator to yield boolean", "NULLIF"),
1102 parser_errposition(pstate, a->location)));
1103 if (result->opretset)
1104 ereport(ERROR,
1105 (errcode(ERRCODE_DATATYPE_MISMATCH),
1106 /* translator: %s is name of a SQL construct, eg NULLIF */
1107 errmsg("%s must not return a set", "NULLIF"),
1108 parser_errposition(pstate, a->location)));
1111 * ... but the NullIfExpr will yield the first operand's type.
1113 result->opresulttype = exprType((Node *) linitial(result->args));
1116 * We rely on NullIfExpr and OpExpr being the same struct
1118 NodeSetTag(result, T_NullIfExpr);
1120 return (Node *) result;
1123 static Node *
1124 transformAExprIn(ParseState *pstate, A_Expr *a)
1126 Node *result = NULL;
1127 Node *lexpr;
1128 List *rexprs;
1129 List *rvars;
1130 List *rnonvars;
1131 bool useOr;
1132 ListCell *l;
1135 * If the operator is <>, combine with AND not OR.
1137 if (strcmp(strVal(linitial(a->name)), "<>") == 0)
1138 useOr = false;
1139 else
1140 useOr = true;
1143 * We try to generate a ScalarArrayOpExpr from IN/NOT IN, but this is only
1144 * possible if there is a suitable array type available. If not, we fall
1145 * back to a boolean condition tree with multiple copies of the lefthand
1146 * expression. Also, any IN-list items that contain Vars are handled as
1147 * separate boolean conditions, because that gives the planner more scope
1148 * for optimization on such clauses.
1150 * First step: transform all the inputs, and detect whether any contain
1151 * Vars.
1153 lexpr = transformExprRecurse(pstate, a->lexpr);
1154 rexprs = rvars = rnonvars = NIL;
1155 foreach(l, (List *) a->rexpr)
1157 Node *rexpr = transformExprRecurse(pstate, lfirst(l));
1159 rexprs = lappend(rexprs, rexpr);
1160 if (contain_vars_of_level(rexpr, 0))
1161 rvars = lappend(rvars, rexpr);
1162 else
1163 rnonvars = lappend(rnonvars, rexpr);
1167 * ScalarArrayOpExpr is only going to be useful if there's more than one
1168 * non-Var righthand item.
1170 if (list_length(rnonvars) > 1)
1172 List *allexprs;
1173 Oid scalar_type;
1174 Oid array_type;
1177 * Try to select a common type for the array elements. Note that
1178 * since the LHS' type is first in the list, it will be preferred when
1179 * there is doubt (eg, when all the RHS items are unknown literals).
1181 * Note: use list_concat here not lcons, to avoid damaging rnonvars.
1183 allexprs = list_concat(list_make1(lexpr), rnonvars);
1184 scalar_type = select_common_type(pstate, allexprs, NULL, NULL);
1186 /* We have to verify that the selected type actually works */
1187 if (OidIsValid(scalar_type) &&
1188 !verify_common_type(scalar_type, allexprs))
1189 scalar_type = InvalidOid;
1192 * Do we have an array type to use? Aside from the case where there
1193 * isn't one, we don't risk using ScalarArrayOpExpr when the common
1194 * type is RECORD, because the RowExpr comparison logic below can cope
1195 * with some cases of non-identical row types.
1197 if (OidIsValid(scalar_type) && scalar_type != RECORDOID)
1198 array_type = get_array_type(scalar_type);
1199 else
1200 array_type = InvalidOid;
1201 if (array_type != InvalidOid)
1204 * OK: coerce all the right-hand non-Var inputs to the common type
1205 * and build an ArrayExpr for them.
1207 List *aexprs;
1208 ArrayExpr *newa;
1210 aexprs = NIL;
1211 foreach(l, rnonvars)
1213 Node *rexpr = (Node *) lfirst(l);
1215 rexpr = coerce_to_common_type(pstate, rexpr,
1216 scalar_type,
1217 "IN");
1218 aexprs = lappend(aexprs, rexpr);
1220 newa = makeNode(ArrayExpr);
1221 newa->array_typeid = array_type;
1222 /* array_collid will be set by parse_collate.c */
1223 newa->element_typeid = scalar_type;
1224 newa->elements = aexprs;
1225 newa->multidims = false;
1226 newa->location = -1;
1228 result = (Node *) make_scalar_array_op(pstate,
1229 a->name,
1230 useOr,
1231 lexpr,
1232 (Node *) newa,
1233 a->location);
1235 /* Consider only the Vars (if any) in the loop below */
1236 rexprs = rvars;
1241 * Must do it the hard way, ie, with a boolean expression tree.
1243 foreach(l, rexprs)
1245 Node *rexpr = (Node *) lfirst(l);
1246 Node *cmp;
1248 if (IsA(lexpr, RowExpr) &&
1249 IsA(rexpr, RowExpr))
1251 /* ROW() op ROW() is handled specially */
1252 cmp = make_row_comparison_op(pstate,
1253 a->name,
1254 copyObject(((RowExpr *) lexpr)->args),
1255 ((RowExpr *) rexpr)->args,
1256 a->location);
1258 else
1260 /* Ordinary scalar operator */
1261 cmp = (Node *) make_op(pstate,
1262 a->name,
1263 copyObject(lexpr),
1264 rexpr,
1265 pstate->p_last_srf,
1266 a->location);
1269 cmp = coerce_to_boolean(pstate, cmp, "IN");
1270 if (result == NULL)
1271 result = cmp;
1272 else
1273 result = (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
1274 list_make2(result, cmp),
1275 a->location);
1278 return result;
1281 static Node *
1282 transformAExprBetween(ParseState *pstate, A_Expr *a)
1284 Node *aexpr;
1285 Node *bexpr;
1286 Node *cexpr;
1287 Node *result;
1288 Node *sub1;
1289 Node *sub2;
1290 List *args;
1292 /* Deconstruct A_Expr into three subexprs */
1293 aexpr = a->lexpr;
1294 args = castNode(List, a->rexpr);
1295 Assert(list_length(args) == 2);
1296 bexpr = (Node *) linitial(args);
1297 cexpr = (Node *) lsecond(args);
1300 * Build the equivalent comparison expression. Make copies of
1301 * multiply-referenced subexpressions for safety. (XXX this is really
1302 * wrong since it results in multiple runtime evaluations of what may be
1303 * volatile expressions ...)
1305 * Ideally we would not use hard-wired operators here but instead use
1306 * opclasses. However, mixed data types and other issues make this
1307 * difficult:
1308 * http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
1310 switch (a->kind)
1312 case AEXPR_BETWEEN:
1313 args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1314 aexpr, bexpr,
1315 a->location),
1316 makeSimpleA_Expr(AEXPR_OP, "<=",
1317 copyObject(aexpr), cexpr,
1318 a->location));
1319 result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1320 break;
1321 case AEXPR_NOT_BETWEEN:
1322 args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1323 aexpr, bexpr,
1324 a->location),
1325 makeSimpleA_Expr(AEXPR_OP, ">",
1326 copyObject(aexpr), cexpr,
1327 a->location));
1328 result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1329 break;
1330 case AEXPR_BETWEEN_SYM:
1331 args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1332 aexpr, bexpr,
1333 a->location),
1334 makeSimpleA_Expr(AEXPR_OP, "<=",
1335 copyObject(aexpr), cexpr,
1336 a->location));
1337 sub1 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1338 args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1339 copyObject(aexpr), copyObject(cexpr),
1340 a->location),
1341 makeSimpleA_Expr(AEXPR_OP, "<=",
1342 copyObject(aexpr), copyObject(bexpr),
1343 a->location));
1344 sub2 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1345 args = list_make2(sub1, sub2);
1346 result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1347 break;
1348 case AEXPR_NOT_BETWEEN_SYM:
1349 args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1350 aexpr, bexpr,
1351 a->location),
1352 makeSimpleA_Expr(AEXPR_OP, ">",
1353 copyObject(aexpr), cexpr,
1354 a->location));
1355 sub1 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1356 args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1357 copyObject(aexpr), copyObject(cexpr),
1358 a->location),
1359 makeSimpleA_Expr(AEXPR_OP, ">",
1360 copyObject(aexpr), copyObject(bexpr),
1361 a->location));
1362 sub2 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1363 args = list_make2(sub1, sub2);
1364 result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1365 break;
1366 default:
1367 elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
1368 result = NULL; /* keep compiler quiet */
1369 break;
1372 return transformExprRecurse(pstate, result);
1375 static Node *
1376 transformMergeSupportFunc(ParseState *pstate, MergeSupportFunc *f)
1379 * All we need to do is check that we're in the RETURNING list of a MERGE
1380 * command. If so, we just return the node as-is.
1382 if (pstate->p_expr_kind != EXPR_KIND_MERGE_RETURNING)
1384 ParseState *parent_pstate = pstate->parentParseState;
1386 while (parent_pstate &&
1387 parent_pstate->p_expr_kind != EXPR_KIND_MERGE_RETURNING)
1388 parent_pstate = parent_pstate->parentParseState;
1390 if (!parent_pstate)
1391 ereport(ERROR,
1392 errcode(ERRCODE_SYNTAX_ERROR),
1393 errmsg("MERGE_ACTION() can only be used in the RETURNING list of a MERGE command"),
1394 parser_errposition(pstate, f->location));
1397 return (Node *) f;
1400 static Node *
1401 transformBoolExpr(ParseState *pstate, BoolExpr *a)
1403 List *args = NIL;
1404 const char *opname;
1405 ListCell *lc;
1407 switch (a->boolop)
1409 case AND_EXPR:
1410 opname = "AND";
1411 break;
1412 case OR_EXPR:
1413 opname = "OR";
1414 break;
1415 case NOT_EXPR:
1416 opname = "NOT";
1417 break;
1418 default:
1419 elog(ERROR, "unrecognized boolop: %d", (int) a->boolop);
1420 opname = NULL; /* keep compiler quiet */
1421 break;
1424 foreach(lc, a->args)
1426 Node *arg = (Node *) lfirst(lc);
1428 arg = transformExprRecurse(pstate, arg);
1429 arg = coerce_to_boolean(pstate, arg, opname);
1430 args = lappend(args, arg);
1433 return (Node *) makeBoolExpr(a->boolop, args, a->location);
1436 static Node *
1437 transformFuncCall(ParseState *pstate, FuncCall *fn)
1439 Node *last_srf = pstate->p_last_srf;
1440 List *targs;
1441 ListCell *args;
1443 /* Transform the list of arguments ... */
1444 targs = NIL;
1445 foreach(args, fn->args)
1447 targs = lappend(targs, transformExprRecurse(pstate,
1448 (Node *) lfirst(args)));
1452 * When WITHIN GROUP is used, we treat its ORDER BY expressions as
1453 * additional arguments to the function, for purposes of function lookup
1454 * and argument type coercion. So, transform each such expression and add
1455 * them to the targs list. We don't explicitly mark where each argument
1456 * came from, but ParseFuncOrColumn can tell what's what by reference to
1457 * list_length(fn->agg_order).
1459 if (fn->agg_within_group)
1461 Assert(fn->agg_order != NIL);
1462 foreach(args, fn->agg_order)
1464 SortBy *arg = (SortBy *) lfirst(args);
1466 targs = lappend(targs, transformExpr(pstate, arg->node,
1467 EXPR_KIND_ORDER_BY));
1471 /* ... and hand off to ParseFuncOrColumn */
1472 return ParseFuncOrColumn(pstate,
1473 fn->funcname,
1474 targs,
1475 last_srf,
1477 false,
1478 fn->location);
1481 static Node *
1482 transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref)
1484 SubLink *sublink;
1485 RowExpr *rexpr;
1486 Query *qtree;
1487 TargetEntry *tle;
1489 /* We should only see this in first-stage processing of UPDATE tlists */
1490 Assert(pstate->p_expr_kind == EXPR_KIND_UPDATE_SOURCE);
1492 /* We only need to transform the source if this is the first column */
1493 if (maref->colno == 1)
1496 * For now, we only allow EXPR SubLinks and RowExprs as the source of
1497 * an UPDATE multiassignment. This is sufficient to cover interesting
1498 * cases; at worst, someone would have to write (SELECT * FROM expr)
1499 * to expand a composite-returning expression of another form.
1501 if (IsA(maref->source, SubLink) &&
1502 ((SubLink *) maref->source)->subLinkType == EXPR_SUBLINK)
1504 /* Relabel it as a MULTIEXPR_SUBLINK */
1505 sublink = (SubLink *) maref->source;
1506 sublink->subLinkType = MULTIEXPR_SUBLINK;
1507 /* And transform it */
1508 sublink = (SubLink *) transformExprRecurse(pstate,
1509 (Node *) sublink);
1511 qtree = castNode(Query, sublink->subselect);
1513 /* Check subquery returns required number of columns */
1514 if (count_nonjunk_tlist_entries(qtree->targetList) != maref->ncolumns)
1515 ereport(ERROR,
1516 (errcode(ERRCODE_SYNTAX_ERROR),
1517 errmsg("number of columns does not match number of values"),
1518 parser_errposition(pstate, sublink->location)));
1521 * Build a resjunk tlist item containing the MULTIEXPR SubLink,
1522 * and add it to pstate->p_multiassign_exprs, whence it will later
1523 * get appended to the completed targetlist. We needn't worry
1524 * about selecting a resno for it; transformUpdateStmt will do
1525 * that.
1527 tle = makeTargetEntry((Expr *) sublink, 0, NULL, true);
1528 pstate->p_multiassign_exprs = lappend(pstate->p_multiassign_exprs,
1529 tle);
1532 * Assign a unique-within-this-targetlist ID to the MULTIEXPR
1533 * SubLink. We can just use its position in the
1534 * p_multiassign_exprs list.
1536 sublink->subLinkId = list_length(pstate->p_multiassign_exprs);
1538 else if (IsA(maref->source, RowExpr))
1540 /* Transform the RowExpr, allowing SetToDefault items */
1541 rexpr = (RowExpr *) transformRowExpr(pstate,
1542 (RowExpr *) maref->source,
1543 true);
1545 /* Check it returns required number of columns */
1546 if (list_length(rexpr->args) != maref->ncolumns)
1547 ereport(ERROR,
1548 (errcode(ERRCODE_SYNTAX_ERROR),
1549 errmsg("number of columns does not match number of values"),
1550 parser_errposition(pstate, rexpr->location)));
1553 * Temporarily append it to p_multiassign_exprs, so we can get it
1554 * back when we come back here for additional columns.
1556 tle = makeTargetEntry((Expr *) rexpr, 0, NULL, true);
1557 pstate->p_multiassign_exprs = lappend(pstate->p_multiassign_exprs,
1558 tle);
1560 else
1561 ereport(ERROR,
1562 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1563 errmsg("source for a multiple-column UPDATE item must be a sub-SELECT or ROW() expression"),
1564 parser_errposition(pstate, exprLocation(maref->source))));
1566 else
1569 * Second or later column in a multiassignment. Re-fetch the
1570 * transformed SubLink or RowExpr, which we assume is still the last
1571 * entry in p_multiassign_exprs.
1573 Assert(pstate->p_multiassign_exprs != NIL);
1574 tle = (TargetEntry *) llast(pstate->p_multiassign_exprs);
1578 * Emit the appropriate output expression for the current column
1580 if (IsA(tle->expr, SubLink))
1582 Param *param;
1584 sublink = (SubLink *) tle->expr;
1585 Assert(sublink->subLinkType == MULTIEXPR_SUBLINK);
1586 qtree = castNode(Query, sublink->subselect);
1588 /* Build a Param representing the current subquery output column */
1589 tle = (TargetEntry *) list_nth(qtree->targetList, maref->colno - 1);
1590 Assert(!tle->resjunk);
1592 param = makeNode(Param);
1593 param->paramkind = PARAM_MULTIEXPR;
1594 param->paramid = (sublink->subLinkId << 16) | maref->colno;
1595 param->paramtype = exprType((Node *) tle->expr);
1596 param->paramtypmod = exprTypmod((Node *) tle->expr);
1597 param->paramcollid = exprCollation((Node *) tle->expr);
1598 param->location = exprLocation((Node *) tle->expr);
1600 return (Node *) param;
1603 if (IsA(tle->expr, RowExpr))
1605 Node *result;
1607 rexpr = (RowExpr *) tle->expr;
1609 /* Just extract and return the next element of the RowExpr */
1610 result = (Node *) list_nth(rexpr->args, maref->colno - 1);
1613 * If we're at the last column, delete the RowExpr from
1614 * p_multiassign_exprs; we don't need it anymore, and don't want it in
1615 * the finished UPDATE tlist. We assume this is still the last entry
1616 * in p_multiassign_exprs.
1618 if (maref->colno == maref->ncolumns)
1619 pstate->p_multiassign_exprs =
1620 list_delete_last(pstate->p_multiassign_exprs);
1622 return result;
1625 elog(ERROR, "unexpected expr type in multiassign list");
1626 return NULL; /* keep compiler quiet */
1629 static Node *
1630 transformCaseExpr(ParseState *pstate, CaseExpr *c)
1632 CaseExpr *newc = makeNode(CaseExpr);
1633 Node *last_srf = pstate->p_last_srf;
1634 Node *arg;
1635 CaseTestExpr *placeholder;
1636 List *newargs;
1637 List *resultexprs;
1638 ListCell *l;
1639 Node *defresult;
1640 Oid ptype;
1642 /* transform the test expression, if any */
1643 arg = transformExprRecurse(pstate, (Node *) c->arg);
1645 /* generate placeholder for test expression */
1646 if (arg)
1649 * If test expression is an untyped literal, force it to text. We have
1650 * to do something now because we won't be able to do this coercion on
1651 * the placeholder. This is not as flexible as what was done in 7.4
1652 * and before, but it's good enough to handle the sort of silly coding
1653 * commonly seen.
1655 if (exprType(arg) == UNKNOWNOID)
1656 arg = coerce_to_common_type(pstate, arg, TEXTOID, "CASE");
1659 * Run collation assignment on the test expression so that we know
1660 * what collation to mark the placeholder with. In principle we could
1661 * leave it to parse_collate.c to do that later, but propagating the
1662 * result to the CaseTestExpr would be unnecessarily complicated.
1664 assign_expr_collations(pstate, arg);
1666 placeholder = makeNode(CaseTestExpr);
1667 placeholder->typeId = exprType(arg);
1668 placeholder->typeMod = exprTypmod(arg);
1669 placeholder->collation = exprCollation(arg);
1671 else
1672 placeholder = NULL;
1674 newc->arg = (Expr *) arg;
1676 /* transform the list of arguments */
1677 newargs = NIL;
1678 resultexprs = NIL;
1679 foreach(l, c->args)
1681 CaseWhen *w = lfirst_node(CaseWhen, l);
1682 CaseWhen *neww = makeNode(CaseWhen);
1683 Node *warg;
1685 warg = (Node *) w->expr;
1686 if (placeholder)
1688 /* shorthand form was specified, so expand... */
1689 warg = (Node *) makeSimpleA_Expr(AEXPR_OP, "=",
1690 (Node *) placeholder,
1691 warg,
1692 w->location);
1694 neww->expr = (Expr *) transformExprRecurse(pstate, warg);
1696 neww->expr = (Expr *) coerce_to_boolean(pstate,
1697 (Node *) neww->expr,
1698 "CASE/WHEN");
1700 warg = (Node *) w->result;
1701 neww->result = (Expr *) transformExprRecurse(pstate, warg);
1702 neww->location = w->location;
1704 newargs = lappend(newargs, neww);
1705 resultexprs = lappend(resultexprs, neww->result);
1708 newc->args = newargs;
1710 /* transform the default clause */
1711 defresult = (Node *) c->defresult;
1712 if (defresult == NULL)
1714 A_Const *n = makeNode(A_Const);
1716 n->isnull = true;
1717 n->location = -1;
1718 defresult = (Node *) n;
1720 newc->defresult = (Expr *) transformExprRecurse(pstate, defresult);
1723 * Note: default result is considered the most significant type in
1724 * determining preferred type. This is how the code worked before, but it
1725 * seems a little bogus to me --- tgl
1727 resultexprs = lcons(newc->defresult, resultexprs);
1729 ptype = select_common_type(pstate, resultexprs, "CASE", NULL);
1730 Assert(OidIsValid(ptype));
1731 newc->casetype = ptype;
1732 /* casecollid will be set by parse_collate.c */
1734 /* Convert default result clause, if necessary */
1735 newc->defresult = (Expr *)
1736 coerce_to_common_type(pstate,
1737 (Node *) newc->defresult,
1738 ptype,
1739 "CASE/ELSE");
1741 /* Convert when-clause results, if necessary */
1742 foreach(l, newc->args)
1744 CaseWhen *w = (CaseWhen *) lfirst(l);
1746 w->result = (Expr *)
1747 coerce_to_common_type(pstate,
1748 (Node *) w->result,
1749 ptype,
1750 "CASE/WHEN");
1753 /* if any subexpression contained a SRF, complain */
1754 if (pstate->p_last_srf != last_srf)
1755 ereport(ERROR,
1756 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1757 /* translator: %s is name of a SQL construct, eg GROUP BY */
1758 errmsg("set-returning functions are not allowed in %s",
1759 "CASE"),
1760 errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
1761 parser_errposition(pstate,
1762 exprLocation(pstate->p_last_srf))));
1764 newc->location = c->location;
1766 return (Node *) newc;
1769 static Node *
1770 transformSubLink(ParseState *pstate, SubLink *sublink)
1772 Node *result = (Node *) sublink;
1773 Query *qtree;
1774 const char *err;
1777 * Check to see if the sublink is in an invalid place within the query. We
1778 * allow sublinks everywhere in SELECT/INSERT/UPDATE/DELETE/MERGE, but
1779 * generally not in utility statements.
1781 err = NULL;
1782 switch (pstate->p_expr_kind)
1784 case EXPR_KIND_NONE:
1785 Assert(false); /* can't happen */
1786 break;
1787 case EXPR_KIND_OTHER:
1788 /* Accept sublink here; caller must throw error if wanted */
1789 break;
1790 case EXPR_KIND_JOIN_ON:
1791 case EXPR_KIND_JOIN_USING:
1792 case EXPR_KIND_FROM_SUBSELECT:
1793 case EXPR_KIND_FROM_FUNCTION:
1794 case EXPR_KIND_WHERE:
1795 case EXPR_KIND_POLICY:
1796 case EXPR_KIND_HAVING:
1797 case EXPR_KIND_FILTER:
1798 case EXPR_KIND_WINDOW_PARTITION:
1799 case EXPR_KIND_WINDOW_ORDER:
1800 case EXPR_KIND_WINDOW_FRAME_RANGE:
1801 case EXPR_KIND_WINDOW_FRAME_ROWS:
1802 case EXPR_KIND_WINDOW_FRAME_GROUPS:
1803 case EXPR_KIND_SELECT_TARGET:
1804 case EXPR_KIND_INSERT_TARGET:
1805 case EXPR_KIND_UPDATE_SOURCE:
1806 case EXPR_KIND_UPDATE_TARGET:
1807 case EXPR_KIND_MERGE_WHEN:
1808 case EXPR_KIND_GROUP_BY:
1809 case EXPR_KIND_ORDER_BY:
1810 case EXPR_KIND_DISTINCT_ON:
1811 case EXPR_KIND_LIMIT:
1812 case EXPR_KIND_OFFSET:
1813 case EXPR_KIND_RETURNING:
1814 case EXPR_KIND_MERGE_RETURNING:
1815 case EXPR_KIND_VALUES:
1816 case EXPR_KIND_VALUES_SINGLE:
1817 case EXPR_KIND_CYCLE_MARK:
1818 /* okay */
1819 break;
1820 case EXPR_KIND_CHECK_CONSTRAINT:
1821 case EXPR_KIND_DOMAIN_CHECK:
1822 err = _("cannot use subquery in check constraint");
1823 break;
1824 case EXPR_KIND_COLUMN_DEFAULT:
1825 case EXPR_KIND_FUNCTION_DEFAULT:
1826 err = _("cannot use subquery in DEFAULT expression");
1827 break;
1828 case EXPR_KIND_INDEX_EXPRESSION:
1829 err = _("cannot use subquery in index expression");
1830 break;
1831 case EXPR_KIND_INDEX_PREDICATE:
1832 err = _("cannot use subquery in index predicate");
1833 break;
1834 case EXPR_KIND_STATS_EXPRESSION:
1835 err = _("cannot use subquery in statistics expression");
1836 break;
1837 case EXPR_KIND_ALTER_COL_TRANSFORM:
1838 err = _("cannot use subquery in transform expression");
1839 break;
1840 case EXPR_KIND_EXECUTE_PARAMETER:
1841 err = _("cannot use subquery in EXECUTE parameter");
1842 break;
1843 case EXPR_KIND_TRIGGER_WHEN:
1844 err = _("cannot use subquery in trigger WHEN condition");
1845 break;
1846 case EXPR_KIND_PARTITION_BOUND:
1847 err = _("cannot use subquery in partition bound");
1848 break;
1849 case EXPR_KIND_PARTITION_EXPRESSION:
1850 err = _("cannot use subquery in partition key expression");
1851 break;
1852 case EXPR_KIND_CALL_ARGUMENT:
1853 err = _("cannot use subquery in CALL argument");
1854 break;
1855 case EXPR_KIND_COPY_WHERE:
1856 err = _("cannot use subquery in COPY FROM WHERE condition");
1857 break;
1858 case EXPR_KIND_GENERATED_COLUMN:
1859 err = _("cannot use subquery in column generation expression");
1860 break;
1863 * There is intentionally no default: case here, so that the
1864 * compiler will warn if we add a new ParseExprKind without
1865 * extending this switch. If we do see an unrecognized value at
1866 * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
1867 * which is sane anyway.
1870 if (err)
1871 ereport(ERROR,
1872 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1873 errmsg_internal("%s", err),
1874 parser_errposition(pstate, sublink->location)));
1876 pstate->p_hasSubLinks = true;
1879 * OK, let's transform the sub-SELECT.
1881 qtree = parse_sub_analyze(sublink->subselect, pstate, NULL, false, true);
1884 * Check that we got a SELECT. Anything else should be impossible given
1885 * restrictions of the grammar, but check anyway.
1887 if (!IsA(qtree, Query) ||
1888 qtree->commandType != CMD_SELECT)
1889 elog(ERROR, "unexpected non-SELECT command in SubLink");
1891 sublink->subselect = (Node *) qtree;
1893 if (sublink->subLinkType == EXISTS_SUBLINK)
1896 * EXISTS needs no test expression or combining operator. These fields
1897 * should be null already, but make sure.
1899 sublink->testexpr = NULL;
1900 sublink->operName = NIL;
1902 else if (sublink->subLinkType == EXPR_SUBLINK ||
1903 sublink->subLinkType == ARRAY_SUBLINK)
1906 * Make sure the subselect delivers a single column (ignoring resjunk
1907 * targets).
1909 if (count_nonjunk_tlist_entries(qtree->targetList) != 1)
1910 ereport(ERROR,
1911 (errcode(ERRCODE_SYNTAX_ERROR),
1912 errmsg("subquery must return only one column"),
1913 parser_errposition(pstate, sublink->location)));
1916 * EXPR and ARRAY need no test expression or combining operator. These
1917 * fields should be null already, but make sure.
1919 sublink->testexpr = NULL;
1920 sublink->operName = NIL;
1922 else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
1924 /* Same as EXPR case, except no restriction on number of columns */
1925 sublink->testexpr = NULL;
1926 sublink->operName = NIL;
1928 else
1930 /* ALL, ANY, or ROWCOMPARE: generate row-comparing expression */
1931 Node *lefthand;
1932 List *left_list;
1933 List *right_list;
1934 ListCell *l;
1937 * If the source was "x IN (select)", convert to "x = ANY (select)".
1939 if (sublink->operName == NIL)
1940 sublink->operName = list_make1(makeString("="));
1943 * Transform lefthand expression, and convert to a list
1945 lefthand = transformExprRecurse(pstate, sublink->testexpr);
1946 if (lefthand && IsA(lefthand, RowExpr))
1947 left_list = ((RowExpr *) lefthand)->args;
1948 else
1949 left_list = list_make1(lefthand);
1952 * Build a list of PARAM_SUBLINK nodes representing the output columns
1953 * of the subquery.
1955 right_list = NIL;
1956 foreach(l, qtree->targetList)
1958 TargetEntry *tent = (TargetEntry *) lfirst(l);
1959 Param *param;
1961 if (tent->resjunk)
1962 continue;
1964 param = makeNode(Param);
1965 param->paramkind = PARAM_SUBLINK;
1966 param->paramid = tent->resno;
1967 param->paramtype = exprType((Node *) tent->expr);
1968 param->paramtypmod = exprTypmod((Node *) tent->expr);
1969 param->paramcollid = exprCollation((Node *) tent->expr);
1970 param->location = -1;
1972 right_list = lappend(right_list, param);
1976 * We could rely on make_row_comparison_op to complain if the list
1977 * lengths differ, but we prefer to generate a more specific error
1978 * message.
1980 if (list_length(left_list) < list_length(right_list))
1981 ereport(ERROR,
1982 (errcode(ERRCODE_SYNTAX_ERROR),
1983 errmsg("subquery has too many columns"),
1984 parser_errposition(pstate, sublink->location)));
1985 if (list_length(left_list) > list_length(right_list))
1986 ereport(ERROR,
1987 (errcode(ERRCODE_SYNTAX_ERROR),
1988 errmsg("subquery has too few columns"),
1989 parser_errposition(pstate, sublink->location)));
1992 * Identify the combining operator(s) and generate a suitable
1993 * row-comparison expression.
1995 sublink->testexpr = make_row_comparison_op(pstate,
1996 sublink->operName,
1997 left_list,
1998 right_list,
1999 sublink->location);
2002 return result;
2006 * transformArrayExpr
2008 * If the caller specifies the target type, the resulting array will
2009 * be of exactly that type. Otherwise we try to infer a common type
2010 * for the elements using select_common_type().
2012 static Node *
2013 transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
2014 Oid array_type, Oid element_type, int32 typmod)
2016 ArrayExpr *newa = makeNode(ArrayExpr);
2017 List *newelems = NIL;
2018 List *newcoercedelems = NIL;
2019 ListCell *element;
2020 Oid coerce_type;
2021 bool coerce_hard;
2024 * Transform the element expressions
2026 * Assume that the array is one-dimensional unless we find an array-type
2027 * element expression.
2029 newa->multidims = false;
2030 foreach(element, a->elements)
2032 Node *e = (Node *) lfirst(element);
2033 Node *newe;
2036 * If an element is itself an A_ArrayExpr, recurse directly so that we
2037 * can pass down any target type we were given.
2039 if (IsA(e, A_ArrayExpr))
2041 newe = transformArrayExpr(pstate,
2042 (A_ArrayExpr *) e,
2043 array_type,
2044 element_type,
2045 typmod);
2046 /* we certainly have an array here */
2047 Assert(array_type == InvalidOid || array_type == exprType(newe));
2048 newa->multidims = true;
2050 else
2052 newe = transformExprRecurse(pstate, e);
2055 * Check for sub-array expressions, if we haven't already found
2056 * one.
2058 if (!newa->multidims && type_is_array(exprType(newe)))
2059 newa->multidims = true;
2062 newelems = lappend(newelems, newe);
2066 * Select a target type for the elements.
2068 * If we haven't been given a target array type, we must try to deduce a
2069 * common type based on the types of the individual elements present.
2071 if (OidIsValid(array_type))
2073 /* Caller must ensure array_type matches element_type */
2074 Assert(OidIsValid(element_type));
2075 coerce_type = (newa->multidims ? array_type : element_type);
2076 coerce_hard = true;
2078 else
2080 /* Can't handle an empty array without a target type */
2081 if (newelems == NIL)
2082 ereport(ERROR,
2083 (errcode(ERRCODE_INDETERMINATE_DATATYPE),
2084 errmsg("cannot determine type of empty array"),
2085 errhint("Explicitly cast to the desired type, "
2086 "for example ARRAY[]::integer[]."),
2087 parser_errposition(pstate, a->location)));
2089 /* Select a common type for the elements */
2090 coerce_type = select_common_type(pstate, newelems, "ARRAY", NULL);
2092 if (newa->multidims)
2094 array_type = coerce_type;
2095 element_type = get_element_type(array_type);
2096 if (!OidIsValid(element_type))
2097 ereport(ERROR,
2098 (errcode(ERRCODE_UNDEFINED_OBJECT),
2099 errmsg("could not find element type for data type %s",
2100 format_type_be(array_type)),
2101 parser_errposition(pstate, a->location)));
2103 else
2105 element_type = coerce_type;
2106 array_type = get_array_type(element_type);
2107 if (!OidIsValid(array_type))
2108 ereport(ERROR,
2109 (errcode(ERRCODE_UNDEFINED_OBJECT),
2110 errmsg("could not find array type for data type %s",
2111 format_type_be(element_type)),
2112 parser_errposition(pstate, a->location)));
2114 coerce_hard = false;
2118 * Coerce elements to target type
2120 * If the array has been explicitly cast, then the elements are in turn
2121 * explicitly coerced.
2123 * If the array's type was merely derived from the common type of its
2124 * elements, then the elements are implicitly coerced to the common type.
2125 * This is consistent with other uses of select_common_type().
2127 foreach(element, newelems)
2129 Node *e = (Node *) lfirst(element);
2130 Node *newe;
2132 if (coerce_hard)
2134 newe = coerce_to_target_type(pstate, e,
2135 exprType(e),
2136 coerce_type,
2137 typmod,
2138 COERCION_EXPLICIT,
2139 COERCE_EXPLICIT_CAST,
2140 -1);
2141 if (newe == NULL)
2142 ereport(ERROR,
2143 (errcode(ERRCODE_CANNOT_COERCE),
2144 errmsg("cannot cast type %s to %s",
2145 format_type_be(exprType(e)),
2146 format_type_be(coerce_type)),
2147 parser_errposition(pstate, exprLocation(e))));
2149 else
2150 newe = coerce_to_common_type(pstate, e,
2151 coerce_type,
2152 "ARRAY");
2153 newcoercedelems = lappend(newcoercedelems, newe);
2156 newa->array_typeid = array_type;
2157 /* array_collid will be set by parse_collate.c */
2158 newa->element_typeid = element_type;
2159 newa->elements = newcoercedelems;
2160 newa->location = a->location;
2162 return (Node *) newa;
2165 static Node *
2166 transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
2168 RowExpr *newr;
2169 char fname[16];
2170 int fnum;
2172 newr = makeNode(RowExpr);
2174 /* Transform the field expressions */
2175 newr->args = transformExpressionList(pstate, r->args,
2176 pstate->p_expr_kind, allowDefault);
2178 /* Disallow more columns than will fit in a tuple */
2179 if (list_length(newr->args) > MaxTupleAttributeNumber)
2180 ereport(ERROR,
2181 (errcode(ERRCODE_TOO_MANY_COLUMNS),
2182 errmsg("ROW expressions can have at most %d entries",
2183 MaxTupleAttributeNumber),
2184 parser_errposition(pstate, r->location)));
2186 /* Barring later casting, we consider the type RECORD */
2187 newr->row_typeid = RECORDOID;
2188 newr->row_format = COERCE_IMPLICIT_CAST;
2190 /* ROW() has anonymous columns, so invent some field names */
2191 newr->colnames = NIL;
2192 for (fnum = 1; fnum <= list_length(newr->args); fnum++)
2194 snprintf(fname, sizeof(fname), "f%d", fnum);
2195 newr->colnames = lappend(newr->colnames, makeString(pstrdup(fname)));
2198 newr->location = r->location;
2200 return (Node *) newr;
2203 static Node *
2204 transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c)
2206 CoalesceExpr *newc = makeNode(CoalesceExpr);
2207 Node *last_srf = pstate->p_last_srf;
2208 List *newargs = NIL;
2209 List *newcoercedargs = NIL;
2210 ListCell *args;
2212 foreach(args, c->args)
2214 Node *e = (Node *) lfirst(args);
2215 Node *newe;
2217 newe = transformExprRecurse(pstate, e);
2218 newargs = lappend(newargs, newe);
2221 newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
2222 /* coalescecollid will be set by parse_collate.c */
2224 /* Convert arguments if necessary */
2225 foreach(args, newargs)
2227 Node *e = (Node *) lfirst(args);
2228 Node *newe;
2230 newe = coerce_to_common_type(pstate, e,
2231 newc->coalescetype,
2232 "COALESCE");
2233 newcoercedargs = lappend(newcoercedargs, newe);
2236 /* if any subexpression contained a SRF, complain */
2237 if (pstate->p_last_srf != last_srf)
2238 ereport(ERROR,
2239 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2240 /* translator: %s is name of a SQL construct, eg GROUP BY */
2241 errmsg("set-returning functions are not allowed in %s",
2242 "COALESCE"),
2243 errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
2244 parser_errposition(pstate,
2245 exprLocation(pstate->p_last_srf))));
2247 newc->args = newcoercedargs;
2248 newc->location = c->location;
2249 return (Node *) newc;
2252 static Node *
2253 transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
2255 MinMaxExpr *newm = makeNode(MinMaxExpr);
2256 List *newargs = NIL;
2257 List *newcoercedargs = NIL;
2258 const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
2259 ListCell *args;
2261 newm->op = m->op;
2262 foreach(args, m->args)
2264 Node *e = (Node *) lfirst(args);
2265 Node *newe;
2267 newe = transformExprRecurse(pstate, e);
2268 newargs = lappend(newargs, newe);
2271 newm->minmaxtype = select_common_type(pstate, newargs, funcname, NULL);
2272 /* minmaxcollid and inputcollid will be set by parse_collate.c */
2274 /* Convert arguments if necessary */
2275 foreach(args, newargs)
2277 Node *e = (Node *) lfirst(args);
2278 Node *newe;
2280 newe = coerce_to_common_type(pstate, e,
2281 newm->minmaxtype,
2282 funcname);
2283 newcoercedargs = lappend(newcoercedargs, newe);
2286 newm->args = newcoercedargs;
2287 newm->location = m->location;
2288 return (Node *) newm;
2291 static Node *
2292 transformSQLValueFunction(ParseState *pstate, SQLValueFunction *svf)
2295 * All we need to do is insert the correct result type and (where needed)
2296 * validate the typmod, so we just modify the node in-place.
2298 switch (svf->op)
2300 case SVFOP_CURRENT_DATE:
2301 svf->type = DATEOID;
2302 break;
2303 case SVFOP_CURRENT_TIME:
2304 svf->type = TIMETZOID;
2305 break;
2306 case SVFOP_CURRENT_TIME_N:
2307 svf->type = TIMETZOID;
2308 svf->typmod = anytime_typmod_check(true, svf->typmod);
2309 break;
2310 case SVFOP_CURRENT_TIMESTAMP:
2311 svf->type = TIMESTAMPTZOID;
2312 break;
2313 case SVFOP_CURRENT_TIMESTAMP_N:
2314 svf->type = TIMESTAMPTZOID;
2315 svf->typmod = anytimestamp_typmod_check(true, svf->typmod);
2316 break;
2317 case SVFOP_LOCALTIME:
2318 svf->type = TIMEOID;
2319 break;
2320 case SVFOP_LOCALTIME_N:
2321 svf->type = TIMEOID;
2322 svf->typmod = anytime_typmod_check(false, svf->typmod);
2323 break;
2324 case SVFOP_LOCALTIMESTAMP:
2325 svf->type = TIMESTAMPOID;
2326 break;
2327 case SVFOP_LOCALTIMESTAMP_N:
2328 svf->type = TIMESTAMPOID;
2329 svf->typmod = anytimestamp_typmod_check(false, svf->typmod);
2330 break;
2331 case SVFOP_CURRENT_ROLE:
2332 case SVFOP_CURRENT_USER:
2333 case SVFOP_USER:
2334 case SVFOP_SESSION_USER:
2335 case SVFOP_CURRENT_CATALOG:
2336 case SVFOP_CURRENT_SCHEMA:
2337 svf->type = NAMEOID;
2338 break;
2341 return (Node *) svf;
2344 static Node *
2345 transformXmlExpr(ParseState *pstate, XmlExpr *x)
2347 XmlExpr *newx;
2348 ListCell *lc;
2349 int i;
2351 newx = makeNode(XmlExpr);
2352 newx->op = x->op;
2353 if (x->name)
2354 newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
2355 else
2356 newx->name = NULL;
2357 newx->xmloption = x->xmloption;
2358 newx->type = XMLOID; /* this just marks the node as transformed */
2359 newx->typmod = -1;
2360 newx->location = x->location;
2363 * gram.y built the named args as a list of ResTarget. Transform each,
2364 * and break the names out as a separate list.
2366 newx->named_args = NIL;
2367 newx->arg_names = NIL;
2369 foreach(lc, x->named_args)
2371 ResTarget *r = lfirst_node(ResTarget, lc);
2372 Node *expr;
2373 char *argname;
2375 expr = transformExprRecurse(pstate, r->val);
2377 if (r->name)
2378 argname = map_sql_identifier_to_xml_name(r->name, false, false);
2379 else if (IsA(r->val, ColumnRef))
2380 argname = map_sql_identifier_to_xml_name(FigureColname(r->val),
2381 true, false);
2382 else
2384 ereport(ERROR,
2385 (errcode(ERRCODE_SYNTAX_ERROR),
2386 x->op == IS_XMLELEMENT
2387 ? errmsg("unnamed XML attribute value must be a column reference")
2388 : errmsg("unnamed XML element value must be a column reference"),
2389 parser_errposition(pstate, r->location)));
2390 argname = NULL; /* keep compiler quiet */
2393 /* reject duplicate argnames in XMLELEMENT only */
2394 if (x->op == IS_XMLELEMENT)
2396 ListCell *lc2;
2398 foreach(lc2, newx->arg_names)
2400 if (strcmp(argname, strVal(lfirst(lc2))) == 0)
2401 ereport(ERROR,
2402 (errcode(ERRCODE_SYNTAX_ERROR),
2403 errmsg("XML attribute name \"%s\" appears more than once",
2404 argname),
2405 parser_errposition(pstate, r->location)));
2409 newx->named_args = lappend(newx->named_args, expr);
2410 newx->arg_names = lappend(newx->arg_names, makeString(argname));
2413 /* The other arguments are of varying types depending on the function */
2414 newx->args = NIL;
2415 i = 0;
2416 foreach(lc, x->args)
2418 Node *e = (Node *) lfirst(lc);
2419 Node *newe;
2421 newe = transformExprRecurse(pstate, e);
2422 switch (x->op)
2424 case IS_XMLCONCAT:
2425 newe = coerce_to_specific_type(pstate, newe, XMLOID,
2426 "XMLCONCAT");
2427 break;
2428 case IS_XMLELEMENT:
2429 /* no coercion necessary */
2430 break;
2431 case IS_XMLFOREST:
2432 newe = coerce_to_specific_type(pstate, newe, XMLOID,
2433 "XMLFOREST");
2434 break;
2435 case IS_XMLPARSE:
2436 if (i == 0)
2437 newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2438 "XMLPARSE");
2439 else
2440 newe = coerce_to_boolean(pstate, newe, "XMLPARSE");
2441 break;
2442 case IS_XMLPI:
2443 newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2444 "XMLPI");
2445 break;
2446 case IS_XMLROOT:
2447 if (i == 0)
2448 newe = coerce_to_specific_type(pstate, newe, XMLOID,
2449 "XMLROOT");
2450 else if (i == 1)
2451 newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2452 "XMLROOT");
2453 else
2454 newe = coerce_to_specific_type(pstate, newe, INT4OID,
2455 "XMLROOT");
2456 break;
2457 case IS_XMLSERIALIZE:
2458 /* not handled here */
2459 Assert(false);
2460 break;
2461 case IS_DOCUMENT:
2462 newe = coerce_to_specific_type(pstate, newe, XMLOID,
2463 "IS DOCUMENT");
2464 break;
2466 newx->args = lappend(newx->args, newe);
2467 i++;
2470 return (Node *) newx;
2473 static Node *
2474 transformXmlSerialize(ParseState *pstate, XmlSerialize *xs)
2476 Node *result;
2477 XmlExpr *xexpr;
2478 Oid targetType;
2479 int32 targetTypmod;
2481 xexpr = makeNode(XmlExpr);
2482 xexpr->op = IS_XMLSERIALIZE;
2483 xexpr->args = list_make1(coerce_to_specific_type(pstate,
2484 transformExprRecurse(pstate, xs->expr),
2485 XMLOID,
2486 "XMLSERIALIZE"));
2488 typenameTypeIdAndMod(pstate, xs->typeName, &targetType, &targetTypmod);
2490 xexpr->xmloption = xs->xmloption;
2491 xexpr->indent = xs->indent;
2492 xexpr->location = xs->location;
2493 /* We actually only need these to be able to parse back the expression. */
2494 xexpr->type = targetType;
2495 xexpr->typmod = targetTypmod;
2498 * The actual target type is determined this way. SQL allows char and
2499 * varchar as target types. We allow anything that can be cast implicitly
2500 * from text. This way, user-defined text-like data types automatically
2501 * fit in.
2503 result = coerce_to_target_type(pstate, (Node *) xexpr,
2504 TEXTOID, targetType, targetTypmod,
2505 COERCION_IMPLICIT,
2506 COERCE_IMPLICIT_CAST,
2507 -1);
2508 if (result == NULL)
2509 ereport(ERROR,
2510 (errcode(ERRCODE_CANNOT_COERCE),
2511 errmsg("cannot cast XMLSERIALIZE result to %s",
2512 format_type_be(targetType)),
2513 parser_errposition(pstate, xexpr->location)));
2514 return result;
2517 static Node *
2518 transformBooleanTest(ParseState *pstate, BooleanTest *b)
2520 const char *clausename;
2522 switch (b->booltesttype)
2524 case IS_TRUE:
2525 clausename = "IS TRUE";
2526 break;
2527 case IS_NOT_TRUE:
2528 clausename = "IS NOT TRUE";
2529 break;
2530 case IS_FALSE:
2531 clausename = "IS FALSE";
2532 break;
2533 case IS_NOT_FALSE:
2534 clausename = "IS NOT FALSE";
2535 break;
2536 case IS_UNKNOWN:
2537 clausename = "IS UNKNOWN";
2538 break;
2539 case IS_NOT_UNKNOWN:
2540 clausename = "IS NOT UNKNOWN";
2541 break;
2542 default:
2543 elog(ERROR, "unrecognized booltesttype: %d",
2544 (int) b->booltesttype);
2545 clausename = NULL; /* keep compiler quiet */
2548 b->arg = (Expr *) transformExprRecurse(pstate, (Node *) b->arg);
2550 b->arg = (Expr *) coerce_to_boolean(pstate,
2551 (Node *) b->arg,
2552 clausename);
2554 return (Node *) b;
2557 static Node *
2558 transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr)
2560 /* CURRENT OF can only appear at top level of UPDATE/DELETE */
2561 Assert(pstate->p_target_nsitem != NULL);
2562 cexpr->cvarno = pstate->p_target_nsitem->p_rtindex;
2565 * Check to see if the cursor name matches a parameter of type REFCURSOR.
2566 * If so, replace the raw name reference with a parameter reference. (This
2567 * is a hack for the convenience of plpgsql.)
2569 if (cexpr->cursor_name != NULL) /* in case already transformed */
2571 ColumnRef *cref = makeNode(ColumnRef);
2572 Node *node = NULL;
2574 /* Build an unqualified ColumnRef with the given name */
2575 cref->fields = list_make1(makeString(cexpr->cursor_name));
2576 cref->location = -1;
2578 /* See if there is a translation available from a parser hook */
2579 if (pstate->p_pre_columnref_hook != NULL)
2580 node = pstate->p_pre_columnref_hook(pstate, cref);
2581 if (node == NULL && pstate->p_post_columnref_hook != NULL)
2582 node = pstate->p_post_columnref_hook(pstate, cref, NULL);
2585 * XXX Should we throw an error if we get a translation that isn't a
2586 * refcursor Param? For now it seems best to silently ignore false
2587 * matches.
2589 if (node != NULL && IsA(node, Param))
2591 Param *p = (Param *) node;
2593 if (p->paramkind == PARAM_EXTERN &&
2594 p->paramtype == REFCURSOROID)
2596 /* Matches, so convert CURRENT OF to a param reference */
2597 cexpr->cursor_name = NULL;
2598 cexpr->cursor_param = p->paramid;
2603 return (Node *) cexpr;
2607 * Construct a whole-row reference to represent the notation "relation.*".
2609 static Node *
2610 transformWholeRowRef(ParseState *pstate, ParseNamespaceItem *nsitem,
2611 int sublevels_up, int location)
2614 * Build the appropriate referencing node. Normally this can be a
2615 * whole-row Var, but if the nsitem is a JOIN USING alias then it contains
2616 * only a subset of the columns of the underlying join RTE, so that will
2617 * not work. Instead we immediately expand the reference into a RowExpr.
2618 * Since the JOIN USING's common columns are fully determined at this
2619 * point, there seems no harm in expanding it now rather than during
2620 * planning.
2622 * Note that if the nsitem is an OLD/NEW alias for the target RTE (as can
2623 * appear in a RETURNING list), its alias won't match the target RTE's
2624 * alias, but we still want to make a whole-row Var here rather than a
2625 * RowExpr, for consistency with direct references to the target RTE, and
2626 * so that any dropped columns are handled correctly. Thus we also check
2627 * p_returning_type here.
2629 * Note that if the RTE is a function returning scalar, we create just a
2630 * plain reference to the function value, not a composite containing a
2631 * single column. This is pretty inconsistent at first sight, but it's
2632 * what we've done historically. One argument for it is that "rel" and
2633 * "rel.*" mean the same thing for composite relations, so why not for
2634 * scalar functions...
2636 if (nsitem->p_names == nsitem->p_rte->eref ||
2637 nsitem->p_returning_type != VAR_RETURNING_DEFAULT)
2639 Var *result;
2641 result = makeWholeRowVar(nsitem->p_rte, nsitem->p_rtindex,
2642 sublevels_up, true);
2644 /* mark Var for RETURNING OLD/NEW, as necessary */
2645 result->varreturningtype = nsitem->p_returning_type;
2647 /* location is not filled in by makeWholeRowVar */
2648 result->location = location;
2650 /* mark Var if it's nulled by any outer joins */
2651 markNullableIfNeeded(pstate, result);
2653 /* mark relation as requiring whole-row SELECT access */
2654 markVarForSelectPriv(pstate, result);
2656 return (Node *) result;
2658 else
2660 RowExpr *rowexpr;
2661 List *fields;
2664 * We want only as many columns as are listed in p_names->colnames,
2665 * and we should use those names not whatever possibly-aliased names
2666 * are in the RTE. We needn't worry about marking the RTE for SELECT
2667 * access, as the common columns are surely so marked already.
2669 expandRTE(nsitem->p_rte, nsitem->p_rtindex, sublevels_up,
2670 nsitem->p_returning_type, location, false, NULL, &fields);
2671 rowexpr = makeNode(RowExpr);
2672 rowexpr->args = list_truncate(fields,
2673 list_length(nsitem->p_names->colnames));
2674 rowexpr->row_typeid = RECORDOID;
2675 rowexpr->row_format = COERCE_IMPLICIT_CAST;
2676 rowexpr->colnames = copyObject(nsitem->p_names->colnames);
2677 rowexpr->location = location;
2679 /* XXX we ought to mark the row as possibly nullable */
2681 return (Node *) rowexpr;
2686 * Handle an explicit CAST construct.
2688 * Transform the argument, look up the type name, and apply any necessary
2689 * coercion function(s).
2691 static Node *
2692 transformTypeCast(ParseState *pstate, TypeCast *tc)
2694 Node *result;
2695 Node *arg = tc->arg;
2696 Node *expr;
2697 Oid inputType;
2698 Oid targetType;
2699 int32 targetTypmod;
2700 int location;
2702 /* Look up the type name first */
2703 typenameTypeIdAndMod(pstate, tc->typeName, &targetType, &targetTypmod);
2706 * If the subject of the typecast is an ARRAY[] construct and the target
2707 * type is an array type, we invoke transformArrayExpr() directly so that
2708 * we can pass down the type information. This avoids some cases where
2709 * transformArrayExpr() might not infer the correct type. Otherwise, just
2710 * transform the argument normally.
2712 if (IsA(arg, A_ArrayExpr))
2714 Oid targetBaseType;
2715 int32 targetBaseTypmod;
2716 Oid elementType;
2719 * If target is a domain over array, work with the base array type
2720 * here. Below, we'll cast the array type to the domain. In the
2721 * usual case that the target is not a domain, the remaining steps
2722 * will be a no-op.
2724 targetBaseTypmod = targetTypmod;
2725 targetBaseType = getBaseTypeAndTypmod(targetType, &targetBaseTypmod);
2726 elementType = get_element_type(targetBaseType);
2727 if (OidIsValid(elementType))
2729 expr = transformArrayExpr(pstate,
2730 (A_ArrayExpr *) arg,
2731 targetBaseType,
2732 elementType,
2733 targetBaseTypmod);
2735 else
2736 expr = transformExprRecurse(pstate, arg);
2738 else
2739 expr = transformExprRecurse(pstate, arg);
2741 inputType = exprType(expr);
2742 if (inputType == InvalidOid)
2743 return expr; /* do nothing if NULL input */
2746 * Location of the coercion is preferentially the location of the :: or
2747 * CAST symbol, but if there is none then use the location of the type
2748 * name (this can happen in TypeName 'string' syntax, for instance).
2750 location = tc->location;
2751 if (location < 0)
2752 location = tc->typeName->location;
2754 result = coerce_to_target_type(pstate, expr, inputType,
2755 targetType, targetTypmod,
2756 COERCION_EXPLICIT,
2757 COERCE_EXPLICIT_CAST,
2758 location);
2759 if (result == NULL)
2760 ereport(ERROR,
2761 (errcode(ERRCODE_CANNOT_COERCE),
2762 errmsg("cannot cast type %s to %s",
2763 format_type_be(inputType),
2764 format_type_be(targetType)),
2765 parser_coercion_errposition(pstate, location, expr)));
2767 return result;
2771 * Handle an explicit COLLATE clause.
2773 * Transform the argument, and look up the collation name.
2775 static Node *
2776 transformCollateClause(ParseState *pstate, CollateClause *c)
2778 CollateExpr *newc;
2779 Oid argtype;
2781 newc = makeNode(CollateExpr);
2782 newc->arg = (Expr *) transformExprRecurse(pstate, c->arg);
2784 argtype = exprType((Node *) newc->arg);
2787 * The unknown type is not collatable, but coerce_type() takes care of it
2788 * separately, so we'll let it go here.
2790 if (!type_is_collatable(argtype) && argtype != UNKNOWNOID)
2791 ereport(ERROR,
2792 (errcode(ERRCODE_DATATYPE_MISMATCH),
2793 errmsg("collations are not supported by type %s",
2794 format_type_be(argtype)),
2795 parser_errposition(pstate, c->location)));
2797 newc->collOid = LookupCollation(pstate, c->collname, c->location);
2798 newc->location = c->location;
2800 return (Node *) newc;
2804 * Transform a "row compare-op row" construct
2806 * The inputs are lists of already-transformed expressions.
2807 * As with coerce_type, pstate may be NULL if no special unknown-Param
2808 * processing is wanted.
2810 * The output may be a single OpExpr, an AND or OR combination of OpExprs,
2811 * or a RowCompareExpr. In all cases it is guaranteed to return boolean.
2812 * The AND, OR, and RowCompareExpr cases further imply things about the
2813 * behavior of the operators (ie, they behave as =, <>, or < <= > >=).
2815 static Node *
2816 make_row_comparison_op(ParseState *pstate, List *opname,
2817 List *largs, List *rargs, int location)
2819 RowCompareExpr *rcexpr;
2820 CompareType cmptype;
2821 List *opexprs;
2822 List *opnos;
2823 List *opfamilies;
2824 ListCell *l,
2826 List **opinfo_lists;
2827 Bitmapset *strats;
2828 int nopers;
2829 int i;
2831 nopers = list_length(largs);
2832 if (nopers != list_length(rargs))
2833 ereport(ERROR,
2834 (errcode(ERRCODE_SYNTAX_ERROR),
2835 errmsg("unequal number of entries in row expressions"),
2836 parser_errposition(pstate, location)));
2839 * We can't compare zero-length rows because there is no principled basis
2840 * for figuring out what the operator is.
2842 if (nopers == 0)
2843 ereport(ERROR,
2844 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2845 errmsg("cannot compare rows of zero length"),
2846 parser_errposition(pstate, location)));
2849 * Identify all the pairwise operators, using make_op so that behavior is
2850 * the same as in the simple scalar case.
2852 opexprs = NIL;
2853 forboth(l, largs, r, rargs)
2855 Node *larg = (Node *) lfirst(l);
2856 Node *rarg = (Node *) lfirst(r);
2857 OpExpr *cmp;
2859 cmp = castNode(OpExpr, make_op(pstate, opname, larg, rarg,
2860 pstate->p_last_srf, location));
2863 * We don't use coerce_to_boolean here because we insist on the
2864 * operator yielding boolean directly, not via coercion. If it
2865 * doesn't yield bool it won't be in any index opfamilies...
2867 if (cmp->opresulttype != BOOLOID)
2868 ereport(ERROR,
2869 (errcode(ERRCODE_DATATYPE_MISMATCH),
2870 errmsg("row comparison operator must yield type boolean, "
2871 "not type %s",
2872 format_type_be(cmp->opresulttype)),
2873 parser_errposition(pstate, location)));
2874 if (expression_returns_set((Node *) cmp))
2875 ereport(ERROR,
2876 (errcode(ERRCODE_DATATYPE_MISMATCH),
2877 errmsg("row comparison operator must not return a set"),
2878 parser_errposition(pstate, location)));
2879 opexprs = lappend(opexprs, cmp);
2883 * If rows are length 1, just return the single operator. In this case we
2884 * don't insist on identifying btree semantics for the operator (but we
2885 * still require it to return boolean).
2887 if (nopers == 1)
2888 return (Node *) linitial(opexprs);
2891 * Now we must determine which row comparison semantics (= <> < <= > >=)
2892 * apply to this set of operators. We look for btree opfamilies
2893 * containing the operators, and see which interpretations (strategy
2894 * numbers) exist for each operator.
2896 opinfo_lists = (List **) palloc(nopers * sizeof(List *));
2897 strats = NULL;
2898 i = 0;
2899 foreach(l, opexprs)
2901 Oid opno = ((OpExpr *) lfirst(l))->opno;
2902 Bitmapset *this_strats;
2903 ListCell *j;
2905 opinfo_lists[i] = get_op_btree_interpretation(opno);
2908 * convert strategy numbers into a Bitmapset to make the intersection
2909 * calculation easy.
2911 this_strats = NULL;
2912 foreach(j, opinfo_lists[i])
2914 OpBtreeInterpretation *opinfo = lfirst(j);
2916 this_strats = bms_add_member(this_strats, opinfo->strategy);
2918 if (i == 0)
2919 strats = this_strats;
2920 else
2921 strats = bms_int_members(strats, this_strats);
2922 i++;
2926 * If there are multiple common interpretations, we may use any one of
2927 * them ... this coding arbitrarily picks the lowest btree strategy
2928 * number.
2930 i = bms_next_member(strats, -1);
2931 if (i < 0)
2933 /* No common interpretation, so fail */
2934 ereport(ERROR,
2935 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2936 errmsg("could not determine interpretation of row comparison operator %s",
2937 strVal(llast(opname))),
2938 errhint("Row comparison operators must be associated with btree operator families."),
2939 parser_errposition(pstate, location)));
2941 cmptype = (CompareType) i;
2944 * For = and <> cases, we just combine the pairwise operators with AND or
2945 * OR respectively.
2947 if (cmptype == COMPARE_EQ)
2948 return (Node *) makeBoolExpr(AND_EXPR, opexprs, location);
2949 if (cmptype == COMPARE_NE)
2950 return (Node *) makeBoolExpr(OR_EXPR, opexprs, location);
2953 * Otherwise we need to choose exactly which opfamily to associate with
2954 * each operator.
2956 opfamilies = NIL;
2957 for (i = 0; i < nopers; i++)
2959 Oid opfamily = InvalidOid;
2960 ListCell *j;
2962 foreach(j, opinfo_lists[i])
2964 OpBtreeInterpretation *opinfo = lfirst(j);
2966 if (opinfo->strategy == cmptype)
2968 opfamily = opinfo->opfamily_id;
2969 break;
2972 if (OidIsValid(opfamily))
2973 opfamilies = lappend_oid(opfamilies, opfamily);
2974 else /* should not happen */
2975 ereport(ERROR,
2976 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2977 errmsg("could not determine interpretation of row comparison operator %s",
2978 strVal(llast(opname))),
2979 errdetail("There are multiple equally-plausible candidates."),
2980 parser_errposition(pstate, location)));
2984 * Now deconstruct the OpExprs and create a RowCompareExpr.
2986 * Note: can't just reuse the passed largs/rargs lists, because of
2987 * possibility that make_op inserted coercion operations.
2989 opnos = NIL;
2990 largs = NIL;
2991 rargs = NIL;
2992 foreach(l, opexprs)
2994 OpExpr *cmp = (OpExpr *) lfirst(l);
2996 opnos = lappend_oid(opnos, cmp->opno);
2997 largs = lappend(largs, linitial(cmp->args));
2998 rargs = lappend(rargs, lsecond(cmp->args));
3001 rcexpr = makeNode(RowCompareExpr);
3002 rcexpr->cmptype = cmptype;
3003 rcexpr->opnos = opnos;
3004 rcexpr->opfamilies = opfamilies;
3005 rcexpr->inputcollids = NIL; /* assign_expr_collations will fix this */
3006 rcexpr->largs = largs;
3007 rcexpr->rargs = rargs;
3009 return (Node *) rcexpr;
3013 * Transform a "row IS DISTINCT FROM row" construct
3015 * The input RowExprs are already transformed
3017 static Node *
3018 make_row_distinct_op(ParseState *pstate, List *opname,
3019 RowExpr *lrow, RowExpr *rrow,
3020 int location)
3022 Node *result = NULL;
3023 List *largs = lrow->args;
3024 List *rargs = rrow->args;
3025 ListCell *l,
3028 if (list_length(largs) != list_length(rargs))
3029 ereport(ERROR,
3030 (errcode(ERRCODE_SYNTAX_ERROR),
3031 errmsg("unequal number of entries in row expressions"),
3032 parser_errposition(pstate, location)));
3034 forboth(l, largs, r, rargs)
3036 Node *larg = (Node *) lfirst(l);
3037 Node *rarg = (Node *) lfirst(r);
3038 Node *cmp;
3040 cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg, location);
3041 if (result == NULL)
3042 result = cmp;
3043 else
3044 result = (Node *) makeBoolExpr(OR_EXPR,
3045 list_make2(result, cmp),
3046 location);
3049 if (result == NULL)
3051 /* zero-length rows? Generate constant FALSE */
3052 result = makeBoolConst(false, false);
3055 return result;
3059 * make the node for an IS DISTINCT FROM operator
3061 static Expr *
3062 make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree,
3063 int location)
3065 Expr *result;
3067 result = make_op(pstate, opname, ltree, rtree,
3068 pstate->p_last_srf, location);
3069 if (((OpExpr *) result)->opresulttype != BOOLOID)
3070 ereport(ERROR,
3071 (errcode(ERRCODE_DATATYPE_MISMATCH),
3072 /* translator: %s is name of a SQL construct, eg NULLIF */
3073 errmsg("%s requires = operator to yield boolean",
3074 "IS DISTINCT FROM"),
3075 parser_errposition(pstate, location)));
3076 if (((OpExpr *) result)->opretset)
3077 ereport(ERROR,
3078 (errcode(ERRCODE_DATATYPE_MISMATCH),
3079 /* translator: %s is name of a SQL construct, eg NULLIF */
3080 errmsg("%s must not return a set", "IS DISTINCT FROM"),
3081 parser_errposition(pstate, location)));
3084 * We rely on DistinctExpr and OpExpr being same struct
3086 NodeSetTag(result, T_DistinctExpr);
3088 return result;
3092 * Produce a NullTest node from an IS [NOT] DISTINCT FROM NULL construct
3094 * "arg" is the untransformed other argument
3096 static Node *
3097 make_nulltest_from_distinct(ParseState *pstate, A_Expr *distincta, Node *arg)
3099 NullTest *nt = makeNode(NullTest);
3101 nt->arg = (Expr *) transformExprRecurse(pstate, arg);
3102 /* the argument can be any type, so don't coerce it */
3103 if (distincta->kind == AEXPR_NOT_DISTINCT)
3104 nt->nulltesttype = IS_NULL;
3105 else
3106 nt->nulltesttype = IS_NOT_NULL;
3107 /* argisrow = false is correct whether or not arg is composite */
3108 nt->argisrow = false;
3109 nt->location = distincta->location;
3110 return (Node *) nt;
3114 * Produce a string identifying an expression by kind.
3116 * Note: when practical, use a simple SQL keyword for the result. If that
3117 * doesn't work well, check call sites to see whether custom error message
3118 * strings are required.
3120 const char *
3121 ParseExprKindName(ParseExprKind exprKind)
3123 switch (exprKind)
3125 case EXPR_KIND_NONE:
3126 return "invalid expression context";
3127 case EXPR_KIND_OTHER:
3128 return "extension expression";
3129 case EXPR_KIND_JOIN_ON:
3130 return "JOIN/ON";
3131 case EXPR_KIND_JOIN_USING:
3132 return "JOIN/USING";
3133 case EXPR_KIND_FROM_SUBSELECT:
3134 return "sub-SELECT in FROM";
3135 case EXPR_KIND_FROM_FUNCTION:
3136 return "function in FROM";
3137 case EXPR_KIND_WHERE:
3138 return "WHERE";
3139 case EXPR_KIND_POLICY:
3140 return "POLICY";
3141 case EXPR_KIND_HAVING:
3142 return "HAVING";
3143 case EXPR_KIND_FILTER:
3144 return "FILTER";
3145 case EXPR_KIND_WINDOW_PARTITION:
3146 return "window PARTITION BY";
3147 case EXPR_KIND_WINDOW_ORDER:
3148 return "window ORDER BY";
3149 case EXPR_KIND_WINDOW_FRAME_RANGE:
3150 return "window RANGE";
3151 case EXPR_KIND_WINDOW_FRAME_ROWS:
3152 return "window ROWS";
3153 case EXPR_KIND_WINDOW_FRAME_GROUPS:
3154 return "window GROUPS";
3155 case EXPR_KIND_SELECT_TARGET:
3156 return "SELECT";
3157 case EXPR_KIND_INSERT_TARGET:
3158 return "INSERT";
3159 case EXPR_KIND_UPDATE_SOURCE:
3160 case EXPR_KIND_UPDATE_TARGET:
3161 return "UPDATE";
3162 case EXPR_KIND_MERGE_WHEN:
3163 return "MERGE WHEN";
3164 case EXPR_KIND_GROUP_BY:
3165 return "GROUP BY";
3166 case EXPR_KIND_ORDER_BY:
3167 return "ORDER BY";
3168 case EXPR_KIND_DISTINCT_ON:
3169 return "DISTINCT ON";
3170 case EXPR_KIND_LIMIT:
3171 return "LIMIT";
3172 case EXPR_KIND_OFFSET:
3173 return "OFFSET";
3174 case EXPR_KIND_RETURNING:
3175 case EXPR_KIND_MERGE_RETURNING:
3176 return "RETURNING";
3177 case EXPR_KIND_VALUES:
3178 case EXPR_KIND_VALUES_SINGLE:
3179 return "VALUES";
3180 case EXPR_KIND_CHECK_CONSTRAINT:
3181 case EXPR_KIND_DOMAIN_CHECK:
3182 return "CHECK";
3183 case EXPR_KIND_COLUMN_DEFAULT:
3184 case EXPR_KIND_FUNCTION_DEFAULT:
3185 return "DEFAULT";
3186 case EXPR_KIND_INDEX_EXPRESSION:
3187 return "index expression";
3188 case EXPR_KIND_INDEX_PREDICATE:
3189 return "index predicate";
3190 case EXPR_KIND_STATS_EXPRESSION:
3191 return "statistics expression";
3192 case EXPR_KIND_ALTER_COL_TRANSFORM:
3193 return "USING";
3194 case EXPR_KIND_EXECUTE_PARAMETER:
3195 return "EXECUTE";
3196 case EXPR_KIND_TRIGGER_WHEN:
3197 return "WHEN";
3198 case EXPR_KIND_PARTITION_BOUND:
3199 return "partition bound";
3200 case EXPR_KIND_PARTITION_EXPRESSION:
3201 return "PARTITION BY";
3202 case EXPR_KIND_CALL_ARGUMENT:
3203 return "CALL";
3204 case EXPR_KIND_COPY_WHERE:
3205 return "WHERE";
3206 case EXPR_KIND_GENERATED_COLUMN:
3207 return "GENERATED AS";
3208 case EXPR_KIND_CYCLE_MARK:
3209 return "CYCLE";
3212 * There is intentionally no default: case here, so that the
3213 * compiler will warn if we add a new ParseExprKind without
3214 * extending this switch. If we do see an unrecognized value at
3215 * runtime, we'll fall through to the "unrecognized" return.
3218 return "unrecognized expression kind";
3222 * Make string Const node from JSON encoding name.
3224 * UTF8 is default encoding.
3226 static Const *
3227 getJsonEncodingConst(JsonFormat *format)
3229 JsonEncoding encoding;
3230 const char *enc;
3231 Name encname = palloc(sizeof(NameData));
3233 if (!format ||
3234 format->format_type == JS_FORMAT_DEFAULT ||
3235 format->encoding == JS_ENC_DEFAULT)
3236 encoding = JS_ENC_UTF8;
3237 else
3238 encoding = format->encoding;
3240 switch (encoding)
3242 case JS_ENC_UTF16:
3243 enc = "UTF16";
3244 break;
3245 case JS_ENC_UTF32:
3246 enc = "UTF32";
3247 break;
3248 case JS_ENC_UTF8:
3249 enc = "UTF8";
3250 break;
3251 default:
3252 elog(ERROR, "invalid JSON encoding: %d", encoding);
3253 break;
3256 namestrcpy(encname, enc);
3258 return makeConst(NAMEOID, -1, InvalidOid, NAMEDATALEN,
3259 NameGetDatum(encname), false, false);
3263 * Make bytea => text conversion using specified JSON format encoding.
3265 static Node *
3266 makeJsonByteaToTextConversion(Node *expr, JsonFormat *format, int location)
3268 Const *encoding = getJsonEncodingConst(format);
3269 FuncExpr *fexpr = makeFuncExpr(F_CONVERT_FROM, TEXTOID,
3270 list_make2(expr, encoding),
3271 InvalidOid, InvalidOid,
3272 COERCE_EXPLICIT_CALL);
3274 fexpr->location = location;
3276 return (Node *) fexpr;
3280 * Transform JSON value expression using specified input JSON format or
3281 * default format otherwise, coercing to the targettype if needed.
3283 * Returned expression is either ve->raw_expr coerced to text (if needed) or
3284 * a JsonValueExpr with formatted_expr set to the coerced copy of raw_expr
3285 * if the specified format and the targettype requires it.
3287 static Node *
3288 transformJsonValueExpr(ParseState *pstate, const char *constructName,
3289 JsonValueExpr *ve, JsonFormatType default_format,
3290 Oid targettype, bool isarg)
3292 Node *expr = transformExprRecurse(pstate, (Node *) ve->raw_expr);
3293 Node *rawexpr;
3294 JsonFormatType format;
3295 Oid exprtype;
3296 int location;
3297 char typcategory;
3298 bool typispreferred;
3300 if (exprType(expr) == UNKNOWNOID)
3301 expr = coerce_to_specific_type(pstate, expr, TEXTOID, constructName);
3303 rawexpr = expr;
3304 exprtype = exprType(expr);
3305 location = exprLocation(expr);
3307 get_type_category_preferred(exprtype, &typcategory, &typispreferred);
3309 if (ve->format->format_type != JS_FORMAT_DEFAULT)
3311 if (ve->format->encoding != JS_ENC_DEFAULT && exprtype != BYTEAOID)
3312 ereport(ERROR,
3313 errcode(ERRCODE_DATATYPE_MISMATCH),
3314 errmsg("JSON ENCODING clause is only allowed for bytea input type"),
3315 parser_errposition(pstate, ve->format->location));
3317 if (exprtype == JSONOID || exprtype == JSONBOID)
3318 format = JS_FORMAT_DEFAULT; /* do not format json[b] types */
3319 else
3320 format = ve->format->format_type;
3322 else if (isarg)
3325 * Special treatment for PASSING arguments.
3327 * Pass types supported by GetJsonPathVar() / JsonItemFromDatum()
3328 * directly without converting to json[b].
3330 switch (exprtype)
3332 case BOOLOID:
3333 case NUMERICOID:
3334 case INT2OID:
3335 case INT4OID:
3336 case INT8OID:
3337 case FLOAT4OID:
3338 case FLOAT8OID:
3339 case TEXTOID:
3340 case VARCHAROID:
3341 case DATEOID:
3342 case TIMEOID:
3343 case TIMETZOID:
3344 case TIMESTAMPOID:
3345 case TIMESTAMPTZOID:
3346 return expr;
3348 default:
3349 if (typcategory == TYPCATEGORY_STRING)
3350 return expr;
3351 /* else convert argument to json[b] type */
3352 break;
3355 format = default_format;
3357 else if (exprtype == JSONOID || exprtype == JSONBOID)
3358 format = JS_FORMAT_DEFAULT; /* do not format json[b] types */
3359 else
3360 format = default_format;
3362 if (format != JS_FORMAT_DEFAULT ||
3363 (OidIsValid(targettype) && exprtype != targettype))
3365 Node *coerced;
3366 bool only_allow_cast = OidIsValid(targettype);
3369 * PASSING args are handled appropriately by GetJsonPathVar() /
3370 * JsonItemFromDatum().
3372 if (!isarg &&
3373 !only_allow_cast &&
3374 exprtype != BYTEAOID && typcategory != TYPCATEGORY_STRING)
3375 ereport(ERROR,
3376 errcode(ERRCODE_DATATYPE_MISMATCH),
3377 ve->format->format_type == JS_FORMAT_DEFAULT ?
3378 errmsg("cannot use non-string types with implicit FORMAT JSON clause") :
3379 errmsg("cannot use non-string types with explicit FORMAT JSON clause"),
3380 parser_errposition(pstate, ve->format->location >= 0 ?
3381 ve->format->location : location));
3383 /* Convert encoded JSON text from bytea. */
3384 if (format == JS_FORMAT_JSON && exprtype == BYTEAOID)
3386 expr = makeJsonByteaToTextConversion(expr, ve->format, location);
3387 exprtype = TEXTOID;
3390 if (!OidIsValid(targettype))
3391 targettype = format == JS_FORMAT_JSONB ? JSONBOID : JSONOID;
3393 /* Try to coerce to the target type. */
3394 coerced = coerce_to_target_type(pstate, expr, exprtype,
3395 targettype, -1,
3396 COERCION_EXPLICIT,
3397 COERCE_EXPLICIT_CAST,
3398 location);
3400 if (!coerced)
3402 /* If coercion failed, use to_json()/to_jsonb() functions. */
3403 FuncExpr *fexpr;
3404 Oid fnoid;
3407 * Though only allow a cast when the target type is specified by
3408 * the caller.
3410 if (only_allow_cast)
3411 ereport(ERROR,
3412 (errcode(ERRCODE_CANNOT_COERCE),
3413 errmsg("cannot cast type %s to %s",
3414 format_type_be(exprtype),
3415 format_type_be(targettype)),
3416 parser_errposition(pstate, location)));
3418 fnoid = targettype == JSONOID ? F_TO_JSON : F_TO_JSONB;
3419 fexpr = makeFuncExpr(fnoid, targettype, list_make1(expr),
3420 InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);
3422 fexpr->location = location;
3424 coerced = (Node *) fexpr;
3427 if (coerced == expr)
3428 expr = rawexpr;
3429 else
3431 ve = copyObject(ve);
3432 ve->raw_expr = (Expr *) rawexpr;
3433 ve->formatted_expr = (Expr *) coerced;
3435 expr = (Node *) ve;
3439 /* If returning a JsonValueExpr, formatted_expr must have been set. */
3440 Assert(!IsA(expr, JsonValueExpr) ||
3441 ((JsonValueExpr *) expr)->formatted_expr != NULL);
3443 return expr;
3447 * Checks specified output format for its applicability to the target type.
3449 static void
3450 checkJsonOutputFormat(ParseState *pstate, const JsonFormat *format,
3451 Oid targettype, bool allow_format_for_non_strings)
3453 if (!allow_format_for_non_strings &&
3454 format->format_type != JS_FORMAT_DEFAULT &&
3455 (targettype != BYTEAOID &&
3456 targettype != JSONOID &&
3457 targettype != JSONBOID))
3459 char typcategory;
3460 bool typispreferred;
3462 get_type_category_preferred(targettype, &typcategory, &typispreferred);
3464 if (typcategory != TYPCATEGORY_STRING)
3465 ereport(ERROR,
3466 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3467 parser_errposition(pstate, format->location),
3468 errmsg("cannot use JSON format with non-string output types"));
3471 if (format->format_type == JS_FORMAT_JSON)
3473 JsonEncoding enc = format->encoding != JS_ENC_DEFAULT ?
3474 format->encoding : JS_ENC_UTF8;
3476 if (targettype != BYTEAOID &&
3477 format->encoding != JS_ENC_DEFAULT)
3478 ereport(ERROR,
3479 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3480 parser_errposition(pstate, format->location),
3481 errmsg("cannot set JSON encoding for non-bytea output types"));
3483 if (enc != JS_ENC_UTF8)
3484 ereport(ERROR,
3485 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3486 errmsg("unsupported JSON encoding"),
3487 errhint("Only UTF8 JSON encoding is supported."),
3488 parser_errposition(pstate, format->location));
3493 * Transform JSON output clause.
3495 * Assigns target type oid and modifier.
3496 * Assigns default format or checks specified format for its applicability to
3497 * the target type.
3499 static JsonReturning *
3500 transformJsonOutput(ParseState *pstate, const JsonOutput *output,
3501 bool allow_format)
3503 JsonReturning *ret;
3505 /* if output clause is not specified, make default clause value */
3506 if (!output)
3508 ret = makeNode(JsonReturning);
3510 ret->format = makeJsonFormat(JS_FORMAT_DEFAULT, JS_ENC_DEFAULT, -1);
3511 ret->typid = InvalidOid;
3512 ret->typmod = -1;
3514 return ret;
3517 ret = copyObject(output->returning);
3519 typenameTypeIdAndMod(pstate, output->typeName, &ret->typid, &ret->typmod);
3521 if (output->typeName->setof)
3522 ereport(ERROR,
3523 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3524 errmsg("returning SETOF types is not supported in SQL/JSON functions"));
3526 if (get_typtype(ret->typid) == TYPTYPE_PSEUDO)
3527 ereport(ERROR,
3528 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3529 errmsg("returning pseudo-types is not supported in SQL/JSON functions"));
3531 if (ret->format->format_type == JS_FORMAT_DEFAULT)
3532 /* assign JSONB format when returning jsonb, or JSON format otherwise */
3533 ret->format->format_type =
3534 ret->typid == JSONBOID ? JS_FORMAT_JSONB : JS_FORMAT_JSON;
3535 else
3536 checkJsonOutputFormat(pstate, ret->format, ret->typid, allow_format);
3538 return ret;
3542 * Transform JSON output clause of JSON constructor functions.
3544 * Derive RETURNING type, if not specified, from argument types.
3546 static JsonReturning *
3547 transformJsonConstructorOutput(ParseState *pstate, JsonOutput *output,
3548 List *args)
3550 JsonReturning *returning = transformJsonOutput(pstate, output, true);
3552 if (!OidIsValid(returning->typid))
3554 ListCell *lc;
3555 bool have_jsonb = false;
3557 foreach(lc, args)
3559 Node *expr = lfirst(lc);
3560 Oid typid = exprType(expr);
3562 have_jsonb |= typid == JSONBOID;
3564 if (have_jsonb)
3565 break;
3568 if (have_jsonb)
3570 returning->typid = JSONBOID;
3571 returning->format->format_type = JS_FORMAT_JSONB;
3573 else
3575 /* XXX TEXT is default by the standard, but we return JSON */
3576 returning->typid = JSONOID;
3577 returning->format->format_type = JS_FORMAT_JSON;
3580 returning->typmod = -1;
3583 return returning;
3587 * Coerce json[b]-valued function expression to the output type.
3589 static Node *
3590 coerceJsonFuncExpr(ParseState *pstate, Node *expr,
3591 const JsonReturning *returning, bool report_error)
3593 Node *res;
3594 int location;
3595 Oid exprtype = exprType(expr);
3597 /* if output type is not specified or equals to function type, return */
3598 if (!OidIsValid(returning->typid) || returning->typid == exprtype)
3599 return expr;
3601 location = exprLocation(expr);
3603 if (location < 0)
3604 location = returning->format->location;
3606 /* special case for RETURNING bytea FORMAT json */
3607 if (returning->format->format_type == JS_FORMAT_JSON &&
3608 returning->typid == BYTEAOID)
3610 /* encode json text into bytea using pg_convert_to() */
3611 Node *texpr = coerce_to_specific_type(pstate, expr, TEXTOID,
3612 "JSON_FUNCTION");
3613 Const *enc = getJsonEncodingConst(returning->format);
3614 FuncExpr *fexpr = makeFuncExpr(F_CONVERT_TO, BYTEAOID,
3615 list_make2(texpr, enc),
3616 InvalidOid, InvalidOid,
3617 COERCE_EXPLICIT_CALL);
3619 fexpr->location = location;
3621 return (Node *) fexpr;
3625 * For other cases, try to coerce expression to the output type using
3626 * assignment-level casts, erroring out if none available. This basically
3627 * allows coercing the jsonb value to any string type (typcategory = 'S').
3629 * Requesting assignment-level here means that typmod / length coercion
3630 * assumes implicit coercion which is the behavior we want; see
3631 * build_coercion_expression().
3633 res = coerce_to_target_type(pstate, expr, exprtype,
3634 returning->typid, returning->typmod,
3635 COERCION_ASSIGNMENT,
3636 COERCE_IMPLICIT_CAST,
3637 location);
3639 if (!res && report_error)
3640 ereport(ERROR,
3641 errcode(ERRCODE_CANNOT_COERCE),
3642 errmsg("cannot cast type %s to %s",
3643 format_type_be(exprtype),
3644 format_type_be(returning->typid)),
3645 parser_coercion_errposition(pstate, location, expr));
3647 return res;
3651 * Make a JsonConstructorExpr node.
3653 static Node *
3654 makeJsonConstructorExpr(ParseState *pstate, JsonConstructorType type,
3655 List *args, Expr *fexpr, JsonReturning *returning,
3656 bool unique, bool absent_on_null, int location)
3658 JsonConstructorExpr *jsctor = makeNode(JsonConstructorExpr);
3659 Node *placeholder;
3660 Node *coercion;
3662 jsctor->args = args;
3663 jsctor->func = fexpr;
3664 jsctor->type = type;
3665 jsctor->returning = returning;
3666 jsctor->unique = unique;
3667 jsctor->absent_on_null = absent_on_null;
3668 jsctor->location = location;
3671 * Coerce to the RETURNING type and format, if needed. We abuse
3672 * CaseTestExpr here as placeholder to pass the result of either
3673 * evaluating 'fexpr' or whatever is produced by ExecEvalJsonConstructor()
3674 * that is of type JSON or JSONB to the coercion function.
3676 if (fexpr)
3678 CaseTestExpr *cte = makeNode(CaseTestExpr);
3680 cte->typeId = exprType((Node *) fexpr);
3681 cte->typeMod = exprTypmod((Node *) fexpr);
3682 cte->collation = exprCollation((Node *) fexpr);
3684 placeholder = (Node *) cte;
3686 else
3688 CaseTestExpr *cte = makeNode(CaseTestExpr);
3690 cte->typeId = returning->format->format_type == JS_FORMAT_JSONB ?
3691 JSONBOID : JSONOID;
3692 cte->typeMod = -1;
3693 cte->collation = InvalidOid;
3695 placeholder = (Node *) cte;
3698 coercion = coerceJsonFuncExpr(pstate, placeholder, returning, true);
3700 if (coercion != placeholder)
3701 jsctor->coercion = (Expr *) coercion;
3703 return (Node *) jsctor;
3707 * Transform JSON_OBJECT() constructor.
3709 * JSON_OBJECT() is transformed into a JsonConstructorExpr node of type
3710 * JSCTOR_JSON_OBJECT. The result is coerced to the target type given
3711 * by ctor->output.
3713 static Node *
3714 transformJsonObjectConstructor(ParseState *pstate, JsonObjectConstructor *ctor)
3716 JsonReturning *returning;
3717 List *args = NIL;
3719 /* transform key-value pairs, if any */
3720 if (ctor->exprs)
3722 ListCell *lc;
3724 /* transform and append key-value arguments */
3725 foreach(lc, ctor->exprs)
3727 JsonKeyValue *kv = castNode(JsonKeyValue, lfirst(lc));
3728 Node *key = transformExprRecurse(pstate, (Node *) kv->key);
3729 Node *val = transformJsonValueExpr(pstate, "JSON_OBJECT()",
3730 kv->value,
3731 JS_FORMAT_DEFAULT,
3732 InvalidOid, false);
3734 args = lappend(args, key);
3735 args = lappend(args, val);
3739 returning = transformJsonConstructorOutput(pstate, ctor->output, args);
3741 return makeJsonConstructorExpr(pstate, JSCTOR_JSON_OBJECT, args, NULL,
3742 returning, ctor->unique,
3743 ctor->absent_on_null, ctor->location);
3747 * Transform JSON_ARRAY(query [FORMAT] [RETURNING] [ON NULL]) into
3748 * (SELECT JSON_ARRAYAGG(a [FORMAT] [RETURNING] [ON NULL]) FROM (query) q(a))
3750 static Node *
3751 transformJsonArrayQueryConstructor(ParseState *pstate,
3752 JsonArrayQueryConstructor *ctor)
3754 SubLink *sublink = makeNode(SubLink);
3755 SelectStmt *select = makeNode(SelectStmt);
3756 RangeSubselect *range = makeNode(RangeSubselect);
3757 Alias *alias = makeNode(Alias);
3758 ResTarget *target = makeNode(ResTarget);
3759 JsonArrayAgg *agg = makeNode(JsonArrayAgg);
3760 ColumnRef *colref = makeNode(ColumnRef);
3761 Query *query;
3762 ParseState *qpstate;
3764 /* Transform query only for counting target list entries. */
3765 qpstate = make_parsestate(pstate);
3767 query = transformStmt(qpstate, ctor->query);
3769 if (count_nonjunk_tlist_entries(query->targetList) != 1)
3770 ereport(ERROR,
3771 errcode(ERRCODE_SYNTAX_ERROR),
3772 errmsg("subquery must return only one column"),
3773 parser_errposition(pstate, ctor->location));
3775 free_parsestate(qpstate);
3777 colref->fields = list_make2(makeString(pstrdup("q")),
3778 makeString(pstrdup("a")));
3779 colref->location = ctor->location;
3782 * No formatting necessary, so set formatted_expr to be the same as
3783 * raw_expr.
3785 agg->arg = makeJsonValueExpr((Expr *) colref, (Expr *) colref,
3786 ctor->format);
3787 agg->absent_on_null = ctor->absent_on_null;
3788 agg->constructor = makeNode(JsonAggConstructor);
3789 agg->constructor->agg_order = NIL;
3790 agg->constructor->output = ctor->output;
3791 agg->constructor->location = ctor->location;
3793 target->name = NULL;
3794 target->indirection = NIL;
3795 target->val = (Node *) agg;
3796 target->location = ctor->location;
3798 alias->aliasname = pstrdup("q");
3799 alias->colnames = list_make1(makeString(pstrdup("a")));
3801 range->lateral = false;
3802 range->subquery = ctor->query;
3803 range->alias = alias;
3805 select->targetList = list_make1(target);
3806 select->fromClause = list_make1(range);
3808 sublink->subLinkType = EXPR_SUBLINK;
3809 sublink->subLinkId = 0;
3810 sublink->testexpr = NULL;
3811 sublink->operName = NIL;
3812 sublink->subselect = (Node *) select;
3813 sublink->location = ctor->location;
3815 return transformExprRecurse(pstate, (Node *) sublink);
3819 * Common code for JSON_OBJECTAGG and JSON_ARRAYAGG transformation.
3821 static Node *
3822 transformJsonAggConstructor(ParseState *pstate, JsonAggConstructor *agg_ctor,
3823 JsonReturning *returning, List *args,
3824 Oid aggfnoid, Oid aggtype,
3825 JsonConstructorType ctor_type,
3826 bool unique, bool absent_on_null)
3828 Node *node;
3829 Expr *aggfilter;
3831 aggfilter = agg_ctor->agg_filter ? (Expr *)
3832 transformWhereClause(pstate, agg_ctor->agg_filter,
3833 EXPR_KIND_FILTER, "FILTER") : NULL;
3835 if (agg_ctor->over)
3837 /* window function */
3838 WindowFunc *wfunc = makeNode(WindowFunc);
3840 wfunc->winfnoid = aggfnoid;
3841 wfunc->wintype = aggtype;
3842 /* wincollid and inputcollid will be set by parse_collate.c */
3843 wfunc->args = args;
3844 wfunc->aggfilter = aggfilter;
3845 wfunc->runCondition = NIL;
3846 /* winref will be set by transformWindowFuncCall */
3847 wfunc->winstar = false;
3848 wfunc->winagg = true;
3849 wfunc->location = agg_ctor->location;
3852 * ordered aggs not allowed in windows yet
3854 if (agg_ctor->agg_order != NIL)
3855 ereport(ERROR,
3856 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3857 errmsg("aggregate ORDER BY is not implemented for window functions"),
3858 parser_errposition(pstate, agg_ctor->location));
3860 /* parse_agg.c does additional window-func-specific processing */
3861 transformWindowFuncCall(pstate, wfunc, agg_ctor->over);
3863 node = (Node *) wfunc;
3865 else
3867 Aggref *aggref = makeNode(Aggref);
3869 aggref->aggfnoid = aggfnoid;
3870 aggref->aggtype = aggtype;
3872 /* aggcollid and inputcollid will be set by parse_collate.c */
3873 /* aggtranstype will be set by planner */
3874 /* aggargtypes will be set by transformAggregateCall */
3875 /* aggdirectargs and args will be set by transformAggregateCall */
3876 /* aggorder and aggdistinct will be set by transformAggregateCall */
3877 aggref->aggfilter = aggfilter;
3878 aggref->aggstar = false;
3879 aggref->aggvariadic = false;
3880 aggref->aggkind = AGGKIND_NORMAL;
3881 aggref->aggpresorted = false;
3882 /* agglevelsup will be set by transformAggregateCall */
3883 aggref->aggsplit = AGGSPLIT_SIMPLE; /* planner might change this */
3884 aggref->aggno = -1; /* planner will set aggno and aggtransno */
3885 aggref->aggtransno = -1;
3886 aggref->location = agg_ctor->location;
3888 transformAggregateCall(pstate, aggref, args, agg_ctor->agg_order, false);
3890 node = (Node *) aggref;
3893 return makeJsonConstructorExpr(pstate, ctor_type, NIL, (Expr *) node,
3894 returning, unique, absent_on_null,
3895 agg_ctor->location);
3899 * Transform JSON_OBJECTAGG() aggregate function.
3901 * JSON_OBJECTAGG() is transformed into a JsonConstructorExpr node of type
3902 * JSCTOR_JSON_OBJECTAGG, which at runtime becomes a
3903 * json[b]_object_agg[_unique][_strict](agg->arg->key, agg->arg->value) call
3904 * depending on the output JSON format. The result is coerced to the target
3905 * type given by agg->constructor->output.
3907 static Node *
3908 transformJsonObjectAgg(ParseState *pstate, JsonObjectAgg *agg)
3910 JsonReturning *returning;
3911 Node *key;
3912 Node *val;
3913 List *args;
3914 Oid aggfnoid;
3915 Oid aggtype;
3917 key = transformExprRecurse(pstate, (Node *) agg->arg->key);
3918 val = transformJsonValueExpr(pstate, "JSON_OBJECTAGG()",
3919 agg->arg->value,
3920 JS_FORMAT_DEFAULT,
3921 InvalidOid, false);
3922 args = list_make2(key, val);
3924 returning = transformJsonConstructorOutput(pstate, agg->constructor->output,
3925 args);
3927 if (returning->format->format_type == JS_FORMAT_JSONB)
3929 if (agg->absent_on_null)
3930 if (agg->unique)
3931 aggfnoid = F_JSONB_OBJECT_AGG_UNIQUE_STRICT;
3932 else
3933 aggfnoid = F_JSONB_OBJECT_AGG_STRICT;
3934 else if (agg->unique)
3935 aggfnoid = F_JSONB_OBJECT_AGG_UNIQUE;
3936 else
3937 aggfnoid = F_JSONB_OBJECT_AGG;
3939 aggtype = JSONBOID;
3941 else
3943 if (agg->absent_on_null)
3944 if (agg->unique)
3945 aggfnoid = F_JSON_OBJECT_AGG_UNIQUE_STRICT;
3946 else
3947 aggfnoid = F_JSON_OBJECT_AGG_STRICT;
3948 else if (agg->unique)
3949 aggfnoid = F_JSON_OBJECT_AGG_UNIQUE;
3950 else
3951 aggfnoid = F_JSON_OBJECT_AGG;
3953 aggtype = JSONOID;
3956 return transformJsonAggConstructor(pstate, agg->constructor, returning,
3957 args, aggfnoid, aggtype,
3958 JSCTOR_JSON_OBJECTAGG,
3959 agg->unique, agg->absent_on_null);
3963 * Transform JSON_ARRAYAGG() aggregate function.
3965 * JSON_ARRAYAGG() is transformed into a JsonConstructorExpr node of type
3966 * JSCTOR_JSON_ARRAYAGG, which at runtime becomes a
3967 * json[b]_object_agg[_unique][_strict](agg->arg) call depending on the output
3968 * JSON format. The result is coerced to the target type given by
3969 * agg->constructor->output.
3971 static Node *
3972 transformJsonArrayAgg(ParseState *pstate, JsonArrayAgg *agg)
3974 JsonReturning *returning;
3975 Node *arg;
3976 Oid aggfnoid;
3977 Oid aggtype;
3979 arg = transformJsonValueExpr(pstate, "JSON_ARRAYAGG()", agg->arg,
3980 JS_FORMAT_DEFAULT, InvalidOid, false);
3982 returning = transformJsonConstructorOutput(pstate, agg->constructor->output,
3983 list_make1(arg));
3985 if (returning->format->format_type == JS_FORMAT_JSONB)
3987 aggfnoid = agg->absent_on_null ? F_JSONB_AGG_STRICT : F_JSONB_AGG;
3988 aggtype = JSONBOID;
3990 else
3992 aggfnoid = agg->absent_on_null ? F_JSON_AGG_STRICT : F_JSON_AGG;
3993 aggtype = JSONOID;
3996 return transformJsonAggConstructor(pstate, agg->constructor, returning,
3997 list_make1(arg), aggfnoid, aggtype,
3998 JSCTOR_JSON_ARRAYAGG,
3999 false, agg->absent_on_null);
4003 * Transform JSON_ARRAY() constructor.
4005 * JSON_ARRAY() is transformed into a JsonConstructorExpr node of type
4006 * JSCTOR_JSON_ARRAY. The result is coerced to the target type given
4007 * by ctor->output.
4009 static Node *
4010 transformJsonArrayConstructor(ParseState *pstate, JsonArrayConstructor *ctor)
4012 JsonReturning *returning;
4013 List *args = NIL;
4015 /* transform element expressions, if any */
4016 if (ctor->exprs)
4018 ListCell *lc;
4020 /* transform and append element arguments */
4021 foreach(lc, ctor->exprs)
4023 JsonValueExpr *jsval = castNode(JsonValueExpr, lfirst(lc));
4024 Node *val = transformJsonValueExpr(pstate, "JSON_ARRAY()",
4025 jsval, JS_FORMAT_DEFAULT,
4026 InvalidOid, false);
4028 args = lappend(args, val);
4032 returning = transformJsonConstructorOutput(pstate, ctor->output, args);
4034 return makeJsonConstructorExpr(pstate, JSCTOR_JSON_ARRAY, args, NULL,
4035 returning, false, ctor->absent_on_null,
4036 ctor->location);
4039 static Node *
4040 transformJsonParseArg(ParseState *pstate, Node *jsexpr, JsonFormat *format,
4041 Oid *exprtype)
4043 Node *raw_expr = transformExprRecurse(pstate, jsexpr);
4044 Node *expr = raw_expr;
4046 *exprtype = exprType(expr);
4048 /* prepare input document */
4049 if (*exprtype == BYTEAOID)
4051 JsonValueExpr *jve;
4053 expr = raw_expr;
4054 expr = makeJsonByteaToTextConversion(expr, format, exprLocation(expr));
4055 *exprtype = TEXTOID;
4057 jve = makeJsonValueExpr((Expr *) raw_expr, (Expr *) expr, format);
4058 expr = (Node *) jve;
4060 else
4062 char typcategory;
4063 bool typispreferred;
4065 get_type_category_preferred(*exprtype, &typcategory, &typispreferred);
4067 if (*exprtype == UNKNOWNOID || typcategory == TYPCATEGORY_STRING)
4069 expr = coerce_to_target_type(pstate, (Node *) expr, *exprtype,
4070 TEXTOID, -1,
4071 COERCION_IMPLICIT,
4072 COERCE_IMPLICIT_CAST, -1);
4073 *exprtype = TEXTOID;
4076 if (format->encoding != JS_ENC_DEFAULT)
4077 ereport(ERROR,
4078 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4079 parser_errposition(pstate, format->location),
4080 errmsg("cannot use JSON FORMAT ENCODING clause for non-bytea input types")));
4083 return expr;
4087 * Transform IS JSON predicate.
4089 static Node *
4090 transformJsonIsPredicate(ParseState *pstate, JsonIsPredicate *pred)
4092 Oid exprtype;
4093 Node *expr = transformJsonParseArg(pstate, pred->expr, pred->format,
4094 &exprtype);
4096 /* make resulting expression */
4097 if (exprtype != TEXTOID && exprtype != JSONOID && exprtype != JSONBOID)
4098 ereport(ERROR,
4099 (errcode(ERRCODE_DATATYPE_MISMATCH),
4100 errmsg("cannot use type %s in IS JSON predicate",
4101 format_type_be(exprtype))));
4103 /* This intentionally(?) drops the format clause. */
4104 return makeJsonIsPredicate(expr, NULL, pred->item_type,
4105 pred->unique_keys, pred->location);
4109 * Transform the RETURNING clause of a JSON_*() expression if there is one and
4110 * create one if not.
4112 static JsonReturning *
4113 transformJsonReturning(ParseState *pstate, JsonOutput *output, const char *fname)
4115 JsonReturning *returning;
4117 if (output)
4119 returning = transformJsonOutput(pstate, output, false);
4121 Assert(OidIsValid(returning->typid));
4123 if (returning->typid != JSONOID && returning->typid != JSONBOID)
4124 ereport(ERROR,
4125 (errcode(ERRCODE_DATATYPE_MISMATCH),
4126 errmsg("cannot use type %s in RETURNING clause of %s",
4127 format_type_be(returning->typid), fname),
4128 errhint("Try returning json or jsonb."),
4129 parser_errposition(pstate, output->typeName->location)));
4131 else
4133 /* Output type is JSON by default. */
4134 Oid targettype = JSONOID;
4135 JsonFormatType format = JS_FORMAT_JSON;
4137 returning = makeNode(JsonReturning);
4138 returning->format = makeJsonFormat(format, JS_ENC_DEFAULT, -1);
4139 returning->typid = targettype;
4140 returning->typmod = -1;
4143 return returning;
4147 * Transform a JSON() expression.
4149 * JSON() is transformed into a JsonConstructorExpr of type JSCTOR_JSON_PARSE,
4150 * which validates the input expression value as JSON.
4152 static Node *
4153 transformJsonParseExpr(ParseState *pstate, JsonParseExpr *jsexpr)
4155 JsonOutput *output = jsexpr->output;
4156 JsonReturning *returning;
4157 Node *arg;
4159 returning = transformJsonReturning(pstate, output, "JSON()");
4161 if (jsexpr->unique_keys)
4164 * Coerce string argument to text and then to json[b] in the executor
4165 * node with key uniqueness check.
4167 JsonValueExpr *jve = jsexpr->expr;
4168 Oid arg_type;
4170 arg = transformJsonParseArg(pstate, (Node *) jve->raw_expr, jve->format,
4171 &arg_type);
4173 if (arg_type != TEXTOID)
4174 ereport(ERROR,
4175 (errcode(ERRCODE_DATATYPE_MISMATCH),
4176 errmsg("cannot use non-string types with WITH UNIQUE KEYS clause"),
4177 parser_errposition(pstate, jsexpr->location)));
4179 else
4182 * Coerce argument to target type using CAST for compatibility with PG
4183 * function-like CASTs.
4185 arg = transformJsonValueExpr(pstate, "JSON()", jsexpr->expr,
4186 JS_FORMAT_JSON, returning->typid, false);
4189 return makeJsonConstructorExpr(pstate, JSCTOR_JSON_PARSE, list_make1(arg), NULL,
4190 returning, jsexpr->unique_keys, false,
4191 jsexpr->location);
4195 * Transform a JSON_SCALAR() expression.
4197 * JSON_SCALAR() is transformed into a JsonConstructorExpr of type
4198 * JSCTOR_JSON_SCALAR, which converts the input SQL scalar value into
4199 * a json[b] value.
4201 static Node *
4202 transformJsonScalarExpr(ParseState *pstate, JsonScalarExpr *jsexpr)
4204 Node *arg = transformExprRecurse(pstate, (Node *) jsexpr->expr);
4205 JsonOutput *output = jsexpr->output;
4206 JsonReturning *returning;
4208 returning = transformJsonReturning(pstate, output, "JSON_SCALAR()");
4210 if (exprType(arg) == UNKNOWNOID)
4211 arg = coerce_to_specific_type(pstate, arg, TEXTOID, "JSON_SCALAR");
4213 return makeJsonConstructorExpr(pstate, JSCTOR_JSON_SCALAR, list_make1(arg), NULL,
4214 returning, false, false, jsexpr->location);
4218 * Transform a JSON_SERIALIZE() expression.
4220 * JSON_SERIALIZE() is transformed into a JsonConstructorExpr of type
4221 * JSCTOR_JSON_SERIALIZE which converts the input JSON value into a character
4222 * or bytea string.
4224 static Node *
4225 transformJsonSerializeExpr(ParseState *pstate, JsonSerializeExpr *expr)
4227 JsonReturning *returning;
4228 Node *arg = transformJsonValueExpr(pstate, "JSON_SERIALIZE()",
4229 expr->expr,
4230 JS_FORMAT_JSON,
4231 InvalidOid, false);
4233 if (expr->output)
4235 returning = transformJsonOutput(pstate, expr->output, true);
4237 if (returning->typid != BYTEAOID)
4239 char typcategory;
4240 bool typispreferred;
4242 get_type_category_preferred(returning->typid, &typcategory,
4243 &typispreferred);
4244 if (typcategory != TYPCATEGORY_STRING)
4245 ereport(ERROR,
4246 (errcode(ERRCODE_DATATYPE_MISMATCH),
4247 errmsg("cannot use type %s in RETURNING clause of %s",
4248 format_type_be(returning->typid),
4249 "JSON_SERIALIZE()"),
4250 errhint("Try returning a string type or bytea.")));
4253 else
4255 /* RETURNING TEXT FORMAT JSON is by default */
4256 returning = makeNode(JsonReturning);
4257 returning->format = makeJsonFormat(JS_FORMAT_JSON, JS_ENC_DEFAULT, -1);
4258 returning->typid = TEXTOID;
4259 returning->typmod = -1;
4262 return makeJsonConstructorExpr(pstate, JSCTOR_JSON_SERIALIZE, list_make1(arg),
4263 NULL, returning, false, false, expr->location);
4267 * Transform JSON_VALUE, JSON_QUERY, JSON_EXISTS, JSON_TABLE functions into
4268 * a JsonExpr node.
4270 static Node *
4271 transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
4273 JsonExpr *jsexpr;
4274 Node *path_spec;
4275 const char *func_name = NULL;
4276 JsonFormatType default_format;
4278 switch (func->op)
4280 case JSON_EXISTS_OP:
4281 func_name = "JSON_EXISTS";
4282 default_format = JS_FORMAT_DEFAULT;
4283 break;
4284 case JSON_QUERY_OP:
4285 func_name = "JSON_QUERY";
4286 default_format = JS_FORMAT_JSONB;
4287 break;
4288 case JSON_VALUE_OP:
4289 func_name = "JSON_VALUE";
4290 default_format = JS_FORMAT_DEFAULT;
4291 break;
4292 case JSON_TABLE_OP:
4293 func_name = "JSON_TABLE";
4294 default_format = JS_FORMAT_JSONB;
4295 break;
4296 default:
4297 elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4298 default_format = JS_FORMAT_DEFAULT; /* keep compiler quiet */
4299 break;
4303 * Even though the syntax allows it, FORMAT JSON specification in
4304 * RETURNING is meaningless except for JSON_QUERY(). Flag if not
4305 * JSON_QUERY().
4307 if (func->output && func->op != JSON_QUERY_OP)
4309 JsonFormat *format = func->output->returning->format;
4311 if (format->format_type != JS_FORMAT_DEFAULT ||
4312 format->encoding != JS_ENC_DEFAULT)
4313 ereport(ERROR,
4314 errcode(ERRCODE_SYNTAX_ERROR),
4315 errmsg("cannot specify FORMAT JSON in RETURNING clause of %s()",
4316 func_name),
4317 parser_errposition(pstate, format->location));
4320 /* OMIT QUOTES is meaningless when strings are wrapped. */
4321 if (func->op == JSON_QUERY_OP)
4323 if (func->quotes == JS_QUOTES_OMIT &&
4324 (func->wrapper == JSW_CONDITIONAL ||
4325 func->wrapper == JSW_UNCONDITIONAL))
4326 ereport(ERROR,
4327 errcode(ERRCODE_SYNTAX_ERROR),
4328 errmsg("SQL/JSON QUOTES behavior must not be specified when WITH WRAPPER is used"),
4329 parser_errposition(pstate, func->location));
4330 if (func->on_empty != NULL &&
4331 func->on_empty->btype != JSON_BEHAVIOR_ERROR &&
4332 func->on_empty->btype != JSON_BEHAVIOR_NULL &&
4333 func->on_empty->btype != JSON_BEHAVIOR_EMPTY &&
4334 func->on_empty->btype != JSON_BEHAVIOR_EMPTY_ARRAY &&
4335 func->on_empty->btype != JSON_BEHAVIOR_EMPTY_OBJECT &&
4336 func->on_empty->btype != JSON_BEHAVIOR_DEFAULT)
4338 if (func->column_name == NULL)
4339 ereport(ERROR,
4340 errcode(ERRCODE_SYNTAX_ERROR),
4341 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4342 errmsg("invalid %s behavior", "ON EMPTY"),
4343 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4344 second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4345 errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for %s.",
4346 "ON EMPTY", "JSON_QUERY()"),
4347 parser_errposition(pstate, func->on_empty->location));
4348 else
4349 ereport(ERROR,
4350 errcode(ERRCODE_SYNTAX_ERROR),
4351 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4352 errmsg("invalid %s behavior for column \"%s\"",
4353 "ON EMPTY", func->column_name),
4354 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4355 errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for formatted columns.",
4356 "ON EMPTY"),
4357 parser_errposition(pstate, func->on_empty->location));
4359 if (func->on_error != NULL &&
4360 func->on_error->btype != JSON_BEHAVIOR_ERROR &&
4361 func->on_error->btype != JSON_BEHAVIOR_NULL &&
4362 func->on_error->btype != JSON_BEHAVIOR_EMPTY &&
4363 func->on_error->btype != JSON_BEHAVIOR_EMPTY_ARRAY &&
4364 func->on_error->btype != JSON_BEHAVIOR_EMPTY_OBJECT &&
4365 func->on_error->btype != JSON_BEHAVIOR_DEFAULT)
4367 if (func->column_name == NULL)
4368 ereport(ERROR,
4369 errcode(ERRCODE_SYNTAX_ERROR),
4370 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4371 errmsg("invalid %s behavior", "ON ERROR"),
4372 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4373 second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4374 errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for %s.",
4375 "ON ERROR", "JSON_QUERY()"),
4376 parser_errposition(pstate, func->on_error->location));
4377 else
4378 ereport(ERROR,
4379 errcode(ERRCODE_SYNTAX_ERROR),
4380 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4381 errmsg("invalid %s behavior for column \"%s\"",
4382 "ON ERROR", func->column_name),
4383 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4384 errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for formatted columns.",
4385 "ON ERROR"),
4386 parser_errposition(pstate, func->on_error->location));
4390 /* Check that ON ERROR/EMPTY behavior values are valid for the function. */
4391 if (func->op == JSON_EXISTS_OP &&
4392 func->on_error != NULL &&
4393 func->on_error->btype != JSON_BEHAVIOR_ERROR &&
4394 func->on_error->btype != JSON_BEHAVIOR_TRUE &&
4395 func->on_error->btype != JSON_BEHAVIOR_FALSE &&
4396 func->on_error->btype != JSON_BEHAVIOR_UNKNOWN)
4398 if (func->column_name == NULL)
4399 ereport(ERROR,
4400 errcode(ERRCODE_SYNTAX_ERROR),
4401 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4402 errmsg("invalid %s behavior", "ON ERROR"),
4403 errdetail("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in %s for %s.",
4404 "ON ERROR", "JSON_EXISTS()"),
4405 parser_errposition(pstate, func->on_error->location));
4406 else
4407 ereport(ERROR,
4408 errcode(ERRCODE_SYNTAX_ERROR),
4409 /*- translator: first %s is name a SQL/JSON clause (eg. ON EMPTY) */
4410 errmsg("invalid %s behavior for column \"%s\"",
4411 "ON ERROR", func->column_name),
4412 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4413 errdetail("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in %s for EXISTS columns.",
4414 "ON ERROR"),
4415 parser_errposition(pstate, func->on_error->location));
4417 if (func->op == JSON_VALUE_OP)
4419 if (func->on_empty != NULL &&
4420 func->on_empty->btype != JSON_BEHAVIOR_ERROR &&
4421 func->on_empty->btype != JSON_BEHAVIOR_NULL &&
4422 func->on_empty->btype != JSON_BEHAVIOR_DEFAULT)
4424 if (func->column_name == NULL)
4425 ereport(ERROR,
4426 errcode(ERRCODE_SYNTAX_ERROR),
4427 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4428 errmsg("invalid %s behavior", "ON EMPTY"),
4429 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4430 second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4431 errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for %s.",
4432 "ON EMPTY", "JSON_VALUE()"),
4433 parser_errposition(pstate, func->on_empty->location));
4434 else
4435 ereport(ERROR,
4436 errcode(ERRCODE_SYNTAX_ERROR),
4437 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4438 errmsg("invalid %s behavior for column \"%s\"",
4439 "ON EMPTY", func->column_name),
4440 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4441 errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for scalar columns.",
4442 "ON EMPTY"),
4443 parser_errposition(pstate, func->on_empty->location));
4445 if (func->on_error != NULL &&
4446 func->on_error->btype != JSON_BEHAVIOR_ERROR &&
4447 func->on_error->btype != JSON_BEHAVIOR_NULL &&
4448 func->on_error->btype != JSON_BEHAVIOR_DEFAULT)
4450 if (func->column_name == NULL)
4451 ereport(ERROR,
4452 errcode(ERRCODE_SYNTAX_ERROR),
4453 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4454 errmsg("invalid %s behavior", "ON ERROR"),
4455 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4456 second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4457 errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for %s.",
4458 "ON ERROR", "JSON_VALUE()"),
4459 parser_errposition(pstate, func->on_error->location));
4460 else
4461 ereport(ERROR,
4462 errcode(ERRCODE_SYNTAX_ERROR),
4463 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4464 errmsg("invalid %s behavior for column \"%s\"",
4465 "ON ERROR", func->column_name),
4466 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4467 errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for scalar columns.",
4468 "ON ERROR"),
4469 parser_errposition(pstate, func->on_error->location));
4473 jsexpr = makeNode(JsonExpr);
4474 jsexpr->location = func->location;
4475 jsexpr->op = func->op;
4476 jsexpr->column_name = func->column_name;
4479 * jsonpath machinery can only handle jsonb documents, so coerce the input
4480 * if not already of jsonb type.
4482 jsexpr->formatted_expr = transformJsonValueExpr(pstate, func_name,
4483 func->context_item,
4484 default_format,
4485 JSONBOID,
4486 false);
4487 jsexpr->format = func->context_item->format;
4489 path_spec = transformExprRecurse(pstate, func->pathspec);
4490 path_spec = coerce_to_target_type(pstate, path_spec, exprType(path_spec),
4491 JSONPATHOID, -1,
4492 COERCION_EXPLICIT, COERCE_IMPLICIT_CAST,
4493 exprLocation(path_spec));
4494 if (path_spec == NULL)
4495 ereport(ERROR,
4496 (errcode(ERRCODE_DATATYPE_MISMATCH),
4497 errmsg("JSON path expression must be of type %s, not of type %s",
4498 "jsonpath", format_type_be(exprType(path_spec))),
4499 parser_errposition(pstate, exprLocation(path_spec))));
4500 jsexpr->path_spec = path_spec;
4502 /* Transform and coerce the PASSING arguments to jsonb. */
4503 transformJsonPassingArgs(pstate, func_name,
4504 JS_FORMAT_JSONB,
4505 func->passing,
4506 &jsexpr->passing_values,
4507 &jsexpr->passing_names);
4509 /* Transform the JsonOutput into JsonReturning. */
4510 jsexpr->returning = transformJsonOutput(pstate, func->output, false);
4512 switch (func->op)
4514 case JSON_EXISTS_OP:
4515 /* JSON_EXISTS returns boolean by default. */
4516 if (!OidIsValid(jsexpr->returning->typid))
4518 jsexpr->returning->typid = BOOLOID;
4519 jsexpr->returning->typmod = -1;
4522 /* JSON_TABLE() COLUMNS can specify a non-boolean type. */
4523 if (jsexpr->returning->typid != BOOLOID)
4524 jsexpr->use_json_coercion = true;
4526 jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4527 JSON_BEHAVIOR_FALSE,
4528 jsexpr->returning);
4529 break;
4531 case JSON_QUERY_OP:
4532 /* JSON_QUERY returns jsonb by default. */
4533 if (!OidIsValid(jsexpr->returning->typid))
4535 JsonReturning *ret = jsexpr->returning;
4537 ret->typid = JSONBOID;
4538 ret->typmod = -1;
4542 * Keep quotes on scalar strings by default, omitting them only if
4543 * OMIT QUOTES is specified.
4545 jsexpr->omit_quotes = (func->quotes == JS_QUOTES_OMIT);
4546 jsexpr->wrapper = func->wrapper;
4549 * Set up to coerce the result value of JsonPathValue() to the
4550 * RETURNING type (default or user-specified), if needed. Also if
4551 * OMIT QUOTES is specified.
4553 if (jsexpr->returning->typid != JSONBOID || jsexpr->omit_quotes)
4554 jsexpr->use_json_coercion = true;
4556 /* Assume NULL ON EMPTY when ON EMPTY is not specified. */
4557 jsexpr->on_empty = transformJsonBehavior(pstate, func->on_empty,
4558 JSON_BEHAVIOR_NULL,
4559 jsexpr->returning);
4560 /* Assume NULL ON ERROR when ON ERROR is not specified. */
4561 jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4562 JSON_BEHAVIOR_NULL,
4563 jsexpr->returning);
4564 break;
4566 case JSON_VALUE_OP:
4567 /* JSON_VALUE returns text by default. */
4568 if (!OidIsValid(jsexpr->returning->typid))
4570 jsexpr->returning->typid = TEXTOID;
4571 jsexpr->returning->typmod = -1;
4575 * Override whatever transformJsonOutput() set these to, which
4576 * assumes that output type to be jsonb.
4578 jsexpr->returning->format->format_type = JS_FORMAT_DEFAULT;
4579 jsexpr->returning->format->encoding = JS_ENC_DEFAULT;
4581 /* Always omit quotes from scalar strings. */
4582 jsexpr->omit_quotes = true;
4585 * Set up to coerce the result value of JsonPathValue() to the
4586 * RETURNING type (default or user-specified), if needed.
4588 if (jsexpr->returning->typid != TEXTOID)
4590 if (get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN &&
4591 DomainHasConstraints(jsexpr->returning->typid))
4592 jsexpr->use_json_coercion = true;
4593 else
4594 jsexpr->use_io_coercion = true;
4597 /* Assume NULL ON EMPTY when ON EMPTY is not specified. */
4598 jsexpr->on_empty = transformJsonBehavior(pstate, func->on_empty,
4599 JSON_BEHAVIOR_NULL,
4600 jsexpr->returning);
4601 /* Assume NULL ON ERROR when ON ERROR is not specified. */
4602 jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4603 JSON_BEHAVIOR_NULL,
4604 jsexpr->returning);
4605 break;
4607 case JSON_TABLE_OP:
4608 if (!OidIsValid(jsexpr->returning->typid))
4610 jsexpr->returning->typid = exprType(jsexpr->formatted_expr);
4611 jsexpr->returning->typmod = -1;
4615 * Assume EMPTY ARRAY ON ERROR when ON ERROR is not specified.
4617 * ON EMPTY cannot be specified at the top level but it can be for
4618 * the individual columns.
4620 jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4621 JSON_BEHAVIOR_EMPTY_ARRAY,
4622 jsexpr->returning);
4623 break;
4625 default:
4626 elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4627 break;
4630 return (Node *) jsexpr;
4634 * Transform a SQL/JSON PASSING clause.
4636 static void
4637 transformJsonPassingArgs(ParseState *pstate, const char *constructName,
4638 JsonFormatType format, List *args,
4639 List **passing_values, List **passing_names)
4641 ListCell *lc;
4643 *passing_values = NIL;
4644 *passing_names = NIL;
4646 foreach(lc, args)
4648 JsonArgument *arg = castNode(JsonArgument, lfirst(lc));
4649 Node *expr = transformJsonValueExpr(pstate, constructName,
4650 arg->val, format,
4651 InvalidOid, true);
4653 *passing_values = lappend(*passing_values, expr);
4654 *passing_names = lappend(*passing_names, makeString(arg->name));
4659 * Recursively checks if the given expression, or its sub-node in some cases,
4660 * is valid for using as an ON ERROR / ON EMPTY DEFAULT expression.
4662 static bool
4663 ValidJsonBehaviorDefaultExpr(Node *expr, void *context)
4665 if (expr == NULL)
4666 return false;
4668 switch (nodeTag(expr))
4670 /* Acceptable expression nodes */
4671 case T_Const:
4672 case T_FuncExpr:
4673 case T_OpExpr:
4674 return true;
4676 /* Acceptable iff arg of the following nodes is one of the above */
4677 case T_CoerceViaIO:
4678 case T_CoerceToDomain:
4679 case T_ArrayCoerceExpr:
4680 case T_ConvertRowtypeExpr:
4681 case T_RelabelType:
4682 case T_CollateExpr:
4683 return expression_tree_walker(expr, ValidJsonBehaviorDefaultExpr,
4684 context);
4685 default:
4686 break;
4689 return false;
4693 * Transform a JSON BEHAVIOR clause.
4695 static JsonBehavior *
4696 transformJsonBehavior(ParseState *pstate, JsonBehavior *behavior,
4697 JsonBehaviorType default_behavior,
4698 JsonReturning *returning)
4700 JsonBehaviorType btype = default_behavior;
4701 Node *expr = NULL;
4702 bool coerce_at_runtime = false;
4703 int location = -1;
4705 if (behavior)
4707 btype = behavior->btype;
4708 location = behavior->location;
4709 if (btype == JSON_BEHAVIOR_DEFAULT)
4711 expr = transformExprRecurse(pstate, behavior->expr);
4712 if (!ValidJsonBehaviorDefaultExpr(expr, NULL))
4713 ereport(ERROR,
4714 (errcode(ERRCODE_DATATYPE_MISMATCH),
4715 errmsg("can only specify a constant, non-aggregate function, or operator expression for DEFAULT"),
4716 parser_errposition(pstate, exprLocation(expr))));
4717 if (contain_var_clause(expr))
4718 ereport(ERROR,
4719 (errcode(ERRCODE_DATATYPE_MISMATCH),
4720 errmsg("DEFAULT expression must not contain column references"),
4721 parser_errposition(pstate, exprLocation(expr))));
4722 if (expression_returns_set(expr))
4723 ereport(ERROR,
4724 (errcode(ERRCODE_DATATYPE_MISMATCH),
4725 errmsg("DEFAULT expression must not return a set"),
4726 parser_errposition(pstate, exprLocation(expr))));
4730 if (expr == NULL && btype != JSON_BEHAVIOR_ERROR)
4731 expr = GetJsonBehaviorConst(btype, location);
4734 * Try to coerce the expression if needed.
4736 * Use runtime coercion using json_populate_type() if the expression is
4737 * NULL, jsonb-valued, or boolean-valued (unless the target type is
4738 * integer or domain over integer, in which case use the
4739 * boolean-to-integer cast function).
4741 * For other non-NULL expressions, try to find a cast and error out if one
4742 * is not found.
4744 if (expr && exprType(expr) != returning->typid)
4746 bool isnull = (IsA(expr, Const) && ((Const *) expr)->constisnull);
4748 if (isnull ||
4749 exprType(expr) == JSONBOID ||
4750 (exprType(expr) == BOOLOID &&
4751 getBaseType(returning->typid) != INT4OID))
4753 coerce_at_runtime = true;
4756 * json_populate_type() expects to be passed a jsonb value, so gin
4757 * up a Const containing the appropriate boolean value represented
4758 * as jsonb, discarding the original Const containing a plain
4759 * boolean.
4761 if (exprType(expr) == BOOLOID)
4763 char *val = btype == JSON_BEHAVIOR_TRUE ? "true" : "false";
4765 expr = (Node *) makeConst(JSONBOID, -1, InvalidOid, -1,
4766 DirectFunctionCall1(jsonb_in,
4767 CStringGetDatum(val)),
4768 false, false);
4771 else
4773 Node *coerced_expr;
4774 char typcategory = TypeCategory(returning->typid);
4777 * Use an assignment cast if coercing to a string type so that
4778 * build_coercion_expression() assumes implicit coercion when
4779 * coercing the typmod, so that inputs exceeding length cause an
4780 * error instead of silent truncation.
4782 coerced_expr =
4783 coerce_to_target_type(pstate, expr, exprType(expr),
4784 returning->typid, returning->typmod,
4785 (typcategory == TYPCATEGORY_STRING ||
4786 typcategory == TYPCATEGORY_BITSTRING) ?
4787 COERCION_ASSIGNMENT :
4788 COERCION_EXPLICIT,
4789 COERCE_EXPLICIT_CAST,
4790 exprLocation((Node *) behavior));
4792 if (coerced_expr == NULL)
4795 * Provide a HINT if the expression comes from a DEFAULT
4796 * clause.
4798 if (btype == JSON_BEHAVIOR_DEFAULT)
4799 ereport(ERROR,
4800 errcode(ERRCODE_CANNOT_COERCE),
4801 errmsg("cannot cast behavior expression of type %s to %s",
4802 format_type_be(exprType(expr)),
4803 format_type_be(returning->typid)),
4804 errhint("You will need to explicitly cast the expression to type %s.",
4805 format_type_be(returning->typid)),
4806 parser_errposition(pstate, exprLocation(expr)));
4807 else
4808 ereport(ERROR,
4809 errcode(ERRCODE_CANNOT_COERCE),
4810 errmsg("cannot cast behavior expression of type %s to %s",
4811 format_type_be(exprType(expr)),
4812 format_type_be(returning->typid)),
4813 parser_errposition(pstate, exprLocation(expr)));
4816 expr = coerced_expr;
4820 if (behavior)
4821 behavior->expr = expr;
4822 else
4823 behavior = makeJsonBehavior(btype, expr, location);
4825 behavior->coerce = coerce_at_runtime;
4827 return behavior;
4831 * Returns a Const node holding the value for the given non-ERROR
4832 * JsonBehaviorType.
4834 static Node *
4835 GetJsonBehaviorConst(JsonBehaviorType btype, int location)
4837 Datum val = (Datum) 0;
4838 Oid typid = JSONBOID;
4839 int len = -1;
4840 bool isbyval = false;
4841 bool isnull = false;
4842 Const *con;
4844 switch (btype)
4846 case JSON_BEHAVIOR_EMPTY_ARRAY:
4847 val = DirectFunctionCall1(jsonb_in, CStringGetDatum("[]"));
4848 break;
4850 case JSON_BEHAVIOR_EMPTY_OBJECT:
4851 val = DirectFunctionCall1(jsonb_in, CStringGetDatum("{}"));
4852 break;
4854 case JSON_BEHAVIOR_TRUE:
4855 val = BoolGetDatum(true);
4856 typid = BOOLOID;
4857 len = sizeof(bool);
4858 isbyval = true;
4859 break;
4861 case JSON_BEHAVIOR_FALSE:
4862 val = BoolGetDatum(false);
4863 typid = BOOLOID;
4864 len = sizeof(bool);
4865 isbyval = true;
4866 break;
4868 case JSON_BEHAVIOR_NULL:
4869 case JSON_BEHAVIOR_UNKNOWN:
4870 case JSON_BEHAVIOR_EMPTY:
4871 val = (Datum) 0;
4872 isnull = true;
4873 typid = INT4OID;
4874 len = sizeof(int32);
4875 isbyval = true;
4876 break;
4878 /* These two behavior types are handled by the caller. */
4879 case JSON_BEHAVIOR_DEFAULT:
4880 case JSON_BEHAVIOR_ERROR:
4881 Assert(false);
4882 break;
4884 default:
4885 elog(ERROR, "unrecognized SQL/JSON behavior %d", btype);
4886 break;
4889 con = makeConst(typid, -1, InvalidOid, len, val, isnull, isbyval);
4890 con->location = location;
4892 return (Node *) con;