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