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 *
4876 tgl@sss.pgh.pa.us 119 :CBC 925133 : 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 [ - + ]: 925133 : Assert(exprKind != EXPR_KIND_NONE);
126 : 925133 : sv_expr_kind = pstate->p_expr_kind;
127 : 925133 : pstate->p_expr_kind = exprKind;
128 : :
129 : 925133 : result = transformExprRecurse(pstate, expr);
130 : :
131 : 921965 : pstate->p_expr_kind = sv_expr_kind;
132 : :
133 : 921965 : return result;
134 : : }
135 : :
136 : : static Node *
137 : 2455242 : transformExprRecurse(ParseState *pstate, Node *expr)
138 : : {
139 : : Node *result;
140 : :
10248 bruce@momjian.us 141 [ + + ]: 2455242 : if (expr == NULL)
142 : 17206 : return NULL;
143 : :
144 : : /* Guard against stack overflow due to overly complex expressions */
7937 tgl@sss.pgh.pa.us 145 : 2438036 : check_stack_depth();
146 : :
10248 bruce@momjian.us 147 [ + + + + : 2438036 : switch (nodeTag(expr))
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ - + + +
+ + + + +
+ + + - ]
148 : : {
8671 tgl@sss.pgh.pa.us 149 : 916184 : case T_ColumnRef:
7636 neilc@samurai.com 150 : 916184 : result = transformColumnRef(pstate, (ColumnRef *) expr);
151 : 915840 : break;
152 : :
153 : 20957 : case T_ParamRef:
154 : 20957 : result = transformParamRef(pstate, (ParamRef *) expr);
155 : 20951 : break;
156 : :
10248 bruce@momjian.us 157 : 622139 : case T_A_Const:
1559 peter@eisentraut.org 158 : 622139 : result = (Node *) make_const(pstate, (A_Const *) expr);
159 : 622127 : break;
160 : :
7860 tgl@sss.pgh.pa.us 161 : 11898 : case T_A_Indirection:
3108 162 : 11898 : result = transformIndirection(pstate, (A_Indirection *) expr);
163 : 11853 : break;
164 : :
6480 165 : 3225 : case T_A_ArrayExpr:
166 : 3225 : result = transformArrayExpr(pstate, (A_ArrayExpr *) expr,
167 : : InvalidOid, InvalidOid, -1);
168 : 3222 : break;
169 : :
9465 170 : 162315 : case T_TypeCast:
3950 171 : 162315 : result = transformTypeCast(pstate, (TypeCast *) expr);
172 : 160390 : break;
173 : :
5425 peter_e@gmx.net 174 : 4755 : case T_CollateClause:
175 : 4755 : result = transformCollateClause(pstate, (CollateClause *) expr);
176 : 4746 : break;
177 : :
10248 bruce@momjian.us 178 : 325013 : case T_A_Expr:
179 : : {
180 : 325013 : A_Expr *a = (A_Expr *) expr;
181 : :
8345 tgl@sss.pgh.pa.us 182 [ + + + + : 325013 : switch (a->kind)
+ + + +
- ]
183 : : {
184 : 302898 : case AEXPR_OP:
7636 neilc@samurai.com 185 : 302898 : result = transformAExprOp(pstate, a);
10248 bruce@momjian.us 186 : 302657 : break;
8206 tgl@sss.pgh.pa.us 187 : 8479 : case AEXPR_OP_ANY:
7636 neilc@samurai.com 188 : 8479 : result = transformAExprOpAny(pstate, a);
8206 tgl@sss.pgh.pa.us 189 : 8473 : break;
190 : 150 : case AEXPR_OP_ALL:
7636 neilc@samurai.com 191 : 150 : result = transformAExprOpAll(pstate, a);
8206 tgl@sss.pgh.pa.us 192 : 150 : break;
8345 193 : 578 : case AEXPR_DISTINCT:
194 : : case AEXPR_NOT_DISTINCT:
7636 neilc@samurai.com 195 : 578 : result = transformAExprDistinct(pstate, a);
8535 lockhart@fourpalms.o 196 : 578 : break;
8339 tgl@sss.pgh.pa.us 197 : 241 : case AEXPR_NULLIF:
7636 neilc@samurai.com 198 : 241 : result = transformAExprNullIf(pstate, a);
8339 tgl@sss.pgh.pa.us 199 : 241 : break;
7323 200 : 10994 : case AEXPR_IN:
201 : 10994 : result = transformAExprIn(pstate, a);
202 : 10988 : break;
3949 203 : 1414 : case AEXPR_LIKE:
204 : : case AEXPR_ILIKE:
205 : : case AEXPR_SIMILAR:
206 : : /* we can transform these just like AEXPR_OP */
207 : 1414 : result = transformAExprOp(pstate, a);
208 : 1411 : break;
3950 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;
7636 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 : : }
10248 bruce@momjian.us 220 :CBC 324757 : break;
221 : : }
222 : :
4201 tgl@sss.pgh.pa.us 223 : 78306 : case T_BoolExpr:
224 : 78306 : result = transformBoolExpr(pstate, (BoolExpr *) expr);
225 : 78296 : break;
226 : :
10248 bruce@momjian.us 227 : 190226 : case T_FuncCall:
7636 neilc@samurai.com 228 : 190226 : result = transformFuncCall(pstate, (FuncCall *) expr);
229 : 189662 : break;
230 : :
4199 tgl@sss.pgh.pa.us 231 : 187 : case T_MultiAssignRef:
232 : 187 : result = transformMultiAssignRef(pstate, (MultiAssignRef *) expr);
233 : 184 : break;
234 : :
3867 andres@anarazel.de 235 : 181 : case T_GroupingFunc:
236 : 181 : result = transformGroupingFunc(pstate, (GroupingFunc *) expr);
237 : 181 : break;
238 : :
639 dean.a.rasheed@gmail 239 : 102 : case T_MergeSupportFunc:
240 : 102 : result = transformMergeSupportFunc(pstate,
241 : : (MergeSupportFunc *) expr);
242 : 96 : break;
243 : :
5913 tgl@sss.pgh.pa.us 244 : 23253 : case T_NamedArgExpr:
245 : : {
246 : 23253 : NamedArgExpr *na = (NamedArgExpr *) expr;
247 : :
4876 248 : 23253 : na->arg = (Expr *) transformExprRecurse(pstate, (Node *) na->arg);
5913 249 : 23253 : result = expr;
250 : 23253 : break;
251 : : }
252 : :
10193 bruce@momjian.us 253 : 24509 : case T_SubLink:
7636 neilc@samurai.com 254 : 24509 : result = transformSubLink(pstate, (SubLink *) expr);
255 : 24458 : break;
256 : :
9874 lockhart@fourpalms.o 257 : 20177 : case T_CaseExpr:
7636 neilc@samurai.com 258 : 20177 : result = transformCaseExpr(pstate, (CaseExpr *) expr);
259 : 20174 : break;
260 : :
7890 tgl@sss.pgh.pa.us 261 : 2952 : case T_RowExpr:
3311 262 : 2952 : result = transformRowExpr(pstate, (RowExpr *) expr, false);
7636 neilc@samurai.com 263 : 2952 : break;
264 : :
8339 tgl@sss.pgh.pa.us 265 : 1633 : case T_CoalesceExpr:
7636 neilc@samurai.com 266 : 1633 : result = transformCoalesceExpr(pstate, (CoalesceExpr *) expr);
267 : 1630 : break;
268 : :
7478 tgl@sss.pgh.pa.us 269 : 134 : case T_MinMaxExpr:
270 : 134 : result = transformMinMaxExpr(pstate, (MinMaxExpr *) expr);
271 : 134 : break;
272 : :
944 michael@paquier.xyz 273 : 1368 : case T_SQLValueFunction:
274 : 1368 : result = transformSQLValueFunction(pstate,
275 : : (SQLValueFunction *) expr);
276 : 1368 : break;
277 : :
6932 tgl@sss.pgh.pa.us 278 : 298 : case T_XmlExpr:
279 : 298 : result = transformXmlExpr(pstate, (XmlExpr *) expr);
280 : 283 : break;
281 : :
6891 peter_e@gmx.net 282 : 109 : case T_XmlSerialize:
283 : 109 : result = transformXmlSerialize(pstate, (XmlSerialize *) expr);
284 : 109 : break;
285 : :
8946 tgl@sss.pgh.pa.us 286 : 9275 : case T_NullTest:
287 : : {
288 : 9275 : NullTest *n = (NullTest *) expr;
289 : :
4876 290 : 9275 : n->arg = (Expr *) transformExprRecurse(pstate, (Node *) n->arg);
291 : : /* the argument can be any type, so don't coerce it */
5828 292 : 9275 : n->argisrow = type_is_rowtype(exprType((Node *) n->arg));
8946 293 : 9275 : result = expr;
294 : 9275 : break;
295 : : }
296 : :
297 : 474 : case T_BooleanTest:
7636 neilc@samurai.com 298 : 474 : result = transformBooleanTest(pstate, (BooleanTest *) expr);
299 : 474 : break;
300 : :
6763 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 : : */
3311 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 : : */
7944 tgl@sss.pgh.pa.us 327 :CBC 15781 : case T_CaseTestExpr:
328 : : case T_Var:
329 : : {
14 peter@eisentraut.org 330 :GNC 15781 : result = expr;
10127 bruce@momjian.us 331 :CBC 15781 : break;
332 : : }
333 : :
993 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 : :
991 354 : 167 : case T_JsonIsPredicate:
355 : 167 : result = transformJsonIsPredicate(pstate, (JsonIsPredicate *) expr);
356 : 164 : break;
357 : :
880 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 : :
635 370 : 1573 : case T_JsonFuncExpr:
371 : 1573 : result = transformJsonFuncExpr(pstate, (JsonFuncExpr *) expr);
372 : 1486 : break;
373 : :
10248 bruce@momjian.us 374 :UBC 0 : default:
375 : : /* should not reach here */
8186 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 : :
10248 bruce@momjian.us 381 :CBC 2434637 : 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
2968 peter_e@gmx.net 391 : 19 : unknown_attribute(ParseState *pstate, Node *relref, const char *attname,
392 : : int location)
393 : : {
394 : : RangeTblEntry *rte;
395 : :
5890 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 */
5890 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 */
5890 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
5890 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 *
3108 tgl@sss.pgh.pa.us 437 :CBC 11898 : transformIndirection(ParseState *pstate, A_Indirection *ind)
438 : : {
439 : 11898 : Node *last_srf = pstate->p_last_srf;
440 : 11898 : Node *result = transformExprRecurse(pstate, ind->arg);
7860 441 : 11898 : List *subscripts = NIL;
3108 442 : 11898 : 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 [ + + + + : 23511 : foreach(i, ind->indirection)
+ + ]
451 : : {
7779 bruce@momjian.us 452 : 11632 : Node *n = lfirst(i);
453 : :
7860 tgl@sss.pgh.pa.us 454 [ + + ]: 11632 : if (IsA(n, A_Indices))
455 : 5907 : subscripts = lappend(subscripts, n);
6317 456 [ - + ]: 5725 : else if (IsA(n, A_Star))
457 : : {
6317 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 : :
7860 tgl@sss.pgh.pa.us 467 [ - + ]:CBC 5725 : Assert(IsA(n, String));
468 : :
469 : : /* process subscripts before this field selection */
470 [ + + ]: 5725 : if (subscripts)
2510 alvherre@alvh.no-ip. 471 : 68 : result = (Node *) transformContainerSubscripts(pstate,
472 : : result,
473 : : exprType(result),
474 : : exprTypmod(result),
475 : : subscripts,
476 : : false);
7860 tgl@sss.pgh.pa.us 477 : 5725 : subscripts = NIL;
478 : :
5890 479 : 5725 : newresult = ParseFuncOrColumn(pstate,
480 : 5725 : list_make1(n),
481 : 5725 : list_make1(result),
482 : : last_srf,
483 : : NULL,
484 : : false,
485 : : location);
486 [ + + ]: 5725 : if (newresult == NULL)
487 : 19 : unknown_attribute(pstate, result, strVal(n), location);
488 : 5706 : result = newresult;
489 : : }
490 : : }
491 : : /* process trailing subscripts, if any */
7860 492 [ + + ]: 11879 : if (subscripts)
2510 alvherre@alvh.no-ip. 493 : 5679 : result = (Node *) transformContainerSubscripts(pstate,
494 : : result,
495 : : exprType(result),
496 : : exprTypmod(result),
497 : : subscripts,
498 : : false);
499 : :
7860 tgl@sss.pgh.pa.us 500 : 11853 : 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 *
8671 509 : 916184 : transformColumnRef(ParseState *pstate, ColumnRef *cref)
510 : : {
5890 511 : 916184 : Node *node = NULL;
512 : 916184 : char *nspname = NULL;
513 : 916184 : char *relname = NULL;
514 : 916184 : 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 : 916184 : } 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 : : */
2456 michael@paquier.xyz 531 : 916184 : err = NULL;
532 [ - + + + : 916184 : switch (pstate->p_expr_kind)
- ]
533 : : {
2456 michael@paquier.xyz 534 :UBC 0 : case EXPR_KIND_NONE:
535 : 0 : Assert(false); /* can't happen */
536 : : break;
2456 michael@paquier.xyz 537 :CBC 916142 : 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 : 916142 : 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 [ + + ]: 916184 : 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 : : */
5890 tgl@sss.pgh.pa.us 607 [ + + ]: 916142 : if (pstate->p_pre_columnref_hook != NULL)
608 : : {
3022 peter_e@gmx.net 609 : 20418 : node = pstate->p_pre_columnref_hook(pstate, cref);
5890 tgl@sss.pgh.pa.us 610 [ + + ]: 20418 : if (node != NULL)
611 : 408 : 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 [ + + + - : 915734 : switch (list_length(cref->fields))
- ]
638 : : {
8671 639 : 379426 : case 1:
640 : : {
6317 641 : 379426 : Node *field1 = (Node *) linitial(cref->fields);
642 : :
5890 643 : 379426 : colname = strVal(field1);
644 : :
645 : : /* Try to identify as an unqualified column */
646 : 379426 : node = colNameToVar(pstate, colname, false, cref->location);
647 : :
8504 bruce@momjian.us 648 [ + + ]: 379393 : 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 : : */
2182 tgl@sss.pgh.pa.us 661 : 19536 : nsitem = refnameNamespaceItem(pstate, NULL, colname,
662 : : cref->location,
663 : : &levels_up);
664 [ + + ]: 19536 : if (nsitem)
665 : 3776 : node = transformWholeRowRef(pstate, nsitem, levels_up,
666 : : cref->location);
667 : : }
8504 bruce@momjian.us 668 : 379393 : break;
669 : : }
670 : 536266 : case 2:
671 : : {
6317 tgl@sss.pgh.pa.us 672 : 536266 : Node *field1 = (Node *) linitial(cref->fields);
673 : 536266 : Node *field2 = (Node *) lsecond(cref->fields);
674 : :
5890 675 : 536266 : relname = strVal(field1);
676 : :
677 : : /* Locate the referenced nsitem */
2182 678 : 536266 : nsitem = refnameNamespaceItem(pstate, nspname, relname,
679 : : cref->location,
680 : : &levels_up);
681 [ + + ]: 536254 : if (nsitem == NULL)
682 : : {
5890 683 : 2740 : crerr = CRERR_NO_RTE;
684 : 2740 : break;
685 : : }
686 : :
687 : : /* Whole-row reference? */
6317 688 [ + + ]: 533514 : if (IsA(field2, A_Star))
689 : : {
2182 690 : 729 : node = transformWholeRowRef(pstate, nsitem, levels_up,
691 : : cref->location);
8504 bruce@momjian.us 692 : 729 : break;
693 : : }
694 : :
5890 tgl@sss.pgh.pa.us 695 : 532785 : colname = strVal(field2);
696 : :
697 : : /* Try to identify as a column of the nsitem */
2182 698 : 532785 : node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
699 : : cref->location);
8504 bruce@momjian.us 700 [ + + ]: 532782 : if (node == NULL)
701 : : {
702 : : /* Try it as a function call on the whole row */
2182 tgl@sss.pgh.pa.us 703 : 84 : node = transformWholeRowRef(pstate, nsitem, levels_up,
704 : : cref->location);
8504 bruce@momjian.us 705 : 84 : node = ParseFuncOrColumn(pstate,
5890 tgl@sss.pgh.pa.us 706 : 84 : list_make1(makeString(colname)),
7870 neilc@samurai.com 707 : 84 : list_make1(node),
708 : : pstate->p_last_srf,
709 : : NULL,
710 : : false,
711 : : cref->location);
712 : : }
8671 tgl@sss.pgh.pa.us 713 : 532782 : break;
714 : : }
715 : 42 : case 3:
716 : : {
6317 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 : :
5890 721 : 42 : nspname = strVal(field1);
722 : 42 : relname = strVal(field2);
723 : :
724 : : /* Locate the referenced nsitem */
2182 725 : 42 : nsitem = refnameNamespaceItem(pstate, nspname, relname,
726 : : cref->location,
727 : : &levels_up);
728 [ + + ]: 42 : if (nsitem == NULL)
729 : : {
5890 730 : 33 : crerr = CRERR_NO_RTE;
731 : 33 : break;
732 : : }
733 : :
734 : : /* Whole-row reference? */
6317 735 [ + + ]: 9 : if (IsA(field3, A_Star))
736 : : {
2182 737 : 3 : node = transformWholeRowRef(pstate, nsitem, levels_up,
738 : : cref->location);
8504 bruce@momjian.us 739 : 3 : break;
740 : : }
741 : :
5890 tgl@sss.pgh.pa.us 742 : 6 : colname = strVal(field3);
743 : :
744 : : /* Try to identify as a column of the nsitem */
2182 745 : 6 : node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
746 : : cref->location);
8504 bruce@momjian.us 747 [ - + ]: 6 : if (node == NULL)
748 : : {
749 : : /* Try it as a function call on the whole row */
2182 tgl@sss.pgh.pa.us 750 :UBC 0 : node = transformWholeRowRef(pstate, nsitem, levels_up,
751 : : cref->location);
8504 bruce@momjian.us 752 : 0 : node = ParseFuncOrColumn(pstate,
5890 tgl@sss.pgh.pa.us 753 : 0 : list_make1(makeString(colname)),
7870 neilc@samurai.com 754 : 0 : list_make1(node),
755 : : pstate->p_last_srf,
756 : : NULL,
757 : : false,
758 : : cref->location);
759 : : }
8504 bruce@momjian.us 760 :CBC 6 : break;
761 : : }
8671 tgl@sss.pgh.pa.us 762 :UBC 0 : case 4:
763 : : {
6317 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 : :
5890 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 */
2182 784 : 0 : nsitem = refnameNamespaceItem(pstate, nspname, relname,
785 : : cref->location,
786 : : &levels_up);
787 [ # # ]: 0 : if (nsitem == NULL)
788 : : {
5890 789 : 0 : crerr = CRERR_NO_RTE;
790 : 0 : break;
791 : : }
792 : :
793 : : /* Whole-row reference? */
6317 794 [ # # ]: 0 : if (IsA(field4, A_Star))
795 : : {
2182 796 : 0 : node = transformWholeRowRef(pstate, nsitem, levels_up,
797 : : cref->location);
8504 bruce@momjian.us 798 : 0 : break;
799 : : }
800 : :
5890 tgl@sss.pgh.pa.us 801 : 0 : colname = strVal(field4);
802 : :
803 : : /* Try to identify as a column of the nsitem */
2182 804 : 0 : node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
805 : : cref->location);
8504 bruce@momjian.us 806 [ # # ]: 0 : if (node == NULL)
807 : : {
808 : : /* Try it as a function call on the whole row */
2182 tgl@sss.pgh.pa.us 809 : 0 : node = transformWholeRowRef(pstate, nsitem, levels_up,
810 : : cref->location);
8504 bruce@momjian.us 811 : 0 : node = ParseFuncOrColumn(pstate,
5890 tgl@sss.pgh.pa.us 812 : 0 : list_make1(makeString(colname)),
7870 neilc@samurai.com 813 : 0 : list_make1(node),
814 : : pstate->p_last_srf,
815 : : NULL,
816 : : false,
817 : : cref->location);
818 : : }
8504 bruce@momjian.us 819 : 0 : break;
820 : : }
8671 tgl@sss.pgh.pa.us 821 : 0 : default:
3100 822 : 0 : crerr = CRERR_TOO_MANY; /* too many dotted names */
8671 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 : : */
5890 tgl@sss.pgh.pa.us 835 [ + + ]:CBC 915686 : if (pstate->p_post_columnref_hook != NULL)
836 : : {
837 : : Node *hookresult;
838 : :
3022 peter_e@gmx.net 839 : 24577 : hookresult = pstate->p_post_columnref_hook(pstate, cref, node);
5890 tgl@sss.pgh.pa.us 840 [ + + ]: 24559 : if (node == NULL)
841 : 18336 : node = hookresult;
842 [ - + ]: 6223 : else if (hookresult != NULL)
7636 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 : : */
5890 tgl@sss.pgh.pa.us 853 [ + + ]:CBC 915668 : if (node == NULL)
854 : : {
855 [ + + - - : 236 : switch (crerr)
- ]
856 : : {
857 : 182 : case CRERR_NO_COLUMN:
4879 858 : 182 : errorMissingColumn(pstate, relname, colname, cref->location);
859 : : break;
5890 860 : 54 : case CRERR_NO_RTE:
861 : 54 : errorMissingRTE(pstate, makeRangeVar(nspname, relname,
862 : : cref->location));
863 : : break;
5890 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 : :
5890 tgl@sss.pgh.pa.us 881 :CBC 915432 : return node;
882 : : }
883 : :
884 : : static Node *
6763 885 : 20957 : 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 : : */
5890 893 [ + + ]: 20957 : if (pstate->p_paramref_hook != NULL)
3022 peter_e@gmx.net 894 : 20954 : result = pstate->p_paramref_hook(pstate, pref);
895 : : else
5890 tgl@sss.pgh.pa.us 896 : 3 : result = NULL;
897 : :
898 [ + + ]: 20957 : 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 : 20951 : return result;
905 : : }
906 : :
907 : : /* Test whether an a_expr is a plain NULL constant or not */
908 : : static bool
6623 909 : 1141 : exprIsNullConstant(Node *arg)
910 : : {
911 [ + - + + ]: 1141 : if (arg && IsA(arg, A_Const))
912 : : {
6606 bruce@momjian.us 913 : 54 : A_Const *con = (A_Const *) arg;
914 : :
1559 peter@eisentraut.org 915 [ + + ]: 54 : if (con->isnull)
6623 tgl@sss.pgh.pa.us 916 : 15 : return true;
917 : : }
918 : 1126 : return false;
919 : : }
920 : :
921 : : static Node *
7636 neilc@samurai.com 922 : 304312 : transformAExprOp(ParseState *pstate, A_Expr *a)
923 : : {
924 : 304312 : Node *lexpr = a->lexpr;
925 : 304312 : 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 [ - + - - ]: 304312 : if (Transform_null_equals &&
7636 neilc@samurai.com 936 :UBC 0 : list_length(a->name) == 1 &&
937 [ # # # # ]: 0 : strcmp(strVal(linitial(a->name)), "=") == 0 &&
5183 heikki.linnakangas@i 938 [ # # ]: 0 : (exprIsNullConstant(lexpr) || exprIsNullConstant(rexpr)) &&
2040 tgl@sss.pgh.pa.us 939 [ # # # # ]: 0 : (!IsA(lexpr, CaseTestExpr) && !IsA(rexpr, CaseTestExpr)))
7636 neilc@samurai.com 940 : 0 : {
941 : 0 : NullTest *n = makeNode(NullTest);
942 : :
943 : 0 : n->nulltesttype = IS_NULL;
3950 tgl@sss.pgh.pa.us 944 : 0 : n->location = a->location;
945 : :
7636 neilc@samurai.com 946 [ # # ]: 0 : if (exprIsNullConstant(lexpr))
947 : 0 : n->arg = (Expr *) rexpr;
948 : : else
949 : 0 : n->arg = (Expr *) lexpr;
950 : :
4876 tgl@sss.pgh.pa.us 951 : 0 : result = transformExprRecurse(pstate, (Node *) n);
952 : : }
7636 neilc@samurai.com 953 [ + + + + :CBC 304312 : else if (lexpr && IsA(lexpr, RowExpr) &&
+ - ]
954 [ + + ]: 413 : 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 : :
7293 tgl@sss.pgh.pa.us 964 : 15 : s->subLinkType = ROWCOMPARE_SUBLINK;
965 : 15 : s->testexpr = lexpr;
7636 neilc@samurai.com 966 : 15 : s->operName = a->name;
6319 tgl@sss.pgh.pa.us 967 : 15 : s->location = a->location;
4876 968 : 15 : result = transformExprRecurse(pstate, (Node *) s);
969 : : }
7636 neilc@samurai.com 970 [ + + + + : 304297 : else if (lexpr && IsA(lexpr, RowExpr) &&
+ - ]
971 [ + + ]: 398 : rexpr && IsA(rexpr, RowExpr))
972 : : {
973 : : /* ROW() op ROW() is handled specially */
4876 tgl@sss.pgh.pa.us 974 : 395 : lexpr = transformExprRecurse(pstate, lexpr);
975 : 395 : rexpr = transformExprRecurse(pstate, rexpr);
976 : :
7293 977 : 395 : result = make_row_comparison_op(pstate,
978 : : a->name,
3220 peter_e@gmx.net 979 : 395 : castNode(RowExpr, lexpr)->args,
980 : 395 : castNode(RowExpr, rexpr)->args,
981 : : a->location);
982 : : }
983 : : else
984 : : {
985 : : /* Ordinary scalar operator */
3108 tgl@sss.pgh.pa.us 986 : 303902 : Node *last_srf = pstate->p_last_srf;
987 : :
4876 988 : 303902 : lexpr = transformExprRecurse(pstate, lexpr);
989 : 303766 : rexpr = transformExprRecurse(pstate, rexpr);
990 : :
7636 neilc@samurai.com 991 : 303723 : result = (Node *) make_op(pstate,
992 : : a->name,
993 : : lexpr,
994 : : rexpr,
995 : : last_srf,
996 : : a->location);
997 : : }
998 : :
999 : 304068 : return result;
1000 : : }
1001 : :
1002 : : static Node *
1003 : 8479 : transformAExprOpAny(ParseState *pstate, A_Expr *a)
1004 : : {
1834 tgl@sss.pgh.pa.us 1005 : 8479 : Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1006 : 8479 : Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1007 : :
7636 neilc@samurai.com 1008 : 8479 : 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 : : {
1834 tgl@sss.pgh.pa.us 1019 : 150 : Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1020 : 150 : Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1021 : :
7636 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 : 578 : transformAExprDistinct(ParseState *pstate, A_Expr *a)
1032 : : {
3933 tgl@sss.pgh.pa.us 1033 : 578 : Node *lexpr = a->lexpr;
1034 : 578 : 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 : : */
3428 1042 [ + + ]: 578 : if (exprIsNullConstant(rexpr))
1043 : 15 : return make_nulltest_from_distinct(pstate, a, lexpr);
1044 [ - + ]: 563 : if (exprIsNullConstant(lexpr))
3428 tgl@sss.pgh.pa.us 1045 :UBC 0 : return make_nulltest_from_distinct(pstate, a, rexpr);
1046 : :
3933 tgl@sss.pgh.pa.us 1047 :CBC 563 : lexpr = transformExprRecurse(pstate, lexpr);
1048 : 563 : rexpr = transformExprRecurse(pstate, rexpr);
1049 : :
7636 neilc@samurai.com 1050 [ + - + + : 563 : if (lexpr && IsA(lexpr, RowExpr) &&
+ - ]
1051 [ + - ]: 3 : rexpr && IsA(rexpr, RowExpr))
1052 : : {
1053 : : /* ROW() op ROW() is handled specially */
3428 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 : 560 : 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 [ + + ]: 563 : if (a->kind == AEXPR_NOT_DISTINCT)
1074 : 28 : result = (Node *) makeBoolExpr(NOT_EXPR,
1075 : 28 : list_make1(result),
1076 : : a->location);
1077 : :
1078 : 563 : return result;
1079 : : }
1080 : :
1081 : : static Node *
7636 neilc@samurai.com 1082 : 241 : transformAExprNullIf(ParseState *pstate, A_Expr *a)
1083 : : {
4876 tgl@sss.pgh.pa.us 1084 : 241 : Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1085 : 241 : Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1086 : : OpExpr *result;
1087 : :
5386 1088 : 241 : 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 [ - + ]: 241 : if (result->opresulttype != BOOLOID)
7636 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)));
3108 tgl@sss.pgh.pa.us 1104 [ - + ]:CBC 241 : if (result->opretset)
3108 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 : : */
5386 tgl@sss.pgh.pa.us 1114 :CBC 241 : result->opresulttype = exprType((Node *) linitial(result->args));
1115 : :
1116 : : /*
1117 : : * We rely on NullIfExpr and OpExpr being the same struct
1118 : : */
7636 neilc@samurai.com 1119 : 241 : NodeSetTag(result, T_NullIfExpr);
1120 : :
5386 tgl@sss.pgh.pa.us 1121 : 241 : return (Node *) result;
1122 : : }
1123 : :
1124 : : static Node *
7323 1125 : 10994 : transformAExprIn(ParseState *pstate, A_Expr *a)
1126 : : {
6260 1127 : 10994 : 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 : : */
7323 1138 [ + + ]: 10994 : if (strcmp(strVal(linitial(a->name)), "<>") == 0)
1139 : 1374 : useOr = false;
1140 : : else
1141 : 9620 : 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 : : */
4876 1154 : 10994 : lexpr = transformExprRecurse(pstate, a->lexpr);
6260 1155 : 10994 : rexprs = rvars = rnonvars = NIL;
7323 1156 [ + - + + : 41047 : foreach(l, (List *) a->rexpr)
+ + ]
1157 : : {
4876 1158 : 30056 : Node *rexpr = transformExprRecurse(pstate, lfirst(l));
1159 : :
7323 1160 : 30053 : rexprs = lappend(rexprs, rexpr);
6260 1161 [ - + ]: 30053 : if (contain_vars_of_level(rexpr, 0))
6260 tgl@sss.pgh.pa.us 1162 :UBC 0 : rvars = lappend(rvars, rexpr);
1163 : : else
6260 tgl@sss.pgh.pa.us 1164 :CBC 30053 : 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 : : */
4573 1171 [ + + ]: 10991 : 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 : : */
6260 1184 : 9472 : allexprs = list_concat(list_make1(lexpr), rnonvars);
6261 1185 : 9472 : scalar_type = select_common_type(pstate, allexprs, NULL, NULL);
1186 : :
1187 : : /* We have to verify that the selected type actually works */
1417 1188 [ + + ]: 9472 : if (OidIsValid(scalar_type) &&
1189 [ + + ]: 9470 : !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 : : */
4573 1198 [ + + + + ]: 9472 : if (OidIsValid(scalar_type) && scalar_type != RECORDOID)
6261 1199 : 9454 : array_type = get_array_type(scalar_type);
1200 : : else
1201 : 18 : array_type = InvalidOid;
7323 1202 [ + + ]: 9472 : 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 : 9448 : aexprs = NIL;
6260 1212 [ + - + + : 37934 : foreach(l, rnonvars)
+ + ]
1213 : : {
7323 1214 : 28486 : Node *rexpr = (Node *) lfirst(l);
1215 : :
1216 : 28486 : rexpr = coerce_to_common_type(pstate, rexpr,
1217 : : scalar_type,
1218 : : "IN");
1219 : 28486 : aexprs = lappend(aexprs, rexpr);
1220 : : }
1221 : 9448 : newa = makeNode(ArrayExpr);
1222 : 9448 : newa->array_typeid = array_type;
1223 : : /* array_collid will be set by parse_collate.c */
1224 : 9448 : newa->element_typeid = scalar_type;
1225 : 9448 : newa->elements = aexprs;
1226 : 9448 : newa->multidims = false;
187 alvherre@kurilemu.de 1227 : 9448 : newa->list_start = a->rexpr_list_start;
1228 : 9448 : newa->list_end = a->rexpr_list_end;
6319 tgl@sss.pgh.pa.us 1229 : 9448 : newa->location = -1;
1230 : :
6260 1231 : 9448 : 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 : 9448 : rexprs = rvars;
1240 : : }
1241 : : }
1242 : :
1243 : : /*
1244 : : * Must do it the hard way, ie, with a boolean expression tree.
1245 : : */
7323 1246 [ + + + + : 12552 : foreach(l, rexprs)
+ + ]
1247 : : {
1248 : 1564 : Node *rexpr = (Node *) lfirst(l);
1249 : : Node *cmp;
1250 : :
4573 1251 [ + + ]: 1564 : if (IsA(lexpr, RowExpr) &&
1252 [ + - ]: 26 : IsA(rexpr, RowExpr))
1253 : : {
1254 : : /* ROW() op ROW() is handled specially */
7293 1255 : 26 : cmp = make_row_comparison_op(pstate,
1256 : : a->name,
3100 1257 : 26 : copyObject(((RowExpr *) lexpr)->args),
1258 : : ((RowExpr *) rexpr)->args,
1259 : : a->location);
1260 : : }
1261 : : else
1262 : : {
1263 : : /* Ordinary scalar operator */
7323 1264 : 1538 : cmp = (Node *) make_op(pstate,
1265 : : a->name,
1266 : 1538 : copyObject(lexpr),
1267 : : rexpr,
1268 : : pstate->p_last_srf,
1269 : : a->location);
1270 : : }
1271 : :
1272 : 1561 : cmp = coerce_to_boolean(pstate, cmp, "IN");
1273 [ + + ]: 1561 : if (result == NULL)
1274 : 1540 : result = cmp;
1275 : : else
1276 : 21 : result = (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
6319 1277 : 21 : list_make2(result, cmp),
1278 : : a->location);
1279 : : }
1280 : :
7323 1281 : 10988 : return result;
1282 : : }
1283 : :
1284 : : static Node *
3950 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;
3220 peter_e@gmx.net 1297 : 259 : args = castNode(List, a->rexpr);
3950 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;
3950 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 : :
3950 tgl@sss.pgh.pa.us 1375 :CBC 259 : return transformExprRecurse(pstate, result);
1376 : : }
1377 : :
1378 : : static Node *
639 dean.a.rasheed@gmail 1379 : 102 : 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 [ + + ]: 102 : 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)
639 dean.a.rasheed@gmail 1391 :UBC 0 : parent_pstate = parent_pstate->parentParseState;
1392 : :
639 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 : 96 : return (Node *) f;
1401 : : }
1402 : :
1403 : : static Node *
4201 tgl@sss.pgh.pa.us 1404 : 78306 : transformBoolExpr(ParseState *pstate, BoolExpr *a)
1405 : : {
1406 : 78306 : List *args = NIL;
1407 : : const char *opname;
1408 : : ListCell *lc;
1409 : :
1410 [ + + + - ]: 78306 : switch (a->boolop)
1411 : : {
1412 : 63896 : case AND_EXPR:
1413 : 63896 : opname = "AND";
1414 : 63896 : break;
1415 : 6088 : case OR_EXPR:
1416 : 6088 : opname = "OR";
1417 : 6088 : break;
1418 : 8322 : case NOT_EXPR:
1419 : 8322 : opname = "NOT";
1420 : 8322 : break;
4201 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 : :
4201 tgl@sss.pgh.pa.us 1427 [ + - + + :CBC 283477 : foreach(lc, a->args)
+ + ]
1428 : : {
1429 : 205181 : Node *arg = (Node *) lfirst(lc);
1430 : :
1431 : 205181 : arg = transformExprRecurse(pstate, arg);
1432 : 205171 : arg = coerce_to_boolean(pstate, arg, opname);
1433 : 205171 : args = lappend(args, arg);
1434 : : }
1435 : :
1436 : 78296 : return (Node *) makeBoolExpr(a->boolop, args, a->location);
1437 : : }
1438 : :
1439 : : static Node *
7636 neilc@samurai.com 1440 : 190226 : transformFuncCall(ParseState *pstate, FuncCall *fn)
1441 : : {
3108 tgl@sss.pgh.pa.us 1442 : 190226 : Node *last_srf = pstate->p_last_srf;
1443 : : List *targs;
1444 : : ListCell *args;
1445 : :
1446 : : /* Transform the list of arguments ... */
6362 1447 : 190226 : targs = NIL;
1448 [ + + + + : 502111 : foreach(args, fn->args)
+ + ]
1449 : : {
4876 1450 : 311885 : targs = lappend(targs, transformExprRecurse(pstate,
1451 : 311915 : (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 : : */
4376 1462 [ + + ]: 190196 : 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 */
5619 1475 : 190196 : return ParseFuncOrColumn(pstate,
1476 : : fn->funcname,
1477 : : targs,
1478 : : last_srf,
1479 : : fn,
1480 : : false,
1481 : : fn->location);
1482 : : }
1483 : :
1484 : : static Node *
4199 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 : : */
3311 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 : :
3246 1514 : 69 : qtree = castNode(Query, sublink->subselect);
1515 : :
1516 : : /* Check subquery returns required number of columns */
3311 1517 [ - + ]: 69 : if (count_nonjunk_tlist_entries(qtree->targetList) != maref->ncolumns)
3311 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 : : */
3311 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)
3311 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 : : */
3311 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 : : */
4199 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 : : */
3311 1583 [ + + ]: 184 : if (IsA(tle->expr, SubLink))
1584 : : {
1585 : : Param *param;
1586 : :
4199 1587 : 140 : sublink = (SubLink *) tle->expr;
1588 [ - + ]: 140 : Assert(sublink->subLinkType == MULTIEXPR_SUBLINK);
3246 1589 : 140 : qtree = castNode(Query, sublink->subselect);
1590 : :
1591 : : /* Build a Param representing the current subquery output column */
3311 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 =
1881 drowley@postgresql.o 1623 : 19 : list_delete_last(pstate->p_multiassign_exprs);
1624 : :
3311 tgl@sss.pgh.pa.us 1625 : 44 : return result;
1626 : : }
1627 : :
3311 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 *
7636 neilc@samurai.com 1633 :CBC 20177 : transformCaseExpr(ParseState *pstate, CaseExpr *c)
1634 : : {
3108 tgl@sss.pgh.pa.us 1635 : 20177 : CaseExpr *newc = makeNode(CaseExpr);
1636 : 20177 : 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 */
4876 1646 : 20177 : arg = transformExprRecurse(pstate, (Node *) c->arg);
1647 : :
1648 : : /* generate placeholder for test expression */
7636 neilc@samurai.com 1649 [ + + ]: 20177 : 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 [ + + ]: 3601 : 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 : : */
5386 tgl@sss.pgh.pa.us 1667 : 3601 : assign_expr_collations(pstate, arg);
1668 : :
7636 neilc@samurai.com 1669 : 3601 : placeholder = makeNode(CaseTestExpr);
1670 : 3601 : placeholder->typeId = exprType(arg);
1671 : 3601 : placeholder->typeMod = exprTypmod(arg);
5425 peter_e@gmx.net 1672 : 3601 : placeholder->collation = exprCollation(arg);
1673 : : }
1674 : : else
7636 neilc@samurai.com 1675 : 16576 : placeholder = NULL;
1676 : :
1677 : 20177 : newc->arg = (Expr *) arg;
1678 : :
1679 : : /* transform the list of arguments */
1680 : 20177 : newargs = NIL;
6319 tgl@sss.pgh.pa.us 1681 : 20177 : resultexprs = NIL;
7636 neilc@samurai.com 1682 [ + - + + : 55597 : foreach(l, c->args)
+ + ]
1683 : : {
3172 tgl@sss.pgh.pa.us 1684 : 35420 : CaseWhen *w = lfirst_node(CaseWhen, l);
7636 neilc@samurai.com 1685 : 35420 : CaseWhen *neww = makeNode(CaseWhen);
1686 : : Node *warg;
1687 : :
1688 : 35420 : warg = (Node *) w->expr;
1689 [ + + ]: 35420 : if (placeholder)
1690 : : {
1691 : : /* shorthand form was specified, so expand... */
1692 : 13644 : warg = (Node *) makeSimpleA_Expr(AEXPR_OP, "=",
1693 : : (Node *) placeholder,
1694 : : warg,
1695 : : w->location);
1696 : : }
4876 tgl@sss.pgh.pa.us 1697 : 35420 : neww->expr = (Expr *) transformExprRecurse(pstate, warg);
1698 : :
7636 neilc@samurai.com 1699 : 70840 : neww->expr = (Expr *) coerce_to_boolean(pstate,
1700 : 35420 : (Node *) neww->expr,
1701 : : "CASE/WHEN");
1702 : :
1703 : 35420 : warg = (Node *) w->result;
4876 tgl@sss.pgh.pa.us 1704 : 35420 : neww->result = (Expr *) transformExprRecurse(pstate, warg);
6319 1705 : 35420 : neww->location = w->location;
1706 : :
7636 neilc@samurai.com 1707 : 35420 : newargs = lappend(newargs, neww);
6319 tgl@sss.pgh.pa.us 1708 : 35420 : resultexprs = lappend(resultexprs, neww->result);
1709 : : }
1710 : :
7636 neilc@samurai.com 1711 : 20177 : newc->args = newargs;
1712 : :
1713 : : /* transform the default clause */
1714 : 20177 : defresult = (Node *) c->defresult;
1715 [ + + ]: 20177 : if (defresult == NULL)
1716 : : {
1717 : 5031 : A_Const *n = makeNode(A_Const);
1718 : :
1559 peter@eisentraut.org 1719 : 5031 : n->isnull = true;
6319 tgl@sss.pgh.pa.us 1720 : 5031 : n->location = -1;
7636 neilc@samurai.com 1721 : 5031 : defresult = (Node *) n;
1722 : : }
4876 tgl@sss.pgh.pa.us 1723 : 20177 : 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 : : */
6319 1730 : 20177 : resultexprs = lcons(newc->defresult, resultexprs);
1731 : :
1732 : 20177 : ptype = select_common_type(pstate, resultexprs, "CASE", NULL);
7636 neilc@samurai.com 1733 [ - + ]: 20177 : Assert(OidIsValid(ptype));
1734 : 20177 : newc->casetype = ptype;
1735 : : /* casecollid will be set by parse_collate.c */
1736 : :
1737 : : /* Convert default result clause, if necessary */
1738 : 20177 : newc->defresult = (Expr *)
1739 : 20177 : coerce_to_common_type(pstate,
1740 : 20177 : (Node *) newc->defresult,
1741 : : ptype,
1742 : : "CASE/ELSE");
1743 : :
1744 : : /* Convert when-clause results, if necessary */
1745 [ + - + + : 55597 : foreach(l, newc->args)
+ + ]
1746 : : {
1747 : 35420 : CaseWhen *w = (CaseWhen *) lfirst(l);
1748 : :
1749 : 35420 : w->result = (Expr *)
1750 : 35420 : coerce_to_common_type(pstate,
1751 : 35420 : (Node *) w->result,
1752 : : ptype,
1753 : : "CASE/WHEN");
1754 : : }
1755 : :
1756 : : /* if any subexpression contained a SRF, complain */
3108 tgl@sss.pgh.pa.us 1757 [ + + ]: 20177 : 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 : :
6319 1767 : 20174 : newc->location = c->location;
1768 : :
7636 neilc@samurai.com 1769 : 20174 : return (Node *) newc;
1770 : : }
1771 : :
1772 : : static Node *
1773 : 24509 : transformSubLink(ParseState *pstate, SubLink *sublink)
1774 : : {
1775 : 24509 : 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 : : */
4876 tgl@sss.pgh.pa.us 1784 : 24509 : err = NULL;
1785 [ - - + - : 24509 : switch (pstate->p_expr_kind)
+ - - - -
- - + + -
+ + - ]
1786 : : {
4876 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;
4876 tgl@sss.pgh.pa.us 1793 :CBC 24488 : 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 : 24488 : break;
4876 tgl@sss.pgh.pa.us 1823 :UBC 0 : case EXPR_KIND_CHECK_CONSTRAINT:
1824 : : case EXPR_KIND_DOMAIN_CHECK:
4728 peter_e@gmx.net 1825 : 0 : err = _("cannot use subquery in check constraint");
4876 tgl@sss.pgh.pa.us 1826 : 0 : break;
4876 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;
4876 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;
1726 tomas.vondra@postgre 1837 : 0 : case EXPR_KIND_STATS_EXPRESSION:
1838 : 0 : err = _("cannot use subquery in statistics expression");
1839 : 0 : break;
4876 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;
2517 peter@eisentraut.org 1849 :CBC 6 : case EXPR_KIND_PARTITION_BOUND:
1850 : 6 : err = _("cannot use subquery in partition bound");
1851 : 6 : break;
3296 rhaas@postgresql.org 1852 : 3 : case EXPR_KIND_PARTITION_EXPRESSION:
1853 : 3 : err = _("cannot use subquery in partition key expression");
1854 : 3 : break;
2866 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;
2523 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;
2453 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 : : }
4876 tgl@sss.pgh.pa.us 1873 [ + + ]: 24509 : if (err)
1874 [ + - ]: 21 : ereport(ERROR,
1875 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1876 : : errmsg_internal("%s", err),
1877 : : parser_errposition(pstate, sublink->location)));
1878 : :
7636 neilc@samurai.com 1879 : 24488 : pstate->p_hasSubLinks = true;
1880 : :
1881 : : /*
1882 : : * OK, let's transform the sub-SELECT.
1883 : : */
3247 tgl@sss.pgh.pa.us 1884 : 24488 : 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 : : */
6315 1890 [ + - ]: 24464 : if (!IsA(qtree, Query) ||
3258 1891 [ - + ]: 24464 : qtree->commandType != CMD_SELECT)
6315 tgl@sss.pgh.pa.us 1892 [ # # ]:UBC 0 : elog(ERROR, "unexpected non-SELECT command in SubLink");
1893 : :
7636 neilc@samurai.com 1894 :CBC 24464 : sublink->subselect = (Node *) qtree;
1895 : :
1896 [ + + ]: 24464 : 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 : : */
7293 tgl@sss.pgh.pa.us 1902 : 3113 : sublink->testexpr = NULL;
7636 neilc@samurai.com 1903 : 3113 : sublink->operName = NIL;
1904 : : }
1905 [ + + ]: 21351 : else if (sublink->subLinkType == EXPR_SUBLINK ||
1906 [ + + ]: 7408 : sublink->subLinkType == ARRAY_SUBLINK)
1907 : : {
1908 : : /*
1909 : : * Make sure the subselect delivers a single column (ignoring resjunk
1910 : : * targets).
1911 : : */
4199 tgl@sss.pgh.pa.us 1912 [ - + ]: 18260 : if (count_nonjunk_tlist_entries(qtree->targetList) != 1)
7636 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 : : */
7293 tgl@sss.pgh.pa.us 1922 :CBC 18260 : sublink->testexpr = NULL;
7636 neilc@samurai.com 1923 : 18260 : sublink->operName = NIL;
1924 : : }
4199 tgl@sss.pgh.pa.us 1925 [ + + ]: 3091 : 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 : : */
3949 1942 [ + + ]: 3022 : if (sublink->operName == NIL)
1943 : 2917 : sublink->operName = list_make1(makeString("="));
1944 : :
1945 : : /*
1946 : : * Transform lefthand expression, and convert to a list
1947 : : */
4876 1948 : 3022 : lefthand = transformExprRecurse(pstate, sublink->testexpr);
7293 1949 [ + - + + ]: 3022 : if (lefthand && IsA(lefthand, RowExpr))
1950 : 156 : left_list = ((RowExpr *) lefthand)->args;
1951 : : else
1952 : 2866 : 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 : 3022 : right_list = NIL;
1959 [ + - + + : 6298 : foreach(l, qtree->targetList)
+ + ]
1960 : : {
7636 neilc@samurai.com 1961 : 3276 : TargetEntry *tent = (TargetEntry *) lfirst(l);
1962 : : Param *param;
1963 : :
7559 tgl@sss.pgh.pa.us 1964 [ + + ]: 3276 : if (tent->resjunk)
7636 neilc@samurai.com 1965 : 6 : continue;
1966 : :
7293 tgl@sss.pgh.pa.us 1967 : 3270 : param = makeNode(Param);
1968 : 3270 : param->paramkind = PARAM_SUBLINK;
7178 1969 : 3270 : param->paramid = tent->resno;
7293 1970 : 3270 : param->paramtype = exprType((Node *) tent->expr);
6946 1971 : 3270 : param->paramtypmod = exprTypmod((Node *) tent->expr);
5386 1972 : 3270 : param->paramcollid = exprCollation((Node *) tent->expr);
6319 1973 : 3270 : param->location = -1;
1974 : :
7293 1975 : 3270 : 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 [ - + ]: 3022 : if (list_length(left_list) < list_length(right_list))
7293 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)));
7293 tgl@sss.pgh.pa.us 1988 [ - + ]:CBC 3022 : if (list_length(left_list) > list_length(right_list))
7636 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 : : */
7293 tgl@sss.pgh.pa.us 1998 :CBC 3022 : sublink->testexpr = make_row_comparison_op(pstate,
1999 : : sublink->operName,
2000 : : left_list,
2001 : : right_list,
2002 : : sublink->location);
2003 : : }
2004 : :
7636 neilc@samurai.com 2005 : 24458 : 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 *
6480 tgl@sss.pgh.pa.us 2016 : 4002 : transformArrayExpr(ParseState *pstate, A_ArrayExpr *a,
2017 : : Oid array_type, Oid element_type, int32 typmod)
2018 : : {
7636 neilc@samurai.com 2019 : 4002 : ArrayExpr *newa = makeNode(ArrayExpr);
2020 : 4002 : List *newelems = NIL;
2021 : 4002 : 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 : : */
6480 tgl@sss.pgh.pa.us 2032 : 4002 : newa->multidims = false;
7636 neilc@samurai.com 2033 [ + + + + : 13399 : foreach(element, a->elements)
+ + ]
2034 : : {
2035 : 9397 : 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 : : */
6480 tgl@sss.pgh.pa.us 2042 [ + + ]: 9397 : 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 */
6319 2050 [ + + - + ]: 420 : Assert(array_type == InvalidOid || array_type == exprType(newe));
6480 2051 : 420 : newa->multidims = true;
2052 : : }
2053 : : else
2054 : : {
4876 2055 : 8977 : 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 : : */
278 2063 [ + - ]: 8977 : if (!newa->multidims)
2064 : : {
2065 : 8977 : Oid newetype = exprType(newe);
2066 : :
2067 [ + + + + : 17930 : if (newetype != INT2VECTOROID && newetype != OIDVECTOROID &&
+ + ]
2068 : 8953 : type_is_array(newetype))
2069 : 3 : newa->multidims = true;
2070 : : }
2071 : : }
2072 : :
7636 neilc@samurai.com 2073 : 9397 : 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 : : */
6480 tgl@sss.pgh.pa.us 2082 [ + + ]: 4002 : 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 */
6319 2092 [ + + ]: 3581 : if (newelems == NIL)
6480 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 */
6319 2101 : 3578 : coerce_type = select_common_type(pstate, newelems, "ARRAY", NULL);
2102 : :
6480 2103 [ + + ]: 3578 : 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))
6480 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 : : {
6480 tgl@sss.pgh.pa.us 2116 :CBC 3378 : element_type = coerce_type;
2117 : 3378 : array_type = get_array_type(element_type);
2118 [ - + ]: 3378 : if (!OidIsValid(array_type))
6480 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 : : }
6480 tgl@sss.pgh.pa.us 2125 :CBC 3578 : 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 : : */
7636 neilc@samurai.com 2138 [ + + + + : 13396 : foreach(element, newelems)
+ + ]
2139 : : {
2140 : 9397 : Node *e = (Node *) lfirst(element);
2141 : : Node *newe;
2142 : :
6480 tgl@sss.pgh.pa.us 2143 [ + + ]: 9397 : if (coerce_hard)
2144 : : {
6032 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);
6480 tgl@sss.pgh.pa.us 2152 [ - + ]: 1019 : if (newe == NULL)
6480 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
6480 tgl@sss.pgh.pa.us 2161 :CBC 8378 : newe = coerce_to_common_type(pstate, e,
2162 : : coerce_type,
2163 : : "ARRAY");
7636 neilc@samurai.com 2164 : 9397 : newcoercedelems = lappend(newcoercedelems, newe);
2165 : : }
2166 : :
2167 : 3999 : newa->array_typeid = array_type;
2168 : : /* array_collid will be set by parse_collate.c */
2169 : 3999 : newa->element_typeid = element_type;
2170 : 3999 : newa->elements = newcoercedelems;
187 alvherre@kurilemu.de 2171 : 3999 : newa->list_start = a->list_start;
2172 : 3999 : newa->list_end = a->list_end;
6319 tgl@sss.pgh.pa.us 2173 : 3999 : newa->location = a->location;
2174 : :
7636 neilc@samurai.com 2175 : 3999 : return (Node *) newa;
2176 : : }
2177 : :
2178 : : static Node *
3311 tgl@sss.pgh.pa.us 2179 : 2971 : transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
2180 : : {
2181 : : RowExpr *newr;
2182 : : char fname[16];
2183 : : int fnum;
2184 : :
4741 2185 : 2971 : newr = makeNode(RowExpr);
2186 : :
2187 : : /* Transform the field expressions */
3311 2188 : 2971 : newr->args = transformExpressionList(pstate, r->args,
2189 : : pstate->p_expr_kind, allowDefault);
2190 : :
2191 : : /* Disallow more columns than will fit in a tuple */
1236 2192 [ - + ]: 2971 : if (list_length(newr->args) > MaxTupleAttributeNumber)
1236 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 */
7636 neilc@samurai.com 2200 :CBC 2971 : newr->row_typeid = RECORDOID;
2201 : 2971 : newr->row_format = COERCE_IMPLICIT_CAST;
2202 : :
2203 : : /* ROW() has anonymous columns, so invent some field names */
5054 tgl@sss.pgh.pa.us 2204 : 2971 : newr->colnames = NIL;
2346 2205 [ + + ]: 10390 : for (fnum = 1; fnum <= list_length(newr->args); fnum++)
2206 : : {
2207 : 7419 : snprintf(fname, sizeof(fname), "f%d", fnum);
5054 2208 : 7419 : newr->colnames = lappend(newr->colnames, makeString(pstrdup(fname)));
2209 : : }
2210 : :
6319 2211 : 2971 : newr->location = r->location;
2212 : :
7636 neilc@samurai.com 2213 : 2971 : return (Node *) newr;
2214 : : }
2215 : :
2216 : : static Node *
2217 : 1633 : transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c)
2218 : : {
2219 : 1633 : CoalesceExpr *newc = makeNode(CoalesceExpr);
3108 tgl@sss.pgh.pa.us 2220 : 1633 : Node *last_srf = pstate->p_last_srf;
7636 neilc@samurai.com 2221 : 1633 : List *newargs = NIL;
2222 : 1633 : List *newcoercedargs = NIL;
2223 : : ListCell *args;
2224 : :
2225 [ + - + + : 4893 : foreach(args, c->args)
+ + ]
2226 : : {
2227 : 3260 : Node *e = (Node *) lfirst(args);
2228 : : Node *newe;
2229 : :
4876 tgl@sss.pgh.pa.us 2230 : 3260 : newe = transformExprRecurse(pstate, e);
7636 neilc@samurai.com 2231 : 3260 : newargs = lappend(newargs, newe);
2232 : : }
2233 : :
6319 tgl@sss.pgh.pa.us 2234 : 1633 : newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
2235 : : /* coalescecollid will be set by parse_collate.c */
2236 : :
2237 : : /* Convert arguments if necessary */
7636 neilc@samurai.com 2238 [ + - + + : 4893 : foreach(args, newargs)
+ + ]
2239 : : {
2240 : 3260 : Node *e = (Node *) lfirst(args);
2241 : : Node *newe;
2242 : :
2243 : 3260 : newe = coerce_to_common_type(pstate, e,
2244 : : newc->coalescetype,
2245 : : "COALESCE");
2246 : 3260 : newcoercedargs = lappend(newcoercedargs, newe);
2247 : : }
2248 : :
2249 : : /* if any subexpression contained a SRF, complain */
3108 tgl@sss.pgh.pa.us 2250 [ + + ]: 1633 : 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 : :
7636 neilc@samurai.com 2260 : 1630 : newc->args = newcoercedargs;
6319 tgl@sss.pgh.pa.us 2261 : 1630 : newc->location = c->location;
7636 neilc@samurai.com 2262 : 1630 : return (Node *) newc;
2263 : : }
2264 : :
2265 : : static Node *
7478 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;
6319 2271 [ + + ]: 134 : const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
2272 : : ListCell *args;
2273 : :
7478 2274 : 134 : newm->op = m->op;
2275 [ + - + + : 429 : foreach(args, m->args)
+ + ]
2276 : : {
2277 : 295 : Node *e = (Node *) lfirst(args);
2278 : : Node *newe;
2279 : :
4876 2280 : 295 : newe = transformExprRecurse(pstate, e);
7478 2281 : 295 : newargs = lappend(newargs, newe);
2282 : : }
2283 : :
6319 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 */
7478 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;
6319 2300 : 134 : newm->location = m->location;
7478 2301 : 134 : return (Node *) newm;
2302 : : }
2303 : :
2304 : : static Node *
944 michael@paquier.xyz 2305 : 1368 : 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 [ + + + + : 1368 : switch (svf->op)
+ + + + +
+ - ]
2312 : : {
2313 : 156 : case SVFOP_CURRENT_DATE:
2314 : 156 : svf->type = DATEOID;
2315 : 156 : 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 : 88 : case SVFOP_CURRENT_TIMESTAMP_N:
2327 : 88 : svf->type = TIMESTAMPTZOID;
2328 : 88 : svf->typmod = anytimestamp_typmod_check(true, svf->typmod);
2329 : 88 : 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 : 903 : 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 : 903 : svf->type = NAMEOID;
2351 : 903 : break;
2352 : : }
2353 : :
2354 : 1368 : return (Node *) svf;
2355 : : }
2356 : :
2357 : : static Node *
6606 bruce@momjian.us 2358 : 298 : transformXmlExpr(ParseState *pstate, XmlExpr *x)
2359 : : {
2360 : : XmlExpr *newx;
2361 : : ListCell *lc;
2362 : : int i;
2363 : :
4741 tgl@sss.pgh.pa.us 2364 : 298 : newx = makeNode(XmlExpr);
6932 2365 : 298 : newx->op = x->op;
2366 [ + + ]: 298 : if (x->name)
6883 peter_e@gmx.net 2367 : 129 : newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
2368 : : else
6932 tgl@sss.pgh.pa.us 2369 : 169 : newx->name = NULL;
6319 2370 : 298 : newx->xmloption = x->xmloption;
4741 2371 : 298 : newx->type = XMLOID; /* this just marks the node as transformed */
2372 : 298 : newx->typmod = -1;
6319 2373 : 298 : 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 : : */
6932 2379 : 298 : newx->named_args = NIL;
2380 : 298 : newx->arg_names = NIL;
2381 : :
2382 [ + + + + : 408 : foreach(lc, x->named_args)
+ + ]
2383 : : {
3172 2384 : 116 : ResTarget *r = lfirst_node(ResTarget, lc);
2385 : : Node *expr;
2386 : : char *argname;
2387 : :
4876 2388 : 116 : expr = transformExprRecurse(pstate, r->val);
2389 : :
6932 2390 [ + + ]: 116 : if (r->name)
6883 peter_e@gmx.net 2391 : 53 : argname = map_sql_identifier_to_xml_name(r->name, false, false);
6932 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 */
6319 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)
6917 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 : :
6319 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 */
6932 2427 : 292 : newx->args = NIL;
2428 : 292 : i = 0;
2429 [ + + + + : 701 : foreach(lc, x->args)
+ + ]
2430 : : {
2431 : 418 : Node *e = (Node *) lfirst(lc);
2432 : : Node *newe;
2433 : :
4876 2434 : 418 : newe = transformExprRecurse(pstate, e);
6932 2435 [ + + - + : 418 : switch (x->op)
+ + - +
- ]
2436 : : {
2437 : 65 : case IS_XMLCONCAT:
2438 : 65 : newe = coerce_to_specific_type(pstate, newe, XMLOID,
2439 : : "XMLCONCAT");
2440 : 59 : break;
6913 peter_e@gmx.net 2441 : 68 : case IS_XMLELEMENT:
2442 : : /* no coercion necessary */
2443 : 68 : break;
6932 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;
6932 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
6900 peter_e@gmx.net 2467 : 34 : newe = coerce_to_specific_type(pstate, newe, INT4OID,
2468 : : "XMLROOT");
6932 tgl@sss.pgh.pa.us 2469 : 102 : break;
6891 peter_e@gmx.net 2470 :UBC 0 : case IS_XMLSERIALIZE:
2471 : : /* not handled here */
6319 tgl@sss.pgh.pa.us 2472 : 0 : Assert(false);
2473 : : break;
6911 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 : : }
6932 tgl@sss.pgh.pa.us 2479 : 409 : newx->args = lappend(newx->args, newe);
2480 : 409 : i++;
2481 : : }
2482 : :
2483 : 283 : return (Node *) newx;
2484 : : }
2485 : :
2486 : : static Node *
6606 bruce@momjian.us 2487 : 109 : transformXmlSerialize(ParseState *pstate, XmlSerialize *xs)
2488 : : {
2489 : : Node *result;
2490 : : XmlExpr *xexpr;
2491 : : Oid targetType;
2492 : : int32 targetTypmod;
2493 : :
6891 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 : :
5531 2501 : 109 : typenameTypeIdAndMod(pstate, xs->typeName, &targetType, &targetTypmod);
2502 : :
6891 2503 : 109 : xexpr->xmloption = xs->xmloption;
1007 tgl@sss.pgh.pa.us 2504 : 109 : xexpr->indent = xs->indent;
6319 2505 : 109 : xexpr->location = xs->location;
2506 : : /* We actually only need these to be able to parse back the expression. */
6891 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 : : */
6319 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)
6319 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)));
6319 tgl@sss.pgh.pa.us 2527 :CBC 109 : return result;
2528 : : }
2529 : :
2530 : : static Node *
7636 neilc@samurai.com 2531 : 474 : transformBooleanTest(ParseState *pstate, BooleanTest *b)
2532 : : {
2533 : : const char *clausename;
2534 : :
2535 [ + + + + : 474 : switch (b->booltesttype)
+ + - ]
2536 : : {
2537 : 229 : case IS_TRUE:
2538 : 229 : clausename = "IS TRUE";
2539 : 229 : break;
2540 : 70 : case IS_NOT_TRUE:
2541 : 70 : clausename = "IS NOT TRUE";
2542 : 70 : break;
2543 : 79 : case IS_FALSE:
2544 : 79 : clausename = "IS FALSE";
2545 : 79 : 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;
7636 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 : :
4876 tgl@sss.pgh.pa.us 2561 :CBC 474 : b->arg = (Expr *) transformExprRecurse(pstate, (Node *) b->arg);
2562 : :
7636 neilc@samurai.com 2563 : 948 : b->arg = (Expr *) coerce_to_boolean(pstate,
2564 : 474 : (Node *) b->arg,
2565 : : clausename);
2566 : :
2567 : 474 : return (Node *) b;
2568 : : }
2569 : :
2570 : : static Node *
6606 bruce@momjian.us 2571 : 127 : transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr)
2572 : : {
2573 : : /* CURRENT OF can only appear at top level of UPDATE/DELETE */
2175 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 : : */
3100 2582 [ + - ]: 127 : if (cexpr->cursor_name != NULL) /* in case already transformed */
2583 : : {
5881 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)
3022 peter_e@gmx.net 2593 : 6 : node = pstate->p_pre_columnref_hook(pstate, cref);
5881 tgl@sss.pgh.pa.us 2594 [ + - + + ]: 127 : if (node == NULL && pstate->p_post_columnref_hook != NULL)
3022 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 : : */
5881 tgl@sss.pgh.pa.us 2602 [ + + + - ]: 127 : if (node != NULL && IsA(node, Param))
2603 : : {
5772 bruce@momjian.us 2604 : 6 : Param *p = (Param *) node;
2605 : :
5881 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 : :
6763 2616 : 127 : return (Node *) cexpr;
2617 : : }
2618 : :
2619 : : /*
2620 : : * Construct a whole-row reference to represent the notation "relation.*".
2621 : : */
2622 : : static Node *
2182 2623 : 4592 : 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 : : */
334 dean.a.rasheed@gmail 2649 [ + + ]: 4592 : if (nsitem->p_names == nsitem->p_rte->eref ||
2650 [ + + ]: 144 : nsitem->p_returning_type != VAR_RETURNING_DEFAULT)
2651 : : {
2652 : : Var *result;
2653 : :
1721 peter@eisentraut.org 2654 : 4586 : result = makeWholeRowVar(nsitem->p_rte, nsitem->p_rtindex,
2655 : : sublevels_up, true);
2656 : :
2657 : : /* mark Var for RETURNING OLD/NEW, as necessary */
334 dean.a.rasheed@gmail 2658 : 4586 : result->varreturningtype = nsitem->p_returning_type;
2659 : :
2660 : : /* location is not filled in by makeWholeRowVar */
1721 peter@eisentraut.org 2661 : 4586 : result->location = location;
2662 : :
2663 : : /* mark Var if it's nulled by any outer joins */
1051 tgl@sss.pgh.pa.us 2664 : 4586 : markNullableIfNeeded(pstate, result);
2665 : :
2666 : : /* mark relation as requiring whole-row SELECT access */
1721 peter@eisentraut.org 2667 : 4586 : markVarForSelectPriv(pstate, result);
2668 : :
2669 : 4586 : 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 : : */
334 dean.a.rasheed@gmail 2682 : 6 : expandRTE(nsitem->p_rte, nsitem->p_rtindex, sublevels_up,
2683 : : nsitem->p_returning_type, location, false, NULL, &fields);
1721 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 *
6319 tgl@sss.pgh.pa.us 2705 : 162315 : transformTypeCast(ParseState *pstate, TypeCast *tc)
2706 : : {
2707 : : Node *result;
3526 2708 : 162315 : 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 */
5531 peter_e@gmx.net 2716 : 162315 : 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 : : */
3526 tgl@sss.pgh.pa.us 2725 [ + + ]: 162315 : 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 : : */
3950 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
3526 2749 : 5 : expr = transformExprRecurse(pstate, arg);
2750 : : }
2751 : : else
2752 : 161953 : expr = transformExprRecurse(pstate, arg);
2753 : :
3950 2754 : 162306 : inputType = exprType(expr);
9465 2755 [ - + ]: 162306 : if (inputType == InvalidOid)
9465 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 : : */
6319 tgl@sss.pgh.pa.us 2763 :CBC 162306 : location = tc->location;
2764 [ + + ]: 162306 : if (location < 0)
5997 peter_e@gmx.net 2765 : 8618 : location = tc->typeName->location;
2766 : :
6319 tgl@sss.pgh.pa.us 2767 : 162306 : result = coerce_to_target_type(pstate, expr, inputType,
2768 : : targetType, targetTypmod,
2769 : : COERCION_EXPLICIT,
2770 : : COERCE_EXPLICIT_CAST,
2771 : : location);
2772 [ + + ]: 160401 : if (result == NULL)
8186 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 : :
6319 2780 : 160390 : 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 *
5425 peter_e@gmx.net 2789 : 4755 : transformCollateClause(ParseState *pstate, CollateClause *c)
2790 : : {
2791 : : CollateExpr *newc;
2792 : : Oid argtype;
2793 : :
5394 tgl@sss.pgh.pa.us 2794 : 4755 : newc = makeNode(CollateExpr);
4876 2795 : 4755 : newc->arg = (Expr *) transformExprRecurse(pstate, c->arg);
2796 : :
5425 peter_e@gmx.net 2797 : 4755 : 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 [ + + + + ]: 4755 : 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 : :
5394 tgl@sss.pgh.pa.us 2810 : 4746 : newc->collOid = LookupCollation(pstate, c->collname, c->location);
5425 peter_e@gmx.net 2811 : 4746 : newc->location = c->location;
2812 : :
2813 : 4746 : 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 *
7293 tgl@sss.pgh.pa.us 2829 : 3443 : 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 : 3443 : nopers = list_length(largs);
2845 [ - + ]: 3443 : if (nopers != list_length(rargs))
7890 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 : : */
7293 tgl@sss.pgh.pa.us 2855 [ + + ]:CBC 3443 : if (nopers == 0)
7890 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 : : */
7293 2865 : 3440 : opexprs = NIL;
7874 neilc@samurai.com 2866 [ + - + + : 7573 : forboth(l, largs, r, rargs)
+ - + + +
+ + - +
+ ]
2867 : : {
7779 bruce@momjian.us 2868 : 4139 : Node *larg = (Node *) lfirst(l);
2869 : 4139 : Node *rarg = (Node *) lfirst(r);
2870 : : OpExpr *cmp;
2871 : :
3108 tgl@sss.pgh.pa.us 2872 : 4139 : 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 : : */
7293 2880 [ - + ]: 4133 : if (cmp->opresulttype != BOOLOID)
7293 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)));
7293 tgl@sss.pgh.pa.us 2887 [ - + ]:CBC 4133 : if (expression_returns_set((Node *) cmp))
7293 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)));
7293 tgl@sss.pgh.pa.us 2892 :CBC 4133 : 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 [ + + ]: 3434 : if (nopers == 1)
2901 : 2869 : 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 : : */
6 michael@paquier.xyz 2909 :GNC 565 : opinfo_lists = palloc_array(List *, nopers);
254 peter@eisentraut.org 2910 :CBC 565 : cmptypes = NULL;
7293 tgl@sss.pgh.pa.us 2911 : 565 : i = 0;
2912 [ + - + + : 1829 : foreach(l, opexprs)
+ + ]
2913 : : {
6933 2914 : 1264 : Oid opno = ((OpExpr *) lfirst(l))->opno;
2915 : : Bitmapset *this_cmptypes;
2916 : : ListCell *j;
2917 : :
254 peter@eisentraut.org 2918 : 1264 : 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 : 1264 : this_cmptypes = NULL;
5277 tgl@sss.pgh.pa.us 2925 [ + + + + : 2607 : foreach(j, opinfo_lists[i])
+ + ]
2926 : : {
254 peter@eisentraut.org 2927 : 1343 : OpIndexInterpretation *opinfo = lfirst(j);
2928 : :
2929 : 1343 : this_cmptypes = bms_add_member(this_cmptypes, opinfo->cmptype);
2930 : : }
7293 tgl@sss.pgh.pa.us 2931 [ + + ]: 1264 : if (i == 0)
254 peter@eisentraut.org 2932 : 565 : cmptypes = this_cmptypes;
2933 : : else
2934 : 699 : cmptypes = bms_int_members(cmptypes, this_cmptypes);
7293 tgl@sss.pgh.pa.us 2935 : 1264 : 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 : : */
254 peter@eisentraut.org 2943 : 565 : i = bms_next_member(cmptypes, -1);
6933 tgl@sss.pgh.pa.us 2944 [ + + ]: 565 : 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 : : }
335 peter@eisentraut.org 2954 : 562 : cmptype = (CompareType) i;
2955 : :
2956 : : /*
2957 : : * For = and <> cases, we just combine the pairwise operators with AND or
2958 : : * OR respectively.
2959 : : */
2960 [ + + ]: 562 : if (cmptype == COMPARE_EQ)
6319 tgl@sss.pgh.pa.us 2961 : 225 : return (Node *) makeBoolExpr(AND_EXPR, opexprs, location);
335 peter@eisentraut.org 2962 [ + + ]: 337 : if (cmptype == COMPARE_NE)
6319 tgl@sss.pgh.pa.us 2963 : 202 : 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 : : */
6933 2969 : 135 : opfamilies = NIL;
7293 2970 [ + + ]: 432 : for (i = 0; i < nopers; i++)
2971 : : {
6933 2972 : 297 : Oid opfamily = InvalidOid;
2973 : : ListCell *j;
2974 : :
5277 2975 [ + - + - : 297 : foreach(j, opinfo_lists[i])
+ - ]
2976 : : {
254 peter@eisentraut.org 2977 : 297 : OpIndexInterpretation *opinfo = lfirst(j);
2978 : :
2979 [ + - ]: 297 : if (opinfo->cmptype == cmptype)
2980 : : {
5277 tgl@sss.pgh.pa.us 2981 : 297 : opfamily = opinfo->opfamily_id;
6933 2982 : 297 : break;
2983 : : }
2984 : : }
2985 [ + - ]: 297 : if (OidIsValid(opfamily))
2986 : 297 : opfamilies = lappend_oid(opfamilies, opfamily);
2987 : : else /* should not happen */
7293 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 : : */
7293 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);
335 peter@eisentraut.org 3015 : 135 : rcexpr->cmptype = cmptype;
7293 tgl@sss.pgh.pa.us 3016 : 135 : rcexpr->opnos = opnos;
6933 3017 : 135 : rcexpr->opfamilies = opfamilies;
5364 bruce@momjian.us 3018 : 135 : rcexpr->inputcollids = NIL; /* assign_expr_collations will fix this */
7293 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 *
7890 3031 : 3 : make_row_distinct_op(ParseState *pstate, List *opname,
3032 : : RowExpr *lrow, RowExpr *rrow,
3033 : : int location)
3034 : : {
3035 : 3 : Node *result = NULL;
7323 3036 : 3 : List *largs = lrow->args;
3037 : 3 : List *rargs = rrow->args;
3038 : : ListCell *l,
3039 : : *r;
3040 : :
7870 neilc@samurai.com 3041 [ - + ]: 3 : if (list_length(largs) != list_length(rargs))
7890 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 : :
7874 neilc@samurai.com 3047 [ + - + + :CBC 12 : forboth(l, largs, r, rargs)
+ - + + +
+ + - +
+ ]
3048 : : {
7779 bruce@momjian.us 3049 : 9 : Node *larg = (Node *) lfirst(l);
3050 : 9 : Node *rarg = (Node *) lfirst(r);
3051 : : Node *cmp;
3052 : :
7217 tgl@sss.pgh.pa.us 3053 : 9 : cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg, location);
7890 3054 [ + + ]: 9 : if (result == NULL)
3055 : 3 : result = cmp;
3056 : : else
3057 : 6 : result = (Node *) makeBoolExpr(OR_EXPR,
6319 3058 : 6 : list_make2(result, cmp),
3059 : : location);
3060 : : }
3061 : :
7890 3062 [ - + ]: 3 : if (result == NULL)
3063 : : {
3064 : : /* zero-length rows? Generate constant FALSE */
7890 tgl@sss.pgh.pa.us 3065 :UBC 0 : result = makeBoolConst(false, false);
3066 : : }
3067 : :
7890 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 *
7217 3075 : 569 : make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree,
3076 : : int location)
3077 : : {
3078 : : Expr *result;
3079 : :
3108 3080 : 569 : result = make_op(pstate, opname, ltree, rtree,
3081 : : pstate->p_last_srf, location);
7890 3082 [ - + ]: 569 : if (((OpExpr *) result)->opresulttype != BOOLOID)
7890 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)));
3108 tgl@sss.pgh.pa.us 3089 [ - + ]:CBC 569 : if (((OpExpr *) result)->opretset)
3108 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 : : */
7890 tgl@sss.pgh.pa.us 3099 :CBC 569 : NodeSetTag(result, T_DistinctExpr);
3100 : :
3101 : 569 : 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 *
3428 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 *
4876 3134 : 39 : ParseExprKindName(ParseExprKind exprKind)
3135 : : {
3136 [ - - - - : 39 : switch (exprKind)
- - + - -
+ - - - -
- - - + -
+ - - + -
+ + - - -
- - - - -
- - - - -
- - ]
3137 : : {
4876 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";
4876 tgl@sss.pgh.pa.us 3150 :CBC 12 : case EXPR_KIND_WHERE:
3151 : 12 : return "WHERE";
3793 mail@joeconway.com 3152 :UBC 0 : case EXPR_KIND_POLICY:
3153 : 0 : return "POLICY";
4876 tgl@sss.pgh.pa.us 3154 : 0 : case EXPR_KIND_HAVING:
3155 : 0 : return "HAVING";
4536 noah@leadboat.com 3156 :CBC 6 : case EXPR_KIND_FILTER:
3157 : 6 : return "FILTER";
4876 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";
2869 3166 : 0 : case EXPR_KIND_WINDOW_FRAME_GROUPS:
3167 : 0 : return "window GROUPS";
4876 3168 : 0 : case EXPR_KIND_SELECT_TARGET:
3169 : 0 : return "SELECT";
3170 : 0 : case EXPR_KIND_INSERT_TARGET:
3171 : 0 : return "INSERT";
4876 tgl@sss.pgh.pa.us 3172 :CBC 3 : case EXPR_KIND_UPDATE_SOURCE:
3173 : : case EXPR_KIND_UPDATE_TARGET:
3174 : 3 : return "UPDATE";
1359 alvherre@alvh.no-ip. 3175 :UBC 0 : case EXPR_KIND_MERGE_WHEN:
3176 : 0 : return "MERGE WHEN";
4876 tgl@sss.pgh.pa.us 3177 :CBC 6 : case EXPR_KIND_GROUP_BY:
3178 : 6 : return "GROUP BY";
4876 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";
4876 tgl@sss.pgh.pa.us 3183 :CBC 3 : case EXPR_KIND_LIMIT:
3184 : 3 : return "LIMIT";
4876 tgl@sss.pgh.pa.us 3185 :UBC 0 : case EXPR_KIND_OFFSET:
3186 : 0 : return "OFFSET";
4876 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";
4876 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";
1726 tomas.vondra@postgre 3203 : 0 : case EXPR_KIND_STATS_EXPRESSION:
3204 : 0 : return "statistics expression";
4876 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";
2517 peter@eisentraut.org 3211 : 0 : case EXPR_KIND_PARTITION_BOUND:
3212 : 0 : return "partition bound";
3296 rhaas@postgresql.org 3213 : 0 : case EXPR_KIND_PARTITION_EXPRESSION:
3214 : 0 : return "PARTITION BY";
2866 tgl@sss.pgh.pa.us 3215 : 0 : case EXPR_KIND_CALL_ARGUMENT:
2938 peter_e@gmx.net 3216 : 0 : return "CALL";
2523 tomas.vondra@postgre 3217 : 0 : case EXPR_KIND_COPY_WHERE:
3218 : 0 : return "WHERE";
2453 peter@eisentraut.org 3219 : 0 : case EXPR_KIND_GENERATED_COLUMN:
3220 : 0 : return "GENERATED AS";
1779 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 : : }
4876 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 *
993 alvherre@alvh.no-ip. 3240 :CBC 96 : getJsonEncodingConst(JsonFormat *format)
3241 : : {
3242 : : JsonEncoding encoding;
3243 : : const char *enc;
6 michael@paquier.xyz 3244 :GNC 96 : Name encname = palloc_object(NameData);
3245 : :
993 alvherre@alvh.no-ip. 3246 [ + - ]:CBC 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 : : {
993 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;
993 alvherre@alvh.no-ip. 3261 :CBC 96 : case JS_ENC_UTF8:
3262 : 96 : enc = "UTF8";
3263 : 96 : break;
993 alvherre@alvh.no-ip. 3264 :UBC 0 : default:
3265 [ # # ]: 0 : elog(ERROR, "invalid JSON encoding: %d", encoding);
3266 : : break;
3267 : : }
3268 : :
993 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 *
887 amitlan@postgresql.o 3301 : 2879 : transformJsonValueExpr(ParseState *pstate, const char *constructName,
3302 : : JsonValueExpr *ve, JsonFormatType default_format,
3303 : : Oid targettype, bool isarg)
3304 : : {
993 alvherre@alvh.no-ip. 3305 : 2879 : 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 [ + + ]: 2879 : if (exprType(expr) == UNKNOWNOID)
893 amitlan@postgresql.o 3314 : 353 : expr = coerce_to_specific_type(pstate, expr, TEXTOID, constructName);
3315 : :
993 alvherre@alvh.no-ip. 3316 : 2879 : rawexpr = expr;
3317 : 2879 : exprtype = exprType(expr);
3318 : 2879 : location = exprLocation(expr);
3319 : :
3320 : 2879 : get_type_category_preferred(exprtype, &typcategory, &typispreferred);
3321 : :
3322 [ + + ]: 2879 : 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 : : }
635 amitlan@postgresql.o 3335 [ + + ]: 2759 : 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)
635 amitlan@postgresql.o 3363 :UBC 0 : return expr;
3364 : : /* else convert argument to json[b] type */
635 amitlan@postgresql.o 3365 :CBC 135 : break;
3366 : : }
3367 : :
3368 : 135 : format = default_format;
3369 : : }
993 alvherre@alvh.no-ip. 3370 [ + + + + ]: 2159 : else if (exprtype == JSONOID || exprtype == JSONBOID)
3371 : 1506 : format = JS_FORMAT_DEFAULT; /* do not format json[b] types */
3372 : : else
3373 : 653 : format = default_format;
3374 : :
880 amitlan@postgresql.o 3375 [ + + + + ]: 2401 : if (format != JS_FORMAT_DEFAULT ||
3376 [ + + ]: 1508 : (OidIsValid(targettype) && exprtype != targettype))
3377 : : {
3378 : : Node *coerced;
3379 : 394 : bool only_allow_cast = OidIsValid(targettype);
3380 : :
3381 : : /*
3382 : : * PASSING args are handled appropriately by GetJsonPathVar() /
3383 : : * JsonItemFromDatum().
3384 : : */
635 3385 [ + + ]: 394 : if (!isarg &&
3386 [ + + + + ]: 259 : !only_allow_cast &&
880 3387 [ + + ]: 95 : exprtype != BYTEAOID && typcategory != TYPCATEGORY_STRING)
993 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 [ + + + + ]: 391 : if (format == JS_FORMAT_JSON && exprtype == BYTEAOID)
3398 : : {
3399 : 36 : expr = makeJsonByteaToTextConversion(expr, ve->format, location);
3400 : 36 : exprtype = TEXTOID;
3401 : : }
3402 : :
880 amitlan@postgresql.o 3403 [ + + ]: 391 : if (!OidIsValid(targettype))
3404 [ + + ]: 254 : targettype = format == JS_FORMAT_JSONB ? JSONBOID : JSONOID;
3405 : :
3406 : : /* Try to coerce to the target type. */
993 alvherre@alvh.no-ip. 3407 : 391 : coerced = coerce_to_target_type(pstate, expr, exprtype,
3408 : : targettype, -1,
3409 : : COERCION_EXPLICIT,
3410 : : COERCE_EXPLICIT_CAST,
3411 : : location);
3412 : :
3413 [ + + ]: 391 : 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 : : */
880 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 : :
993 alvherre@alvh.no-ip. 3435 : 12 : fexpr->location = location;
3436 : :
3437 : 12 : coerced = (Node *) fexpr;
3438 : : }
3439 : :
893 amitlan@postgresql.o 3440 [ - + ]: 388 : if (coerced == expr)
993 alvherre@alvh.no-ip. 3441 :UBC 0 : expr = rawexpr;
3442 : : else
3443 : : {
993 alvherre@alvh.no-ip. 3444 :CBC 388 : ve = copyObject(ve);
3445 : 388 : ve->raw_expr = (Expr *) rawexpr;
3446 : 388 : ve->formatted_expr = (Expr *) coerced;
3447 : :
3448 : 388 : expr = (Node *) ve;
3449 : : }
3450 : : }
3451 : :
3452 : : /* If returning a JsonValueExpr, formatted_expr must have been set. */
879 amitlan@postgresql.o 3453 [ + + - + ]: 2395 : Assert(!IsA(expr, JsonValueExpr) ||
3454 : : ((JsonValueExpr *) expr)->formatted_expr != NULL);
3455 : :
993 alvherre@alvh.no-ip. 3456 : 2395 : 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)
993 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 : :
993 alvherre@alvh.no-ip. 3484 [ + - ]:CBC 122 : if (format->format_type == JS_FORMAT_JSON)
3485 : : {
3486 : 244 : JsonEncoding enc = format->encoding != JS_ENC_DEFAULT ?
942 tgl@sss.pgh.pa.us 3487 [ + + ]: 122 : format->encoding : JS_ENC_UTF8;
3488 : :
993 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)
993 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 : :
635 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 : :
993 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,
517 amitlan@postgresql.o 3647 : 125 : returning->typid, returning->typmod,
3648 : : COERCION_ASSIGNMENT,
3649 : : COERCE_IMPLICIT_CAST,
3650 : : location);
3651 : :
993 alvherre@alvh.no-ip. 3652 [ - + - - ]: 125 : if (!res && report_error)
993 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 : :
993 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 : : {
893 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 : : {
993 alvherre@alvh.no-ip. 3701 : 442 : CaseTestExpr *cte = makeNode(CaseTestExpr);
3702 : :
992 3703 : 884 : cte->typeId = returning->format->format_type == JS_FORMAT_JSONB ?
3704 [ + + ]: 442 : JSONBOID : JSONOID;
993 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);
893 amitlan@postgresql.o 3742 : 295 : Node *val = transformJsonValueExpr(pstate, "JSON_OBJECT()",
3743 : : kv->value,
3744 : : JS_FORMAT_DEFAULT,
3745 : : InvalidOid, false);
3746 : :
993 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 : :
255 tgl@sss.pgh.pa.us 3780 : 30 : query = transformStmt(qpstate, copyObject(ctor->query));
3781 : :
993 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 : : */
879 amitlan@postgresql.o 3798 : 21 : agg->arg = makeJsonValueExpr((Expr *) colref, (Expr *) colref,
3799 : : ctor->format);
993 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 : :
992 3844 : 402 : aggfilter = agg_ctor->agg_filter ? (Expr *)
3845 : 21 : transformWhereClause(pstate, agg_ctor->agg_filter,
3846 [ + + ]: 201 : EXPR_KIND_FILTER, "FILTER") : NULL;
3847 : :
993 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;
992 3857 : 24 : wfunc->aggfilter = aggfilter;
590 drowley@postgresql.o 3858 : 24 : wfunc->runCondition = NIL;
3859 : : /* winref will be set by transformWindowFuncCall */
993 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)
993 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 */
993 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;
992 3894 : 177 : aggref->aggpresorted = false;
3895 : : /* agglevelsup will be set by transformAggregateCall */
993 3896 : 177 : aggref->aggsplit = AGGSPLIT_SIMPLE; /* planner might change this */
992 3897 : 177 : aggref->aggno = -1; /* planner will set aggno and aggtransno */
3898 : 177 : aggref->aggtransno = -1;
993 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);
893 amitlan@postgresql.o 3931 : 102 : val = transformJsonValueExpr(pstate, "JSON_OBJECTAGG()",
3932 : 102 : agg->arg->value,
3933 : : JS_FORMAT_DEFAULT,
3934 : : InvalidOid, false);
993 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)
992 3944 : 6 : aggfnoid = F_JSONB_OBJECT_AGG_UNIQUE_STRICT;
3945 : : else
3946 : 3 : aggfnoid = F_JSONB_OBJECT_AGG_STRICT;
993 3947 [ + + ]: 18 : else if (agg->unique)
992 3948 : 3 : aggfnoid = F_JSONB_OBJECT_AGG_UNIQUE;
3949 : : else
3950 : 15 : aggfnoid = F_JSONB_OBJECT_AGG;
3951 : :
993 3952 : 27 : aggtype = JSONBOID;
3953 : : }
3954 : : else
3955 : : {
3956 [ + + ]: 75 : if (agg->absent_on_null)
3957 [ + + ]: 18 : if (agg->unique)
992 3958 : 9 : aggfnoid = F_JSON_OBJECT_AGG_UNIQUE_STRICT;
3959 : : else
3960 : 9 : aggfnoid = F_JSON_OBJECT_AGG_STRICT;
993 3961 [ + + ]: 57 : else if (agg->unique)
992 3962 : 24 : aggfnoid = F_JSON_OBJECT_AGG_UNIQUE;
3963 : : else
3964 : 33 : aggfnoid = F_JSON_OBJECT_AGG;
3965 : :
993 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 : :
635 amitlan@postgresql.o 3992 : 99 : arg = transformJsonValueExpr(pstate, "JSON_ARRAYAGG()", agg->arg,
3993 : : JS_FORMAT_DEFAULT, InvalidOid, false);
3994 : :
993 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 : : {
992 4000 [ + + ]: 36 : aggfnoid = agg->absent_on_null ? F_JSONB_AGG_STRICT : F_JSONB_AGG;
993 4001 : 36 : aggtype = JSONBOID;
4002 : : }
4003 : : else
4004 : : {
992 4005 [ + + ]: 63 : aggfnoid = agg->absent_on_null ? F_JSON_AGG_STRICT : F_JSON_AGG;
993 4006 : 63 : aggtype = JSONOID;
4007 : : }
4008 : :
4009 : 99 : return transformJsonAggConstructor(pstate, agg->constructor, returning,
992 4010 : 99 : list_make1(arg), aggfnoid, aggtype,
4011 : : JSCTOR_JSON_ARRAYAGG,
993 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));
893 amitlan@postgresql.o 4037 : 129 : Node *val = transformJsonValueExpr(pstate, "JSON_ARRAY()",
4038 : : jsval, JS_FORMAT_DEFAULT,
4039 : : InvalidOid, false);
4040 : :
993 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 *
991 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 : :
893 amitlan@postgresql.o 4066 : 30 : expr = raw_expr;
991 alvherre@alvh.no-ip. 4067 : 30 : expr = makeJsonByteaToTextConversion(expr, format, exprLocation(expr));
4068 : 30 : *exprtype = TEXTOID;
4069 : :
879 amitlan@postgresql.o 4070 : 30 : jve = makeJsonValueExpr((Expr *) raw_expr, (Expr *) expr, format);
991 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 : : {
14 peter@eisentraut.org 4082 :GNC 87 : expr = coerce_to_target_type(pstate, expr, *exprtype,
4083 : : TEXTOID, -1,
4084 : : COERCION_IMPLICIT,
4085 : : COERCE_IMPLICIT_CAST, -1);
991 alvherre@alvh.no-ip. 4086 :CBC 87 : *exprtype = TEXTOID;
4087 : : }
4088 : :
4089 [ - + ]: 148 : if (format->encoding != JS_ENC_DEFAULT)
991 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 : :
991 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 *
880 amitlan@postgresql.o 4126 : 123 : transformJsonReturning(ParseState *pstate, JsonOutput *output, const char *fname)
4127 : : {
4128 : : JsonReturning *returning;
4129 : :
4130 [ - + ]: 123 : if (output)
4131 : : {
880 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. */
880 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 *
635 4284 : 1573 : transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
4285 : : {
4286 : : JsonExpr *jsexpr;
4287 : : Node *path_spec;
4288 : : Oid pathspec_type;
4289 : : int pathspec_loc;
4290 : : Node *coerced_path_spec;
4291 : 1573 : const char *func_name = NULL;
4292 : : JsonFormatType default_format;
4293 : :
4294 [ + + + + : 1573 : switch (func->op)
- ]
4295 : : {
4296 : 153 : case JSON_EXISTS_OP:
4297 : 153 : func_name = "JSON_EXISTS";
4298 : 153 : default_format = JS_FORMAT_DEFAULT;
4299 : 153 : break;
4300 : 636 : case JSON_QUERY_OP:
4301 : 636 : func_name = "JSON_QUERY";
4302 : 636 : default_format = JS_FORMAT_JSONB;
4303 : 636 : break;
4304 : 548 : case JSON_VALUE_OP:
4305 : 548 : func_name = "JSON_VALUE";
4306 : 548 : default_format = JS_FORMAT_DEFAULT;
4307 : 548 : break;
621 4308 : 236 : case JSON_TABLE_OP:
4309 : 236 : func_name = "JSON_TABLE";
4310 : 236 : default_format = JS_FORMAT_JSONB;
4311 : 236 : break;
635 amitlan@postgresql.o 4312 :UBC 0 : default:
4313 [ # # ]: 0 : elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4314 : : default_format = JS_FORMAT_DEFAULT; /* keep compiler quiet */
4315 : : break;
4316 : : }
4317 : :
4318 : : /*
4319 : : * Even though the syntax allows it, FORMAT JSON specification in
4320 : : * RETURNING is meaningless except for JSON_QUERY(). Flag if not
4321 : : * JSON_QUERY().
4322 : : */
635 amitlan@postgresql.o 4323 [ + + + + ]:CBC 1573 : if (func->output && func->op != JSON_QUERY_OP)
4324 : : {
4325 : 527 : JsonFormat *format = func->output->returning->format;
4326 : :
4327 [ + + ]: 527 : if (format->format_type != JS_FORMAT_DEFAULT ||
4328 [ - + ]: 524 : format->encoding != JS_ENC_DEFAULT)
4329 [ + - ]: 3 : ereport(ERROR,
4330 : : errcode(ERRCODE_SYNTAX_ERROR),
4331 : : errmsg("cannot specify FORMAT JSON in RETURNING clause of %s()",
4332 : : func_name),
4333 : : parser_errposition(pstate, format->location));
4334 : : }
4335 : :
4336 : : /* OMIT QUOTES is meaningless when strings are wrapped. */
536 4337 [ + + ]: 1570 : if (func->op == JSON_QUERY_OP)
4338 : : {
4339 [ + + ]: 636 : if (func->quotes == JS_QUOTES_OMIT &&
4340 [ + + ]: 90 : (func->wrapper == JSW_CONDITIONAL ||
4341 [ + + ]: 87 : func->wrapper == JSW_UNCONDITIONAL))
4342 [ + - ]: 9 : ereport(ERROR,
4343 : : errcode(ERRCODE_SYNTAX_ERROR),
4344 : : errmsg("SQL/JSON QUOTES behavior must not be specified when WITH WRAPPER is used"),
4345 : : parser_errposition(pstate, func->location));
4346 [ + + ]: 627 : if (func->on_empty != NULL &&
4347 [ + + ]: 48 : func->on_empty->btype != JSON_BEHAVIOR_ERROR &&
4348 [ + + ]: 30 : func->on_empty->btype != JSON_BEHAVIOR_NULL &&
4349 [ + - ]: 27 : func->on_empty->btype != JSON_BEHAVIOR_EMPTY &&
4350 [ + + ]: 27 : func->on_empty->btype != JSON_BEHAVIOR_EMPTY_ARRAY &&
4351 [ + + ]: 15 : func->on_empty->btype != JSON_BEHAVIOR_EMPTY_OBJECT &&
4352 [ - + ]: 12 : func->on_empty->btype != JSON_BEHAVIOR_DEFAULT)
4353 : : {
536 amitlan@postgresql.o 4354 [ # # ]:UBC 0 : if (func->column_name == NULL)
4355 [ # # ]: 0 : ereport(ERROR,
4356 : : errcode(ERRCODE_SYNTAX_ERROR),
4357 : : /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4358 : : errmsg("invalid %s behavior", "ON EMPTY"),
4359 : : /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4360 : : second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4361 : : errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for %s.",
4362 : : "ON EMPTY", "JSON_QUERY()"),
4363 : : parser_errposition(pstate, func->on_empty->location));
4364 : : else
4365 [ # # ]: 0 : ereport(ERROR,
4366 : : errcode(ERRCODE_SYNTAX_ERROR),
4367 : : /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4368 : : errmsg("invalid %s behavior for column \"%s\"",
4369 : : "ON EMPTY", func->column_name),
4370 : : /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4371 : : errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for formatted columns.",
4372 : : "ON EMPTY"),
4373 : : parser_errposition(pstate, func->on_empty->location));
4374 : : }
536 amitlan@postgresql.o 4375 [ + + ]:CBC 627 : if (func->on_error != NULL &&
4376 [ + + ]: 165 : func->on_error->btype != JSON_BEHAVIOR_ERROR &&
4377 [ + + ]: 78 : func->on_error->btype != JSON_BEHAVIOR_NULL &&
4378 [ + - ]: 75 : func->on_error->btype != JSON_BEHAVIOR_EMPTY &&
4379 [ + + ]: 75 : func->on_error->btype != JSON_BEHAVIOR_EMPTY_ARRAY &&
4380 [ + + ]: 72 : func->on_error->btype != JSON_BEHAVIOR_EMPTY_OBJECT &&
4381 [ + + ]: 45 : func->on_error->btype != JSON_BEHAVIOR_DEFAULT)
4382 : : {
4383 [ + + ]: 6 : if (func->column_name == NULL)
4384 [ + - ]: 3 : ereport(ERROR,
4385 : : errcode(ERRCODE_SYNTAX_ERROR),
4386 : : /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4387 : : errmsg("invalid %s behavior", "ON ERROR"),
4388 : : /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4389 : : second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4390 : : errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for %s.",
4391 : : "ON ERROR", "JSON_QUERY()"),
4392 : : parser_errposition(pstate, func->on_error->location));
4393 : : else
4394 [ + - ]: 3 : ereport(ERROR,
4395 : : errcode(ERRCODE_SYNTAX_ERROR),
4396 : : /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4397 : : errmsg("invalid %s behavior for column \"%s\"",
4398 : : "ON ERROR", func->column_name),
4399 : : /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4400 : : errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for formatted columns.",
4401 : : "ON ERROR"),
4402 : : parser_errposition(pstate, func->on_error->location));
4403 : : }
4404 : : }
4405 : :
4406 : : /* Check that ON ERROR/EMPTY behavior values are valid for the function. */
4407 [ + + ]: 1555 : if (func->op == JSON_EXISTS_OP &&
4408 [ + + ]: 153 : func->on_error != NULL &&
4409 [ + + ]: 45 : func->on_error->btype != JSON_BEHAVIOR_ERROR &&
4410 [ + + ]: 24 : func->on_error->btype != JSON_BEHAVIOR_TRUE &&
4411 [ + + ]: 18 : func->on_error->btype != JSON_BEHAVIOR_FALSE &&
4412 [ + + ]: 12 : func->on_error->btype != JSON_BEHAVIOR_UNKNOWN)
4413 : : {
4414 [ + + ]: 6 : if (func->column_name == NULL)
4415 [ + - ]: 3 : ereport(ERROR,
4416 : : errcode(ERRCODE_SYNTAX_ERROR),
4417 : : /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4418 : : errmsg("invalid %s behavior", "ON ERROR"),
4419 : : errdetail("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in %s for %s.",
4420 : : "ON ERROR", "JSON_EXISTS()"),
4421 : : parser_errposition(pstate, func->on_error->location));
4422 : : else
4423 [ + - ]: 3 : ereport(ERROR,
4424 : : errcode(ERRCODE_SYNTAX_ERROR),
4425 : : /*- translator: first %s is name a SQL/JSON clause (eg. ON EMPTY) */
4426 : : errmsg("invalid %s behavior for column \"%s\"",
4427 : : "ON ERROR", func->column_name),
4428 : : /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4429 : : errdetail("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in %s for EXISTS columns.",
4430 : : "ON ERROR"),
4431 : : parser_errposition(pstate, func->on_error->location));
4432 : : }
4433 [ + + ]: 1549 : if (func->op == JSON_VALUE_OP)
4434 : : {
4435 [ + + ]: 545 : if (func->on_empty != NULL &&
4436 [ + + ]: 111 : func->on_empty->btype != JSON_BEHAVIOR_ERROR &&
4437 [ + + ]: 96 : func->on_empty->btype != JSON_BEHAVIOR_NULL &&
4438 [ + + ]: 93 : func->on_empty->btype != JSON_BEHAVIOR_DEFAULT)
4439 : : {
4440 [ - + ]: 3 : if (func->column_name == NULL)
536 amitlan@postgresql.o 4441 [ # # ]:UBC 0 : ereport(ERROR,
4442 : : errcode(ERRCODE_SYNTAX_ERROR),
4443 : : /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4444 : : errmsg("invalid %s behavior", "ON EMPTY"),
4445 : : /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4446 : : second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4447 : : errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for %s.",
4448 : : "ON EMPTY", "JSON_VALUE()"),
4449 : : parser_errposition(pstate, func->on_empty->location));
4450 : : else
536 amitlan@postgresql.o 4451 [ + - ]:CBC 3 : ereport(ERROR,
4452 : : errcode(ERRCODE_SYNTAX_ERROR),
4453 : : /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4454 : : errmsg("invalid %s behavior for column \"%s\"",
4455 : : "ON EMPTY", func->column_name),
4456 : : /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4457 : : errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for scalar columns.",
4458 : : "ON EMPTY"),
4459 : : parser_errposition(pstate, func->on_empty->location));
4460 : : }
4461 [ + + ]: 542 : if (func->on_error != NULL &&
4462 [ + + ]: 162 : func->on_error->btype != JSON_BEHAVIOR_ERROR &&
4463 [ + - ]: 72 : func->on_error->btype != JSON_BEHAVIOR_NULL &&
4464 [ + + ]: 72 : func->on_error->btype != JSON_BEHAVIOR_DEFAULT)
4465 : : {
4466 [ + - ]: 3 : if (func->column_name == NULL)
4467 [ + - ]: 3 : ereport(ERROR,
4468 : : errcode(ERRCODE_SYNTAX_ERROR),
4469 : : /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4470 : : errmsg("invalid %s behavior", "ON ERROR"),
4471 : : /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4472 : : second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4473 : : errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for %s.",
4474 : : "ON ERROR", "JSON_VALUE()"),
4475 : : parser_errposition(pstate, func->on_error->location));
4476 : : else
536 amitlan@postgresql.o 4477 [ # # ]:UBC 0 : ereport(ERROR,
4478 : : errcode(ERRCODE_SYNTAX_ERROR),
4479 : : /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4480 : : errmsg("invalid %s behavior for column \"%s\"",
4481 : : "ON ERROR", func->column_name),
4482 : : /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4483 : : errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for scalar columns.",
4484 : : "ON ERROR"),
4485 : : parser_errposition(pstate, func->on_error->location));
4486 : : }
4487 : : }
4488 : :
635 amitlan@postgresql.o 4489 :CBC 1543 : jsexpr = makeNode(JsonExpr);
4490 : 1543 : jsexpr->location = func->location;
4491 : 1543 : jsexpr->op = func->op;
607 4492 : 1543 : jsexpr->column_name = func->column_name;
4493 : :
4494 : : /*
4495 : : * jsonpath machinery can only handle jsonb documents, so coerce the input
4496 : : * if not already of jsonb type.
4497 : : */
635 4498 : 1543 : jsexpr->formatted_expr = transformJsonValueExpr(pstate, func_name,
4499 : : func->context_item,
4500 : : default_format,
4501 : : JSONBOID,
4502 : : false);
4503 : 1543 : jsexpr->format = func->context_item->format;
4504 : :
4505 : 1543 : path_spec = transformExprRecurse(pstate, func->pathspec);
19 4506 : 1543 : pathspec_type = exprType(path_spec);
4507 : 1543 : pathspec_loc = exprLocation(path_spec);
4508 : 1543 : coerced_path_spec = coerce_to_target_type(pstate, path_spec,
4509 : : pathspec_type,
4510 : : JSONPATHOID, -1,
4511 : : COERCION_EXPLICIT,
4512 : : COERCE_IMPLICIT_CAST,
4513 : : pathspec_loc);
4514 [ + + ]: 1543 : if (coerced_path_spec == NULL)
635 4515 [ + - ]: 6 : ereport(ERROR,
4516 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
4517 : : errmsg("JSON path expression must be of type %s, not of type %s",
4518 : : "jsonpath", format_type_be(pathspec_type)),
4519 : : parser_errposition(pstate, pathspec_loc)));
19 4520 : 1537 : jsexpr->path_spec = coerced_path_spec;
4521 : :
4522 : : /* Transform and coerce the PASSING arguments to jsonb. */
635 4523 : 1537 : transformJsonPassingArgs(pstate, func_name,
4524 : : JS_FORMAT_JSONB,
4525 : : func->passing,
4526 : : &jsexpr->passing_values,
4527 : : &jsexpr->passing_names);
4528 : :
4529 : : /* Transform the JsonOutput into JsonReturning. */
4530 : 1537 : jsexpr->returning = transformJsonOutput(pstate, func->output, false);
4531 : :
4532 [ + + + + : 1531 : switch (func->op)
- ]
4533 : : {
4534 : 147 : case JSON_EXISTS_OP:
4535 : : /* JSON_EXISTS returns boolean by default. */
4536 [ + + ]: 147 : if (!OidIsValid(jsexpr->returning->typid))
4537 : : {
4538 : 81 : jsexpr->returning->typid = BOOLOID;
4539 : 81 : jsexpr->returning->typmod = -1;
68 4540 : 81 : jsexpr->collation = InvalidOid;
4541 : : }
4542 : :
4543 : : /* JSON_TABLE() COLUMNS can specify a non-boolean type. */
621 4544 [ + + ]: 147 : if (jsexpr->returning->typid != BOOLOID)
536 4545 : 60 : jsexpr->use_json_coercion = true;
4546 : :
68 4547 : 147 : jsexpr->on_error = transformJsonBehavior(pstate,
4548 : : jsexpr,
4549 : : func->on_error,
4550 : : JSON_BEHAVIOR_FALSE,
4551 : : jsexpr->returning);
635 4552 : 147 : break;
4553 : :
4554 : 615 : case JSON_QUERY_OP:
4555 : : /* JSON_QUERY returns jsonb by default. */
4556 [ + + ]: 615 : if (!OidIsValid(jsexpr->returning->typid))
4557 : : {
4558 : 246 : JsonReturning *ret = jsexpr->returning;
4559 : :
4560 : 246 : ret->typid = JSONBOID;
4561 : 246 : ret->typmod = -1;
4562 : : }
4563 : :
68 4564 : 615 : jsexpr->collation = get_typcollation(jsexpr->returning->typid);
4565 : :
4566 : : /*
4567 : : * Keep quotes on scalar strings by default, omitting them only if
4568 : : * OMIT QUOTES is specified.
4569 : : */
635 4570 : 615 : jsexpr->omit_quotes = (func->quotes == JS_QUOTES_OMIT);
4571 : 615 : jsexpr->wrapper = func->wrapper;
4572 : :
4573 : : /*
4574 : : * Set up to coerce the result value of JsonPathValue() to the
4575 : : * RETURNING type (default or user-specified), if needed. Also if
4576 : : * OMIT QUOTES is specified.
4577 : : */
536 4578 [ + + + + ]: 615 : if (jsexpr->returning->typid != JSONBOID || jsexpr->omit_quotes)
4579 : 345 : jsexpr->use_json_coercion = true;
4580 : :
4581 : : /* Assume NULL ON EMPTY when ON EMPTY is not specified. */
68 4582 : 615 : jsexpr->on_empty = transformJsonBehavior(pstate,
4583 : : jsexpr,
4584 : : func->on_empty,
4585 : : JSON_BEHAVIOR_NULL,
4586 : : jsexpr->returning);
4587 : : /* Assume NULL ON ERROR when ON ERROR is not specified. */
4588 : 615 : jsexpr->on_error = transformJsonBehavior(pstate,
4589 : : jsexpr,
4590 : : func->on_error,
4591 : : JSON_BEHAVIOR_NULL,
4592 : : jsexpr->returning);
635 4593 : 585 : break;
4594 : :
4595 : 533 : case JSON_VALUE_OP:
4596 : : /* JSON_VALUE returns text by default. */
4597 [ + + ]: 533 : if (!OidIsValid(jsexpr->returning->typid))
4598 : : {
4599 : 87 : jsexpr->returning->typid = TEXTOID;
4600 : 87 : jsexpr->returning->typmod = -1;
4601 : : }
68 4602 : 533 : jsexpr->collation = get_typcollation(jsexpr->returning->typid);
4603 : :
4604 : : /*
4605 : : * Override whatever transformJsonOutput() set these to, which
4606 : : * assumes that output type to be jsonb.
4607 : : */
635 4608 : 533 : jsexpr->returning->format->format_type = JS_FORMAT_DEFAULT;
4609 : 533 : jsexpr->returning->format->encoding = JS_ENC_DEFAULT;
4610 : :
4611 : : /* Always omit quotes from scalar strings. */
4612 : 533 : jsexpr->omit_quotes = true;
4613 : :
4614 : : /*
4615 : : * Set up to coerce the result value of JsonPathValue() to the
4616 : : * RETURNING type (default or user-specified), if needed.
4617 : : */
536 4618 [ + + ]: 533 : if (jsexpr->returning->typid != TEXTOID)
4619 : : {
4620 [ + + + + ]: 500 : if (get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN &&
4621 : 96 : DomainHasConstraints(jsexpr->returning->typid))
4622 : 66 : jsexpr->use_json_coercion = true;
4623 : : else
4624 : 338 : jsexpr->use_io_coercion = true;
4625 : : }
4626 : :
4627 : : /* Assume NULL ON EMPTY when ON EMPTY is not specified. */
68 4628 : 533 : jsexpr->on_empty = transformJsonBehavior(pstate,
4629 : : jsexpr,
4630 : : func->on_empty,
4631 : : JSON_BEHAVIOR_NULL,
4632 : : jsexpr->returning);
4633 : : /* Assume NULL ON ERROR when ON ERROR is not specified. */
4634 : 521 : jsexpr->on_error = transformJsonBehavior(pstate,
4635 : : jsexpr,
4636 : : func->on_error,
4637 : : JSON_BEHAVIOR_NULL,
4638 : : jsexpr->returning);
635 4639 : 518 : break;
4640 : :
621 4641 : 236 : case JSON_TABLE_OP:
4642 [ + - ]: 236 : if (!OidIsValid(jsexpr->returning->typid))
4643 : : {
4644 : 236 : jsexpr->returning->typid = exprType(jsexpr->formatted_expr);
4645 : 236 : jsexpr->returning->typmod = -1;
4646 : : }
68 4647 : 236 : jsexpr->collation = get_typcollation(jsexpr->returning->typid);
4648 : :
4649 : : /*
4650 : : * Assume EMPTY ARRAY ON ERROR when ON ERROR is not specified.
4651 : : *
4652 : : * ON EMPTY cannot be specified at the top level but it can be for
4653 : : * the individual columns.
4654 : : */
4655 : 236 : jsexpr->on_error = transformJsonBehavior(pstate,
4656 : : jsexpr,
4657 : : func->on_error,
4658 : : JSON_BEHAVIOR_EMPTY_ARRAY,
4659 : : jsexpr->returning);
621 4660 : 236 : break;
4661 : :
635 amitlan@postgresql.o 4662 :UBC 0 : default:
4663 [ # # ]: 0 : elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4664 : : break;
4665 : : }
4666 : :
635 amitlan@postgresql.o 4667 :CBC 1486 : return (Node *) jsexpr;
4668 : : }
4669 : :
4670 : : /*
4671 : : * Transform a SQL/JSON PASSING clause.
4672 : : */
4673 : : static void
4674 : 1537 : transformJsonPassingArgs(ParseState *pstate, const char *constructName,
4675 : : JsonFormatType format, List *args,
4676 : : List **passing_values, List **passing_names)
4677 : : {
4678 : : ListCell *lc;
4679 : :
4680 : 1537 : *passing_values = NIL;
4681 : 1537 : *passing_names = NIL;
4682 : :
4683 [ + + + + : 2137 : foreach(lc, args)
+ + ]
4684 : : {
4685 : 600 : JsonArgument *arg = castNode(JsonArgument, lfirst(lc));
4686 : 600 : Node *expr = transformJsonValueExpr(pstate, constructName,
4687 : : arg->val, format,
4688 : : InvalidOid, true);
4689 : :
4690 : 600 : *passing_values = lappend(*passing_values, expr);
4691 : 600 : *passing_names = lappend(*passing_names, makeString(arg->name));
4692 : : }
4693 : 1537 : }
4694 : :
4695 : : /*
4696 : : * Recursively checks if the given expression, or its sub-node in some cases,
4697 : : * is valid for using as an ON ERROR / ON EMPTY DEFAULT expression.
4698 : : */
4699 : : static bool
607 4700 : 291 : ValidJsonBehaviorDefaultExpr(Node *expr, void *context)
4701 : : {
4702 [ - + ]: 291 : if (expr == NULL)
607 amitlan@postgresql.o 4703 :UBC 0 : return false;
4704 : :
607 amitlan@postgresql.o 4705 [ + + + ]:CBC 291 : switch (nodeTag(expr))
4706 : : {
4707 : : /* Acceptable expression nodes */
4708 : 186 : case T_Const:
4709 : : case T_FuncExpr:
4710 : : case T_OpExpr:
4711 : 186 : return true;
4712 : :
4713 : : /* Acceptable iff arg of the following nodes is one of the above */
4714 : 78 : case T_CoerceViaIO:
4715 : : case T_CoerceToDomain:
4716 : : case T_ArrayCoerceExpr:
4717 : : case T_ConvertRowtypeExpr:
4718 : : case T_RelabelType:
4719 : : case T_CollateExpr:
4720 : 78 : return expression_tree_walker(expr, ValidJsonBehaviorDefaultExpr,
4721 : : context);
4722 : 27 : default:
4723 : 27 : break;
4724 : : }
4725 : :
4726 : 27 : return false;
4727 : : }
4728 : :
4729 : : /*
4730 : : * Transform a JSON BEHAVIOR clause.
4731 : : */
4732 : : static JsonBehavior *
68 4733 : 2667 : transformJsonBehavior(ParseState *pstate, JsonExpr *jsexpr,
4734 : : JsonBehavior *behavior,
4735 : : JsonBehaviorType default_behavior,
4736 : : JsonReturning *returning)
4737 : : {
635 4738 : 2667 : JsonBehaviorType btype = default_behavior;
4739 : 2667 : Node *expr = NULL;
4740 : 2667 : bool coerce_at_runtime = false;
4741 : 2667 : int location = -1;
4742 : :
4743 [ + + ]: 2667 : if (behavior)
4744 : : {
4745 : 531 : btype = behavior->btype;
4746 : 531 : location = behavior->location;
4747 [ + + ]: 531 : if (btype == JSON_BEHAVIOR_DEFAULT)
4748 : : {
68 4749 : 210 : Oid targetcoll = jsexpr->collation;
4750 : : Oid exprcoll;
4751 : :
635 4752 : 210 : expr = transformExprRecurse(pstate, behavior->expr);
4753 : :
607 4754 [ + + ]: 210 : if (!ValidJsonBehaviorDefaultExpr(expr, NULL))
635 4755 [ + - ]: 24 : ereport(ERROR,
4756 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
4757 : : errmsg("can only specify a constant, non-aggregate function, or operator expression for DEFAULT"),
4758 : : parser_errposition(pstate, exprLocation(expr))));
4759 [ + + ]: 186 : if (contain_var_clause(expr))
4760 [ + - ]: 3 : ereport(ERROR,
4761 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
4762 : : errmsg("DEFAULT expression must not contain column references"),
4763 : : parser_errposition(pstate, exprLocation(expr))));
4764 [ + + ]: 183 : if (expression_returns_set(expr))
4765 [ + - ]: 3 : ereport(ERROR,
4766 : : (errcode(ERRCODE_DATATYPE_MISMATCH),
4767 : : errmsg("DEFAULT expression must not return a set"),
4768 : : parser_errposition(pstate, exprLocation(expr))));
4769 : :
4770 : : /*
4771 : : * Reject a DEFAULT expression whose collation differs from the
4772 : : * enclosing JSON expression's result collation
4773 : : * (jsexpr->collation), as chosen by the RETURNING clause.
4774 : : */
68 4775 : 180 : exprcoll = exprCollation(expr);
4776 [ + + ]: 180 : if (!OidIsValid(exprcoll))
4777 : 168 : exprcoll = get_typcollation(exprType(expr));
4778 [ + + + + : 180 : if (OidIsValid(targetcoll) && OidIsValid(exprcoll) &&
+ + ]
4779 : : targetcoll != exprcoll)
4780 [ + - ]: 12 : ereport(ERROR,
4781 : : errcode(ERRCODE_COLLATION_MISMATCH),
4782 : : errmsg("collation of DEFAULT expression conflicts with RETURNING clause"),
4783 : : errdetail("\"%s\" versus \"%s\"",
4784 : : get_collation_name(exprcoll),
4785 : : get_collation_name(targetcoll)),
4786 : : parser_errposition(pstate, exprLocation(expr)));
4787 : : }
4788 : : }
4789 : :
635 4790 [ + + + + ]: 2625 : if (expr == NULL && btype != JSON_BEHAVIOR_ERROR)
4791 : 2214 : expr = GetJsonBehaviorConst(btype, location);
4792 : :
4793 : : /*
4794 : : * Try to coerce the expression if needed.
4795 : : *
4796 : : * Use runtime coercion using json_populate_type() if the expression is
4797 : : * NULL, jsonb-valued, or boolean-valued (unless the target type is
4798 : : * integer or domain over integer, in which case use the
4799 : : * boolean-to-integer cast function).
4800 : : *
4801 : : * For other non-NULL expressions, try to find a cast and error out if one
4802 : : * is not found.
4803 : : */
504 4804 [ + + + + ]: 2625 : if (expr && exprType(expr) != returning->typid)
4805 : : {
635 4806 [ + + + + ]: 1635 : bool isnull = (IsA(expr, Const) && ((Const *) expr)->constisnull);
4807 : :
4808 [ + + + + ]: 1791 : if (isnull ||
504 4809 [ + + ]: 291 : exprType(expr) == JSONBOID ||
4810 [ + + ]: 174 : (exprType(expr) == BOOLOID &&
4811 : 39 : getBaseType(returning->typid) != INT4OID))
4812 : : {
635 4813 : 1521 : coerce_at_runtime = true;
4814 : :
4815 : : /*
4816 : : * json_populate_type() expects to be passed a jsonb value, so gin
4817 : : * up a Const containing the appropriate boolean value represented
4818 : : * as jsonb, discarding the original Const containing a plain
4819 : : * boolean.
4820 : : */
504 4821 [ + + ]: 1542 : if (exprType(expr) == BOOLOID)
4822 : : {
4823 [ - + ]: 21 : char *val = btype == JSON_BEHAVIOR_TRUE ? "true" : "false";
4824 : :
4825 : 21 : expr = (Node *) makeConst(JSONBOID, -1, InvalidOid, -1,
4826 : : DirectFunctionCall1(jsonb_in,
4827 : : CStringGetDatum(val)),
4828 : : false, false);
4829 : : }
4830 : : }
4831 : : else
4832 : : {
4833 : : Node *coerced_expr;
4834 : 114 : char typcategory = TypeCategory(returning->typid);
4835 : :
4836 : : /*
4837 : : * Use an assignment cast if coercing to a string type so that
4838 : : * build_coercion_expression() assumes implicit coercion when
4839 : : * coercing the typmod, so that inputs exceeding length cause an
4840 : : * error instead of silent truncation.
4841 : : */
4842 : : coerced_expr =
635 4843 [ + + ]: 162 : coerce_to_target_type(pstate, expr, exprType(expr),
4844 : : returning->typid, returning->typmod,
504 4845 [ + + ]: 48 : (typcategory == TYPCATEGORY_STRING ||
4846 : : typcategory == TYPCATEGORY_BITSTRING) ?
4847 : : COERCION_ASSIGNMENT :
4848 : : COERCION_EXPLICIT,
4849 : : COERCE_EXPLICIT_CAST,
4850 : : exprLocation((Node *) behavior));
4851 : :
4852 [ + + ]: 114 : if (coerced_expr == NULL)
4853 : : {
4854 : : /*
4855 : : * Provide a HINT if the expression comes from a DEFAULT
4856 : : * clause.
4857 : : */
4858 [ + - ]: 3 : if (btype == JSON_BEHAVIOR_DEFAULT)
4859 [ + - ]: 3 : ereport(ERROR,
4860 : : errcode(ERRCODE_CANNOT_COERCE),
4861 : : errmsg("cannot cast behavior expression of type %s to %s",
4862 : : format_type_be(exprType(expr)),
4863 : : format_type_be(returning->typid)),
4864 : : errhint("You will need to explicitly cast the expression to type %s.",
4865 : : format_type_be(returning->typid)),
4866 : : parser_errposition(pstate, exprLocation(expr)));
4867 : : else
504 amitlan@postgresql.o 4868 [ # # ]:UBC 0 : ereport(ERROR,
4869 : : errcode(ERRCODE_CANNOT_COERCE),
4870 : : errmsg("cannot cast behavior expression of type %s to %s",
4871 : : format_type_be(exprType(expr)),
4872 : : format_type_be(returning->typid)),
4873 : : parser_errposition(pstate, exprLocation(expr)));
4874 : : }
4875 : :
635 amitlan@postgresql.o 4876 :CBC 111 : expr = coerced_expr;
4877 : : }
4878 : : }
4879 : :
4880 [ + + ]: 2622 : if (behavior)
4881 : 486 : behavior->expr = expr;
4882 : : else
4883 : 2136 : behavior = makeJsonBehavior(btype, expr, location);
4884 : :
4885 : 2622 : behavior->coerce = coerce_at_runtime;
4886 : :
4887 : 2622 : return behavior;
4888 : : }
4889 : :
4890 : : /*
4891 : : * Returns a Const node holding the value for the given non-ERROR
4892 : : * JsonBehaviorType.
4893 : : */
4894 : : static Node *
4895 : 2214 : GetJsonBehaviorConst(JsonBehaviorType btype, int location)
4896 : : {
4897 : 2214 : Datum val = (Datum) 0;
4898 : 2214 : Oid typid = JSONBOID;
4899 : 2214 : int len = -1;
4900 : 2214 : bool isbyval = false;
4901 : 2214 : bool isnull = false;
4902 : : Const *con;
4903 : :
4904 [ + + + + : 2214 : switch (btype)
+ - - ]
4905 : : {
4906 : 239 : case JSON_BEHAVIOR_EMPTY_ARRAY:
4907 : 239 : val = DirectFunctionCall1(jsonb_in, CStringGetDatum("[]"));
4908 : 239 : break;
4909 : :
4910 : 27 : case JSON_BEHAVIOR_EMPTY_OBJECT:
4911 : 27 : val = DirectFunctionCall1(jsonb_in, CStringGetDatum("{}"));
4912 : 27 : break;
4913 : :
4914 : 6 : case JSON_BEHAVIOR_TRUE:
4915 : 6 : val = BoolGetDatum(true);
4916 : 6 : typid = BOOLOID;
4917 : 6 : len = sizeof(bool);
4918 : 6 : isbyval = true;
4919 : 6 : break;
4920 : :
4921 : 114 : case JSON_BEHAVIOR_FALSE:
4922 : 114 : val = BoolGetDatum(false);
4923 : 114 : typid = BOOLOID;
4924 : 114 : len = sizeof(bool);
4925 : 114 : isbyval = true;
4926 : 114 : break;
4927 : :
4928 : 1828 : case JSON_BEHAVIOR_NULL:
4929 : : case JSON_BEHAVIOR_UNKNOWN:
4930 : : case JSON_BEHAVIOR_EMPTY:
4931 : 1828 : val = (Datum) 0;
4932 : 1828 : isnull = true;
4933 : 1828 : typid = INT4OID;
4934 : 1828 : len = sizeof(int32);
4935 : 1828 : isbyval = true;
4936 : 1828 : break;
4937 : :
4938 : : /* These two behavior types are handled by the caller. */
635 amitlan@postgresql.o 4939 :UBC 0 : case JSON_BEHAVIOR_DEFAULT:
4940 : : case JSON_BEHAVIOR_ERROR:
4941 : 0 : Assert(false);
4942 : : break;
4943 : :
4944 : 0 : default:
4945 [ # # ]: 0 : elog(ERROR, "unrecognized SQL/JSON behavior %d", btype);
4946 : : break;
4947 : : }
4948 : :
635 amitlan@postgresql.o 4949 :CBC 2214 : con = makeConst(typid, -1, InvalidOid, len, val, isnull, isbyval);
4950 : 2214 : con->location = location;
4951 : :
4952 : 2214 : return (Node *) con;
4953 : : }
|