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