Age Owner Branch data TLA Line data Source code
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"
19 : : #include "catalog/pg_aggregate.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"
29 : : #include "parser/parse_collate.h"
30 : : #include "parser/parse_expr.h"
31 : : #include "parser/parse_func.h"
32 : : #include "parser/parse_oper.h"
33 : : #include "parser/parse_relation.h"
34 : : #include "parser/parse_target.h"
35 : : #include "parser/parse_type.h"
36 : : #include "utils/builtins.h"
37 : : #include "utils/date.h"
38 : : #include "utils/fmgroids.h"
39 : : #include "utils/lsyscache.h"
40 : : #include "utils/timestamp.h"
41 : : #include "utils/xml.h"
42 : :
43 : : /* GUC parameters */
44 : : bool Transform_null_equals = false;
45 : :
46 : :
47 : : static Node *transformExprRecurse(ParseState *pstate, Node *expr);
48 : : static Node *transformParamRef(ParseState *pstate, ParamRef *pref);
49 : : static Node *transformAExprOp(ParseState *pstate, A_Expr *a);
50 : : static Node *transformAExprOpAny(ParseState *pstate, A_Expr *a);
51 : : static Node *transformAExprOpAll(ParseState *pstate, A_Expr *a);
52 : : static Node *transformAExprDistinct(ParseState *pstate, A_Expr *a);
53 : : static Node *transformAExprNullIf(ParseState *pstate, A_Expr *a);
54 : : static Node *transformAExprIn(ParseState *pstate, A_Expr *a);
55 : : static Node *transformAExprBetween(ParseState *pstate, A_Expr *a);
56 : : static Node *transformMergeSupportFunc(ParseState *pstate, MergeSupportFunc *f);
57 : : static Node *transformBoolExpr(ParseState *pstate, BoolExpr *a);
58 : : static Node *transformFuncCall(ParseState *pstate, FuncCall *fn);
59 : : static Node *transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref);
60 : : static Node *transformCaseExpr(ParseState *pstate, CaseExpr *c);
61 : : static Node *transformSubLink(ParseState *pstate, SubLink *sublink);
62 : : static Node *transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
63 : : Oid array_type, Oid element_type, int32 typmod);
64 : : static Node *transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault);
65 : : static Node *transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c);
66 : : static Node *transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m);
67 : : static Node *transformSQLValueFunction(ParseState *pstate,
68 : : SQLValueFunction *svf);
69 : : static Node *transformXmlExpr(ParseState *pstate, XmlExpr *x);
70 : : static Node *transformXmlSerialize(ParseState *pstate, XmlSerialize *xs);
71 : : static Node *transformBooleanTest(ParseState *pstate, BooleanTest *b);
72 : : static Node *transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr);
73 : : static Node *transformColumnRef(ParseState *pstate, ColumnRef *cref);
74 : : static Node *transformWholeRowRef(ParseState *pstate,
75 : : ParseNamespaceItem *nsitem,
76 : : int sublevels_up, int location);
77 : : static Node *transformIndirection(ParseState *pstate, A_Indirection *ind);
78 : : static Node *transformTypeCast(ParseState *pstate, TypeCast *tc);
79 : : static Node *transformCollateClause(ParseState *pstate, CollateClause *c);
80 : : static Node *transformJsonObjectConstructor(ParseState *pstate,
81 : : JsonObjectConstructor *ctor);
82 : : static Node *transformJsonArrayConstructor(ParseState *pstate,
83 : : JsonArrayConstructor *ctor);
84 : : static Node *transformJsonArrayQueryConstructor(ParseState *pstate,
85 : : JsonArrayQueryConstructor *ctor);
86 : : static Node *transformJsonObjectAgg(ParseState *pstate, JsonObjectAgg *agg);
87 : : static Node *transformJsonArrayAgg(ParseState *pstate, JsonArrayAgg *agg);
88 : : static Node *transformJsonIsPredicate(ParseState *pstate, JsonIsPredicate *pred);
89 : : static Node *transformJsonParseExpr(ParseState *pstate, JsonParseExpr *jsexpr);
90 : : static Node *transformJsonScalarExpr(ParseState *pstate, JsonScalarExpr *jsexpr);
91 : : static Node *transformJsonSerializeExpr(ParseState *pstate,
92 : : JsonSerializeExpr *expr);
93 : : static Node *transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func);
94 : : static void transformJsonPassingArgs(ParseState *pstate, const char *constructName,
95 : : JsonFormatType format, List *args,
96 : : List **passing_values, List **passing_names);
97 : : static JsonBehavior *transformJsonBehavior(ParseState *pstate, JsonExpr *jsexpr,
98 : : JsonBehavior *behavior,
99 : : JsonBehaviorType default_behavior,
100 : : JsonReturning *returning);
101 : : static Node *GetJsonBehaviorConst(JsonBehaviorType btype, int location);
102 : : static Node *make_row_comparison_op(ParseState *pstate, List *opname,
103 : : List *largs, List *rargs, int location);
104 : : static Node *make_row_distinct_op(ParseState *pstate, List *opname,
105 : : RowExpr *lrow, RowExpr *rrow, int location);
106 : : static Expr *make_distinct_op(ParseState *pstate, List *opname,
107 : : Node *ltree, Node *rtree, int location);
108 : : static Node *make_nulltest_from_distinct(ParseState *pstate,
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 : : */
118 : : Node *
4826 tgl@sss.pgh.pa.us 119 :CBC 903587 : transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
120 : : {
121 : : Node *result;
122 : : ParseExprKind sv_expr_kind;
123 : :
124 : : /* Save and restore identity of expression type we're parsing */
125 [ - + ]: 903587 : Assert(exprKind != EXPR_KIND_NONE);
126 : 903587 : sv_expr_kind = pstate->p_expr_kind;
127 : 903587 : pstate->p_expr_kind = exprKind;
128 : :
129 : 903587 : result = transformExprRecurse(pstate, expr);
130 : :
131 : 900668 : pstate->p_expr_kind = sv_expr_kind;
132 : :
133 : 900668 : return result;
134 : : }
135 : :
136 : : static Node *
137 : 2403353 : transformExprRecurse(ParseState *pstate, Node *expr)
138 : : {
139 : : Node *result;
140 : :
10198 bruce@momjian.us 141 [ + + ]: 2403353 : if (expr == NULL)
142 : 16911 : return NULL;
143 : :
144 : : /* Guard against stack overflow due to overly complex expressions */
7887 tgl@sss.pgh.pa.us 145 : 2386442 : check_stack_depth();
146 : :
10198 bruce@momjian.us 147 [ + + + + : 2386442 : switch (nodeTag(expr))
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ - + + +
+ + + + +
+ + + - ]
148 : : {
8621 tgl@sss.pgh.pa.us 149 : 900042 : case T_ColumnRef:
7586 neilc@samurai.com 150 : 900042 : result = transformColumnRef(pstate, (ColumnRef *) expr);
151 : 899698 : break;
152 : :
153 : 20705 : case T_ParamRef:
154 : 20705 : result = transformParamRef(pstate, (ParamRef *) expr);
155 : 20699 : break;
156 : :
10198 bruce@momjian.us 157 : 606712 : case T_A_Const:
1509 peter@eisentraut.org 158 : 606712 : result = (Node *) make_const(pstate, (A_Const *) expr);
159 : 606700 : break;
160 : :
7810 tgl@sss.pgh.pa.us 161 : 11647 : case T_A_Indirection:
3058 162 : 11647 : result = transformIndirection(pstate, (A_Indirection *) expr);
163 : 11602 : break;
164 : :
6430 165 : 3207 : case T_A_ArrayExpr:
166 : 3207 : result = transformArrayExpr(pstate, (A_ArrayExpr *) expr,
167 : : InvalidOid, InvalidOid, -1);
168 : 3204 : break;
169 : :
9415 170 : 159860 : case T_TypeCast:
3900 171 : 159860 : result = transformTypeCast(pstate, (TypeCast *) expr);
172 : 158172 : break;
173 : :
5375 peter_e@gmx.net 174 : 4533 : case T_CollateClause:
175 : 4533 : result = transformCollateClause(pstate, (CollateClause *) expr);
176 : 4524 : break;
177 : :
10198 bruce@momjian.us 178 : 316600 : case T_A_Expr:
179 : : {
180 : 316600 : A_Expr *a = (A_Expr *) expr;
181 : :
8295 tgl@sss.pgh.pa.us 182 [ + + + + : 316600 : switch (a->kind)
+ + + +
- ]
183 : : {
184 : 294919 : case AEXPR_OP:
7586 neilc@samurai.com 185 : 294919 : result = transformAExprOp(pstate, a);
10198 bruce@momjian.us 186 : 294678 : break;
8156 tgl@sss.pgh.pa.us 187 : 8285 : case AEXPR_OP_ANY:
7586 neilc@samurai.com 188 : 8285 : result = transformAExprOpAny(pstate, a);
8156 tgl@sss.pgh.pa.us 189 : 8279 : break;
190 : 150 : case AEXPR_OP_ALL:
7586 neilc@samurai.com 191 : 150 : result = transformAExprOpAll(pstate, a);
8156 tgl@sss.pgh.pa.us 192 : 150 : break;
8295 193 : 571 : case AEXPR_DISTINCT:
194 : : case AEXPR_NOT_DISTINCT:
7586 neilc@samurai.com 195 : 571 : result = transformAExprDistinct(pstate, a);
8485 lockhart@fourpalms.o 196 : 571 : break;
8289 tgl@sss.pgh.pa.us 197 : 240 : case AEXPR_NULLIF:
7586 neilc@samurai.com 198 : 240 : result = transformAExprNullIf(pstate, a);
8289 tgl@sss.pgh.pa.us 199 : 240 : break;
7273 200 : 10766 : case AEXPR_IN:
201 : 10766 : result = transformAExprIn(pstate, a);
202 : 10760 : break;
3899 203 : 1410 : case AEXPR_LIKE:
204 : : case AEXPR_ILIKE:
205 : : case AEXPR_SIMILAR:
206 : : /* we can transform these just like AEXPR_OP */
207 : 1410 : result = transformAExprOp(pstate, a);
208 : 1407 : break;
3900 209 : 259 : case AEXPR_BETWEEN:
210 : : case AEXPR_NOT_BETWEEN:
211 : : case AEXPR_BETWEEN_SYM:
212 : : case AEXPR_NOT_BETWEEN_SYM:
213 : 259 : result = transformAExprBetween(pstate, a);
214 : 259 : break;
7586 neilc@samurai.com 215 :UBC 0 : default:
216 [ # # ]: 0 : elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
217 : : result = NULL; /* keep compiler quiet */
218 : : break;
219 : : }
10198 bruce@momjian.us 220 :CBC 316344 : break;
221 : : }
222 : :
4151 tgl@sss.pgh.pa.us 223 : 77266 : case T_BoolExpr:
224 : 77266 : result = transformBoolExpr(pstate, (BoolExpr *) expr);
225 : 77256 : break;
226 : :
10198 bruce@momjian.us 227 : 184357 : case T_FuncCall:
7586 neilc@samurai.com 228 : 184357 : result = transformFuncCall(pstate, (FuncCall *) expr);
229 : 183796 : break;
230 : :
4149 tgl@sss.pgh.pa.us 231 : 187 : case T_MultiAssignRef:
232 : 187 : result = transformMultiAssignRef(pstate, (MultiAssignRef *) expr);
233 : 184 : break;
234 : :
3817 andres@anarazel.de 235 : 181 : case T_GroupingFunc:
236 : 181 : result = transformGroupingFunc(pstate, (GroupingFunc *) expr);
237 : 181 : break;
238 : :
589 dean.a.rasheed@gmail 239 : 99 : case T_MergeSupportFunc:
240 : 99 : result = transformMergeSupportFunc(pstate,
241 : : (MergeSupportFunc *) expr);
242 : 93 : break;
243 : :
5863 tgl@sss.pgh.pa.us 244 : 23691 : case T_NamedArgExpr:
245 : : {
246 : 23691 : NamedArgExpr *na = (NamedArgExpr *) expr;
247 : :
4826 248 : 23691 : na->arg = (Expr *) transformExprRecurse(pstate, (Node *) na->arg);
5863 249 : 23691 : result = expr;
250 : 23691 : break;
251 : : }
252 : :
10143 bruce@momjian.us 253 : 24099 : case T_SubLink:
7586 neilc@samurai.com 254 : 24099 : result = transformSubLink(pstate, (SubLink *) expr);
255 : 24051 : break;
256 : :
9824 lockhart@fourpalms.o 257 : 19657 : case T_CaseExpr:
7586 neilc@samurai.com 258 : 19657 : result = transformCaseExpr(pstate, (CaseExpr *) expr);
259 : 19654 : break;
260 : :
7840 tgl@sss.pgh.pa.us 261 : 2928 : case T_RowExpr:
3261 262 : 2928 : result = transformRowExpr(pstate, (RowExpr *) expr, false);
7586 neilc@samurai.com 263 : 2928 : break;
264 : :
8289 tgl@sss.pgh.pa.us 265 : 1609 : case T_CoalesceExpr:
7586 neilc@samurai.com 266 : 1609 : result = transformCoalesceExpr(pstate, (CoalesceExpr *) expr);
267 : 1606 : break;
268 : :
7428 tgl@sss.pgh.pa.us 269 : 134 : case T_MinMaxExpr:
270 : 134 : result = transformMinMaxExpr(pstate, (MinMaxExpr *) expr);
271 : 134 : break;
272 : :
894 michael@paquier.xyz 273 : 1360 : case T_SQLValueFunction:
274 : 1360 : result = transformSQLValueFunction(pstate,
275 : : (SQLValueFunction *) expr);
276 : 1360 : break;
277 : :
6882 tgl@sss.pgh.pa.us 278 : 299 : case T_XmlExpr:
279 : 299 : result = transformXmlExpr(pstate, (XmlExpr *) expr);
280 : 284 : break;
281 : :
6841 peter_e@gmx.net 282 : 109 : case T_XmlSerialize:
283 : 109 : result = transformXmlSerialize(pstate, (XmlSerialize *) expr);
284 : 109 : break;
285 : :
8896 tgl@sss.pgh.pa.us 286 : 9113 : case T_NullTest:
287 : : {
288 : 9113 : NullTest *n = (NullTest *) expr;
289 : :
4826 290 : 9113 : n->arg = (Expr *) transformExprRecurse(pstate, (Node *) n->arg);
291 : : /* the argument can be any type, so don't coerce it */
5778 292 : 9113 : n->argisrow = type_is_rowtype(exprType((Node *) n->arg));
8896 293 : 9113 : result = expr;
294 : 9113 : break;
295 : : }
296 : :
297 : 464 : case T_BooleanTest:
7586 neilc@samurai.com 298 : 464 : result = transformBooleanTest(pstate, (BooleanTest *) expr);
299 : 464 : break;
300 : :
6713 tgl@sss.pgh.pa.us 301 : 127 : case T_CurrentOfExpr:
302 : 127 : result = transformCurrentOfExpr(pstate, (CurrentOfExpr *) expr);
303 : 127 : 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 : : */
3261 tgl@sss.pgh.pa.us 309 :UBC 0 : case T_SetToDefault:
310 [ # # ]: 0 : ereport(ERROR,
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 : : */
7894 tgl@sss.pgh.pa.us 327 :CBC 15004 : case T_CaseTestExpr:
328 : : case T_Var:
329 : : {
10077 bruce@momjian.us 330 : 15004 : result = (Node *) expr;
331 : 15004 : break;
332 : : }
333 : :
943 alvherre@alvh.no-ip. 334 : 215 : case T_JsonObjectConstructor:
335 : 215 : result = transformJsonObjectConstructor(pstate, (JsonObjectConstructor *) expr);
336 : 194 : break;
337 : :
338 : 101 : case T_JsonArrayConstructor:
339 : 101 : result = transformJsonArrayConstructor(pstate, (JsonArrayConstructor *) expr);
340 : 92 : break;
341 : :
342 : 30 : case T_JsonArrayQueryConstructor:
343 : 30 : result = transformJsonArrayQueryConstructor(pstate, (JsonArrayQueryConstructor *) expr);
344 : 21 : break;
345 : :
346 : 102 : case T_JsonObjectAgg:
347 : 102 : result = transformJsonObjectAgg(pstate, (JsonObjectAgg *) expr);
348 : 102 : break;
349 : :
350 : 99 : case T_JsonArrayAgg:
351 : 99 : result = transformJsonArrayAgg(pstate, (JsonArrayAgg *) expr);
352 : 99 : break;
353 : :
941 354 : 167 : case T_JsonIsPredicate:
355 : 167 : result = transformJsonIsPredicate(pstate, (JsonIsPredicate *) expr);
356 : 164 : break;
357 : :
830 amitlan@postgresql.o 358 : 74 : case T_JsonParseExpr:
359 : 74 : result = transformJsonParseExpr(pstate, (JsonParseExpr *) expr);
360 : 63 : break;
361 : :
362 : 49 : case T_JsonScalarExpr:
363 : 49 : result = transformJsonScalarExpr(pstate, (JsonScalarExpr *) expr);
364 : 49 : break;
365 : :
366 : 48 : case T_JsonSerializeExpr:
367 : 48 : result = transformJsonSerializeExpr(pstate, (JsonSerializeExpr *) expr);
368 : 44 : break;
369 : :
585 370 : 1567 : case T_JsonFuncExpr:
371 : 1567 : result = transformJsonFuncExpr(pstate, (JsonFuncExpr *) expr);
372 : 1486 : break;
373 : :
10198 bruce@momjian.us 374 :UBC 0 : default:
375 : : /* should not reach here */
8136 tgl@sss.pgh.pa.us 376 [ # # ]: 0 : elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
377 : : result = NULL; /* keep compiler quiet */
378 : : break;
379 : : }
380 : :
10198 bruce@momjian.us 381 :CBC 2383292 : 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 : : */
390 : : static void
2918 peter_e@gmx.net 391 : 19 : unknown_attribute(ParseState *pstate, Node *relref, const char *attname,
392 : : int location)
393 : : {
394 : : RangeTblEntry *rte;
395 : :
5840 tgl@sss.pgh.pa.us 396 [ + + ]: 19 : if (IsA(relref, Var) &&
397 [ - + ]: 6 : ((Var *) relref)->varattno == InvalidAttrNumber)
398 : : {
399 : : /* Reference the RTE by alias not by actual table name */
5840 tgl@sss.pgh.pa.us 400 :UBC 0 : rte = GetRTEByRangeTablePosn(pstate,
401 : : ((Var *) relref)->varno,
402 : 0 : ((Var *) relref)->varlevelsup);
403 [ # # ]: 0 : ereport(ERROR,
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 */
5840 tgl@sss.pgh.pa.us 412 :CBC 19 : Oid relTypeId = exprType(relref);
413 : :
414 [ + + ]: 19 : if (ISCOMPLEX(relTypeId))
415 [ + - ]: 9 : ereport(ERROR,
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 [ + - ]: 10 : else if (relTypeId == RECORDOID)
421 [ + - ]: 10 : ereport(ERROR,
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
5840 tgl@sss.pgh.pa.us 427 [ # # ]:UBC 0 : ereport(ERROR,
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 : :
436 : : static Node *
3058 tgl@sss.pgh.pa.us 437 :CBC 11647 : transformIndirection(ParseState *pstate, A_Indirection *ind)
438 : : {
439 : 11647 : Node *last_srf = pstate->p_last_srf;
440 : 11647 : Node *result = transformExprRecurse(pstate, ind->arg);
7810 441 : 11647 : List *subscripts = NIL;
3058 442 : 11647 : 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 [ + + + + : 23026 : foreach(i, ind->indirection)
+ + ]
451 : : {
7729 bruce@momjian.us 452 : 11398 : Node *n = lfirst(i);
453 : :
7810 tgl@sss.pgh.pa.us 454 [ + + ]: 11398 : if (IsA(n, A_Indices))
455 : 5779 : subscripts = lappend(subscripts, n);
6267 456 [ - + ]: 5619 : else if (IsA(n, A_Star))
457 : : {
6267 tgl@sss.pgh.pa.us 458 [ # # ]:UBC 0 : ereport(ERROR,
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 : :
7810 tgl@sss.pgh.pa.us 467 [ - + ]:CBC 5619 : Assert(IsA(n, String));
468 : :
469 : : /* process subscripts before this field selection */
470 [ + + ]: 5619 : if (subscripts)
2460 alvherre@alvh.no-ip. 471 : 68 : result = (Node *) transformContainerSubscripts(pstate,
472 : : result,
473 : : exprType(result),
474 : : exprTypmod(result),
475 : : subscripts,
476 : : false);
7810 tgl@sss.pgh.pa.us 477 : 5619 : subscripts = NIL;
478 : :
5840 479 : 5619 : newresult = ParseFuncOrColumn(pstate,
480 : 5619 : list_make1(n),
481 : 5619 : list_make1(result),
482 : : last_srf,
483 : : NULL,
484 : : false,
485 : : location);
486 [ + + ]: 5619 : if (newresult == NULL)
487 : 19 : unknown_attribute(pstate, result, strVal(n), location);
488 : 5600 : result = newresult;
489 : : }
490 : : }
491 : : /* process trailing subscripts, if any */
7810 492 [ + + ]: 11628 : if (subscripts)
2460 alvherre@alvh.no-ip. 493 : 5551 : result = (Node *) transformContainerSubscripts(pstate,
494 : : result,
495 : : exprType(result),
496 : : exprTypmod(result),
497 : : subscripts,
498 : : false);
499 : :
7810 tgl@sss.pgh.pa.us 500 : 11602 : return result;
501 : : }
502 : :
503 : : /*
504 : : * Transform a ColumnRef.
505 : : *
506 : : * If you find yourself changing this code, see also ExpandColumnRefStar.
507 : : */
508 : : static Node *
8621 509 : 900042 : transformColumnRef(ParseState *pstate, ColumnRef *cref)
510 : : {
5840 511 : 900042 : Node *node = NULL;
512 : 900042 : char *nspname = NULL;
513 : 900042 : char *relname = NULL;
514 : 900042 : 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 : 900042 : } 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 : : */
2406 michael@paquier.xyz 531 : 900042 : err = NULL;
532 [ - + + + : 900042 : switch (pstate->p_expr_kind)
- ]
533 : : {
2406 michael@paquier.xyz 534 :UBC 0 : case EXPR_KIND_NONE:
535 : 0 : Assert(false); /* can't happen */
536 : : break;
2406 michael@paquier.xyz 537 :CBC 900000 : case EXPR_KIND_OTHER:
538 : : case EXPR_KIND_JOIN_ON:
539 : : case EXPR_KIND_JOIN_USING:
540 : : case EXPR_KIND_FROM_SUBSELECT:
541 : : case EXPR_KIND_FROM_FUNCTION:
542 : : case EXPR_KIND_WHERE:
543 : : case EXPR_KIND_POLICY:
544 : : case EXPR_KIND_HAVING:
545 : : case EXPR_KIND_FILTER:
546 : : case EXPR_KIND_WINDOW_PARTITION:
547 : : case EXPR_KIND_WINDOW_ORDER:
548 : : case EXPR_KIND_WINDOW_FRAME_RANGE:
549 : : case EXPR_KIND_WINDOW_FRAME_ROWS:
550 : : case EXPR_KIND_WINDOW_FRAME_GROUPS:
551 : : case EXPR_KIND_SELECT_TARGET:
552 : : case EXPR_KIND_INSERT_TARGET:
553 : : case EXPR_KIND_UPDATE_SOURCE:
554 : : case EXPR_KIND_UPDATE_TARGET:
555 : : case EXPR_KIND_MERGE_WHEN:
556 : : case EXPR_KIND_GROUP_BY:
557 : : case EXPR_KIND_ORDER_BY:
558 : : case EXPR_KIND_DISTINCT_ON:
559 : : case EXPR_KIND_LIMIT:
560 : : case EXPR_KIND_OFFSET:
561 : : case EXPR_KIND_RETURNING:
562 : : case EXPR_KIND_MERGE_RETURNING:
563 : : case EXPR_KIND_VALUES:
564 : : case EXPR_KIND_VALUES_SINGLE:
565 : : case EXPR_KIND_CHECK_CONSTRAINT:
566 : : case EXPR_KIND_DOMAIN_CHECK:
567 : : case EXPR_KIND_FUNCTION_DEFAULT:
568 : : case EXPR_KIND_INDEX_EXPRESSION:
569 : : case EXPR_KIND_INDEX_PREDICATE:
570 : : case EXPR_KIND_STATS_EXPRESSION:
571 : : case EXPR_KIND_ALTER_COL_TRANSFORM:
572 : : case EXPR_KIND_EXECUTE_PARAMETER:
573 : : case EXPR_KIND_TRIGGER_WHEN:
574 : : case EXPR_KIND_PARTITION_EXPRESSION:
575 : : case EXPR_KIND_CALL_ARGUMENT:
576 : : case EXPR_KIND_COPY_WHERE:
577 : : case EXPR_KIND_GENERATED_COLUMN:
578 : : case EXPR_KIND_CYCLE_MARK:
579 : : /* okay */
580 : 900000 : break;
581 : :
582 : 12 : case EXPR_KIND_COLUMN_DEFAULT:
583 : 12 : err = _("cannot use column reference in DEFAULT expression");
584 : 12 : break;
585 : 30 : case EXPR_KIND_PARTITION_BOUND:
586 : 30 : err = _("cannot use column reference in partition bound expression");
587 : 30 : 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 [ + + ]: 900042 : if (err)
598 [ + - ]: 42 : ereport(ERROR,
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 : : */
5840 tgl@sss.pgh.pa.us 607 [ + + ]: 900000 : if (pstate->p_pre_columnref_hook != NULL)
608 : : {
2972 peter_e@gmx.net 609 : 20661 : node = pstate->p_pre_columnref_hook(pstate, cref);
5840 tgl@sss.pgh.pa.us 610 [ + + ]: 20661 : if (node != NULL)
611 : 406 : 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 [ + + + - : 899594 : switch (list_length(cref->fields))
- ]
638 : : {
8621 639 : 372101 : case 1:
640 : : {
6267 641 : 372101 : Node *field1 = (Node *) linitial(cref->fields);
642 : :
5840 643 : 372101 : colname = strVal(field1);
644 : :
645 : : /* Try to identify as an unqualified column */
646 : 372101 : node = colNameToVar(pstate, colname, false, cref->location);
647 : :
8454 bruce@momjian.us 648 [ + + ]: 372068 : 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 : : */
2132 tgl@sss.pgh.pa.us 661 : 19630 : nsitem = refnameNamespaceItem(pstate, NULL, colname,
662 : : cref->location,
663 : : &levels_up);
664 [ + + ]: 19630 : if (nsitem)
665 : 3671 : node = transformWholeRowRef(pstate, nsitem, levels_up,
666 : : cref->location);
667 : : }
8454 bruce@momjian.us 668 : 372068 : break;
669 : : }
670 : 527451 : case 2:
671 : : {
6267 tgl@sss.pgh.pa.us 672 : 527451 : Node *field1 = (Node *) linitial(cref->fields);
673 : 527451 : Node *field2 = (Node *) lsecond(cref->fields);
674 : :
5840 675 : 527451 : relname = strVal(field1);
676 : :
677 : : /* Locate the referenced nsitem */
2132 678 : 527451 : nsitem = refnameNamespaceItem(pstate, nspname, relname,
679 : : cref->location,
680 : : &levels_up);
681 [ + + ]: 527439 : if (nsitem == NULL)
682 : : {
5840 683 : 2768 : crerr = CRERR_NO_RTE;
684 : 2768 : break;
685 : : }
686 : :
687 : : /* Whole-row reference? */
6267 688 [ + + ]: 524671 : if (IsA(field2, A_Star))
689 : : {
2132 690 : 719 : node = transformWholeRowRef(pstate, nsitem, levels_up,
691 : : cref->location);
8454 bruce@momjian.us 692 : 719 : break;
693 : : }
694 : :
5840 tgl@sss.pgh.pa.us 695 : 523952 : colname = strVal(field2);
696 : :
697 : : /* Try to identify as a column of the nsitem */
2132 698 : 523952 : node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
699 : : cref->location);
8454 bruce@momjian.us 700 [ + + ]: 523949 : if (node == NULL)
701 : : {
702 : : /* Try it as a function call on the whole row */
2132 tgl@sss.pgh.pa.us 703 : 84 : node = transformWholeRowRef(pstate, nsitem, levels_up,
704 : : cref->location);
8454 bruce@momjian.us 705 : 84 : node = ParseFuncOrColumn(pstate,
5840 tgl@sss.pgh.pa.us 706 : 84 : list_make1(makeString(colname)),
7820 neilc@samurai.com 707 : 84 : list_make1(node),
708 : : pstate->p_last_srf,
709 : : NULL,
710 : : false,
711 : : cref->location);
712 : : }
8621 tgl@sss.pgh.pa.us 713 : 523949 : break;
714 : : }
715 : 42 : case 3:
716 : : {
6267 717 : 42 : Node *field1 = (Node *) linitial(cref->fields);
718 : 42 : Node *field2 = (Node *) lsecond(cref->fields);
719 : 42 : Node *field3 = (Node *) lthird(cref->fields);
720 : :
5840 721 : 42 : nspname = strVal(field1);
722 : 42 : relname = strVal(field2);
723 : :
724 : : /* Locate the referenced nsitem */
2132 725 : 42 : nsitem = refnameNamespaceItem(pstate, nspname, relname,
726 : : cref->location,
727 : : &levels_up);
728 [ + + ]: 42 : if (nsitem == NULL)
729 : : {
5840 730 : 33 : crerr = CRERR_NO_RTE;
731 : 33 : break;
732 : : }
733 : :
734 : : /* Whole-row reference? */
6267 735 [ + + ]: 9 : if (IsA(field3, A_Star))
736 : : {
2132 737 : 3 : node = transformWholeRowRef(pstate, nsitem, levels_up,
738 : : cref->location);
8454 bruce@momjian.us 739 : 3 : break;
740 : : }
741 : :
5840 tgl@sss.pgh.pa.us 742 : 6 : colname = strVal(field3);
743 : :
744 : : /* Try to identify as a column of the nsitem */
2132 745 : 6 : node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
746 : : cref->location);
8454 bruce@momjian.us 747 [ - + ]: 6 : if (node == NULL)
748 : : {
749 : : /* Try it as a function call on the whole row */
2132 tgl@sss.pgh.pa.us 750 :UBC 0 : node = transformWholeRowRef(pstate, nsitem, levels_up,
751 : : cref->location);
8454 bruce@momjian.us 752 : 0 : node = ParseFuncOrColumn(pstate,
5840 tgl@sss.pgh.pa.us 753 : 0 : list_make1(makeString(colname)),
7820 neilc@samurai.com 754 : 0 : list_make1(node),
755 : : pstate->p_last_srf,
756 : : NULL,
757 : : false,
758 : : cref->location);
759 : : }
8454 bruce@momjian.us 760 :CBC 6 : break;
761 : : }
8621 tgl@sss.pgh.pa.us 762 :UBC 0 : case 4:
763 : : {
6267 764 : 0 : Node *field1 = (Node *) linitial(cref->fields);
765 : 0 : Node *field2 = (Node *) lsecond(cref->fields);
766 : 0 : Node *field3 = (Node *) lthird(cref->fields);
767 : 0 : Node *field4 = (Node *) lfourth(cref->fields);
768 : : char *catname;
769 : :
5840 770 : 0 : catname = strVal(field1);
771 : 0 : nspname = strVal(field2);
772 : 0 : relname = strVal(field3);
773 : :
774 : : /*
775 : : * We check the catalog name and then ignore it.
776 : : */
777 [ # # ]: 0 : if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
778 : : {
779 : 0 : crerr = CRERR_WRONG_DB;
780 : 0 : break;
781 : : }
782 : :
783 : : /* Locate the referenced nsitem */
2132 784 : 0 : nsitem = refnameNamespaceItem(pstate, nspname, relname,
785 : : cref->location,
786 : : &levels_up);
787 [ # # ]: 0 : if (nsitem == NULL)
788 : : {
5840 789 : 0 : crerr = CRERR_NO_RTE;
790 : 0 : break;
791 : : }
792 : :
793 : : /* Whole-row reference? */
6267 794 [ # # ]: 0 : if (IsA(field4, A_Star))
795 : : {
2132 796 : 0 : node = transformWholeRowRef(pstate, nsitem, levels_up,
797 : : cref->location);
8454 bruce@momjian.us 798 : 0 : break;
799 : : }
800 : :
5840 tgl@sss.pgh.pa.us 801 : 0 : colname = strVal(field4);
802 : :
803 : : /* Try to identify as a column of the nsitem */
2132 804 : 0 : node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
805 : : cref->location);
8454 bruce@momjian.us 806 [ # # ]: 0 : if (node == NULL)
807 : : {
808 : : /* Try it as a function call on the whole row */
2132 tgl@sss.pgh.pa.us 809 : 0 : node = transformWholeRowRef(pstate, nsitem, levels_up,
810 : : cref->location);
8454 bruce@momjian.us 811 : 0 : node = ParseFuncOrColumn(pstate,
5840 tgl@sss.pgh.pa.us 812 : 0 : list_make1(makeString(colname)),
7820 neilc@samurai.com 813 : 0 : list_make1(node),
814 : : pstate->p_last_srf,
815 : : NULL,
816 : : false,
817 : : cref->location);
818 : : }
8454 bruce@momjian.us 819 : 0 : break;
820 : : }
8621 tgl@sss.pgh.pa.us 821 : 0 : default:
3050 822 : 0 : crerr = CRERR_TOO_MANY; /* too many dotted names */
8621 823 : 0 : 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 : : */
5840 tgl@sss.pgh.pa.us 835 [ + + ]:CBC 899546 : if (pstate->p_post_columnref_hook != NULL)
836 : : {
837 : : Node *hookresult;
838 : :
2972 peter_e@gmx.net 839 : 24744 : hookresult = pstate->p_post_columnref_hook(pstate, cref, node);
5840 tgl@sss.pgh.pa.us 840 [ + + ]: 24726 : if (node == NULL)
841 : 18563 : node = hookresult;
842 [ - + ]: 6163 : else if (hookresult != NULL)
7586 neilc@samurai.com 843 [ # # ]:UBC 0 : ereport(ERROR,
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 : : */
5840 tgl@sss.pgh.pa.us 853 [ + + ]:CBC 899528 : if (node == NULL)
854 : : {
855 [ + + - - : 236 : switch (crerr)
- ]
856 : : {
857 : 182 : case CRERR_NO_COLUMN:
4829 858 : 182 : errorMissingColumn(pstate, relname, colname, cref->location);
859 : : break;
5840 860 : 54 : case CRERR_NO_RTE:
861 : 54 : errorMissingRTE(pstate, makeRangeVar(nspname, relname,
862 : : cref->location));
863 : : break;
5840 tgl@sss.pgh.pa.us 864 :UBC 0 : case CRERR_WRONG_DB:
865 [ # # ]: 0 : ereport(ERROR,
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 : 0 : case CRERR_TOO_MANY:
872 [ # # ]: 0 : ereport(ERROR,
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 : :
5840 tgl@sss.pgh.pa.us 881 :CBC 899292 : return node;
882 : : }
883 : :
884 : : static Node *
6713 885 : 20705 : transformParamRef(ParseState *pstate, ParamRef *pref)
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 : : */
5840 893 [ + + ]: 20705 : if (pstate->p_paramref_hook != NULL)
2972 peter_e@gmx.net 894 : 20702 : result = pstate->p_paramref_hook(pstate, pref);
895 : : else
5840 tgl@sss.pgh.pa.us 896 : 3 : result = NULL;
897 : :
898 [ + + ]: 20705 : if (result == NULL)
899 [ + - ]: 6 : ereport(ERROR,
900 : : (errcode(ERRCODE_UNDEFINED_PARAMETER),
901 : : errmsg("there is no parameter $%d", pref->number),
902 : : parser_errposition(pstate, pref->location)));
903 : :
904 : 20699 : return result;
905 : : }
906 : :
907 : : /* Test whether an a_expr is a plain NULL constant or not */
908 : : static bool
6573 909 : 1127 : exprIsNullConstant(Node *arg)
910 : : {
911 [ + - + + ]: 1127 : if (arg && IsA(arg, A_Const))
912 : : {
6556 bruce@momjian.us 913 : 56 : A_Const *con = (A_Const *) arg;
914 : :
1509 peter@eisentraut.org 915 [ + + ]: 56 : if (con->isnull)
6573 tgl@sss.pgh.pa.us 916 : 15 : return true;
917 : : }
918 : 1112 : return false;
919 : : }
920 : :
921 : : static Node *
7586 neilc@samurai.com 922 : 296329 : transformAExprOp(ParseState *pstate, A_Expr *a)
923 : : {
924 : 296329 : Node *lexpr = a->lexpr;
925 : 296329 : 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 : : */
935 [ - + - - ]: 296329 : if (Transform_null_equals &&
7586 neilc@samurai.com 936 :UBC 0 : list_length(a->name) == 1 &&
937 [ # # # # ]: 0 : strcmp(strVal(linitial(a->name)), "=") == 0 &&
5133 heikki.linnakangas@i 938 [ # # ]: 0 : (exprIsNullConstant(lexpr) || exprIsNullConstant(rexpr)) &&
1990 tgl@sss.pgh.pa.us 939 [ # # # # ]: 0 : (!IsA(lexpr, CaseTestExpr) && !IsA(rexpr, CaseTestExpr)))
7586 neilc@samurai.com 940 : 0 : {
941 : 0 : NullTest *n = makeNode(NullTest);
942 : :
943 : 0 : n->nulltesttype = IS_NULL;
3900 tgl@sss.pgh.pa.us 944 : 0 : n->location = a->location;
945 : :
7586 neilc@samurai.com 946 [ # # ]: 0 : if (exprIsNullConstant(lexpr))
947 : 0 : n->arg = (Expr *) rexpr;
948 : : else
949 : 0 : n->arg = (Expr *) lexpr;
950 : :
4826 tgl@sss.pgh.pa.us 951 : 0 : result = transformExprRecurse(pstate, (Node *) n);
952 : : }
7586 neilc@samurai.com 953 [ + + + + :CBC 296329 : else if (lexpr && IsA(lexpr, RowExpr) &&
+ - ]
954 [ + + ]: 408 : rexpr && IsA(rexpr, SubLink) &&
955 [ + - ]: 15 : ((SubLink *) rexpr)->subLinkType == EXPR_SUBLINK)
956 : 15 : {
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 : 15 : SubLink *s = (SubLink *) rexpr;
963 : :
7243 tgl@sss.pgh.pa.us 964 : 15 : s->subLinkType = ROWCOMPARE_SUBLINK;
965 : 15 : s->testexpr = lexpr;
7586 neilc@samurai.com 966 : 15 : s->operName = a->name;
6269 tgl@sss.pgh.pa.us 967 : 15 : s->location = a->location;
4826 968 : 15 : result = transformExprRecurse(pstate, (Node *) s);
969 : : }
7586 neilc@samurai.com 970 [ + + + + : 296314 : else if (lexpr && IsA(lexpr, RowExpr) &&
+ - ]
971 [ + + ]: 393 : rexpr && IsA(rexpr, RowExpr))
972 : : {
973 : : /* ROW() op ROW() is handled specially */
4826 tgl@sss.pgh.pa.us 974 : 390 : lexpr = transformExprRecurse(pstate, lexpr);
975 : 390 : rexpr = transformExprRecurse(pstate, rexpr);
976 : :
7243 977 : 390 : result = make_row_comparison_op(pstate,
978 : : a->name,
3170 peter_e@gmx.net 979 : 390 : castNode(RowExpr, lexpr)->args,
980 : 390 : castNode(RowExpr, rexpr)->args,
981 : : a->location);
982 : : }
983 : : else
984 : : {
985 : : /* Ordinary scalar operator */
3058 tgl@sss.pgh.pa.us 986 : 295924 : Node *last_srf = pstate->p_last_srf;
987 : :
4826 988 : 295924 : lexpr = transformExprRecurse(pstate, lexpr);
989 : 295788 : rexpr = transformExprRecurse(pstate, rexpr);
990 : :
7586 neilc@samurai.com 991 : 295745 : result = (Node *) make_op(pstate,
992 : : a->name,
993 : : lexpr,
994 : : rexpr,
995 : : last_srf,
996 : : a->location);
997 : : }
998 : :
999 : 296085 : return result;
1000 : : }
1001 : :
1002 : : static Node *
1003 : 8285 : transformAExprOpAny(ParseState *pstate, A_Expr *a)
1004 : : {
1784 tgl@sss.pgh.pa.us 1005 : 8285 : Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1006 : 8285 : Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1007 : :
7586 neilc@samurai.com 1008 : 8285 : return (Node *) make_scalar_array_op(pstate,
1009 : : a->name,
1010 : : true,
1011 : : lexpr,
1012 : : rexpr,
1013 : : a->location);
1014 : : }
1015 : :
1016 : : static Node *
1017 : 150 : transformAExprOpAll(ParseState *pstate, A_Expr *a)
1018 : : {
1784 tgl@sss.pgh.pa.us 1019 : 150 : Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1020 : 150 : Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1021 : :
7586 neilc@samurai.com 1022 : 150 : return (Node *) make_scalar_array_op(pstate,
1023 : : a->name,
1024 : : false,
1025 : : lexpr,
1026 : : rexpr,
1027 : : a->location);
1028 : : }
1029 : :
1030 : : static Node *
1031 : 571 : transformAExprDistinct(ParseState *pstate, A_Expr *a)
1032 : : {
3883 tgl@sss.pgh.pa.us 1033 : 571 : Node *lexpr = a->lexpr;
1034 : 571 : 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 : : */
3378 1042 [ + + ]: 571 : if (exprIsNullConstant(rexpr))
1043 : 15 : return make_nulltest_from_distinct(pstate, a, lexpr);
1044 [ - + ]: 556 : if (exprIsNullConstant(lexpr))
3378 tgl@sss.pgh.pa.us 1045 :UBC 0 : return make_nulltest_from_distinct(pstate, a, rexpr);
1046 : :
3883 tgl@sss.pgh.pa.us 1047 :CBC 556 : lexpr = transformExprRecurse(pstate, lexpr);
1048 : 556 : rexpr = transformExprRecurse(pstate, rexpr);
1049 : :
7586 neilc@samurai.com 1050 [ + - + + : 556 : if (lexpr && IsA(lexpr, RowExpr) &&
+ - ]
1051 [ + - ]: 3 : rexpr && IsA(rexpr, RowExpr))
1052 : : {
1053 : : /* ROW() op ROW() is handled specially */
3378 tgl@sss.pgh.pa.us 1054 : 3 : 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 : 553 : 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 [ + + ]: 556 : if (a->kind == AEXPR_NOT_DISTINCT)
1074 : 28 : result = (Node *) makeBoolExpr(NOT_EXPR,
1075 : 28 : list_make1(result),
1076 : : a->location);
1077 : :
1078 : 556 : return result;
1079 : : }
1080 : :
1081 : : static Node *
7586 neilc@samurai.com 1082 : 240 : transformAExprNullIf(ParseState *pstate, A_Expr *a)
1083 : : {
4826 tgl@sss.pgh.pa.us 1084 : 240 : Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1085 : 240 : Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1086 : : OpExpr *result;
1087 : :
5336 1088 : 240 : 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 [ - + ]: 240 : if (result->opresulttype != BOOLOID)
7586 neilc@samurai.com 1099 [ # # ]:UBC 0 : 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)));
3058 tgl@sss.pgh.pa.us 1104 [ - + ]:CBC 240 : if (result->opretset)
3058 tgl@sss.pgh.pa.us 1105 [ # # ]:UBC 0 : 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 : : */
5336 tgl@sss.pgh.pa.us 1114 :CBC 240 : result->opresulttype = exprType((Node *) linitial(result->args));
1115 : :
1116 : : /*
1117 : : * We rely on NullIfExpr and OpExpr being the same struct
1118 : : */
7586 neilc@samurai.com 1119 : 240 : NodeSetTag(result, T_NullIfExpr);
1120 : :
5336 tgl@sss.pgh.pa.us 1121 : 240 : return (Node *) result;
1122 : : }
1123 : :
1124 : : static Node *
7273 1125 : 10766 : transformAExprIn(ParseState *pstate, A_Expr *a)
1126 : : {
6210 1127 : 10766 : 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 : : */
7273 1138 [ + + ]: 10766 : if (strcmp(strVal(linitial(a->name)), "<>") == 0)
1139 : 1344 : useOr = false;
1140 : : else
1141 : 9422 : 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 : : */
4826 1154 : 10766 : lexpr = transformExprRecurse(pstate, a->lexpr);
6210 1155 : 10766 : rexprs = rvars = rnonvars = NIL;
7273 1156 [ + - + + : 40201 : foreach(l, (List *) a->rexpr)
+ + ]
1157 : : {
4826 1158 : 29438 : Node *rexpr = transformExprRecurse(pstate, lfirst(l));
1159 : :
7273 1160 : 29435 : rexprs = lappend(rexprs, rexpr);
6210 1161 [ - + ]: 29435 : if (contain_vars_of_level(rexpr, 0))
6210 tgl@sss.pgh.pa.us 1162 :UBC 0 : rvars = lappend(rvars, rexpr);
1163 : : else
6210 tgl@sss.pgh.pa.us 1164 :CBC 29435 : 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 : : */
4523 1171 [ + + ]: 10763 : 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 : : */
6210 1184 : 9272 : allexprs = list_concat(list_make1(lexpr), rnonvars);
6211 1185 : 9272 : scalar_type = select_common_type(pstate, allexprs, NULL, NULL);
1186 : :
1187 : : /* We have to verify that the selected type actually works */
1367 1188 [ + + ]: 9272 : if (OidIsValid(scalar_type) &&
1189 [ + + ]: 9270 : !verify_common_type(scalar_type, allexprs))
1190 : 3 : 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 : : */
4523 1198 [ + + + + ]: 9272 : if (OidIsValid(scalar_type) && scalar_type != RECORDOID)
6211 1199 : 9254 : array_type = get_array_type(scalar_type);
1200 : : else
1201 : 18 : array_type = InvalidOid;
7273 1202 [ + + ]: 9272 : 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 : 9248 : aexprs = NIL;
6210 1212 [ + - + + : 37144 : foreach(l, rnonvars)
+ + ]
1213 : : {
7273 1214 : 27896 : Node *rexpr = (Node *) lfirst(l);
1215 : :
1216 : 27896 : rexpr = coerce_to_common_type(pstate, rexpr,
1217 : : scalar_type,
1218 : : "IN");
1219 : 27896 : aexprs = lappend(aexprs, rexpr);
1220 : : }
1221 : 9248 : newa = makeNode(ArrayExpr);
1222 : 9248 : newa->array_typeid = array_type;
1223 : : /* array_collid will be set by parse_collate.c */
1224 : 9248 : newa->element_typeid = scalar_type;
1225 : 9248 : newa->elements = aexprs;
1226 : 9248 : newa->multidims = false;
137 alvherre@kurilemu.de 1227 : 9248 : newa->list_start = a->rexpr_list_start;
1228 : 9248 : newa->list_end = a->rexpr_list_end;
6269 tgl@sss.pgh.pa.us 1229 : 9248 : newa->location = -1;
1230 : :
6210 1231 : 9248 : 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 : 9248 : rexprs = rvars;
1240 : : }
1241 : : }
1242 : :
1243 : : /*
1244 : : * Must do it the hard way, ie, with a boolean expression tree.
1245 : : */
7273 1246 [ + + + + : 12296 : foreach(l, rexprs)
+ + ]
1247 : : {
1248 : 1536 : Node *rexpr = (Node *) lfirst(l);
1249 : : Node *cmp;
1250 : :
4523 1251 [ + + ]: 1536 : if (IsA(lexpr, RowExpr) &&
1252 [ + - ]: 26 : IsA(rexpr, RowExpr))
1253 : : {
1254 : : /* ROW() op ROW() is handled specially */
7243 1255 : 26 : cmp = make_row_comparison_op(pstate,
1256 : : a->name,
3050 1257 : 26 : copyObject(((RowExpr *) lexpr)->args),
1258 : : ((RowExpr *) rexpr)->args,
1259 : : a->location);
1260 : : }
1261 : : else
1262 : : {
1263 : : /* Ordinary scalar operator */
7273 1264 : 1510 : cmp = (Node *) make_op(pstate,
1265 : : a->name,
1266 : 1510 : copyObject(lexpr),
1267 : : rexpr,
1268 : : pstate->p_last_srf,
1269 : : a->location);
1270 : : }
1271 : :
1272 : 1533 : cmp = coerce_to_boolean(pstate, cmp, "IN");
1273 [ + + ]: 1533 : if (result == NULL)
1274 : 1512 : result = cmp;
1275 : : else
1276 : 21 : result = (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
6269 1277 : 21 : list_make2(result, cmp),
1278 : : a->location);
1279 : : }
1280 : :
7273 1281 : 10760 : return result;
1282 : : }
1283 : :
1284 : : static Node *
3900 1285 : 259 : transformAExprBetween(ParseState *pstate, A_Expr *a)
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 : 259 : aexpr = a->lexpr;
3170 peter_e@gmx.net 1297 : 259 : args = castNode(List, a->rexpr);
3900 tgl@sss.pgh.pa.us 1298 [ - + ]: 259 : Assert(list_length(args) == 2);
1299 : 259 : bexpr = (Node *) linitial(args);
1300 : 259 : 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 [ + + + + : 259 : switch (a->kind)
- ]
1314 : : {
1315 : 241 : case AEXPR_BETWEEN:
1316 : 241 : args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1317 : : aexpr, bexpr,
1318 : : a->location),
1319 : : makeSimpleA_Expr(AEXPR_OP, "<=",
1320 : : copyObject(aexpr), cexpr,
1321 : : a->location));
1322 : 241 : result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1323 : 241 : break;
1324 : 6 : case AEXPR_NOT_BETWEEN:
1325 : 6 : args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1326 : : aexpr, bexpr,
1327 : : a->location),
1328 : : makeSimpleA_Expr(AEXPR_OP, ">",
1329 : : copyObject(aexpr), cexpr,
1330 : : a->location));
1331 : 6 : result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1332 : 6 : break;
1333 : 6 : case AEXPR_BETWEEN_SYM:
1334 : 6 : args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1335 : : aexpr, bexpr,
1336 : : a->location),
1337 : : makeSimpleA_Expr(AEXPR_OP, "<=",
1338 : : copyObject(aexpr), cexpr,
1339 : : a->location));
1340 : 6 : sub1 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1341 : 6 : args = list_make2(makeSimpleA_Expr(AEXPR_OP, ">=",
1342 : : copyObject(aexpr), copyObject(cexpr),
1343 : : a->location),
1344 : : makeSimpleA_Expr(AEXPR_OP, "<=",
1345 : : copyObject(aexpr), copyObject(bexpr),
1346 : : a->location));
1347 : 6 : sub2 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1348 : 6 : args = list_make2(sub1, sub2);
1349 : 6 : result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1350 : 6 : break;
1351 : 6 : case AEXPR_NOT_BETWEEN_SYM:
1352 : 6 : args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1353 : : aexpr, bexpr,
1354 : : a->location),
1355 : : makeSimpleA_Expr(AEXPR_OP, ">",
1356 : : copyObject(aexpr), cexpr,
1357 : : a->location));
1358 : 6 : sub1 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1359 : 6 : args = list_make2(makeSimpleA_Expr(AEXPR_OP, "<",
1360 : : copyObject(aexpr), copyObject(cexpr),
1361 : : a->location),
1362 : : makeSimpleA_Expr(AEXPR_OP, ">",
1363 : : copyObject(aexpr), copyObject(bexpr),
1364 : : a->location));
1365 : 6 : sub2 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1366 : 6 : args = list_make2(sub1, sub2);
1367 : 6 : result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1368 : 6 : break;
3900 tgl@sss.pgh.pa.us 1369 :UBC 0 : default:
1370 [ # # ]: 0 : elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
1371 : : result = NULL; /* keep compiler quiet */
1372 : : break;
1373 : : }
1374 : :
3900 tgl@sss.pgh.pa.us 1375 :CBC 259 : return transformExprRecurse(pstate, result);
1376 : : }
1377 : :
1378 : : static Node *
589 dean.a.rasheed@gmail 1379 : 99 : transformMergeSupportFunc(ParseState *pstate, MergeSupportFunc *f)
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 : : */
1385 [ + + ]: 99 : if (pstate->p_expr_kind != EXPR_KIND_MERGE_RETURNING)
1386 : : {
1387 : 9 : ParseState *parent_pstate = pstate->parentParseState;
1388 : :
1389 [ + + ]: 9 : while (parent_pstate &&
1390 [ - + ]: 3 : parent_pstate->p_expr_kind != EXPR_KIND_MERGE_RETURNING)
589 dean.a.rasheed@gmail 1391 :UBC 0 : parent_pstate = parent_pstate->parentParseState;
1392 : :
589 dean.a.rasheed@gmail 1393 [ + + ]:CBC 9 : if (!parent_pstate)
1394 [ + - ]: 6 : 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 : 93 : return (Node *) f;
1401 : : }
1402 : :
1403 : : static Node *
4151 tgl@sss.pgh.pa.us 1404 : 77266 : transformBoolExpr(ParseState *pstate, BoolExpr *a)
1405 : : {
1406 : 77266 : List *args = NIL;
1407 : : const char *opname;
1408 : : ListCell *lc;
1409 : :
1410 [ + + + - ]: 77266 : switch (a->boolop)
1411 : : {
1412 : 63073 : case AND_EXPR:
1413 : 63073 : opname = "AND";
1414 : 63073 : break;
1415 : 5993 : case OR_EXPR:
1416 : 5993 : opname = "OR";
1417 : 5993 : break;
1418 : 8200 : case NOT_EXPR:
1419 : 8200 : opname = "NOT";
1420 : 8200 : break;
4151 tgl@sss.pgh.pa.us 1421 :UBC 0 : default:
1422 [ # # ]: 0 : elog(ERROR, "unrecognized boolop: %d", (int) a->boolop);
1423 : : opname = NULL; /* keep compiler quiet */
1424 : : break;
1425 : : }
1426 : :
4151 tgl@sss.pgh.pa.us 1427 [ + - + + :CBC 279760 : foreach(lc, a->args)
+ + ]
1428 : : {
1429 : 202504 : Node *arg = (Node *) lfirst(lc);
1430 : :
1431 : 202504 : arg = transformExprRecurse(pstate, arg);
1432 : 202494 : arg = coerce_to_boolean(pstate, arg, opname);
1433 : 202494 : args = lappend(args, arg);
1434 : : }
1435 : :
1436 : 77256 : return (Node *) makeBoolExpr(a->boolop, args, a->location);
1437 : : }
1438 : :
1439 : : static Node *
7586 neilc@samurai.com 1440 : 184357 : transformFuncCall(ParseState *pstate, FuncCall *fn)
1441 : : {
3058 tgl@sss.pgh.pa.us 1442 : 184357 : Node *last_srf = pstate->p_last_srf;
1443 : : List *targs;
1444 : : ListCell *args;
1445 : :
1446 : : /* Transform the list of arguments ... */
6312 1447 : 184357 : targs = NIL;
1448 [ + + + + : 491902 : foreach(args, fn->args)
+ + ]
1449 : : {
4826 1450 : 307545 : targs = lappend(targs, transformExprRecurse(pstate,
1451 : 307575 : (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 : : */
4326 1462 [ + + ]: 184327 : if (fn->agg_within_group)
1463 : : {
1464 [ - + ]: 171 : Assert(fn->agg_order != NIL);
1465 [ + - + + : 372 : foreach(args, fn->agg_order)
+ + ]
1466 : : {
1467 : 201 : SortBy *arg = (SortBy *) lfirst(args);
1468 : :
1469 : 201 : targs = lappend(targs, transformExpr(pstate, arg->node,
1470 : : EXPR_KIND_ORDER_BY));
1471 : : }
1472 : : }
1473 : :
1474 : : /* ... and hand off to ParseFuncOrColumn */
5569 1475 : 184327 : return ParseFuncOrColumn(pstate,
1476 : : fn->funcname,
1477 : : targs,
1478 : : last_srf,
1479 : : fn,
1480 : : false,
1481 : : fn->location);
1482 : : }
1483 : :
1484 : : static Node *
4149 1485 : 187 : transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref)
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 */
1493 [ - + ]: 187 : Assert(pstate->p_expr_kind == EXPR_KIND_UPDATE_SOURCE);
1494 : :
1495 : : /* We only need to transform the source if this is the first column */
1496 [ + + ]: 187 : 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 : : */
3261 1504 [ + + ]: 91 : if (IsA(maref->source, SubLink) &&
1505 [ + - ]: 69 : ((SubLink *) maref->source)->subLinkType == EXPR_SUBLINK)
1506 : : {
1507 : : /* Relabel it as a MULTIEXPR_SUBLINK */
1508 : 69 : sublink = (SubLink *) maref->source;
1509 : 69 : sublink->subLinkType = MULTIEXPR_SUBLINK;
1510 : : /* And transform it */
1511 : 69 : sublink = (SubLink *) transformExprRecurse(pstate,
1512 : : (Node *) sublink);
1513 : :
3196 1514 : 69 : qtree = castNode(Query, sublink->subselect);
1515 : :
1516 : : /* Check subquery returns required number of columns */
3261 1517 [ - + ]: 69 : if (count_nonjunk_tlist_entries(qtree->targetList) != maref->ncolumns)
3261 tgl@sss.pgh.pa.us 1518 [ # # ]:UBC 0 : 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 : : */
3261 tgl@sss.pgh.pa.us 1530 :CBC 69 : tle = makeTargetEntry((Expr *) sublink, 0, NULL, true);
1531 : 69 : pstate->p_multiassign_exprs = lappend(pstate->p_multiassign_exprs,
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 : 69 : sublink->subLinkId = list_length(pstate->p_multiassign_exprs);
1540 : : }
1541 [ + + ]: 22 : else if (IsA(maref->source, RowExpr))
1542 : : {
1543 : : /* Transform the RowExpr, allowing SetToDefault items */
1544 : 19 : rexpr = (RowExpr *) transformRowExpr(pstate,
1545 : 19 : (RowExpr *) maref->source,
1546 : : true);
1547 : :
1548 : : /* Check it returns required number of columns */
1549 [ - + ]: 19 : if (list_length(rexpr->args) != maref->ncolumns)
3261 tgl@sss.pgh.pa.us 1550 [ # # ]:UBC 0 : 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 : : */
3261 tgl@sss.pgh.pa.us 1559 :CBC 19 : tle = makeTargetEntry((Expr *) rexpr, 0, NULL, true);
1560 : 19 : pstate->p_multiassign_exprs = lappend(pstate->p_multiassign_exprs,
1561 : : tle);
1562 : : }
1563 : : else
1564 [ + - ]: 3 : 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 : : */
4149 1576 [ - + ]: 96 : Assert(pstate->p_multiassign_exprs != NIL);
1577 : 96 : tle = (TargetEntry *) llast(pstate->p_multiassign_exprs);
1578 : : }
1579 : :
1580 : : /*
1581 : : * Emit the appropriate output expression for the current column
1582 : : */
3261 1583 [ + + ]: 184 : if (IsA(tle->expr, SubLink))
1584 : : {
1585 : : Param *param;
1586 : :
4149 1587 : 140 : sublink = (SubLink *) tle->expr;
1588 [ - + ]: 140 : Assert(sublink->subLinkType == MULTIEXPR_SUBLINK);
3196 1589 : 140 : qtree = castNode(Query, sublink->subselect);
1590 : :
1591 : : /* Build a Param representing the current subquery output column */
3261 1592 : 140 : tle = (TargetEntry *) list_nth(qtree->targetList, maref->colno - 1);
1593 [ - + ]: 140 : Assert(!tle->resjunk);
1594 : :
1595 : 140 : param = makeNode(Param);
1596 : 140 : param->paramkind = PARAM_MULTIEXPR;
1597 : 140 : param->paramid = (sublink->subLinkId << 16) | maref->colno;
1598 : 140 : param->paramtype = exprType((Node *) tle->expr);
1599 : 140 : param->paramtypmod = exprTypmod((Node *) tle->expr);
1600 : 140 : param->paramcollid = exprCollation((Node *) tle->expr);
1601 : 140 : param->location = exprLocation((Node *) tle->expr);
1602 : :
1603 : 140 : return (Node *) param;
1604 : : }
1605 : :
1606 [ + - ]: 44 : if (IsA(tle->expr, RowExpr))
1607 : : {
1608 : : Node *result;
1609 : :
1610 : 44 : rexpr = (RowExpr *) tle->expr;
1611 : :
1612 : : /* Just extract and return the next element of the RowExpr */
1613 : 44 : 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 [ + + ]: 44 : if (maref->colno == maref->ncolumns)
1622 : 19 : pstate->p_multiassign_exprs =
1831 drowley@postgresql.o 1623 : 19 : list_delete_last(pstate->p_multiassign_exprs);
1624 : :
3261 tgl@sss.pgh.pa.us 1625 : 44 : return result;
1626 : : }
1627 : :
3261 tgl@sss.pgh.pa.us 1628 [ # # ]:UBC 0 : elog(ERROR, "unexpected expr type in multiassign list");
1629 : : return NULL; /* keep compiler quiet */
1630 : : }
1631 : :
1632 : : static Node *
7586 neilc@samurai.com 1633 :CBC 19657 : transformCaseExpr(ParseState *pstate, CaseExpr *c)
1634 : : {
3058 tgl@sss.pgh.pa.us 1635 : 19657 : CaseExpr *newc = makeNode(CaseExpr);
1636 : 19657 : 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 */
4826 1646 : 19657 : arg = transformExprRecurse(pstate, (Node *) c->arg);
1647 : :
1648 : : /* generate placeholder for test expression */
7586 neilc@samurai.com 1649 [ + + ]: 19657 : 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 [ + + ]: 3376 : if (exprType(arg) == UNKNOWNOID)
1659 : 3 : 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 : : */
5336 tgl@sss.pgh.pa.us 1667 : 3376 : assign_expr_collations(pstate, arg);
1668 : :
7586 neilc@samurai.com 1669 : 3376 : placeholder = makeNode(CaseTestExpr);
1670 : 3376 : placeholder->typeId = exprType(arg);
1671 : 3376 : placeholder->typeMod = exprTypmod(arg);
5375 peter_e@gmx.net 1672 : 3376 : placeholder->collation = exprCollation(arg);
1673 : : }
1674 : : else
7586 neilc@samurai.com 1675 : 16281 : placeholder = NULL;
1676 : :
1677 : 19657 : newc->arg = (Expr *) arg;
1678 : :
1679 : : /* transform the list of arguments */
1680 : 19657 : newargs = NIL;
6269 tgl@sss.pgh.pa.us 1681 : 19657 : resultexprs = NIL;
7586 neilc@samurai.com 1682 [ + - + + : 53961 : foreach(l, c->args)
+ + ]
1683 : : {
3122 tgl@sss.pgh.pa.us 1684 : 34304 : CaseWhen *w = lfirst_node(CaseWhen, l);
7586 neilc@samurai.com 1685 : 34304 : CaseWhen *neww = makeNode(CaseWhen);
1686 : : Node *warg;
1687 : :
1688 : 34304 : warg = (Node *) w->expr;
1689 [ + + ]: 34304 : if (placeholder)
1690 : : {
1691 : : /* shorthand form was specified, so expand... */
1692 : 12867 : warg = (Node *) makeSimpleA_Expr(AEXPR_OP, "=",
1693 : : (Node *) placeholder,
1694 : : warg,
1695 : : w->location);
1696 : : }
4826 tgl@sss.pgh.pa.us 1697 : 34304 : neww->expr = (Expr *) transformExprRecurse(pstate, warg);
1698 : :
7586 neilc@samurai.com 1699 : 68608 : neww->expr = (Expr *) coerce_to_boolean(pstate,
1700 : 34304 : (Node *) neww->expr,
1701 : : "CASE/WHEN");
1702 : :
1703 : 34304 : warg = (Node *) w->result;
4826 tgl@sss.pgh.pa.us 1704 : 34304 : neww->result = (Expr *) transformExprRecurse(pstate, warg);
6269 1705 : 34304 : neww->location = w->location;
1706 : :
7586 neilc@samurai.com 1707 : 34304 : newargs = lappend(newargs, neww);
6269 tgl@sss.pgh.pa.us 1708 : 34304 : resultexprs = lappend(resultexprs, neww->result);
1709 : : }
1710 : :
7586 neilc@samurai.com 1711 : 19657 : newc->args = newargs;
1712 : :
1713 : : /* transform the default clause */
1714 : 19657 : defresult = (Node *) c->defresult;
1715 [ + + ]: 19657 : if (defresult == NULL)
1716 : : {
1717 : 4930 : A_Const *n = makeNode(A_Const);
1718 : :
1509 peter@eisentraut.org 1719 : 4930 : n->isnull = true;
6269 tgl@sss.pgh.pa.us 1720 : 4930 : n->location = -1;
7586 neilc@samurai.com 1721 : 4930 : defresult = (Node *) n;
1722 : : }
4826 tgl@sss.pgh.pa.us 1723 : 19657 : 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 : : */
6269 1730 : 19657 : resultexprs = lcons(newc->defresult, resultexprs);
1731 : :
1732 : 19657 : ptype = select_common_type(pstate, resultexprs, "CASE", NULL);
7586 neilc@samurai.com 1733 [ - + ]: 19657 : Assert(OidIsValid(ptype));
1734 : 19657 : newc->casetype = ptype;
1735 : : /* casecollid will be set by parse_collate.c */
1736 : :
1737 : : /* Convert default result clause, if necessary */
1738 : 19657 : newc->defresult = (Expr *)
1739 : 19657 : coerce_to_common_type(pstate,
1740 : 19657 : (Node *) newc->defresult,
1741 : : ptype,
1742 : : "CASE/ELSE");
1743 : :
1744 : : /* Convert when-clause results, if necessary */
1745 [ + - + + : 53961 : foreach(l, newc->args)
+ + ]
1746 : : {
1747 : 34304 : CaseWhen *w = (CaseWhen *) lfirst(l);
1748 : :
1749 : 34304 : w->result = (Expr *)
1750 : 34304 : coerce_to_common_type(pstate,
1751 : 34304 : (Node *) w->result,
1752 : : ptype,
1753 : : "CASE/WHEN");
1754 : : }
1755 : :
1756 : : /* if any subexpression contained a SRF, complain */
3058 tgl@sss.pgh.pa.us 1757 [ + + ]: 19657 : if (pstate->p_last_srf != last_srf)
1758 [ + - ]: 3 : 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 : :
6269 1767 : 19654 : newc->location = c->location;
1768 : :
7586 neilc@samurai.com 1769 : 19654 : return (Node *) newc;
1770 : : }
1771 : :
1772 : : static Node *
1773 : 24099 : transformSubLink(ParseState *pstate, SubLink *sublink)
1774 : : {
1775 : 24099 : 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 : : */
4826 tgl@sss.pgh.pa.us 1784 : 24099 : err = NULL;
1785 [ - - + - : 24099 : switch (pstate->p_expr_kind)
+ - - - -
- - + + -
+ + - ]
1786 : : {
4826 tgl@sss.pgh.pa.us 1787 :UBC 0 : case EXPR_KIND_NONE:
1788 : 0 : Assert(false); /* can't happen */
1789 : : break;
1790 : 0 : case EXPR_KIND_OTHER:
1791 : : /* Accept sublink here; caller must throw error if wanted */
1792 : 0 : break;
4826 tgl@sss.pgh.pa.us 1793 :CBC 24078 : case EXPR_KIND_JOIN_ON:
1794 : : case EXPR_KIND_JOIN_USING:
1795 : : case EXPR_KIND_FROM_SUBSELECT:
1796 : : case EXPR_KIND_FROM_FUNCTION:
1797 : : case EXPR_KIND_WHERE:
1798 : : case EXPR_KIND_POLICY:
1799 : : case EXPR_KIND_HAVING:
1800 : : case EXPR_KIND_FILTER:
1801 : : case EXPR_KIND_WINDOW_PARTITION:
1802 : : case EXPR_KIND_WINDOW_ORDER:
1803 : : case EXPR_KIND_WINDOW_FRAME_RANGE:
1804 : : case EXPR_KIND_WINDOW_FRAME_ROWS:
1805 : : case EXPR_KIND_WINDOW_FRAME_GROUPS:
1806 : : case EXPR_KIND_SELECT_TARGET:
1807 : : case EXPR_KIND_INSERT_TARGET:
1808 : : case EXPR_KIND_UPDATE_SOURCE:
1809 : : case EXPR_KIND_UPDATE_TARGET:
1810 : : case EXPR_KIND_MERGE_WHEN:
1811 : : case EXPR_KIND_GROUP_BY:
1812 : : case EXPR_KIND_ORDER_BY:
1813 : : case EXPR_KIND_DISTINCT_ON:
1814 : : case EXPR_KIND_LIMIT:
1815 : : case EXPR_KIND_OFFSET:
1816 : : case EXPR_KIND_RETURNING:
1817 : : case EXPR_KIND_MERGE_RETURNING:
1818 : : case EXPR_KIND_VALUES:
1819 : : case EXPR_KIND_VALUES_SINGLE:
1820 : : case EXPR_KIND_CYCLE_MARK:
1821 : : /* okay */
1822 : 24078 : break;
4826 tgl@sss.pgh.pa.us 1823 :UBC 0 : case EXPR_KIND_CHECK_CONSTRAINT:
1824 : : case EXPR_KIND_DOMAIN_CHECK:
4678 peter_e@gmx.net 1825 : 0 : err = _("cannot use subquery in check constraint");
4826 tgl@sss.pgh.pa.us 1826 : 0 : break;
4826 tgl@sss.pgh.pa.us 1827 :CBC 3 : case EXPR_KIND_COLUMN_DEFAULT:
1828 : : case EXPR_KIND_FUNCTION_DEFAULT:
1829 : 3 : err = _("cannot use subquery in DEFAULT expression");
1830 : 3 : break;
4826 tgl@sss.pgh.pa.us 1831 :UBC 0 : case EXPR_KIND_INDEX_EXPRESSION:
1832 : 0 : err = _("cannot use subquery in index expression");
1833 : 0 : break;
1834 : 0 : case EXPR_KIND_INDEX_PREDICATE:
1835 : 0 : err = _("cannot use subquery in index predicate");
1836 : 0 : break;
1676 tomas.vondra@postgre 1837 : 0 : case EXPR_KIND_STATS_EXPRESSION:
1838 : 0 : err = _("cannot use subquery in statistics expression");
1839 : 0 : break;
4826 tgl@sss.pgh.pa.us 1840 : 0 : case EXPR_KIND_ALTER_COL_TRANSFORM:
1841 : 0 : err = _("cannot use subquery in transform expression");
1842 : 0 : break;
1843 : 0 : case EXPR_KIND_EXECUTE_PARAMETER:
1844 : 0 : err = _("cannot use subquery in EXECUTE parameter");
1845 : 0 : break;
1846 : 0 : case EXPR_KIND_TRIGGER_WHEN:
1847 : 0 : err = _("cannot use subquery in trigger WHEN condition");
1848 : 0 : break;
2467 peter@eisentraut.org 1849 :CBC 6 : case EXPR_KIND_PARTITION_BOUND:
1850 : 6 : err = _("cannot use subquery in partition bound");
1851 : 6 : break;
3246 rhaas@postgresql.org 1852 : 3 : case EXPR_KIND_PARTITION_EXPRESSION:
1853 : 3 : err = _("cannot use subquery in partition key expression");
1854 : 3 : break;
2816 tgl@sss.pgh.pa.us 1855 :UBC 0 : case EXPR_KIND_CALL_ARGUMENT:
1856 : 0 : err = _("cannot use subquery in CALL argument");
1857 : 0 : break;
2473 tomas.vondra@postgre 1858 :CBC 3 : case EXPR_KIND_COPY_WHERE:
1859 : 3 : err = _("cannot use subquery in COPY FROM WHERE condition");
1860 : 3 : break;
2403 peter@eisentraut.org 1861 : 6 : case EXPR_KIND_GENERATED_COLUMN:
1862 : 6 : err = _("cannot use subquery in column generation expression");
1863 : 6 : 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 : : }
4826 tgl@sss.pgh.pa.us 1873 [ + + ]: 24099 : if (err)
1874 [ + - ]: 21 : ereport(ERROR,
1875 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1876 : : errmsg_internal("%s", err),
1877 : : parser_errposition(pstate, sublink->location)));
1878 : :
7586 neilc@samurai.com 1879 : 24078 : pstate->p_hasSubLinks = true;
1880 : :
1881 : : /*
1882 : : * OK, let's transform the sub-SELECT.
1883 : : */
3197 tgl@sss.pgh.pa.us 1884 : 24078 : 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 : : */
6265 1890 [ + - ]: 24057 : if (!IsA(qtree, Query) ||
3208 1891 [ - + ]: 24057 : qtree->commandType != CMD_SELECT)
6265 tgl@sss.pgh.pa.us 1892 [ # # ]:UBC 0 : elog(ERROR, "unexpected non-SELECT command in SubLink");
1893 : :
7586 neilc@samurai.com 1894 :CBC 24057 : sublink->subselect = (Node *) qtree;
1895 : :
1896 [ + + ]: 24057 : 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 : : */
7243 tgl@sss.pgh.pa.us 1902 : 3071 : sublink->testexpr = NULL;
7586 neilc@samurai.com 1903 : 3071 : sublink->operName = NIL;
1904 : : }
1905 [ + + ]: 20986 : else if (sublink->subLinkType == EXPR_SUBLINK ||
1906 [ + + ]: 7246 : sublink->subLinkType == ARRAY_SUBLINK)
1907 : : {
1908 : : /*
1909 : : * Make sure the subselect delivers a single column (ignoring resjunk
1910 : : * targets).
1911 : : */
4149 tgl@sss.pgh.pa.us 1912 [ - + ]: 17972 : if (count_nonjunk_tlist_entries(qtree->targetList) != 1)
7586 neilc@samurai.com 1913 [ # # ]:UBC 0 : 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 : : */
7243 tgl@sss.pgh.pa.us 1922 :CBC 17972 : sublink->testexpr = NULL;
7586 neilc@samurai.com 1923 : 17972 : sublink->operName = NIL;
1924 : : }
4149 tgl@sss.pgh.pa.us 1925 [ + + ]: 3014 : else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
1926 : : {
1927 : : /* Same as EXPR case, except no restriction on number of columns */
1928 : 69 : sublink->testexpr = NULL;
1929 : 69 : 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 : : */
3899 1942 [ + + ]: 2945 : if (sublink->operName == NIL)
1943 : 2840 : sublink->operName = list_make1(makeString("="));
1944 : :
1945 : : /*
1946 : : * Transform lefthand expression, and convert to a list
1947 : : */
4826 1948 : 2945 : lefthand = transformExprRecurse(pstate, sublink->testexpr);
7243 1949 [ + - + + ]: 2945 : if (lefthand && IsA(lefthand, RowExpr))
1950 : 155 : left_list = ((RowExpr *) lefthand)->args;
1951 : : else
1952 : 2790 : 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 : 2945 : right_list = NIL;
1959 [ + - + + : 6141 : foreach(l, qtree->targetList)
+ + ]
1960 : : {
7586 neilc@samurai.com 1961 : 3196 : TargetEntry *tent = (TargetEntry *) lfirst(l);
1962 : : Param *param;
1963 : :
7509 tgl@sss.pgh.pa.us 1964 [ + + ]: 3196 : if (tent->resjunk)
7586 neilc@samurai.com 1965 : 6 : continue;
1966 : :
7243 tgl@sss.pgh.pa.us 1967 : 3190 : param = makeNode(Param);
1968 : 3190 : param->paramkind = PARAM_SUBLINK;
7128 1969 : 3190 : param->paramid = tent->resno;
7243 1970 : 3190 : param->paramtype = exprType((Node *) tent->expr);
6896 1971 : 3190 : param->paramtypmod = exprTypmod((Node *) tent->expr);
5336 1972 : 3190 : param->paramcollid = exprCollation((Node *) tent->expr);
6269 1973 : 3190 : param->location = -1;
1974 : :
7243 1975 : 3190 : 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 [ - + ]: 2945 : if (list_length(left_list) < list_length(right_list))
7243 tgl@sss.pgh.pa.us 1984 [ # # ]:UBC 0 : ereport(ERROR,
1985 : : (errcode(ERRCODE_SYNTAX_ERROR),
1986 : : errmsg("subquery has too many columns"),
1987 : : parser_errposition(pstate, sublink->location)));
7243 tgl@sss.pgh.pa.us 1988 [ - + ]:CBC 2945 : if (list_length(left_list) > list_length(right_list))
7586 neilc@samurai.com 1989 [ # # ]:UBC 0 : 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 : : */
7243 tgl@sss.pgh.pa.us 1998 :CBC 2945 : sublink->testexpr = make_row_comparison_op(pstate,
1999 : : sublink->operName,
2000 : : left_list,
2001 : : right_list,
2002 : : sublink->location);
2003 : : }
2004 : :
7586 neilc@samurai.com 2005 : 24051 : 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 : : */
2015 : : static Node *
6430 tgl@sss.pgh.pa.us 2016 : 3984 : transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
2017 : : Oid array_type, Oid element_type, int32 typmod)
2018 : : {
7586 neilc@samurai.com 2019 : 3984 : ArrayExpr *newa = makeNode(ArrayExpr);
2020 : 3984 : List *newelems = NIL;
2021 : 3984 : List *newcoercedelems = NIL;
2022 : : ListCell *element;
2023 : : Oid coerce_type;
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 : : */
6430 tgl@sss.pgh.pa.us 2032 : 3984 : newa->multidims = false;
7586 neilc@samurai.com 2033 [ + + + + : 13340 : foreach(element, a->elements)
+ + ]
2034 : : {
2035 : 9356 : 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 : : */
6430 tgl@sss.pgh.pa.us 2042 [ + + ]: 9356 : if (IsA(e, A_ArrayExpr))
2043 : : {
2044 : 420 : newe = transformArrayExpr(pstate,
2045 : : (A_ArrayExpr *) e,
2046 : : array_type,
2047 : : element_type,
2048 : : typmod);
2049 : : /* we certainly have an array here */
6269 2050 [ + + - + ]: 420 : Assert(array_type == InvalidOid || array_type == exprType(newe));
6430 2051 : 420 : newa->multidims = true;
2052 : : }
2053 : : else
2054 : : {
4826 2055 : 8936 : 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 : : */
228 2063 [ + - ]: 8936 : if (!newa->multidims)
2064 : : {
2065 : 8936 : Oid newetype = exprType(newe);
2066 : :
2067 [ + + + + : 17848 : if (newetype != INT2VECTOROID && newetype != OIDVECTOROID &&
+ + ]
2068 : 8912 : type_is_array(newetype))
2069 : 3 : newa->multidims = true;
2070 : : }
2071 : : }
2072 : :
7586 neilc@samurai.com 2073 : 9356 : 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 : : */
6430 tgl@sss.pgh.pa.us 2082 [ + + ]: 3984 : if (OidIsValid(array_type))
2083 : : {
2084 : : /* Caller must ensure array_type matches element_type */
2085 [ - + ]: 421 : Assert(OidIsValid(element_type));
2086 [ + + ]: 421 : coerce_type = (newa->multidims ? array_type : element_type);
2087 : 421 : coerce_hard = true;
2088 : : }
2089 : : else
2090 : : {
2091 : : /* Can't handle an empty array without a target type */
6269 2092 [ + + ]: 3563 : if (newelems == NIL)
6430 2093 [ + - ]: 3 : 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 */
6269 2101 : 3560 : coerce_type = select_common_type(pstate, newelems, "ARRAY", NULL);
2102 : :
6430 2103 [ + + ]: 3560 : if (newa->multidims)
2104 : : {
2105 : 200 : array_type = coerce_type;
2106 : 200 : element_type = get_element_type(array_type);
2107 [ - + ]: 200 : if (!OidIsValid(element_type))
6430 tgl@sss.pgh.pa.us 2108 [ # # ]:UBC 0 : 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 : : {
6430 tgl@sss.pgh.pa.us 2116 :CBC 3360 : element_type = coerce_type;
2117 : 3360 : array_type = get_array_type(element_type);
2118 [ - + ]: 3360 : if (!OidIsValid(array_type))
6430 tgl@sss.pgh.pa.us 2119 [ # # ]:UBC 0 : 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 : : }
6430 tgl@sss.pgh.pa.us 2125 :CBC 3560 : 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 : : */
7586 neilc@samurai.com 2138 [ + + + + : 13337 : foreach(element, newelems)
+ + ]
2139 : : {
2140 : 9356 : Node *e = (Node *) lfirst(element);
2141 : : Node *newe;
2142 : :
6430 tgl@sss.pgh.pa.us 2143 [ + + ]: 9356 : if (coerce_hard)
2144 : : {
5982 bruce@momjian.us 2145 : 1019 : newe = coerce_to_target_type(pstate, e,
2146 : : exprType(e),
2147 : : coerce_type,
2148 : : typmod,
2149 : : COERCION_EXPLICIT,
2150 : : COERCE_EXPLICIT_CAST,
2151 : : -1);
6430 tgl@sss.pgh.pa.us 2152 [ - + ]: 1019 : if (newe == NULL)
6430 tgl@sss.pgh.pa.us 2153 [ # # ]:UBC 0 : ereport(ERROR,
2154 : : (errcode(ERRCODE_CANNOT_COERCE),
2155 : : errmsg("cannot cast type %s to %s",
2156 : : format_type_be(exprType(e)),
2157 : : format_type_be(coerce_type)),
2158 : : parser_errposition(pstate, exprLocation(e))));
2159 : : }
2160 : : else
6430 tgl@sss.pgh.pa.us 2161 :CBC 8337 : newe = coerce_to_common_type(pstate, e,
2162 : : coerce_type,
2163 : : "ARRAY");
7586 neilc@samurai.com 2164 : 9356 : newcoercedelems = lappend(newcoercedelems, newe);
2165 : : }
2166 : :
2167 : 3981 : newa->array_typeid = array_type;
2168 : : /* array_collid will be set by parse_collate.c */
2169 : 3981 : newa->element_typeid = element_type;
2170 : 3981 : newa->elements = newcoercedelems;
137 alvherre@kurilemu.de 2171 : 3981 : newa->list_start = a->list_start;
2172 : 3981 : newa->list_end = a->list_end;
6269 tgl@sss.pgh.pa.us 2173 : 3981 : newa->location = a->location;
2174 : :
7586 neilc@samurai.com 2175 : 3981 : return (Node *) newa;
2176 : : }
2177 : :
2178 : : static Node *
3261 tgl@sss.pgh.pa.us 2179 : 2947 : transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
2180 : : {
2181 : : RowExpr *newr;
2182 : : char fname[16];
2183 : : int fnum;
2184 : :
4691 2185 : 2947 : newr = makeNode(RowExpr);
2186 : :
2187 : : /* Transform the field expressions */
3261 2188 : 2947 : newr->args = transformExpressionList(pstate, r->args,
2189 : : pstate->p_expr_kind, allowDefault);
2190 : :
2191 : : /* Disallow more columns than will fit in a tuple */
1186 2192 [ - + ]: 2947 : if (list_length(newr->args) > MaxTupleAttributeNumber)
1186 tgl@sss.pgh.pa.us 2193 [ # # ]:UBC 0 : ereport(ERROR,
2194 : : (errcode(ERRCODE_TOO_MANY_COLUMNS),
2195 : : errmsg("ROW expressions can have at most %d entries",
2196 : : MaxTupleAttributeNumber),
2197 : : parser_errposition(pstate, r->location)));
2198 : :
2199 : : /* Barring later casting, we consider the type RECORD */
7586 neilc@samurai.com 2200 :CBC 2947 : newr->row_typeid = RECORDOID;
2201 : 2947 : newr->row_format = COERCE_IMPLICIT_CAST;
2202 : :
2203 : : /* ROW() has anonymous columns, so invent some field names */
5004 tgl@sss.pgh.pa.us 2204 : 2947 : newr->colnames = NIL;
2296 2205 [ + + ]: 10329 : for (fnum = 1; fnum <= list_length(newr->args); fnum++)
2206 : : {
2207 : 7382 : snprintf(fname, sizeof(fname), "f%d", fnum);
5004 2208 : 7382 : newr->colnames = lappend(newr->colnames, makeString(pstrdup(fname)));
2209 : : }
2210 : :
6269 2211 : 2947 : newr->location = r->location;
2212 : :
7586 neilc@samurai.com 2213 : 2947 : return (Node *) newr;
2214 : : }
2215 : :
2216 : : static Node *
2217 : 1609 : transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c)
2218 : : {
2219 : 1609 : CoalesceExpr *newc = makeNode(CoalesceExpr);
3058 tgl@sss.pgh.pa.us 2220 : 1609 : Node *last_srf = pstate->p_last_srf;
7586 neilc@samurai.com 2221 : 1609 : List *newargs = NIL;
2222 : 1609 : List *newcoercedargs = NIL;
2223 : : ListCell *args;
2224 : :
2225 [ + - + + : 4821 : foreach(args, c->args)
+ + ]
2226 : : {
2227 : 3212 : Node *e = (Node *) lfirst(args);
2228 : : Node *newe;
2229 : :
4826 tgl@sss.pgh.pa.us 2230 : 3212 : newe = transformExprRecurse(pstate, e);
7586 neilc@samurai.com 2231 : 3212 : newargs = lappend(newargs, newe);
2232 : : }
2233 : :
6269 tgl@sss.pgh.pa.us 2234 : 1609 : newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
2235 : : /* coalescecollid will be set by parse_collate.c */
2236 : :
2237 : : /* Convert arguments if necessary */
7586 neilc@samurai.com 2238 [ + - + + : 4821 : foreach(args, newargs)
+ + ]
2239 : : {
2240 : 3212 : Node *e = (Node *) lfirst(args);
2241 : : Node *newe;
2242 : :
2243 : 3212 : newe = coerce_to_common_type(pstate, e,
2244 : : newc->coalescetype,
2245 : : "COALESCE");
2246 : 3212 : newcoercedargs = lappend(newcoercedargs, newe);
2247 : : }
2248 : :
2249 : : /* if any subexpression contained a SRF, complain */
3058 tgl@sss.pgh.pa.us 2250 [ + + ]: 1609 : if (pstate->p_last_srf != last_srf)
2251 [ + - ]: 3 : 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 : :
7586 neilc@samurai.com 2260 : 1606 : newc->args = newcoercedargs;
6269 tgl@sss.pgh.pa.us 2261 : 1606 : newc->location = c->location;
7586 neilc@samurai.com 2262 : 1606 : return (Node *) newc;
2263 : : }
2264 : :
2265 : : static Node *
7428 tgl@sss.pgh.pa.us 2266 : 134 : transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
2267 : : {
2268 : 134 : MinMaxExpr *newm = makeNode(MinMaxExpr);
2269 : 134 : List *newargs = NIL;
2270 : 134 : List *newcoercedargs = NIL;
6269 2271 [ + + ]: 134 : const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
2272 : : ListCell *args;
2273 : :
7428 2274 : 134 : newm->op = m->op;
2275 [ + - + + : 429 : foreach(args, m->args)
+ + ]
2276 : : {
2277 : 295 : Node *e = (Node *) lfirst(args);
2278 : : Node *newe;
2279 : :
4826 2280 : 295 : newe = transformExprRecurse(pstate, e);
7428 2281 : 295 : newargs = lappend(newargs, newe);
2282 : : }
2283 : :
6269 2284 : 134 : 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 */
7428 2288 [ + - + + : 429 : foreach(args, newargs)
+ + ]
2289 : : {
2290 : 295 : Node *e = (Node *) lfirst(args);
2291 : : Node *newe;
2292 : :
2293 : 295 : newe = coerce_to_common_type(pstate, e,
2294 : : newm->minmaxtype,
2295 : : funcname);
2296 : 295 : newcoercedargs = lappend(newcoercedargs, newe);
2297 : : }
2298 : :
2299 : 134 : newm->args = newcoercedargs;
6269 2300 : 134 : newm->location = m->location;
7428 2301 : 134 : return (Node *) newm;
2302 : : }
2303 : :
2304 : : static Node *
894 michael@paquier.xyz 2305 : 1360 : transformSQLValueFunction(ParseState *pstate, SQLValueFunction *svf)
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 [ + + + + : 1360 : switch (svf->op)
+ + + + +
+ - ]
2312 : : {
2313 : 154 : case SVFOP_CURRENT_DATE:
2314 : 154 : svf->type = DATEOID;
2315 : 154 : break;
2316 : 12 : case SVFOP_CURRENT_TIME:
2317 : 12 : svf->type = TIMETZOID;
2318 : 12 : break;
2319 : 12 : case SVFOP_CURRENT_TIME_N:
2320 : 12 : svf->type = TIMETZOID;
2321 : 12 : svf->typmod = anytime_typmod_check(true, svf->typmod);
2322 : 12 : break;
2323 : 143 : case SVFOP_CURRENT_TIMESTAMP:
2324 : 143 : svf->type = TIMESTAMPTZOID;
2325 : 143 : break;
2326 : 87 : case SVFOP_CURRENT_TIMESTAMP_N:
2327 : 87 : svf->type = TIMESTAMPTZOID;
2328 : 87 : svf->typmod = anytimestamp_typmod_check(true, svf->typmod);
2329 : 87 : break;
2330 : 12 : case SVFOP_LOCALTIME:
2331 : 12 : svf->type = TIMEOID;
2332 : 12 : break;
2333 : 12 : case SVFOP_LOCALTIME_N:
2334 : 12 : svf->type = TIMEOID;
2335 : 12 : svf->typmod = anytime_typmod_check(false, svf->typmod);
2336 : 12 : break;
2337 : 18 : case SVFOP_LOCALTIMESTAMP:
2338 : 18 : svf->type = TIMESTAMPOID;
2339 : 18 : break;
2340 : 12 : case SVFOP_LOCALTIMESTAMP_N:
2341 : 12 : svf->type = TIMESTAMPOID;
2342 : 12 : svf->typmod = anytimestamp_typmod_check(false, svf->typmod);
2343 : 12 : break;
2344 : 898 : case SVFOP_CURRENT_ROLE:
2345 : : case SVFOP_CURRENT_USER:
2346 : : case SVFOP_USER:
2347 : : case SVFOP_SESSION_USER:
2348 : : case SVFOP_CURRENT_CATALOG:
2349 : : case SVFOP_CURRENT_SCHEMA:
2350 : 898 : svf->type = NAMEOID;
2351 : 898 : break;
2352 : : }
2353 : :
2354 : 1360 : return (Node *) svf;
2355 : : }
2356 : :
2357 : : static Node *
6556 bruce@momjian.us 2358 : 299 : transformXmlExpr(ParseState *pstate, XmlExpr *x)
2359 : : {
2360 : : XmlExpr *newx;
2361 : : ListCell *lc;
2362 : : int i;
2363 : :
4691 tgl@sss.pgh.pa.us 2364 : 299 : newx = makeNode(XmlExpr);
6882 2365 : 299 : newx->op = x->op;
2366 [ + + ]: 299 : if (x->name)
6833 peter_e@gmx.net 2367 : 129 : newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
2368 : : else
6882 tgl@sss.pgh.pa.us 2369 : 170 : newx->name = NULL;
6269 2370 : 299 : newx->xmloption = x->xmloption;
4691 2371 : 299 : newx->type = XMLOID; /* this just marks the node as transformed */
2372 : 299 : newx->typmod = -1;
6269 2373 : 299 : 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 : : */
6882 2379 : 299 : newx->named_args = NIL;
2380 : 299 : newx->arg_names = NIL;
2381 : :
2382 [ + + + + : 409 : foreach(lc, x->named_args)
+ + ]
2383 : : {
3122 2384 : 116 : ResTarget *r = lfirst_node(ResTarget, lc);
2385 : : Node *expr;
2386 : : char *argname;
2387 : :
4826 2388 : 116 : expr = transformExprRecurse(pstate, r->val);
2389 : :
6882 2390 [ + + ]: 116 : if (r->name)
6833 peter_e@gmx.net 2391 : 53 : argname = map_sql_identifier_to_xml_name(r->name, false, false);
6882 tgl@sss.pgh.pa.us 2392 [ + + ]: 63 : else if (IsA(r->val, ColumnRef))
2393 : 60 : argname = map_sql_identifier_to_xml_name(FigureColname(r->val),
2394 : : true, false);
2395 : : else
2396 : : {
2397 [ + - + - ]: 3 : 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 */
6269 2407 [ + + ]: 113 : if (x->op == IS_XMLELEMENT)
2408 : : {
2409 : : ListCell *lc2;
2410 : :
2411 [ + + + + : 60 : foreach(lc2, newx->arg_names)
+ + ]
2412 : : {
2413 [ + + ]: 19 : if (strcmp(argname, strVal(lfirst(lc2))) == 0)
6867 peter_e@gmx.net 2414 [ + - ]: 3 : 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 : :
6269 tgl@sss.pgh.pa.us 2422 : 110 : newx->named_args = lappend(newx->named_args, expr);
2423 : 110 : newx->arg_names = lappend(newx->arg_names, makeString(argname));
2424 : : }
2425 : :
2426 : : /* The other arguments are of varying types depending on the function */
6882 2427 : 293 : newx->args = NIL;
2428 : 293 : i = 0;
2429 [ + + + + : 704 : foreach(lc, x->args)
+ + ]
2430 : : {
2431 : 420 : Node *e = (Node *) lfirst(lc);
2432 : : Node *newe;
2433 : :
4826 2434 : 420 : newe = transformExprRecurse(pstate, e);
6882 2435 [ + + - + : 420 : switch (x->op)
+ + - +
- ]
2436 : : {
2437 : 67 : case IS_XMLCONCAT:
2438 : 67 : newe = coerce_to_specific_type(pstate, newe, XMLOID,
2439 : : "XMLCONCAT");
2440 : 61 : break;
6863 peter_e@gmx.net 2441 : 68 : case IS_XMLELEMENT:
2442 : : /* no coercion necessary */
2443 : 68 : break;
6882 tgl@sss.pgh.pa.us 2444 :UBC 0 : case IS_XMLFOREST:
2445 : 0 : newe = coerce_to_specific_type(pstate, newe, XMLOID,
2446 : : "XMLFOREST");
2447 : 0 : break;
6882 tgl@sss.pgh.pa.us 2448 :CBC 140 : case IS_XMLPARSE:
2449 [ + + ]: 140 : if (i == 0)
2450 : 70 : newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2451 : : "XMLPARSE");
2452 : : else
2453 : 70 : newe = coerce_to_boolean(pstate, newe, "XMLPARSE");
2454 : 140 : break;
2455 : 25 : case IS_XMLPI:
2456 : 25 : newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2457 : : "XMLPI");
2458 : 25 : break;
2459 : 102 : case IS_XMLROOT:
2460 [ + + ]: 102 : if (i == 0)
2461 : 34 : newe = coerce_to_specific_type(pstate, newe, XMLOID,
2462 : : "XMLROOT");
2463 [ + + ]: 68 : else if (i == 1)
2464 : 34 : newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2465 : : "XMLROOT");
2466 : : else
6850 peter_e@gmx.net 2467 : 34 : newe = coerce_to_specific_type(pstate, newe, INT4OID,
2468 : : "XMLROOT");
6882 tgl@sss.pgh.pa.us 2469 : 102 : break;
6841 peter_e@gmx.net 2470 :UBC 0 : case IS_XMLSERIALIZE:
2471 : : /* not handled here */
6269 tgl@sss.pgh.pa.us 2472 : 0 : Assert(false);
2473 : : break;
6861 peter_e@gmx.net 2474 :CBC 18 : case IS_DOCUMENT:
2475 : 18 : newe = coerce_to_specific_type(pstate, newe, XMLOID,
2476 : : "IS DOCUMENT");
2477 : 15 : break;
2478 : : }
6882 tgl@sss.pgh.pa.us 2479 : 411 : newx->args = lappend(newx->args, newe);
2480 : 411 : i++;
2481 : : }
2482 : :
2483 : 284 : return (Node *) newx;
2484 : : }
2485 : :
2486 : : static Node *
6556 bruce@momjian.us 2487 : 109 : transformXmlSerialize(ParseState *pstate, XmlSerialize *xs)
2488 : : {
2489 : : Node *result;
2490 : : XmlExpr *xexpr;
2491 : : Oid targetType;
2492 : : int32 targetTypmod;
2493 : :
6841 peter_e@gmx.net 2494 : 109 : xexpr = makeNode(XmlExpr);
2495 : 109 : xexpr->op = IS_XMLSERIALIZE;
2496 : 109 : xexpr->args = list_make1(coerce_to_specific_type(pstate,
2497 : : transformExprRecurse(pstate, xs->expr),
2498 : : XMLOID,
2499 : : "XMLSERIALIZE"));
2500 : :
5481 2501 : 109 : typenameTypeIdAndMod(pstate, xs->typeName, &targetType, &targetTypmod);
2502 : :
6841 2503 : 109 : xexpr->xmloption = xs->xmloption;
957 tgl@sss.pgh.pa.us 2504 : 109 : xexpr->indent = xs->indent;
6269 2505 : 109 : xexpr->location = xs->location;
2506 : : /* We actually only need these to be able to parse back the expression. */
6841 peter_e@gmx.net 2507 : 109 : xexpr->type = targetType;
2508 : 109 : 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 : : */
6269 tgl@sss.pgh.pa.us 2516 : 109 : result = coerce_to_target_type(pstate, (Node *) xexpr,
2517 : : TEXTOID, targetType, targetTypmod,
2518 : : COERCION_IMPLICIT,
2519 : : COERCE_IMPLICIT_CAST,
2520 : : -1);
2521 [ - + ]: 109 : if (result == NULL)
6269 tgl@sss.pgh.pa.us 2522 [ # # ]:UBC 0 : 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)));
6269 tgl@sss.pgh.pa.us 2527 :CBC 109 : return result;
2528 : : }
2529 : :
2530 : : static Node *
7586 neilc@samurai.com 2531 : 464 : transformBooleanTest(ParseState *pstate, BooleanTest *b)
2532 : : {
2533 : : const char *clausename;
2534 : :
2535 [ + + + + : 464 : switch (b->booltesttype)
+ + - ]
2536 : : {
2537 : 221 : case IS_TRUE:
2538 : 221 : clausename = "IS TRUE";
2539 : 221 : break;
2540 : 70 : case IS_NOT_TRUE:
2541 : 70 : clausename = "IS NOT TRUE";
2542 : 70 : break;
2543 : 77 : case IS_FALSE:
2544 : 77 : clausename = "IS FALSE";
2545 : 77 : break;
2546 : 46 : case IS_NOT_FALSE:
2547 : 46 : clausename = "IS NOT FALSE";
2548 : 46 : break;
2549 : 26 : case IS_UNKNOWN:
2550 : 26 : clausename = "IS UNKNOWN";
2551 : 26 : break;
2552 : 24 : case IS_NOT_UNKNOWN:
2553 : 24 : clausename = "IS NOT UNKNOWN";
2554 : 24 : break;
7586 neilc@samurai.com 2555 :UBC 0 : default:
2556 [ # # ]: 0 : elog(ERROR, "unrecognized booltesttype: %d",
2557 : : (int) b->booltesttype);
2558 : : clausename = NULL; /* keep compiler quiet */
2559 : : }
2560 : :
4826 tgl@sss.pgh.pa.us 2561 :CBC 464 : b->arg = (Expr *) transformExprRecurse(pstate, (Node *) b->arg);
2562 : :
7586 neilc@samurai.com 2563 : 928 : b->arg = (Expr *) coerce_to_boolean(pstate,
2564 : 464 : (Node *) b->arg,
2565 : : clausename);
2566 : :
2567 : 464 : return (Node *) b;
2568 : : }
2569 : :
2570 : : static Node *
6556 bruce@momjian.us 2571 : 127 : transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr)
2572 : : {
2573 : : /* CURRENT OF can only appear at top level of UPDATE/DELETE */
2125 tgl@sss.pgh.pa.us 2574 [ - + ]: 127 : Assert(pstate->p_target_nsitem != NULL);
2575 : 127 : 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 : : */
3050 2582 [ + - ]: 127 : if (cexpr->cursor_name != NULL) /* in case already transformed */
2583 : : {
5831 2584 : 127 : ColumnRef *cref = makeNode(ColumnRef);
2585 : 127 : Node *node = NULL;
2586 : :
2587 : : /* Build an unqualified ColumnRef with the given name */
2588 : 127 : cref->fields = list_make1(makeString(cexpr->cursor_name));
2589 : 127 : cref->location = -1;
2590 : :
2591 : : /* See if there is a translation available from a parser hook */
2592 [ + + ]: 127 : if (pstate->p_pre_columnref_hook != NULL)
2972 peter_e@gmx.net 2593 : 6 : node = pstate->p_pre_columnref_hook(pstate, cref);
5831 tgl@sss.pgh.pa.us 2594 [ + - + + ]: 127 : if (node == NULL && pstate->p_post_columnref_hook != NULL)
2972 peter_e@gmx.net 2595 : 6 : 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 : : */
5831 tgl@sss.pgh.pa.us 2602 [ + + + - ]: 127 : if (node != NULL && IsA(node, Param))
2603 : : {
5722 bruce@momjian.us 2604 : 6 : Param *p = (Param *) node;
2605 : :
5831 tgl@sss.pgh.pa.us 2606 [ + - ]: 6 : if (p->paramkind == PARAM_EXTERN &&
2607 [ + - ]: 6 : p->paramtype == REFCURSOROID)
2608 : : {
2609 : : /* Matches, so convert CURRENT OF to a param reference */
2610 : 6 : cexpr->cursor_name = NULL;
2611 : 6 : cexpr->cursor_param = p->paramid;
2612 : : }
2613 : : }
2614 : : }
2615 : :
6713 2616 : 127 : return (Node *) cexpr;
2617 : : }
2618 : :
2619 : : /*
2620 : : * Construct a whole-row reference to represent the notation "relation.*".
2621 : : */
2622 : : static Node *
2132 2623 : 4477 : transformWholeRowRef(ParseState *pstate, ParseNamespaceItem *nsitem,
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 : : */
284 dean.a.rasheed@gmail 2649 [ + + ]: 4477 : if (nsitem->p_names == nsitem->p_rte->eref ||
2650 [ + + ]: 138 : nsitem->p_returning_type != VAR_RETURNING_DEFAULT)
2651 : : {
2652 : : Var *result;
2653 : :
1671 peter@eisentraut.org 2654 : 4471 : result = makeWholeRowVar(nsitem->p_rte, nsitem->p_rtindex,
2655 : : sublevels_up, true);
2656 : :
2657 : : /* mark Var for RETURNING OLD/NEW, as necessary */
284 dean.a.rasheed@gmail 2658 : 4471 : result->varreturningtype = nsitem->p_returning_type;
2659 : :
2660 : : /* location is not filled in by makeWholeRowVar */
1671 peter@eisentraut.org 2661 : 4471 : result->location = location;
2662 : :
2663 : : /* mark Var if it's nulled by any outer joins */
1001 tgl@sss.pgh.pa.us 2664 : 4471 : markNullableIfNeeded(pstate, result);
2665 : :
2666 : : /* mark relation as requiring whole-row SELECT access */
1671 peter@eisentraut.org 2667 : 4471 : markVarForSelectPriv(pstate, result);
2668 : :
2669 : 4471 : 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 : : */
284 dean.a.rasheed@gmail 2682 : 6 : expandRTE(nsitem->p_rte, nsitem->p_rtindex, sublevels_up,
2683 : : nsitem->p_returning_type, location, false, NULL, &fields);
1671 peter@eisentraut.org 2684 : 6 : rowexpr = makeNode(RowExpr);
2685 : 6 : rowexpr->args = list_truncate(fields,
2686 : 6 : list_length(nsitem->p_names->colnames));
2687 : 6 : rowexpr->row_typeid = RECORDOID;
2688 : 6 : rowexpr->row_format = COERCE_IMPLICIT_CAST;
2689 : 6 : rowexpr->colnames = copyObject(nsitem->p_names->colnames);
2690 : 6 : rowexpr->location = location;
2691 : :
2692 : : /* XXX we ought to mark the row as possibly nullable */
2693 : :
2694 : 6 : 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 : : */
2704 : : static Node *
6269 tgl@sss.pgh.pa.us 2705 : 159860 : transformTypeCast(ParseState *pstate, TypeCast *tc)
2706 : : {
2707 : : Node *result;
3476 2708 : 159860 : 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 */
5481 peter_e@gmx.net 2716 : 159860 : 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 : : */
3476 tgl@sss.pgh.pa.us 2725 [ + + ]: 159860 : 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 : : */
3900 2737 : 362 : targetBaseTypmod = targetTypmod;
2738 : 362 : targetBaseType = getBaseTypeAndTypmod(targetType, &targetBaseTypmod);
2739 : 362 : elementType = get_element_type(targetBaseType);
2740 [ + + ]: 362 : if (OidIsValid(elementType))
2741 : : {
2742 : 357 : expr = transformArrayExpr(pstate,
2743 : : (A_ArrayExpr *) arg,
2744 : : targetBaseType,
2745 : : elementType,
2746 : : targetBaseTypmod);
2747 : : }
2748 : : else
3476 2749 : 5 : expr = transformExprRecurse(pstate, arg);
2750 : : }
2751 : : else
2752 : 159498 : expr = transformExprRecurse(pstate, arg);
2753 : :
3900 2754 : 159851 : inputType = exprType(expr);
9415 2755 [ - + ]: 159851 : if (inputType == InvalidOid)
9415 tgl@sss.pgh.pa.us 2756 :UBC 0 : 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 : : */
6269 tgl@sss.pgh.pa.us 2763 :CBC 159851 : location = tc->location;
2764 [ + + ]: 159851 : if (location < 0)
5947 peter_e@gmx.net 2765 : 8611 : location = tc->typeName->location;
2766 : :
6269 tgl@sss.pgh.pa.us 2767 : 159851 : result = coerce_to_target_type(pstate, expr, inputType,
2768 : : targetType, targetTypmod,
2769 : : COERCION_EXPLICIT,
2770 : : COERCE_EXPLICIT_CAST,
2771 : : location);
2772 [ + + ]: 158183 : if (result == NULL)
8136 2773 [ + - ]: 11 : 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 : :
6269 2780 : 158172 : return result;
2781 : : }
2782 : :
2783 : : /*
2784 : : * Handle an explicit COLLATE clause.
2785 : : *
2786 : : * Transform the argument, and look up the collation name.
2787 : : */
2788 : : static Node *
5375 peter_e@gmx.net 2789 : 4533 : transformCollateClause(ParseState *pstate, CollateClause *c)
2790 : : {
2791 : : CollateExpr *newc;
2792 : : Oid argtype;
2793 : :
5344 tgl@sss.pgh.pa.us 2794 : 4533 : newc = makeNode(CollateExpr);
4826 2795 : 4533 : newc->arg = (Expr *) transformExprRecurse(pstate, c->arg);
2796 : :
5375 peter_e@gmx.net 2797 : 4533 : 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 [ + + + + ]: 4533 : if (!type_is_collatable(argtype) && argtype != UNKNOWNOID)
2804 [ + - ]: 9 : 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 : :
5344 tgl@sss.pgh.pa.us 2810 : 4524 : newc->collOid = LookupCollation(pstate, c->collname, c->location);
5375 peter_e@gmx.net 2811 : 4524 : newc->location = c->location;
2812 : :
2813 : 4524 : 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 : : */
2828 : : static Node *
7243 tgl@sss.pgh.pa.us 2829 : 3361 : make_row_comparison_op(ParseState *pstate, List *opname,
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 : 3361 : nopers = list_length(largs);
2845 [ - + ]: 3361 : if (nopers != list_length(rargs))
7840 tgl@sss.pgh.pa.us 2846 [ # # ]:UBC 0 : 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 : : */
7243 tgl@sss.pgh.pa.us 2855 [ + + ]:CBC 3361 : if (nopers == 0)
7840 2856 [ + - ]: 3 : 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 : : */
7243 2865 : 3358 : opexprs = NIL;
7824 neilc@samurai.com 2866 [ + - + + : 7401 : forboth(l, largs, r, rargs)
+ - + + +
+ + - +
+ ]
2867 : : {
7729 bruce@momjian.us 2868 : 4049 : Node *larg = (Node *) lfirst(l);
2869 : 4049 : Node *rarg = (Node *) lfirst(r);
2870 : : OpExpr *cmp;
2871 : :
3058 tgl@sss.pgh.pa.us 2872 : 4049 : 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 : : */
7243 2880 [ - + ]: 4043 : if (cmp->opresulttype != BOOLOID)
7243 tgl@sss.pgh.pa.us 2881 [ # # ]:UBC 0 : 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)));
7243 tgl@sss.pgh.pa.us 2887 [ - + ]:CBC 4043 : if (expression_returns_set((Node *) cmp))
7243 tgl@sss.pgh.pa.us 2888 [ # # ]:UBC 0 : ereport(ERROR,
2889 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
2890 : : errmsg("row comparison operator must not return a set"),
2891 : : parser_errposition(pstate, location)));
7243 tgl@sss.pgh.pa.us 2892 :CBC 4043 : 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 [ + + ]: 3352 : if (nopers == 1)
2901 : 2793 : 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 : : */
5227 2909 : 559 : opinfo_lists = (List **) palloc(nopers * sizeof(List *));
204 peter@eisentraut.org 2910 : 559 : cmptypes = NULL;
7243 tgl@sss.pgh.pa.us 2911 : 559 : i = 0;
2912 [ + - + + : 1809 : foreach(l, opexprs)
+ + ]
2913 : : {
6883 2914 : 1250 : Oid opno = ((OpExpr *) lfirst(l))->opno;
2915 : : Bitmapset *this_cmptypes;
2916 : : ListCell *j;
2917 : :
204 peter@eisentraut.org 2918 : 1250 : 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 : 1250 : this_cmptypes = NULL;
5227 tgl@sss.pgh.pa.us 2925 [ + + + + : 2578 : foreach(j, opinfo_lists[i])
+ + ]
2926 : : {
204 peter@eisentraut.org 2927 : 1328 : OpIndexInterpretation *opinfo = lfirst(j);
2928 : :
2929 : 1328 : this_cmptypes = bms_add_member(this_cmptypes, opinfo->cmptype);
2930 : : }
7243 tgl@sss.pgh.pa.us 2931 [ + + ]: 1250 : if (i == 0)
204 peter@eisentraut.org 2932 : 559 : cmptypes = this_cmptypes;
2933 : : else
2934 : 691 : cmptypes = bms_int_members(cmptypes, this_cmptypes);
7243 tgl@sss.pgh.pa.us 2935 : 1250 : 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 : : */
204 peter@eisentraut.org 2943 : 559 : i = bms_next_member(cmptypes, -1);
6883 tgl@sss.pgh.pa.us 2944 [ + + ]: 559 : if (i < 0)
2945 : : {
2946 : : /* No common interpretation, so fail */
2947 [ + - ]: 3 : 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 : : }
285 peter@eisentraut.org 2954 : 556 : cmptype = (CompareType) i;
2955 : :
2956 : : /*
2957 : : * For = and <> cases, we just combine the pairwise operators with AND or
2958 : : * OR respectively.
2959 : : */
2960 [ + + ]: 556 : if (cmptype == COMPARE_EQ)
6269 tgl@sss.pgh.pa.us 2961 : 223 : return (Node *) makeBoolExpr(AND_EXPR, opexprs, location);
285 peter@eisentraut.org 2962 [ + + ]: 333 : if (cmptype == COMPARE_NE)
6269 tgl@sss.pgh.pa.us 2963 : 198 : 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 : : */
6883 2969 : 135 : opfamilies = NIL;
7243 2970 [ + + ]: 432 : for (i = 0; i < nopers; i++)
2971 : : {
6883 2972 : 297 : Oid opfamily = InvalidOid;
2973 : : ListCell *j;
2974 : :
5227 2975 [ + - + - : 297 : foreach(j, opinfo_lists[i])
+ - ]
2976 : : {
204 peter@eisentraut.org 2977 : 297 : OpIndexInterpretation *opinfo = lfirst(j);
2978 : :
2979 [ + - ]: 297 : if (opinfo->cmptype == cmptype)
2980 : : {
5227 tgl@sss.pgh.pa.us 2981 : 297 : opfamily = opinfo->opfamily_id;
6883 2982 : 297 : break;
2983 : : }
2984 : : }
2985 [ + - ]: 297 : if (OidIsValid(opfamily))
2986 : 297 : opfamilies = lappend_oid(opfamilies, opfamily);
2987 : : else /* should not happen */
7243 tgl@sss.pgh.pa.us 2988 [ # # ]:UBC 0 : 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 : : */
7243 tgl@sss.pgh.pa.us 3002 :CBC 135 : opnos = NIL;
3003 : 135 : largs = NIL;
3004 : 135 : rargs = NIL;
3005 [ + - + + : 432 : foreach(l, opexprs)
+ + ]
3006 : : {
3007 : 297 : OpExpr *cmp = (OpExpr *) lfirst(l);
3008 : :
3009 : 297 : opnos = lappend_oid(opnos, cmp->opno);
3010 : 297 : largs = lappend(largs, linitial(cmp->args));
3011 : 297 : rargs = lappend(rargs, lsecond(cmp->args));
3012 : : }
3013 : :
3014 : 135 : rcexpr = makeNode(RowCompareExpr);
285 peter@eisentraut.org 3015 : 135 : rcexpr->cmptype = cmptype;
7243 tgl@sss.pgh.pa.us 3016 : 135 : rcexpr->opnos = opnos;
6883 3017 : 135 : rcexpr->opfamilies = opfamilies;
5314 bruce@momjian.us 3018 : 135 : rcexpr->inputcollids = NIL; /* assign_expr_collations will fix this */
7243 tgl@sss.pgh.pa.us 3019 : 135 : rcexpr->largs = largs;
3020 : 135 : rcexpr->rargs = rargs;
3021 : :
3022 : 135 : return (Node *) rcexpr;
3023 : : }
3024 : :
3025 : : /*
3026 : : * Transform a "row IS DISTINCT FROM row" construct
3027 : : *
3028 : : * The input RowExprs are already transformed
3029 : : */
3030 : : static Node *
7840 3031 : 3 : make_row_distinct_op(ParseState *pstate, List *opname,
3032 : : RowExpr *lrow, RowExpr *rrow,
3033 : : int location)
3034 : : {
3035 : 3 : Node *result = NULL;
7273 3036 : 3 : List *largs = lrow->args;
3037 : 3 : List *rargs = rrow->args;
3038 : : ListCell *l,
3039 : : *r;
3040 : :
7820 neilc@samurai.com 3041 [ - + ]: 3 : if (list_length(largs) != list_length(rargs))
7840 tgl@sss.pgh.pa.us 3042 [ # # ]:UBC 0 : ereport(ERROR,
3043 : : (errcode(ERRCODE_SYNTAX_ERROR),
3044 : : errmsg("unequal number of entries in row expressions"),
3045 : : parser_errposition(pstate, location)));
3046 : :
7824 neilc@samurai.com 3047 [ + - + + :CBC 12 : forboth(l, largs, r, rargs)
+ - + + +
+ + - +
+ ]
3048 : : {
7729 bruce@momjian.us 3049 : 9 : Node *larg = (Node *) lfirst(l);
3050 : 9 : Node *rarg = (Node *) lfirst(r);
3051 : : Node *cmp;
3052 : :
7167 tgl@sss.pgh.pa.us 3053 : 9 : cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg, location);
7840 3054 [ + + ]: 9 : if (result == NULL)
3055 : 3 : result = cmp;
3056 : : else
3057 : 6 : result = (Node *) makeBoolExpr(OR_EXPR,
6269 3058 : 6 : list_make2(result, cmp),
3059 : : location);
3060 : : }
3061 : :
7840 3062 [ - + ]: 3 : if (result == NULL)
3063 : : {
3064 : : /* zero-length rows? Generate constant FALSE */
7840 tgl@sss.pgh.pa.us 3065 :UBC 0 : result = makeBoolConst(false, false);
3066 : : }
3067 : :
7840 tgl@sss.pgh.pa.us 3068 :CBC 3 : return result;
3069 : : }
3070 : :
3071 : : /*
3072 : : * make the node for an IS DISTINCT FROM operator
3073 : : */
3074 : : static Expr *
7167 3075 : 562 : make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree,
3076 : : int location)
3077 : : {
3078 : : Expr *result;
3079 : :
3058 3080 : 562 : result = make_op(pstate, opname, ltree, rtree,
3081 : : pstate->p_last_srf, location);
7840 3082 [ - + ]: 562 : if (((OpExpr *) result)->opresulttype != BOOLOID)
7840 tgl@sss.pgh.pa.us 3083 [ # # ]:UBC 0 : 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)));
3058 tgl@sss.pgh.pa.us 3089 [ - + ]:CBC 562 : if (((OpExpr *) result)->opretset)
3058 tgl@sss.pgh.pa.us 3090 [ # # ]:UBC 0 : 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 : : */
7840 tgl@sss.pgh.pa.us 3099 :CBC 562 : NodeSetTag(result, T_DistinctExpr);
3100 : :
3101 : 562 : 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 : : */
3109 : : static Node *
3378 3110 : 15 : make_nulltest_from_distinct(ParseState *pstate, A_Expr *distincta, Node *arg)
3111 : : {
3112 : 15 : NullTest *nt = makeNode(NullTest);
3113 : :
3114 : 15 : nt->arg = (Expr *) transformExprRecurse(pstate, arg);
3115 : : /* the argument can be any type, so don't coerce it */
3116 [ + + ]: 15 : if (distincta->kind == AEXPR_NOT_DISTINCT)
3117 : 6 : nt->nulltesttype = IS_NULL;
3118 : : else
3119 : 9 : nt->nulltesttype = IS_NOT_NULL;
3120 : : /* argisrow = false is correct whether or not arg is composite */
3121 : 15 : nt->argisrow = false;
3122 : 15 : nt->location = distincta->location;
3123 : 15 : 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 : : */
3133 : : const char *
4826 3134 : 39 : ParseExprKindName(ParseExprKind exprKind)
3135 : : {
3136 [ - - - - : 39 : switch (exprKind)
- - + - -
+ - - - -
- - - + -
+ - - + -
+ + - - -
- - - - -
- - - - -
- - ]
3137 : : {
4826 tgl@sss.pgh.pa.us 3138 :UBC 0 : case EXPR_KIND_NONE:
3139 : 0 : return "invalid expression context";
3140 : 0 : case EXPR_KIND_OTHER:
3141 : 0 : return "extension expression";
3142 : 0 : case EXPR_KIND_JOIN_ON:
3143 : 0 : return "JOIN/ON";
3144 : 0 : case EXPR_KIND_JOIN_USING:
3145 : 0 : return "JOIN/USING";
3146 : 0 : case EXPR_KIND_FROM_SUBSELECT:
3147 : 0 : return "sub-SELECT in FROM";
3148 : 0 : case EXPR_KIND_FROM_FUNCTION:
3149 : 0 : return "function in FROM";
4826 tgl@sss.pgh.pa.us 3150 :CBC 12 : case EXPR_KIND_WHERE:
3151 : 12 : return "WHERE";
3743 mail@joeconway.com 3152 :UBC 0 : case EXPR_KIND_POLICY:
3153 : 0 : return "POLICY";
4826 tgl@sss.pgh.pa.us 3154 : 0 : case EXPR_KIND_HAVING:
3155 : 0 : return "HAVING";
4486 noah@leadboat.com 3156 :CBC 6 : case EXPR_KIND_FILTER:
3157 : 6 : return "FILTER";
4826 tgl@sss.pgh.pa.us 3158 :UBC 0 : case EXPR_KIND_WINDOW_PARTITION:
3159 : 0 : return "window PARTITION BY";
3160 : 0 : case EXPR_KIND_WINDOW_ORDER:
3161 : 0 : return "window ORDER BY";
3162 : 0 : case EXPR_KIND_WINDOW_FRAME_RANGE:
3163 : 0 : return "window RANGE";
3164 : 0 : case EXPR_KIND_WINDOW_FRAME_ROWS:
3165 : 0 : return "window ROWS";
2819 3166 : 0 : case EXPR_KIND_WINDOW_FRAME_GROUPS:
3167 : 0 : return "window GROUPS";
4826 3168 : 0 : case EXPR_KIND_SELECT_TARGET:
3169 : 0 : return "SELECT";
3170 : 0 : case EXPR_KIND_INSERT_TARGET:
3171 : 0 : return "INSERT";
4826 tgl@sss.pgh.pa.us 3172 :CBC 3 : case EXPR_KIND_UPDATE_SOURCE:
3173 : : case EXPR_KIND_UPDATE_TARGET:
3174 : 3 : return "UPDATE";
1309 alvherre@alvh.no-ip. 3175 :UBC 0 : case EXPR_KIND_MERGE_WHEN:
3176 : 0 : return "MERGE WHEN";
4826 tgl@sss.pgh.pa.us 3177 :CBC 6 : case EXPR_KIND_GROUP_BY:
3178 : 6 : return "GROUP BY";
4826 tgl@sss.pgh.pa.us 3179 :UBC 0 : case EXPR_KIND_ORDER_BY:
3180 : 0 : return "ORDER BY";
3181 : 0 : case EXPR_KIND_DISTINCT_ON:
3182 : 0 : return "DISTINCT ON";
4826 tgl@sss.pgh.pa.us 3183 :CBC 3 : case EXPR_KIND_LIMIT:
3184 : 3 : return "LIMIT";
4826 tgl@sss.pgh.pa.us 3185 :UBC 0 : case EXPR_KIND_OFFSET:
3186 : 0 : return "OFFSET";
4826 tgl@sss.pgh.pa.us 3187 :CBC 6 : case EXPR_KIND_RETURNING:
3188 : : case EXPR_KIND_MERGE_RETURNING:
3189 : 6 : return "RETURNING";
3190 : 3 : case EXPR_KIND_VALUES:
3191 : : case EXPR_KIND_VALUES_SINGLE:
3192 : 3 : return "VALUES";
4826 tgl@sss.pgh.pa.us 3193 :UBC 0 : case EXPR_KIND_CHECK_CONSTRAINT:
3194 : : case EXPR_KIND_DOMAIN_CHECK:
3195 : 0 : return "CHECK";
3196 : 0 : case EXPR_KIND_COLUMN_DEFAULT:
3197 : : case EXPR_KIND_FUNCTION_DEFAULT:
3198 : 0 : return "DEFAULT";
3199 : 0 : case EXPR_KIND_INDEX_EXPRESSION:
3200 : 0 : return "index expression";
3201 : 0 : case EXPR_KIND_INDEX_PREDICATE:
3202 : 0 : return "index predicate";
1676 tomas.vondra@postgre 3203 : 0 : case EXPR_KIND_STATS_EXPRESSION:
3204 : 0 : return "statistics expression";
4826 tgl@sss.pgh.pa.us 3205 : 0 : case EXPR_KIND_ALTER_COL_TRANSFORM:
3206 : 0 : return "USING";
3207 : 0 : case EXPR_KIND_EXECUTE_PARAMETER:
3208 : 0 : return "EXECUTE";
3209 : 0 : case EXPR_KIND_TRIGGER_WHEN:
3210 : 0 : return "WHEN";
2467 peter@eisentraut.org 3211 : 0 : case EXPR_KIND_PARTITION_BOUND:
3212 : 0 : return "partition bound";
3246 rhaas@postgresql.org 3213 : 0 : case EXPR_KIND_PARTITION_EXPRESSION:
3214 : 0 : return "PARTITION BY";
2816 tgl@sss.pgh.pa.us 3215 : 0 : case EXPR_KIND_CALL_ARGUMENT:
2888 peter_e@gmx.net 3216 : 0 : return "CALL";
2473 tomas.vondra@postgre 3217 : 0 : case EXPR_KIND_COPY_WHERE:
3218 : 0 : return "WHERE";
2403 peter@eisentraut.org 3219 : 0 : case EXPR_KIND_GENERATED_COLUMN:
3220 : 0 : return "GENERATED AS";
1729 3221 : 0 : case EXPR_KIND_CYCLE_MARK:
3222 : 0 : 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 : : }
4826 tgl@sss.pgh.pa.us 3231 : 0 : return "unrecognized expression kind";
3232 : : }
3233 : :
3234 : : /*
3235 : : * Make string Const node from JSON encoding name.
3236 : : *
3237 : : * UTF8 is default encoding.
3238 : : */
3239 : : static Const *
943 alvherre@alvh.no-ip. 3240 :CBC 96 : getJsonEncodingConst(JsonFormat *format)
3241 : : {
3242 : : JsonEncoding encoding;
3243 : : const char *enc;
3244 : 96 : Name encname = palloc(sizeof(NameData));
3245 : :
3246 [ + - ]: 96 : if (!format ||
3247 [ + + ]: 96 : format->format_type == JS_FORMAT_DEFAULT ||
3248 [ + + ]: 66 : format->encoding == JS_ENC_DEFAULT)
3249 : 84 : encoding = JS_ENC_UTF8;
3250 : : else
3251 : 12 : encoding = format->encoding;
3252 : :
3253 [ - - + - ]: 96 : switch (encoding)
3254 : : {
943 alvherre@alvh.no-ip. 3255 :UBC 0 : case JS_ENC_UTF16:
3256 : 0 : enc = "UTF16";
3257 : 0 : break;
3258 : 0 : case JS_ENC_UTF32:
3259 : 0 : enc = "UTF32";
3260 : 0 : break;
943 alvherre@alvh.no-ip. 3261 :CBC 96 : case JS_ENC_UTF8:
3262 : 96 : enc = "UTF8";
3263 : 96 : break;
943 alvherre@alvh.no-ip. 3264 :UBC 0 : default:
3265 [ # # ]: 0 : elog(ERROR, "invalid JSON encoding: %d", encoding);
3266 : : break;
3267 : : }
3268 : :
943 alvherre@alvh.no-ip. 3269 :CBC 96 : namestrcpy(encname, enc);
3270 : :
3271 : 96 : 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 : : */
3278 : : static Node *
3279 : 66 : makeJsonByteaToTextConversion(Node *expr, JsonFormat *format, int location)
3280 : : {
3281 : 66 : Const *encoding = getJsonEncodingConst(format);
3282 : 66 : FuncExpr *fexpr = makeFuncExpr(F_CONVERT_FROM, TEXTOID,
3283 : 66 : list_make2(expr, encoding),
3284 : : InvalidOid, InvalidOid,
3285 : : COERCE_EXPLICIT_CALL);
3286 : :
3287 : 66 : fexpr->location = location;
3288 : :
3289 : 66 : 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 : : */
3300 : : static Node *
837 amitlan@postgresql.o 3301 : 2873 : transformJsonValueExpr(ParseState *pstate, const char *constructName,
3302 : : JsonValueExpr *ve, JsonFormatType default_format,
3303 : : Oid targettype, bool isarg)
3304 : : {
943 alvherre@alvh.no-ip. 3305 : 2873 : Node *expr = transformExprRecurse(pstate, (Node *) ve->raw_expr);
3306 : : Node *rawexpr;
3307 : : JsonFormatType format;
3308 : : Oid exprtype;
3309 : : int location;
3310 : : char typcategory;
3311 : : bool typispreferred;
3312 : :
3313 [ + + ]: 2873 : if (exprType(expr) == UNKNOWNOID)
843 amitlan@postgresql.o 3314 : 350 : expr = coerce_to_specific_type(pstate, expr, TEXTOID, constructName);
3315 : :
943 alvherre@alvh.no-ip. 3316 : 2873 : rawexpr = expr;
3317 : 2873 : exprtype = exprType(expr);
3318 : 2873 : location = exprLocation(expr);
3319 : :
3320 : 2873 : get_type_category_preferred(exprtype, &typcategory, &typispreferred);
3321 : :
3322 [ + + ]: 2873 : if (ve->format->format_type != JS_FORMAT_DEFAULT)
3323 : : {
3324 [ + + + + ]: 120 : if (ve->format->encoding != JS_ENC_DEFAULT && exprtype != BYTEAOID)
3325 [ + - ]: 13 : 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 [ + + + + ]: 107 : if (exprtype == JSONOID || exprtype == JSONBOID)
3331 : 6 : format = JS_FORMAT_DEFAULT; /* do not format json[b] types */
3332 : : else
3333 : 101 : format = ve->format->format_type;
3334 : : }
585 amitlan@postgresql.o 3335 [ + + ]: 2753 : 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 [ + + ]: 600 : switch (exprtype)
3344 : : {
3345 : 465 : 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 : 465 : return expr;
3360 : :
3361 : 135 : default:
3362 [ - + ]: 135 : if (typcategory == TYPCATEGORY_STRING)
585 amitlan@postgresql.o 3363 :UBC 0 : return expr;
3364 : : /* else convert argument to json[b] type */
585 amitlan@postgresql.o 3365 :CBC 135 : break;
3366 : : }
3367 : :
3368 : 135 : format = default_format;
3369 : : }
943 alvherre@alvh.no-ip. 3370 [ + + + + ]: 2153 : else if (exprtype == JSONOID || exprtype == JSONBOID)
3371 : 1503 : format = JS_FORMAT_DEFAULT; /* do not format json[b] types */
3372 : : else
3373 : 650 : format = default_format;
3374 : :
830 amitlan@postgresql.o 3375 [ + + + + ]: 2395 : if (format != JS_FORMAT_DEFAULT ||
3376 [ + + ]: 1502 : (OidIsValid(targettype) && exprtype != targettype))
3377 : : {
3378 : : Node *coerced;
3379 : 391 : bool only_allow_cast = OidIsValid(targettype);
3380 : :
3381 : : /*
3382 : : * PASSING args are handled appropriately by GetJsonPathVar() /
3383 : : * JsonItemFromDatum().
3384 : : */
585 3385 [ + + ]: 391 : if (!isarg &&
3386 [ + + + + ]: 256 : !only_allow_cast &&
830 3387 [ + + ]: 95 : exprtype != BYTEAOID && typcategory != TYPCATEGORY_STRING)
943 alvherre@alvh.no-ip. 3388 [ + - - + : 3 : ereport(ERROR,
+ - ]
3389 : : errcode(ERRCODE_DATATYPE_MISMATCH),
3390 : : ve->format->format_type == JS_FORMAT_DEFAULT ?
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 [ + + + + ]: 388 : if (format == JS_FORMAT_JSON && exprtype == BYTEAOID)
3398 : : {
3399 : 36 : expr = makeJsonByteaToTextConversion(expr, ve->format, location);
3400 : 36 : exprtype = TEXTOID;
3401 : : }
3402 : :
830 amitlan@postgresql.o 3403 [ + + ]: 388 : if (!OidIsValid(targettype))
3404 [ + + ]: 254 : targettype = format == JS_FORMAT_JSONB ? JSONBOID : JSONOID;
3405 : :
3406 : : /* Try to coerce to the target type. */
943 alvherre@alvh.no-ip. 3407 : 388 : coerced = coerce_to_target_type(pstate, expr, exprtype,
3408 : : targettype, -1,
3409 : : COERCION_EXPLICIT,
3410 : : COERCE_EXPLICIT_CAST,
3411 : : location);
3412 : :
3413 [ + + ]: 388 : 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 : : */
830 amitlan@postgresql.o 3423 [ + + ]: 15 : if (only_allow_cast)
3424 [ + - ]: 3 : 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 [ - + ]: 12 : fnoid = targettype == JSONOID ? F_TO_JSON : F_TO_JSONB;
3432 : 12 : fexpr = makeFuncExpr(fnoid, targettype, list_make1(expr),
3433 : : InvalidOid, InvalidOid, COERCE_EXPLICIT_CALL);
3434 : :
943 alvherre@alvh.no-ip. 3435 : 12 : fexpr->location = location;
3436 : :
3437 : 12 : coerced = (Node *) fexpr;
3438 : : }
3439 : :
843 amitlan@postgresql.o 3440 [ - + ]: 385 : if (coerced == expr)
943 alvherre@alvh.no-ip. 3441 :UBC 0 : expr = rawexpr;
3442 : : else
3443 : : {
943 alvherre@alvh.no-ip. 3444 :CBC 385 : ve = copyObject(ve);
3445 : 385 : ve->raw_expr = (Expr *) rawexpr;
3446 : 385 : ve->formatted_expr = (Expr *) coerced;
3447 : :
3448 : 385 : expr = (Node *) ve;
3449 : : }
3450 : : }
3451 : :
3452 : : /* If returning a JsonValueExpr, formatted_expr must have been set. */
829 amitlan@postgresql.o 3453 [ + + - + ]: 2389 : Assert(!IsA(expr, JsonValueExpr) ||
3454 : : ((JsonValueExpr *) expr)->formatted_expr != NULL);
3455 : :
943 alvherre@alvh.no-ip. 3456 : 2389 : return expr;
3457 : : }
3458 : :
3459 : : /*
3460 : : * Checks specified output format for its applicability to the target type.
3461 : : */
3462 : : static void
3463 : 122 : checkJsonOutputFormat(ParseState *pstate, const JsonFormat *format,
3464 : : Oid targettype, bool allow_format_for_non_strings)
3465 : : {
3466 [ + + ]: 122 : if (!allow_format_for_non_strings &&
3467 [ + - + + ]: 72 : format->format_type != JS_FORMAT_DEFAULT &&
3468 [ + + ]: 63 : (targettype != BYTEAOID &&
3469 [ + + ]: 60 : targettype != JSONOID &&
3470 : : targettype != JSONBOID))
3471 : : {
3472 : : char typcategory;
3473 : : bool typispreferred;
3474 : :
3475 : 45 : get_type_category_preferred(targettype, &typcategory, &typispreferred);
3476 : :
3477 [ - + ]: 45 : if (typcategory != TYPCATEGORY_STRING)
943 alvherre@alvh.no-ip. 3478 [ # # ]:UBC 0 : 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 : :
943 alvherre@alvh.no-ip. 3484 [ + - ]:CBC 122 : if (format->format_type == JS_FORMAT_JSON)
3485 : : {
3486 : 244 : JsonEncoding enc = format->encoding != JS_ENC_DEFAULT ?
892 tgl@sss.pgh.pa.us 3487 [ + + ]: 122 : format->encoding : JS_ENC_UTF8;
3488 : :
943 alvherre@alvh.no-ip. 3489 [ + + ]: 122 : if (targettype != BYTEAOID &&
3490 [ + + ]: 89 : format->encoding != JS_ENC_DEFAULT)
3491 [ + - ]: 6 : 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 [ + + ]: 116 : if (enc != JS_ENC_UTF8)
3497 [ + - ]: 12 : 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 : 104 : }
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 : : */
3512 : : static JsonReturning *
3513 : 2065 : transformJsonOutput(ParseState *pstate, const JsonOutput *output,
3514 : : bool allow_format)
3515 : : {
3516 : : JsonReturning *ret;
3517 : :
3518 : : /* if output clause is not specified, make default clause value */
3519 [ + + ]: 2065 : if (!output)
3520 : : {
3521 : 898 : ret = makeNode(JsonReturning);
3522 : :
3523 : 898 : ret->format = makeJsonFormat(JS_FORMAT_DEFAULT, JS_ENC_DEFAULT, -1);
3524 : 898 : ret->typid = InvalidOid;
3525 : 898 : ret->typmod = -1;
3526 : :
3527 : 898 : return ret;
3528 : : }
3529 : :
3530 : 1167 : ret = copyObject(output->returning);
3531 : :
3532 : 1167 : typenameTypeIdAndMod(pstate, output->typeName, &ret->typid, &ret->typmod);
3533 : :
3534 [ - + ]: 1167 : if (output->typeName->setof)
943 alvherre@alvh.no-ip. 3535 [ # # ]:UBC 0 : ereport(ERROR,
3536 : : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3537 : : errmsg("returning SETOF types is not supported in SQL/JSON functions"));
3538 : :
585 amitlan@postgresql.o 3539 [ + + ]:CBC 1167 : if (get_typtype(ret->typid) == TYPTYPE_PSEUDO)
3540 [ + - ]: 6 : ereport(ERROR,
3541 : : errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3542 : : errmsg("returning pseudo-types is not supported in SQL/JSON functions"));
3543 : :
943 alvherre@alvh.no-ip. 3544 [ + + ]: 1161 : if (ret->format->format_type == JS_FORMAT_DEFAULT)
3545 : : /* assign JSONB format when returning jsonb, or JSON format otherwise */
3546 : 1039 : ret->format->format_type =
3547 [ + + ]: 1039 : ret->typid == JSONBOID ? JS_FORMAT_JSONB : JS_FORMAT_JSON;
3548 : : else
3549 : 122 : checkJsonOutputFormat(pstate, ret->format, ret->typid, allow_format);
3550 : :
3551 : 1143 : 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 : : */
3559 : : static JsonReturning *
3560 : 505 : transformJsonConstructorOutput(ParseState *pstate, JsonOutput *output,
3561 : : List *args)
3562 : : {
3563 : 505 : JsonReturning *returning = transformJsonOutput(pstate, output, true);
3564 : :
3565 [ + + ]: 487 : if (!OidIsValid(returning->typid))
3566 : : {
3567 : : ListCell *lc;
3568 : 248 : bool have_jsonb = false;
3569 : :
3570 [ + + + + : 828 : foreach(lc, args)
+ + ]
3571 : : {
3572 : 595 : Node *expr = lfirst(lc);
3573 : 595 : Oid typid = exprType(expr);
3574 : :
3575 : 595 : have_jsonb |= typid == JSONBOID;
3576 : :
3577 [ + + ]: 595 : if (have_jsonb)
3578 : 15 : break;
3579 : : }
3580 : :
3581 [ + + ]: 248 : if (have_jsonb)
3582 : : {
3583 : 15 : returning->typid = JSONBOID;
3584 : 15 : returning->format->format_type = JS_FORMAT_JSONB;
3585 : : }
3586 : : else
3587 : : {
3588 : : /* XXX TEXT is default by the standard, but we return JSON */
3589 : 233 : returning->typid = JSONOID;
3590 : 233 : returning->format->format_type = JS_FORMAT_JSON;
3591 : : }
3592 : :
3593 : 248 : returning->typmod = -1;
3594 : : }
3595 : :
3596 : 487 : return returning;
3597 : : }
3598 : :
3599 : : /*
3600 : : * Coerce json[b]-valued function expression to the output type.
3601 : : */
3602 : : static Node *
3603 : 643 : coerceJsonFuncExpr(ParseState *pstate, Node *expr,
3604 : : const JsonReturning *returning, bool report_error)
3605 : : {
3606 : : Node *res;
3607 : : int location;
3608 : 643 : Oid exprtype = exprType(expr);
3609 : :
3610 : : /* if output type is not specified or equals to function type, return */
3611 [ + - + + ]: 643 : if (!OidIsValid(returning->typid) || returning->typid == exprtype)
3612 : 488 : return expr;
3613 : :
3614 : 155 : location = exprLocation(expr);
3615 : :
3616 [ + - ]: 155 : if (location < 0)
3617 : 155 : location = returning->format->location;
3618 : :
3619 : : /* special case for RETURNING bytea FORMAT json */
3620 [ + - ]: 155 : if (returning->format->format_type == JS_FORMAT_JSON &&
3621 [ + + ]: 155 : returning->typid == BYTEAOID)
3622 : : {
3623 : : /* encode json text into bytea using pg_convert_to() */
3624 : 30 : Node *texpr = coerce_to_specific_type(pstate, expr, TEXTOID,
3625 : : "JSON_FUNCTION");
3626 : 30 : Const *enc = getJsonEncodingConst(returning->format);
3627 : 30 : FuncExpr *fexpr = makeFuncExpr(F_CONVERT_TO, BYTEAOID,
3628 : 30 : list_make2(texpr, enc),
3629 : : InvalidOid, InvalidOid,
3630 : : COERCE_EXPLICIT_CALL);
3631 : :
3632 : 30 : fexpr->location = location;
3633 : :
3634 : 30 : 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 : 125 : res = coerce_to_target_type(pstate, expr, exprtype,
467 amitlan@postgresql.o 3647 : 125 : returning->typid, returning->typmod,
3648 : : COERCION_ASSIGNMENT,
3649 : : COERCE_IMPLICIT_CAST,
3650 : : location);
3651 : :
943 alvherre@alvh.no-ip. 3652 [ - + - - ]: 125 : if (!res && report_error)
943 alvherre@alvh.no-ip. 3653 [ # # ]:UBC 0 : 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 : :
943 alvherre@alvh.no-ip. 3660 :CBC 125 : return res;
3661 : : }
3662 : :
3663 : : /*
3664 : : * Make a JsonConstructorExpr node.
3665 : : */
3666 : : static Node *
3667 : 643 : makeJsonConstructorExpr(ParseState *pstate, JsonConstructorType type,
3668 : : List *args, Expr *fexpr, JsonReturning *returning,
3669 : : bool unique, bool absent_on_null, int location)
3670 : : {
3671 : 643 : JsonConstructorExpr *jsctor = makeNode(JsonConstructorExpr);
3672 : : Node *placeholder;
3673 : : Node *coercion;
3674 : :
3675 : 643 : jsctor->args = args;
3676 : 643 : jsctor->func = fexpr;
3677 : 643 : jsctor->type = type;
3678 : 643 : jsctor->returning = returning;
3679 : 643 : jsctor->unique = unique;
3680 : 643 : jsctor->absent_on_null = absent_on_null;
3681 : 643 : 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 [ + + ]: 643 : if (fexpr)
3690 : : {
843 amitlan@postgresql.o 3691 : 201 : CaseTestExpr *cte = makeNode(CaseTestExpr);
3692 : :
3693 : 201 : cte->typeId = exprType((Node *) fexpr);
3694 : 201 : cte->typeMod = exprTypmod((Node *) fexpr);
3695 : 201 : cte->collation = exprCollation((Node *) fexpr);
3696 : :
3697 : 201 : placeholder = (Node *) cte;
3698 : : }
3699 : : else
3700 : : {
943 alvherre@alvh.no-ip. 3701 : 442 : CaseTestExpr *cte = makeNode(CaseTestExpr);
3702 : :
942 3703 : 884 : cte->typeId = returning->format->format_type == JS_FORMAT_JSONB ?
3704 [ + + ]: 442 : JSONBOID : JSONOID;
943 3705 : 442 : cte->typeMod = -1;
3706 : 442 : cte->collation = InvalidOid;
3707 : :
3708 : 442 : placeholder = (Node *) cte;
3709 : : }
3710 : :
3711 : 643 : coercion = coerceJsonFuncExpr(pstate, placeholder, returning, true);
3712 : :
3713 [ + + ]: 643 : if (coercion != placeholder)
3714 : 155 : jsctor->coercion = (Expr *) coercion;
3715 : :
3716 : 643 : 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 : : */
3726 : : static Node *
3727 : 215 : transformJsonObjectConstructor(ParseState *pstate, JsonObjectConstructor *ctor)
3728 : : {
3729 : : JsonReturning *returning;
3730 : 215 : List *args = NIL;
3731 : :
3732 : : /* transform key-value pairs, if any */
3733 [ + + ]: 215 : if (ctor->exprs)
3734 : : {
3735 : : ListCell *lc;
3736 : :
3737 : : /* transform and append key-value arguments */
3738 [ + - + + : 454 : foreach(lc, ctor->exprs)
+ + ]
3739 : : {
3740 : 295 : JsonKeyValue *kv = castNode(JsonKeyValue, lfirst(lc));
3741 : 295 : Node *key = transformExprRecurse(pstate, (Node *) kv->key);
843 amitlan@postgresql.o 3742 : 295 : Node *val = transformJsonValueExpr(pstate, "JSON_OBJECT()",
3743 : : kv->value,
3744 : : JS_FORMAT_DEFAULT,
3745 : : InvalidOid, false);
3746 : :
943 alvherre@alvh.no-ip. 3747 : 283 : args = lappend(args, key);
3748 : 283 : args = lappend(args, val);
3749 : : }
3750 : : }
3751 : :
3752 : 203 : returning = transformJsonConstructorOutput(pstate, ctor->output, args);
3753 : :
3754 : 388 : return makeJsonConstructorExpr(pstate, JSCTOR_JSON_OBJECT, args, NULL,
3755 : 194 : returning, ctor->unique,
3756 : 194 : 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 : : */
3763 : : static Node *
3764 : 30 : transformJsonArrayQueryConstructor(ParseState *pstate,
3765 : : JsonArrayQueryConstructor *ctor)
3766 : : {
3767 : 30 : SubLink *sublink = makeNode(SubLink);
3768 : 30 : SelectStmt *select = makeNode(SelectStmt);
3769 : 30 : RangeSubselect *range = makeNode(RangeSubselect);
3770 : 30 : Alias *alias = makeNode(Alias);
3771 : 30 : ResTarget *target = makeNode(ResTarget);
3772 : 30 : JsonArrayAgg *agg = makeNode(JsonArrayAgg);
3773 : 30 : ColumnRef *colref = makeNode(ColumnRef);
3774 : : Query *query;
3775 : : ParseState *qpstate;
3776 : :
3777 : : /* Transform query only for counting target list entries. */
3778 : 30 : qpstate = make_parsestate(pstate);
3779 : :
205 tgl@sss.pgh.pa.us 3780 : 30 : query = transformStmt(qpstate, copyObject(ctor->query));
3781 : :
943 alvherre@alvh.no-ip. 3782 [ + + ]: 30 : if (count_nonjunk_tlist_entries(query->targetList) != 1)
3783 [ + - ]: 9 : ereport(ERROR,
3784 : : errcode(ERRCODE_SYNTAX_ERROR),
3785 : : errmsg("subquery must return only one column"),
3786 : : parser_errposition(pstate, ctor->location));
3787 : :
3788 : 21 : free_parsestate(qpstate);
3789 : :
3790 : 21 : colref->fields = list_make2(makeString(pstrdup("q")),
3791 : : makeString(pstrdup("a")));
3792 : 21 : colref->location = ctor->location;
3793 : :
3794 : : /*
3795 : : * No formatting necessary, so set formatted_expr to be the same as
3796 : : * raw_expr.
3797 : : */
829 amitlan@postgresql.o 3798 : 21 : agg->arg = makeJsonValueExpr((Expr *) colref, (Expr *) colref,
3799 : : ctor->format);
943 alvherre@alvh.no-ip. 3800 : 21 : agg->absent_on_null = ctor->absent_on_null;
3801 : 21 : agg->constructor = makeNode(JsonAggConstructor);
3802 : 21 : agg->constructor->agg_order = NIL;
3803 : 21 : agg->constructor->output = ctor->output;
3804 : 21 : agg->constructor->location = ctor->location;
3805 : :
3806 : 21 : target->name = NULL;
3807 : 21 : target->indirection = NIL;
3808 : 21 : target->val = (Node *) agg;
3809 : 21 : target->location = ctor->location;
3810 : :
3811 : 21 : alias->aliasname = pstrdup("q");
3812 : 21 : alias->colnames = list_make1(makeString(pstrdup("a")));
3813 : :
3814 : 21 : range->lateral = false;
3815 : 21 : range->subquery = ctor->query;
3816 : 21 : range->alias = alias;
3817 : :
3818 : 21 : select->targetList = list_make1(target);
3819 : 21 : select->fromClause = list_make1(range);
3820 : :
3821 : 21 : sublink->subLinkType = EXPR_SUBLINK;
3822 : 21 : sublink->subLinkId = 0;
3823 : 21 : sublink->testexpr = NULL;
3824 : 21 : sublink->operName = NIL;
3825 : 21 : sublink->subselect = (Node *) select;
3826 : 21 : sublink->location = ctor->location;
3827 : :
3828 : 21 : return transformExprRecurse(pstate, (Node *) sublink);
3829 : : }
3830 : :
3831 : : /*
3832 : : * Common code for JSON_OBJECTAGG and JSON_ARRAYAGG transformation.
3833 : : */
3834 : : static Node *
3835 : 201 : transformJsonAggConstructor(ParseState *pstate, JsonAggConstructor *agg_ctor,
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 : :
942 3844 : 402 : aggfilter = agg_ctor->agg_filter ? (Expr *)
3845 : 21 : transformWhereClause(pstate, agg_ctor->agg_filter,
3846 [ + + ]: 201 : EXPR_KIND_FILTER, "FILTER") : NULL;
3847 : :
943 3848 [ + + ]: 201 : if (agg_ctor->over)
3849 : : {
3850 : : /* window function */
3851 : 24 : WindowFunc *wfunc = makeNode(WindowFunc);
3852 : :
3853 : 24 : wfunc->winfnoid = aggfnoid;
3854 : 24 : wfunc->wintype = aggtype;
3855 : : /* wincollid and inputcollid will be set by parse_collate.c */
3856 : 24 : wfunc->args = args;
942 3857 : 24 : wfunc->aggfilter = aggfilter;
540 drowley@postgresql.o 3858 : 24 : wfunc->runCondition = NIL;
3859 : : /* winref will be set by transformWindowFuncCall */
943 alvherre@alvh.no-ip. 3860 : 24 : wfunc->winstar = false;
3861 : 24 : wfunc->winagg = true;
3862 : 24 : wfunc->location = agg_ctor->location;
3863 : :
3864 : : /*
3865 : : * ordered aggs not allowed in windows yet
3866 : : */
3867 [ - + ]: 24 : if (agg_ctor->agg_order != NIL)
943 alvherre@alvh.no-ip. 3868 [ # # ]:UBC 0 : 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 */
943 alvherre@alvh.no-ip. 3874 :CBC 24 : transformWindowFuncCall(pstate, wfunc, agg_ctor->over);
3875 : :
3876 : 24 : node = (Node *) wfunc;
3877 : : }
3878 : : else
3879 : : {
3880 : 177 : Aggref *aggref = makeNode(Aggref);
3881 : :
3882 : 177 : aggref->aggfnoid = aggfnoid;
3883 : 177 : 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 : 177 : aggref->aggfilter = aggfilter;
3891 : 177 : aggref->aggstar = false;
3892 : 177 : aggref->aggvariadic = false;
3893 : 177 : aggref->aggkind = AGGKIND_NORMAL;
942 3894 : 177 : aggref->aggpresorted = false;
3895 : : /* agglevelsup will be set by transformAggregateCall */
943 3896 : 177 : aggref->aggsplit = AGGSPLIT_SIMPLE; /* planner might change this */
942 3897 : 177 : aggref->aggno = -1; /* planner will set aggno and aggtransno */
3898 : 177 : aggref->aggtransno = -1;
943 3899 : 177 : aggref->location = agg_ctor->location;
3900 : :
3901 : 177 : transformAggregateCall(pstate, aggref, args, agg_ctor->agg_order, false);
3902 : :
3903 : 177 : node = (Node *) aggref;
3904 : : }
3905 : :
3906 : 201 : 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 : : */
3920 : : static Node *
3921 : 102 : transformJsonObjectAgg(ParseState *pstate, JsonObjectAgg *agg)
3922 : : {
3923 : : JsonReturning *returning;
3924 : : Node *key;
3925 : : Node *val;
3926 : : List *args;
3927 : : Oid aggfnoid;
3928 : : Oid aggtype;
3929 : :
3930 : 102 : key = transformExprRecurse(pstate, (Node *) agg->arg->key);
843 amitlan@postgresql.o 3931 : 102 : val = transformJsonValueExpr(pstate, "JSON_OBJECTAGG()",
3932 : 102 : agg->arg->value,
3933 : : JS_FORMAT_DEFAULT,
3934 : : InvalidOid, false);
943 alvherre@alvh.no-ip. 3935 : 102 : args = list_make2(key, val);
3936 : :
3937 : 102 : returning = transformJsonConstructorOutput(pstate, agg->constructor->output,
3938 : : args);
3939 : :
3940 [ + + ]: 102 : if (returning->format->format_type == JS_FORMAT_JSONB)
3941 : : {
3942 [ + + ]: 27 : if (agg->absent_on_null)
3943 [ + + ]: 9 : if (agg->unique)
942 3944 : 6 : aggfnoid = F_JSONB_OBJECT_AGG_UNIQUE_STRICT;
3945 : : else
3946 : 3 : aggfnoid = F_JSONB_OBJECT_AGG_STRICT;
943 3947 [ + + ]: 18 : else if (agg->unique)
942 3948 : 3 : aggfnoid = F_JSONB_OBJECT_AGG_UNIQUE;
3949 : : else
3950 : 15 : aggfnoid = F_JSONB_OBJECT_AGG;
3951 : :
943 3952 : 27 : aggtype = JSONBOID;
3953 : : }
3954 : : else
3955 : : {
3956 [ + + ]: 75 : if (agg->absent_on_null)
3957 [ + + ]: 18 : if (agg->unique)
942 3958 : 9 : aggfnoid = F_JSON_OBJECT_AGG_UNIQUE_STRICT;
3959 : : else
3960 : 9 : aggfnoid = F_JSON_OBJECT_AGG_STRICT;
943 3961 [ + + ]: 57 : else if (agg->unique)
942 3962 : 24 : aggfnoid = F_JSON_OBJECT_AGG_UNIQUE;
3963 : : else
3964 : 33 : aggfnoid = F_JSON_OBJECT_AGG;
3965 : :
943 3966 : 75 : aggtype = JSONOID;
3967 : : }
3968 : :
3969 : 204 : return transformJsonAggConstructor(pstate, agg->constructor, returning,
3970 : : args, aggfnoid, aggtype,
3971 : : JSCTOR_JSON_OBJECTAGG,
3972 : 102 : 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 : : */
3984 : : static Node *
3985 : 99 : transformJsonArrayAgg(ParseState *pstate, JsonArrayAgg *agg)
3986 : : {
3987 : : JsonReturning *returning;
3988 : : Node *arg;
3989 : : Oid aggfnoid;
3990 : : Oid aggtype;
3991 : :
585 amitlan@postgresql.o 3992 : 99 : arg = transformJsonValueExpr(pstate, "JSON_ARRAYAGG()", agg->arg,
3993 : : JS_FORMAT_DEFAULT, InvalidOid, false);
3994 : :
943 alvherre@alvh.no-ip. 3995 : 99 : returning = transformJsonConstructorOutput(pstate, agg->constructor->output,
3996 : 99 : list_make1(arg));
3997 : :
3998 [ + + ]: 99 : if (returning->format->format_type == JS_FORMAT_JSONB)
3999 : : {
942 4000 [ + + ]: 36 : aggfnoid = agg->absent_on_null ? F_JSONB_AGG_STRICT : F_JSONB_AGG;
943 4001 : 36 : aggtype = JSONBOID;
4002 : : }
4003 : : else
4004 : : {
942 4005 [ + + ]: 63 : aggfnoid = agg->absent_on_null ? F_JSON_AGG_STRICT : F_JSON_AGG;
943 4006 : 63 : aggtype = JSONOID;
4007 : : }
4008 : :
4009 : 99 : return transformJsonAggConstructor(pstate, agg->constructor, returning,
942 4010 : 99 : list_make1(arg), aggfnoid, aggtype,
4011 : : JSCTOR_JSON_ARRAYAGG,
943 4012 : 99 : 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 : : */
4022 : : static Node *
4023 : 101 : transformJsonArrayConstructor(ParseState *pstate, JsonArrayConstructor *ctor)
4024 : : {
4025 : : JsonReturning *returning;
4026 : 101 : List *args = NIL;
4027 : :
4028 : : /* transform element expressions, if any */
4029 [ + + ]: 101 : if (ctor->exprs)
4030 : : {
4031 : : ListCell *lc;
4032 : :
4033 : : /* transform and append element arguments */
4034 [ + - + + : 189 : foreach(lc, ctor->exprs)
+ + ]
4035 : : {
4036 : 129 : JsonValueExpr *jsval = castNode(JsonValueExpr, lfirst(lc));
843 amitlan@postgresql.o 4037 : 129 : Node *val = transformJsonValueExpr(pstate, "JSON_ARRAY()",
4038 : : jsval, JS_FORMAT_DEFAULT,
4039 : : InvalidOid, false);
4040 : :
943 alvherre@alvh.no-ip. 4041 : 129 : args = lappend(args, val);
4042 : : }
4043 : : }
4044 : :
4045 : 101 : returning = transformJsonConstructorOutput(pstate, ctor->output, args);
4046 : :
4047 : 184 : return makeJsonConstructorExpr(pstate, JSCTOR_JSON_ARRAY, args, NULL,
4048 : 92 : returning, false, ctor->absent_on_null,
4049 : : ctor->location);
4050 : : }
4051 : :
4052 : : static Node *
941 4053 : 178 : transformJsonParseArg(ParseState *pstate, Node *jsexpr, JsonFormat *format,
4054 : : Oid *exprtype)
4055 : : {
4056 : 178 : Node *raw_expr = transformExprRecurse(pstate, jsexpr);
4057 : 178 : Node *expr = raw_expr;
4058 : :
4059 : 178 : *exprtype = exprType(expr);
4060 : :
4061 : : /* prepare input document */
4062 [ + + ]: 178 : if (*exprtype == BYTEAOID)
4063 : : {
4064 : : JsonValueExpr *jve;
4065 : :
843 amitlan@postgresql.o 4066 : 30 : expr = raw_expr;
941 alvherre@alvh.no-ip. 4067 : 30 : expr = makeJsonByteaToTextConversion(expr, format, exprLocation(expr));
4068 : 30 : *exprtype = TEXTOID;
4069 : :
829 amitlan@postgresql.o 4070 : 30 : jve = makeJsonValueExpr((Expr *) raw_expr, (Expr *) expr, format);
941 alvherre@alvh.no-ip. 4071 : 30 : expr = (Node *) jve;
4072 : : }
4073 : : else
4074 : : {
4075 : : char typcategory;
4076 : : bool typispreferred;
4077 : :
4078 : 148 : get_type_category_preferred(*exprtype, &typcategory, &typispreferred);
4079 : :
4080 [ + + + + ]: 148 : if (*exprtype == UNKNOWNOID || typcategory == TYPCATEGORY_STRING)
4081 : : {
4082 : 87 : expr = coerce_to_target_type(pstate, (Node *) expr, *exprtype,
4083 : : TEXTOID, -1,
4084 : : COERCION_IMPLICIT,
4085 : : COERCE_IMPLICIT_CAST, -1);
4086 : 87 : *exprtype = TEXTOID;
4087 : : }
4088 : :
4089 [ - + ]: 148 : if (format->encoding != JS_ENC_DEFAULT)
941 alvherre@alvh.no-ip. 4090 [ # # ]:UBC 0 : 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 : :
941 alvherre@alvh.no-ip. 4096 :CBC 178 : return expr;
4097 : : }
4098 : :
4099 : : /*
4100 : : * Transform IS JSON predicate.
4101 : : */
4102 : : static Node *
4103 : 167 : transformJsonIsPredicate(ParseState *pstate, JsonIsPredicate *pred)
4104 : : {
4105 : : Oid exprtype;
4106 : 167 : Node *expr = transformJsonParseArg(pstate, pred->expr, pred->format,
4107 : : &exprtype);
4108 : :
4109 : : /* make resulting expression */
4110 [ + + + + : 167 : if (exprtype != TEXTOID && exprtype != JSONOID && exprtype != JSONBOID)
+ + ]
4111 [ + - ]: 3 : 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 : 328 : return makeJsonIsPredicate(expr, NULL, pred->item_type,
4118 : 164 : 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 : : */
4125 : : static JsonReturning *
830 amitlan@postgresql.o 4126 : 123 : transformJsonReturning(ParseState *pstate, JsonOutput *output, const char *fname)
4127 : : {
4128 : : JsonReturning *returning;
4129 : :
4130 [ - + ]: 123 : if (output)
4131 : : {
830 amitlan@postgresql.o 4132 :UBC 0 : returning = transformJsonOutput(pstate, output, false);
4133 : :
4134 [ # # ]: 0 : Assert(OidIsValid(returning->typid));
4135 : :
4136 [ # # # # ]: 0 : if (returning->typid != JSONOID && returning->typid != JSONBOID)
4137 [ # # ]: 0 : 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. */
830 amitlan@postgresql.o 4147 :CBC 123 : Oid targettype = JSONOID;
4148 : 123 : JsonFormatType format = JS_FORMAT_JSON;
4149 : :
4150 : 123 : returning = makeNode(JsonReturning);
4151 : 123 : returning->format = makeJsonFormat(format, JS_ENC_DEFAULT, -1);
4152 : 123 : returning->typid = targettype;
4153 : 123 : returning->typmod = -1;
4154 : : }
4155 : :
4156 : 123 : 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 : : */
4165 : : static Node *
4166 : 74 : transformJsonParseExpr(ParseState *pstate, JsonParseExpr *jsexpr)
4167 : : {
4168 : 74 : JsonOutput *output = jsexpr->output;
4169 : : JsonReturning *returning;
4170 : : Node *arg;
4171 : :
4172 : 74 : returning = transformJsonReturning(pstate, output, "JSON()");
4173 : :
4174 [ + + ]: 74 : 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 : 11 : JsonValueExpr *jve = jsexpr->expr;
4181 : : Oid arg_type;
4182 : :
4183 : 11 : arg = transformJsonParseArg(pstate, (Node *) jve->raw_expr, jve->format,
4184 : : &arg_type);
4185 : :
4186 [ + + ]: 11 : if (arg_type != TEXTOID)
4187 [ + - ]: 4 : 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 : 63 : arg = transformJsonValueExpr(pstate, "JSON()", jsexpr->expr,
4199 : : JS_FORMAT_JSON, returning->typid, false);
4200 : : }
4201 : :
4202 : 63 : return makeJsonConstructorExpr(pstate, JSCTOR_JSON_PARSE, list_make1(arg), NULL,
4203 : 63 : 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 : : */
4214 : : static Node *
4215 : 49 : transformJsonScalarExpr(ParseState *pstate, JsonScalarExpr *jsexpr)
4216 : : {
4217 : 49 : Node *arg = transformExprRecurse(pstate, (Node *) jsexpr->expr);
4218 : 49 : JsonOutput *output = jsexpr->output;
4219 : : JsonReturning *returning;
4220 : :
4221 : 49 : returning = transformJsonReturning(pstate, output, "JSON_SCALAR()");
4222 : :
4223 [ + + ]: 49 : if (exprType(arg) == UNKNOWNOID)
4224 : 11 : arg = coerce_to_specific_type(pstate, arg, TEXTOID, "JSON_SCALAR");
4225 : :
4226 : 49 : return makeJsonConstructorExpr(pstate, JSCTOR_JSON_SCALAR, list_make1(arg), NULL,
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 : : */
4237 : : static Node *
4238 : 48 : transformJsonSerializeExpr(ParseState *pstate, JsonSerializeExpr *expr)
4239 : : {
4240 : : JsonReturning *returning;
4241 : 48 : Node *arg = transformJsonValueExpr(pstate, "JSON_SERIALIZE()",
4242 : : expr->expr,
4243 : : JS_FORMAT_JSON,
4244 : : InvalidOid, false);
4245 : :
4246 [ + + ]: 48 : if (expr->output)
4247 : : {
4248 : 23 : returning = transformJsonOutput(pstate, expr->output, true);
4249 : :
4250 [ + + ]: 23 : if (returning->typid != BYTEAOID)
4251 : : {
4252 : : char typcategory;
4253 : : bool typispreferred;
4254 : :
4255 : 17 : get_type_category_preferred(returning->typid, &typcategory,
4256 : : &typispreferred);
4257 [ + + ]: 17 : if (typcategory != TYPCATEGORY_STRING)
4258 [ + - ]: 4 : 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 : 25 : returning = makeNode(JsonReturning);
4270 : 25 : returning->format = makeJsonFormat(JS_FORMAT_JSON, JS_ENC_DEFAULT, -1);
4271 : 25 : returning->typid = TEXTOID;
4272 : 25 : returning->typmod = -1;
4273 : : }
4274 : :
4275 : 44 : return makeJsonConstructorExpr(pstate, JSCTOR_JSON_SERIALIZE, list_make1(arg),
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 : : */
4283 : : static Node *
585 4284 : 1567 : transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
4285 : : {
4286 : : JsonExpr *jsexpr;
4287 : : Node *path_spec;
4288 : 1567 : const char *func_name = NULL;
4289 : : JsonFormatType default_format;
4290 : :
4291 [ + + + + : 1567 : switch (func->op)
- ]
4292 : : {
4293 : 153 : case JSON_EXISTS_OP:
4294 : 153 : func_name = "JSON_EXISTS";
4295 : 153 : default_format = JS_FORMAT_DEFAULT;
4296 : 153 : break;
4297 : 633 : case JSON_QUERY_OP:
4298 : 633 : func_name = "JSON_QUERY";
4299 : 633 : default_format = JS_FORMAT_JSONB;
4300 : 633 : break;
4301 : 545 : case JSON_VALUE_OP:
4302 : 545 : func_name = "JSON_VALUE";
4303 : 545 : default_format = JS_FORMAT_DEFAULT;
4304 : 545 : break;
571 4305 : 236 : case JSON_TABLE_OP:
4306 : 236 : func_name = "JSON_TABLE";
4307 : 236 : default_format = JS_FORMAT_JSONB;
4308 : 236 : break;
585 amitlan@postgresql.o 4309 :UBC 0 : default:
4310 [ # # ]: 0 : 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 : : */
585 amitlan@postgresql.o 4320 [ + + + + ]:CBC 1567 : if (func->output && func->op != JSON_QUERY_OP)
4321 : : {
4322 : 524 : JsonFormat *format = func->output->returning->format;
4323 : :
4324 [ + + ]: 524 : if (format->format_type != JS_FORMAT_DEFAULT ||
4325 [ - + ]: 521 : format->encoding != JS_ENC_DEFAULT)
4326 [ + - ]: 3 : 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. */
486 4334 [ + + ]: 1564 : if (func->op == JSON_QUERY_OP)
4335 : : {
4336 [ + + ]: 633 : if (func->quotes == JS_QUOTES_OMIT &&
4337 [ + + ]: 90 : (func->wrapper == JSW_CONDITIONAL ||
4338 [ + + ]: 87 : func->wrapper == JSW_UNCONDITIONAL))
4339 [ + - ]: 9 : 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 [ + + ]: 624 : if (func->on_empty != NULL &&
4344 [ + + ]: 48 : func->on_empty->btype != JSON_BEHAVIOR_ERROR &&
4345 [ + + ]: 30 : func->on_empty->btype != JSON_BEHAVIOR_NULL &&
4346 [ + - ]: 27 : func->on_empty->btype != JSON_BEHAVIOR_EMPTY &&
4347 [ + + ]: 27 : func->on_empty->btype != JSON_BEHAVIOR_EMPTY_ARRAY &&
4348 [ + + ]: 15 : func->on_empty->btype != JSON_BEHAVIOR_EMPTY_OBJECT &&
4349 [ - + ]: 12 : func->on_empty->btype != JSON_BEHAVIOR_DEFAULT)
4350 : : {
486 amitlan@postgresql.o 4351 [ # # ]:UBC 0 : if (func->column_name == NULL)
4352 [ # # ]: 0 : 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 [ # # ]: 0 : 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 : : }
486 amitlan@postgresql.o 4372 [ + + ]:CBC 624 : if (func->on_error != NULL &&
4373 [ + + ]: 165 : func->on_error->btype != JSON_BEHAVIOR_ERROR &&
4374 [ + + ]: 78 : func->on_error->btype != JSON_BEHAVIOR_NULL &&
4375 [ + - ]: 75 : func->on_error->btype != JSON_BEHAVIOR_EMPTY &&
4376 [ + + ]: 75 : func->on_error->btype != JSON_BEHAVIOR_EMPTY_ARRAY &&
4377 [ + + ]: 72 : func->on_error->btype != JSON_BEHAVIOR_EMPTY_OBJECT &&
4378 [ + + ]: 45 : func->on_error->btype != JSON_BEHAVIOR_DEFAULT)
4379 : : {
4380 [ + + ]: 6 : if (func->column_name == NULL)
4381 [ + - ]: 3 : 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 [ + - ]: 3 : 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 [ + + ]: 1549 : if (func->op == JSON_EXISTS_OP &&
4405 [ + + ]: 153 : func->on_error != NULL &&
4406 [ + + ]: 45 : func->on_error->btype != JSON_BEHAVIOR_ERROR &&
4407 [ + + ]: 24 : func->on_error->btype != JSON_BEHAVIOR_TRUE &&
4408 [ + + ]: 18 : func->on_error->btype != JSON_BEHAVIOR_FALSE &&
4409 [ + + ]: 12 : func->on_error->btype != JSON_BEHAVIOR_UNKNOWN)
4410 : : {
4411 [ + + ]: 6 : if (func->column_name == NULL)
4412 [ + - ]: 3 : 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 [ + - ]: 3 : 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 [ + + ]: 1543 : if (func->op == JSON_VALUE_OP)
4431 : : {
4432 [ + + ]: 542 : if (func->on_empty != NULL &&
4433 [ + + ]: 111 : func->on_empty->btype != JSON_BEHAVIOR_ERROR &&
4434 [ + + ]: 96 : func->on_empty->btype != JSON_BEHAVIOR_NULL &&
4435 [ + + ]: 93 : func->on_empty->btype != JSON_BEHAVIOR_DEFAULT)
4436 : : {
4437 [ - + ]: 3 : if (func->column_name == NULL)
486 amitlan@postgresql.o 4438 [ # # ]:UBC 0 : 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
486 amitlan@postgresql.o 4448 [ + - ]:CBC 3 : 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 [ + + ]: 539 : if (func->on_error != NULL &&
4459 [ + + ]: 162 : func->on_error->btype != JSON_BEHAVIOR_ERROR &&
4460 [ + - ]: 72 : func->on_error->btype != JSON_BEHAVIOR_NULL &&
4461 [ + + ]: 72 : func->on_error->btype != JSON_BEHAVIOR_DEFAULT)
4462 : : {
4463 [ + - ]: 3 : if (func->column_name == NULL)
4464 [ + - ]: 3 : 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
486 amitlan@postgresql.o 4474 [ # # ]:UBC 0 : 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 : :
585 amitlan@postgresql.o 4486 :CBC 1537 : jsexpr = makeNode(JsonExpr);
4487 : 1537 : jsexpr->location = func->location;
4488 : 1537 : jsexpr->op = func->op;
557 4489 : 1537 : 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 : : */
585 4495 : 1537 : jsexpr->formatted_expr = transformJsonValueExpr(pstate, func_name,
4496 : : func->context_item,
4497 : : default_format,
4498 : : JSONBOID,
4499 : : false);
4500 : 1537 : jsexpr->format = func->context_item->format;
4501 : :
4502 : 1537 : path_spec = transformExprRecurse(pstate, func->pathspec);
4503 : 1537 : path_spec = coerce_to_target_type(pstate, path_spec, exprType(path_spec),
4504 : : JSONPATHOID, -1,
4505 : : COERCION_EXPLICIT, COERCE_IMPLICIT_CAST,
4506 : : exprLocation(path_spec));
4507 [ - + ]: 1537 : if (path_spec == NULL)
585 amitlan@postgresql.o 4508 [ # # ]:UBC 0 : 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))));
585 amitlan@postgresql.o 4513 :CBC 1537 : jsexpr->path_spec = path_spec;
4514 : :
4515 : : /* Transform and coerce the PASSING arguments to jsonb. */
4516 : 1537 : transformJsonPassingArgs(pstate, func_name,
4517 : : JS_FORMAT_JSONB,
4518 : : func->passing,
4519 : : &jsexpr->passing_values,
4520 : : &jsexpr->passing_names);
4521 : :
4522 : : /* Transform the JsonOutput into JsonReturning. */
4523 : 1537 : jsexpr->returning = transformJsonOutput(pstate, func->output, false);
4524 : :
4525 [ + + + + : 1531 : switch (func->op)
- ]
4526 : : {
4527 : 147 : case JSON_EXISTS_OP:
4528 : : /* JSON_EXISTS returns boolean by default. */
4529 [ + + ]: 147 : if (!OidIsValid(jsexpr->returning->typid))
4530 : : {
4531 : 81 : jsexpr->returning->typid = BOOLOID;
4532 : 81 : jsexpr->returning->typmod = -1;
18 4533 : 81 : jsexpr->collation = InvalidOid;
4534 : : }
4535 : :
4536 : : /* JSON_TABLE() COLUMNS can specify a non-boolean type. */
571 4537 [ + + ]: 147 : if (jsexpr->returning->typid != BOOLOID)
486 4538 : 60 : jsexpr->use_json_coercion = true;
4539 : :
18 4540 : 147 : jsexpr->on_error = transformJsonBehavior(pstate,
4541 : : jsexpr,
4542 : : func->on_error,
4543 : : JSON_BEHAVIOR_FALSE,
4544 : : jsexpr->returning);
585 4545 : 147 : break;
4546 : :
4547 : 615 : case JSON_QUERY_OP:
4548 : : /* JSON_QUERY returns jsonb by default. */
4549 [ + + ]: 615 : if (!OidIsValid(jsexpr->returning->typid))
4550 : : {
4551 : 246 : JsonReturning *ret = jsexpr->returning;
4552 : :
4553 : 246 : ret->typid = JSONBOID;
4554 : 246 : ret->typmod = -1;
4555 : : }
4556 : :
18 4557 : 615 : 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 : : */
585 4563 : 615 : jsexpr->omit_quotes = (func->quotes == JS_QUOTES_OMIT);
4564 : 615 : 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 : : */
486 4571 [ + + + + ]: 615 : if (jsexpr->returning->typid != JSONBOID || jsexpr->omit_quotes)
4572 : 345 : jsexpr->use_json_coercion = true;
4573 : :
4574 : : /* Assume NULL ON EMPTY when ON EMPTY is not specified. */
18 4575 : 615 : jsexpr->on_empty = transformJsonBehavior(pstate,
4576 : : jsexpr,
4577 : : func->on_empty,
4578 : : JSON_BEHAVIOR_NULL,
4579 : : jsexpr->returning);
4580 : : /* Assume NULL ON ERROR when ON ERROR is not specified. */
4581 : 615 : jsexpr->on_error = transformJsonBehavior(pstate,
4582 : : jsexpr,
4583 : : func->on_error,
4584 : : JSON_BEHAVIOR_NULL,
4585 : : jsexpr->returning);
585 4586 : 585 : break;
4587 : :
4588 : 533 : case JSON_VALUE_OP:
4589 : : /* JSON_VALUE returns text by default. */
4590 [ + + ]: 533 : if (!OidIsValid(jsexpr->returning->typid))
4591 : : {
4592 : 87 : jsexpr->returning->typid = TEXTOID;
4593 : 87 : jsexpr->returning->typmod = -1;
4594 : : }
18 4595 : 533 : 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 : : */
585 4601 : 533 : jsexpr->returning->format->format_type = JS_FORMAT_DEFAULT;
4602 : 533 : jsexpr->returning->format->encoding = JS_ENC_DEFAULT;
4603 : :
4604 : : /* Always omit quotes from scalar strings. */
4605 : 533 : 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 : : */
486 4611 [ + + ]: 533 : if (jsexpr->returning->typid != TEXTOID)
4612 : : {
4613 [ + + + + ]: 500 : if (get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN &&
4614 : 96 : DomainHasConstraints(jsexpr->returning->typid))
4615 : 66 : jsexpr->use_json_coercion = true;
4616 : : else
4617 : 338 : jsexpr->use_io_coercion = true;
4618 : : }
4619 : :
4620 : : /* Assume NULL ON EMPTY when ON EMPTY is not specified. */
18 4621 : 533 : jsexpr->on_empty = transformJsonBehavior(pstate,
4622 : : jsexpr,
4623 : : func->on_empty,
4624 : : JSON_BEHAVIOR_NULL,
4625 : : jsexpr->returning);
4626 : : /* Assume NULL ON ERROR when ON ERROR is not specified. */
4627 : 521 : jsexpr->on_error = transformJsonBehavior(pstate,
4628 : : jsexpr,
4629 : : func->on_error,
4630 : : JSON_BEHAVIOR_NULL,
4631 : : jsexpr->returning);
585 4632 : 518 : break;
4633 : :
571 4634 : 236 : case JSON_TABLE_OP:
4635 [ + - ]: 236 : if (!OidIsValid(jsexpr->returning->typid))
4636 : : {
4637 : 236 : jsexpr->returning->typid = exprType(jsexpr->formatted_expr);
4638 : 236 : jsexpr->returning->typmod = -1;
4639 : : }
18 4640 : 236 : 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 : 236 : jsexpr->on_error = transformJsonBehavior(pstate,
4649 : : jsexpr,
4650 : : func->on_error,
4651 : : JSON_BEHAVIOR_EMPTY_ARRAY,
4652 : : jsexpr->returning);
571 4653 : 236 : break;
4654 : :
585 amitlan@postgresql.o 4655 :UBC 0 : default:
4656 [ # # ]: 0 : elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4657 : : break;
4658 : : }
4659 : :
585 amitlan@postgresql.o 4660 :CBC 1486 : return (Node *) jsexpr;
4661 : : }
4662 : :
4663 : : /*
4664 : : * Transform a SQL/JSON PASSING clause.
4665 : : */
4666 : : static void
4667 : 1537 : transformJsonPassingArgs(ParseState *pstate, const char *constructName,
4668 : : JsonFormatType format, List *args,
4669 : : List **passing_values, List **passing_names)
4670 : : {
4671 : : ListCell *lc;
4672 : :
4673 : 1537 : *passing_values = NIL;
4674 : 1537 : *passing_names = NIL;
4675 : :
4676 [ + + + + : 2137 : foreach(lc, args)
+ + ]
4677 : : {
4678 : 600 : JsonArgument *arg = castNode(JsonArgument, lfirst(lc));
4679 : 600 : Node *expr = transformJsonValueExpr(pstate, constructName,
4680 : : arg->val, format,
4681 : : InvalidOid, true);
4682 : :
4683 : 600 : *passing_values = lappend(*passing_values, expr);
4684 : 600 : *passing_names = lappend(*passing_names, makeString(arg->name));
4685 : : }
4686 : 1537 : }
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 : : */
4692 : : static bool
557 4693 : 291 : ValidJsonBehaviorDefaultExpr(Node *expr, void *context)
4694 : : {
4695 [ - + ]: 291 : if (expr == NULL)
557 amitlan@postgresql.o 4696 :UBC 0 : return false;
4697 : :
557 amitlan@postgresql.o 4698 [ + + + ]:CBC 291 : switch (nodeTag(expr))
4699 : : {
4700 : : /* Acceptable expression nodes */
4701 : 186 : case T_Const:
4702 : : case T_FuncExpr:
4703 : : case T_OpExpr:
4704 : 186 : return true;
4705 : :
4706 : : /* Acceptable iff arg of the following nodes is one of the above */
4707 : 78 : case T_CoerceViaIO:
4708 : : case T_CoerceToDomain:
4709 : : case T_ArrayCoerceExpr:
4710 : : case T_ConvertRowtypeExpr:
4711 : : case T_RelabelType:
4712 : : case T_CollateExpr:
4713 : 78 : return expression_tree_walker(expr, ValidJsonBehaviorDefaultExpr,
4714 : : context);
4715 : 27 : default:
4716 : 27 : break;
4717 : : }
4718 : :
4719 : 27 : return false;
4720 : : }
4721 : :
4722 : : /*
4723 : : * Transform a JSON BEHAVIOR clause.
4724 : : */
4725 : : static JsonBehavior *
18 4726 : 2667 : transformJsonBehavior(ParseState *pstate, JsonExpr *jsexpr,
4727 : : JsonBehavior *behavior,
4728 : : JsonBehaviorType default_behavior,
4729 : : JsonReturning *returning)
4730 : : {
585 4731 : 2667 : JsonBehaviorType btype = default_behavior;
4732 : 2667 : Node *expr = NULL;
4733 : 2667 : bool coerce_at_runtime = false;
4734 : 2667 : int location = -1;
4735 : :
4736 [ + + ]: 2667 : if (behavior)
4737 : : {
4738 : 531 : btype = behavior->btype;
4739 : 531 : location = behavior->location;
4740 [ + + ]: 531 : if (btype == JSON_BEHAVIOR_DEFAULT)
4741 : : {
18 4742 : 210 : Oid targetcoll = jsexpr->collation;
4743 : : Oid exprcoll;
4744 : :
585 4745 : 210 : expr = transformExprRecurse(pstate, behavior->expr);
4746 : :
557 4747 [ + + ]: 210 : if (!ValidJsonBehaviorDefaultExpr(expr, NULL))
585 4748 [ + - ]: 24 : 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 [ + + ]: 186 : if (contain_var_clause(expr))
4753 [ + - ]: 3 : ereport(ERROR,
4754 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
4755 : : errmsg("DEFAULT expression must not contain column references"),
4756 : : parser_errposition(pstate, exprLocation(expr))));
4757 [ + + ]: 183 : if (expression_returns_set(expr))
4758 [ + - ]: 3 : 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 : : */
18 4768 : 180 : exprcoll = exprCollation(expr);
4769 [ + + ]: 180 : if (!OidIsValid(exprcoll))
4770 : 168 : exprcoll = get_typcollation(exprType(expr));
4771 [ + + + + : 180 : if (OidIsValid(targetcoll) && OidIsValid(exprcoll) &&
+ + ]
4772 : : targetcoll != exprcoll)
4773 [ + - ]: 12 : ereport(ERROR,
4774 : : errcode(ERRCODE_COLLATION_MISMATCH),
4775 : : errmsg("the 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 : :
585 4783 [ + + + + ]: 2625 : if (expr == NULL && btype != JSON_BEHAVIOR_ERROR)
4784 : 2214 : 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 : : */
454 4797 [ + + + + ]: 2625 : if (expr && exprType(expr) != returning->typid)
4798 : : {
585 4799 [ + + + + ]: 1635 : bool isnull = (IsA(expr, Const) && ((Const *) expr)->constisnull);
4800 : :
4801 [ + + + + ]: 1791 : if (isnull ||
454 4802 [ + + ]: 291 : exprType(expr) == JSONBOID ||
4803 [ + + ]: 174 : (exprType(expr) == BOOLOID &&
4804 : 39 : getBaseType(returning->typid) != INT4OID))
4805 : : {
585 4806 : 1521 : 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 : : */
454 4814 [ + + ]: 1542 : if (exprType(expr) == BOOLOID)
4815 : : {
4816 [ - + ]: 21 : char *val = btype == JSON_BEHAVIOR_TRUE ? "true" : "false";
4817 : :
4818 : 21 : expr = (Node *) makeConst(JSONBOID, -1, InvalidOid, -1,
4819 : : DirectFunctionCall1(jsonb_in,
4820 : : CStringGetDatum(val)),
4821 : : false, false);
4822 : : }
4823 : : }
4824 : : else
4825 : : {
4826 : : Node *coerced_expr;
4827 : 114 : 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 =
585 4836 [ + + ]: 162 : coerce_to_target_type(pstate, expr, exprType(expr),
4837 : : returning->typid, returning->typmod,
454 4838 [ + + ]: 48 : (typcategory == TYPCATEGORY_STRING ||
4839 : : typcategory == TYPCATEGORY_BITSTRING) ?
4840 : : COERCION_ASSIGNMENT :
4841 : : COERCION_EXPLICIT,
4842 : : COERCE_EXPLICIT_CAST,
4843 : : exprLocation((Node *) behavior));
4844 : :
4845 [ + + ]: 114 : if (coerced_expr == NULL)
4846 : : {
4847 : : /*
4848 : : * Provide a HINT if the expression comes from a DEFAULT
4849 : : * clause.
4850 : : */
4851 [ + - ]: 3 : if (btype == JSON_BEHAVIOR_DEFAULT)
4852 [ + - ]: 3 : 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
454 amitlan@postgresql.o 4861 [ # # ]:UBC 0 : 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 : :
585 amitlan@postgresql.o 4869 :CBC 111 : expr = coerced_expr;
4870 : : }
4871 : : }
4872 : :
4873 [ + + ]: 2622 : if (behavior)
4874 : 486 : behavior->expr = expr;
4875 : : else
4876 : 2136 : behavior = makeJsonBehavior(btype, expr, location);
4877 : :
4878 : 2622 : behavior->coerce = coerce_at_runtime;
4879 : :
4880 : 2622 : return behavior;
4881 : : }
4882 : :
4883 : : /*
4884 : : * Returns a Const node holding the value for the given non-ERROR
4885 : : * JsonBehaviorType.
4886 : : */
4887 : : static Node *
4888 : 2214 : GetJsonBehaviorConst(JsonBehaviorType btype, int location)
4889 : : {
4890 : 2214 : Datum val = (Datum) 0;
4891 : 2214 : Oid typid = JSONBOID;
4892 : 2214 : int len = -1;
4893 : 2214 : bool isbyval = false;
4894 : 2214 : bool isnull = false;
4895 : : Const *con;
4896 : :
4897 [ + + + + : 2214 : switch (btype)
+ - - ]
4898 : : {
4899 : 239 : case JSON_BEHAVIOR_EMPTY_ARRAY:
4900 : 239 : val = DirectFunctionCall1(jsonb_in, CStringGetDatum("[]"));
4901 : 239 : break;
4902 : :
4903 : 27 : case JSON_BEHAVIOR_EMPTY_OBJECT:
4904 : 27 : val = DirectFunctionCall1(jsonb_in, CStringGetDatum("{}"));
4905 : 27 : break;
4906 : :
4907 : 6 : case JSON_BEHAVIOR_TRUE:
4908 : 6 : val = BoolGetDatum(true);
4909 : 6 : typid = BOOLOID;
4910 : 6 : len = sizeof(bool);
4911 : 6 : isbyval = true;
4912 : 6 : break;
4913 : :
4914 : 114 : case JSON_BEHAVIOR_FALSE:
4915 : 114 : val = BoolGetDatum(false);
4916 : 114 : typid = BOOLOID;
4917 : 114 : len = sizeof(bool);
4918 : 114 : isbyval = true;
4919 : 114 : break;
4920 : :
4921 : 1828 : case JSON_BEHAVIOR_NULL:
4922 : : case JSON_BEHAVIOR_UNKNOWN:
4923 : : case JSON_BEHAVIOR_EMPTY:
4924 : 1828 : val = (Datum) 0;
4925 : 1828 : isnull = true;
4926 : 1828 : typid = INT4OID;
4927 : 1828 : len = sizeof(int32);
4928 : 1828 : isbyval = true;
4929 : 1828 : break;
4930 : :
4931 : : /* These two behavior types are handled by the caller. */
585 amitlan@postgresql.o 4932 :UBC 0 : case JSON_BEHAVIOR_DEFAULT:
4933 : : case JSON_BEHAVIOR_ERROR:
4934 : 0 : Assert(false);
4935 : : break;
4936 : :
4937 : 0 : default:
4938 [ # # ]: 0 : elog(ERROR, "unrecognized SQL/JSON behavior %d", btype);
4939 : : break;
4940 : : }
4941 : :
585 amitlan@postgresql.o 4942 :CBC 2214 : con = makeConst(typid, -1, InvalidOid, len, val, isnull, isbyval);
4943 : 2214 : con->location = location;
4944 : :
4945 : 2214 : return (Node *) con;
4946 : : }
|