PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
parse_expr.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * parse_expr.c
4 * handle expressions in parser
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/parser/parse_expr.c
12 *
13 *-------------------------------------------------------------------------
14 */
15
16#include "postgres.h"
17
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"
30#include "parser/parse_expr.h"
31#include "parser/parse_func.h"
32#include "parser/parse_oper.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"
42
43/* GUC parameters */
45
46
47static Node *transformExprRecurse(ParseState *pstate, Node *expr);
48static Node *transformParamRef(ParseState *pstate, ParamRef *pref);
49static Node *transformAExprOp(ParseState *pstate, A_Expr *a);
50static Node *transformAExprOpAny(ParseState *pstate, A_Expr *a);
51static Node *transformAExprOpAll(ParseState *pstate, A_Expr *a);
54static Node *transformAExprIn(ParseState *pstate, A_Expr *a);
57static Node *transformBoolExpr(ParseState *pstate, BoolExpr *a);
60static Node *transformCaseExpr(ParseState *pstate, CaseExpr *c);
61static Node *transformSubLink(ParseState *pstate, SubLink *sublink);
63 Oid array_type, Oid element_type, int32 typmod);
64static Node *transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault);
68 SQLValueFunction *svf);
69static Node *transformXmlExpr(ParseState *pstate, XmlExpr *x);
73static Node *transformColumnRef(ParseState *pstate, ColumnRef *cref);
75 ParseNamespaceItem *nsitem,
76 int sublevels_up, int location);
78static Node *transformTypeCast(ParseState *pstate, TypeCast *tc);
89static Node *transformJsonParseExpr(ParseState *pstate, JsonParseExpr *jsexpr);
92 JsonSerializeExpr *expr);
94static void transformJsonPassingArgs(ParseState *pstate, const char *constructName,
96 List **passing_values, List **passing_names);
98 JsonBehaviorType default_behavior,
99 JsonReturning *returning);
100static Node *GetJsonBehaviorConst(JsonBehaviorType btype, int location);
101static Node *make_row_comparison_op(ParseState *pstate, List *opname,
102 List *largs, List *rargs, int location);
103static Node *make_row_distinct_op(ParseState *pstate, List *opname,
104 RowExpr *lrow, RowExpr *rrow, int location);
105static Expr *make_distinct_op(ParseState *pstate, List *opname,
106 Node *ltree, Node *rtree, int location);
108 A_Expr *distincta, Node *arg);
109
110
111/*
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.
116 */
117Node *
119{
120 Node *result;
121 ParseExprKind sv_expr_kind;
122
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;
127
128 result = transformExprRecurse(pstate, expr);
129
130 pstate->p_expr_kind = sv_expr_kind;
131
132 return result;
133}
134
135static Node *
137{
138 Node *result;
139
140 if (expr == NULL)
141 return NULL;
142
143 /* Guard against stack overflow due to overly complex expressions */
145
146 switch (nodeTag(expr))
147 {
148 case T_ColumnRef:
149 result = transformColumnRef(pstate, (ColumnRef *) expr);
150 break;
151
152 case T_ParamRef:
153 result = transformParamRef(pstate, (ParamRef *) expr);
154 break;
155
156 case T_A_Const:
157 result = (Node *) make_const(pstate, (A_Const *) expr);
158 break;
159
160 case T_A_Indirection:
161 result = transformIndirection(pstate, (A_Indirection *) expr);
162 break;
163
164 case T_A_ArrayExpr:
165 result = transformArrayExpr(pstate, (A_ArrayExpr *) expr,
167 break;
168
169 case T_TypeCast:
170 result = transformTypeCast(pstate, (TypeCast *) expr);
171 break;
172
173 case T_CollateClause:
174 result = transformCollateClause(pstate, (CollateClause *) expr);
175 break;
176
177 case T_A_Expr:
178 {
179 A_Expr *a = (A_Expr *) expr;
180
181 switch (a->kind)
182 {
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:
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:
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;
218 }
219 break;
220 }
221
222 case T_BoolExpr:
223 result = transformBoolExpr(pstate, (BoolExpr *) expr);
224 break;
225
226 case T_FuncCall:
227 result = transformFuncCall(pstate, (FuncCall *) expr);
228 break;
229
230 case T_MultiAssignRef:
231 result = transformMultiAssignRef(pstate, (MultiAssignRef *) expr);
232 break;
233
234 case T_GroupingFunc:
235 result = transformGroupingFunc(pstate, (GroupingFunc *) expr);
236 break;
237
238 case T_MergeSupportFunc:
239 result = transformMergeSupportFunc(pstate,
240 (MergeSupportFunc *) expr);
241 break;
242
243 case T_NamedArgExpr:
244 {
245 NamedArgExpr *na = (NamedArgExpr *) expr;
246
247 na->arg = (Expr *) transformExprRecurse(pstate, (Node *) na->arg);
248 result = expr;
249 break;
250 }
251
252 case T_SubLink:
253 result = transformSubLink(pstate, (SubLink *) expr);
254 break;
255
256 case T_CaseExpr:
257 result = transformCaseExpr(pstate, (CaseExpr *) expr);
258 break;
259
260 case T_RowExpr:
261 result = transformRowExpr(pstate, (RowExpr *) expr, false);
262 break;
263
264 case T_CoalesceExpr:
265 result = transformCoalesceExpr(pstate, (CoalesceExpr *) expr);
266 break;
267
268 case T_MinMaxExpr:
269 result = transformMinMaxExpr(pstate, (MinMaxExpr *) expr);
270 break;
271
272 case T_SQLValueFunction:
273 result = transformSQLValueFunction(pstate,
274 (SQLValueFunction *) expr);
275 break;
276
277 case T_XmlExpr:
278 result = transformXmlExpr(pstate, (XmlExpr *) expr);
279 break;
280
281 case T_XmlSerialize:
282 result = transformXmlSerialize(pstate, (XmlSerialize *) expr);
283 break;
284
285 case T_NullTest:
286 {
287 NullTest *n = (NullTest *) expr;
288
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;
294 }
295
296 case T_BooleanTest:
297 result = transformBooleanTest(pstate, (BooleanTest *) expr);
298 break;
299
300 case T_CurrentOfExpr:
301 result = transformCurrentOfExpr(pstate, (CurrentOfExpr *) expr);
302 break;
303
304 /*
305 * In all places where DEFAULT is legal, the caller should have
306 * processed it rather than passing it to transformExpr().
307 */
308 case T_SetToDefault:
310 (errcode(ERRCODE_SYNTAX_ERROR),
311 errmsg("DEFAULT is not allowed in this context"),
312 parser_errposition(pstate,
313 ((SetToDefault *) expr)->location)));
314 break;
315
316 /*
317 * CaseTestExpr doesn't require any processing; it is only
318 * injected into parse trees in a fully-formed state.
319 *
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.
325 */
326 case T_CaseTestExpr:
327 case T_Var:
328 {
329 result = (Node *) expr;
330 break;
331 }
332
333 case T_JsonObjectConstructor:
334 result = transformJsonObjectConstructor(pstate, (JsonObjectConstructor *) expr);
335 break;
336
337 case T_JsonArrayConstructor:
338 result = transformJsonArrayConstructor(pstate, (JsonArrayConstructor *) expr);
339 break;
340
341 case T_JsonArrayQueryConstructor:
343 break;
344
345 case T_JsonObjectAgg:
346 result = transformJsonObjectAgg(pstate, (JsonObjectAgg *) expr);
347 break;
348
349 case T_JsonArrayAgg:
350 result = transformJsonArrayAgg(pstate, (JsonArrayAgg *) expr);
351 break;
352
353 case T_JsonIsPredicate:
354 result = transformJsonIsPredicate(pstate, (JsonIsPredicate *) expr);
355 break;
356
357 case T_JsonParseExpr:
358 result = transformJsonParseExpr(pstate, (JsonParseExpr *) expr);
359 break;
360
361 case T_JsonScalarExpr:
362 result = transformJsonScalarExpr(pstate, (JsonScalarExpr *) expr);
363 break;
364
365 case T_JsonSerializeExpr:
366 result = transformJsonSerializeExpr(pstate, (JsonSerializeExpr *) expr);
367 break;
368
369 case T_JsonFuncExpr:
370 result = transformJsonFuncExpr(pstate, (JsonFuncExpr *) expr);
371 break;
372
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;
378 }
379
380 return result;
381}
382
383/*
384 * helper routine for delivering "column does not exist" error message
385 *
386 * (Usually we don't have to work this hard, but the general case of field
387 * selection from an arbitrary node needs it.)
388 */
389static void
390unknown_attribute(ParseState *pstate, Node *relref, const char *attname,
391 int location)
392{
393 RangeTblEntry *rte;
394
395 if (IsA(relref, Var) &&
396 ((Var *) relref)->varattno == InvalidAttrNumber)
397 {
398 /* Reference the RTE by alias not by actual table name */
399 rte = GetRTEByRangeTablePosn(pstate,
400 ((Var *) relref)->varno,
401 ((Var *) relref)->varlevelsup);
403 (errcode(ERRCODE_UNDEFINED_COLUMN),
404 errmsg("column %s.%s does not exist",
405 rte->eref->aliasname, attname),
406 parser_errposition(pstate, location)));
407 }
408 else
409 {
410 /* Have to do it by reference to the type of the expression */
411 Oid relTypeId = exprType(relref);
412
413 if (ISCOMPLEX(relTypeId))
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)
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
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)));
432 }
433}
434
435static Node *
437{
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;
443
444 /*
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.
448 */
449 foreach(i, ind->indirection)
450 {
451 Node *n = lfirst(i);
452
453 if (IsA(n, A_Indices))
454 subscripts = lappend(subscripts, n);
455 else if (IsA(n, A_Star))
456 {
458 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
459 errmsg("row expansion via \"*\" is not supported here"),
460 parser_errposition(pstate, location)));
461 }
462 else
463 {
464 Node *newresult;
465
466 Assert(IsA(n, String));
467
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;
477
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;
488 }
489 }
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);
498
499 return result;
500}
501
502/*
503 * Transform a ColumnRef.
504 *
505 * If you find yourself changing this code, see also ExpandColumnRefStar.
506 */
507static Node *
509{
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
517 {
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;
524
525 /*
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.
529 */
530 err = NULL;
531 switch (pstate->p_expr_kind)
532 {
533 case EXPR_KIND_NONE:
534 Assert(false); /* can't happen */
535 break;
536 case EXPR_KIND_OTHER:
541 case EXPR_KIND_WHERE:
542 case EXPR_KIND_POLICY:
543 case EXPR_KIND_HAVING:
544 case EXPR_KIND_FILTER:
558 case EXPR_KIND_LIMIT:
559 case EXPR_KIND_OFFSET:
562 case EXPR_KIND_VALUES:
578 /* okay */
579 break;
580
582 err = _("cannot use column reference in DEFAULT expression");
583 break;
585 err = _("cannot use column reference in partition bound expression");
586 break;
587
588 /*
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.
594 */
595 }
596 if (err)
598 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
599 errmsg_internal("%s", err),
600 parser_errposition(pstate, cref->location)));
601
602 /*
603 * Give the PreParseColumnRefHook, if any, first shot. If it returns
604 * non-null then that's all, folks.
605 */
606 if (pstate->p_pre_columnref_hook != NULL)
607 {
608 node = pstate->p_pre_columnref_hook(pstate, cref);
609 if (node != NULL)
610 return node;
611 }
612
613 /*----------
614 * The allowed syntaxes are:
615 *
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.
625 *
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.
631 *
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 *----------
635 */
636 switch (list_length(cref->fields))
637 {
638 case 1:
639 {
640 Node *field1 = (Node *) linitial(cref->fields);
641
642 colname = strVal(field1);
643
644 /* Try to identify as an unqualified column */
645 node = colNameToVar(pstate, colname, false, cref->location);
646
647 if (node == NULL)
648 {
649 /*
650 * Not known as a column of any range-table entry.
651 *
652 * Try to find the name as a relation. Note that only
653 * relations already entered into the rangetable will be
654 * recognized.
655 *
656 * This is a hack for backwards compatibility with
657 * PostQUEL-inspired syntax. The preferred form now is
658 * "rel.*".
659 */
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);
666 }
667 break;
668 }
669 case 2:
670 {
671 Node *field1 = (Node *) linitial(cref->fields);
672 Node *field2 = (Node *) lsecond(cref->fields);
673
674 relname = strVal(field1);
675
676 /* Locate the referenced nsitem */
677 nsitem = refnameNamespaceItem(pstate, nspname, relname,
678 cref->location,
679 &levels_up);
680 if (nsitem == NULL)
681 {
682 crerr = CRERR_NO_RTE;
683 break;
684 }
685
686 /* Whole-row reference? */
687 if (IsA(field2, A_Star))
688 {
689 node = transformWholeRowRef(pstate, nsitem, levels_up,
690 cref->location);
691 break;
692 }
693
694 colname = strVal(field2);
695
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)
700 {
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);
711 }
712 break;
713 }
714 case 3:
715 {
716 Node *field1 = (Node *) linitial(cref->fields);
717 Node *field2 = (Node *) lsecond(cref->fields);
718 Node *field3 = (Node *) lthird(cref->fields);
719
720 nspname = strVal(field1);
721 relname = strVal(field2);
722
723 /* Locate the referenced nsitem */
724 nsitem = refnameNamespaceItem(pstate, nspname, relname,
725 cref->location,
726 &levels_up);
727 if (nsitem == NULL)
728 {
729 crerr = CRERR_NO_RTE;
730 break;
731 }
732
733 /* Whole-row reference? */
734 if (IsA(field3, A_Star))
735 {
736 node = transformWholeRowRef(pstate, nsitem, levels_up,
737 cref->location);
738 break;
739 }
740
741 colname = strVal(field3);
742
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)
747 {
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);
758 }
759 break;
760 }
761 case 4:
762 {
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;
768
769 catname = strVal(field1);
770 nspname = strVal(field2);
771 relname = strVal(field3);
772
773 /*
774 * We check the catalog name and then ignore it.
775 */
776 if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
777 {
778 crerr = CRERR_WRONG_DB;
779 break;
780 }
781
782 /* Locate the referenced nsitem */
783 nsitem = refnameNamespaceItem(pstate, nspname, relname,
784 cref->location,
785 &levels_up);
786 if (nsitem == NULL)
787 {
788 crerr = CRERR_NO_RTE;
789 break;
790 }
791
792 /* Whole-row reference? */
793 if (IsA(field4, A_Star))
794 {
795 node = transformWholeRowRef(pstate, nsitem, levels_up,
796 cref->location);
797 break;
798 }
799
800 colname = strVal(field4);
801
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)
806 {
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);
817 }
818 break;
819 }
820 default:
821 crerr = CRERR_TOO_MANY; /* too many dotted names */
822 break;
823 }
824
825 /*
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.
833 */
834 if (pstate->p_post_columnref_hook != NULL)
835 {
836 Node *hookresult;
837
838 hookresult = pstate->p_post_columnref_hook(pstate, cref, node);
839 if (node == NULL)
840 node = hookresult;
841 else if (hookresult != NULL)
843 (errcode(ERRCODE_AMBIGUOUS_COLUMN),
844 errmsg("column reference \"%s\" is ambiguous",
845 NameListToString(cref->fields)),
846 parser_errposition(pstate, cref->location)));
847 }
848
849 /*
850 * Throw error if no translation found.
851 */
852 if (node == NULL)
853 {
854 switch (crerr)
855 {
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:
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:
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;
877 }
878 }
879
880 return node;
881}
882
883static Node *
885{
886 Node *result;
887
888 /*
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.
891 */
892 if (pstate->p_paramref_hook != NULL)
893 result = pstate->p_paramref_hook(pstate, pref);
894 else
895 result = NULL;
896
897 if (result == NULL)
899 (errcode(ERRCODE_UNDEFINED_PARAMETER),
900 errmsg("there is no parameter $%d", pref->number),
901 parser_errposition(pstate, pref->location)));
902
903 return result;
904}
905
906/* Test whether an a_expr is a plain NULL constant or not */
907static bool
909{
910 if (arg && IsA(arg, A_Const))
911 {
912 A_Const *con = (A_Const *) arg;
913
914 if (con->isnull)
915 return true;
916 }
917 return false;
918}
919
920static Node *
922{
923 Node *lexpr = a->lexpr;
924 Node *rexpr = a->rexpr;
925 Node *result;
926
927 /*
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.)
933 */
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)))
939 {
941
943 n->location = a->location;
944
945 if (exprIsNullConstant(lexpr))
946 n->arg = (Expr *) rexpr;
947 else
948 n->arg = (Expr *) lexpr;
949
950 result = transformExprRecurse(pstate, (Node *) n);
951 }
952 else if (lexpr && IsA(lexpr, RowExpr) &&
953 rexpr && IsA(rexpr, SubLink) &&
954 ((SubLink *) rexpr)->subLinkType == EXPR_SUBLINK)
955 {
956 /*
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.
960 */
961 SubLink *s = (SubLink *) rexpr;
962
964 s->testexpr = lexpr;
965 s->operName = a->name;
966 s->location = a->location;
967 result = transformExprRecurse(pstate, (Node *) s);
968 }
969 else if (lexpr && IsA(lexpr, RowExpr) &&
970 rexpr && IsA(rexpr, RowExpr))
971 {
972 /* ROW() op ROW() is handled specially */
973 lexpr = transformExprRecurse(pstate, lexpr);
974 rexpr = transformExprRecurse(pstate, rexpr);
975
976 result = make_row_comparison_op(pstate,
977 a->name,
978 castNode(RowExpr, lexpr)->args,
979 castNode(RowExpr, rexpr)->args,
980 a->location);
981 }
982 else
983 {
984 /* Ordinary scalar operator */
985 Node *last_srf = pstate->p_last_srf;
986
987 lexpr = transformExprRecurse(pstate, lexpr);
988 rexpr = transformExprRecurse(pstate, rexpr);
989
990 result = (Node *) make_op(pstate,
991 a->name,
992 lexpr,
993 rexpr,
994 last_srf,
995 a->location);
996 }
997
998 return result;
999}
1000
1001static Node *
1003{
1004 Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1005 Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1006
1007 return (Node *) make_scalar_array_op(pstate,
1008 a->name,
1009 true,
1010 lexpr,
1011 rexpr,
1012 a->location);
1013}
1014
1015static Node *
1017{
1018 Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1019 Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1020
1021 return (Node *) make_scalar_array_op(pstate,
1022 a->name,
1023 false,
1024 lexpr,
1025 rexpr,
1026 a->location);
1027}
1028
1029static Node *
1031{
1032 Node *lexpr = a->lexpr;
1033 Node *rexpr = a->rexpr;
1034 Node *result;
1035
1036 /*
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.
1040 */
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);
1045
1046 lexpr = transformExprRecurse(pstate, lexpr);
1047 rexpr = transformExprRecurse(pstate, rexpr);
1048
1049 if (lexpr && IsA(lexpr, RowExpr) &&
1050 rexpr && IsA(rexpr, RowExpr))
1051 {
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);
1057 }
1058 else
1059 {
1060 /* Ordinary scalar operator */
1061 result = (Node *) make_distinct_op(pstate,
1062 a->name,
1063 lexpr,
1064 rexpr,
1065 a->location);
1066 }
1067
1068 /*
1069 * If it's NOT DISTINCT, we first build a DistinctExpr and then stick a
1070 * NOT on top.
1071 */
1072 if (a->kind == AEXPR_NOT_DISTINCT)
1073 result = (Node *) makeBoolExpr(NOT_EXPR,
1074 list_make1(result),
1075 a->location);
1076
1077 return result;
1078}
1079
1080static Node *
1082{
1083 Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1084 Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1085 OpExpr *result;
1086
1087 result = (OpExpr *) make_op(pstate,
1088 a->name,
1089 lexpr,
1090 rexpr,
1091 pstate->p_last_srf,
1092 a->location);
1093
1094 /*
1095 * The comparison operator itself should yield boolean ...
1096 */
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)));
1109
1110 /*
1111 * ... but the NullIfExpr will yield the first operand's type.
1112 */
1113 result->opresulttype = exprType((Node *) linitial(result->args));
1114
1115 /*
1116 * We rely on NullIfExpr and OpExpr being the same struct
1117 */
1118 NodeSetTag(result, T_NullIfExpr);
1119
1120 return (Node *) result;
1121}
1122
1123static Node *
1125{
1126 Node *result = NULL;
1127 Node *lexpr;
1128 List *rexprs;
1129 List *rvars;
1130 List *rnonvars;
1131 bool useOr;
1132 ListCell *l;
1133
1134 /*
1135 * If the operator is <>, combine with AND not OR.
1136 */
1137 if (strcmp(strVal(linitial(a->name)), "<>") == 0)
1138 useOr = false;
1139 else
1140 useOr = true;
1141
1142 /*
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.
1149 *
1150 * First step: transform all the inputs, and detect whether any contain
1151 * Vars.
1152 */
1153 lexpr = transformExprRecurse(pstate, a->lexpr);
1154 rexprs = rvars = rnonvars = NIL;
1155 foreach(l, (List *) a->rexpr)
1156 {
1157 Node *rexpr = transformExprRecurse(pstate, lfirst(l));
1158
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);
1164 }
1165
1166 /*
1167 * ScalarArrayOpExpr is only going to be useful if there's more than one
1168 * non-Var righthand item.
1169 */
1170 if (list_length(rnonvars) > 1)
1171 {
1172 List *allexprs;
1173 Oid scalar_type;
1174 Oid array_type;
1175
1176 /*
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).
1180 *
1181 * Note: use list_concat here not lcons, to avoid damaging rnonvars.
1182 */
1183 allexprs = list_concat(list_make1(lexpr), rnonvars);
1184 scalar_type = select_common_type(pstate, allexprs, NULL, NULL);
1185
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;
1190
1191 /*
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.
1196 */
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)
1202 {
1203 /*
1204 * OK: coerce all the right-hand non-Var inputs to the common type
1205 * and build an ArrayExpr for them.
1206 */
1207 List *aexprs;
1208 ArrayExpr *newa;
1209
1210 aexprs = NIL;
1211 foreach(l, rnonvars)
1212 {
1213 Node *rexpr = (Node *) lfirst(l);
1214
1215 rexpr = coerce_to_common_type(pstate, rexpr,
1216 scalar_type,
1217 "IN");
1218 aexprs = lappend(aexprs, rexpr);
1219 }
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;
1227
1228 result = (Node *) make_scalar_array_op(pstate,
1229 a->name,
1230 useOr,
1231 lexpr,
1232 (Node *) newa,
1233 a->location);
1234
1235 /* Consider only the Vars (if any) in the loop below */
1236 rexprs = rvars;
1237 }
1238 }
1239
1240 /*
1241 * Must do it the hard way, ie, with a boolean expression tree.
1242 */
1243 foreach(l, rexprs)
1244 {
1245 Node *rexpr = (Node *) lfirst(l);
1246 Node *cmp;
1247
1248 if (IsA(lexpr, RowExpr) &&
1249 IsA(rexpr, RowExpr))
1250 {
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);
1257 }
1258 else
1259 {
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);
1267 }
1268
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);
1276 }
1277
1278 return result;
1279}
1280
1281static Node *
1283{
1284 Node *aexpr;
1285 Node *bexpr;
1286 Node *cexpr;
1287 Node *result;
1288 Node *sub1;
1289 Node *sub2;
1290 List *args;
1291
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);
1298
1299 /*
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 ...)
1304 *
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
1309 */
1310 switch (a->kind)
1311 {
1312 case AEXPR_BETWEEN:
1314 aexpr, bexpr,
1315 a->location),
1317 copyObject(aexpr), cexpr,
1318 a->location));
1319 result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1320 break;
1321 case AEXPR_NOT_BETWEEN:
1323 aexpr, bexpr,
1324 a->location),
1326 copyObject(aexpr), cexpr,
1327 a->location));
1328 result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1329 break;
1330 case AEXPR_BETWEEN_SYM:
1332 aexpr, bexpr,
1333 a->location),
1335 copyObject(aexpr), cexpr,
1336 a->location));
1337 sub1 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1339 copyObject(aexpr), copyObject(cexpr),
1340 a->location),
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;
1350 aexpr, bexpr,
1351 a->location),
1353 copyObject(aexpr), cexpr,
1354 a->location));
1355 sub1 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1357 copyObject(aexpr), copyObject(cexpr),
1358 a->location),
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;
1370 }
1371
1372 return transformExprRecurse(pstate, result);
1373}
1374
1375static Node *
1377{
1378 /*
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.
1381 */
1383 {
1384 ParseState *parent_pstate = pstate->parentParseState;
1385
1386 while (parent_pstate &&
1387 parent_pstate->p_expr_kind != EXPR_KIND_MERGE_RETURNING)
1388 parent_pstate = parent_pstate->parentParseState;
1389
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));
1395 }
1396
1397 return (Node *) f;
1398}
1399
1400static Node *
1402{
1403 List *args = NIL;
1404 const char *opname;
1405 ListCell *lc;
1406
1407 switch (a->boolop)
1408 {
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;
1422 }
1423
1424 foreach(lc, a->args)
1425 {
1426 Node *arg = (Node *) lfirst(lc);
1427
1428 arg = transformExprRecurse(pstate, arg);
1429 arg = coerce_to_boolean(pstate, arg, opname);
1430 args = lappend(args, arg);
1431 }
1432
1433 return (Node *) makeBoolExpr(a->boolop, args, a->location);
1434}
1435
1436static Node *
1438{
1439 Node *last_srf = pstate->p_last_srf;
1440 List *targs;
1441 ListCell *args;
1442
1443 /* Transform the list of arguments ... */
1444 targs = NIL;
1445 foreach(args, fn->args)
1446 {
1447 targs = lappend(targs, transformExprRecurse(pstate,
1448 (Node *) lfirst(args)));
1449 }
1450
1451 /*
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).
1458 */
1459 if (fn->agg_within_group)
1460 {
1461 Assert(fn->agg_order != NIL);
1462 foreach(args, fn->agg_order)
1463 {
1464 SortBy *arg = (SortBy *) lfirst(args);
1465
1466 targs = lappend(targs, transformExpr(pstate, arg->node,
1468 }
1469 }
1470
1471 /* ... and hand off to ParseFuncOrColumn */
1472 return ParseFuncOrColumn(pstate,
1473 fn->funcname,
1474 targs,
1475 last_srf,
1476 fn,
1477 false,
1478 fn->location);
1479}
1480
1481static Node *
1483{
1484 SubLink *sublink;
1485 RowExpr *rexpr;
1486 Query *qtree;
1487 TargetEntry *tle;
1488
1489 /* We should only see this in first-stage processing of UPDATE tlists */
1491
1492 /* We only need to transform the source if this is the first column */
1493 if (maref->colno == 1)
1494 {
1495 /*
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.
1500 */
1501 if (IsA(maref->source, SubLink) &&
1502 ((SubLink *) maref->source)->subLinkType == EXPR_SUBLINK)
1503 {
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);
1510
1511 qtree = castNode(Query, sublink->subselect);
1512
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)));
1519
1520 /*
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.
1526 */
1527 tle = makeTargetEntry((Expr *) sublink, 0, NULL, true);
1529 tle);
1530
1531 /*
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.
1535 */
1536 sublink->subLinkId = list_length(pstate->p_multiassign_exprs);
1537 }
1538 else if (IsA(maref->source, RowExpr))
1539 {
1540 /* Transform the RowExpr, allowing SetToDefault items */
1541 rexpr = (RowExpr *) transformRowExpr(pstate,
1542 (RowExpr *) maref->source,
1543 true);
1544
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)));
1551
1552 /*
1553 * Temporarily append it to p_multiassign_exprs, so we can get it
1554 * back when we come back here for additional columns.
1555 */
1556 tle = makeTargetEntry((Expr *) rexpr, 0, NULL, true);
1558 tle);
1559 }
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))));
1565 }
1566 else
1567 {
1568 /*
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.
1572 */
1573 Assert(pstate->p_multiassign_exprs != NIL);
1574 tle = (TargetEntry *) llast(pstate->p_multiassign_exprs);
1575 }
1576
1577 /*
1578 * Emit the appropriate output expression for the current column
1579 */
1580 if (IsA(tle->expr, SubLink))
1581 {
1582 Param *param;
1583
1584 sublink = (SubLink *) tle->expr;
1586 qtree = castNode(Query, sublink->subselect);
1587
1588 /* Build a Param representing the current subquery output column */
1589 tle = (TargetEntry *) list_nth(qtree->targetList, maref->colno - 1);
1590 Assert(!tle->resjunk);
1591
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);
1599
1600 return (Node *) param;
1601 }
1602
1603 if (IsA(tle->expr, RowExpr))
1604 {
1605 Node *result;
1606
1607 rexpr = (RowExpr *) tle->expr;
1608
1609 /* Just extract and return the next element of the RowExpr */
1610 result = (Node *) list_nth(rexpr->args, maref->colno - 1);
1611
1612 /*
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.
1617 */
1618 if (maref->colno == maref->ncolumns)
1619 pstate->p_multiassign_exprs =
1621
1622 return result;
1623 }
1624
1625 elog(ERROR, "unexpected expr type in multiassign list");
1626 return NULL; /* keep compiler quiet */
1627}
1628
1629static Node *
1631{
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;
1641
1642 /* transform the test expression, if any */
1643 arg = transformExprRecurse(pstate, (Node *) c->arg);
1644
1645 /* generate placeholder for test expression */
1646 if (arg)
1647 {
1648 /*
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.
1654 */
1655 if (exprType(arg) == UNKNOWNOID)
1656 arg = coerce_to_common_type(pstate, arg, TEXTOID, "CASE");
1657
1658 /*
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.
1663 */
1664 assign_expr_collations(pstate, arg);
1665
1666 placeholder = makeNode(CaseTestExpr);
1667 placeholder->typeId = exprType(arg);
1668 placeholder->typeMod = exprTypmod(arg);
1669 placeholder->collation = exprCollation(arg);
1670 }
1671 else
1672 placeholder = NULL;
1673
1674 newc->arg = (Expr *) arg;
1675
1676 /* transform the list of arguments */
1677 newargs = NIL;
1678 resultexprs = NIL;
1679 foreach(l, c->args)
1680 {
1681 CaseWhen *w = lfirst_node(CaseWhen, l);
1682 CaseWhen *neww = makeNode(CaseWhen);
1683 Node *warg;
1684
1685 warg = (Node *) w->expr;
1686 if (placeholder)
1687 {
1688 /* shorthand form was specified, so expand... */
1689 warg = (Node *) makeSimpleA_Expr(AEXPR_OP, "=",
1690 (Node *) placeholder,
1691 warg,
1692 w->location);
1693 }
1694 neww->expr = (Expr *) transformExprRecurse(pstate, warg);
1695
1696 neww->expr = (Expr *) coerce_to_boolean(pstate,
1697 (Node *) neww->expr,
1698 "CASE/WHEN");
1699
1700 warg = (Node *) w->result;
1701 neww->result = (Expr *) transformExprRecurse(pstate, warg);
1702 neww->location = w->location;
1703
1704 newargs = lappend(newargs, neww);
1705 resultexprs = lappend(resultexprs, neww->result);
1706 }
1707
1708 newc->args = newargs;
1709
1710 /* transform the default clause */
1711 defresult = (Node *) c->defresult;
1712 if (defresult == NULL)
1713 {
1714 A_Const *n = makeNode(A_Const);
1715
1716 n->isnull = true;
1717 n->location = -1;
1718 defresult = (Node *) n;
1719 }
1720 newc->defresult = (Expr *) transformExprRecurse(pstate, defresult);
1721
1722 /*
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
1726 */
1727 resultexprs = lcons(newc->defresult, resultexprs);
1728
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 */
1733
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");
1740
1741 /* Convert when-clause results, if necessary */
1742 foreach(l, newc->args)
1743 {
1744 CaseWhen *w = (CaseWhen *) lfirst(l);
1745
1746 w->result = (Expr *)
1747 coerce_to_common_type(pstate,
1748 (Node *) w->result,
1749 ptype,
1750 "CASE/WHEN");
1751 }
1752
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))));
1763
1764 newc->location = c->location;
1765
1766 return (Node *) newc;
1767}
1768
1769static Node *
1771{
1772 Node *result = (Node *) sublink;
1773 Query *qtree;
1774 const char *err;
1775
1776 /*
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.
1780 */
1781 err = NULL;
1782 switch (pstate->p_expr_kind)
1783 {
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:
1794 case EXPR_KIND_WHERE:
1795 case EXPR_KIND_POLICY:
1796 case EXPR_KIND_HAVING:
1797 case EXPR_KIND_FILTER:
1808 case EXPR_KIND_GROUP_BY:
1809 case EXPR_KIND_ORDER_BY:
1811 case EXPR_KIND_LIMIT:
1812 case EXPR_KIND_OFFSET:
1815 case EXPR_KIND_VALUES:
1818 /* okay */
1819 break;
1822 err = _("cannot use subquery in check constraint");
1823 break;
1826 err = _("cannot use subquery in DEFAULT expression");
1827 break;
1829 err = _("cannot use subquery in index expression");
1830 break;
1832 err = _("cannot use subquery in index predicate");
1833 break;
1835 err = _("cannot use subquery in statistics expression");
1836 break;
1838 err = _("cannot use subquery in transform expression");
1839 break;
1841 err = _("cannot use subquery in EXECUTE parameter");
1842 break;
1844 err = _("cannot use subquery in trigger WHEN condition");
1845 break;
1847 err = _("cannot use subquery in partition bound");
1848 break;
1850 err = _("cannot use subquery in partition key expression");
1851 break;
1853 err = _("cannot use subquery in CALL argument");
1854 break;
1856 err = _("cannot use subquery in COPY FROM WHERE condition");
1857 break;
1859 err = _("cannot use subquery in column generation expression");
1860 break;
1861
1862 /*
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.
1868 */
1869 }
1870 if (err)
1871 ereport(ERROR,
1872 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1873 errmsg_internal("%s", err),
1874 parser_errposition(pstate, sublink->location)));
1875
1876 pstate->p_hasSubLinks = true;
1877
1878 /*
1879 * OK, let's transform the sub-SELECT.
1880 */
1881 qtree = parse_sub_analyze(sublink->subselect, pstate, NULL, false, true);
1882
1883 /*
1884 * Check that we got a SELECT. Anything else should be impossible given
1885 * restrictions of the grammar, but check anyway.
1886 */
1887 if (!IsA(qtree, Query) ||
1888 qtree->commandType != CMD_SELECT)
1889 elog(ERROR, "unexpected non-SELECT command in SubLink");
1890
1891 sublink->subselect = (Node *) qtree;
1892
1893 if (sublink->subLinkType == EXISTS_SUBLINK)
1894 {
1895 /*
1896 * EXISTS needs no test expression or combining operator. These fields
1897 * should be null already, but make sure.
1898 */
1899 sublink->testexpr = NULL;
1900 sublink->operName = NIL;
1901 }
1902 else if (sublink->subLinkType == EXPR_SUBLINK ||
1903 sublink->subLinkType == ARRAY_SUBLINK)
1904 {
1905 /*
1906 * Make sure the subselect delivers a single column (ignoring resjunk
1907 * targets).
1908 */
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)));
1914
1915 /*
1916 * EXPR and ARRAY need no test expression or combining operator. These
1917 * fields should be null already, but make sure.
1918 */
1919 sublink->testexpr = NULL;
1920 sublink->operName = NIL;
1921 }
1922 else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
1923 {
1924 /* Same as EXPR case, except no restriction on number of columns */
1925 sublink->testexpr = NULL;
1926 sublink->operName = NIL;
1927 }
1928 else
1929 {
1930 /* ALL, ANY, or ROWCOMPARE: generate row-comparing expression */
1931 Node *lefthand;
1932 List *left_list;
1933 List *right_list;
1934 ListCell *l;
1935
1936 /*
1937 * If the source was "x IN (select)", convert to "x = ANY (select)".
1938 */
1939 if (sublink->operName == NIL)
1940 sublink->operName = list_make1(makeString("="));
1941
1942 /*
1943 * Transform lefthand expression, and convert to a list
1944 */
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);
1950
1951 /*
1952 * Build a list of PARAM_SUBLINK nodes representing the output columns
1953 * of the subquery.
1954 */
1955 right_list = NIL;
1956 foreach(l, qtree->targetList)
1957 {
1958 TargetEntry *tent = (TargetEntry *) lfirst(l);
1959 Param *param;
1960
1961 if (tent->resjunk)
1962 continue;
1963
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;
1971
1972 right_list = lappend(right_list, param);
1973 }
1974
1975 /*
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.
1979 */
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)));
1990
1991 /*
1992 * Identify the combining operator(s) and generate a suitable
1993 * row-comparison expression.
1994 */
1995 sublink->testexpr = make_row_comparison_op(pstate,
1996 sublink->operName,
1997 left_list,
1998 right_list,
1999 sublink->location);
2000 }
2001
2002 return result;
2003}
2004
2005/*
2006 * transformArrayExpr
2007 *
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().
2011 */
2012static Node *
2014 Oid array_type, Oid element_type, int32 typmod)
2015{
2016 ArrayExpr *newa = makeNode(ArrayExpr);
2017 List *newelems = NIL;
2018 List *newcoercedelems = NIL;
2021 bool coerce_hard;
2022
2023 /*
2024 * Transform the element expressions
2025 *
2026 * Assume that the array is one-dimensional unless we find an array-type
2027 * element expression.
2028 */
2029 newa->multidims = false;
2030 foreach(element, a->elements)
2031 {
2032 Node *e = (Node *) lfirst(element);
2033 Node *newe;
2034
2035 /*
2036 * If an element is itself an A_ArrayExpr, recurse directly so that we
2037 * can pass down any target type we were given.
2038 */
2039 if (IsA(e, A_ArrayExpr))
2040 {
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;
2049 }
2050 else
2051 {
2052 newe = transformExprRecurse(pstate, e);
2053
2054 /*
2055 * Check for sub-array expressions, if we haven't already found
2056 * one. Note we don't accept domain-over-array as a sub-array,
2057 * nor int2vector nor oidvector; those have constraints that don't
2058 * map well to being treated as a sub-array.
2059 */
2060 if (!newa->multidims)
2061 {
2062 Oid newetype = exprType(newe);
2063
2064 if (newetype != INT2VECTOROID && newetype != OIDVECTOROID &&
2065 type_is_array(newetype))
2066 newa->multidims = true;
2067 }
2068 }
2069
2070 newelems = lappend(newelems, newe);
2071 }
2072
2073 /*
2074 * Select a target type for the elements.
2075 *
2076 * If we haven't been given a target array type, we must try to deduce a
2077 * common type based on the types of the individual elements present.
2078 */
2079 if (OidIsValid(array_type))
2080 {
2081 /* Caller must ensure array_type matches element_type */
2082 Assert(OidIsValid(element_type));
2083 coerce_type = (newa->multidims ? array_type : element_type);
2084 coerce_hard = true;
2085 }
2086 else
2087 {
2088 /* Can't handle an empty array without a target type */
2089 if (newelems == NIL)
2090 ereport(ERROR,
2091 (errcode(ERRCODE_INDETERMINATE_DATATYPE),
2092 errmsg("cannot determine type of empty array"),
2093 errhint("Explicitly cast to the desired type, "
2094 "for example ARRAY[]::integer[]."),
2095 parser_errposition(pstate, a->location)));
2096
2097 /* Select a common type for the elements */
2098 coerce_type = select_common_type(pstate, newelems, "ARRAY", NULL);
2099
2100 if (newa->multidims)
2101 {
2102 array_type = coerce_type;
2103 element_type = get_element_type(array_type);
2104 if (!OidIsValid(element_type))
2105 ereport(ERROR,
2106 (errcode(ERRCODE_UNDEFINED_OBJECT),
2107 errmsg("could not find element type for data type %s",
2108 format_type_be(array_type)),
2109 parser_errposition(pstate, a->location)));
2110 }
2111 else
2112 {
2113 element_type = coerce_type;
2114 array_type = get_array_type(element_type);
2115 if (!OidIsValid(array_type))
2116 ereport(ERROR,
2117 (errcode(ERRCODE_UNDEFINED_OBJECT),
2118 errmsg("could not find array type for data type %s",
2119 format_type_be(element_type)),
2120 parser_errposition(pstate, a->location)));
2121 }
2122 coerce_hard = false;
2123 }
2124
2125 /*
2126 * Coerce elements to target type
2127 *
2128 * If the array has been explicitly cast, then the elements are in turn
2129 * explicitly coerced.
2130 *
2131 * If the array's type was merely derived from the common type of its
2132 * elements, then the elements are implicitly coerced to the common type.
2133 * This is consistent with other uses of select_common_type().
2134 */
2135 foreach(element, newelems)
2136 {
2137 Node *e = (Node *) lfirst(element);
2138 Node *newe;
2139
2140 if (coerce_hard)
2141 {
2142 newe = coerce_to_target_type(pstate, e,
2143 exprType(e),
2145 typmod,
2148 -1);
2149 if (newe == NULL)
2150 ereport(ERROR,
2151 (errcode(ERRCODE_CANNOT_COERCE),
2152 errmsg("cannot cast type %s to %s",
2155 parser_errposition(pstate, exprLocation(e))));
2156 }
2157 else
2158 newe = coerce_to_common_type(pstate, e,
2160 "ARRAY");
2161 newcoercedelems = lappend(newcoercedelems, newe);
2162 }
2163
2164 newa->array_typeid = array_type;
2165 /* array_collid will be set by parse_collate.c */
2166 newa->element_typeid = element_type;
2167 newa->elements = newcoercedelems;
2168 newa->location = a->location;
2169
2170 return (Node *) newa;
2171}
2172
2173static Node *
2174transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
2175{
2176 RowExpr *newr;
2177 char fname[16];
2178 int fnum;
2179
2180 newr = makeNode(RowExpr);
2181
2182 /* Transform the field expressions */
2183 newr->args = transformExpressionList(pstate, r->args,
2184 pstate->p_expr_kind, allowDefault);
2185
2186 /* Disallow more columns than will fit in a tuple */
2188 ereport(ERROR,
2189 (errcode(ERRCODE_TOO_MANY_COLUMNS),
2190 errmsg("ROW expressions can have at most %d entries",
2192 parser_errposition(pstate, r->location)));
2193
2194 /* Barring later casting, we consider the type RECORD */
2195 newr->row_typeid = RECORDOID;
2196 newr->row_format = COERCE_IMPLICIT_CAST;
2197
2198 /* ROW() has anonymous columns, so invent some field names */
2199 newr->colnames = NIL;
2200 for (fnum = 1; fnum <= list_length(newr->args); fnum++)
2201 {
2202 snprintf(fname, sizeof(fname), "f%d", fnum);
2203 newr->colnames = lappend(newr->colnames, makeString(pstrdup(fname)));
2204 }
2205
2206 newr->location = r->location;
2207
2208 return (Node *) newr;
2209}
2210
2211static Node *
2213{
2215 Node *last_srf = pstate->p_last_srf;
2216 List *newargs = NIL;
2217 List *newcoercedargs = NIL;
2218 ListCell *args;
2219
2220 foreach(args, c->args)
2221 {
2222 Node *e = (Node *) lfirst(args);
2223 Node *newe;
2224
2225 newe = transformExprRecurse(pstate, e);
2226 newargs = lappend(newargs, newe);
2227 }
2228
2229 newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
2230 /* coalescecollid will be set by parse_collate.c */
2231
2232 /* Convert arguments if necessary */
2233 foreach(args, newargs)
2234 {
2235 Node *e = (Node *) lfirst(args);
2236 Node *newe;
2237
2238 newe = coerce_to_common_type(pstate, e,
2239 newc->coalescetype,
2240 "COALESCE");
2241 newcoercedargs = lappend(newcoercedargs, newe);
2242 }
2243
2244 /* if any subexpression contained a SRF, complain */
2245 if (pstate->p_last_srf != last_srf)
2246 ereport(ERROR,
2247 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2248 /* translator: %s is name of a SQL construct, eg GROUP BY */
2249 errmsg("set-returning functions are not allowed in %s",
2250 "COALESCE"),
2251 errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
2252 parser_errposition(pstate,
2253 exprLocation(pstate->p_last_srf))));
2254
2255 newc->args = newcoercedargs;
2256 newc->location = c->location;
2257 return (Node *) newc;
2258}
2259
2260static Node *
2262{
2264 List *newargs = NIL;
2265 List *newcoercedargs = NIL;
2266 const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
2267 ListCell *args;
2268
2269 newm->op = m->op;
2270 foreach(args, m->args)
2271 {
2272 Node *e = (Node *) lfirst(args);
2273 Node *newe;
2274
2275 newe = transformExprRecurse(pstate, e);
2276 newargs = lappend(newargs, newe);
2277 }
2278
2279 newm->minmaxtype = select_common_type(pstate, newargs, funcname, NULL);
2280 /* minmaxcollid and inputcollid will be set by parse_collate.c */
2281
2282 /* Convert arguments if necessary */
2283 foreach(args, newargs)
2284 {
2285 Node *e = (Node *) lfirst(args);
2286 Node *newe;
2287
2288 newe = coerce_to_common_type(pstate, e,
2289 newm->minmaxtype,
2290 funcname);
2291 newcoercedargs = lappend(newcoercedargs, newe);
2292 }
2293
2294 newm->args = newcoercedargs;
2295 newm->location = m->location;
2296 return (Node *) newm;
2297}
2298
2299static Node *
2301{
2302 /*
2303 * All we need to do is insert the correct result type and (where needed)
2304 * validate the typmod, so we just modify the node in-place.
2305 */
2306 switch (svf->op)
2307 {
2308 case SVFOP_CURRENT_DATE:
2309 svf->type = DATEOID;
2310 break;
2311 case SVFOP_CURRENT_TIME:
2312 svf->type = TIMETZOID;
2313 break;
2315 svf->type = TIMETZOID;
2316 svf->typmod = anytime_typmod_check(true, svf->typmod);
2317 break;
2319 svf->type = TIMESTAMPTZOID;
2320 break;
2322 svf->type = TIMESTAMPTZOID;
2323 svf->typmod = anytimestamp_typmod_check(true, svf->typmod);
2324 break;
2325 case SVFOP_LOCALTIME:
2326 svf->type = TIMEOID;
2327 break;
2328 case SVFOP_LOCALTIME_N:
2329 svf->type = TIMEOID;
2330 svf->typmod = anytime_typmod_check(false, svf->typmod);
2331 break;
2333 svf->type = TIMESTAMPOID;
2334 break;
2336 svf->type = TIMESTAMPOID;
2337 svf->typmod = anytimestamp_typmod_check(false, svf->typmod);
2338 break;
2339 case SVFOP_CURRENT_ROLE:
2340 case SVFOP_CURRENT_USER:
2341 case SVFOP_USER:
2342 case SVFOP_SESSION_USER:
2345 svf->type = NAMEOID;
2346 break;
2347 }
2348
2349 return (Node *) svf;
2350}
2351
2352static Node *
2354{
2355 XmlExpr *newx;
2356 ListCell *lc;
2357 int i;
2358
2359 newx = makeNode(XmlExpr);
2360 newx->op = x->op;
2361 if (x->name)
2362 newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
2363 else
2364 newx->name = NULL;
2365 newx->xmloption = x->xmloption;
2366 newx->type = XMLOID; /* this just marks the node as transformed */
2367 newx->typmod = -1;
2368 newx->location = x->location;
2369
2370 /*
2371 * gram.y built the named args as a list of ResTarget. Transform each,
2372 * and break the names out as a separate list.
2373 */
2374 newx->named_args = NIL;
2375 newx->arg_names = NIL;
2376
2377 foreach(lc, x->named_args)
2378 {
2380 Node *expr;
2381 char *argname;
2382
2383 expr = transformExprRecurse(pstate, r->val);
2384
2385 if (r->name)
2386 argname = map_sql_identifier_to_xml_name(r->name, false, false);
2387 else if (IsA(r->val, ColumnRef))
2389 true, false);
2390 else
2391 {
2392 ereport(ERROR,
2393 (errcode(ERRCODE_SYNTAX_ERROR),
2394 x->op == IS_XMLELEMENT
2395 ? errmsg("unnamed XML attribute value must be a column reference")
2396 : errmsg("unnamed XML element value must be a column reference"),
2397 parser_errposition(pstate, r->location)));
2398 argname = NULL; /* keep compiler quiet */
2399 }
2400
2401 /* reject duplicate argnames in XMLELEMENT only */
2402 if (x->op == IS_XMLELEMENT)
2403 {
2404 ListCell *lc2;
2405
2406 foreach(lc2, newx->arg_names)
2407 {
2408 if (strcmp(argname, strVal(lfirst(lc2))) == 0)
2409 ereport(ERROR,
2410 (errcode(ERRCODE_SYNTAX_ERROR),
2411 errmsg("XML attribute name \"%s\" appears more than once",
2412 argname),
2413 parser_errposition(pstate, r->location)));
2414 }
2415 }
2416
2417 newx->named_args = lappend(newx->named_args, expr);
2418 newx->arg_names = lappend(newx->arg_names, makeString(argname));
2419 }
2420
2421 /* The other arguments are of varying types depending on the function */
2422 newx->args = NIL;
2423 i = 0;
2424 foreach(lc, x->args)
2425 {
2426 Node *e = (Node *) lfirst(lc);
2427 Node *newe;
2428
2429 newe = transformExprRecurse(pstate, e);
2430 switch (x->op)
2431 {
2432 case IS_XMLCONCAT:
2433 newe = coerce_to_specific_type(pstate, newe, XMLOID,
2434 "XMLCONCAT");
2435 break;
2436 case IS_XMLELEMENT:
2437 /* no coercion necessary */
2438 break;
2439 case IS_XMLFOREST:
2440 newe = coerce_to_specific_type(pstate, newe, XMLOID,
2441 "XMLFOREST");
2442 break;
2443 case IS_XMLPARSE:
2444 if (i == 0)
2445 newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2446 "XMLPARSE");
2447 else
2448 newe = coerce_to_boolean(pstate, newe, "XMLPARSE");
2449 break;
2450 case IS_XMLPI:
2451 newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2452 "XMLPI");
2453 break;
2454 case IS_XMLROOT:
2455 if (i == 0)
2456 newe = coerce_to_specific_type(pstate, newe, XMLOID,
2457 "XMLROOT");
2458 else if (i == 1)
2459 newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2460 "XMLROOT");
2461 else
2462 newe = coerce_to_specific_type(pstate, newe, INT4OID,
2463 "XMLROOT");
2464 break;
2465 case IS_XMLSERIALIZE:
2466 /* not handled here */
2467 Assert(false);
2468 break;
2469 case IS_DOCUMENT:
2470 newe = coerce_to_specific_type(pstate, newe, XMLOID,
2471 "IS DOCUMENT");
2472 break;
2473 }
2474 newx->args = lappend(newx->args, newe);
2475 i++;
2476 }
2477
2478 return (Node *) newx;
2479}
2480
2481static Node *
2483{
2484 Node *result;
2485 XmlExpr *xexpr;
2486 Oid targetType;
2487 int32 targetTypmod;
2488
2489 xexpr = makeNode(XmlExpr);
2490 xexpr->op = IS_XMLSERIALIZE;
2491 xexpr->args = list_make1(coerce_to_specific_type(pstate,
2492 transformExprRecurse(pstate, xs->expr),
2493 XMLOID,
2494 "XMLSERIALIZE"));
2495
2496 typenameTypeIdAndMod(pstate, xs->typeName, &targetType, &targetTypmod);
2497
2498 xexpr->xmloption = xs->xmloption;
2499 xexpr->indent = xs->indent;
2500 xexpr->location = xs->location;
2501 /* We actually only need these to be able to parse back the expression. */
2502 xexpr->type = targetType;
2503 xexpr->typmod = targetTypmod;
2504
2505 /*
2506 * The actual target type is determined this way. SQL allows char and
2507 * varchar as target types. We allow anything that can be cast implicitly
2508 * from text. This way, user-defined text-like data types automatically
2509 * fit in.
2510 */
2511 result = coerce_to_target_type(pstate, (Node *) xexpr,
2512 TEXTOID, targetType, targetTypmod,
2515 -1);
2516 if (result == NULL)
2517 ereport(ERROR,
2518 (errcode(ERRCODE_CANNOT_COERCE),
2519 errmsg("cannot cast XMLSERIALIZE result to %s",
2520 format_type_be(targetType)),
2521 parser_errposition(pstate, xexpr->location)));
2522 return result;
2523}
2524
2525static Node *
2527{
2528 const char *clausename;
2529
2530 switch (b->booltesttype)
2531 {
2532 case IS_TRUE:
2533 clausename = "IS TRUE";
2534 break;
2535 case IS_NOT_TRUE:
2536 clausename = "IS NOT TRUE";
2537 break;
2538 case IS_FALSE:
2539 clausename = "IS FALSE";
2540 break;
2541 case IS_NOT_FALSE:
2542 clausename = "IS NOT FALSE";
2543 break;
2544 case IS_UNKNOWN:
2545 clausename = "IS UNKNOWN";
2546 break;
2547 case IS_NOT_UNKNOWN:
2548 clausename = "IS NOT UNKNOWN";
2549 break;
2550 default:
2551 elog(ERROR, "unrecognized booltesttype: %d",
2552 (int) b->booltesttype);
2553 clausename = NULL; /* keep compiler quiet */
2554 }
2555
2556 b->arg = (Expr *) transformExprRecurse(pstate, (Node *) b->arg);
2557
2558 b->arg = (Expr *) coerce_to_boolean(pstate,
2559 (Node *) b->arg,
2560 clausename);
2561
2562 return (Node *) b;
2563}
2564
2565static Node *
2567{
2568 /* CURRENT OF can only appear at top level of UPDATE/DELETE */
2569 Assert(pstate->p_target_nsitem != NULL);
2570 cexpr->cvarno = pstate->p_target_nsitem->p_rtindex;
2571
2572 /*
2573 * Check to see if the cursor name matches a parameter of type REFCURSOR.
2574 * If so, replace the raw name reference with a parameter reference. (This
2575 * is a hack for the convenience of plpgsql.)
2576 */
2577 if (cexpr->cursor_name != NULL) /* in case already transformed */
2578 {
2579 ColumnRef *cref = makeNode(ColumnRef);
2580 Node *node = NULL;
2581
2582 /* Build an unqualified ColumnRef with the given name */
2583 cref->fields = list_make1(makeString(cexpr->cursor_name));
2584 cref->location = -1;
2585
2586 /* See if there is a translation available from a parser hook */
2587 if (pstate->p_pre_columnref_hook != NULL)
2588 node = pstate->p_pre_columnref_hook(pstate, cref);
2589 if (node == NULL && pstate->p_post_columnref_hook != NULL)
2590 node = pstate->p_post_columnref_hook(pstate, cref, NULL);
2591
2592 /*
2593 * XXX Should we throw an error if we get a translation that isn't a
2594 * refcursor Param? For now it seems best to silently ignore false
2595 * matches.
2596 */
2597 if (node != NULL && IsA(node, Param))
2598 {
2599 Param *p = (Param *) node;
2600
2601 if (p->paramkind == PARAM_EXTERN &&
2602 p->paramtype == REFCURSOROID)
2603 {
2604 /* Matches, so convert CURRENT OF to a param reference */
2605 cexpr->cursor_name = NULL;
2606 cexpr->cursor_param = p->paramid;
2607 }
2608 }
2609 }
2610
2611 return (Node *) cexpr;
2612}
2613
2614/*
2615 * Construct a whole-row reference to represent the notation "relation.*".
2616 */
2617static Node *
2619 int sublevels_up, int location)
2620{
2621 /*
2622 * Build the appropriate referencing node. Normally this can be a
2623 * whole-row Var, but if the nsitem is a JOIN USING alias then it contains
2624 * only a subset of the columns of the underlying join RTE, so that will
2625 * not work. Instead we immediately expand the reference into a RowExpr.
2626 * Since the JOIN USING's common columns are fully determined at this
2627 * point, there seems no harm in expanding it now rather than during
2628 * planning.
2629 *
2630 * Note that if the nsitem is an OLD/NEW alias for the target RTE (as can
2631 * appear in a RETURNING list), its alias won't match the target RTE's
2632 * alias, but we still want to make a whole-row Var here rather than a
2633 * RowExpr, for consistency with direct references to the target RTE, and
2634 * so that any dropped columns are handled correctly. Thus we also check
2635 * p_returning_type here.
2636 *
2637 * Note that if the RTE is a function returning scalar, we create just a
2638 * plain reference to the function value, not a composite containing a
2639 * single column. This is pretty inconsistent at first sight, but it's
2640 * what we've done historically. One argument for it is that "rel" and
2641 * "rel.*" mean the same thing for composite relations, so why not for
2642 * scalar functions...
2643 */
2644 if (nsitem->p_names == nsitem->p_rte->eref ||
2646 {
2647 Var *result;
2648
2649 result = makeWholeRowVar(nsitem->p_rte, nsitem->p_rtindex,
2650 sublevels_up, true);
2651
2652 /* mark Var for RETURNING OLD/NEW, as necessary */
2653 result->varreturningtype = nsitem->p_returning_type;
2654
2655 /* location is not filled in by makeWholeRowVar */
2656 result->location = location;
2657
2658 /* mark Var if it's nulled by any outer joins */
2659 markNullableIfNeeded(pstate, result);
2660
2661 /* mark relation as requiring whole-row SELECT access */
2662 markVarForSelectPriv(pstate, result);
2663
2664 return (Node *) result;
2665 }
2666 else
2667 {
2668 RowExpr *rowexpr;
2669 List *fields;
2670
2671 /*
2672 * We want only as many columns as are listed in p_names->colnames,
2673 * and we should use those names not whatever possibly-aliased names
2674 * are in the RTE. We needn't worry about marking the RTE for SELECT
2675 * access, as the common columns are surely so marked already.
2676 */
2677 expandRTE(nsitem->p_rte, nsitem->p_rtindex, sublevels_up,
2678 nsitem->p_returning_type, location, false, NULL, &fields);
2679 rowexpr = makeNode(RowExpr);
2680 rowexpr->args = list_truncate(fields,
2681 list_length(nsitem->p_names->colnames));
2682 rowexpr->row_typeid = RECORDOID;
2683 rowexpr->row_format = COERCE_IMPLICIT_CAST;
2684 rowexpr->colnames = copyObject(nsitem->p_names->colnames);
2685 rowexpr->location = location;
2686
2687 /* XXX we ought to mark the row as possibly nullable */
2688
2689 return (Node *) rowexpr;
2690 }
2691}
2692
2693/*
2694 * Handle an explicit CAST construct.
2695 *
2696 * Transform the argument, look up the type name, and apply any necessary
2697 * coercion function(s).
2698 */
2699static Node *
2701{
2702 Node *result;
2703 Node *arg = tc->arg;
2704 Node *expr;
2705 Oid inputType;
2706 Oid targetType;
2707 int32 targetTypmod;
2708 int location;
2709
2710 /* Look up the type name first */
2711 typenameTypeIdAndMod(pstate, tc->typeName, &targetType, &targetTypmod);
2712
2713 /*
2714 * If the subject of the typecast is an ARRAY[] construct and the target
2715 * type is an array type, we invoke transformArrayExpr() directly so that
2716 * we can pass down the type information. This avoids some cases where
2717 * transformArrayExpr() might not infer the correct type. Otherwise, just
2718 * transform the argument normally.
2719 */
2720 if (IsA(arg, A_ArrayExpr))
2721 {
2722 Oid targetBaseType;
2723 int32 targetBaseTypmod;
2724 Oid elementType;
2725
2726 /*
2727 * If target is a domain over array, work with the base array type
2728 * here. Below, we'll cast the array type to the domain. In the
2729 * usual case that the target is not a domain, the remaining steps
2730 * will be a no-op.
2731 */
2732 targetBaseTypmod = targetTypmod;
2733 targetBaseType = getBaseTypeAndTypmod(targetType, &targetBaseTypmod);
2734 elementType = get_element_type(targetBaseType);
2735 if (OidIsValid(elementType))
2736 {
2737 expr = transformArrayExpr(pstate,
2738 (A_ArrayExpr *) arg,
2739 targetBaseType,
2740 elementType,
2741 targetBaseTypmod);
2742 }
2743 else
2744 expr = transformExprRecurse(pstate, arg);
2745 }
2746 else
2747 expr = transformExprRecurse(pstate, arg);
2748
2749 inputType = exprType(expr);
2750 if (inputType == InvalidOid)
2751 return expr; /* do nothing if NULL input */
2752
2753 /*
2754 * Location of the coercion is preferentially the location of the :: or
2755 * CAST symbol, but if there is none then use the location of the type
2756 * name (this can happen in TypeName 'string' syntax, for instance).
2757 */
2758 location = tc->location;
2759 if (location < 0)
2760 location = tc->typeName->location;
2761
2762 result = coerce_to_target_type(pstate, expr, inputType,
2763 targetType, targetTypmod,
2766 location);
2767 if (result == NULL)
2768 ereport(ERROR,
2769 (errcode(ERRCODE_CANNOT_COERCE),
2770 errmsg("cannot cast type %s to %s",
2771 format_type_be(inputType),
2772 format_type_be(targetType)),
2773 parser_coercion_errposition(pstate, location, expr)));
2774
2775 return result;
2776}
2777
2778/*
2779 * Handle an explicit COLLATE clause.
2780 *
2781 * Transform the argument, and look up the collation name.
2782 */
2783static Node *
2785{
2786 CollateExpr *newc;
2787 Oid argtype;
2788
2789 newc = makeNode(CollateExpr);
2790 newc->arg = (Expr *) transformExprRecurse(pstate, c->arg);
2791
2792 argtype = exprType((Node *) newc->arg);
2793
2794 /*
2795 * The unknown type is not collatable, but coerce_type() takes care of it
2796 * separately, so we'll let it go here.
2797 */
2798 if (!type_is_collatable(argtype) && argtype != UNKNOWNOID)
2799 ereport(ERROR,
2800 (errcode(ERRCODE_DATATYPE_MISMATCH),
2801 errmsg("collations are not supported by type %s",
2802 format_type_be(argtype)),
2803 parser_errposition(pstate, c->location)));
2804
2805 newc->collOid = LookupCollation(pstate, c->collname, c->location);
2806 newc->location = c->location;
2807
2808 return (Node *) newc;
2809}
2810
2811/*
2812 * Transform a "row compare-op row" construct
2813 *
2814 * The inputs are lists of already-transformed expressions.
2815 * As with coerce_type, pstate may be NULL if no special unknown-Param
2816 * processing is wanted.
2817 *
2818 * The output may be a single OpExpr, an AND or OR combination of OpExprs,
2819 * or a RowCompareExpr. In all cases it is guaranteed to return boolean.
2820 * The AND, OR, and RowCompareExpr cases further imply things about the
2821 * behavior of the operators (ie, they behave as =, <>, or < <= > >=).
2822 */
2823static Node *
2825 List *largs, List *rargs, int location)
2826{
2827 RowCompareExpr *rcexpr;
2828 CompareType cmptype;
2829 List *opexprs;
2830 List *opnos;
2831 List *opfamilies;
2832 ListCell *l,
2833 *r;
2834 List **opinfo_lists;
2835 Bitmapset *cmptypes;
2836 int nopers;
2837 int i;
2838
2839 nopers = list_length(largs);
2840 if (nopers != list_length(rargs))
2841 ereport(ERROR,
2842 (errcode(ERRCODE_SYNTAX_ERROR),
2843 errmsg("unequal number of entries in row expressions"),
2844 parser_errposition(pstate, location)));
2845
2846 /*
2847 * We can't compare zero-length rows because there is no principled basis
2848 * for figuring out what the operator is.
2849 */
2850 if (nopers == 0)
2851 ereport(ERROR,
2852 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2853 errmsg("cannot compare rows of zero length"),
2854 parser_errposition(pstate, location)));
2855
2856 /*
2857 * Identify all the pairwise operators, using make_op so that behavior is
2858 * the same as in the simple scalar case.
2859 */
2860 opexprs = NIL;
2861 forboth(l, largs, r, rargs)
2862 {
2863 Node *larg = (Node *) lfirst(l);
2864 Node *rarg = (Node *) lfirst(r);
2865 OpExpr *cmp;
2866
2867 cmp = castNode(OpExpr, make_op(pstate, opname, larg, rarg,
2868 pstate->p_last_srf, location));
2869
2870 /*
2871 * We don't use coerce_to_boolean here because we insist on the
2872 * operator yielding boolean directly, not via coercion. If it
2873 * doesn't yield bool it won't be in any index opfamilies...
2874 */
2875 if (cmp->opresulttype != BOOLOID)
2876 ereport(ERROR,
2877 (errcode(ERRCODE_DATATYPE_MISMATCH),
2878 errmsg("row comparison operator must yield type boolean, "
2879 "not type %s",
2880 format_type_be(cmp->opresulttype)),
2881 parser_errposition(pstate, location)));
2883 ereport(ERROR,
2884 (errcode(ERRCODE_DATATYPE_MISMATCH),
2885 errmsg("row comparison operator must not return a set"),
2886 parser_errposition(pstate, location)));
2887 opexprs = lappend(opexprs, cmp);
2888 }
2889
2890 /*
2891 * If rows are length 1, just return the single operator. In this case we
2892 * don't insist on identifying btree semantics for the operator (but we
2893 * still require it to return boolean).
2894 */
2895 if (nopers == 1)
2896 return (Node *) linitial(opexprs);
2897
2898 /*
2899 * Now we must determine which row comparison semantics (= <> < <= > >=)
2900 * apply to this set of operators. We look for opfamilies containing the
2901 * operators, and see which interpretations (cmptypes) exist for each
2902 * operator.
2903 */
2904 opinfo_lists = (List **) palloc(nopers * sizeof(List *));
2905 cmptypes = NULL;
2906 i = 0;
2907 foreach(l, opexprs)
2908 {
2909 Oid opno = ((OpExpr *) lfirst(l))->opno;
2910 Bitmapset *this_cmptypes;
2911 ListCell *j;
2912
2913 opinfo_lists[i] = get_op_index_interpretation(opno);
2914
2915 /*
2916 * convert comparison types into a Bitmapset to make the intersection
2917 * calculation easy.
2918 */
2919 this_cmptypes = NULL;
2920 foreach(j, opinfo_lists[i])
2921 {
2922 OpIndexInterpretation *opinfo = lfirst(j);
2923
2924 this_cmptypes = bms_add_member(this_cmptypes, opinfo->cmptype);
2925 }
2926 if (i == 0)
2927 cmptypes = this_cmptypes;
2928 else
2929 cmptypes = bms_int_members(cmptypes, this_cmptypes);
2930 i++;
2931 }
2932
2933 /*
2934 * If there are multiple common interpretations, we may use any one of
2935 * them ... this coding arbitrarily picks the lowest comparison type
2936 * number.
2937 */
2938 i = bms_next_member(cmptypes, -1);
2939 if (i < 0)
2940 {
2941 /* No common interpretation, so fail */
2942 ereport(ERROR,
2943 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2944 errmsg("could not determine interpretation of row comparison operator %s",
2945 strVal(llast(opname))),
2946 errhint("Row comparison operators must be associated with btree operator families."),
2947 parser_errposition(pstate, location)));
2948 }
2949 cmptype = (CompareType) i;
2950
2951 /*
2952 * For = and <> cases, we just combine the pairwise operators with AND or
2953 * OR respectively.
2954 */
2955 if (cmptype == COMPARE_EQ)
2956 return (Node *) makeBoolExpr(AND_EXPR, opexprs, location);
2957 if (cmptype == COMPARE_NE)
2958 return (Node *) makeBoolExpr(OR_EXPR, opexprs, location);
2959
2960 /*
2961 * Otherwise we need to choose exactly which opfamily to associate with
2962 * each operator.
2963 */
2964 opfamilies = NIL;
2965 for (i = 0; i < nopers; i++)
2966 {
2967 Oid opfamily = InvalidOid;
2968 ListCell *j;
2969
2970 foreach(j, opinfo_lists[i])
2971 {
2972 OpIndexInterpretation *opinfo = lfirst(j);
2973
2974 if (opinfo->cmptype == cmptype)
2975 {
2976 opfamily = opinfo->opfamily_id;
2977 break;
2978 }
2979 }
2980 if (OidIsValid(opfamily))
2981 opfamilies = lappend_oid(opfamilies, opfamily);
2982 else /* should not happen */
2983 ereport(ERROR,
2984 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2985 errmsg("could not determine interpretation of row comparison operator %s",
2986 strVal(llast(opname))),
2987 errdetail("There are multiple equally-plausible candidates."),
2988 parser_errposition(pstate, location)));
2989 }
2990
2991 /*
2992 * Now deconstruct the OpExprs and create a RowCompareExpr.
2993 *
2994 * Note: can't just reuse the passed largs/rargs lists, because of
2995 * possibility that make_op inserted coercion operations.
2996 */
2997 opnos = NIL;
2998 largs = NIL;
2999 rargs = NIL;
3000 foreach(l, opexprs)
3001 {
3002 OpExpr *cmp = (OpExpr *) lfirst(l);
3003
3004 opnos = lappend_oid(opnos, cmp->opno);
3005 largs = lappend(largs, linitial(cmp->args));
3006 rargs = lappend(rargs, lsecond(cmp->args));
3007 }
3008
3009 rcexpr = makeNode(RowCompareExpr);
3010 rcexpr->cmptype = cmptype;
3011 rcexpr->opnos = opnos;
3012 rcexpr->opfamilies = opfamilies;
3013 rcexpr->inputcollids = NIL; /* assign_expr_collations will fix this */
3014 rcexpr->largs = largs;
3015 rcexpr->rargs = rargs;
3016
3017 return (Node *) rcexpr;
3018}
3019
3020/*
3021 * Transform a "row IS DISTINCT FROM row" construct
3022 *
3023 * The input RowExprs are already transformed
3024 */
3025static Node *
3027 RowExpr *lrow, RowExpr *rrow,
3028 int location)
3029{
3030 Node *result = NULL;
3031 List *largs = lrow->args;
3032 List *rargs = rrow->args;
3033 ListCell *l,
3034 *r;
3035
3036 if (list_length(largs) != list_length(rargs))
3037 ereport(ERROR,
3038 (errcode(ERRCODE_SYNTAX_ERROR),
3039 errmsg("unequal number of entries in row expressions"),
3040 parser_errposition(pstate, location)));
3041
3042 forboth(l, largs, r, rargs)
3043 {
3044 Node *larg = (Node *) lfirst(l);
3045 Node *rarg = (Node *) lfirst(r);
3046 Node *cmp;
3047
3048 cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg, location);
3049 if (result == NULL)
3050 result = cmp;
3051 else
3052 result = (Node *) makeBoolExpr(OR_EXPR,
3053 list_make2(result, cmp),
3054 location);
3055 }
3056
3057 if (result == NULL)
3058 {
3059 /* zero-length rows? Generate constant FALSE */
3060 result = makeBoolConst(false, false);
3061 }
3062
3063 return result;
3064}
3065
3066/*
3067 * make the node for an IS DISTINCT FROM operator
3068 */
3069static Expr *
3070make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree,
3071 int location)
3072{
3073 Expr *result;
3074
3075 result = make_op(pstate, opname, ltree, rtree,
3076 pstate->p_last_srf, location);
3077 if (((OpExpr *) result)->opresulttype != BOOLOID)
3078 ereport(ERROR,
3079 (errcode(ERRCODE_DATATYPE_MISMATCH),
3080 /* translator: %s is name of a SQL construct, eg NULLIF */
3081 errmsg("%s requires = operator to yield boolean",
3082 "IS DISTINCT FROM"),
3083 parser_errposition(pstate, location)));
3084 if (((OpExpr *) result)->opretset)
3085 ereport(ERROR,
3086 (errcode(ERRCODE_DATATYPE_MISMATCH),
3087 /* translator: %s is name of a SQL construct, eg NULLIF */
3088 errmsg("%s must not return a set", "IS DISTINCT FROM"),
3089 parser_errposition(pstate, location)));
3090
3091 /*
3092 * We rely on DistinctExpr and OpExpr being same struct
3093 */
3094 NodeSetTag(result, T_DistinctExpr);
3095
3096 return result;
3097}
3098
3099/*
3100 * Produce a NullTest node from an IS [NOT] DISTINCT FROM NULL construct
3101 *
3102 * "arg" is the untransformed other argument
3103 */
3104static Node *
3106{
3107 NullTest *nt = makeNode(NullTest);
3108
3109 nt->arg = (Expr *) transformExprRecurse(pstate, arg);
3110 /* the argument can be any type, so don't coerce it */
3111 if (distincta->kind == AEXPR_NOT_DISTINCT)
3112 nt->nulltesttype = IS_NULL;
3113 else
3115 /* argisrow = false is correct whether or not arg is composite */
3116 nt->argisrow = false;
3117 nt->location = distincta->location;
3118 return (Node *) nt;
3119}
3120
3121/*
3122 * Produce a string identifying an expression by kind.
3123 *
3124 * Note: when practical, use a simple SQL keyword for the result. If that
3125 * doesn't work well, check call sites to see whether custom error message
3126 * strings are required.
3127 */
3128const char *
3130{
3131 switch (exprKind)
3132 {
3133 case EXPR_KIND_NONE:
3134 return "invalid expression context";
3135 case EXPR_KIND_OTHER:
3136 return "extension expression";
3137 case EXPR_KIND_JOIN_ON:
3138 return "JOIN/ON";
3140 return "JOIN/USING";
3142 return "sub-SELECT in FROM";
3144 return "function in FROM";
3145 case EXPR_KIND_WHERE:
3146 return "WHERE";
3147 case EXPR_KIND_POLICY:
3148 return "POLICY";
3149 case EXPR_KIND_HAVING:
3150 return "HAVING";
3151 case EXPR_KIND_FILTER:
3152 return "FILTER";
3154 return "window PARTITION BY";
3156 return "window ORDER BY";
3158 return "window RANGE";
3160 return "window ROWS";
3162 return "window GROUPS";
3164 return "SELECT";
3166 return "INSERT";
3169 return "UPDATE";
3171 return "MERGE WHEN";
3172 case EXPR_KIND_GROUP_BY:
3173 return "GROUP BY";
3174 case EXPR_KIND_ORDER_BY:
3175 return "ORDER BY";
3177 return "DISTINCT ON";
3178 case EXPR_KIND_LIMIT:
3179 return "LIMIT";
3180 case EXPR_KIND_OFFSET:
3181 return "OFFSET";
3184 return "RETURNING";
3185 case EXPR_KIND_VALUES:
3187 return "VALUES";
3190 return "CHECK";
3193 return "DEFAULT";
3195 return "index expression";
3197 return "index predicate";
3199 return "statistics expression";
3201 return "USING";
3203 return "EXECUTE";
3205 return "WHEN";
3207 return "partition bound";
3209 return "PARTITION BY";
3211 return "CALL";
3213 return "WHERE";
3215 return "GENERATED AS";
3217 return "CYCLE";
3218
3219 /*
3220 * There is intentionally no default: case here, so that the
3221 * compiler will warn if we add a new ParseExprKind without
3222 * extending this switch. If we do see an unrecognized value at
3223 * runtime, we'll fall through to the "unrecognized" return.
3224 */
3225 }
3226 return "unrecognized expression kind";
3227}
3228
3229/*
3230 * Make string Const node from JSON encoding name.
3231 *
3232 * UTF8 is default encoding.
3233 */
3234static Const *
3236{
3238 const char *enc;
3239 Name encname = palloc(sizeof(NameData));
3240
3241 if (!format ||
3242 format->format_type == JS_FORMAT_DEFAULT ||
3243 format->encoding == JS_ENC_DEFAULT)
3245 else
3246 encoding = format->encoding;
3247
3248 switch (encoding)
3249 {
3250 case JS_ENC_UTF16:
3251 enc = "UTF16";
3252 break;
3253 case JS_ENC_UTF32:
3254 enc = "UTF32";
3255 break;
3256 case JS_ENC_UTF8:
3257 enc = "UTF8";
3258 break;
3259 default:
3260 elog(ERROR, "invalid JSON encoding: %d", encoding);
3261 break;
3262 }
3263
3264 namestrcpy(encname, enc);
3265
3266 return makeConst(NAMEOID, -1, InvalidOid, NAMEDATALEN,
3267 NameGetDatum(encname), false, false);
3268}
3269
3270/*
3271 * Make bytea => text conversion using specified JSON format encoding.
3272 */
3273static Node *
3275{
3277 FuncExpr *fexpr = makeFuncExpr(F_CONVERT_FROM, TEXTOID,
3278 list_make2(expr, encoding),
3281
3282 fexpr->location = location;
3283
3284 return (Node *) fexpr;
3285}
3286
3287/*
3288 * Transform JSON value expression using specified input JSON format or
3289 * default format otherwise, coercing to the targettype if needed.
3290 *
3291 * Returned expression is either ve->raw_expr coerced to text (if needed) or
3292 * a JsonValueExpr with formatted_expr set to the coerced copy of raw_expr
3293 * if the specified format and the targettype requires it.
3294 */
3295static Node *
3296transformJsonValueExpr(ParseState *pstate, const char *constructName,
3297 JsonValueExpr *ve, JsonFormatType default_format,
3298 Oid targettype, bool isarg)
3299{
3300 Node *expr = transformExprRecurse(pstate, (Node *) ve->raw_expr);
3301 Node *rawexpr;
3303 Oid exprtype;
3304 int location;
3305 char typcategory;
3306 bool typispreferred;
3307
3308 if (exprType(expr) == UNKNOWNOID)
3309 expr = coerce_to_specific_type(pstate, expr, TEXTOID, constructName);
3310
3311 rawexpr = expr;
3312 exprtype = exprType(expr);
3313 location = exprLocation(expr);
3314
3315 get_type_category_preferred(exprtype, &typcategory, &typispreferred);
3316
3318 {
3319 if (ve->format->encoding != JS_ENC_DEFAULT && exprtype != BYTEAOID)
3320 ereport(ERROR,
3321 errcode(ERRCODE_DATATYPE_MISMATCH),
3322 errmsg("JSON ENCODING clause is only allowed for bytea input type"),
3323 parser_errposition(pstate, ve->format->location));
3324
3325 if (exprtype == JSONOID || exprtype == JSONBOID)
3326 format = JS_FORMAT_DEFAULT; /* do not format json[b] types */
3327 else
3328 format = ve->format->format_type;
3329 }
3330 else if (isarg)
3331 {
3332 /*
3333 * Special treatment for PASSING arguments.
3334 *
3335 * Pass types supported by GetJsonPathVar() / JsonItemFromDatum()
3336 * directly without converting to json[b].
3337 */
3338 switch (exprtype)
3339 {
3340 case BOOLOID:
3341 case NUMERICOID:
3342 case INT2OID:
3343 case INT4OID:
3344 case INT8OID:
3345 case FLOAT4OID:
3346 case FLOAT8OID:
3347 case TEXTOID:
3348 case VARCHAROID:
3349 case DATEOID:
3350 case TIMEOID:
3351 case TIMETZOID:
3352 case TIMESTAMPOID:
3353 case TIMESTAMPTZOID:
3354 return expr;
3355
3356 default:
3357 if (typcategory == TYPCATEGORY_STRING)
3358 return expr;
3359 /* else convert argument to json[b] type */
3360 break;
3361 }
3362
3363 format = default_format;
3364 }
3365 else if (exprtype == JSONOID || exprtype == JSONBOID)
3366 format = JS_FORMAT_DEFAULT; /* do not format json[b] types */
3367 else
3368 format = default_format;
3369
3370 if (format != JS_FORMAT_DEFAULT ||
3371 (OidIsValid(targettype) && exprtype != targettype))
3372 {
3373 Node *coerced;
3374 bool only_allow_cast = OidIsValid(targettype);
3375
3376 /*
3377 * PASSING args are handled appropriately by GetJsonPathVar() /
3378 * JsonItemFromDatum().
3379 */
3380 if (!isarg &&
3381 !only_allow_cast &&
3382 exprtype != BYTEAOID && typcategory != TYPCATEGORY_STRING)
3383 ereport(ERROR,
3384 errcode(ERRCODE_DATATYPE_MISMATCH),
3386 errmsg("cannot use non-string types with implicit FORMAT JSON clause") :
3387 errmsg("cannot use non-string types with explicit FORMAT JSON clause"),
3388 parser_errposition(pstate, ve->format->location >= 0 ?
3389 ve->format->location : location));
3390
3391 /* Convert encoded JSON text from bytea. */
3392 if (format == JS_FORMAT_JSON && exprtype == BYTEAOID)
3393 {
3394 expr = makeJsonByteaToTextConversion(expr, ve->format, location);
3395 exprtype = TEXTOID;
3396 }
3397
3398 if (!OidIsValid(targettype))
3399 targettype = format == JS_FORMAT_JSONB ? JSONBOID : JSONOID;
3400
3401 /* Try to coerce to the target type. */
3402 coerced = coerce_to_target_type(pstate, expr, exprtype,
3403 targettype, -1,
3406 location);
3407
3408 if (!coerced)
3409 {
3410 /* If coercion failed, use to_json()/to_jsonb() functions. */
3411 FuncExpr *fexpr;
3412 Oid fnoid;
3413
3414 /*
3415 * Though only allow a cast when the target type is specified by
3416 * the caller.
3417 */
3418 if (only_allow_cast)
3419 ereport(ERROR,
3420 (errcode(ERRCODE_CANNOT_COERCE),
3421 errmsg("cannot cast type %s to %s",
3422 format_type_be(exprtype),
3423 format_type_be(targettype)),
3424 parser_errposition(pstate, location)));
3425
3426 fnoid = targettype == JSONOID ? F_TO_JSON : F_TO_JSONB;
3427 fexpr = makeFuncExpr(fnoid, targettype, list_make1(expr),
3429
3430 fexpr->location = location;
3431
3432 coerced = (Node *) fexpr;
3433 }
3434
3435 if (coerced == expr)
3436 expr = rawexpr;
3437 else
3438 {
3439 ve = copyObject(ve);
3440 ve->raw_expr = (Expr *) rawexpr;
3441 ve->formatted_expr = (Expr *) coerced;
3442
3443 expr = (Node *) ve;
3444 }
3445 }
3446
3447 /* If returning a JsonValueExpr, formatted_expr must have been set. */
3448 Assert(!IsA(expr, JsonValueExpr) ||
3449 ((JsonValueExpr *) expr)->formatted_expr != NULL);
3450
3451 return expr;
3452}
3453
3454/*
3455 * Checks specified output format for its applicability to the target type.
3456 */
3457static void
3459 Oid targettype, bool allow_format_for_non_strings)
3460{
3461 if (!allow_format_for_non_strings &&
3462 format->format_type != JS_FORMAT_DEFAULT &&
3463 (targettype != BYTEAOID &&
3464 targettype != JSONOID &&
3465 targettype != JSONBOID))
3466 {
3467 char typcategory;
3468 bool typispreferred;
3469
3470 get_type_category_preferred(targettype, &typcategory, &typispreferred);
3471
3472 if (typcategory != TYPCATEGORY_STRING)
3473 ereport(ERROR,
3474 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3475 parser_errposition(pstate, format->location),
3476 errmsg("cannot use JSON format with non-string output types"));
3477 }
3478
3479 if (format->format_type == JS_FORMAT_JSON)
3480 {
3481 JsonEncoding enc = format->encoding != JS_ENC_DEFAULT ?
3482 format->encoding : JS_ENC_UTF8;
3483
3484 if (targettype != BYTEAOID &&
3485 format->encoding != JS_ENC_DEFAULT)
3486 ereport(ERROR,
3487 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3488 parser_errposition(pstate, format->location),
3489 errmsg("cannot set JSON encoding for non-bytea output types"));
3490
3491 if (enc != JS_ENC_UTF8)
3492 ereport(ERROR,
3493 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3494 errmsg("unsupported JSON encoding"),
3495 errhint("Only UTF8 JSON encoding is supported."),
3496 parser_errposition(pstate, format->location));
3497 }
3498}
3499
3500/*
3501 * Transform JSON output clause.
3502 *
3503 * Assigns target type oid and modifier.
3504 * Assigns default format or checks specified format for its applicability to
3505 * the target type.
3506 */
3507static JsonReturning *
3509 bool allow_format)
3510{
3511 JsonReturning *ret;
3512
3513 /* if output clause is not specified, make default clause value */
3514 if (!output)
3515 {
3516 ret = makeNode(JsonReturning);
3517
3519 ret->typid = InvalidOid;
3520 ret->typmod = -1;
3521
3522 return ret;
3523 }
3524
3525 ret = copyObject(output->returning);
3526
3527 typenameTypeIdAndMod(pstate, output->typeName, &ret->typid, &ret->typmod);
3528
3529 if (output->typeName->setof)
3530 ereport(ERROR,
3531 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3532 errmsg("returning SETOF types is not supported in SQL/JSON functions"));
3533
3534 if (get_typtype(ret->typid) == TYPTYPE_PSEUDO)
3535 ereport(ERROR,
3536 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3537 errmsg("returning pseudo-types is not supported in SQL/JSON functions"));
3538
3540 /* assign JSONB format when returning jsonb, or JSON format otherwise */
3541 ret->format->format_type =
3542 ret->typid == JSONBOID ? JS_FORMAT_JSONB : JS_FORMAT_JSON;
3543 else
3544 checkJsonOutputFormat(pstate, ret->format, ret->typid, allow_format);
3545
3546 return ret;
3547}
3548
3549/*
3550 * Transform JSON output clause of JSON constructor functions.
3551 *
3552 * Derive RETURNING type, if not specified, from argument types.
3553 */
3554static JsonReturning *
3556 List *args)
3557{
3558 JsonReturning *returning = transformJsonOutput(pstate, output, true);
3559
3560 if (!OidIsValid(returning->typid))
3561 {
3562 ListCell *lc;
3563 bool have_jsonb = false;
3564
3565 foreach(lc, args)
3566 {
3567 Node *expr = lfirst(lc);
3568 Oid typid = exprType(expr);
3569
3570 have_jsonb |= typid == JSONBOID;
3571
3572 if (have_jsonb)
3573 break;
3574 }
3575
3576 if (have_jsonb)
3577 {
3578 returning->typid = JSONBOID;
3579 returning->format->format_type = JS_FORMAT_JSONB;
3580 }
3581 else
3582 {
3583 /* XXX TEXT is default by the standard, but we return JSON */
3584 returning->typid = JSONOID;
3585 returning->format->format_type = JS_FORMAT_JSON;
3586 }
3587
3588 returning->typmod = -1;
3589 }
3590
3591 return returning;
3592}
3593
3594/*
3595 * Coerce json[b]-valued function expression to the output type.
3596 */
3597static Node *
3599 const JsonReturning *returning, bool report_error)
3600{
3601 Node *res;
3602 int location;
3603 Oid exprtype = exprType(expr);
3604
3605 /* if output type is not specified or equals to function type, return */
3606 if (!OidIsValid(returning->typid) || returning->typid == exprtype)
3607 return expr;
3608
3609 location = exprLocation(expr);
3610
3611 if (location < 0)
3612 location = returning->format->location;
3613
3614 /* special case for RETURNING bytea FORMAT json */
3615 if (returning->format->format_type == JS_FORMAT_JSON &&
3616 returning->typid == BYTEAOID)
3617 {
3618 /* encode json text into bytea using pg_convert_to() */
3619 Node *texpr = coerce_to_specific_type(pstate, expr, TEXTOID,
3620 "JSON_FUNCTION");
3621 Const *enc = getJsonEncodingConst(returning->format);
3622 FuncExpr *fexpr = makeFuncExpr(F_CONVERT_TO, BYTEAOID,
3623 list_make2(texpr, enc),
3626
3627 fexpr->location = location;
3628
3629 return (Node *) fexpr;
3630 }
3631
3632 /*
3633 * For other cases, try to coerce expression to the output type using
3634 * assignment-level casts, erroring out if none available. This basically
3635 * allows coercing the jsonb value to any string type (typcategory = 'S').
3636 *
3637 * Requesting assignment-level here means that typmod / length coercion
3638 * assumes implicit coercion which is the behavior we want; see
3639 * build_coercion_expression().
3640 */
3641 res = coerce_to_target_type(pstate, expr, exprtype,
3642 returning->typid, returning->typmod,
3645 location);
3646
3647 if (!res && report_error)
3648 ereport(ERROR,
3649 errcode(ERRCODE_CANNOT_COERCE),
3650 errmsg("cannot cast type %s to %s",
3651 format_type_be(exprtype),
3652 format_type_be(returning->typid)),
3653 parser_coercion_errposition(pstate, location, expr));
3654
3655 return res;
3656}
3657
3658/*
3659 * Make a JsonConstructorExpr node.
3660 */
3661static Node *
3663 List *args, Expr *fexpr, JsonReturning *returning,
3664 bool unique, bool absent_on_null, int location)
3665{
3667 Node *placeholder;
3668 Node *coercion;
3669
3670 jsctor->args = args;
3671 jsctor->func = fexpr;
3672 jsctor->type = type;
3673 jsctor->returning = returning;
3674 jsctor->unique = unique;
3675 jsctor->absent_on_null = absent_on_null;
3676 jsctor->location = location;
3677
3678 /*
3679 * Coerce to the RETURNING type and format, if needed. We abuse
3680 * CaseTestExpr here as placeholder to pass the result of either
3681 * evaluating 'fexpr' or whatever is produced by ExecEvalJsonConstructor()
3682 * that is of type JSON or JSONB to the coercion function.
3683 */
3684 if (fexpr)
3685 {
3687
3688 cte->typeId = exprType((Node *) fexpr);
3689 cte->typeMod = exprTypmod((Node *) fexpr);
3690 cte->collation = exprCollation((Node *) fexpr);
3691
3692 placeholder = (Node *) cte;
3693 }
3694 else
3695 {
3697
3698 cte->typeId = returning->format->format_type == JS_FORMAT_JSONB ?
3699 JSONBOID : JSONOID;
3700 cte->typeMod = -1;
3701 cte->collation = InvalidOid;
3702
3703 placeholder = (Node *) cte;
3704 }
3705
3706 coercion = coerceJsonFuncExpr(pstate, placeholder, returning, true);
3707
3708 if (coercion != placeholder)
3709 jsctor->coercion = (Expr *) coercion;
3710
3711 return (Node *) jsctor;
3712}
3713
3714/*
3715 * Transform JSON_OBJECT() constructor.
3716 *
3717 * JSON_OBJECT() is transformed into a JsonConstructorExpr node of type
3718 * JSCTOR_JSON_OBJECT. The result is coerced to the target type given
3719 * by ctor->output.
3720 */
3721static Node *
3723{
3724 JsonReturning *returning;
3725 List *args = NIL;
3726
3727 /* transform key-value pairs, if any */
3728 if (ctor->exprs)
3729 {
3730 ListCell *lc;
3731
3732 /* transform and append key-value arguments */
3733 foreach(lc, ctor->exprs)
3734 {
3736 Node *key = transformExprRecurse(pstate, (Node *) kv->key);
3737 Node *val = transformJsonValueExpr(pstate, "JSON_OBJECT()",
3738 kv->value,
3740 InvalidOid, false);
3741
3742 args = lappend(args, key);
3743 args = lappend(args, val);
3744 }
3745 }
3746
3747 returning = transformJsonConstructorOutput(pstate, ctor->output, args);
3748
3749 return makeJsonConstructorExpr(pstate, JSCTOR_JSON_OBJECT, args, NULL,
3750 returning, ctor->unique,
3751 ctor->absent_on_null, ctor->location);
3752}
3753
3754/*
3755 * Transform JSON_ARRAY(query [FORMAT] [RETURNING] [ON NULL]) into
3756 * (SELECT JSON_ARRAYAGG(a [FORMAT] [RETURNING] [ON NULL]) FROM (query) q(a))
3757 */
3758static Node *
3761{
3762 SubLink *sublink = makeNode(SubLink);
3765 Alias *alias = makeNode(Alias);
3766 ResTarget *target = makeNode(ResTarget);
3768 ColumnRef *colref = makeNode(ColumnRef);
3769 Query *query;
3770 ParseState *qpstate;
3771
3772 /* Transform query only for counting target list entries. */
3773 qpstate = make_parsestate(pstate);
3774
3775 query = transformStmt(qpstate, copyObject(ctor->query));
3776
3777 if (count_nonjunk_tlist_entries(query->targetList) != 1)
3778 ereport(ERROR,
3779 errcode(ERRCODE_SYNTAX_ERROR),
3780 errmsg("subquery must return only one column"),
3781 parser_errposition(pstate, ctor->location));
3782
3783 free_parsestate(qpstate);
3784
3785 colref->fields = list_make2(makeString(pstrdup("q")),
3786 makeString(pstrdup("a")));
3787 colref->location = ctor->location;
3788
3789 /*
3790 * No formatting necessary, so set formatted_expr to be the same as
3791 * raw_expr.
3792 */
3793 agg->arg = makeJsonValueExpr((Expr *) colref, (Expr *) colref,
3794 ctor->format);
3795 agg->absent_on_null = ctor->absent_on_null;
3797 agg->constructor->agg_order = NIL;
3798 agg->constructor->output = ctor->output;
3799 agg->constructor->location = ctor->location;
3800
3801 target->name = NULL;
3802 target->indirection = NIL;
3803 target->val = (Node *) agg;
3804 target->location = ctor->location;
3805
3806 alias->aliasname = pstrdup("q");
3807 alias->colnames = list_make1(makeString(pstrdup("a")));
3808
3809 range->lateral = false;
3810 range->subquery = ctor->query;
3811 range->alias = alias;
3812
3813 select->targetList = list_make1(target);
3814 select->fromClause = list_make1(range);
3815
3816 sublink->subLinkType = EXPR_SUBLINK;
3817 sublink->subLinkId = 0;
3818 sublink->testexpr = NULL;
3819 sublink->operName = NIL;
3820 sublink->subselect = (Node *) select;
3821 sublink->location = ctor->location;
3822
3823 return transformExprRecurse(pstate, (Node *) sublink);
3824}
3825
3826/*
3827 * Common code for JSON_OBJECTAGG and JSON_ARRAYAGG transformation.
3828 */
3829static Node *
3831 JsonReturning *returning, List *args,
3832 Oid aggfnoid, Oid aggtype,
3833 JsonConstructorType ctor_type,
3834 bool unique, bool absent_on_null)
3835{
3836 Node *node;
3837 Expr *aggfilter;
3838
3839 aggfilter = agg_ctor->agg_filter ? (Expr *)
3840 transformWhereClause(pstate, agg_ctor->agg_filter,
3841 EXPR_KIND_FILTER, "FILTER") : NULL;
3842
3843 if (agg_ctor->over)
3844 {
3845 /* window function */
3846 WindowFunc *wfunc = makeNode(WindowFunc);
3847
3848 wfunc->winfnoid = aggfnoid;
3849 wfunc->wintype = aggtype;
3850 /* wincollid and inputcollid will be set by parse_collate.c */
3851 wfunc->args = args;
3852 wfunc->aggfilter = aggfilter;
3853 wfunc->runCondition = NIL;
3854 /* winref will be set by transformWindowFuncCall */
3855 wfunc->winstar = false;
3856 wfunc->winagg = true;
3857 wfunc->location = agg_ctor->location;
3858
3859 /*
3860 * ordered aggs not allowed in windows yet
3861 */
3862 if (agg_ctor->agg_order != NIL)
3863 ereport(ERROR,
3864 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3865 errmsg("aggregate ORDER BY is not implemented for window functions"),
3866 parser_errposition(pstate, agg_ctor->location));
3867
3868 /* parse_agg.c does additional window-func-specific processing */
3869 transformWindowFuncCall(pstate, wfunc, agg_ctor->over);
3870
3871 node = (Node *) wfunc;
3872 }
3873 else
3874 {
3875 Aggref *aggref = makeNode(Aggref);
3876
3877 aggref->aggfnoid = aggfnoid;
3878 aggref->aggtype = aggtype;
3879
3880 /* aggcollid and inputcollid will be set by parse_collate.c */
3881 /* aggtranstype will be set by planner */
3882 /* aggargtypes will be set by transformAggregateCall */
3883 /* aggdirectargs and args will be set by transformAggregateCall */
3884 /* aggorder and aggdistinct will be set by transformAggregateCall */
3885 aggref->aggfilter = aggfilter;
3886 aggref->aggstar = false;
3887 aggref->aggvariadic = false;
3888 aggref->aggkind = AGGKIND_NORMAL;
3889 aggref->aggpresorted = false;
3890 /* agglevelsup will be set by transformAggregateCall */
3891 aggref->aggsplit = AGGSPLIT_SIMPLE; /* planner might change this */
3892 aggref->aggno = -1; /* planner will set aggno and aggtransno */
3893 aggref->aggtransno = -1;
3894 aggref->location = agg_ctor->location;
3895
3896 transformAggregateCall(pstate, aggref, args, agg_ctor->agg_order, false);
3897
3898 node = (Node *) aggref;
3899 }
3900
3901 return makeJsonConstructorExpr(pstate, ctor_type, NIL, (Expr *) node,
3902 returning, unique, absent_on_null,
3903 agg_ctor->location);
3904}
3905
3906/*
3907 * Transform JSON_OBJECTAGG() aggregate function.
3908 *
3909 * JSON_OBJECTAGG() is transformed into a JsonConstructorExpr node of type
3910 * JSCTOR_JSON_OBJECTAGG, which at runtime becomes a
3911 * json[b]_object_agg[_unique][_strict](agg->arg->key, agg->arg->value) call
3912 * depending on the output JSON format. The result is coerced to the target
3913 * type given by agg->constructor->output.
3914 */
3915static Node *
3917{
3918 JsonReturning *returning;
3919 Node *key;
3920 Node *val;
3921 List *args;
3922 Oid aggfnoid;
3923 Oid aggtype;
3924
3925 key = transformExprRecurse(pstate, (Node *) agg->arg->key);
3926 val = transformJsonValueExpr(pstate, "JSON_OBJECTAGG()",
3927 agg->arg->value,
3929 InvalidOid, false);
3930 args = list_make2(key, val);
3931
3932 returning = transformJsonConstructorOutput(pstate, agg->constructor->output,
3933 args);
3934
3935 if (returning->format->format_type == JS_FORMAT_JSONB)
3936 {
3937 if (agg->absent_on_null)
3938 if (agg->unique)
3939 aggfnoid = F_JSONB_OBJECT_AGG_UNIQUE_STRICT;
3940 else
3941 aggfnoid = F_JSONB_OBJECT_AGG_STRICT;
3942 else if (agg->unique)
3943 aggfnoid = F_JSONB_OBJECT_AGG_UNIQUE;
3944 else
3945 aggfnoid = F_JSONB_OBJECT_AGG;
3946
3947 aggtype = JSONBOID;
3948 }
3949 else
3950 {
3951 if (agg->absent_on_null)
3952 if (agg->unique)
3953 aggfnoid = F_JSON_OBJECT_AGG_UNIQUE_STRICT;
3954 else
3955 aggfnoid = F_JSON_OBJECT_AGG_STRICT;
3956 else if (agg->unique)
3957 aggfnoid = F_JSON_OBJECT_AGG_UNIQUE;
3958 else
3959 aggfnoid = F_JSON_OBJECT_AGG;
3960
3961 aggtype = JSONOID;
3962 }
3963
3964 return transformJsonAggConstructor(pstate, agg->constructor, returning,
3965 args, aggfnoid, aggtype,
3967 agg->unique, agg->absent_on_null);
3968}
3969
3970/*
3971 * Transform JSON_ARRAYAGG() aggregate function.
3972 *
3973 * JSON_ARRAYAGG() is transformed into a JsonConstructorExpr node of type
3974 * JSCTOR_JSON_ARRAYAGG, which at runtime becomes a
3975 * json[b]_object_agg[_unique][_strict](agg->arg) call depending on the output
3976 * JSON format. The result is coerced to the target type given by
3977 * agg->constructor->output.
3978 */
3979static Node *
3981{
3982 JsonReturning *returning;
3983 Node *arg;
3984 Oid aggfnoid;
3985 Oid aggtype;
3986
3987 arg = transformJsonValueExpr(pstate, "JSON_ARRAYAGG()", agg->arg,
3989
3990 returning = transformJsonConstructorOutput(pstate, agg->constructor->output,
3991 list_make1(arg));
3992
3993 if (returning->format->format_type == JS_FORMAT_JSONB)
3994 {
3995 aggfnoid = agg->absent_on_null ? F_JSONB_AGG_STRICT : F_JSONB_AGG;
3996 aggtype = JSONBOID;
3997 }
3998 else
3999 {
4000 aggfnoid = agg->absent_on_null ? F_JSON_AGG_STRICT : F_JSON_AGG;
4001 aggtype = JSONOID;
4002 }
4003
4004 return transformJsonAggConstructor(pstate, agg->constructor, returning,
4005 list_make1(arg), aggfnoid, aggtype,
4007 false, agg->absent_on_null);
4008}
4009
4010/*
4011 * Transform JSON_ARRAY() constructor.
4012 *
4013 * JSON_ARRAY() is transformed into a JsonConstructorExpr node of type
4014 * JSCTOR_JSON_ARRAY. The result is coerced to the target type given
4015 * by ctor->output.
4016 */
4017static Node *
4019{
4020 JsonReturning *returning;
4021 List *args = NIL;
4022
4023 /* transform element expressions, if any */
4024 if (ctor->exprs)
4025 {
4026 ListCell *lc;
4027
4028 /* transform and append element arguments */
4029 foreach(lc, ctor->exprs)
4030 {
4032 Node *val = transformJsonValueExpr(pstate, "JSON_ARRAY()",
4033 jsval, JS_FORMAT_DEFAULT,
4034 InvalidOid, false);
4035
4036 args = lappend(args, val);
4037 }
4038 }
4039
4040 returning = transformJsonConstructorOutput(pstate, ctor->output, args);
4041
4042 return makeJsonConstructorExpr(pstate, JSCTOR_JSON_ARRAY, args, NULL,
4043 returning, false, ctor->absent_on_null,
4044 ctor->location);
4045}
4046
4047static Node *
4049 Oid *exprtype)
4050{
4051 Node *raw_expr = transformExprRecurse(pstate, jsexpr);
4052 Node *expr = raw_expr;
4053
4054 *exprtype = exprType(expr);
4055
4056 /* prepare input document */
4057 if (*exprtype == BYTEAOID)
4058 {
4059 JsonValueExpr *jve;
4060
4061 expr = raw_expr;
4063 *exprtype = TEXTOID;
4064
4065 jve = makeJsonValueExpr((Expr *) raw_expr, (Expr *) expr, format);
4066 expr = (Node *) jve;
4067 }
4068 else
4069 {
4070 char typcategory;
4071 bool typispreferred;
4072
4073 get_type_category_preferred(*exprtype, &typcategory, &typispreferred);
4074
4075 if (*exprtype == UNKNOWNOID || typcategory == TYPCATEGORY_STRING)
4076 {
4077 expr = coerce_to_target_type(pstate, (Node *) expr, *exprtype,
4078 TEXTOID, -1,
4081 *exprtype = TEXTOID;
4082 }
4083
4084 if (format->encoding != JS_ENC_DEFAULT)
4085 ereport(ERROR,
4086 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4087 parser_errposition(pstate, format->location),
4088 errmsg("cannot use JSON FORMAT ENCODING clause for non-bytea input types")));
4089 }
4090
4091 return expr;
4092}
4093
4094/*
4095 * Transform IS JSON predicate.
4096 */
4097static Node *
4099{
4100 Oid exprtype;
4101 Node *expr = transformJsonParseArg(pstate, pred->expr, pred->format,
4102 &exprtype);
4103
4104 /* make resulting expression */
4105 if (exprtype != TEXTOID && exprtype != JSONOID && exprtype != JSONBOID)
4106 ereport(ERROR,
4107 (errcode(ERRCODE_DATATYPE_MISMATCH),
4108 errmsg("cannot use type %s in IS JSON predicate",
4109 format_type_be(exprtype))));
4110
4111 /* This intentionally(?) drops the format clause. */
4112 return makeJsonIsPredicate(expr, NULL, pred->item_type,
4113 pred->unique_keys, pred->location);
4114}
4115
4116/*
4117 * Transform the RETURNING clause of a JSON_*() expression if there is one and
4118 * create one if not.
4119 */
4120static JsonReturning *
4122{
4123 JsonReturning *returning;
4124
4125 if (output)
4126 {
4127 returning = transformJsonOutput(pstate, output, false);
4128
4129 Assert(OidIsValid(returning->typid));
4130
4131 if (returning->typid != JSONOID && returning->typid != JSONBOID)
4132 ereport(ERROR,
4133 (errcode(ERRCODE_DATATYPE_MISMATCH),
4134 errmsg("cannot use type %s in RETURNING clause of %s",
4135 format_type_be(returning->typid), fname),
4136 errhint("Try returning json or jsonb."),
4137 parser_errposition(pstate, output->typeName->location)));
4138 }
4139 else
4140 {
4141 /* Output type is JSON by default. */
4142 Oid targettype = JSONOID;
4144
4145 returning = makeNode(JsonReturning);
4146 returning->format = makeJsonFormat(format, JS_ENC_DEFAULT, -1);
4147 returning->typid = targettype;
4148 returning->typmod = -1;
4149 }
4150
4151 return returning;
4152}
4153
4154/*
4155 * Transform a JSON() expression.
4156 *
4157 * JSON() is transformed into a JsonConstructorExpr of type JSCTOR_JSON_PARSE,
4158 * which validates the input expression value as JSON.
4159 */
4160static Node *
4162{
4163 JsonOutput *output = jsexpr->output;
4164 JsonReturning *returning;
4165 Node *arg;
4166
4167 returning = transformJsonReturning(pstate, output, "JSON()");
4168
4169 if (jsexpr->unique_keys)
4170 {
4171 /*
4172 * Coerce string argument to text and then to json[b] in the executor
4173 * node with key uniqueness check.
4174 */
4175 JsonValueExpr *jve = jsexpr->expr;
4176 Oid arg_type;
4177
4178 arg = transformJsonParseArg(pstate, (Node *) jve->raw_expr, jve->format,
4179 &arg_type);
4180
4181 if (arg_type != TEXTOID)
4182 ereport(ERROR,
4183 (errcode(ERRCODE_DATATYPE_MISMATCH),
4184 errmsg("cannot use non-string types with WITH UNIQUE KEYS clause"),
4185 parser_errposition(pstate, jsexpr->location)));
4186 }
4187 else
4188 {
4189 /*
4190 * Coerce argument to target type using CAST for compatibility with PG
4191 * function-like CASTs.
4192 */
4193 arg = transformJsonValueExpr(pstate, "JSON()", jsexpr->expr,
4194 JS_FORMAT_JSON, returning->typid, false);
4195 }
4196
4198 returning, jsexpr->unique_keys, false,
4199 jsexpr->location);
4200}
4201
4202/*
4203 * Transform a JSON_SCALAR() expression.
4204 *
4205 * JSON_SCALAR() is transformed into a JsonConstructorExpr of type
4206 * JSCTOR_JSON_SCALAR, which converts the input SQL scalar value into
4207 * a json[b] value.
4208 */
4209static Node *
4211{
4212 Node *arg = transformExprRecurse(pstate, (Node *) jsexpr->expr);
4213 JsonOutput *output = jsexpr->output;
4214 JsonReturning *returning;
4215
4216 returning = transformJsonReturning(pstate, output, "JSON_SCALAR()");
4217
4218 if (exprType(arg) == UNKNOWNOID)
4219 arg = coerce_to_specific_type(pstate, arg, TEXTOID, "JSON_SCALAR");
4220
4222 returning, false, false, jsexpr->location);
4223}
4224
4225/*
4226 * Transform a JSON_SERIALIZE() expression.
4227 *
4228 * JSON_SERIALIZE() is transformed into a JsonConstructorExpr of type
4229 * JSCTOR_JSON_SERIALIZE which converts the input JSON value into a character
4230 * or bytea string.
4231 */
4232static Node *
4234{
4235 JsonReturning *returning;
4236 Node *arg = transformJsonValueExpr(pstate, "JSON_SERIALIZE()",
4237 expr->expr,
4239 InvalidOid, false);
4240
4241 if (expr->output)
4242 {
4243 returning = transformJsonOutput(pstate, expr->output, true);
4244
4245 if (returning->typid != BYTEAOID)
4246 {
4247 char typcategory;
4248 bool typispreferred;
4249
4250 get_type_category_preferred(returning->typid, &typcategory,
4251 &typispreferred);
4252 if (typcategory != TYPCATEGORY_STRING)
4253 ereport(ERROR,
4254 (errcode(ERRCODE_DATATYPE_MISMATCH),
4255 errmsg("cannot use type %s in RETURNING clause of %s",
4256 format_type_be(returning->typid),
4257 "JSON_SERIALIZE()"),
4258 errhint("Try returning a string type or bytea.")));
4259 }
4260 }
4261 else
4262 {
4263 /* RETURNING TEXT FORMAT JSON is by default */
4264 returning = makeNode(JsonReturning);
4266 returning->typid = TEXTOID;
4267 returning->typmod = -1;
4268 }
4269
4271 NULL, returning, false, false, expr->location);
4272}
4273
4274/*
4275 * Transform JSON_VALUE, JSON_QUERY, JSON_EXISTS, JSON_TABLE functions into
4276 * a JsonExpr node.
4277 */
4278static Node *
4280{
4281 JsonExpr *jsexpr;
4282 Node *path_spec;
4283 const char *func_name = NULL;
4284 JsonFormatType default_format;
4285
4286 switch (func->op)
4287 {
4288 case JSON_EXISTS_OP:
4289 func_name = "JSON_EXISTS";
4290 default_format = JS_FORMAT_DEFAULT;
4291 break;
4292 case JSON_QUERY_OP:
4293 func_name = "JSON_QUERY";
4294 default_format = JS_FORMAT_JSONB;
4295 break;
4296 case JSON_VALUE_OP:
4297 func_name = "JSON_VALUE";
4298 default_format = JS_FORMAT_DEFAULT;
4299 break;
4300 case JSON_TABLE_OP:
4301 func_name = "JSON_TABLE";
4302 default_format = JS_FORMAT_JSONB;
4303 break;
4304 default:
4305 elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4306 default_format = JS_FORMAT_DEFAULT; /* keep compiler quiet */
4307 break;
4308 }
4309
4310 /*
4311 * Even though the syntax allows it, FORMAT JSON specification in
4312 * RETURNING is meaningless except for JSON_QUERY(). Flag if not
4313 * JSON_QUERY().
4314 */
4315 if (func->output && func->op != JSON_QUERY_OP)
4316 {
4318
4319 if (format->format_type != JS_FORMAT_DEFAULT ||
4320 format->encoding != JS_ENC_DEFAULT)
4321 ereport(ERROR,
4322 errcode(ERRCODE_SYNTAX_ERROR),
4323 errmsg("cannot specify FORMAT JSON in RETURNING clause of %s()",
4324 func_name),
4325 parser_errposition(pstate, format->location));
4326 }
4327
4328 /* OMIT QUOTES is meaningless when strings are wrapped. */
4329 if (func->op == JSON_QUERY_OP)
4330 {
4331 if (func->quotes == JS_QUOTES_OMIT &&
4332 (func->wrapper == JSW_CONDITIONAL ||
4333 func->wrapper == JSW_UNCONDITIONAL))
4334 ereport(ERROR,
4335 errcode(ERRCODE_SYNTAX_ERROR),
4336 errmsg("SQL/JSON QUOTES behavior must not be specified when WITH WRAPPER is used"),
4337 parser_errposition(pstate, func->location));
4338 if (func->on_empty != NULL &&
4340 func->on_empty->btype != JSON_BEHAVIOR_NULL &&
4345 {
4346 if (func->column_name == NULL)
4347 ereport(ERROR,
4348 errcode(ERRCODE_SYNTAX_ERROR),
4349 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4350 errmsg("invalid %s behavior", "ON EMPTY"),
4351 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4352 second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4353 errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for %s.",
4354 "ON EMPTY", "JSON_QUERY()"),
4355 parser_errposition(pstate, func->on_empty->location));
4356 else
4357 ereport(ERROR,
4358 errcode(ERRCODE_SYNTAX_ERROR),
4359 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4360 errmsg("invalid %s behavior for column \"%s\"",
4361 "ON EMPTY", func->column_name),
4362 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4363 errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for formatted columns.",
4364 "ON EMPTY"),
4365 parser_errposition(pstate, func->on_empty->location));
4366 }
4367 if (func->on_error != NULL &&
4369 func->on_error->btype != JSON_BEHAVIOR_NULL &&
4374 {
4375 if (func->column_name == NULL)
4376 ereport(ERROR,
4377 errcode(ERRCODE_SYNTAX_ERROR),
4378 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4379 errmsg("invalid %s behavior", "ON ERROR"),
4380 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4381 second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4382 errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for %s.",
4383 "ON ERROR", "JSON_QUERY()"),
4384 parser_errposition(pstate, func->on_error->location));
4385 else
4386 ereport(ERROR,
4387 errcode(ERRCODE_SYNTAX_ERROR),
4388 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4389 errmsg("invalid %s behavior for column \"%s\"",
4390 "ON ERROR", func->column_name),
4391 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4392 errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for formatted columns.",
4393 "ON ERROR"),
4394 parser_errposition(pstate, func->on_error->location));
4395 }
4396 }
4397
4398 /* Check that ON ERROR/EMPTY behavior values are valid for the function. */
4399 if (func->op == JSON_EXISTS_OP &&
4400 func->on_error != NULL &&
4402 func->on_error->btype != JSON_BEHAVIOR_TRUE &&
4405 {
4406 if (func->column_name == NULL)
4407 ereport(ERROR,
4408 errcode(ERRCODE_SYNTAX_ERROR),
4409 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4410 errmsg("invalid %s behavior", "ON ERROR"),
4411 errdetail("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in %s for %s.",
4412 "ON ERROR", "JSON_EXISTS()"),
4413 parser_errposition(pstate, func->on_error->location));
4414 else
4415 ereport(ERROR,
4416 errcode(ERRCODE_SYNTAX_ERROR),
4417 /*- translator: first %s is name a SQL/JSON clause (eg. ON EMPTY) */
4418 errmsg("invalid %s behavior for column \"%s\"",
4419 "ON ERROR", func->column_name),
4420 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4421 errdetail("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in %s for EXISTS columns.",
4422 "ON ERROR"),
4423 parser_errposition(pstate, func->on_error->location));
4424 }
4425 if (func->op == JSON_VALUE_OP)
4426 {
4427 if (func->on_empty != NULL &&
4429 func->on_empty->btype != JSON_BEHAVIOR_NULL &&
4431 {
4432 if (func->column_name == NULL)
4433 ereport(ERROR,
4434 errcode(ERRCODE_SYNTAX_ERROR),
4435 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4436 errmsg("invalid %s behavior", "ON EMPTY"),
4437 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4438 second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4439 errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for %s.",
4440 "ON EMPTY", "JSON_VALUE()"),
4441 parser_errposition(pstate, func->on_empty->location));
4442 else
4443 ereport(ERROR,
4444 errcode(ERRCODE_SYNTAX_ERROR),
4445 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4446 errmsg("invalid %s behavior for column \"%s\"",
4447 "ON EMPTY", func->column_name),
4448 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4449 errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for scalar columns.",
4450 "ON EMPTY"),
4451 parser_errposition(pstate, func->on_empty->location));
4452 }
4453 if (func->on_error != NULL &&
4455 func->on_error->btype != JSON_BEHAVIOR_NULL &&
4457 {
4458 if (func->column_name == NULL)
4459 ereport(ERROR,
4460 errcode(ERRCODE_SYNTAX_ERROR),
4461 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4462 errmsg("invalid %s behavior", "ON ERROR"),
4463 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4464 second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4465 errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for %s.",
4466 "ON ERROR", "JSON_VALUE()"),
4467 parser_errposition(pstate, func->on_error->location));
4468 else
4469 ereport(ERROR,
4470 errcode(ERRCODE_SYNTAX_ERROR),
4471 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4472 errmsg("invalid %s behavior for column \"%s\"",
4473 "ON ERROR", func->column_name),
4474 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4475 errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for scalar columns.",
4476 "ON ERROR"),
4477 parser_errposition(pstate, func->on_error->location));
4478 }
4479 }
4480
4481 jsexpr = makeNode(JsonExpr);
4482 jsexpr->location = func->location;
4483 jsexpr->op = func->op;
4484 jsexpr->column_name = func->column_name;
4485
4486 /*
4487 * jsonpath machinery can only handle jsonb documents, so coerce the input
4488 * if not already of jsonb type.
4489 */
4490 jsexpr->formatted_expr = transformJsonValueExpr(pstate, func_name,
4491 func->context_item,
4492 default_format,
4493 JSONBOID,
4494 false);
4495 jsexpr->format = func->context_item->format;
4496
4497 path_spec = transformExprRecurse(pstate, func->pathspec);
4498 path_spec = coerce_to_target_type(pstate, path_spec, exprType(path_spec),
4499 JSONPATHOID, -1,
4501 exprLocation(path_spec));
4502 if (path_spec == NULL)
4503 ereport(ERROR,
4504 (errcode(ERRCODE_DATATYPE_MISMATCH),
4505 errmsg("JSON path expression must be of type %s, not of type %s",
4506 "jsonpath", format_type_be(exprType(path_spec))),
4507 parser_errposition(pstate, exprLocation(path_spec))));
4508 jsexpr->path_spec = path_spec;
4509
4510 /* Transform and coerce the PASSING arguments to jsonb. */
4511 transformJsonPassingArgs(pstate, func_name,
4513 func->passing,
4514 &jsexpr->passing_values,
4515 &jsexpr->passing_names);
4516
4517 /* Transform the JsonOutput into JsonReturning. */
4518 jsexpr->returning = transformJsonOutput(pstate, func->output, false);
4519
4520 switch (func->op)
4521 {
4522 case JSON_EXISTS_OP:
4523 /* JSON_EXISTS returns boolean by default. */
4524 if (!OidIsValid(jsexpr->returning->typid))
4525 {
4526 jsexpr->returning->typid = BOOLOID;
4527 jsexpr->returning->typmod = -1;
4528 }
4529
4530 /* JSON_TABLE() COLUMNS can specify a non-boolean type. */
4531 if (jsexpr->returning->typid != BOOLOID)
4532 jsexpr->use_json_coercion = true;
4533
4534 jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4536 jsexpr->returning);
4537 break;
4538
4539 case JSON_QUERY_OP:
4540 /* JSON_QUERY returns jsonb by default. */
4541 if (!OidIsValid(jsexpr->returning->typid))
4542 {
4543 JsonReturning *ret = jsexpr->returning;
4544
4545 ret->typid = JSONBOID;
4546 ret->typmod = -1;
4547 }
4548
4549 /*
4550 * Keep quotes on scalar strings by default, omitting them only if
4551 * OMIT QUOTES is specified.
4552 */
4553 jsexpr->omit_quotes = (func->quotes == JS_QUOTES_OMIT);
4554 jsexpr->wrapper = func->wrapper;
4555
4556 /*
4557 * Set up to coerce the result value of JsonPathValue() to the
4558 * RETURNING type (default or user-specified), if needed. Also if
4559 * OMIT QUOTES is specified.
4560 */
4561 if (jsexpr->returning->typid != JSONBOID || jsexpr->omit_quotes)
4562 jsexpr->use_json_coercion = true;
4563
4564 /* Assume NULL ON EMPTY when ON EMPTY is not specified. */
4565 jsexpr->on_empty = transformJsonBehavior(pstate, func->on_empty,
4567 jsexpr->returning);
4568 /* Assume NULL ON ERROR when ON ERROR is not specified. */
4569 jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4571 jsexpr->returning);
4572 break;
4573
4574 case JSON_VALUE_OP:
4575 /* JSON_VALUE returns text by default. */
4576 if (!OidIsValid(jsexpr->returning->typid))
4577 {
4578 jsexpr->returning->typid = TEXTOID;
4579 jsexpr->returning->typmod = -1;
4580 }
4581
4582 /*
4583 * Override whatever transformJsonOutput() set these to, which
4584 * assumes that output type to be jsonb.
4585 */
4588
4589 /* Always omit quotes from scalar strings. */
4590 jsexpr->omit_quotes = true;
4591
4592 /*
4593 * Set up to coerce the result value of JsonPathValue() to the
4594 * RETURNING type (default or user-specified), if needed.
4595 */
4596 if (jsexpr->returning->typid != TEXTOID)
4597 {
4598 if (get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN &&
4600 jsexpr->use_json_coercion = true;
4601 else
4602 jsexpr->use_io_coercion = true;
4603 }
4604
4605 /* Assume NULL ON EMPTY when ON EMPTY is not specified. */
4606 jsexpr->on_empty = transformJsonBehavior(pstate, func->on_empty,
4608 jsexpr->returning);
4609 /* Assume NULL ON ERROR when ON ERROR is not specified. */
4610 jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4612 jsexpr->returning);
4613 break;
4614
4615 case JSON_TABLE_OP:
4616 if (!OidIsValid(jsexpr->returning->typid))
4617 {
4618 jsexpr->returning->typid = exprType(jsexpr->formatted_expr);
4619 jsexpr->returning->typmod = -1;
4620 }
4621
4622 /*
4623 * Assume EMPTY ARRAY ON ERROR when ON ERROR is not specified.
4624 *
4625 * ON EMPTY cannot be specified at the top level but it can be for
4626 * the individual columns.
4627 */
4628 jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4630 jsexpr->returning);
4631 break;
4632
4633 default:
4634 elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4635 break;
4636 }
4637
4638 return (Node *) jsexpr;
4639}
4640
4641/*
4642 * Transform a SQL/JSON PASSING clause.
4643 */
4644static void
4645transformJsonPassingArgs(ParseState *pstate, const char *constructName,
4647 List **passing_values, List **passing_names)
4648{
4649 ListCell *lc;
4650
4651 *passing_values = NIL;
4652 *passing_names = NIL;
4653
4654 foreach(lc, args)
4655 {
4657 Node *expr = transformJsonValueExpr(pstate, constructName,
4658 arg->val, format,
4659 InvalidOid, true);
4660
4661 *passing_values = lappend(*passing_values, expr);
4662 *passing_names = lappend(*passing_names, makeString(arg->name));
4663 }
4664}
4665
4666/*
4667 * Recursively checks if the given expression, or its sub-node in some cases,
4668 * is valid for using as an ON ERROR / ON EMPTY DEFAULT expression.
4669 */
4670static bool
4672{
4673 if (expr == NULL)
4674 return false;
4675
4676 switch (nodeTag(expr))
4677 {
4678 /* Acceptable expression nodes */
4679 case T_Const:
4680 case T_FuncExpr:
4681 case T_OpExpr:
4682 return true;
4683
4684 /* Acceptable iff arg of the following nodes is one of the above */
4685 case T_CoerceViaIO:
4686 case T_CoerceToDomain:
4687 case T_ArrayCoerceExpr:
4688 case T_ConvertRowtypeExpr:
4689 case T_RelabelType:
4690 case T_CollateExpr:
4692 context);
4693 default:
4694 break;
4695 }
4696
4697 return false;
4698}
4699
4700/*
4701 * Transform a JSON BEHAVIOR clause.
4702 */
4703static JsonBehavior *
4705 JsonBehaviorType default_behavior,
4706 JsonReturning *returning)
4707{
4708 JsonBehaviorType btype = default_behavior;
4709 Node *expr = NULL;
4710 bool coerce_at_runtime = false;
4711 int location = -1;
4712
4713 if (behavior)
4714 {
4715 btype = behavior->btype;
4716 location = behavior->location;
4717 if (btype == JSON_BEHAVIOR_DEFAULT)
4718 {
4719 expr = transformExprRecurse(pstate, behavior->expr);
4720 if (!ValidJsonBehaviorDefaultExpr(expr, NULL))
4721 ereport(ERROR,
4722 (errcode(ERRCODE_DATATYPE_MISMATCH),
4723 errmsg("can only specify a constant, non-aggregate function, or operator expression for DEFAULT"),
4724 parser_errposition(pstate, exprLocation(expr))));
4725 if (contain_var_clause(expr))
4726 ereport(ERROR,
4727 (errcode(ERRCODE_DATATYPE_MISMATCH),
4728 errmsg("DEFAULT expression must not contain column references"),
4729 parser_errposition(pstate, exprLocation(expr))));
4730 if (expression_returns_set(expr))
4731 ereport(ERROR,
4732 (errcode(ERRCODE_DATATYPE_MISMATCH),
4733 errmsg("DEFAULT expression must not return a set"),
4734 parser_errposition(pstate, exprLocation(expr))));
4735 }
4736 }
4737
4738 if (expr == NULL && btype != JSON_BEHAVIOR_ERROR)
4739 expr = GetJsonBehaviorConst(btype, location);
4740
4741 /*
4742 * Try to coerce the expression if needed.
4743 *
4744 * Use runtime coercion using json_populate_type() if the expression is
4745 * NULL, jsonb-valued, or boolean-valued (unless the target type is
4746 * integer or domain over integer, in which case use the
4747 * boolean-to-integer cast function).
4748 *
4749 * For other non-NULL expressions, try to find a cast and error out if one
4750 * is not found.
4751 */
4752 if (expr && exprType(expr) != returning->typid)
4753 {
4754 bool isnull = (IsA(expr, Const) && ((Const *) expr)->constisnull);
4755
4756 if (isnull ||
4757 exprType(expr) == JSONBOID ||
4758 (exprType(expr) == BOOLOID &&
4759 getBaseType(returning->typid) != INT4OID))
4760 {
4761 coerce_at_runtime = true;
4762
4763 /*
4764 * json_populate_type() expects to be passed a jsonb value, so gin
4765 * up a Const containing the appropriate boolean value represented
4766 * as jsonb, discarding the original Const containing a plain
4767 * boolean.
4768 */
4769 if (exprType(expr) == BOOLOID)
4770 {
4771 char *val = btype == JSON_BEHAVIOR_TRUE ? "true" : "false";
4772
4773 expr = (Node *) makeConst(JSONBOID, -1, InvalidOid, -1,
4776 false, false);
4777 }
4778 }
4779 else
4780 {
4781 Node *coerced_expr;
4782 char typcategory = TypeCategory(returning->typid);
4783
4784 /*
4785 * Use an assignment cast if coercing to a string type so that
4786 * build_coercion_expression() assumes implicit coercion when
4787 * coercing the typmod, so that inputs exceeding length cause an
4788 * error instead of silent truncation.
4789 */
4790 coerced_expr =
4791 coerce_to_target_type(pstate, expr, exprType(expr),
4792 returning->typid, returning->typmod,
4793 (typcategory == TYPCATEGORY_STRING ||
4794 typcategory == TYPCATEGORY_BITSTRING) ?
4798 exprLocation((Node *) behavior));
4799
4800 if (coerced_expr == NULL)
4801 {
4802 /*
4803 * Provide a HINT if the expression comes from a DEFAULT
4804 * clause.
4805 */
4806 if (btype == JSON_BEHAVIOR_DEFAULT)
4807 ereport(ERROR,
4808 errcode(ERRCODE_CANNOT_COERCE),
4809 errmsg("cannot cast behavior expression of type %s to %s",
4810 format_type_be(exprType(expr)),
4811 format_type_be(returning->typid)),
4812 errhint("You will need to explicitly cast the expression to type %s.",
4813 format_type_be(returning->typid)),
4814 parser_errposition(pstate, exprLocation(expr)));
4815 else
4816 ereport(ERROR,
4817 errcode(ERRCODE_CANNOT_COERCE),
4818 errmsg("cannot cast behavior expression of type %s to %s",
4819 format_type_be(exprType(expr)),
4820 format_type_be(returning->typid)),
4821 parser_errposition(pstate, exprLocation(expr)));
4822 }
4823
4824 expr = coerced_expr;
4825 }
4826 }
4827
4828 if (behavior)
4829 behavior->expr = expr;
4830 else
4831 behavior = makeJsonBehavior(btype, expr, location);
4832
4833 behavior->coerce = coerce_at_runtime;
4834
4835 return behavior;
4836}
4837
4838/*
4839 * Returns a Const node holding the value for the given non-ERROR
4840 * JsonBehaviorType.
4841 */
4842static Node *
4844{
4845 Datum val = (Datum) 0;
4846 Oid typid = JSONBOID;
4847 int len = -1;
4848 bool isbyval = false;
4849 bool isnull = false;
4850 Const *con;
4851
4852 switch (btype)
4853 {
4856 break;
4857
4860 break;
4861
4862 case JSON_BEHAVIOR_TRUE:
4863 val = BoolGetDatum(true);
4864 typid = BOOLOID;
4865 len = sizeof(bool);
4866 isbyval = true;
4867 break;
4868
4870 val = BoolGetDatum(false);
4871 typid = BOOLOID;
4872 len = sizeof(bool);
4873 isbyval = true;
4874 break;
4875
4876 case JSON_BEHAVIOR_NULL:
4879 val = (Datum) 0;
4880 isnull = true;
4881 typid = INT4OID;
4882 len = sizeof(int32);
4883 isbyval = true;
4884 break;
4885
4886 /* These two behavior types are handled by the caller. */
4889 Assert(false);
4890 break;
4891
4892 default:
4893 elog(ERROR, "unrecognized SQL/JSON behavior %d", btype);
4894 break;
4895 }
4896
4897 con = makeConst(typid, -1, InvalidOid, len, val, isnull, isbyval);
4898 con->location = location;
4899
4900 return (Node *) con;
4901}
#define InvalidAttrNumber
Definition: attnum.h:23
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:125
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1109
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
int32_t int32
Definition: c.h:498
#define OidIsValid(objectId)
Definition: c.h:746
CompareType
Definition: cmptype.h:32
@ COMPARE_EQ
Definition: cmptype.h:36
@ COMPARE_NE
Definition: cmptype.h:39
enc
int32 anytime_typmod_check(bool istz, int32 typmod)
Definition: date.c:72
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3188
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1158
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errhint(const char *fmt,...)
Definition: elog.c:1318
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define _(x)
Definition: elog.c:91
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
void err(int eval, const char *fmt,...)
Definition: err.c:43
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:682
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
Oid MyDatabaseId
Definition: globals.c:95
Assert(PointerIsAligned(start, uint64))
#define MaxTupleAttributeNumber
Definition: htup_details.h:34
#define funcname
Definition: indent_codes.h:69
FILE * output
long val
Definition: informix.c:689
int b
Definition: isn.c:74
int x
Definition: isn.c:75
int a
Definition: isn.c:73
int j
Definition: isn.c:78
int i
Definition: isn.c:77
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
Datum jsonb_in(PG_FUNCTION_ARGS)
Definition: jsonb.c:73
List * lappend(List *list, void *datum)
Definition: list.c:339
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
List * lcons(void *datum, List *list)
Definition: list.c:495
List * list_delete_last(List *list)
Definition: list.c:957
List * list_truncate(List *list, int new_size)
Definition: list.c:631
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2899
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2795
List * get_op_index_interpretation(Oid opno)
Definition: lsyscache.c:672
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:3221
char get_typtype(Oid typid)
Definition: lsyscache.c:2769
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2678
Oid getBaseType(Oid typid)
Definition: lsyscache.c:2661
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2927
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition: lsyscache.c:2850
#define type_is_array(typid)
Definition: lsyscache.h:214
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:420
JsonBehavior * makeJsonBehavior(JsonBehaviorType btype, Node *expr, int location)
Definition: makefuncs.c:955
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:48
Var * makeWholeRowVar(RangeTblEntry *rte, int varno, Index varlevelsup, bool allowScalar)
Definition: makefuncs.c:137
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:408
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:473
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:289
FuncExpr * makeFuncExpr(Oid funcid, Oid rettype, List *args, Oid funccollid, Oid inputcollid, CoercionForm fformat)
Definition: makefuncs.c:594
JsonValueExpr * makeJsonValueExpr(Expr *raw_expr, Expr *formatted_expr, JsonFormat *format)
Definition: makefuncs.c:938
JsonFormat * makeJsonFormat(JsonFormatType type, JsonEncoding encoding, int location)
Definition: makefuncs.c:922
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:350
Node * makeJsonIsPredicate(Node *expr, JsonFormat *format, JsonValueType item_type, bool unique_keys, int location)
Definition: makefuncs.c:986
char * pstrdup(const char *in)
Definition: mcxt.c:2325
void * palloc(Size size)
Definition: mcxt.c:1943
void namestrcpy(Name name, const char *str)
Definition: name.c:233
char * NameListToString(const List *names)
Definition: namespace.c:3594
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:301
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:821
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1388
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:763
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:153
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define copyObject(obj)
Definition: nodes.h:230
#define nodeTag(nodeptr)
Definition: nodes.h:139
@ CMD_SELECT
Definition: nodes.h:271
@ AGGSPLIT_SIMPLE
Definition: nodes.h:383
#define NodeSetTag(nodeptr, t)
Definition: nodes.h:162
#define makeNode(_type_)
Definition: nodes.h:161
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
Node * transformGroupingFunc(ParseState *pstate, GroupingFunc *p)
Definition: parse_agg.c:265
void transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, WindowDef *windef)
Definition: parse_agg.c:825
void transformAggregateCall(ParseState *pstate, Aggref *agg, List *args, List *aggorder, bool agg_distinct)
Definition: parse_agg.c:109
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
TYPCATEGORY TypeCategory(Oid type)
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
bool verify_common_type(Oid common_type, List *exprs)
int parser_coercion_errposition(ParseState *pstate, int coerce_location, Node *input_expr)
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:157
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:78
void assign_expr_collations(ParseState *pstate, Node *expr)
static Node * transformMergeSupportFunc(ParseState *pstate, MergeSupportFunc *f)
Definition: parse_expr.c:1376
static Node * make_nulltest_from_distinct(ParseState *pstate, A_Expr *distincta, Node *arg)
Definition: parse_expr.c:3105
static void unknown_attribute(ParseState *pstate, Node *relref, const char *attname, int location)
Definition: parse_expr.c:390
static Node * transformJsonArrayConstructor(ParseState *pstate, JsonArrayConstructor *ctor)
Definition: parse_expr.c:4018
static Node * transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr)
Definition: parse_expr.c:2566
static Node * transformAExprOpAll(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1016
static Node * makeJsonByteaToTextConversion(Node *expr, JsonFormat *format, int location)
Definition: parse_expr.c:3274
static Node * transformJsonParseExpr(ParseState *pstate, JsonParseExpr *jsexpr)
Definition: parse_expr.c:4161
static Node * transformAExprIn(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1124
static JsonReturning * transformJsonOutput(ParseState *pstate, const JsonOutput *output, bool allow_format)
Definition: parse_expr.c:3508
static void checkJsonOutputFormat(ParseState *pstate, const JsonFormat *format, Oid targettype, bool allow_format_for_non_strings)
Definition: parse_expr.c:3458
static Node * transformJsonParseArg(ParseState *pstate, Node *jsexpr, JsonFormat *format, Oid *exprtype)
Definition: parse_expr.c:4048
static Node * makeJsonConstructorExpr(ParseState *pstate, JsonConstructorType type, List *args, Expr *fexpr, JsonReturning *returning, bool unique, bool absent_on_null, int location)
Definition: parse_expr.c:3662
static Node * transformAExprOpAny(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1002
static Node * transformXmlSerialize(ParseState *pstate, XmlSerialize *xs)
Definition: parse_expr.c:2482
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:136
static Node * transformAExprNullIf(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1081
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:118
static Node * transformJsonScalarExpr(ParseState *pstate, JsonScalarExpr *jsexpr)
Definition: parse_expr.c:4210
static bool exprIsNullConstant(Node *arg)
Definition: parse_expr.c:908
static Expr * make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)
Definition: parse_expr.c:3070
static Node * transformJsonArrayQueryConstructor(ParseState *pstate, JsonArrayQueryConstructor *ctor)
Definition: parse_expr.c:3759
static Node * transformSQLValueFunction(ParseState *pstate, SQLValueFunction *svf)
Definition: parse_expr.c:2300
static Node * transformColumnRef(ParseState *pstate, ColumnRef *cref)
Definition: parse_expr.c:508
static Node * transformCollateClause(ParseState *pstate, CollateClause *c)
Definition: parse_expr.c:2784
static Node * transformBoolExpr(ParseState *pstate, BoolExpr *a)
Definition: parse_expr.c:1401
static bool ValidJsonBehaviorDefaultExpr(Node *expr, void *context)
Definition: parse_expr.c:4671
static Node * transformFuncCall(ParseState *pstate, FuncCall *fn)
Definition: parse_expr.c:1437
static JsonReturning * transformJsonConstructorOutput(ParseState *pstate, JsonOutput *output, List *args)
Definition: parse_expr.c:3555
static Node * transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
Definition: parse_expr.c:2261
static Node * transformJsonAggConstructor(ParseState *pstate, JsonAggConstructor *agg_ctor, JsonReturning *returning, List *args, Oid aggfnoid, Oid aggtype, JsonConstructorType ctor_type, bool unique, bool absent_on_null)
Definition: parse_expr.c:3830
static Node * transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c)
Definition: parse_expr.c:2212
bool Transform_null_equals
Definition: parse_expr.c:44
static Node * transformSubLink(ParseState *pstate, SubLink *sublink)
Definition: parse_expr.c:1770
static void transformJsonPassingArgs(ParseState *pstate, const char *constructName, JsonFormatType format, List *args, List **passing_values, List **passing_names)
Definition: parse_expr.c:4645
static Node * transformJsonObjectConstructor(ParseState *pstate, JsonObjectConstructor *ctor)
Definition: parse_expr.c:3722
static Node * make_row_comparison_op(ParseState *pstate, List *opname, List *largs, List *rargs, int location)
Definition: parse_expr.c:2824
static JsonReturning * transformJsonReturning(ParseState *pstate, JsonOutput *output, const char *fname)
Definition: parse_expr.c:4121
static Node * transformAExprOp(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:921
static Node * GetJsonBehaviorConst(JsonBehaviorType btype, int location)
Definition: parse_expr.c:4843
static JsonBehavior * transformJsonBehavior(ParseState *pstate, JsonBehavior *behavior, JsonBehaviorType default_behavior, JsonReturning *returning)
Definition: parse_expr.c:4704
static Node * transformJsonArrayAgg(ParseState *pstate, JsonArrayAgg *agg)
Definition: parse_expr.c:3980
static Node * transformArrayExpr(ParseState *pstate, A_ArrayExpr *a, Oid array_type, Oid element_type, int32 typmod)
Definition: parse_expr.c:2013
static Node * transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref)
Definition: parse_expr.c:1482
static Node * transformBooleanTest(ParseState *pstate, BooleanTest *b)
Definition: parse_expr.c:2526
static Node * make_row_distinct_op(ParseState *pstate, List *opname, RowExpr *lrow, RowExpr *rrow, int location)
Definition: parse_expr.c:3026
static Node * transformTypeCast(ParseState *pstate, TypeCast *tc)
Definition: parse_expr.c:2700
static Node * transformJsonValueExpr(ParseState *pstate, const char *constructName, JsonValueExpr *ve, JsonFormatType default_format, Oid targettype, bool isarg)
Definition: parse_expr.c:3296
static Node * transformParamRef(ParseState *pstate, ParamRef *pref)
Definition: parse_expr.c:884
static Node * transformCaseExpr(ParseState *pstate, CaseExpr *c)
Definition: parse_expr.c:1630
static Node * transformIndirection(ParseState *pstate, A_Indirection *ind)
Definition: parse_expr.c:436
static Node * coerceJsonFuncExpr(ParseState *pstate, Node *expr, const JsonReturning *returning, bool report_error)
Definition: parse_expr.c:3598
static Node * transformJsonSerializeExpr(ParseState *pstate, JsonSerializeExpr *expr)
Definition: parse_expr.c:4233
static Node * transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
Definition: parse_expr.c:2174
static Node * transformXmlExpr(ParseState *pstate, XmlExpr *x)
Definition: parse_expr.c:2353
static Const * getJsonEncodingConst(JsonFormat *format)
Definition: parse_expr.c:3235
static Node * transformAExprBetween(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1282
static Node * transformWholeRowRef(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location)
Definition: parse_expr.c:2618
const char * ParseExprKindName(ParseExprKind exprKind)
Definition: parse_expr.c:3129
static Node * transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
Definition: parse_expr.c:4279
static Node * transformJsonIsPredicate(ParseState *pstate, JsonIsPredicate *pred)
Definition: parse_expr.c:4098
static Node * transformJsonObjectAgg(ParseState *pstate, JsonObjectAgg *agg)
Definition: parse_expr.c:3916
static Node * transformAExprDistinct(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1030
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, Node *last_srf, FuncCall *fn, bool proc_call, int location)
Definition: parse_func.c:90
void free_parsestate(ParseState *pstate)
Definition: parse_node.c:72
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
SubscriptingRef * transformContainerSubscripts(ParseState *pstate, Node *containerBase, Oid containerType, int32 containerTypMod, List *indirection, bool isAssignment)
Definition: parse_node.c:243
Const * make_const(ParseState *pstate, A_Const *aconst)
Definition: parse_node.c:347
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:39
ParseExprKind
Definition: parse_node.h:39
@ EXPR_KIND_EXECUTE_PARAMETER
Definition: parse_node.h:76
@ EXPR_KIND_DOMAIN_CHECK
Definition: parse_node.h:69
@ EXPR_KIND_COPY_WHERE
Definition: parse_node.h:82
@ EXPR_KIND_COLUMN_DEFAULT
Definition: parse_node.h:70
@ EXPR_KIND_DISTINCT_ON
Definition: parse_node.h:61
@ EXPR_KIND_MERGE_WHEN
Definition: parse_node.h:58
@ EXPR_KIND_STATS_EXPRESSION
Definition: parse_node.h:74
@ EXPR_KIND_INDEX_EXPRESSION
Definition: parse_node.h:72
@ EXPR_KIND_MERGE_RETURNING
Definition: parse_node.h:65
@ EXPR_KIND_PARTITION_BOUND
Definition: parse_node.h:79
@ EXPR_KIND_FUNCTION_DEFAULT
Definition: parse_node.h:71
@ EXPR_KIND_WINDOW_FRAME_RANGE
Definition: parse_node.h:51
@ EXPR_KIND_VALUES
Definition: parse_node.h:66
@ EXPR_KIND_FROM_SUBSELECT
Definition: parse_node.h:44
@ EXPR_KIND_POLICY
Definition: parse_node.h:78
@ EXPR_KIND_WINDOW_FRAME_GROUPS
Definition: parse_node.h:53
@ EXPR_KIND_PARTITION_EXPRESSION
Definition: parse_node.h:80
@ EXPR_KIND_JOIN_USING
Definition: parse_node.h:43
@ EXPR_KIND_INDEX_PREDICATE
Definition: parse_node.h:73
@ EXPR_KIND_ORDER_BY
Definition: parse_node.h:60
@ EXPR_KIND_OFFSET
Definition: parse_node.h:63
@ EXPR_KIND_JOIN_ON
Definition: parse_node.h:42
@ EXPR_KIND_HAVING
Definition: parse_node.h:47
@ EXPR_KIND_INSERT_TARGET
Definition: parse_node.h:55
@ EXPR_KIND_ALTER_COL_TRANSFORM
Definition: parse_node.h:75
@ EXPR_KIND_LIMIT
Definition: parse_node.h:62
@ EXPR_KIND_WHERE
Definition: parse_node.h:46
@ EXPR_KIND_UPDATE_TARGET
Definition: parse_node.h:57
@ EXPR_KIND_SELECT_TARGET
Definition: parse_node.h:54
@ EXPR_KIND_RETURNING
Definition: parse_node.h:64
@ EXPR_KIND_GENERATED_COLUMN
Definition: parse_node.h:83
@ EXPR_KIND_NONE
Definition: parse_node.h:40
@ EXPR_KIND_CALL_ARGUMENT
Definition: parse_node.h:81
@ EXPR_KIND_GROUP_BY
Definition: parse_node.h:59
@ EXPR_KIND_OTHER
Definition: parse_node.h:41
@ EXPR_KIND_FROM_FUNCTION
Definition: parse_node.h:45
@ EXPR_KIND_TRIGGER_WHEN
Definition: parse_node.h:77
@ EXPR_KIND_FILTER
Definition: parse_node.h:48
@ EXPR_KIND_UPDATE_SOURCE
Definition: parse_node.h:56
@ EXPR_KIND_CHECK_CONSTRAINT
Definition: parse_node.h:68
@ EXPR_KIND_WINDOW_PARTITION
Definition: parse_node.h:49
@ EXPR_KIND_CYCLE_MARK
Definition: parse_node.h:84
@ EXPR_KIND_WINDOW_FRAME_ROWS
Definition: parse_node.h:52
@ EXPR_KIND_WINDOW_ORDER
Definition: parse_node.h:50
@ EXPR_KIND_VALUES_SINGLE
Definition: parse_node.h:67
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition: parse_oper.c:660
Expr * make_scalar_array_op(ParseState *pstate, List *opname, bool useOr, Node *ltree, Node *rtree, int location)
Definition: parse_oper.c:770
void markNullableIfNeeded(ParseState *pstate, Var *var)
void errorMissingColumn(ParseState *pstate, const char *relname, const char *colname, int location)
Node * colNameToVar(ParseState *pstate, const char *colname, bool localonly, int location)
void markVarForSelectPriv(ParseState *pstate, Var *var)
void errorMissingRTE(ParseState *pstate, RangeVar *relation)
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, VarReturningType returning_type, int location, bool include_dropped, List **colnames, List **colvars)
Node * scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
List * transformExpressionList(ParseState *pstate, List *exprlist, ParseExprKind exprKind, bool allowDefault)
Definition: parse_target.c:220
char * FigureColname(Node *node)
Oid LookupCollation(ParseState *pstate, List *collnames, int location)
Definition: parse_type.c:515
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:310
#define ISCOMPLEX(typeid)
Definition: parse_type.h:59
@ JS_QUOTES_OMIT
Definition: parsenodes.h:1826
@ AEXPR_BETWEEN
Definition: parsenodes.h:334
@ AEXPR_NULLIF
Definition: parsenodes.h:329
@ AEXPR_NOT_DISTINCT
Definition: parsenodes.h:328
@ AEXPR_BETWEEN_SYM
Definition: parsenodes.h:336
@ AEXPR_NOT_BETWEEN_SYM
Definition: parsenodes.h:337
@ AEXPR_ILIKE
Definition: parsenodes.h:332
@ AEXPR_IN
Definition: parsenodes.h:330
@ AEXPR_NOT_BETWEEN
Definition: parsenodes.h:335
@ AEXPR_DISTINCT
Definition: parsenodes.h:327
@ AEXPR_SIMILAR
Definition: parsenodes.h:333
@ AEXPR_LIKE
Definition: parsenodes.h:331
@ AEXPR_OP
Definition: parsenodes.h:324
@ AEXPR_OP_ANY
Definition: parsenodes.h:325
@ AEXPR_OP_ALL
Definition: parsenodes.h:326
Query * parse_sub_analyze(Node *parseTree, ParseState *parentParseState, CommonTableExpr *parentCTE, bool locked_from_parent, bool resolve_unknowns)
Definition: analyze.c:222
Query * transformStmt(ParseState *pstate, Node *parseTree)
Definition: analyze.c:396
NameData attname
Definition: pg_attribute.h:41
void * arg
static char format
NameData relname
Definition: pg_class.h:38
#define NAMEDATALEN
const void size_t len
int32 encoding
Definition: pg_database.h:41
#define lfirst(lc)
Definition: pg_list.h:172
#define llast(l)
Definition: pg_list.h:198
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
#define lthird(l)
Definition: pg_list.h:188
#define list_make1(x1)
Definition: pg_list.h:212
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
#define lfourth(l)
Definition: pg_list.h:193
#define list_make2(x1, x2)
Definition: pg_list.h:214
#define snprintf
Definition: port.h:239
uintptr_t Datum
Definition: postgres.h:69
static Datum BoolGetDatum(bool X)
Definition: postgres.h:107
static Datum NameGetDatum(const NameData *X)
Definition: postgres.h:378
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:355
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30
char * c
e
Definition: preproc-init.c:82
@ IS_NOT_TRUE
Definition: primnodes.h:1981
@ IS_NOT_FALSE
Definition: primnodes.h:1981
@ IS_NOT_UNKNOWN
Definition: primnodes.h:1981
@ IS_TRUE
Definition: primnodes.h:1981
@ IS_UNKNOWN
Definition: primnodes.h:1981
@ IS_FALSE
Definition: primnodes.h:1981
@ ARRAY_SUBLINK
Definition: primnodes.h:1020
@ MULTIEXPR_SUBLINK
Definition: primnodes.h:1019
@ EXPR_SUBLINK
Definition: primnodes.h:1018
@ ROWCOMPARE_SUBLINK
Definition: primnodes.h:1017
@ EXISTS_SUBLINK
Definition: primnodes.h:1014
JsonFormatType
Definition: primnodes.h:1642
@ JS_FORMAT_JSONB
Definition: primnodes.h:1645
@ JS_FORMAT_DEFAULT
Definition: primnodes.h:1643
@ JS_FORMAT_JSON
Definition: primnodes.h:1644
@ IS_GREATEST
Definition: primnodes.h:1507
@ AND_EXPR
Definition: primnodes.h:948
@ OR_EXPR
Definition: primnodes.h:948
@ NOT_EXPR
Definition: primnodes.h:948
JsonEncoding
Definition: primnodes.h:1630
@ JS_ENC_DEFAULT
Definition: primnodes.h:1631
@ JS_ENC_UTF32
Definition: primnodes.h:1634
@ JS_ENC_UTF8
Definition: primnodes.h:1632
@ JS_ENC_UTF16
Definition: primnodes.h:1633
@ SVFOP_CURRENT_CATALOG
Definition: primnodes.h:1554
@ SVFOP_LOCALTIME_N
Definition: primnodes.h:1547
@ SVFOP_CURRENT_TIMESTAMP
Definition: primnodes.h:1544
@ SVFOP_LOCALTIME
Definition: primnodes.h:1546
@ SVFOP_CURRENT_TIMESTAMP_N
Definition: primnodes.h:1545
@ SVFOP_CURRENT_ROLE
Definition: primnodes.h:1550
@ SVFOP_USER
Definition: primnodes.h:1552
@ SVFOP_CURRENT_SCHEMA
Definition: primnodes.h:1555
@ SVFOP_LOCALTIMESTAMP_N
Definition: primnodes.h:1549
@ SVFOP_CURRENT_DATE
Definition: primnodes.h:1541
@ SVFOP_CURRENT_TIME_N
Definition: primnodes.h:1543
@ SVFOP_CURRENT_TIME
Definition: primnodes.h:1542
@ SVFOP_LOCALTIMESTAMP
Definition: primnodes.h:1548
@ SVFOP_CURRENT_USER
Definition: primnodes.h:1551
@ SVFOP_SESSION_USER
Definition: primnodes.h:1553
@ PARAM_MULTIEXPR
Definition: primnodes.h:387
@ PARAM_EXTERN
Definition: primnodes.h:384
@ PARAM_SUBLINK
Definition: primnodes.h:386
@ JSW_UNCONDITIONAL
Definition: primnodes.h:1758
@ JSW_CONDITIONAL
Definition: primnodes.h:1757
@ IS_DOCUMENT
Definition: primnodes.h:1592
@ IS_XMLFOREST
Definition: primnodes.h:1587
@ IS_XMLCONCAT
Definition: primnodes.h:1585
@ IS_XMLPI
Definition: primnodes.h:1589
@ IS_XMLPARSE
Definition: primnodes.h:1588
@ IS_XMLSERIALIZE
Definition: primnodes.h:1591
@ IS_XMLROOT
Definition: primnodes.h:1590
@ IS_XMLELEMENT
Definition: primnodes.h:1586
@ VAR_RETURNING_DEFAULT
Definition: primnodes.h:256
JsonBehaviorType
Definition: primnodes.h:1769
@ JSON_BEHAVIOR_ERROR
Definition: primnodes.h:1771
@ JSON_BEHAVIOR_TRUE
Definition: primnodes.h:1773
@ JSON_BEHAVIOR_DEFAULT
Definition: primnodes.h:1778
@ JSON_BEHAVIOR_EMPTY
Definition: primnodes.h:1772
@ JSON_BEHAVIOR_FALSE
Definition: primnodes.h:1774
@ JSON_BEHAVIOR_NULL
Definition: primnodes.h:1770
@ JSON_BEHAVIOR_EMPTY_OBJECT
Definition: primnodes.h:1777
@ JSON_BEHAVIOR_UNKNOWN
Definition: primnodes.h:1775
@ JSON_BEHAVIOR_EMPTY_ARRAY
Definition: primnodes.h:1776
@ JSON_QUERY_OP
Definition: primnodes.h:1808
@ JSON_TABLE_OP
Definition: primnodes.h:1810
@ JSON_EXISTS_OP
Definition: primnodes.h:1807
@ JSON_VALUE_OP
Definition: primnodes.h:1809
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:753
@ COERCE_EXPLICIT_CAST
Definition: primnodes.h:752
@ COERCE_EXPLICIT_CALL
Definition: primnodes.h:751
@ IS_NULL
Definition: primnodes.h:1957
@ IS_NOT_NULL
Definition: primnodes.h:1957
@ COERCION_ASSIGNMENT
Definition: primnodes.h:732
@ COERCION_EXPLICIT
Definition: primnodes.h:734
@ COERCION_IMPLICIT
Definition: primnodes.h:731
JsonConstructorType
Definition: primnodes.h:1694
@ JSCTOR_JSON_SERIALIZE
Definition: primnodes.h:1701
@ JSCTOR_JSON_ARRAYAGG
Definition: primnodes.h:1698
@ JSCTOR_JSON_PARSE
Definition: primnodes.h:1699
@ JSCTOR_JSON_OBJECT
Definition: primnodes.h:1695
@ JSCTOR_JSON_SCALAR
Definition: primnodes.h:1700
@ JSCTOR_JSON_ARRAY
Definition: primnodes.h:1696
@ JSCTOR_JSON_OBJECTAGG
Definition: primnodes.h:1697
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:743
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412
static chr element(struct vars *v, const chr *startp, const chr *endp)
Definition: regc_locale.c:376
void check_stack_depth(void)
Definition: stack_depth.c:95
bool isnull
Definition: parsenodes.h:374
ParseLoc location
Definition: parsenodes.h:375
ParseLoc location
Definition: parsenodes.h:349
A_Expr_Kind kind
Definition: parsenodes.h:345
Oid aggfnoid
Definition: primnodes.h:461
Expr * aggfilter
Definition: primnodes.h:494
ParseLoc location
Definition: primnodes.h:524
char * aliasname
Definition: primnodes.h:51
List * colnames
Definition: primnodes.h:52
ParseLoc location
Definition: primnodes.h:1401
Expr * arg
Definition: primnodes.h:1330
ParseLoc location
Definition: primnodes.h:1333
Expr * defresult
Definition: primnodes.h:1332
List * args
Definition: primnodes.h:1331
Expr * result
Definition: primnodes.h:1343
Expr * expr
Definition: primnodes.h:1342
ParseLoc location
Definition: primnodes.h:1344
List * args
Definition: primnodes.h:1497
ParseLoc location
Definition: primnodes.h:1499
Expr * arg
Definition: primnodes.h:1296
ParseLoc location
Definition: primnodes.h:1298
ParseLoc location
Definition: parsenodes.h:306
List * fields
Definition: parsenodes.h:305
char * cursor_name
Definition: primnodes.h:2103
ParseLoc location
Definition: primnodes.h:787
struct WindowDef * over
Definition: parsenodes.h:2017
JsonOutput * output
Definition: parsenodes.h:2014
bool absent_on_null
Definition: parsenodes.h:2043
JsonValueExpr * arg
Definition: parsenodes.h:2042
JsonAggConstructor * constructor
Definition: parsenodes.h:2041
JsonOutput * output
Definition: parsenodes.h:1987
Node * expr
Definition: primnodes.h:1796
ParseLoc location
Definition: primnodes.h:1798
JsonBehaviorType btype
Definition: primnodes.h:1795
JsonReturning * returning
Definition: primnodes.h:1715
JsonConstructorType type
Definition: primnodes.h:1711
char * column_name
Definition: primnodes.h:1824
Node * formatted_expr
Definition: primnodes.h:1828
ParseLoc location
Definition: primnodes.h:1864
List * passing_values
Definition: primnodes.h:1841
JsonBehavior * on_empty
Definition: primnodes.h:1844
JsonFormat * format
Definition: primnodes.h:1831
List * passing_names
Definition: primnodes.h:1840
Node * path_spec
Definition: primnodes.h:1834
bool use_io_coercion
Definition: primnodes.h:1851
JsonReturning * returning
Definition: primnodes.h:1837
bool use_json_coercion
Definition: primnodes.h:1852
JsonWrapper wrapper
Definition: primnodes.h:1855
JsonExprOp op
Definition: primnodes.h:1822
JsonBehavior * on_error
Definition: primnodes.h:1845
bool omit_quotes
Definition: primnodes.h:1858
ParseLoc location
Definition: primnodes.h:1658
JsonEncoding encoding
Definition: primnodes.h:1657
JsonFormatType format_type
Definition: primnodes.h:1656
JsonOutput * output
Definition: parsenodes.h:1843
char * column_name
Definition: parsenodes.h:1838
JsonWrapper wrapper
Definition: parsenodes.h:1846
JsonQuotes quotes
Definition: parsenodes.h:1847
JsonExprOp op
Definition: parsenodes.h:1837
List * passing
Definition: parsenodes.h:1842
JsonBehavior * on_empty
Definition: parsenodes.h:1844
ParseLoc location
Definition: parsenodes.h:1848
Node * pathspec
Definition: parsenodes.h:1841
JsonBehavior * on_error
Definition: parsenodes.h:1845
JsonValueExpr * context_item
Definition: parsenodes.h:1840
JsonFormat * format
Definition: primnodes.h:1741
JsonValueType item_type
Definition: primnodes.h:1742
ParseLoc location
Definition: primnodes.h:1744
JsonValueExpr * value
Definition: parsenodes.h:1925
JsonAggConstructor * constructor
Definition: parsenodes.h:2028
JsonKeyValue * arg
Definition: parsenodes.h:2029
bool absent_on_null
Definition: parsenodes.h:2030
JsonOutput * output
Definition: parsenodes.h:1973
JsonReturning * returning
Definition: parsenodes.h:1804
JsonValueExpr * expr
Definition: parsenodes.h:1935
ParseLoc location
Definition: parsenodes.h:1938
JsonOutput * output
Definition: parsenodes.h:1936
JsonFormat * format
Definition: primnodes.h:1668
ParseLoc location
Definition: parsenodes.h:1950
JsonOutput * output
Definition: parsenodes.h:1949
JsonOutput * output
Definition: parsenodes.h:1961
JsonValueExpr * expr
Definition: parsenodes.h:1960
Expr * formatted_expr
Definition: primnodes.h:1689
JsonFormat * format
Definition: primnodes.h:1690
Expr * raw_expr
Definition: primnodes.h:1688
Definition: pg_list.h:54
ParseLoc location
Definition: primnodes.h:653
List * args
Definition: primnodes.h:1523
ParseLoc location
Definition: primnodes.h:1525
MinMaxOp op
Definition: primnodes.h:1521
Expr * arg
Definition: primnodes.h:808
Definition: nodes.h:135
NullTestType nulltesttype
Definition: primnodes.h:1964
ParseLoc location
Definition: primnodes.h:1967
Expr * arg
Definition: primnodes.h:1963
List * args
Definition: primnodes.h:853
CompareType cmptype
Definition: lsyscache.h:28
ParseLoc location
Definition: parsenodes.h:316
int number
Definition: parsenodes.h:315
ParseLoc location
Definition: primnodes.h:401
int paramid
Definition: primnodes.h:394
Oid paramtype
Definition: primnodes.h:395
ParamKind paramkind
Definition: primnodes.h:393
RangeTblEntry * p_rte
Definition: parse_node.h:311
VarReturningType p_returning_type
Definition: parse_node.h:320
ParseState * parentParseState
Definition: parse_node.h:208
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:226
ParseExprKind p_expr_kind
Definition: parse_node.h:230
List * p_multiassign_exprs
Definition: parse_node.h:232
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:256
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:254
bool p_hasSubLinks
Definition: parse_node.h:245
Node * p_last_srf
Definition: parse_node.h:248
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:255
CmdType commandType
Definition: parsenodes.h:121
List * targetList
Definition: parsenodes.h:193
Node * val
Definition: parsenodes.h:530
ParseLoc location
Definition: parsenodes.h:531
List * indirection
Definition: parsenodes.h:529
char * name
Definition: parsenodes.h:528
CompareType cmptype
Definition: primnodes.h:1473
List * args
Definition: primnodes.h:1428
ParseLoc location
Definition: primnodes.h:1452
SQLValueFunctionOp op
Definition: primnodes.h:1561
Definition: value.h:64
Expr * expr
Definition: primnodes.h:2219
AttrNumber resno
Definition: primnodes.h:2221
TypeName * typeName
Definition: parsenodes.h:385
ParseLoc location
Definition: parsenodes.h:386
Node * arg
Definition: parsenodes.h:384
ParseLoc location
Definition: parsenodes.h:286
Definition: primnodes.h:262
ParseLoc location
Definition: primnodes.h:310
VarReturningType varreturningtype
Definition: primnodes.h:297
List * args
Definition: primnodes.h:592
Expr * aggfilter
Definition: primnodes.h:594
ParseLoc location
Definition: primnodes.h:604
Oid winfnoid
Definition: primnodes.h:584
List * args
Definition: primnodes.h:1613
ParseLoc location
Definition: primnodes.h:1622
bool indent
Definition: primnodes.h:1617
List * named_args
Definition: primnodes.h:1609
XmlExprOp op
Definition: primnodes.h:1605
ParseLoc location
Definition: parsenodes.h:860
TypeName * typeName
Definition: parsenodes.h:858
Node * expr
Definition: parsenodes.h:857
XmlOptionType xmloption
Definition: parsenodes.h:856
Definition: ltree.h:43
Definition: c.h:712
static void * fn(void *arg)
Definition: thread-alloc.c:119
int count_nonjunk_tlist_entries(List *tlist)
Definition: tlist.c:186
bool DomainHasConstraints(Oid type_id)
Definition: typcache.c:1489
String * makeString(char *str)
Definition: value.c:63
#define strVal(v)
Definition: value.h:82
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:444
bool contain_var_clause(Node *node)
Definition: var.c:406
const char * type
#define select(n, r, w, e, timeout)
Definition: win32_port.h:503
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2378