PostgreSQL Source Code  git master
parse_expr.c File Reference
#include "postgres.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/optimizer.h"
#include "parser/analyze.h"
#include "parser/parse_agg.h"
#include "parser/parse_clause.h"
#include "parser/parse_coerce.h"
#include "parser/parse_collate.h"
#include "parser/parse_expr.h"
#include "parser/parse_func.h"
#include "parser/parse_oper.h"
#include "parser/parse_relation.h"
#include "parser/parse_target.h"
#include "parser/parse_type.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/fmgroids.h"
#include "utils/jsonb.h"
#include "utils/lsyscache.h"
#include "utils/timestamp.h"
#include "utils/xml.h"
Include dependency graph for parse_expr.c:

Go to the source code of this file.

Functions

static NodetransformExprRecurse (ParseState *pstate, Node *expr)
 
static NodetransformParamRef (ParseState *pstate, ParamRef *pref)
 
static NodetransformAExprOp (ParseState *pstate, A_Expr *a)
 
static NodetransformAExprOpAny (ParseState *pstate, A_Expr *a)
 
static NodetransformAExprOpAll (ParseState *pstate, A_Expr *a)
 
static NodetransformAExprDistinct (ParseState *pstate, A_Expr *a)
 
static NodetransformAExprNullIf (ParseState *pstate, A_Expr *a)
 
static NodetransformAExprIn (ParseState *pstate, A_Expr *a)
 
static NodetransformAExprBetween (ParseState *pstate, A_Expr *a)
 
static NodetransformMergeSupportFunc (ParseState *pstate, MergeSupportFunc *f)
 
static NodetransformBoolExpr (ParseState *pstate, BoolExpr *a)
 
static NodetransformFuncCall (ParseState *pstate, FuncCall *fn)
 
static NodetransformMultiAssignRef (ParseState *pstate, MultiAssignRef *maref)
 
static NodetransformCaseExpr (ParseState *pstate, CaseExpr *c)
 
static NodetransformSubLink (ParseState *pstate, SubLink *sublink)
 
static NodetransformArrayExpr (ParseState *pstate, A_ArrayExpr *a, Oid array_type, Oid element_type, int32 typmod)
 
static NodetransformRowExpr (ParseState *pstate, RowExpr *r, bool allowDefault)
 
static NodetransformCoalesceExpr (ParseState *pstate, CoalesceExpr *c)
 
static NodetransformMinMaxExpr (ParseState *pstate, MinMaxExpr *m)
 
static NodetransformSQLValueFunction (ParseState *pstate, SQLValueFunction *svf)
 
static NodetransformXmlExpr (ParseState *pstate, XmlExpr *x)
 
static NodetransformXmlSerialize (ParseState *pstate, XmlSerialize *xs)
 
static NodetransformBooleanTest (ParseState *pstate, BooleanTest *b)
 
static NodetransformCurrentOfExpr (ParseState *pstate, CurrentOfExpr *cexpr)
 
static NodetransformColumnRef (ParseState *pstate, ColumnRef *cref)
 
static NodetransformWholeRowRef (ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location)
 
static NodetransformIndirection (ParseState *pstate, A_Indirection *ind)
 
static NodetransformTypeCast (ParseState *pstate, TypeCast *tc)
 
static NodetransformCollateClause (ParseState *pstate, CollateClause *c)
 
static NodetransformJsonObjectConstructor (ParseState *pstate, JsonObjectConstructor *ctor)
 
static NodetransformJsonArrayConstructor (ParseState *pstate, JsonArrayConstructor *ctor)
 
static NodetransformJsonArrayQueryConstructor (ParseState *pstate, JsonArrayQueryConstructor *ctor)
 
static NodetransformJsonObjectAgg (ParseState *pstate, JsonObjectAgg *agg)
 
static NodetransformJsonArrayAgg (ParseState *pstate, JsonArrayAgg *agg)
 
static NodetransformJsonIsPredicate (ParseState *pstate, JsonIsPredicate *pred)
 
static NodetransformJsonParseExpr (ParseState *pstate, JsonParseExpr *expr)
 
static NodetransformJsonScalarExpr (ParseState *pstate, JsonScalarExpr *expr)
 
static NodetransformJsonSerializeExpr (ParseState *pstate, JsonSerializeExpr *expr)
 
static NodetransformJsonFuncExpr (ParseState *pstate, JsonFuncExpr *p)
 
static void transformJsonPassingArgs (ParseState *pstate, const char *constructName, JsonFormatType format, List *args, List **passing_values, List **passing_names)
 
static void coerceJsonExprOutput (ParseState *pstate, JsonExpr *jsexpr)
 
static JsonBehaviortransformJsonBehavior (ParseState *pstate, JsonBehavior *behavior, JsonBehaviorType default_behavior, JsonReturning *returning)
 
static NodeGetJsonBehaviorConst (JsonBehaviorType btype, int location)
 
static Nodemake_row_comparison_op (ParseState *pstate, List *opname, List *largs, List *rargs, int location)
 
static Nodemake_row_distinct_op (ParseState *pstate, List *opname, RowExpr *lrow, RowExpr *rrow, int location)
 
static Exprmake_distinct_op (ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)
 
static Nodemake_nulltest_from_distinct (ParseState *pstate, A_Expr *distincta, Node *arg)
 
NodetransformExpr (ParseState *pstate, Node *expr, ParseExprKind exprKind)
 
static void unknown_attribute (ParseState *pstate, Node *relref, const char *attname, int location)
 
static bool exprIsNullConstant (Node *arg)
 
const char * ParseExprKindName (ParseExprKind exprKind)
 
static ConstgetJsonEncodingConst (JsonFormat *format)
 
static NodemakeJsonByteaToTextConversion (Node *expr, JsonFormat *format, int location)
 
static NodetransformJsonValueExpr (ParseState *pstate, const char *constructName, JsonValueExpr *ve, JsonFormatType default_format, Oid targettype, bool isarg)
 
static void checkJsonOutputFormat (ParseState *pstate, const JsonFormat *format, Oid targettype, bool allow_format_for_non_strings)
 
static JsonReturningtransformJsonOutput (ParseState *pstate, const JsonOutput *output, bool allow_format)
 
static JsonReturningtransformJsonConstructorOutput (ParseState *pstate, JsonOutput *output, List *args)
 
static NodecoerceJsonFuncExpr (ParseState *pstate, Node *expr, const JsonReturning *returning, bool report_error)
 
static NodemakeJsonConstructorExpr (ParseState *pstate, JsonConstructorType type, List *args, Expr *fexpr, JsonReturning *returning, bool unique, bool absent_on_null, int location)
 
static NodetransformJsonAggConstructor (ParseState *pstate, JsonAggConstructor *agg_ctor, JsonReturning *returning, List *args, Oid aggfnoid, Oid aggtype, JsonConstructorType ctor_type, bool unique, bool absent_on_null)
 
static NodetransformJsonParseArg (ParseState *pstate, Node *jsexpr, JsonFormat *format, Oid *exprtype)
 
static JsonReturningtransformJsonReturning (ParseState *pstate, JsonOutput *output, const char *fname)
 
static bool ValidJsonBehaviorDefaultExpr (Node *expr, void *context)
 

Variables

bool Transform_null_equals = false
 

Function Documentation

◆ checkJsonOutputFormat()

static void checkJsonOutputFormat ( ParseState pstate,
const JsonFormat format,
Oid  targettype,
bool  allow_format_for_non_strings 
)
static

Definition at line 3440 of file parse_expr.c.

3442 {
3443  if (!allow_format_for_non_strings &&
3444  format->format_type != JS_FORMAT_DEFAULT &&
3445  (targettype != BYTEAOID &&
3446  targettype != JSONOID &&
3447  targettype != JSONBOID))
3448  {
3449  char typcategory;
3450  bool typispreferred;
3451 
3452  get_type_category_preferred(targettype, &typcategory, &typispreferred);
3453 
3454  if (typcategory != TYPCATEGORY_STRING)
3455  ereport(ERROR,
3456  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3457  parser_errposition(pstate, format->location),
3458  errmsg("cannot use JSON format with non-string output types"));
3459  }
3460 
3461  if (format->format_type == JS_FORMAT_JSON)
3462  {
3463  JsonEncoding enc = format->encoding != JS_ENC_DEFAULT ?
3464  format->encoding : JS_ENC_UTF8;
3465 
3466  if (targettype != BYTEAOID &&
3467  format->encoding != JS_ENC_DEFAULT)
3468  ereport(ERROR,
3469  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3470  parser_errposition(pstate, format->location),
3471  errmsg("cannot set JSON encoding for non-bytea output types"));
3472 
3473  if (enc != JS_ENC_UTF8)
3474  ereport(ERROR,
3475  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3476  errmsg("unsupported JSON encoding"),
3477  errhint("Only UTF8 JSON encoding is supported."),
3478  parser_errposition(pstate, format->location));
3479  }
3480 }
enc
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:857
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition: lsyscache.c:2710
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:106
static char format
@ JS_FORMAT_DEFAULT
Definition: primnodes.h:1638
@ JS_FORMAT_JSON
Definition: primnodes.h:1639
JsonEncoding
Definition: primnodes.h:1625
@ JS_ENC_DEFAULT
Definition: primnodes.h:1626
@ JS_ENC_UTF8
Definition: primnodes.h:1627

References enc, ereport, errcode(), errhint(), errmsg(), ERROR, format, get_type_category_preferred(), JS_ENC_DEFAULT, JS_ENC_UTF8, JS_FORMAT_DEFAULT, JS_FORMAT_JSON, and parser_errposition().

Referenced by transformJsonOutput().

◆ coerceJsonExprOutput()

static void coerceJsonExprOutput ( ParseState pstate,
JsonExpr jsexpr 
)
static

Definition at line 4510 of file parse_expr.c.

4511 {
4512  JsonReturning *returning = jsexpr->returning;
4513  Node *context_item = jsexpr->formatted_expr;
4514  int default_typmod;
4515  Oid default_typid;
4516  bool omit_quotes =
4517  jsexpr->op == JSON_QUERY_OP && jsexpr->omit_quotes;
4518  Node *coercion_expr = NULL;
4519 
4520  Assert(returning);
4521 
4522  /*
4523  * Check for cases where the coercion should be handled at runtime, that
4524  * is, without using a cast expression.
4525  */
4526  if (jsexpr->op == JSON_VALUE_OP)
4527  {
4528  /*
4529  * Use cast expressions for types with typmod and domain types.
4530  */
4531  if (returning->typmod == -1 &&
4532  get_typtype(returning->typid) != TYPTYPE_DOMAIN)
4533  {
4534  jsexpr->use_io_coercion = true;
4535  return;
4536  }
4537  }
4538  else if (jsexpr->op == JSON_QUERY_OP)
4539  {
4540  /*
4541  * Cast functions from jsonb to the following types (jsonb_bool() et
4542  * al) don't handle errors softly, so coerce either by calling
4543  * json_populate_type() or the type's input function so that any
4544  * errors are handled appropriately. The latter only if OMIT QUOTES is
4545  * true.
4546  */
4547  switch (returning->typid)
4548  {
4549  case BOOLOID:
4550  case NUMERICOID:
4551  case INT2OID:
4552  case INT4OID:
4553  case INT8OID:
4554  case FLOAT4OID:
4555  case FLOAT8OID:
4556  if (jsexpr->omit_quotes)
4557  jsexpr->use_io_coercion = true;
4558  else
4559  jsexpr->use_json_coercion = true;
4560  return;
4561  default:
4562  break;
4563  }
4564  }
4565 
4566  /* Look up a cast expression. */
4567 
4568  /*
4569  * For JSON_VALUE() and for JSON_QUERY() when OMIT QUOTES is true,
4570  * ExecEvalJsonExprPath() will convert a quote-stripped source value to
4571  * its text representation, so use TEXTOID as the source type.
4572  */
4573  if (omit_quotes || jsexpr->op == JSON_VALUE_OP)
4574  {
4575  default_typid = TEXTOID;
4576  default_typmod = -1;
4577  }
4578  else
4579  {
4580  default_typid = exprType(context_item);
4581  default_typmod = exprTypmod(context_item);
4582  }
4583 
4584  if (returning->typid != default_typid ||
4585  returning->typmod != default_typmod)
4586  {
4587  /*
4588  * We abuse CaseTestExpr here as placeholder to pass the result of
4589  * jsonpath evaluation as input to the coercion expression.
4590  */
4591  CaseTestExpr *placeholder = makeNode(CaseTestExpr);
4592 
4593  placeholder->typeId = default_typid;
4594  placeholder->typeMod = default_typmod;
4595 
4596  coercion_expr = coerceJsonFuncExpr(pstate, (Node *) placeholder,
4597  returning, false);
4598  if (coercion_expr == (Node *) placeholder)
4599  coercion_expr = NULL;
4600  }
4601 
4602  jsexpr->coercion_expr = coercion_expr;
4603 
4604  if (coercion_expr == NULL)
4605  {
4606  /*
4607  * Either no cast was found or coercion is unnecessary but still must
4608  * convert the string value to the output type.
4609  */
4610  if (omit_quotes || jsexpr->op == JSON_VALUE_OP)
4611  jsexpr->use_io_coercion = true;
4612  else
4613  jsexpr->use_json_coercion = true;
4614  }
4615 
4616  Assert(jsexpr->coercion_expr != NULL ||
4617  (jsexpr->use_io_coercion != jsexpr->use_json_coercion));
4618 }
#define Assert(condition)
Definition: c.h:858
char get_typtype(Oid typid)
Definition: lsyscache.c:2629
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:298
#define makeNode(_type_)
Definition: nodes.h:155
static Node * coerceJsonFuncExpr(ParseState *pstate, Node *expr, const JsonReturning *returning, bool report_error)
Definition: parse_expr.c:3580
unsigned int Oid
Definition: postgres_ext.h:31
@ JSON_QUERY_OP
Definition: primnodes.h:1799
@ JSON_VALUE_OP
Definition: primnodes.h:1800
Node * formatted_expr
Definition: primnodes.h:1819
bool use_io_coercion
Definition: primnodes.h:1848
JsonReturning * returning
Definition: primnodes.h:1828
bool use_json_coercion
Definition: primnodes.h:1849
JsonExprOp op
Definition: primnodes.h:1813
bool omit_quotes
Definition: primnodes.h:1855
Node * coercion_expr
Definition: primnodes.h:1847
Definition: nodes.h:129

References Assert, coerceJsonFuncExpr(), JsonExpr::coercion_expr, exprType(), exprTypmod(), JsonExpr::formatted_expr, get_typtype(), JSON_QUERY_OP, JSON_VALUE_OP, makeNode, JsonExpr::omit_quotes, JsonExpr::op, JsonExpr::returning, CaseTestExpr::typeId, JsonReturning::typid, JsonReturning::typmod, JsonExpr::use_io_coercion, and JsonExpr::use_json_coercion.

Referenced by transformJsonFuncExpr().

◆ coerceJsonFuncExpr()

static Node* coerceJsonFuncExpr ( ParseState pstate,
Node expr,
const JsonReturning returning,
bool  report_error 
)
static

Definition at line 3580 of file parse_expr.c.

3582 {
3583  Node *res;
3584  int location;
3585  Oid exprtype = exprType(expr);
3586 
3587  /* if output type is not specified or equals to function type, return */
3588  if (!OidIsValid(returning->typid) || returning->typid == exprtype)
3589  return expr;
3590 
3591  location = exprLocation(expr);
3592 
3593  if (location < 0)
3594  location = returning->format->location;
3595 
3596  /* special case for RETURNING bytea FORMAT json */
3597  if (returning->format->format_type == JS_FORMAT_JSON &&
3598  returning->typid == BYTEAOID)
3599  {
3600  /* encode json text into bytea using pg_convert_to() */
3601  Node *texpr = coerce_to_specific_type(pstate, expr, TEXTOID,
3602  "JSON_FUNCTION");
3603  Const *enc = getJsonEncodingConst(returning->format);
3604  FuncExpr *fexpr = makeFuncExpr(F_CONVERT_TO, BYTEAOID,
3605  list_make2(texpr, enc),
3608 
3609  fexpr->location = location;
3610 
3611  return (Node *) fexpr;
3612  }
3613 
3614  /* try to coerce expression to the output type */
3615  res = coerce_to_target_type(pstate, expr, exprtype,
3616  returning->typid, returning->typmod,
3619  location);
3620 
3621  if (!res && report_error)
3622  ereport(ERROR,
3623  errcode(ERRCODE_CANNOT_COERCE),
3624  errmsg("cannot cast type %s to %s",
3625  format_type_be(exprtype),
3626  format_type_be(returning->typid)),
3627  parser_coercion_errposition(pstate, location, expr));
3628 
3629  return res;
3630 }
#define OidIsValid(objectId)
Definition: c.h:775
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
FuncExpr * makeFuncExpr(Oid funcid, Oid rettype, List *args, Oid funccollid, Oid inputcollid, CoercionForm fformat)
Definition: makefuncs.c:521
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1386
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:78
int parser_coercion_errposition(ParseState *pstate, int coerce_location, Node *input_expr)
Node * coerce_to_specific_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *constructName)
static Const * getJsonEncodingConst(JsonFormat *format)
Definition: parse_expr.c:3217
#define list_make2(x1, x2)
Definition: pg_list.h:214
#define InvalidOid
Definition: postgres_ext.h:36
@ COERCE_EXPLICIT_CAST
Definition: primnodes.h:735
@ COERCE_EXPLICIT_CALL
Definition: primnodes.h:734
@ COERCION_EXPLICIT
Definition: primnodes.h:717
ParseLoc location
Definition: primnodes.h:770
ParseLoc location
Definition: primnodes.h:1653
JsonFormatType format_type
Definition: primnodes.h:1651
JsonFormat * format
Definition: primnodes.h:1663

References COERCE_EXPLICIT_CALL, COERCE_EXPLICIT_CAST, coerce_to_specific_type(), coerce_to_target_type(), COERCION_EXPLICIT, enc, ereport, errcode(), errmsg(), ERROR, exprLocation(), exprType(), JsonReturning::format, JsonFormat::format_type, format_type_be(), getJsonEncodingConst(), InvalidOid, JS_FORMAT_JSON, list_make2, FuncExpr::location, JsonFormat::location, makeFuncExpr(), OidIsValid, parser_coercion_errposition(), res, JsonReturning::typid, and JsonReturning::typmod.

Referenced by coerceJsonExprOutput(), and makeJsonConstructorExpr().

◆ exprIsNullConstant()

static bool exprIsNullConstant ( Node arg)
static

Definition at line 911 of file parse_expr.c.

912 {
913  if (arg && IsA(arg, A_Const))
914  {
915  A_Const *con = (A_Const *) arg;
916 
917  if (con->isnull)
918  return true;
919  }
920  return false;
921 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
void * arg
bool isnull
Definition: parsenodes.h:363

References arg, IsA, and A_Const::isnull.

Referenced by transformAExprDistinct(), and transformAExprOp().

◆ GetJsonBehaviorConst()

static Node * GetJsonBehaviorConst ( JsonBehaviorType  btype,
int  location 
)
static

Definition at line 4744 of file parse_expr.c.

4745 {
4746  Datum val = (Datum) 0;
4747  Oid typid = JSONBOID;
4748  int len = -1;
4749  bool isbyval = false;
4750  bool isnull = false;
4751  Const *con;
4752 
4753  switch (btype)
4754  {
4757  break;
4758 
4761  break;
4762 
4763  case JSON_BEHAVIOR_TRUE:
4764  val = BoolGetDatum(true);
4765  typid = BOOLOID;
4766  len = sizeof(bool);
4767  isbyval = true;
4768  break;
4769 
4770  case JSON_BEHAVIOR_FALSE:
4771  val = BoolGetDatum(false);
4772  typid = BOOLOID;
4773  len = sizeof(bool);
4774  isbyval = true;
4775  break;
4776 
4777  case JSON_BEHAVIOR_NULL:
4778  case JSON_BEHAVIOR_UNKNOWN:
4779  case JSON_BEHAVIOR_EMPTY:
4780  val = (Datum) 0;
4781  isnull = true;
4782  typid = INT4OID;
4783  len = sizeof(int32);
4784  isbyval = true;
4785  break;
4786 
4787  /* These two behavior types are handled by the caller. */
4788  case JSON_BEHAVIOR_DEFAULT:
4789  case JSON_BEHAVIOR_ERROR:
4790  Assert(false);
4791  break;
4792 
4793  default:
4794  elog(ERROR, "unrecognized SQL/JSON behavior %d", btype);
4795  break;
4796  }
4797 
4798  con = makeConst(typid, -1, InvalidOid, len, val, isnull, isbyval);
4799  con->location = location;
4800 
4801  return (Node *) con;
4802 }
signed int int32
Definition: c.h:494
unsigned char bool
Definition: c.h:456
#define elog(elevel,...)
Definition: elog.h:224
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
long val
Definition: informix.c:670
Datum jsonb_in(PG_FUNCTION_ARGS)
Definition: jsonb.c:73
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:301
const void size_t len
uintptr_t Datum
Definition: postgres.h:64
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
@ JSON_BEHAVIOR_ERROR
Definition: primnodes.h:1762
@ JSON_BEHAVIOR_TRUE
Definition: primnodes.h:1764
@ JSON_BEHAVIOR_DEFAULT
Definition: primnodes.h:1769
@ JSON_BEHAVIOR_EMPTY
Definition: primnodes.h:1763
@ JSON_BEHAVIOR_FALSE
Definition: primnodes.h:1765
@ JSON_BEHAVIOR_NULL
Definition: primnodes.h:1761
@ JSON_BEHAVIOR_EMPTY_OBJECT
Definition: primnodes.h:1768
@ JSON_BEHAVIOR_UNKNOWN
Definition: primnodes.h:1766
@ JSON_BEHAVIOR_EMPTY_ARRAY
Definition: primnodes.h:1767

References Assert, BoolGetDatum(), CStringGetDatum(), DirectFunctionCall1, elog, ERROR, InvalidOid, JSON_BEHAVIOR_DEFAULT, JSON_BEHAVIOR_EMPTY, JSON_BEHAVIOR_EMPTY_ARRAY, JSON_BEHAVIOR_EMPTY_OBJECT, JSON_BEHAVIOR_ERROR, JSON_BEHAVIOR_FALSE, JSON_BEHAVIOR_NULL, JSON_BEHAVIOR_TRUE, JSON_BEHAVIOR_UNKNOWN, jsonb_in(), len, makeConst(), and val.

Referenced by transformJsonBehavior().

◆ getJsonEncodingConst()

static Const* getJsonEncodingConst ( JsonFormat format)
static

Definition at line 3217 of file parse_expr.c.

3218 {
3220  const char *enc;
3221  Name encname = palloc(sizeof(NameData));
3222 
3223  if (!format ||
3224  format->format_type == JS_FORMAT_DEFAULT ||
3225  format->encoding == JS_ENC_DEFAULT)
3227  else
3228  encoding = format->encoding;
3229 
3230  switch (encoding)
3231  {
3232  case JS_ENC_UTF16:
3233  enc = "UTF16";
3234  break;
3235  case JS_ENC_UTF32:
3236  enc = "UTF32";
3237  break;
3238  case JS_ENC_UTF8:
3239  enc = "UTF8";
3240  break;
3241  default:
3242  elog(ERROR, "invalid JSON encoding: %d", encoding);
3243  break;
3244  }
3245 
3246  namestrcpy(encname, enc);
3247 
3248  return makeConst(NAMEOID, -1, InvalidOid, NAMEDATALEN,
3249  NameGetDatum(encname), false, false);
3250 }
void * palloc(Size size)
Definition: mcxt.c:1316
void namestrcpy(Name name, const char *str)
Definition: name.c:233
#define NAMEDATALEN
int32 encoding
Definition: pg_database.h:41
static Datum NameGetDatum(const NameData *X)
Definition: postgres.h:373
@ JS_ENC_UTF32
Definition: primnodes.h:1629
@ JS_ENC_UTF16
Definition: primnodes.h:1628
Definition: c.h:741

References elog, enc, encoding, ERROR, format, InvalidOid, JS_ENC_DEFAULT, JS_ENC_UTF16, JS_ENC_UTF32, JS_ENC_UTF8, JS_FORMAT_DEFAULT, makeConst(), NAMEDATALEN, NameGetDatum(), namestrcpy(), and palloc().

Referenced by coerceJsonFuncExpr(), and makeJsonByteaToTextConversion().

◆ make_distinct_op()

static Expr * make_distinct_op ( ParseState pstate,
List opname,
Node ltree,
Node rtree,
int  location 
)
static

Definition at line 3054 of file parse_expr.c.

3056 {
3057  Expr *result;
3058 
3059  result = make_op(pstate, opname, ltree, rtree,
3060  pstate->p_last_srf, location);
3061  if (((OpExpr *) result)->opresulttype != BOOLOID)
3062  ereport(ERROR,
3063  (errcode(ERRCODE_DATATYPE_MISMATCH),
3064  errmsg("IS DISTINCT FROM requires = operator to yield boolean"),
3065  parser_errposition(pstate, location)));
3066  if (((OpExpr *) result)->opretset)
3067  ereport(ERROR,
3068  (errcode(ERRCODE_DATATYPE_MISMATCH),
3069  /* translator: %s is name of a SQL construct, eg NULLIF */
3070  errmsg("%s must not return a set", "IS DISTINCT FROM"),
3071  parser_errposition(pstate, location)));
3072 
3073  /*
3074  * We rely on DistinctExpr and OpExpr being same struct
3075  */
3076  NodeSetTag(result, T_DistinctExpr);
3077 
3078  return result;
3079 }
#define NodeSetTag(nodeptr, t)
Definition: nodes.h:156
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition: parse_oper.c:660
Node * p_last_srf
Definition: parse_node.h:229
Definition: ltree.h:43

References ereport, errcode(), errmsg(), ERROR, make_op(), NodeSetTag, ParseState::p_last_srf, and parser_errposition().

Referenced by make_row_distinct_op(), and transformAExprDistinct().

◆ make_nulltest_from_distinct()

static Node * make_nulltest_from_distinct ( ParseState pstate,
A_Expr distincta,
Node arg 
)
static

Definition at line 3087 of file parse_expr.c.

3088 {
3089  NullTest *nt = makeNode(NullTest);
3090 
3091  nt->arg = (Expr *) transformExprRecurse(pstate, arg);
3092  /* the argument can be any type, so don't coerce it */
3093  if (distincta->kind == AEXPR_NOT_DISTINCT)
3094  nt->nulltesttype = IS_NULL;
3095  else
3096  nt->nulltesttype = IS_NOT_NULL;
3097  /* argisrow = false is correct whether or not arg is composite */
3098  nt->argisrow = false;
3099  nt->location = distincta->location;
3100  return (Node *) nt;
3101 }
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition: parse_expr.c:139
@ AEXPR_NOT_DISTINCT
Definition: parsenodes.h:317
@ IS_NULL
Definition: primnodes.h:1954
@ IS_NOT_NULL
Definition: primnodes.h:1954
ParseLoc location
Definition: parsenodes.h:338
A_Expr_Kind kind
Definition: parsenodes.h:334
NullTestType nulltesttype
Definition: primnodes.h:1961
ParseLoc location
Definition: primnodes.h:1964
Expr * arg
Definition: primnodes.h:1960

References AEXPR_NOT_DISTINCT, arg, NullTest::arg, IS_NOT_NULL, IS_NULL, A_Expr::kind, A_Expr::location, NullTest::location, makeNode, NullTest::nulltesttype, and transformExprRecurse().

Referenced by transformAExprDistinct().

◆ make_row_comparison_op()

static Node * make_row_comparison_op ( ParseState pstate,
List opname,
List largs,
List rargs,
int  location 
)
static

Definition at line 2808 of file parse_expr.c.

2810 {
2811  RowCompareExpr *rcexpr;
2812  RowCompareType rctype;
2813  List *opexprs;
2814  List *opnos;
2815  List *opfamilies;
2816  ListCell *l,
2817  *r;
2818  List **opinfo_lists;
2819  Bitmapset *strats;
2820  int nopers;
2821  int i;
2822 
2823  nopers = list_length(largs);
2824  if (nopers != list_length(rargs))
2825  ereport(ERROR,
2826  (errcode(ERRCODE_SYNTAX_ERROR),
2827  errmsg("unequal number of entries in row expressions"),
2828  parser_errposition(pstate, location)));
2829 
2830  /*
2831  * We can't compare zero-length rows because there is no principled basis
2832  * for figuring out what the operator is.
2833  */
2834  if (nopers == 0)
2835  ereport(ERROR,
2836  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2837  errmsg("cannot compare rows of zero length"),
2838  parser_errposition(pstate, location)));
2839 
2840  /*
2841  * Identify all the pairwise operators, using make_op so that behavior is
2842  * the same as in the simple scalar case.
2843  */
2844  opexprs = NIL;
2845  forboth(l, largs, r, rargs)
2846  {
2847  Node *larg = (Node *) lfirst(l);
2848  Node *rarg = (Node *) lfirst(r);
2849  OpExpr *cmp;
2850 
2851  cmp = castNode(OpExpr, make_op(pstate, opname, larg, rarg,
2852  pstate->p_last_srf, location));
2853 
2854  /*
2855  * We don't use coerce_to_boolean here because we insist on the
2856  * operator yielding boolean directly, not via coercion. If it
2857  * doesn't yield bool it won't be in any index opfamilies...
2858  */
2859  if (cmp->opresulttype != BOOLOID)
2860  ereport(ERROR,
2861  (errcode(ERRCODE_DATATYPE_MISMATCH),
2862  errmsg("row comparison operator must yield type boolean, "
2863  "not type %s",
2864  format_type_be(cmp->opresulttype)),
2865  parser_errposition(pstate, location)));
2866  if (expression_returns_set((Node *) cmp))
2867  ereport(ERROR,
2868  (errcode(ERRCODE_DATATYPE_MISMATCH),
2869  errmsg("row comparison operator must not return a set"),
2870  parser_errposition(pstate, location)));
2871  opexprs = lappend(opexprs, cmp);
2872  }
2873 
2874  /*
2875  * If rows are length 1, just return the single operator. In this case we
2876  * don't insist on identifying btree semantics for the operator (but we
2877  * still require it to return boolean).
2878  */
2879  if (nopers == 1)
2880  return (Node *) linitial(opexprs);
2881 
2882  /*
2883  * Now we must determine which row comparison semantics (= <> < <= > >=)
2884  * apply to this set of operators. We look for btree opfamilies
2885  * containing the operators, and see which interpretations (strategy
2886  * numbers) exist for each operator.
2887  */
2888  opinfo_lists = (List **) palloc(nopers * sizeof(List *));
2889  strats = NULL;
2890  i = 0;
2891  foreach(l, opexprs)
2892  {
2893  Oid opno = ((OpExpr *) lfirst(l))->opno;
2894  Bitmapset *this_strats;
2895  ListCell *j;
2896 
2897  opinfo_lists[i] = get_op_btree_interpretation(opno);
2898 
2899  /*
2900  * convert strategy numbers into a Bitmapset to make the intersection
2901  * calculation easy.
2902  */
2903  this_strats = NULL;
2904  foreach(j, opinfo_lists[i])
2905  {
2906  OpBtreeInterpretation *opinfo = lfirst(j);
2907 
2908  this_strats = bms_add_member(this_strats, opinfo->strategy);
2909  }
2910  if (i == 0)
2911  strats = this_strats;
2912  else
2913  strats = bms_int_members(strats, this_strats);
2914  i++;
2915  }
2916 
2917  /*
2918  * If there are multiple common interpretations, we may use any one of
2919  * them ... this coding arbitrarily picks the lowest btree strategy
2920  * number.
2921  */
2922  i = bms_next_member(strats, -1);
2923  if (i < 0)
2924  {
2925  /* No common interpretation, so fail */
2926  ereport(ERROR,
2927  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2928  errmsg("could not determine interpretation of row comparison operator %s",
2929  strVal(llast(opname))),
2930  errhint("Row comparison operators must be associated with btree operator families."),
2931  parser_errposition(pstate, location)));
2932  }
2933  rctype = (RowCompareType) i;
2934 
2935  /*
2936  * For = and <> cases, we just combine the pairwise operators with AND or
2937  * OR respectively.
2938  */
2939  if (rctype == ROWCOMPARE_EQ)
2940  return (Node *) makeBoolExpr(AND_EXPR, opexprs, location);
2941  if (rctype == ROWCOMPARE_NE)
2942  return (Node *) makeBoolExpr(OR_EXPR, opexprs, location);
2943 
2944  /*
2945  * Otherwise we need to choose exactly which opfamily to associate with
2946  * each operator.
2947  */
2948  opfamilies = NIL;
2949  for (i = 0; i < nopers; i++)
2950  {
2951  Oid opfamily = InvalidOid;
2952  ListCell *j;
2953 
2954  foreach(j, opinfo_lists[i])
2955  {
2956  OpBtreeInterpretation *opinfo = lfirst(j);
2957 
2958  if (opinfo->strategy == rctype)
2959  {
2960  opfamily = opinfo->opfamily_id;
2961  break;
2962  }
2963  }
2964  if (OidIsValid(opfamily))
2965  opfamilies = lappend_oid(opfamilies, opfamily);
2966  else /* should not happen */
2967  ereport(ERROR,
2968  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2969  errmsg("could not determine interpretation of row comparison operator %s",
2970  strVal(llast(opname))),
2971  errdetail("There are multiple equally-plausible candidates."),
2972  parser_errposition(pstate, location)));
2973  }
2974 
2975  /*
2976  * Now deconstruct the OpExprs and create a RowCompareExpr.
2977  *
2978  * Note: can't just reuse the passed largs/rargs lists, because of
2979  * possibility that make_op inserted coercion operations.
2980  */
2981  opnos = NIL;
2982  largs = NIL;
2983  rargs = NIL;
2984  foreach(l, opexprs)
2985  {
2986  OpExpr *cmp = (OpExpr *) lfirst(l);
2987 
2988  opnos = lappend_oid(opnos, cmp->opno);
2989  largs = lappend(largs, linitial(cmp->args));
2990  rargs = lappend(rargs, lsecond(cmp->args));
2991  }
2992 
2993  rcexpr = makeNode(RowCompareExpr);
2994  rcexpr->rctype = rctype;
2995  rcexpr->opnos = opnos;
2996  rcexpr->opfamilies = opfamilies;
2997  rcexpr->inputcollids = NIL; /* assign_expr_collations will fix this */
2998  rcexpr->largs = largs;
2999  rcexpr->rargs = rargs;
3000 
3001  return (Node *) rcexpr;
3002 }
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:1109
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int j
Definition: isn.c:74
int i
Definition: isn.c:73
List * lappend(List *list, void *datum)
Definition: list.c:339
List * lappend_oid(List *list, Oid datum)
Definition: list.c:375
List * get_op_btree_interpretation(Oid opno)
Definition: lsyscache.c:601
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition: makefuncs.c:371
bool expression_returns_set(Node *clause)
Definition: nodeFuncs.c:758
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
#define lfirst(lc)
Definition: pg_list.h:172
#define llast(l)
Definition: pg_list.h:198
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define forboth(cell1, list1, cell2, list2)
Definition: pg_list.h:518
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
@ AND_EXPR
Definition: primnodes.h:931
@ OR_EXPR
Definition: primnodes.h:931
RowCompareType
Definition: primnodes.h:1453
@ ROWCOMPARE_NE
Definition: primnodes.h:1460
@ ROWCOMPARE_EQ
Definition: primnodes.h:1457
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:743
Definition: pg_list.h:54
RowCompareType rctype
Definition: primnodes.h:1468
#define strVal(v)
Definition: value.h:82

References AND_EXPR, bms_add_member(), bms_int_members(), bms_next_member(), castNode, cmp(), ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, expression_returns_set(), forboth, format_type_be(), get_op_btree_interpretation(), i, InvalidOid, j, lappend(), lappend_oid(), RowCompareExpr::largs, lfirst, linitial, list_length(), llast, lsecond, make_op(), makeBoolExpr(), makeNode, NIL, OidIsValid, OpBtreeInterpretation::opfamily_id, OR_EXPR, ParseState::p_last_srf, palloc(), parser_errposition(), RowCompareExpr::rargs, RowCompareExpr::rctype, ROWCOMPARE_EQ, ROWCOMPARE_NE, OpBtreeInterpretation::strategy, and strVal.

Referenced by transformAExprIn(), transformAExprOp(), and transformSubLink().

◆ make_row_distinct_op()

static Node * make_row_distinct_op ( ParseState pstate,
List opname,
RowExpr lrow,
RowExpr rrow,
int  location 
)
static

Definition at line 3010 of file parse_expr.c.

3013 {
3014  Node *result = NULL;
3015  List *largs = lrow->args;
3016  List *rargs = rrow->args;
3017  ListCell *l,
3018  *r;
3019 
3020  if (list_length(largs) != list_length(rargs))
3021  ereport(ERROR,
3022  (errcode(ERRCODE_SYNTAX_ERROR),
3023  errmsg("unequal number of entries in row expressions"),
3024  parser_errposition(pstate, location)));
3025 
3026  forboth(l, largs, r, rargs)
3027  {
3028  Node *larg = (Node *) lfirst(l);
3029  Node *rarg = (Node *) lfirst(r);
3030  Node *cmp;
3031 
3032  cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg, location);
3033  if (result == NULL)
3034  result = cmp;
3035  else
3036  result = (Node *) makeBoolExpr(OR_EXPR,
3037  list_make2(result, cmp),
3038  location);
3039  }
3040 
3041  if (result == NULL)
3042  {
3043  /* zero-length rows? Generate constant FALSE */
3044  result = makeBoolConst(false, false);
3045  }
3046 
3047  return result;
3048 }
Node * makeBoolConst(bool value, bool isnull)
Definition: makefuncs.c:359
static Expr * make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)
Definition: parse_expr.c:3054
List * args
Definition: primnodes.h:1411

References RowExpr::args, cmp(), ereport, errcode(), errmsg(), ERROR, forboth, lfirst, list_length(), list_make2, make_distinct_op(), makeBoolConst(), makeBoolExpr(), OR_EXPR, and parser_errposition().

Referenced by transformAExprDistinct().

◆ makeJsonByteaToTextConversion()

static Node* makeJsonByteaToTextConversion ( Node expr,
JsonFormat format,
int  location 
)
static

Definition at line 3256 of file parse_expr.c.

3257 {
3259  FuncExpr *fexpr = makeFuncExpr(F_CONVERT_FROM, TEXTOID,
3260  list_make2(expr, encoding),
3263 
3264  fexpr->location = location;
3265 
3266  return (Node *) fexpr;
3267 }

References COERCE_EXPLICIT_CALL, encoding, format, getJsonEncodingConst(), InvalidOid, list_make2, FuncExpr::location, and makeFuncExpr().

Referenced by transformJsonParseArg(), and transformJsonValueExpr().

◆ makeJsonConstructorExpr()

static Node* makeJsonConstructorExpr ( ParseState pstate,
JsonConstructorType  type,
List args,
Expr fexpr,
JsonReturning returning,
bool  unique,
bool  absent_on_null,
int  location 
)
static

Definition at line 3636 of file parse_expr.c.

3639 {
3641  Node *placeholder;
3642  Node *coercion;
3643 
3644  jsctor->args = args;
3645  jsctor->func = fexpr;
3646  jsctor->type = type;
3647  jsctor->returning = returning;
3648  jsctor->unique = unique;
3649  jsctor->absent_on_null = absent_on_null;
3650  jsctor->location = location;
3651 
3652  /*
3653  * Coerce to the RETURNING type and format, if needed. We abuse
3654  * CaseTestExpr here as placeholder to pass the result of either
3655  * evaluating 'fexpr' or whatever is produced by ExecEvalJsonConstructor()
3656  * that is of type JSON or JSONB to the coercion function.
3657  */
3658  if (fexpr)
3659  {
3661 
3662  cte->typeId = exprType((Node *) fexpr);
3663  cte->typeMod = exprTypmod((Node *) fexpr);
3664  cte->collation = exprCollation((Node *) fexpr);
3665 
3666  placeholder = (Node *) cte;
3667  }
3668  else
3669  {
3671 
3672  cte->typeId = returning->format->format_type == JS_FORMAT_JSONB ?
3673  JSONBOID : JSONOID;
3674  cte->typeMod = -1;
3675  cte->collation = InvalidOid;
3676 
3677  placeholder = (Node *) cte;
3678  }
3679 
3680  coercion = coerceJsonFuncExpr(pstate, placeholder, returning, true);
3681 
3682  if (coercion != placeholder)
3683  jsctor->coercion = (Expr *) coercion;
3684 
3685  return (Node *) jsctor;
3686 }
Oid exprCollation(const Node *expr)
Definition: nodeFuncs.c:816
@ JS_FORMAT_JSONB
Definition: primnodes.h:1640
JsonReturning * returning
Definition: primnodes.h:1706
JsonConstructorType type
Definition: primnodes.h:1702
const char * type

References JsonConstructorExpr::absent_on_null, generate_unaccent_rules::args, JsonConstructorExpr::args, coerceJsonFuncExpr(), JsonConstructorExpr::coercion, exprCollation(), exprType(), exprTypmod(), JsonReturning::format, JsonFormat::format_type, JsonConstructorExpr::func, InvalidOid, JS_FORMAT_JSONB, JsonConstructorExpr::location, makeNode, JsonConstructorExpr::returning, type, JsonConstructorExpr::type, CaseTestExpr::typeId, and JsonConstructorExpr::unique.

Referenced by transformJsonAggConstructor(), transformJsonArrayConstructor(), transformJsonObjectConstructor(), transformJsonParseExpr(), transformJsonScalarExpr(), and transformJsonSerializeExpr().

◆ ParseExprKindName()

const char* ParseExprKindName ( ParseExprKind  exprKind)

Definition at line 3111 of file parse_expr.c.

3112 {
3113  switch (exprKind)
3114  {
3115  case EXPR_KIND_NONE:
3116  return "invalid expression context";
3117  case EXPR_KIND_OTHER:
3118  return "extension expression";
3119  case EXPR_KIND_JOIN_ON:
3120  return "JOIN/ON";
3121  case EXPR_KIND_JOIN_USING:
3122  return "JOIN/USING";
3124  return "sub-SELECT in FROM";
3126  return "function in FROM";
3127  case EXPR_KIND_WHERE:
3128  return "WHERE";
3129  case EXPR_KIND_POLICY:
3130  return "POLICY";
3131  case EXPR_KIND_HAVING:
3132  return "HAVING";
3133  case EXPR_KIND_FILTER:
3134  return "FILTER";
3136  return "window PARTITION BY";
3138  return "window ORDER BY";
3140  return "window RANGE";
3142  return "window ROWS";
3144  return "window GROUPS";
3146  return "SELECT";
3148  return "INSERT";
3151  return "UPDATE";
3152  case EXPR_KIND_MERGE_WHEN:
3153  return "MERGE WHEN";
3154  case EXPR_KIND_GROUP_BY:
3155  return "GROUP BY";
3156  case EXPR_KIND_ORDER_BY:
3157  return "ORDER BY";
3158  case EXPR_KIND_DISTINCT_ON:
3159  return "DISTINCT ON";
3160  case EXPR_KIND_LIMIT:
3161  return "LIMIT";
3162  case EXPR_KIND_OFFSET:
3163  return "OFFSET";
3164  case EXPR_KIND_RETURNING:
3166  return "RETURNING";
3167  case EXPR_KIND_VALUES:
3169  return "VALUES";
3172  return "CHECK";
3175  return "DEFAULT";
3177  return "index expression";
3179  return "index predicate";
3181  return "statistics expression";
3183  return "USING";
3185  return "EXECUTE";
3187  return "WHEN";
3189  return "partition bound";
3191  return "PARTITION BY";
3193  return "CALL";
3194  case EXPR_KIND_COPY_WHERE:
3195  return "WHERE";
3197  return "GENERATED AS";
3198  case EXPR_KIND_CYCLE_MARK:
3199  return "CYCLE";
3200 
3201  /*
3202  * There is intentionally no default: case here, so that the
3203  * compiler will warn if we add a new ParseExprKind without
3204  * extending this switch. If we do see an unrecognized value at
3205  * runtime, we'll fall through to the "unrecognized" return.
3206  */
3207  }
3208  return "unrecognized expression kind";
3209 }
@ EXPR_KIND_EXECUTE_PARAMETER
Definition: parse_node.h:76
@ EXPR_KIND_DOMAIN_CHECK
Definition: parse_node.h:69
@ EXPR_KIND_COPY_WHERE
Definition: parse_node.h:82
@ EXPR_KIND_COLUMN_DEFAULT
Definition: parse_node.h:70
@ EXPR_KIND_DISTINCT_ON
Definition: parse_node.h:61
@ EXPR_KIND_MERGE_WHEN
Definition: parse_node.h:58
@ EXPR_KIND_STATS_EXPRESSION
Definition: parse_node.h:74
@ EXPR_KIND_INDEX_EXPRESSION
Definition: parse_node.h:72
@ EXPR_KIND_MERGE_RETURNING
Definition: parse_node.h:65
@ EXPR_KIND_PARTITION_BOUND
Definition: parse_node.h:79
@ EXPR_KIND_FUNCTION_DEFAULT
Definition: parse_node.h:71
@ EXPR_KIND_WINDOW_FRAME_RANGE
Definition: parse_node.h:51
@ EXPR_KIND_VALUES
Definition: parse_node.h:66
@ EXPR_KIND_FROM_SUBSELECT
Definition: parse_node.h:44
@ EXPR_KIND_POLICY
Definition: parse_node.h:78
@ EXPR_KIND_WINDOW_FRAME_GROUPS
Definition: parse_node.h:53
@ EXPR_KIND_PARTITION_EXPRESSION
Definition: parse_node.h:80
@ EXPR_KIND_JOIN_USING
Definition: parse_node.h:43
@ EXPR_KIND_INDEX_PREDICATE
Definition: parse_node.h:73
@ EXPR_KIND_ORDER_BY
Definition: parse_node.h:60
@ EXPR_KIND_OFFSET
Definition: parse_node.h:63
@ EXPR_KIND_JOIN_ON
Definition: parse_node.h:42
@ EXPR_KIND_HAVING
Definition: parse_node.h:47
@ EXPR_KIND_INSERT_TARGET
Definition: parse_node.h:55
@ EXPR_KIND_ALTER_COL_TRANSFORM
Definition: parse_node.h:75
@ EXPR_KIND_LIMIT
Definition: parse_node.h:62
@ EXPR_KIND_WHERE
Definition: parse_node.h:46
@ EXPR_KIND_UPDATE_TARGET
Definition: parse_node.h:57
@ EXPR_KIND_SELECT_TARGET
Definition: parse_node.h:54
@ EXPR_KIND_RETURNING
Definition: parse_node.h:64
@ EXPR_KIND_GENERATED_COLUMN
Definition: parse_node.h:83
@ EXPR_KIND_NONE
Definition: parse_node.h:40
@ EXPR_KIND_CALL_ARGUMENT
Definition: parse_node.h:81
@ EXPR_KIND_GROUP_BY
Definition: parse_node.h:59
@ EXPR_KIND_OTHER
Definition: parse_node.h:41
@ EXPR_KIND_FROM_FUNCTION
Definition: parse_node.h:45
@ EXPR_KIND_TRIGGER_WHEN
Definition: parse_node.h:77
@ EXPR_KIND_FILTER
Definition: parse_node.h:48
@ EXPR_KIND_UPDATE_SOURCE
Definition: parse_node.h:56
@ EXPR_KIND_CHECK_CONSTRAINT
Definition: parse_node.h:68
@ EXPR_KIND_WINDOW_PARTITION
Definition: parse_node.h:49
@ EXPR_KIND_CYCLE_MARK
Definition: parse_node.h:84
@ EXPR_KIND_WINDOW_FRAME_ROWS
Definition: parse_node.h:52
@ EXPR_KIND_WINDOW_ORDER
Definition: parse_node.h:50
@ EXPR_KIND_VALUES_SINGLE
Definition: parse_node.h:67

References EXPR_KIND_ALTER_COL_TRANSFORM, EXPR_KIND_CALL_ARGUMENT, EXPR_KIND_CHECK_CONSTRAINT, EXPR_KIND_COLUMN_DEFAULT, EXPR_KIND_COPY_WHERE, EXPR_KIND_CYCLE_MARK, EXPR_KIND_DISTINCT_ON, EXPR_KIND_DOMAIN_CHECK, EXPR_KIND_EXECUTE_PARAMETER, EXPR_KIND_FILTER, EXPR_KIND_FROM_FUNCTION, EXPR_KIND_FROM_SUBSELECT, EXPR_KIND_FUNCTION_DEFAULT, EXPR_KIND_GENERATED_COLUMN, EXPR_KIND_GROUP_BY, EXPR_KIND_HAVING, EXPR_KIND_INDEX_EXPRESSION, EXPR_KIND_INDEX_PREDICATE, EXPR_KIND_INSERT_TARGET, EXPR_KIND_JOIN_ON, EXPR_KIND_JOIN_USING, EXPR_KIND_LIMIT, EXPR_KIND_MERGE_RETURNING, EXPR_KIND_MERGE_WHEN, EXPR_KIND_NONE, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, EXPR_KIND_OTHER, EXPR_KIND_PARTITION_BOUND, EXPR_KIND_PARTITION_EXPRESSION, EXPR_KIND_POLICY, EXPR_KIND_RETURNING, EXPR_KIND_SELECT_TARGET, EXPR_KIND_STATS_EXPRESSION, EXPR_KIND_TRIGGER_WHEN, EXPR_KIND_UPDATE_SOURCE, EXPR_KIND_UPDATE_TARGET, EXPR_KIND_VALUES, EXPR_KIND_VALUES_SINGLE, EXPR_KIND_WHERE, EXPR_KIND_WINDOW_FRAME_GROUPS, EXPR_KIND_WINDOW_FRAME_RANGE, EXPR_KIND_WINDOW_FRAME_ROWS, EXPR_KIND_WINDOW_ORDER, and EXPR_KIND_WINDOW_PARTITION.

Referenced by check_agglevels_and_constraints(), check_srf_call_placement(), checkTargetlistEntrySQL92(), findTargetlistEntrySQL92(), and transformWindowFuncCall().

◆ transformAExprBetween()

static Node * transformAExprBetween ( ParseState pstate,
A_Expr a 
)
static

Definition at line 1284 of file parse_expr.c.

1285 {
1286  Node *aexpr;
1287  Node *bexpr;
1288  Node *cexpr;
1289  Node *result;
1290  Node *sub1;
1291  Node *sub2;
1292  List *args;
1293 
1294  /* Deconstruct A_Expr into three subexprs */
1295  aexpr = a->lexpr;
1296  args = castNode(List, a->rexpr);
1297  Assert(list_length(args) == 2);
1298  bexpr = (Node *) linitial(args);
1299  cexpr = (Node *) lsecond(args);
1300 
1301  /*
1302  * Build the equivalent comparison expression. Make copies of
1303  * multiply-referenced subexpressions for safety. (XXX this is really
1304  * wrong since it results in multiple runtime evaluations of what may be
1305  * volatile expressions ...)
1306  *
1307  * Ideally we would not use hard-wired operators here but instead use
1308  * opclasses. However, mixed data types and other issues make this
1309  * difficult:
1310  * http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
1311  */
1312  switch (a->kind)
1313  {
1314  case AEXPR_BETWEEN:
1316  aexpr, bexpr,
1317  a->location),
1318  makeSimpleA_Expr(AEXPR_OP, "<=",
1319  copyObject(aexpr), cexpr,
1320  a->location));
1321  result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1322  break;
1323  case AEXPR_NOT_BETWEEN:
1325  aexpr, bexpr,
1326  a->location),
1328  copyObject(aexpr), cexpr,
1329  a->location));
1330  result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1331  break;
1332  case AEXPR_BETWEEN_SYM:
1334  aexpr, bexpr,
1335  a->location),
1336  makeSimpleA_Expr(AEXPR_OP, "<=",
1337  copyObject(aexpr), cexpr,
1338  a->location));
1339  sub1 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1341  copyObject(aexpr), copyObject(cexpr),
1342  a->location),
1343  makeSimpleA_Expr(AEXPR_OP, "<=",
1344  copyObject(aexpr), copyObject(bexpr),
1345  a->location));
1346  sub2 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1347  args = list_make2(sub1, sub2);
1348  result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1349  break;
1350  case AEXPR_NOT_BETWEEN_SYM:
1352  aexpr, bexpr,
1353  a->location),
1355  copyObject(aexpr), cexpr,
1356  a->location));
1357  sub1 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1359  copyObject(aexpr), copyObject(cexpr),
1360  a->location),
1362  copyObject(aexpr), copyObject(bexpr),
1363  a->location));
1364  sub2 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1365  args = list_make2(sub1, sub2);
1366  result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1367  break;
1368  default:
1369  elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
1370  result = NULL; /* keep compiler quiet */
1371  break;
1372  }
1373 
1374  return transformExprRecurse(pstate, result);
1375 }
int a
Definition: isn.c:69
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:48
#define copyObject(obj)
Definition: nodes.h:224
@ AEXPR_BETWEEN
Definition: parsenodes.h:323
@ AEXPR_BETWEEN_SYM
Definition: parsenodes.h:325
@ AEXPR_NOT_BETWEEN_SYM
Definition: parsenodes.h:326
@ AEXPR_NOT_BETWEEN
Definition: parsenodes.h:324
@ AEXPR_OP
Definition: parsenodes.h:313

References a, AEXPR_BETWEEN, AEXPR_BETWEEN_SYM, AEXPR_NOT_BETWEEN, AEXPR_NOT_BETWEEN_SYM, AEXPR_OP, AND_EXPR, generate_unaccent_rules::args, Assert, castNode, copyObject, elog, ERROR, linitial, list_length(), list_make2, lsecond, makeBoolExpr(), makeSimpleA_Expr(), OR_EXPR, and transformExprRecurse().

Referenced by transformExprRecurse().

◆ transformAExprDistinct()

static Node * transformAExprDistinct ( ParseState pstate,
A_Expr a 
)
static

Definition at line 1033 of file parse_expr.c.

1034 {
1035  Node *lexpr = a->lexpr;
1036  Node *rexpr = a->rexpr;
1037  Node *result;
1038 
1039  /*
1040  * If either input is an undecorated NULL literal, transform to a NullTest
1041  * on the other input. That's simpler to process than a full DistinctExpr,
1042  * and it avoids needing to require that the datatype have an = operator.
1043  */
1044  if (exprIsNullConstant(rexpr))
1045  return make_nulltest_from_distinct(pstate, a, lexpr);
1046  if (exprIsNullConstant(lexpr))
1047  return make_nulltest_from_distinct(pstate, a, rexpr);
1048 
1049  lexpr = transformExprRecurse(pstate, lexpr);
1050  rexpr = transformExprRecurse(pstate, rexpr);
1051 
1052  if (lexpr && IsA(lexpr, RowExpr) &&
1053  rexpr && IsA(rexpr, RowExpr))
1054  {
1055  /* ROW() op ROW() is handled specially */
1056  result = make_row_distinct_op(pstate, a->name,
1057  (RowExpr *) lexpr,
1058  (RowExpr *) rexpr,
1059  a->location);
1060  }
1061  else
1062  {
1063  /* Ordinary scalar operator */
1064  result = (Node *) make_distinct_op(pstate,
1065  a->name,
1066  lexpr,
1067  rexpr,
1068  a->location);
1069  }
1070 
1071  /*
1072  * If it's NOT DISTINCT, we first build a DistinctExpr and then stick a
1073  * NOT on top.
1074  */
1075  if (a->kind == AEXPR_NOT_DISTINCT)
1076  result = (Node *) makeBoolExpr(NOT_EXPR,
1077  list_make1(result),
1078  a->location);
1079 
1080  return result;
1081 }
static Node * make_nulltest_from_distinct(ParseState *pstate, A_Expr *distincta, Node *arg)
Definition: parse_expr.c:3087
static bool exprIsNullConstant(Node *arg)
Definition: parse_expr.c:911
static Node * make_row_distinct_op(ParseState *pstate, List *opname, RowExpr *lrow, RowExpr *rrow, int location)
Definition: parse_expr.c:3010
#define list_make1(x1)
Definition: pg_list.h:212
@ NOT_EXPR
Definition: primnodes.h:931

References a, AEXPR_NOT_DISTINCT, exprIsNullConstant(), IsA, list_make1, make_distinct_op(), make_nulltest_from_distinct(), make_row_distinct_op(), makeBoolExpr(), NOT_EXPR, and transformExprRecurse().

Referenced by transformExprRecurse().

◆ transformAExprIn()

static Node * transformAExprIn ( ParseState pstate,
A_Expr a 
)
static

Definition at line 1126 of file parse_expr.c.

1127 {
1128  Node *result = NULL;
1129  Node *lexpr;
1130  List *rexprs;
1131  List *rvars;
1132  List *rnonvars;
1133  bool useOr;
1134  ListCell *l;
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  rvars = lappend(rvars, rexpr);
1164  else
1165  rnonvars = lappend(rnonvars, rexpr);
1166  }
1167 
1168  /*
1169  * ScalarArrayOpExpr is only going to be useful if there's more than one
1170  * non-Var righthand item.
1171  */
1172  if (list_length(rnonvars) > 1)
1173  {
1174  List *allexprs;
1175  Oid scalar_type;
1176  Oid array_type;
1177 
1178  /*
1179  * Try to select a common type for the array elements. Note that
1180  * since the LHS' type is first in the list, it will be preferred when
1181  * there is doubt (eg, when all the RHS items are unknown literals).
1182  *
1183  * Note: use list_concat here not lcons, to avoid damaging rnonvars.
1184  */
1185  allexprs = list_concat(list_make1(lexpr), rnonvars);
1186  scalar_type = select_common_type(pstate, allexprs, NULL, NULL);
1187 
1188  /* We have to verify that the selected type actually works */
1189  if (OidIsValid(scalar_type) &&
1190  !verify_common_type(scalar_type, allexprs))
1191  scalar_type = InvalidOid;
1192 
1193  /*
1194  * Do we have an array type to use? Aside from the case where there
1195  * isn't one, we don't risk using ScalarArrayOpExpr when the common
1196  * type is RECORD, because the RowExpr comparison logic below can cope
1197  * with some cases of non-identical row types.
1198  */
1199  if (OidIsValid(scalar_type) && scalar_type != RECORDOID)
1200  array_type = get_array_type(scalar_type);
1201  else
1202  array_type = InvalidOid;
1203  if (array_type != InvalidOid)
1204  {
1205  /*
1206  * OK: coerce all the right-hand non-Var inputs to the common type
1207  * and build an ArrayExpr for them.
1208  */
1209  List *aexprs;
1210  ArrayExpr *newa;
1211 
1212  aexprs = NIL;
1213  foreach(l, rnonvars)
1214  {
1215  Node *rexpr = (Node *) lfirst(l);
1216 
1217  rexpr = coerce_to_common_type(pstate, rexpr,
1218  scalar_type,
1219  "IN");
1220  aexprs = lappend(aexprs, rexpr);
1221  }
1222  newa = makeNode(ArrayExpr);
1223  newa->array_typeid = array_type;
1224  /* array_collid will be set by parse_collate.c */
1225  newa->element_typeid = scalar_type;
1226  newa->elements = aexprs;
1227  newa->multidims = false;
1228  newa->location = -1;
1229 
1230  result = (Node *) make_scalar_array_op(pstate,
1231  a->name,
1232  useOr,
1233  lexpr,
1234  (Node *) newa,
1235  a->location);
1236 
1237  /* Consider only the Vars (if any) in the loop below */
1238  rexprs = rvars;
1239  }
1240  }
1241 
1242  /*
1243  * Must do it the hard way, ie, with a boolean expression tree.
1244  */
1245  foreach(l, rexprs)
1246  {
1247  Node *rexpr = (Node *) lfirst(l);
1248  Node *cmp;
1249 
1250  if (IsA(lexpr, RowExpr) &&
1251  IsA(rexpr, RowExpr))
1252  {
1253  /* ROW() op ROW() is handled specially */
1254  cmp = make_row_comparison_op(pstate,
1255  a->name,
1256  copyObject(((RowExpr *) lexpr)->args),
1257  ((RowExpr *) rexpr)->args,
1258  a->location);
1259  }
1260  else
1261  {
1262  /* Ordinary scalar operator */
1263  cmp = (Node *) make_op(pstate,
1264  a->name,
1265  copyObject(lexpr),
1266  rexpr,
1267  pstate->p_last_srf,
1268  a->location);
1269  }
1270 
1271  cmp = coerce_to_boolean(pstate, cmp, "IN");
1272  if (result == NULL)
1273  result = cmp;
1274  else
1275  result = (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
1276  list_make2(result, cmp),
1277  a->location);
1278  }
1279 
1280  return result;
1281 }
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
Oid get_array_type(Oid typid)
Definition: lsyscache.c:2787
Node * coerce_to_common_type(ParseState *pstate, Node *node, Oid targetTypeId, const char *context)
bool verify_common_type(Oid common_type, List *exprs)
Node * coerce_to_boolean(ParseState *pstate, Node *node, const char *constructName)
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
static Node * make_row_comparison_op(ParseState *pstate, List *opname, List *largs, List *rargs, int location)
Definition: parse_expr.c:2808
Expr * make_scalar_array_op(ParseState *pstate, List *opname, bool useOr, Node *ltree, Node *rtree, int location)
Definition: parse_oper.c:770
ParseLoc location
Definition: primnodes.h:1384
List * elements
Definition: primnodes.h:1380
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:441

References a, AND_EXPR, generate_unaccent_rules::args, cmp(), coerce_to_boolean(), coerce_to_common_type(), contain_vars_of_level(), copyObject, ArrayExpr::elements, get_array_type(), InvalidOid, IsA, lappend(), lfirst, linitial, list_concat(), list_length(), list_make1, list_make2, ArrayExpr::location, make_op(), make_row_comparison_op(), make_scalar_array_op(), makeBoolExpr(), makeNode, NIL, OidIsValid, OR_EXPR, ParseState::p_last_srf, select_common_type(), strVal, transformExprRecurse(), and verify_common_type().

Referenced by transformExprRecurse().

◆ transformAExprNullIf()

static Node * transformAExprNullIf ( ParseState pstate,
A_Expr a 
)
static

Definition at line 1084 of file parse_expr.c.

1085 {
1086  Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1087  Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1088  OpExpr *result;
1089 
1090  result = (OpExpr *) make_op(pstate,
1091  a->name,
1092  lexpr,
1093  rexpr,
1094  pstate->p_last_srf,
1095  a->location);
1096 
1097  /*
1098  * The comparison operator itself should yield boolean ...
1099  */
1100  if (result->opresulttype != BOOLOID)
1101  ereport(ERROR,
1102  (errcode(ERRCODE_DATATYPE_MISMATCH),
1103  errmsg("NULLIF requires = operator to yield boolean"),
1104  parser_errposition(pstate, a->location)));
1105  if (result->opretset)
1106  ereport(ERROR,
1107  (errcode(ERRCODE_DATATYPE_MISMATCH),
1108  /* translator: %s is name of a SQL construct, eg NULLIF */
1109  errmsg("%s must not return a set", "NULLIF"),
1110  parser_errposition(pstate, a->location)));
1111 
1112  /*
1113  * ... but the NullIfExpr will yield the first operand's type.
1114  */
1115  result->opresulttype = exprType((Node *) linitial(result->args));
1116 
1117  /*
1118  * We rely on NullIfExpr and OpExpr being the same struct
1119  */
1120  NodeSetTag(result, T_NullIfExpr);
1121 
1122  return (Node *) result;
1123 }
List * args
Definition: primnodes.h:836

References a, OpExpr::args, ereport, errcode(), errmsg(), ERROR, exprType(), linitial, make_op(), NodeSetTag, ParseState::p_last_srf, parser_errposition(), and transformExprRecurse().

Referenced by transformExprRecurse().

◆ transformAExprOp()

static Node * transformAExprOp ( ParseState pstate,
A_Expr a 
)
static

Definition at line 924 of file parse_expr.c.

925 {
926  Node *lexpr = a->lexpr;
927  Node *rexpr = a->rexpr;
928  Node *result;
929 
930  /*
931  * Special-case "foo = NULL" and "NULL = foo" for compatibility with
932  * standards-broken products (like Microsoft's). Turn these into IS NULL
933  * exprs. (If either side is a CaseTestExpr, then the expression was
934  * generated internally from a CASE-WHEN expression, and
935  * transform_null_equals does not apply.)
936  */
937  if (Transform_null_equals &&
938  list_length(a->name) == 1 &&
939  strcmp(strVal(linitial(a->name)), "=") == 0 &&
940  (exprIsNullConstant(lexpr) || exprIsNullConstant(rexpr)) &&
941  (!IsA(lexpr, CaseTestExpr) && !IsA(rexpr, CaseTestExpr)))
942  {
943  NullTest *n = makeNode(NullTest);
944 
945  n->nulltesttype = IS_NULL;
946  n->location = a->location;
947 
948  if (exprIsNullConstant(lexpr))
949  n->arg = (Expr *) rexpr;
950  else
951  n->arg = (Expr *) lexpr;
952 
953  result = transformExprRecurse(pstate, (Node *) n);
954  }
955  else if (lexpr && IsA(lexpr, RowExpr) &&
956  rexpr && IsA(rexpr, SubLink) &&
957  ((SubLink *) rexpr)->subLinkType == EXPR_SUBLINK)
958  {
959  /*
960  * Convert "row op subselect" into a ROWCOMPARE sublink. Formerly the
961  * grammar did this, but now that a row construct is allowed anywhere
962  * in expressions, it's easier to do it here.
963  */
964  SubLink *s = (SubLink *) rexpr;
965 
967  s->testexpr = lexpr;
968  s->operName = a->name;
969  s->location = a->location;
970  result = transformExprRecurse(pstate, (Node *) s);
971  }
972  else if (lexpr && IsA(lexpr, RowExpr) &&
973  rexpr && IsA(rexpr, RowExpr))
974  {
975  /* ROW() op ROW() is handled specially */
976  lexpr = transformExprRecurse(pstate, lexpr);
977  rexpr = transformExprRecurse(pstate, rexpr);
978 
979  result = make_row_comparison_op(pstate,
980  a->name,
981  castNode(RowExpr, lexpr)->args,
982  castNode(RowExpr, rexpr)->args,
983  a->location);
984  }
985  else
986  {
987  /* Ordinary scalar operator */
988  Node *last_srf = pstate->p_last_srf;
989 
990  lexpr = transformExprRecurse(pstate, lexpr);
991  rexpr = transformExprRecurse(pstate, rexpr);
992 
993  result = (Node *) make_op(pstate,
994  a->name,
995  lexpr,
996  rexpr,
997  last_srf,
998  a->location);
999  }
1000 
1001  return result;
1002 }
bool Transform_null_equals
Definition: parse_expr.c:46
@ EXPR_SUBLINK
Definition: primnodes.h:1001
@ ROWCOMPARE_SUBLINK
Definition: primnodes.h:1000

References a, NullTest::arg, generate_unaccent_rules::args, castNode, EXPR_SUBLINK, exprIsNullConstant(), IS_NULL, IsA, linitial, list_length(), SubLink::location, NullTest::location, make_op(), make_row_comparison_op(), makeNode, NullTest::nulltesttype, ParseState::p_last_srf, ROWCOMPARE_SUBLINK, strVal, SubLink::subLinkType, SubLink::testexpr, Transform_null_equals, and transformExprRecurse().

Referenced by transformExprRecurse().

◆ transformAExprOpAll()

static Node * transformAExprOpAll ( ParseState pstate,
A_Expr a 
)
static

Definition at line 1019 of file parse_expr.c.

1020 {
1021  Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1022  Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1023 
1024  return (Node *) make_scalar_array_op(pstate,
1025  a->name,
1026  false,
1027  lexpr,
1028  rexpr,
1029  a->location);
1030 }

References a, make_scalar_array_op(), and transformExprRecurse().

Referenced by transformExprRecurse().

◆ transformAExprOpAny()

static Node * transformAExprOpAny ( ParseState pstate,
A_Expr a 
)
static

Definition at line 1005 of file parse_expr.c.

1006 {
1007  Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1008  Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1009 
1010  return (Node *) make_scalar_array_op(pstate,
1011  a->name,
1012  true,
1013  lexpr,
1014  rexpr,
1015  a->location);
1016 }

References a, make_scalar_array_op(), and transformExprRecurse().

Referenced by transformExprRecurse().

◆ transformArrayExpr()

static Node * transformArrayExpr ( ParseState pstate,
A_ArrayExpr a,
Oid  array_type,
Oid  element_type,
int32  typmod 
)
static

Definition at line 2015 of file parse_expr.c.

2017 {
2018  ArrayExpr *newa = makeNode(ArrayExpr);
2019  List *newelems = NIL;
2020  List *newcoercedelems = NIL;
2021  ListCell *element;
2022  Oid coerce_type;
2023  bool coerce_hard;
2024 
2025  /*
2026  * Transform the element expressions
2027  *
2028  * Assume that the array is one-dimensional unless we find an array-type
2029  * element expression.
2030  */
2031  newa->multidims = false;
2032  foreach(element, a->elements)
2033  {
2034  Node *e = (Node *) lfirst(element);
2035  Node *newe;
2036 
2037  /*
2038  * If an element is itself an A_ArrayExpr, recurse directly so that we
2039  * can pass down any target type we were given.
2040  */
2041  if (IsA(e, A_ArrayExpr))
2042  {
2043  newe = transformArrayExpr(pstate,
2044  (A_ArrayExpr *) e,
2045  array_type,
2046  element_type,
2047  typmod);
2048  /* we certainly have an array here */
2049  Assert(array_type == InvalidOid || array_type == exprType(newe));
2050  newa->multidims = true;
2051  }
2052  else
2053  {
2054  newe = transformExprRecurse(pstate, e);
2055 
2056  /*
2057  * Check for sub-array expressions, if we haven't already found
2058  * one.
2059  */
2060  if (!newa->multidims && type_is_array(exprType(newe)))
2061  newa->multidims = true;
2062  }
2063 
2064  newelems = lappend(newelems, newe);
2065  }
2066 
2067  /*
2068  * Select a target type for the elements.
2069  *
2070  * If we haven't been given a target array type, we must try to deduce a
2071  * common type based on the types of the individual elements present.
2072  */
2073  if (OidIsValid(array_type))
2074  {
2075  /* Caller must ensure array_type matches element_type */
2076  Assert(OidIsValid(element_type));
2077  coerce_type = (newa->multidims ? array_type : element_type);
2078  coerce_hard = true;
2079  }
2080  else
2081  {
2082  /* Can't handle an empty array without a target type */
2083  if (newelems == NIL)
2084  ereport(ERROR,
2085  (errcode(ERRCODE_INDETERMINATE_DATATYPE),
2086  errmsg("cannot determine type of empty array"),
2087  errhint("Explicitly cast to the desired type, "
2088  "for example ARRAY[]::integer[]."),
2089  parser_errposition(pstate, a->location)));
2090 
2091  /* Select a common type for the elements */
2092  coerce_type = select_common_type(pstate, newelems, "ARRAY", NULL);
2093 
2094  if (newa->multidims)
2095  {
2096  array_type = coerce_type;
2097  element_type = get_element_type(array_type);
2098  if (!OidIsValid(element_type))
2099  ereport(ERROR,
2100  (errcode(ERRCODE_UNDEFINED_OBJECT),
2101  errmsg("could not find element type for data type %s",
2102  format_type_be(array_type)),
2103  parser_errposition(pstate, a->location)));
2104  }
2105  else
2106  {
2107  element_type = coerce_type;
2108  array_type = get_array_type(element_type);
2109  if (!OidIsValid(array_type))
2110  ereport(ERROR,
2111  (errcode(ERRCODE_UNDEFINED_OBJECT),
2112  errmsg("could not find array type for data type %s",
2113  format_type_be(element_type)),
2114  parser_errposition(pstate, a->location)));
2115  }
2116  coerce_hard = false;
2117  }
2118 
2119  /*
2120  * Coerce elements to target type
2121  *
2122  * If the array has been explicitly cast, then the elements are in turn
2123  * explicitly coerced.
2124  *
2125  * If the array's type was merely derived from the common type of its
2126  * elements, then the elements are implicitly coerced to the common type.
2127  * This is consistent with other uses of select_common_type().
2128  */
2129  foreach(element, newelems)
2130  {
2131  Node *e = (Node *) lfirst(element);
2132  Node *newe;
2133 
2134  if (coerce_hard)
2135  {
2136  newe = coerce_to_target_type(pstate, e,
2137  exprType(e),
2138  coerce_type,
2139  typmod,
2142  -1);
2143  if (newe == NULL)
2144  ereport(ERROR,
2145  (errcode(ERRCODE_CANNOT_COERCE),
2146  errmsg("cannot cast type %s to %s",
2149  parser_errposition(pstate, exprLocation(e))));
2150  }
2151  else
2152  newe = coerce_to_common_type(pstate, e,
2153  coerce_type,
2154  "ARRAY");
2155  newcoercedelems = lappend(newcoercedelems, newe);
2156  }
2157 
2158  newa->array_typeid = array_type;
2159  /* array_collid will be set by parse_collate.c */
2160  newa->element_typeid = element_type;
2161  newa->elements = newcoercedelems;
2162  newa->location = a->location;
2163 
2164  return (Node *) newa;
2165 }
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2759
#define type_is_array(typid)
Definition: lsyscache.h:209
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:157
static Node * transformArrayExpr(ParseState *pstate, A_ArrayExpr *a, Oid array_type, Oid element_type, int32 typmod)
Definition: parse_expr.c:2015
e
Definition: preproc-init.c:82
static chr element(struct vars *v, const chr *startp, const chr *endp)
Definition: regc_locale.c:376

References a, Assert, COERCE_EXPLICIT_CAST, coerce_to_common_type(), coerce_to_target_type(), coerce_type(), COERCION_EXPLICIT, element(), ArrayExpr::elements, ereport, errcode(), errhint(), errmsg(), ERROR, exprLocation(), exprType(), format_type_be(), get_array_type(), get_element_type(), InvalidOid, IsA, lappend(), lfirst, ArrayExpr::location, makeNode, NIL, OidIsValid, parser_errposition(), select_common_type(), transformExprRecurse(), and type_is_array.

Referenced by transformExprRecurse(), and transformTypeCast().

◆ transformBooleanTest()

static Node * transformBooleanTest ( ParseState pstate,
BooleanTest b 
)
static

Definition at line 2520 of file parse_expr.c.

2521 {
2522  const char *clausename;
2523 
2524  switch (b->booltesttype)
2525  {
2526  case IS_TRUE:
2527  clausename = "IS TRUE";
2528  break;
2529  case IS_NOT_TRUE:
2530  clausename = "IS NOT TRUE";
2531  break;
2532  case IS_FALSE:
2533  clausename = "IS FALSE";
2534  break;
2535  case IS_NOT_FALSE:
2536  clausename = "IS NOT FALSE";
2537  break;
2538  case IS_UNKNOWN:
2539  clausename = "IS UNKNOWN";
2540  break;
2541  case IS_NOT_UNKNOWN:
2542  clausename = "IS NOT UNKNOWN";
2543  break;
2544  default:
2545  elog(ERROR, "unrecognized booltesttype: %d",
2546  (int) b->booltesttype);
2547  clausename = NULL; /* keep compiler quiet */
2548  }
2549 
2550  b->arg = (Expr *) transformExprRecurse(pstate, (Node *) b->arg);
2551 
2552  b->arg = (Expr *) coerce_to_boolean(pstate,
2553  (Node *) b->arg,
2554  clausename);
2555 
2556  return (Node *) b;
2557 }
int b
Definition: isn.c:70
@ IS_NOT_TRUE
Definition: primnodes.h:1978
@ IS_NOT_FALSE
Definition: primnodes.h:1978
@ IS_NOT_UNKNOWN
Definition: primnodes.h:1978
@ IS_TRUE
Definition: primnodes.h:1978
@ IS_UNKNOWN
Definition: primnodes.h:1978
@ IS_FALSE
Definition: primnodes.h:1978

References b, coerce_to_boolean(), elog, ERROR, IS_FALSE, IS_NOT_FALSE, IS_NOT_TRUE, IS_NOT_UNKNOWN, IS_TRUE, IS_UNKNOWN, and transformExprRecurse().

Referenced by transformExprRecurse().

◆ transformBoolExpr()

static Node * transformBoolExpr ( ParseState pstate,
BoolExpr a 
)
static

Definition at line 1403 of file parse_expr.c.

1404 {
1405  List *args = NIL;
1406  const char *opname;
1407  ListCell *lc;
1408 
1409  switch (a->boolop)
1410  {
1411  case AND_EXPR:
1412  opname = "AND";
1413  break;
1414  case OR_EXPR:
1415  opname = "OR";
1416  break;
1417  case NOT_EXPR:
1418  opname = "NOT";
1419  break;
1420  default:
1421  elog(ERROR, "unrecognized boolop: %d", (int) a->boolop);
1422  opname = NULL; /* keep compiler quiet */
1423  break;
1424  }
1425 
1426  foreach(lc, a->args)
1427  {
1428  Node *arg = (Node *) lfirst(lc);
1429 
1430  arg = transformExprRecurse(pstate, arg);
1431  arg = coerce_to_boolean(pstate, arg, opname);
1432  args = lappend(args, arg);
1433  }
1434 
1435  return (Node *) makeBoolExpr(a->boolop, args, a->location);
1436 }

References a, AND_EXPR, arg, generate_unaccent_rules::args, coerce_to_boolean(), elog, ERROR, lappend(), lfirst, makeBoolExpr(), NIL, NOT_EXPR, OR_EXPR, and transformExprRecurse().

Referenced by transformExprRecurse().

◆ transformCaseExpr()

static Node * transformCaseExpr ( ParseState pstate,
CaseExpr c 
)
static

Definition at line 1632 of file parse_expr.c.

1633 {
1634  CaseExpr *newc = makeNode(CaseExpr);
1635  Node *last_srf = pstate->p_last_srf;
1636  Node *arg;
1637  CaseTestExpr *placeholder;
1638  List *newargs;
1639  List *resultexprs;
1640  ListCell *l;
1641  Node *defresult;
1642  Oid ptype;
1643 
1644  /* transform the test expression, if any */
1645  arg = transformExprRecurse(pstate, (Node *) c->arg);
1646 
1647  /* generate placeholder for test expression */
1648  if (arg)
1649  {
1650  /*
1651  * If test expression is an untyped literal, force it to text. We have
1652  * to do something now because we won't be able to do this coercion on
1653  * the placeholder. This is not as flexible as what was done in 7.4
1654  * and before, but it's good enough to handle the sort of silly coding
1655  * commonly seen.
1656  */
1657  if (exprType(arg) == UNKNOWNOID)
1658  arg = coerce_to_common_type(pstate, arg, TEXTOID, "CASE");
1659 
1660  /*
1661  * Run collation assignment on the test expression so that we know
1662  * what collation to mark the placeholder with. In principle we could
1663  * leave it to parse_collate.c to do that later, but propagating the
1664  * result to the CaseTestExpr would be unnecessarily complicated.
1665  */
1666  assign_expr_collations(pstate, arg);
1667 
1668  placeholder = makeNode(CaseTestExpr);
1669  placeholder->typeId = exprType(arg);
1670  placeholder->typeMod = exprTypmod(arg);
1671  placeholder->collation = exprCollation(arg);
1672  }
1673  else
1674  placeholder = NULL;
1675 
1676  newc->arg = (Expr *) arg;
1677 
1678  /* transform the list of arguments */
1679  newargs = NIL;
1680  resultexprs = NIL;
1681  foreach(l, c->args)
1682  {
1683  CaseWhen *w = lfirst_node(CaseWhen, l);
1684  CaseWhen *neww = makeNode(CaseWhen);
1685  Node *warg;
1686 
1687  warg = (Node *) w->expr;
1688  if (placeholder)
1689  {
1690  /* shorthand form was specified, so expand... */
1691  warg = (Node *) makeSimpleA_Expr(AEXPR_OP, "=",
1692  (Node *) placeholder,
1693  warg,
1694  w->location);
1695  }
1696  neww->expr = (Expr *) transformExprRecurse(pstate, warg);
1697 
1698  neww->expr = (Expr *) coerce_to_boolean(pstate,
1699  (Node *) neww->expr,
1700  "CASE/WHEN");
1701 
1702  warg = (Node *) w->result;
1703  neww->result = (Expr *) transformExprRecurse(pstate, warg);
1704  neww->location = w->location;
1705 
1706  newargs = lappend(newargs, neww);
1707  resultexprs = lappend(resultexprs, neww->result);
1708  }
1709 
1710  newc->args = newargs;
1711 
1712  /* transform the default clause */
1713  defresult = (Node *) c->defresult;
1714  if (defresult == NULL)
1715  {
1716  A_Const *n = makeNode(A_Const);
1717 
1718  n->isnull = true;
1719  n->location = -1;
1720  defresult = (Node *) n;
1721  }
1722  newc->defresult = (Expr *) transformExprRecurse(pstate, defresult);
1723 
1724  /*
1725  * Note: default result is considered the most significant type in
1726  * determining preferred type. This is how the code worked before, but it
1727  * seems a little bogus to me --- tgl
1728  */
1729  resultexprs = lcons(newc->defresult, resultexprs);
1730 
1731  ptype = select_common_type(pstate, resultexprs, "CASE", NULL);
1732  Assert(OidIsValid(ptype));
1733  newc->casetype = ptype;
1734  /* casecollid will be set by parse_collate.c */
1735 
1736  /* Convert default result clause, if necessary */
1737  newc->defresult = (Expr *)
1738  coerce_to_common_type(pstate,
1739  (Node *) newc->defresult,
1740  ptype,
1741  "CASE/ELSE");
1742 
1743  /* Convert when-clause results, if necessary */
1744  foreach(l, newc->args)
1745  {
1746  CaseWhen *w = (CaseWhen *) lfirst(l);
1747 
1748  w->result = (Expr *)
1749  coerce_to_common_type(pstate,
1750  (Node *) w->result,
1751  ptype,
1752  "CASE/WHEN");
1753  }
1754 
1755  /* if any subexpression contained a SRF, complain */
1756  if (pstate->p_last_srf != last_srf)
1757  ereport(ERROR,
1758  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1759  /* translator: %s is name of a SQL construct, eg GROUP BY */
1760  errmsg("set-returning functions are not allowed in %s",
1761  "CASE"),
1762  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
1763  parser_errposition(pstate,
1764  exprLocation(pstate->p_last_srf))));
1765 
1766  newc->location = c->location;
1767 
1768  return (Node *) newc;
1769 }
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
List * lcons(void *datum, List *list)
Definition: list.c:495
void assign_expr_collations(ParseState *pstate, Node *expr)
#define lfirst_node(type, lc)
Definition: pg_list.h:176
char * c
ParseLoc location
Definition: parsenodes.h:364
Expr * arg
Definition: primnodes.h:1313
ParseLoc location
Definition: primnodes.h:1316
Expr * defresult
Definition: primnodes.h:1315
List * args
Definition: primnodes.h:1314
Expr * result
Definition: primnodes.h:1326
Expr * expr
Definition: primnodes.h:1325
ParseLoc location
Definition: primnodes.h:1327

References AEXPR_OP, arg, CaseExpr::arg, CaseExpr::args, Assert, assign_expr_collations(), coerce_to_boolean(), coerce_to_common_type(), CaseExpr::defresult, ereport, errcode(), errhint(), errmsg(), ERROR, CaseWhen::expr, exprCollation(), exprLocation(), exprType(), exprTypmod(), if(), A_Const::isnull, lappend(), lcons(), lfirst, lfirst_node, A_Const::location, CaseExpr::location, CaseWhen::location, makeNode, makeSimpleA_Expr(), NIL, OidIsValid, ParseState::p_last_srf, parser_errposition(), CaseWhen::result, select_common_type(), transformExprRecurse(), and CaseTestExpr::typeId.

Referenced by transformExprRecurse().

◆ transformCoalesceExpr()

static Node * transformCoalesceExpr ( ParseState pstate,
CoalesceExpr c 
)
static

Definition at line 2206 of file parse_expr.c.

2207 {
2209  Node *last_srf = pstate->p_last_srf;
2210  List *newargs = NIL;
2211  List *newcoercedargs = NIL;
2212  ListCell *args;
2213 
2214  foreach(args, c->args)
2215  {
2216  Node *e = (Node *) lfirst(args);
2217  Node *newe;
2218 
2219  newe = transformExprRecurse(pstate, e);
2220  newargs = lappend(newargs, newe);
2221  }
2222 
2223  newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
2224  /* coalescecollid will be set by parse_collate.c */
2225 
2226  /* Convert arguments if necessary */
2227  foreach(args, newargs)
2228  {
2229  Node *e = (Node *) lfirst(args);
2230  Node *newe;
2231 
2232  newe = coerce_to_common_type(pstate, e,
2233  newc->coalescetype,
2234  "COALESCE");
2235  newcoercedargs = lappend(newcoercedargs, newe);
2236  }
2237 
2238  /* if any subexpression contained a SRF, complain */
2239  if (pstate->p_last_srf != last_srf)
2240  ereport(ERROR,
2241  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2242  /* translator: %s is name of a SQL construct, eg GROUP BY */
2243  errmsg("set-returning functions are not allowed in %s",
2244  "COALESCE"),
2245  errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
2246  parser_errposition(pstate,
2247  exprLocation(pstate->p_last_srf))));
2248 
2249  newc->args = newcoercedargs;
2250  newc->location = c->location;
2251  return (Node *) newc;
2252 }
List * args
Definition: primnodes.h:1492
ParseLoc location
Definition: primnodes.h:1494

References generate_unaccent_rules::args, CoalesceExpr::args, coerce_to_common_type(), ereport, errcode(), errhint(), errmsg(), ERROR, exprLocation(), lappend(), lfirst, CoalesceExpr::location, makeNode, NIL, ParseState::p_last_srf, parser_errposition(), select_common_type(), and transformExprRecurse().

Referenced by transformExprRecurse().

◆ transformCollateClause()

static Node * transformCollateClause ( ParseState pstate,
CollateClause c 
)
static

Definition at line 2768 of file parse_expr.c.

2769 {
2770  CollateExpr *newc;
2771  Oid argtype;
2772 
2773  newc = makeNode(CollateExpr);
2774  newc->arg = (Expr *) transformExprRecurse(pstate, c->arg);
2775 
2776  argtype = exprType((Node *) newc->arg);
2777 
2778  /*
2779  * The unknown type is not collatable, but coerce_type() takes care of it
2780  * separately, so we'll let it go here.
2781  */
2782  if (!type_is_collatable(argtype) && argtype != UNKNOWNOID)
2783  ereport(ERROR,
2784  (errcode(ERRCODE_DATATYPE_MISMATCH),
2785  errmsg("collations are not supported by type %s",
2786  format_type_be(argtype)),
2787  parser_errposition(pstate, c->location)));
2788 
2789  newc->collOid = LookupCollation(pstate, c->collname, c->location);
2790  newc->location = c->location;
2791 
2792  return (Node *) newc;
2793 }
bool type_is_collatable(Oid typid)
Definition: lsyscache.c:3081
Oid LookupCollation(ParseState *pstate, List *collnames, int location)
Definition: parse_type.c:515
Expr * arg
Definition: primnodes.h:1279
ParseLoc location
Definition: primnodes.h:1281

References CollateExpr::arg, CollateExpr::collOid, ereport, errcode(), errmsg(), ERROR, exprType(), format_type_be(), CollateExpr::location, LookupCollation(), makeNode, parser_errposition(), transformExprRecurse(), and type_is_collatable().

Referenced by transformExprRecurse().

◆ transformColumnRef()

static Node * transformColumnRef ( ParseState pstate,
ColumnRef cref 
)
static

Definition at line 511 of file parse_expr.c.

512 {
513  Node *node = NULL;
514  char *nspname = NULL;
515  char *relname = NULL;
516  char *colname = NULL;
517  ParseNamespaceItem *nsitem;
518  int levels_up;
519  enum
520  {
521  CRERR_NO_COLUMN,
522  CRERR_NO_RTE,
523  CRERR_WRONG_DB,
524  CRERR_TOO_MANY
525  } crerr = CRERR_NO_COLUMN;
526  const char *err;
527 
528  /*
529  * Check to see if the column reference is in an invalid place within the
530  * query. We allow column references in most places, except in default
531  * expressions and partition bound expressions.
532  */
533  err = NULL;
534  switch (pstate->p_expr_kind)
535  {
536  case EXPR_KIND_NONE:
537  Assert(false); /* can't happen */
538  break;
539  case EXPR_KIND_OTHER:
540  case EXPR_KIND_JOIN_ON:
544  case EXPR_KIND_WHERE:
545  case EXPR_KIND_POLICY:
546  case EXPR_KIND_HAVING:
547  case EXPR_KIND_FILTER:
558  case EXPR_KIND_GROUP_BY:
559  case EXPR_KIND_ORDER_BY:
561  case EXPR_KIND_LIMIT:
562  case EXPR_KIND_OFFSET:
563  case EXPR_KIND_RETURNING:
565  case EXPR_KIND_VALUES:
581  /* okay */
582  break;
583 
585  err = _("cannot use column reference in DEFAULT expression");
586  break;
588  err = _("cannot use column reference in partition bound expression");
589  break;
590 
591  /*
592  * There is intentionally no default: case here, so that the
593  * compiler will warn if we add a new ParseExprKind without
594  * extending this switch. If we do see an unrecognized value at
595  * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
596  * which is sane anyway.
597  */
598  }
599  if (err)
600  ereport(ERROR,
601  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
602  errmsg_internal("%s", err),
603  parser_errposition(pstate, cref->location)));
604 
605  /*
606  * Give the PreParseColumnRefHook, if any, first shot. If it returns
607  * non-null then that's all, folks.
608  */
609  if (pstate->p_pre_columnref_hook != NULL)
610  {
611  node = pstate->p_pre_columnref_hook(pstate, cref);
612  if (node != NULL)
613  return node;
614  }
615 
616  /*----------
617  * The allowed syntaxes are:
618  *
619  * A First try to resolve as unqualified column name;
620  * if no luck, try to resolve as unqualified table name (A.*).
621  * A.B A is an unqualified table name; B is either a
622  * column or function name (trying column name first).
623  * A.B.C schema A, table B, col or func name C.
624  * A.B.C.D catalog A, schema B, table C, col or func D.
625  * A.* A is an unqualified table name; means whole-row value.
626  * A.B.* whole-row value of table B in schema A.
627  * A.B.C.* whole-row value of table C in schema B in catalog A.
628  *
629  * We do not need to cope with bare "*"; that will only be accepted by
630  * the grammar at the top level of a SELECT list, and transformTargetList
631  * will take care of it before it ever gets here. Also, "A.*" etc will
632  * be expanded by transformTargetList if they appear at SELECT top level,
633  * so here we are only going to see them as function or operator inputs.
634  *
635  * Currently, if a catalog name is given then it must equal the current
636  * database name; we check it here and then discard it.
637  *----------
638  */
639  switch (list_length(cref->fields))
640  {
641  case 1:
642  {
643  Node *field1 = (Node *) linitial(cref->fields);
644 
645  colname = strVal(field1);
646 
647  /* Try to identify as an unqualified column */
648  node = colNameToVar(pstate, colname, false, cref->location);
649 
650  if (node == NULL)
651  {
652  /*
653  * Not known as a column of any range-table entry.
654  *
655  * Try to find the name as a relation. Note that only
656  * relations already entered into the rangetable will be
657  * recognized.
658  *
659  * This is a hack for backwards compatibility with
660  * PostQUEL-inspired syntax. The preferred form now is
661  * "rel.*".
662  */
663  nsitem = refnameNamespaceItem(pstate, NULL, colname,
664  cref->location,
665  &levels_up);
666  if (nsitem)
667  node = transformWholeRowRef(pstate, nsitem, levels_up,
668  cref->location);
669  }
670  break;
671  }
672  case 2:
673  {
674  Node *field1 = (Node *) linitial(cref->fields);
675  Node *field2 = (Node *) lsecond(cref->fields);
676 
677  relname = strVal(field1);
678 
679  /* Locate the referenced nsitem */
680  nsitem = refnameNamespaceItem(pstate, nspname, relname,
681  cref->location,
682  &levels_up);
683  if (nsitem == NULL)
684  {
685  crerr = CRERR_NO_RTE;
686  break;
687  }
688 
689  /* Whole-row reference? */
690  if (IsA(field2, A_Star))
691  {
692  node = transformWholeRowRef(pstate, nsitem, levels_up,
693  cref->location);
694  break;
695  }
696 
697  colname = strVal(field2);
698 
699  /* Try to identify as a column of the nsitem */
700  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
701  cref->location);
702  if (node == NULL)
703  {
704  /* Try it as a function call on the whole row */
705  node = transformWholeRowRef(pstate, nsitem, levels_up,
706  cref->location);
707  node = ParseFuncOrColumn(pstate,
708  list_make1(makeString(colname)),
709  list_make1(node),
710  pstate->p_last_srf,
711  NULL,
712  false,
713  cref->location);
714  }
715  break;
716  }
717  case 3:
718  {
719  Node *field1 = (Node *) linitial(cref->fields);
720  Node *field2 = (Node *) lsecond(cref->fields);
721  Node *field3 = (Node *) lthird(cref->fields);
722 
723  nspname = strVal(field1);
724  relname = strVal(field2);
725 
726  /* Locate the referenced nsitem */
727  nsitem = refnameNamespaceItem(pstate, nspname, relname,
728  cref->location,
729  &levels_up);
730  if (nsitem == NULL)
731  {
732  crerr = CRERR_NO_RTE;
733  break;
734  }
735 
736  /* Whole-row reference? */
737  if (IsA(field3, A_Star))
738  {
739  node = transformWholeRowRef(pstate, nsitem, levels_up,
740  cref->location);
741  break;
742  }
743 
744  colname = strVal(field3);
745 
746  /* Try to identify as a column of the nsitem */
747  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
748  cref->location);
749  if (node == NULL)
750  {
751  /* Try it as a function call on the whole row */
752  node = transformWholeRowRef(pstate, nsitem, levels_up,
753  cref->location);
754  node = ParseFuncOrColumn(pstate,
755  list_make1(makeString(colname)),
756  list_make1(node),
757  pstate->p_last_srf,
758  NULL,
759  false,
760  cref->location);
761  }
762  break;
763  }
764  case 4:
765  {
766  Node *field1 = (Node *) linitial(cref->fields);
767  Node *field2 = (Node *) lsecond(cref->fields);
768  Node *field3 = (Node *) lthird(cref->fields);
769  Node *field4 = (Node *) lfourth(cref->fields);
770  char *catname;
771 
772  catname = strVal(field1);
773  nspname = strVal(field2);
774  relname = strVal(field3);
775 
776  /*
777  * We check the catalog name and then ignore it.
778  */
779  if (strcmp(catname, get_database_name(MyDatabaseId)) != 0)
780  {
781  crerr = CRERR_WRONG_DB;
782  break;
783  }
784 
785  /* Locate the referenced nsitem */
786  nsitem = refnameNamespaceItem(pstate, nspname, relname,
787  cref->location,
788  &levels_up);
789  if (nsitem == NULL)
790  {
791  crerr = CRERR_NO_RTE;
792  break;
793  }
794 
795  /* Whole-row reference? */
796  if (IsA(field4, A_Star))
797  {
798  node = transformWholeRowRef(pstate, nsitem, levels_up,
799  cref->location);
800  break;
801  }
802 
803  colname = strVal(field4);
804 
805  /* Try to identify as a column of the nsitem */
806  node = scanNSItemForColumn(pstate, nsitem, levels_up, colname,
807  cref->location);
808  if (node == NULL)
809  {
810  /* Try it as a function call on the whole row */
811  node = transformWholeRowRef(pstate, nsitem, levels_up,
812  cref->location);
813  node = ParseFuncOrColumn(pstate,
814  list_make1(makeString(colname)),
815  list_make1(node),
816  pstate->p_last_srf,
817  NULL,
818  false,
819  cref->location);
820  }
821  break;
822  }
823  default:
824  crerr = CRERR_TOO_MANY; /* too many dotted names */
825  break;
826  }
827 
828  /*
829  * Now give the PostParseColumnRefHook, if any, a chance. We pass the
830  * translation-so-far so that it can throw an error if it wishes in the
831  * case that it has a conflicting interpretation of the ColumnRef. (If it
832  * just translates anyway, we'll throw an error, because we can't undo
833  * whatever effects the preceding steps may have had on the pstate.) If it
834  * returns NULL, use the standard translation, or throw a suitable error
835  * if there is none.
836  */
837  if (pstate->p_post_columnref_hook != NULL)
838  {
839  Node *hookresult;
840 
841  hookresult = pstate->p_post_columnref_hook(pstate, cref, node);
842  if (node == NULL)
843  node = hookresult;
844  else if (hookresult != NULL)
845  ereport(ERROR,
846  (errcode(ERRCODE_AMBIGUOUS_COLUMN),
847  errmsg("column reference \"%s\" is ambiguous",
848  NameListToString(cref->fields)),
849  parser_errposition(pstate, cref->location)));
850  }
851 
852  /*
853  * Throw error if no translation found.
854  */
855  if (node == NULL)
856  {
857  switch (crerr)
858  {
859  case CRERR_NO_COLUMN:
860  errorMissingColumn(pstate, relname, colname, cref->location);
861  break;
862  case CRERR_NO_RTE:
863  errorMissingRTE(pstate, makeRangeVar(nspname, relname,
864  cref->location));
865  break;
866  case CRERR_WRONG_DB:
867  ereport(ERROR,
868  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
869  errmsg("cross-database references are not implemented: %s",
870  NameListToString(cref->fields)),
871  parser_errposition(pstate, cref->location)));
872  break;
873  case CRERR_TOO_MANY:
874  ereport(ERROR,
875  (errcode(ERRCODE_SYNTAX_ERROR),
876  errmsg("improper qualified name (too many dotted names): %s",
877  NameListToString(cref->fields)),
878  parser_errposition(pstate, cref->location)));
879  break;
880  }
881  }
882 
883  return node;
884 }
char * get_database_name(Oid dbid)
Definition: dbcommands.c:3154
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
#define _(x)
Definition: elog.c:90
void err(int eval, const char *fmt,...)
Definition: err.c:43
Oid MyDatabaseId
Definition: globals.c:91
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:424
char * NameListToString(const List *names)
Definition: namespace.c:3579
static Node * transformWholeRowRef(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, int location)
Definition: parse_expr.c:2612
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, Node *last_srf, FuncCall *fn, bool proc_call, int location)
Definition: parse_func.c:90
void errorMissingColumn(ParseState *pstate, const char *relname, const char *colname, int location)
void errorMissingRTE(ParseState *pstate, RangeVar *relation)
Node * scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem, int sublevels_up, const char *colname, int location)
ParseNamespaceItem * refnameNamespaceItem(ParseState *pstate, const char *schemaname, const char *refname, int location, int *sublevels_up)
Node * colNameToVar(ParseState *pstate, const char *colname, bool localonly, int location)
NameData relname
Definition: pg_class.h:38
#define lthird(l)
Definition: pg_list.h:188
#define lfourth(l)
Definition: pg_list.h:193
ParseLoc location
Definition: parsenodes.h:295
List * fields
Definition: parsenodes.h:294
ParseExprKind p_expr_kind
Definition: parse_node.h:211
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:235
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:236
String * makeString(char *str)
Definition: value.c:63

References _, Assert, colNameToVar(), ereport, err(), errcode(), errmsg(), errmsg_internal(), ERROR, errorMissingColumn(), errorMissingRTE(), EXPR_KIND_ALTER_COL_TRANSFORM, EXPR_KIND_CALL_ARGUMENT, EXPR_KIND_CHECK_CONSTRAINT, EXPR_KIND_COLUMN_DEFAULT, EXPR_KIND_COPY_WHERE, EXPR_KIND_CYCLE_MARK, EXPR_KIND_DISTINCT_ON, EXPR_KIND_DOMAIN_CHECK, EXPR_KIND_EXECUTE_PARAMETER, EXPR_KIND_FILTER, EXPR_KIND_FROM_FUNCTION, EXPR_KIND_FROM_SUBSELECT, EXPR_KIND_FUNCTION_DEFAULT, EXPR_KIND_GENERATED_COLUMN, EXPR_KIND_GROUP_BY, EXPR_KIND_HAVING, EXPR_KIND_INDEX_EXPRESSION, EXPR_KIND_INDEX_PREDICATE, EXPR_KIND_INSERT_TARGET, EXPR_KIND_JOIN_ON, EXPR_KIND_JOIN_USING, EXPR_KIND_LIMIT, EXPR_KIND_MERGE_RETURNING, EXPR_KIND_MERGE_WHEN, EXPR_KIND_NONE, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, EXPR_KIND_OTHER, EXPR_KIND_PARTITION_BOUND, EXPR_KIND_PARTITION_EXPRESSION, EXPR_KIND_POLICY, EXPR_KIND_RETURNING, EXPR_KIND_SELECT_TARGET, EXPR_KIND_STATS_EXPRESSION, EXPR_KIND_TRIGGER_WHEN, EXPR_KIND_UPDATE_SOURCE, EXPR_KIND_UPDATE_TARGET, EXPR_KIND_VALUES, EXPR_KIND_VALUES_SINGLE, EXPR_KIND_WHERE, EXPR_KIND_WINDOW_FRAME_GROUPS, EXPR_KIND_WINDOW_FRAME_RANGE, EXPR_KIND_WINDOW_FRAME_ROWS, EXPR_KIND_WINDOW_ORDER, EXPR_KIND_WINDOW_PARTITION, ColumnRef::fields, get_database_name(), IsA, lfourth, linitial, list_length(), list_make1, ColumnRef::location, lsecond, lthird, makeRangeVar(), makeString(), MyDatabaseId, NameListToString(), ParseState::p_expr_kind, ParseState::p_last_srf, ParseState::p_post_columnref_hook, ParseState::p_pre_columnref_hook, ParseFuncOrColumn(), parser_errposition(), refnameNamespaceItem(), relname, scanNSItemForColumn(), strVal, and transformWholeRowRef().

Referenced by transformExprRecurse().

◆ transformCurrentOfExpr()

static Node * transformCurrentOfExpr ( ParseState pstate,
CurrentOfExpr cexpr 
)
static

Definition at line 2560 of file parse_expr.c.

2561 {
2562  /* CURRENT OF can only appear at top level of UPDATE/DELETE */
2563  Assert(pstate->p_target_nsitem != NULL);
2564  cexpr->cvarno = pstate->p_target_nsitem->p_rtindex;
2565 
2566  /*
2567  * Check to see if the cursor name matches a parameter of type REFCURSOR.
2568  * If so, replace the raw name reference with a parameter reference. (This
2569  * is a hack for the convenience of plpgsql.)
2570  */
2571  if (cexpr->cursor_name != NULL) /* in case already transformed */
2572  {
2573  ColumnRef *cref = makeNode(ColumnRef);
2574  Node *node = NULL;
2575 
2576  /* Build an unqualified ColumnRef with the given name */
2577  cref->fields = list_make1(makeString(cexpr->cursor_name));
2578  cref->location = -1;
2579 
2580  /* See if there is a translation available from a parser hook */
2581  if (pstate->p_pre_columnref_hook != NULL)
2582  node = pstate->p_pre_columnref_hook(pstate, cref);
2583  if (node == NULL && pstate->p_post_columnref_hook != NULL)
2584  node = pstate->p_post_columnref_hook(pstate, cref, NULL);
2585 
2586  /*
2587  * XXX Should we throw an error if we get a translation that isn't a
2588  * refcursor Param? For now it seems best to silently ignore false
2589  * matches.
2590  */
2591  if (node != NULL && IsA(node, Param))
2592  {
2593  Param *p = (Param *) node;
2594 
2595  if (p->paramkind == PARAM_EXTERN &&
2596  p->paramtype == REFCURSOROID)
2597  {
2598  /* Matches, so convert CURRENT OF to a param reference */
2599  cexpr->cursor_name = NULL;
2600  cexpr->cursor_param = p->paramid;
2601  }
2602  }
2603  }
2604 
2605  return (Node *) cexpr;
2606 }
@ PARAM_EXTERN
Definition: primnodes.h:367
char * cursor_name
Definition: primnodes.h:2100
int paramid
Definition: primnodes.h:377
Oid paramtype
Definition: primnodes.h:378
ParamKind paramkind
Definition: primnodes.h:376
ParseNamespaceItem * p_target_nsitem
Definition: parse_node.h:208

References Assert, CurrentOfExpr::cursor_name, CurrentOfExpr::cursor_param, CurrentOfExpr::cvarno, ColumnRef::fields, IsA, list_make1, ColumnRef::location, makeNode, makeString(), ParseState::p_post_columnref_hook, ParseState::p_pre_columnref_hook, ParseNamespaceItem::p_rtindex, ParseState::p_target_nsitem, PARAM_EXTERN, Param::paramid, Param::paramkind, and Param::paramtype.

Referenced by transformExprRecurse().

◆ transformExpr()

Node* transformExpr ( ParseState pstate,
Node expr,
ParseExprKind  exprKind 
)

◆ transformExprRecurse()

static Node * transformExprRecurse ( ParseState pstate,
Node expr 
)
static

Definition at line 139 of file parse_expr.c.

140 {
141  Node *result;
142 
143  if (expr == NULL)
144  return NULL;
145 
146  /* Guard against stack overflow due to overly complex expressions */
148 
149  switch (nodeTag(expr))
150  {
151  case T_ColumnRef:
152  result = transformColumnRef(pstate, (ColumnRef *) expr);
153  break;
154 
155  case T_ParamRef:
156  result = transformParamRef(pstate, (ParamRef *) expr);
157  break;
158 
159  case T_A_Const:
160  result = (Node *) make_const(pstate, (A_Const *) expr);
161  break;
162 
163  case T_A_Indirection:
164  result = transformIndirection(pstate, (A_Indirection *) expr);
165  break;
166 
167  case T_A_ArrayExpr:
168  result = transformArrayExpr(pstate, (A_ArrayExpr *) expr,
169  InvalidOid, InvalidOid, -1);
170  break;
171 
172  case T_TypeCast:
173  result = transformTypeCast(pstate, (TypeCast *) expr);
174  break;
175 
176  case T_CollateClause:
177  result = transformCollateClause(pstate, (CollateClause *) expr);
178  break;
179 
180  case T_A_Expr:
181  {
182  A_Expr *a = (A_Expr *) expr;
183 
184  switch (a->kind)
185  {
186  case AEXPR_OP:
187  result = transformAExprOp(pstate, a);
188  break;
189  case AEXPR_OP_ANY:
190  result = transformAExprOpAny(pstate, a);
191  break;
192  case AEXPR_OP_ALL:
193  result = transformAExprOpAll(pstate, a);
194  break;
195  case AEXPR_DISTINCT:
196  case AEXPR_NOT_DISTINCT:
197  result = transformAExprDistinct(pstate, a);
198  break;
199  case AEXPR_NULLIF:
200  result = transformAExprNullIf(pstate, a);
201  break;
202  case AEXPR_IN:
203  result = transformAExprIn(pstate, a);
204  break;
205  case AEXPR_LIKE:
206  case AEXPR_ILIKE:
207  case AEXPR_SIMILAR:
208  /* we can transform these just like AEXPR_OP */
209  result = transformAExprOp(pstate, a);
210  break;
211  case AEXPR_BETWEEN:
212  case AEXPR_NOT_BETWEEN:
213  case AEXPR_BETWEEN_SYM:
215  result = transformAExprBetween(pstate, a);
216  break;
217  default:
218  elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
219  result = NULL; /* keep compiler quiet */
220  break;
221  }
222  break;
223  }
224 
225  case T_BoolExpr:
226  result = transformBoolExpr(pstate, (BoolExpr *) expr);
227  break;
228 
229  case T_FuncCall:
230  result = transformFuncCall(pstate, (FuncCall *) expr);
231  break;
232 
233  case T_MultiAssignRef:
234  result = transformMultiAssignRef(pstate, (MultiAssignRef *) expr);
235  break;
236 
237  case T_GroupingFunc:
238  result = transformGroupingFunc(pstate, (GroupingFunc *) expr);
239  break;
240 
241  case T_MergeSupportFunc:
242  result = transformMergeSupportFunc(pstate,
243  (MergeSupportFunc *) expr);
244  break;
245 
246  case T_NamedArgExpr:
247  {
248  NamedArgExpr *na = (NamedArgExpr *) expr;
249 
250  na->arg = (Expr *) transformExprRecurse(pstate, (Node *) na->arg);
251  result = expr;
252  break;
253  }
254 
255  case T_SubLink:
256  result = transformSubLink(pstate, (SubLink *) expr);
257  break;
258 
259  case T_CaseExpr:
260  result = transformCaseExpr(pstate, (CaseExpr *) expr);
261  break;
262 
263  case T_RowExpr:
264  result = transformRowExpr(pstate, (RowExpr *) expr, false);
265  break;
266 
267  case T_CoalesceExpr:
268  result = transformCoalesceExpr(pstate, (CoalesceExpr *) expr);
269  break;
270 
271  case T_MinMaxExpr:
272  result = transformMinMaxExpr(pstate, (MinMaxExpr *) expr);
273  break;
274 
275  case T_SQLValueFunction:
276  result = transformSQLValueFunction(pstate,
277  (SQLValueFunction *) expr);
278  break;
279 
280  case T_XmlExpr:
281  result = transformXmlExpr(pstate, (XmlExpr *) expr);
282  break;
283 
284  case T_XmlSerialize:
285  result = transformXmlSerialize(pstate, (XmlSerialize *) expr);
286  break;
287 
288  case T_NullTest:
289  {
290  NullTest *n = (NullTest *) expr;
291 
292  n->arg = (Expr *) transformExprRecurse(pstate, (Node *) n->arg);
293  /* the argument can be any type, so don't coerce it */
294  n->argisrow = type_is_rowtype(exprType((Node *) n->arg));
295  result = expr;
296  break;
297  }
298 
299  case T_BooleanTest:
300  result = transformBooleanTest(pstate, (BooleanTest *) expr);
301  break;
302 
303  case T_CurrentOfExpr:
304  result = transformCurrentOfExpr(pstate, (CurrentOfExpr *) expr);
305  break;
306 
307  /*
308  * In all places where DEFAULT is legal, the caller should have
309  * processed it rather than passing it to transformExpr().
310  */
311  case T_SetToDefault:
312  ereport(ERROR,
313  (errcode(ERRCODE_SYNTAX_ERROR),
314  errmsg("DEFAULT is not allowed in this context"),
315  parser_errposition(pstate,
316  ((SetToDefault *) expr)->location)));
317  break;
318 
319  /*
320  * CaseTestExpr doesn't require any processing; it is only
321  * injected into parse trees in a fully-formed state.
322  *
323  * Ordinarily we should not see a Var here, but it is convenient
324  * for transformJoinUsingClause() to create untransformed operator
325  * trees containing already-transformed Vars. The best
326  * alternative would be to deconstruct and reconstruct column
327  * references, which seems expensively pointless. So allow it.
328  */
329  case T_CaseTestExpr:
330  case T_Var:
331  {
332  result = (Node *) expr;
333  break;
334  }
335 
336  case T_JsonObjectConstructor:
337  result = transformJsonObjectConstructor(pstate, (JsonObjectConstructor *) expr);
338  break;
339 
340  case T_JsonArrayConstructor:
341  result = transformJsonArrayConstructor(pstate, (JsonArrayConstructor *) expr);
342  break;
343 
344  case T_JsonArrayQueryConstructor:
346  break;
347 
348  case T_JsonObjectAgg:
349  result = transformJsonObjectAgg(pstate, (JsonObjectAgg *) expr);
350  break;
351 
352  case T_JsonArrayAgg:
353  result = transformJsonArrayAgg(pstate, (JsonArrayAgg *) expr);
354  break;
355 
356  case T_JsonIsPredicate:
357  result = transformJsonIsPredicate(pstate, (JsonIsPredicate *) expr);
358  break;
359 
360  case T_JsonParseExpr:
361  result = transformJsonParseExpr(pstate, (JsonParseExpr *) expr);
362  break;
363 
364  case T_JsonScalarExpr:
365  result = transformJsonScalarExpr(pstate, (JsonScalarExpr *) expr);
366  break;
367 
368  case T_JsonSerializeExpr:
369  result = transformJsonSerializeExpr(pstate, (JsonSerializeExpr *) expr);
370  break;
371 
372  case T_JsonFuncExpr:
373  result = transformJsonFuncExpr(pstate, (JsonFuncExpr *) expr);
374  break;
375 
376  default:
377  /* should not reach here */
378  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(expr));
379  result = NULL; /* keep compiler quiet */
380  break;
381  }
382 
383  return result;
384 }
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2655
#define nodeTag(nodeptr)
Definition: nodes.h:133
Node * transformGroupingFunc(ParseState *pstate, GroupingFunc *p)
Definition: parse_agg.c:260
static Node * transformMergeSupportFunc(ParseState *pstate, MergeSupportFunc *f)
Definition: parse_expr.c:1378
static Node * transformJsonArrayConstructor(ParseState *pstate, JsonArrayConstructor *ctor)
Definition: parse_expr.c:3993
static Node * transformJsonScalarExpr(ParseState *pstate, JsonScalarExpr *expr)
Definition: parse_expr.c:4184
static Node * transformCurrentOfExpr(ParseState *pstate, CurrentOfExpr *cexpr)
Definition: parse_expr.c:2560
static Node * transformAExprOpAll(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1019
static Node * transformAExprIn(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1126
static Node * transformAExprOpAny(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1005
static Node * transformXmlSerialize(ParseState *pstate, XmlSerialize *xs)
Definition: parse_expr.c:2476
static Node * transformAExprNullIf(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1084
static Node * transformJsonArrayQueryConstructor(ParseState *pstate, JsonArrayQueryConstructor *ctor)
Definition: parse_expr.c:3735
static Node * transformSQLValueFunction(ParseState *pstate, SQLValueFunction *svf)
Definition: parse_expr.c:2294
static Node * transformColumnRef(ParseState *pstate, ColumnRef *cref)
Definition: parse_expr.c:511
static Node * transformCollateClause(ParseState *pstate, CollateClause *c)
Definition: parse_expr.c:2768
static Node * transformBoolExpr(ParseState *pstate, BoolExpr *a)
Definition: parse_expr.c:1403
static Node * transformFuncCall(ParseState *pstate, FuncCall *fn)
Definition: parse_expr.c:1439
static Node * transformMinMaxExpr(ParseState *pstate, MinMaxExpr *m)
Definition: parse_expr.c:2255
static Node * transformCoalesceExpr(ParseState *pstate, CoalesceExpr *c)
Definition: parse_expr.c:2206
static Node * transformSubLink(ParseState *pstate, SubLink *sublink)
Definition: parse_expr.c:1772
static Node * transformJsonObjectConstructor(ParseState *pstate, JsonObjectConstructor *ctor)
Definition: parse_expr.c:3698
static Node * transformAExprOp(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:924
static Node * transformJsonArrayAgg(ParseState *pstate, JsonArrayAgg *agg)
Definition: parse_expr.c:3953
static Node * transformMultiAssignRef(ParseState *pstate, MultiAssignRef *maref)
Definition: parse_expr.c:1484
static Node * transformBooleanTest(ParseState *pstate, BooleanTest *b)
Definition: parse_expr.c:2520
static Node * transformTypeCast(ParseState *pstate, TypeCast *tc)
Definition: parse_expr.c:2684
static Node * transformParamRef(ParseState *pstate, ParamRef *pref)
Definition: parse_expr.c:887
static Node * transformCaseExpr(ParseState *pstate, CaseExpr *c)
Definition: parse_expr.c:1632
static Node * transformJsonParseExpr(ParseState *pstate, JsonParseExpr *expr)
Definition: parse_expr.c:4135
static Node * transformIndirection(ParseState *pstate, A_Indirection *ind)
Definition: parse_expr.c:439
static Node * transformJsonSerializeExpr(ParseState *pstate, JsonSerializeExpr *expr)
Definition: parse_expr.c:4207
static Node * transformRowExpr(ParseState *pstate, RowExpr *r, bool allowDefault)
Definition: parse_expr.c:2168
static Node * transformXmlExpr(ParseState *pstate, XmlExpr *x)
Definition: parse_expr.c:2347
static Node * transformAExprBetween(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1284
static Node * transformJsonIsPredicate(ParseState *pstate, JsonIsPredicate *pred)
Definition: parse_expr.c:4073
static Node * transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *p)
Definition: parse_expr.c:4253
static Node * transformJsonObjectAgg(ParseState *pstate, JsonObjectAgg *agg)
Definition: parse_expr.c:3891
static Node * transformAExprDistinct(ParseState *pstate, A_Expr *a)
Definition: parse_expr.c:1033
Const * make_const(ParseState *pstate, A_Const *aconst)
Definition: parse_node.c:347
@ AEXPR_NULLIF
Definition: parsenodes.h:318
@ AEXPR_ILIKE
Definition: parsenodes.h:321
@ AEXPR_IN
Definition: parsenodes.h:319
@ AEXPR_DISTINCT
Definition: parsenodes.h:316
@ AEXPR_SIMILAR
Definition: parsenodes.h:322
@ AEXPR_LIKE
Definition: parsenodes.h:320
@ AEXPR_OP_ANY
Definition: parsenodes.h:314
@ AEXPR_OP_ALL
Definition: parsenodes.h:315
void check_stack_depth(void)
Definition: postgres.c:3531
Expr * arg
Definition: primnodes.h:791

References a, AEXPR_BETWEEN, AEXPR_BETWEEN_SYM, AEXPR_DISTINCT, AEXPR_ILIKE, AEXPR_IN, AEXPR_LIKE, AEXPR_NOT_BETWEEN, AEXPR_NOT_BETWEEN_SYM, AEXPR_NOT_DISTINCT, AEXPR_NULLIF, AEXPR_OP, AEXPR_OP_ALL, AEXPR_OP_ANY, AEXPR_SIMILAR, NamedArgExpr::arg, NullTest::arg, check_stack_depth(), elog, ereport, errcode(), errmsg(), ERROR, exprType(), InvalidOid, make_const(), nodeTag, parser_errposition(), transformAExprBetween(), transformAExprDistinct(), transformAExprIn(), transformAExprNullIf(), transformAExprOp(), transformAExprOpAll(), transformAExprOpAny(), transformArrayExpr(), transformBooleanTest(), transformBoolExpr(), transformCaseExpr(), transformCoalesceExpr(), transformCollateClause(), transformColumnRef(), transformCurrentOfExpr(), transformFuncCall(), transformGroupingFunc(), transformIndirection(), transformJsonArrayAgg(), transformJsonArrayConstructor(), transformJsonArrayQueryConstructor(), transformJsonFuncExpr(), transformJsonIsPredicate(), transformJsonObjectAgg(), transformJsonObjectConstructor(), transformJsonParseExpr(), transformJsonScalarExpr(), transformJsonSerializeExpr(), transformMergeSupportFunc(), transformMinMaxExpr(), transformMultiAssignRef(), transformParamRef(), transformRowExpr(), transformSQLValueFunction(), transformSubLink(), transformTypeCast(), transformXmlExpr(), transformXmlSerialize(), and type_is_rowtype().

Referenced by make_nulltest_from_distinct(), transformAExprBetween(), transformAExprDistinct(), transformAExprIn(), transformAExprNullIf(), transformAExprOp(), transformAExprOpAll(), transformAExprOpAny(), transformArrayExpr(), transformBooleanTest(), transformBoolExpr(), transformCaseExpr(), transformCoalesceExpr(), transformCollateClause(), transformExpr(), transformFuncCall(), transformIndirection(), transformJsonArrayQueryConstructor(), transformJsonBehavior(), transformJsonFuncExpr(), transformJsonObjectAgg(), transformJsonObjectConstructor(), transformJsonParseArg(), transformJsonScalarExpr(), transformJsonValueExpr(), transformMinMaxExpr(), transformMultiAssignRef(), transformSubLink(), transformTypeCast(), transformXmlExpr(), and transformXmlSerialize().

◆ transformFuncCall()

static Node * transformFuncCall ( ParseState pstate,
FuncCall fn 
)
static

Definition at line 1439 of file parse_expr.c.

1440 {
1441  Node *last_srf = pstate->p_last_srf;
1442  List *targs;
1443  ListCell *args;
1444 
1445  /* Transform the list of arguments ... */
1446  targs = NIL;
1447  foreach(args, fn->args)
1448  {
1449  targs = lappend(targs, transformExprRecurse(pstate,
1450  (Node *) lfirst(args)));
1451  }
1452 
1453  /*
1454  * When WITHIN GROUP is used, we treat its ORDER BY expressions as
1455  * additional arguments to the function, for purposes of function lookup
1456  * and argument type coercion. So, transform each such expression and add
1457  * them to the targs list. We don't explicitly mark where each argument
1458  * came from, but ParseFuncOrColumn can tell what's what by reference to
1459  * list_length(fn->agg_order).
1460  */
1461  if (fn->agg_within_group)
1462  {
1463  Assert(fn->agg_order != NIL);
1464  foreach(args, fn->agg_order)
1465  {
1466  SortBy *arg = (SortBy *) lfirst(args);
1467 
1468  targs = lappend(targs, transformExpr(pstate, arg->node,
1470  }
1471  }
1472 
1473  /* ... and hand off to ParseFuncOrColumn */
1474  return ParseFuncOrColumn(pstate,
1475  fn->funcname,
1476  targs,
1477  last_srf,
1478  fn,
1479  false,
1480  fn->location);
1481 }
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition: parse_expr.c:121
static void * fn(void *arg)
Definition: thread-alloc.c:119

References arg, generate_unaccent_rules::args, Assert, EXPR_KIND_ORDER_BY, fn(), lappend(), lfirst, NIL, ParseState::p_last_srf, ParseFuncOrColumn(), transformExpr(), and transformExprRecurse().

Referenced by transformExprRecurse().

◆ transformIndirection()

static Node * transformIndirection ( ParseState pstate,
A_Indirection ind 
)
static

Definition at line 439 of file parse_expr.c.

440 {
441  Node *last_srf = pstate->p_last_srf;
442  Node *result = transformExprRecurse(pstate, ind->arg);
443  List *subscripts = NIL;
444  int location = exprLocation(result);
445  ListCell *i;
446 
447  /*
448  * We have to split any field-selection operations apart from
449  * subscripting. Adjacent A_Indices nodes have to be treated as a single
450  * multidimensional subscript operation.
451  */
452  foreach(i, ind->indirection)
453  {
454  Node *n = lfirst(i);
455 
456  if (IsA(n, A_Indices))
457  subscripts = lappend(subscripts, n);
458  else if (IsA(n, A_Star))
459  {
460  ereport(ERROR,
461  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
462  errmsg("row expansion via \"*\" is not supported here"),
463  parser_errposition(pstate, location)));
464  }
465  else
466  {
467  Node *newresult;
468 
469  Assert(IsA(n, String));
470 
471  /* process subscripts before this field selection */
472  if (subscripts)
473  result = (Node *) transformContainerSubscripts(pstate,
474  result,
475  exprType(result),
476  exprTypmod(result),
477  subscripts,
478  false);
479  subscripts = NIL;
480 
481  newresult = ParseFuncOrColumn(pstate,
482  list_make1(n),
483  list_make1(result),
484  last_srf,
485  NULL,
486  false,
487  location);
488  if (newresult == NULL)
489  unknown_attribute(pstate, result, strVal(n), location);
490  result = newresult;
491  }
492  }
493  /* process trailing subscripts, if any */
494  if (subscripts)
495  result = (Node *) transformContainerSubscripts(pstate,
496  result,
497  exprType(result),
498  exprTypmod(result),
499  subscripts,
500  false);
501 
502  return result;
503 }
static void unknown_attribute(ParseState *pstate, Node *relref, const char *attname, int location)
Definition: parse_expr.c:393
SubscriptingRef * transformContainerSubscripts(ParseState *pstate, Node *containerBase, Oid containerType, int32 containerTypMod, List *indirection, bool isAssignment)
Definition: parse_node.c:243
Definition: value.h:64

References Assert, ereport, errcode(), errmsg(), ERROR, exprLocation(), exprType(), exprTypmod(), i, IsA, lappend(), lfirst, list_make1, NIL, ParseState::p_last_srf, ParseFuncOrColumn(), parser_errposition(), strVal, transformContainerSubscripts(), transformExprRecurse(), and unknown_attribute().

Referenced by transformExprRecurse().

◆ transformJsonAggConstructor()

static Node* transformJsonAggConstructor ( ParseState pstate,
JsonAggConstructor agg_ctor,
JsonReturning returning,
List args,
Oid  aggfnoid,
Oid  aggtype,
JsonConstructorType  ctor_type,
bool  unique,
bool  absent_on_null 
)
static

Definition at line 3806 of file parse_expr.c.

3811 {
3812  Node *node;
3813  Expr *aggfilter;
3814 
3815  aggfilter = agg_ctor->agg_filter ? (Expr *)
3816  transformWhereClause(pstate, agg_ctor->agg_filter,
3817  EXPR_KIND_FILTER, "FILTER") : NULL;
3818 
3819  if (agg_ctor->over)
3820  {
3821  /* window function */
3822  WindowFunc *wfunc = makeNode(WindowFunc);
3823 
3824  wfunc->winfnoid = aggfnoid;
3825  wfunc->wintype = aggtype;
3826  /* wincollid and inputcollid will be set by parse_collate.c */
3827  wfunc->args = args;
3828  wfunc->aggfilter = aggfilter;
3829  wfunc->runCondition = NIL;
3830  /* winref will be set by transformWindowFuncCall */
3831  wfunc->winstar = false;
3832  wfunc->winagg = true;
3833  wfunc->location = agg_ctor->location;
3834 
3835  /*
3836  * ordered aggs not allowed in windows yet
3837  */
3838  if (agg_ctor->agg_order != NIL)
3839  ereport(ERROR,
3840  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3841  errmsg("aggregate ORDER BY is not implemented for window functions"),
3842  parser_errposition(pstate, agg_ctor->location));
3843 
3844  /* parse_agg.c does additional window-func-specific processing */
3845  transformWindowFuncCall(pstate, wfunc, agg_ctor->over);
3846 
3847  node = (Node *) wfunc;
3848  }
3849  else
3850  {
3851  Aggref *aggref = makeNode(Aggref);
3852 
3853  aggref->aggfnoid = aggfnoid;
3854  aggref->aggtype = aggtype;
3855 
3856  /* aggcollid and inputcollid will be set by parse_collate.c */
3857  /* aggtranstype will be set by planner */
3858  /* aggargtypes will be set by transformAggregateCall */
3859  /* aggdirectargs and args will be set by transformAggregateCall */
3860  /* aggorder and aggdistinct will be set by transformAggregateCall */
3861  aggref->aggfilter = aggfilter;
3862  aggref->aggstar = false;
3863  aggref->aggvariadic = false;
3864  aggref->aggkind = AGGKIND_NORMAL;
3865  aggref->aggpresorted = false;
3866  /* agglevelsup will be set by transformAggregateCall */
3867  aggref->aggsplit = AGGSPLIT_SIMPLE; /* planner might change this */
3868  aggref->aggno = -1; /* planner will set aggno and aggtransno */
3869  aggref->aggtransno = -1;
3870  aggref->location = agg_ctor->location;
3871 
3872  transformAggregateCall(pstate, aggref, args, agg_ctor->agg_order, false);
3873 
3874  node = (Node *) aggref;
3875  }
3876 
3877  return makeJsonConstructorExpr(pstate, ctor_type, NIL, (Expr *) node,
3878  returning, unique, absent_on_null,
3879  agg_ctor->location);
3880 }
@ AGGSPLIT_SIMPLE
Definition: nodes.h:376
void transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, WindowDef *windef)
Definition: parse_agg.c:822
void transformAggregateCall(ParseState *pstate, Aggref *agg, List *args, List *aggorder, bool agg_distinct)
Definition: parse_agg.c:104
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
static Node * makeJsonConstructorExpr(ParseState *pstate, JsonConstructorType type, List *args, Expr *fexpr, JsonReturning *returning, bool unique, bool absent_on_null, int location)
Definition: parse_expr.c:3636
Oid aggfnoid
Definition: primnodes.h:444
Expr * aggfilter
Definition: primnodes.h:477
ParseLoc location
Definition: primnodes.h:507
struct WindowDef * over
Definition: parsenodes.h:1971
List * args
Definition: primnodes.h:575
Expr * aggfilter
Definition: primnodes.h:577
ParseLoc location
Definition: primnodes.h:587
Oid winfnoid
Definition: primnodes.h:567

References JsonAggConstructor::agg_filter, JsonAggConstructor::agg_order, Aggref::aggfilter, WindowFunc::aggfilter, Aggref::aggfnoid, AGGSPLIT_SIMPLE, generate_unaccent_rules::args, WindowFunc::args, ereport, errcode(), errmsg(), ERROR, EXPR_KIND_FILTER, JsonAggConstructor::location, Aggref::location, WindowFunc::location, makeJsonConstructorExpr(), makeNode, NIL, JsonAggConstructor::over, parser_errposition(), transformAggregateCall(), transformWhereClause(), transformWindowFuncCall(), and WindowFunc::winfnoid.

Referenced by transformJsonArrayAgg(), and transformJsonObjectAgg().

◆ transformJsonArrayAgg()

static Node * transformJsonArrayAgg ( ParseState pstate,
JsonArrayAgg agg 
)
static

Definition at line 3953 of file parse_expr.c.

3954 {
3955  JsonReturning *returning;
3956  Node *arg;
3957  Oid aggfnoid;
3958  Oid aggtype;
3959 
3960  arg = transformJsonValueExpr(pstate, "JSON_ARRAYAGG()", agg->arg,
3961  JS_FORMAT_DEFAULT, InvalidOid, false);
3962 
3963  returning = transformJsonConstructorOutput(pstate, agg->constructor->output,
3964  list_make1(arg));
3965 
3966  if (returning->format->format_type == JS_FORMAT_JSONB)
3967  {
3968  aggfnoid = agg->absent_on_null ? F_JSONB_AGG_STRICT : F_JSONB_AGG;
3969  aggtype = JSONBOID;
3970  }
3971  else
3972  {
3973  aggfnoid = agg->absent_on_null ? F_JSON_AGG_STRICT : F_JSON_AGG;
3974  aggtype = JSONOID;
3975  }
3976 
3977  return transformJsonAggConstructor(pstate, agg->constructor, returning,
3978  list_make1(arg), aggfnoid, aggtype,
3980  false, agg->absent_on_null);
3981 }
static Node * transformJsonValueExpr(ParseState *pstate, const char *constructName, JsonValueExpr *ve, JsonFormatType default_format, Oid targettype, bool isarg)
Definition: parse_expr.c:3278
static JsonReturning * transformJsonConstructorOutput(ParseState *pstate, JsonOutput *output, List *args)
Definition: parse_expr.c:3537
static Node * transformJsonAggConstructor(ParseState *pstate, JsonAggConstructor *agg_ctor, JsonReturning *returning, List *args, Oid aggfnoid, Oid aggtype, JsonConstructorType ctor_type, bool unique, bool absent_on_null)
Definition: parse_expr.c:3806
@ JSCTOR_JSON_ARRAYAGG
Definition: primnodes.h:1689
JsonOutput * output
Definition: parsenodes.h:1968
bool absent_on_null
Definition: parsenodes.h:1997
JsonValueExpr * arg
Definition: parsenodes.h:1996
JsonAggConstructor * constructor
Definition: parsenodes.h:1995

References JsonArrayAgg::absent_on_null, arg, JsonArrayAgg::arg, JsonArrayAgg::constructor, JsonReturning::format, JsonFormat::format_type, InvalidOid, JS_FORMAT_DEFAULT, JS_FORMAT_JSONB, JSCTOR_JSON_ARRAYAGG, list_make1, JsonAggConstructor::output, transformJsonAggConstructor(), transformJsonConstructorOutput(), and transformJsonValueExpr().

Referenced by transformExprRecurse().

◆ transformJsonArrayConstructor()

static Node * transformJsonArrayConstructor ( ParseState pstate,
JsonArrayConstructor ctor 
)
static

Definition at line 3993 of file parse_expr.c.

3994 {
3995  JsonReturning *returning;
3996  List *args = NIL;
3997 
3998  /* transform element expressions, if any */
3999  if (ctor->exprs)
4000  {
4001  ListCell *lc;
4002 
4003  /* transform and append element arguments */
4004  foreach(lc, ctor->exprs)
4005  {
4006  JsonValueExpr *jsval = castNode(JsonValueExpr, lfirst(lc));
4007  Node *val = transformJsonValueExpr(pstate, "JSON_ARRAY()",
4008  jsval, JS_FORMAT_DEFAULT,
4009  InvalidOid, false);
4010 
4011  args = lappend(args, val);
4012  }
4013  }
4014 
4015  returning = transformJsonConstructorOutput(pstate, ctor->output, args);
4016 
4017  return makeJsonConstructorExpr(pstate, JSCTOR_JSON_ARRAY, args, NULL,
4018  returning, false, ctor->absent_on_null,
4019  ctor->location);
4020 }
@ JSCTOR_JSON_ARRAY
Definition: primnodes.h:1687
JsonOutput * output
Definition: parsenodes.h:1941

References JsonArrayConstructor::absent_on_null, generate_unaccent_rules::args, castNode, JsonArrayConstructor::exprs, InvalidOid, JS_FORMAT_DEFAULT, JSCTOR_JSON_ARRAY, lappend(), lfirst, JsonArrayConstructor::location, makeJsonConstructorExpr(), NIL, JsonArrayConstructor::output, transformJsonConstructorOutput(), transformJsonValueExpr(), and val.

Referenced by transformExprRecurse().

◆ transformJsonArrayQueryConstructor()

static Node * transformJsonArrayQueryConstructor ( ParseState pstate,
JsonArrayQueryConstructor ctor 
)
static

Definition at line 3735 of file parse_expr.c.

3737 {
3738  SubLink *sublink = makeNode(SubLink);
3741  Alias *alias = makeNode(Alias);
3742  ResTarget *target = makeNode(ResTarget);
3744  ColumnRef *colref = makeNode(ColumnRef);
3745  Query *query;
3746  ParseState *qpstate;
3747 
3748  /* Transform query only for counting target list entries. */
3749  qpstate = make_parsestate(pstate);
3750 
3751  query = transformStmt(qpstate, ctor->query);
3752 
3753  if (count_nonjunk_tlist_entries(query->targetList) != 1)
3754  ereport(ERROR,
3755  errcode(ERRCODE_SYNTAX_ERROR),
3756  errmsg("subquery must return only one column"),
3757  parser_errposition(pstate, ctor->location));
3758 
3759  free_parsestate(qpstate);
3760 
3761  colref->fields = list_make2(makeString(pstrdup("q")),
3762  makeString(pstrdup("a")));
3763  colref->location = ctor->location;
3764 
3765  /*
3766  * No formatting necessary, so set formatted_expr to be the same as
3767  * raw_expr.
3768  */
3769  agg->arg = makeJsonValueExpr((Expr *) colref, (Expr *) colref,
3770  ctor->format);
3771  agg->absent_on_null = ctor->absent_on_null;
3773  agg->constructor->agg_order = NIL;
3774  agg->constructor->output = ctor->output;
3775  agg->constructor->location = ctor->location;
3776 
3777  target->name = NULL;
3778  target->indirection = NIL;
3779  target->val = (Node *) agg;
3780  target->location = ctor->location;
3781 
3782  alias->aliasname = pstrdup("q");
3783  alias->colnames = list_make1(makeString(pstrdup("a")));
3784 
3785  range->lateral = false;
3786  range->subquery = ctor->query;
3787  range->alias = alias;
3788 
3789  select->targetList = list_make1(target);
3790  select->fromClause = list_make1(range);
3791 
3792  sublink->subLinkType = EXPR_SUBLINK;
3793  sublink->subLinkId = 0;
3794  sublink->testexpr = NULL;
3795  sublink->operName = NIL;
3796  sublink->subselect = (Node *) select;
3797  sublink->location = ctor->location;
3798 
3799  return transformExprRecurse(pstate, (Node *) sublink);
3800 }
JsonValueExpr * makeJsonValueExpr(Expr *raw_expr, Expr *formatted_expr, JsonFormat *format)
Definition: makefuncs.c:863
char * pstrdup(const char *in)
Definition: mcxt.c:1695
void free_parsestate(ParseState *pstate)
Definition: parse_node.c:72
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:39
Query * transformStmt(ParseState *pstate, Node *parseTree)
Definition: analyze.c:311
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412
char * aliasname
Definition: primnodes.h:50
List * colnames
Definition: primnodes.h:51
List * targetList
Definition: parsenodes.h:191
Node * val
Definition: parsenodes.h:519
ParseLoc location
Definition: parsenodes.h:520
List * indirection
Definition: parsenodes.h:518
char * name
Definition: parsenodes.h:517
int count_nonjunk_tlist_entries(List *tlist)
Definition: tlist.c:186
#define select(n, r, w, e, timeout)
Definition: win32_port.h:495

References JsonArrayQueryConstructor::absent_on_null, JsonArrayAgg::absent_on_null, JsonAggConstructor::agg_order, Alias::aliasname, JsonArrayAgg::arg, Alias::colnames, JsonArrayAgg::constructor, count_nonjunk_tlist_entries(), ereport, errcode(), errmsg(), ERROR, EXPR_SUBLINK, ColumnRef::fields, JsonArrayQueryConstructor::format, free_parsestate(), ResTarget::indirection, list_make1, list_make2, ColumnRef::location, ResTarget::location, JsonArrayQueryConstructor::location, JsonAggConstructor::location, SubLink::location, make_parsestate(), makeJsonValueExpr(), makeNode, makeString(), ResTarget::name, NIL, JsonArrayQueryConstructor::output, JsonAggConstructor::output, parser_errposition(), pstrdup(), JsonArrayQueryConstructor::query, range(), select, SubLink::subLinkId, SubLink::subLinkType, SubLink::subselect, Query::targetList, SubLink::testexpr, transformExprRecurse(), transformStmt(), and ResTarget::val.

Referenced by transformExprRecurse().

◆ transformJsonBehavior()

static JsonBehavior * transformJsonBehavior ( ParseState pstate,
JsonBehavior behavior,
JsonBehaviorType  default_behavior,
JsonReturning returning 
)
static

Definition at line 4658 of file parse_expr.c.

4661 {
4662  JsonBehaviorType btype = default_behavior;
4663  Node *expr = NULL;
4664  bool coerce_at_runtime = false;
4665  int location = -1;
4666 
4667  if (behavior)
4668  {
4669  btype = behavior->btype;
4670  location = behavior->location;
4671  if (btype == JSON_BEHAVIOR_DEFAULT)
4672  {
4673  expr = transformExprRecurse(pstate, behavior->expr);
4674  if (!ValidJsonBehaviorDefaultExpr(expr, NULL))
4675  ereport(ERROR,
4676  (errcode(ERRCODE_DATATYPE_MISMATCH),
4677  errmsg("can only specify a constant, non-aggregate function, or operator expression for DEFAULT"),
4678  parser_errposition(pstate, exprLocation(expr))));
4679  if (contain_var_clause(expr))
4680  ereport(ERROR,
4681  (errcode(ERRCODE_DATATYPE_MISMATCH),
4682  errmsg("DEFAULT expression must not contain column references"),
4683  parser_errposition(pstate, exprLocation(expr))));
4684  if (expression_returns_set(expr))
4685  ereport(ERROR,
4686  (errcode(ERRCODE_DATATYPE_MISMATCH),
4687  errmsg("DEFAULT expression must not return a set"),
4688  parser_errposition(pstate, exprLocation(expr))));
4689  }
4690  }
4691 
4692  if (expr == NULL && btype != JSON_BEHAVIOR_ERROR)
4693  expr = GetJsonBehaviorConst(btype, location);
4694 
4695  if (expr)
4696  {
4697  Node *coerced_expr = expr;
4698  bool isnull = (IsA(expr, Const) && ((Const *) expr)->constisnull);
4699 
4700  /*
4701  * Coerce NULLs and "internal" (that is, not specified by the user)
4702  * jsonb-valued expressions at runtime using json_populate_type().
4703  *
4704  * For other (user-specified) non-NULL values, try to find a cast and
4705  * error out if one is not found.
4706  */
4707  if (isnull ||
4708  (exprType(expr) == JSONBOID &&
4709  btype == default_behavior))
4710  coerce_at_runtime = true;
4711  else
4712  coerced_expr =
4713  coerce_to_target_type(pstate, expr, exprType(expr),
4714  returning->typid, returning->typmod,
4716  exprLocation((Node *) behavior));
4717 
4718  if (coerced_expr == NULL)
4719  ereport(ERROR,
4720  errcode(ERRCODE_CANNOT_COERCE),
4721  errmsg("cannot cast behavior expression of type %s to %s",
4722  format_type_be(exprType(expr)),
4723  format_type_be(returning->typid)),
4724  parser_errposition(pstate, exprLocation(expr)));
4725  else
4726  expr = coerced_expr;
4727  }
4728 
4729  if (behavior)
4730  behavior->expr = expr;
4731  else
4732  behavior = makeJsonBehavior(btype, expr, location);
4733 
4734  behavior->coerce = coerce_at_runtime;
4735 
4736  return behavior;
4737 }
JsonBehavior * makeJsonBehavior(JsonBehaviorType btype, Node *expr, int location)
Definition: makefuncs.c:880
static bool ValidJsonBehaviorDefaultExpr(Node *expr, void *context)
Definition: parse_expr.c:4625
static Node * GetJsonBehaviorConst(JsonBehaviorType btype, int location)
Definition: parse_expr.c:4744
JsonBehaviorType
Definition: primnodes.h:1760
Node * expr
Definition: primnodes.h:1787
ParseLoc location
Definition: primnodes.h:1789
JsonBehaviorType btype
Definition: primnodes.h:1786
bool contain_var_clause(Node *node)
Definition: var.c:403

References JsonBehavior::btype, JsonBehavior::coerce, COERCE_EXPLICIT_CAST, coerce_to_target_type(), COERCION_EXPLICIT, contain_var_clause(), ereport, errcode(), errmsg(), ERROR, JsonBehavior::expr, expression_returns_set(), exprLocation(), exprType(), format_type_be(), GetJsonBehaviorConst(), IsA, JSON_BEHAVIOR_DEFAULT, JSON_BEHAVIOR_ERROR, JsonBehavior::location, makeJsonBehavior(), parser_errposition(), transformExprRecurse(), JsonReturning::typid, JsonReturning::typmod, and ValidJsonBehaviorDefaultExpr().

Referenced by transformJsonFuncExpr().

◆ transformJsonConstructorOutput()

static JsonReturning* transformJsonConstructorOutput ( ParseState pstate,
JsonOutput output,
List args 
)
static

Definition at line 3537 of file parse_expr.c.

3539 {
3540  JsonReturning *returning = transformJsonOutput(pstate, output, true);
3541 
3542  if (!OidIsValid(returning->typid))
3543  {
3544  ListCell *lc;
3545  bool have_jsonb = false;
3546 
3547  foreach(lc, args)
3548  {
3549  Node *expr = lfirst(lc);
3550  Oid typid = exprType(expr);
3551 
3552  have_jsonb |= typid == JSONBOID;
3553 
3554  if (have_jsonb)
3555  break;
3556  }
3557 
3558  if (have_jsonb)
3559  {
3560  returning->typid = JSONBOID;
3561  returning->format->format_type = JS_FORMAT_JSONB;
3562  }
3563  else
3564  {
3565  /* XXX TEXT is default by the standard, but we return JSON */
3566  returning->typid = JSONOID;
3567  returning->format->format_type = JS_FORMAT_JSON;
3568  }
3569 
3570  returning->typmod = -1;
3571  }
3572 
3573  return returning;
3574 }
FILE * output
static JsonReturning * transformJsonOutput(ParseState *pstate, const JsonOutput *output, bool allow_format)
Definition: parse_expr.c:3490

References generate_unaccent_rules::args, exprType(), JsonReturning::format, JsonFormat::format_type, JS_FORMAT_JSON, JS_FORMAT_JSONB, lfirst, OidIsValid, output, transformJsonOutput(), JsonReturning::typid, and JsonReturning::typmod.

Referenced by transformJsonArrayAgg(), transformJsonArrayConstructor(), transformJsonObjectAgg(), and transformJsonObjectConstructor().

◆ transformJsonFuncExpr()

static Node * transformJsonFuncExpr ( ParseState pstate,
JsonFuncExpr p 
)
static

Definition at line 4253 of file parse_expr.c.

4254 {
4255  JsonExpr *jsexpr;
4256  Node *path_spec;
4257  const char *func_name = NULL;
4258  JsonFormatType default_format;
4259 
4260  switch (func->op)
4261  {
4262  case JSON_EXISTS_OP:
4263  func_name = "JSON_EXISTS";
4264  default_format = JS_FORMAT_DEFAULT;
4265  break;
4266  case JSON_QUERY_OP:
4267  func_name = "JSON_QUERY";
4268  default_format = JS_FORMAT_JSONB;
4269  break;
4270  case JSON_VALUE_OP:
4271  func_name = "JSON_VALUE";
4272  default_format = JS_FORMAT_DEFAULT;
4273  break;
4274  case JSON_TABLE_OP:
4275  func_name = "JSON_TABLE";
4276  default_format = JS_FORMAT_JSONB;
4277  break;
4278  default:
4279  elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4280  default_format = JS_FORMAT_DEFAULT; /* keep compiler quiet */
4281  break;
4282  }
4283 
4284  /*
4285  * Even though the syntax allows it, FORMAT JSON specification in
4286  * RETURNING is meaningless except for JSON_QUERY(). Flag if not
4287  * JSON_QUERY().
4288  */
4289  if (func->output && func->op != JSON_QUERY_OP)
4290  {
4291  JsonFormat *format = func->output->returning->format;
4292 
4293  if (format->format_type != JS_FORMAT_DEFAULT ||
4294  format->encoding != JS_ENC_DEFAULT)
4295  ereport(ERROR,
4296  errcode(ERRCODE_SYNTAX_ERROR),
4297  errmsg("cannot specify FORMAT JSON in RETURNING clause of %s()",
4298  func_name),
4299  parser_errposition(pstate, format->location));
4300  }
4301 
4302  /* OMIT QUOTES is meaningless when strings are wrapped. */
4303  if (func->op == JSON_QUERY_OP &&
4304  func->quotes == JS_QUOTES_OMIT &&
4305  (func->wrapper == JSW_CONDITIONAL ||
4306  func->wrapper == JSW_UNCONDITIONAL))
4307  ereport(ERROR,
4308  errcode(ERRCODE_SYNTAX_ERROR),
4309  errmsg("SQL/JSON QUOTES behavior must not be specified when WITH WRAPPER is used"),
4310  parser_errposition(pstate, func->location));
4311 
4312  jsexpr = makeNode(JsonExpr);
4313  jsexpr->location = func->location;
4314  jsexpr->op = func->op;
4315  jsexpr->column_name = func->column_name;
4316 
4317  /*
4318  * jsonpath machinery can only handle jsonb documents, so coerce the input
4319  * if not already of jsonb type.
4320  */
4321  jsexpr->formatted_expr = transformJsonValueExpr(pstate, func_name,
4322  func->context_item,
4323  default_format,
4324  JSONBOID,
4325  false);
4326  jsexpr->format = func->context_item->format;
4327 
4328  path_spec = transformExprRecurse(pstate, func->pathspec);
4329  path_spec = coerce_to_target_type(pstate, path_spec, exprType(path_spec),
4330  JSONPATHOID, -1,
4332  exprLocation(path_spec));
4333  if (path_spec == NULL)
4334  ereport(ERROR,
4335  (errcode(ERRCODE_DATATYPE_MISMATCH),
4336  errmsg("JSON path expression must be of type %s, not of type %s",
4337  "jsonpath", format_type_be(exprType(path_spec))),
4338  parser_errposition(pstate, exprLocation(path_spec))));
4339  jsexpr->path_spec = path_spec;
4340 
4341  /* Transform and coerce the PASSING arguments to jsonb. */
4342  transformJsonPassingArgs(pstate, func_name,
4344  func->passing,
4345  &jsexpr->passing_values,
4346  &jsexpr->passing_names);
4347 
4348  /* Transform the JsonOutput into JsonReturning. */
4349  jsexpr->returning = transformJsonOutput(pstate, func->output, false);
4350 
4351  switch (func->op)
4352  {
4353  case JSON_EXISTS_OP:
4354  /* JSON_EXISTS returns boolean by default. */
4355  if (!OidIsValid(jsexpr->returning->typid))
4356  {
4357  jsexpr->returning->typid = BOOLOID;
4358  jsexpr->returning->typmod = -1;
4359  }
4360 
4361  /* JSON_TABLE() COLUMNS can specify a non-boolean type. */
4362  if (jsexpr->returning->typid != BOOLOID)
4363  {
4364  Node *coercion_expr;
4365  CaseTestExpr *placeholder = makeNode(CaseTestExpr);
4366  int location = exprLocation((Node *) jsexpr);
4367 
4368  /*
4369  * We abuse CaseTestExpr here as placeholder to pass the
4370  * result of evaluating JSON_EXISTS to the coercion
4371  * expression.
4372  */
4373  placeholder->typeId = BOOLOID;
4374  placeholder->typeMod = -1;
4375  placeholder->collation = InvalidOid;
4376 
4377  coercion_expr =
4378  coerce_to_target_type(pstate, (Node *) placeholder, BOOLOID,
4379  jsexpr->returning->typid,
4380  jsexpr->returning->typmod,
4383  location);
4384 
4385  if (coercion_expr == NULL)
4386  ereport(ERROR,
4387  (errcode(ERRCODE_CANNOT_COERCE),
4388  errmsg("cannot cast type %s to %s",
4389  format_type_be(BOOLOID),
4390  format_type_be(jsexpr->returning->typid)),
4391  parser_coercion_errposition(pstate, location, (Node *) jsexpr)));
4392 
4393  if (coercion_expr != (Node *) placeholder)
4394  jsexpr->coercion_expr = coercion_expr;
4395  }
4396 
4397  jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4399  jsexpr->returning);
4400  break;
4401 
4402  case JSON_QUERY_OP:
4403  /* JSON_QUERY returns jsonb by default. */
4404  if (!OidIsValid(jsexpr->returning->typid))
4405  {
4406  JsonReturning *ret = jsexpr->returning;
4407 
4408  ret->typid = JSONBOID;
4409  ret->typmod = -1;
4410  }
4411 
4412  /*
4413  * Keep quotes on scalar strings by default, omitting them only if
4414  * OMIT QUOTES is specified.
4415  */
4416  jsexpr->omit_quotes = (func->quotes == JS_QUOTES_OMIT);
4417  jsexpr->wrapper = func->wrapper;
4418 
4419  coerceJsonExprOutput(pstate, jsexpr);
4420 
4421  if (func->on_empty)
4422  jsexpr->on_empty = transformJsonBehavior(pstate,
4423  func->on_empty,
4425  jsexpr->returning);
4426  jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4428  jsexpr->returning);
4429  break;
4430 
4431  case JSON_VALUE_OP:
4432  /* JSON_VALUE returns text by default. */
4433  if (!OidIsValid(jsexpr->returning->typid))
4434  {
4435  jsexpr->returning->typid = TEXTOID;
4436  jsexpr->returning->typmod = -1;
4437  }
4438 
4439  /*
4440  * Override whatever transformJsonOutput() set these to, which
4441  * assumes that output type to be jsonb.
4442  */
4445 
4446  /* Always omit quotes from scalar strings. */
4447  jsexpr->omit_quotes = true;
4448 
4449  coerceJsonExprOutput(pstate, jsexpr);
4450 
4451  if (func->on_empty)
4452  jsexpr->on_empty = transformJsonBehavior(pstate,
4453  func->on_empty,
4455  jsexpr->returning);
4456  jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4458  jsexpr->returning);
4459  break;
4460 
4461  case JSON_TABLE_OP:
4462  if (!OidIsValid(jsexpr->returning->typid))
4463  {
4464  jsexpr->returning->typid = exprType(jsexpr->formatted_expr);
4465  jsexpr->returning->typmod = -1;
4466  }
4467  jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4469  jsexpr->returning);
4470  break;
4471 
4472  default:
4473  elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4474  break;
4475  }
4476 
4477  return (Node *) jsexpr;
4478 }
static void coerceJsonExprOutput(ParseState *pstate, JsonExpr *jsexpr)
Definition: parse_expr.c:4510
static void transformJsonPassingArgs(ParseState *pstate, const char *constructName, JsonFormatType format, List *args, List **passing_values, List **passing_names)
Definition: parse_expr.c:4484
static JsonBehavior * transformJsonBehavior(ParseState *pstate, JsonBehavior *behavior, JsonBehaviorType default_behavior, JsonReturning *returning)
Definition: parse_expr.c:4658
@ JS_QUOTES_OMIT
Definition: parsenodes.h:1780
JsonFormatType
Definition: primnodes.h:1637
@ JSW_UNCONDITIONAL
Definition: primnodes.h:1749
@ JSW_CONDITIONAL
Definition: primnodes.h:1748
@ JSON_TABLE_OP
Definition: primnodes.h:1801
@ JSON_EXISTS_OP
Definition: primnodes.h:1798
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:736
char * column_name
Definition: primnodes.h:1815
ParseLoc location
Definition: primnodes.h:1861
List * passing_values
Definition: primnodes.h:1832
JsonBehavior * on_empty
Definition: primnodes.h:1835
JsonFormat * format
Definition: primnodes.h:1822
List * passing_names
Definition: primnodes.h:1831
Node * path_spec
Definition: primnodes.h:1825
JsonWrapper wrapper
Definition: primnodes.h:1852
JsonBehavior * on_error
Definition: primnodes.h:1836
JsonEncoding encoding
Definition: primnodes.h:1652

References COERCE_IMPLICIT_CAST, coerce_to_target_type(), coerceJsonExprOutput(), COERCION_EXPLICIT, JsonFuncExpr::column_name, JsonExpr::column_name, JsonFuncExpr::context_item, elog, JsonFormat::encoding, ereport, errcode(), errmsg(), ERROR, exprLocation(), exprType(), format, JsonReturning::format, JsonValueExpr::format, JsonExpr::format, JsonFormat::format_type, format_type_be(), JsonExpr::formatted_expr, InvalidOid, JS_ENC_DEFAULT, JS_FORMAT_DEFAULT, JS_FORMAT_JSONB, JS_QUOTES_OMIT, JSON_BEHAVIOR_EMPTY, JSON_BEHAVIOR_FALSE, JSON_BEHAVIOR_NULL, JSON_EXISTS_OP, JSON_QUERY_OP, JSON_TABLE_OP, JSON_VALUE_OP, JSW_CONDITIONAL, JSW_UNCONDITIONAL, JsonFuncExpr::location, JsonExpr::location, makeNode, OidIsValid, JsonExpr::omit_quotes, JsonFuncExpr::on_empty, JsonExpr::on_empty, JsonFuncExpr::on_error, JsonExpr::on_error, JsonFuncExpr::op, JsonExpr::op, JsonFuncExpr::output, parser_coercion_errposition(), parser_errposition(), JsonFuncExpr::passing, JsonExpr::passing_names, JsonExpr::passing_values, JsonExpr::path_spec, JsonFuncExpr::pathspec, JsonFuncExpr::quotes, JsonOutput::returning, JsonExpr::returning, transformExprRecurse(), transformJsonBehavior(), transformJsonOutput(), transformJsonPassingArgs(), transformJsonValueExpr(), CaseTestExpr::typeId, JsonReturning::typid, JsonReturning::typmod, JsonFuncExpr::wrapper, and JsonExpr::wrapper.

Referenced by transformExprRecurse().

◆ transformJsonIsPredicate()

static Node * transformJsonIsPredicate ( ParseState pstate,
JsonIsPredicate pred 
)
static

Definition at line 4073 of file parse_expr.c.

4074 {
4075  Oid exprtype;
4076  Node *expr = transformJsonParseArg(pstate, pred->expr, pred->format,
4077  &exprtype);
4078 
4079  /* make resulting expression */
4080  if (exprtype != TEXTOID && exprtype != JSONOID && exprtype != JSONBOID)
4081  ereport(ERROR,
4082  (errcode(ERRCODE_DATATYPE_MISMATCH),
4083  errmsg("cannot use type %s in IS JSON predicate",
4084  format_type_be(exprtype))));
4085 
4086  /* This intentionally(?) drops the format clause. */
4087  return makeJsonIsPredicate(expr, NULL, pred->item_type,
4088  pred->unique_keys, pred->location);
4089 }
Node * makeJsonIsPredicate(Node *expr, JsonFormat *format, JsonValueType item_type, bool unique_keys, int location)
Definition: makefuncs.c:911
static Node * transformJsonParseArg(ParseState *pstate, Node *jsexpr, JsonFormat *format, Oid *exprtype)
Definition: parse_expr.c:4023
JsonFormat * format
Definition: primnodes.h:1732
JsonValueType item_type
Definition: primnodes.h:1733
ParseLoc location
Definition: primnodes.h:1735

References ereport, errcode(), errmsg(), ERROR, JsonIsPredicate::expr, JsonIsPredicate::format, format_type_be(), JsonIsPredicate::item_type, JsonIsPredicate::location, makeJsonIsPredicate(), transformJsonParseArg(), and JsonIsPredicate::unique_keys.

Referenced by transformExprRecurse().

◆ transformJsonObjectAgg()

static Node * transformJsonObjectAgg ( ParseState pstate,
JsonObjectAgg agg 
)
static

Definition at line 3891 of file parse_expr.c.

3892 {
3893  JsonReturning *returning;
3894  Node *key;
3895  Node *val;
3896  List *args;
3897  Oid aggfnoid;
3898  Oid aggtype;
3899 
3900  key = transformExprRecurse(pstate, (Node *) agg->arg->key);
3901  val = transformJsonValueExpr(pstate, "JSON_OBJECTAGG()",
3902  agg->arg->value,
3904  InvalidOid, false);
3905  args = list_make2(key, val);
3906 
3907  returning = transformJsonConstructorOutput(pstate, agg->constructor->output,
3908  args);
3909 
3910  if (returning->format->format_type == JS_FORMAT_JSONB)
3911  {
3912  if (agg->absent_on_null)
3913  if (agg->unique)
3914  aggfnoid = F_JSONB_OBJECT_AGG_UNIQUE_STRICT;
3915  else
3916  aggfnoid = F_JSONB_OBJECT_AGG_STRICT;
3917  else if (agg->unique)
3918  aggfnoid = F_JSONB_OBJECT_AGG_UNIQUE;
3919  else
3920  aggfnoid = F_JSONB_OBJECT_AGG;
3921 
3922  aggtype = JSONBOID;
3923  }
3924  else
3925  {
3926  if (agg->absent_on_null)
3927  if (agg->unique)
3928  aggfnoid = F_JSON_OBJECT_AGG_UNIQUE_STRICT;
3929  else
3930  aggfnoid = F_JSON_OBJECT_AGG_STRICT;
3931  else if (agg->unique)
3932  aggfnoid = F_JSON_OBJECT_AGG_UNIQUE;
3933  else
3934  aggfnoid = F_JSON_OBJECT_AGG;
3935 
3936  aggtype = JSONOID;
3937  }
3938 
3939  return transformJsonAggConstructor(pstate, agg->constructor, returning,
3940  args, aggfnoid, aggtype,
3942  agg->unique, agg->absent_on_null);
3943 }
@ JSCTOR_JSON_OBJECTAGG
Definition: primnodes.h:1688
JsonValueExpr * value
Definition: parsenodes.h:1879
JsonAggConstructor * constructor
Definition: parsenodes.h:1982
JsonKeyValue * arg
Definition: parsenodes.h:1983
bool absent_on_null
Definition: parsenodes.h:1984

References JsonObjectAgg::absent_on_null, JsonObjectAgg::arg, generate_unaccent_rules::args, JsonObjectAgg::constructor, JsonReturning::format, JsonFormat::format_type, InvalidOid, JS_FORMAT_DEFAULT, JS_FORMAT_JSONB, JSCTOR_JSON_OBJECTAGG, JsonKeyValue::key, sort-test::key, list_make2, JsonAggConstructor::output, transformExprRecurse(), transformJsonAggConstructor(), transformJsonConstructorOutput(), transformJsonValueExpr(), JsonObjectAgg::unique, val, and JsonKeyValue::value.

Referenced by transformExprRecurse().

◆ transformJsonObjectConstructor()

static Node * transformJsonObjectConstructor ( ParseState pstate,
JsonObjectConstructor ctor 
)
static

Definition at line 3698 of file parse_expr.c.

3699 {
3700  JsonReturning *returning;
3701  List *args = NIL;
3702 
3703  /* transform key-value pairs, if any */
3704  if (ctor->exprs)
3705  {
3706  ListCell *lc;
3707 
3708  /* transform and append key-value arguments */
3709  foreach(lc, ctor->exprs)
3710  {
3712  Node *key = transformExprRecurse(pstate, (Node *) kv->key);
3713  Node *val = transformJsonValueExpr(pstate, "JSON_OBJECT()",
3714  kv->value,
3716  InvalidOid, false);
3717 
3718  args = lappend(args, key);
3719  args = lappend(args, val);
3720  }
3721  }
3722 
3723  returning = transformJsonConstructorOutput(pstate, ctor->output, args);
3724 
3725  return makeJsonConstructorExpr(pstate, JSCTOR_JSON_OBJECT, args, NULL,
3726  returning, ctor->unique,
3727  ctor->absent_on_null, ctor->location);
3728 }
@ JSCTOR_JSON_OBJECT
Definition: primnodes.h:1686
JsonOutput * output
Definition: parsenodes.h:1927

References JsonObjectConstructor::absent_on_null, generate_unaccent_rules::args, castNode, JsonObjectConstructor::exprs, InvalidOid, JS_FORMAT_DEFAULT, JSCTOR_JSON_OBJECT, JsonKeyValue::key, sort-test::key, lappend(), lfirst, JsonObjectConstructor::location, makeJsonConstructorExpr(), NIL, JsonObjectConstructor::output, transformExprRecurse(), transformJsonConstructorOutput(), transformJsonValueExpr(), JsonObjectConstructor::unique, val, and JsonKeyValue::value.

Referenced by transformExprRecurse().

◆ transformJsonOutput()

static JsonReturning* transformJsonOutput ( ParseState pstate,
const JsonOutput output,
bool  allow_format 
)
static

Definition at line 3490 of file parse_expr.c.

3492 {
3493  JsonReturning *ret;
3494 
3495  /* if output clause is not specified, make default clause value */
3496  if (!output)
3497  {
3498  ret = makeNode(JsonReturning);
3499 
3501  ret->typid = InvalidOid;
3502  ret->typmod = -1;
3503 
3504  return ret;
3505  }
3506 
3507  ret = copyObject(output->returning);
3508 
3509  typenameTypeIdAndMod(pstate, output->typeName, &ret->typid, &ret->typmod);
3510 
3511  if (output->typeName->setof)
3512  ereport(ERROR,
3513  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3514  errmsg("returning SETOF types is not supported in SQL/JSON functions"));
3515 
3516  if (get_typtype(ret->typid) == TYPTYPE_PSEUDO)
3517  ereport(ERROR,
3518  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3519  errmsg("returning pseudo-types is not supported in SQL/JSON functions"));
3520 
3521  if (ret->format->format_type == JS_FORMAT_DEFAULT)
3522  /* assign JSONB format when returning jsonb, or JSON format otherwise */
3523  ret->format->format_type =
3524  ret->typid == JSONBOID ? JS_FORMAT_JSONB : JS_FORMAT_JSON;
3525  else
3526  checkJsonOutputFormat(pstate, ret->format, ret->typid, allow_format);
3527 
3528  return ret;
3529 }
JsonFormat * makeJsonFormat(JsonFormatType type, JsonEncoding encoding, int location)
Definition: makefuncs.c:847
static void checkJsonOutputFormat(ParseState *pstate, const JsonFormat *format, Oid targettype, bool allow_format_for_non_strings)
Definition: parse_expr.c:3440
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:310

References checkJsonOutputFormat(), copyObject, ereport, errcode(), errmsg(), ERROR, JsonReturning::format, JsonFormat::format_type, get_typtype(), InvalidOid, JS_ENC_DEFAULT, JS_FORMAT_DEFAULT, JS_FORMAT_JSON, JS_FORMAT_JSONB, makeJsonFormat(), makeNode, output, typenameTypeIdAndMod(), JsonReturning::typid, and JsonReturning::typmod.

Referenced by transformJsonConstructorOutput(), transformJsonFuncExpr(), transformJsonReturning(), and transformJsonSerializeExpr().

◆ transformJsonParseArg()

static Node* transformJsonParseArg ( ParseState pstate,
Node jsexpr,
JsonFormat format,
Oid exprtype 
)
static

Definition at line 4023 of file parse_expr.c.

4025 {
4026  Node *raw_expr = transformExprRecurse(pstate, jsexpr);
4027  Node *expr = raw_expr;
4028 
4029  *exprtype = exprType(expr);
4030 
4031  /* prepare input document */
4032  if (*exprtype == BYTEAOID)
4033  {
4034  JsonValueExpr *jve;
4035 
4036  expr = raw_expr;
4037  expr = makeJsonByteaToTextConversion(expr, format, exprLocation(expr));
4038  *exprtype = TEXTOID;
4039 
4040  jve = makeJsonValueExpr((Expr *) raw_expr, (Expr *) expr, format);
4041  expr = (Node *) jve;
4042  }
4043  else
4044  {
4045  char typcategory;
4046  bool typispreferred;
4047 
4048  get_type_category_preferred(*exprtype, &typcategory, &typispreferred);
4049 
4050  if (*exprtype == UNKNOWNOID || typcategory == TYPCATEGORY_STRING)
4051  {
4052  expr = coerce_to_target_type(pstate, (Node *) expr, *exprtype,
4053  TEXTOID, -1,
4055  COERCE_IMPLICIT_CAST, -1);
4056  *exprtype = TEXTOID;
4057  }
4058 
4059  if (format->encoding != JS_ENC_DEFAULT)
4060  ereport(ERROR,
4061  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4062  parser_errposition(pstate, format->location),
4063  errmsg("cannot use JSON FORMAT ENCODING clause for non-bytea input types")));
4064  }
4065 
4066  return expr;
4067 }
static Node * makeJsonByteaToTextConversion(Node *expr, JsonFormat *format, int location)
Definition: parse_expr.c:3256
@ COERCION_IMPLICIT
Definition: primnodes.h:714

References COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_IMPLICIT, ereport, errcode(), errmsg(), ERROR, exprLocation(), exprType(), format, get_type_category_preferred(), JS_ENC_DEFAULT, makeJsonByteaToTextConversion(), makeJsonValueExpr(), parser_errposition(), and transformExprRecurse().

Referenced by transformJsonIsPredicate(), and transformJsonParseExpr().

◆ transformJsonParseExpr()

static Node * transformJsonParseExpr ( ParseState pstate,
JsonParseExpr expr 
)
static

Definition at line 4135 of file parse_expr.c.

4136 {
4137  JsonOutput *output = jsexpr->output;
4138  JsonReturning *returning;
4139  Node *arg;
4140 
4141  returning = transformJsonReturning(pstate, output, "JSON()");
4142 
4143  if (jsexpr->unique_keys)
4144  {
4145  /*
4146  * Coerce string argument to text and then to json[b] in the executor
4147  * node with key uniqueness check.
4148  */
4149  JsonValueExpr *jve = jsexpr->expr;
4150  Oid arg_type;
4151 
4152  arg = transformJsonParseArg(pstate, (Node *) jve->raw_expr, jve->format,
4153  &arg_type);
4154 
4155  if (arg_type != TEXTOID)
4156  ereport(ERROR,
4157  (errcode(ERRCODE_DATATYPE_MISMATCH),
4158  errmsg("cannot use non-string types with WITH UNIQUE KEYS clause"),
4159  parser_errposition(pstate, jsexpr->location)));
4160  }
4161  else
4162  {
4163  /*
4164  * Coerce argument to target type using CAST for compatibility with PG
4165  * function-like CASTs.
4166  */
4167  arg = transformJsonValueExpr(pstate, "JSON()", jsexpr->expr,
4168  JS_FORMAT_JSON, returning->typid, false);
4169  }
4170 
4172  returning, jsexpr->unique_keys, false,
4173  jsexpr->location);
4174 }
static JsonReturning * transformJsonReturning(ParseState *pstate, JsonOutput *output, const char *fname)
Definition: parse_expr.c:4096
@ JSCTOR_JSON_PARSE
Definition: primnodes.h:1690
JsonFormat * format
Definition: primnodes.h:1681
Expr * raw_expr
Definition: primnodes.h:1679

References arg, ereport, errcode(), errmsg(), ERROR, JsonParseExpr::expr, JsonValueExpr::format, JS_FORMAT_JSON, JSCTOR_JSON_PARSE, list_make1, JsonParseExpr::location, makeJsonConstructorExpr(), JsonParseExpr::output, output, parser_errposition(), JsonValueExpr::raw_expr, transformJsonParseArg(), transformJsonReturning(), transformJsonValueExpr(), JsonReturning::typid, and JsonParseExpr::unique_keys.

Referenced by transformExprRecurse().

◆ transformJsonPassingArgs()

static void transformJsonPassingArgs ( ParseState pstate,
const char *  constructName,
JsonFormatType  format,
List args,
List **  passing_values,
List **  passing_names 
)
static

Definition at line 4484 of file parse_expr.c.

4487 {
4488  ListCell *lc;
4489 
4490  *passing_values = NIL;
4491  *passing_names = NIL;
4492 
4493  foreach(lc, args)
4494  {
4496  Node *expr = transformJsonValueExpr(pstate, constructName,
4497  arg->val, format,
4498  InvalidOid, true);
4499 
4500  *passing_values = lappend(*passing_values, expr);
4501  *passing_names = lappend(*passing_names, makeString(arg->name));
4502  }
4503 }

References arg, generate_unaccent_rules::args, castNode, format, InvalidOid, lappend(), lfirst, makeString(), NIL, and transformJsonValueExpr().

Referenced by transformJsonFuncExpr().

◆ transformJsonReturning()

static JsonReturning* transformJsonReturning ( ParseState pstate,
JsonOutput output,
const char *  fname 
)
static

Definition at line 4096 of file parse_expr.c.

4097 {
4098  JsonReturning *returning;
4099 
4100  if (output)
4101  {
4102  returning = transformJsonOutput(pstate, output, false);
4103 
4104  Assert(OidIsValid(returning->typid));
4105 
4106  if (returning->typid != JSONOID && returning->typid != JSONBOID)
4107  ereport(ERROR,
4108  (errcode(ERRCODE_DATATYPE_MISMATCH),
4109  errmsg("cannot use RETURNING type %s in %s",
4110  format_type_be(returning->typid), fname),
4111  parser_errposition(pstate, output->typeName->location)));
4112  }
4113  else
4114  {
4115  /* Output type is JSON by default. */
4116  Oid targettype = JSONOID;
4118 
4119  returning = makeNode(JsonReturning);
4120  returning->format = makeJsonFormat(format, JS_ENC_DEFAULT, -1);
4121  returning->typid = targettype;
4122  returning->typmod = -1;
4123  }
4124 
4125  return returning;
4126 }

References Assert, ereport, errcode(), errmsg(), ERROR, format, JsonReturning::format, format_type_be(), JS_ENC_DEFAULT, JS_FORMAT_JSON, makeJsonFormat(), makeNode, OidIsValid, output, parser_errposition(), transformJsonOutput(), JsonReturning::typid, and JsonReturning::typmod.

Referenced by transformJsonParseExpr(), and transformJsonScalarExpr().

◆ transformJsonScalarExpr()

static Node * transformJsonScalarExpr ( ParseState pstate,
JsonScalarExpr expr 
)
static

Definition at line 4184 of file parse_expr.c.

4185 {
4186  Node *arg = transformExprRecurse(pstate, (Node *) jsexpr->expr);
4187  JsonOutput *output = jsexpr->output;
4188  JsonReturning *returning;
4189 
4190  returning = transformJsonReturning(pstate, output, "JSON_SCALAR()");
4191 
4192  if (exprType(arg) == UNKNOWNOID)
4193  arg = coerce_to_specific_type(pstate, arg, TEXTOID, "JSON_SCALAR");
4194 
4196  returning, false, false, jsexpr->location);
4197 }
@ JSCTOR_JSON_SCALAR
Definition: primnodes.h:1691

References arg, coerce_to_specific_type(), JsonScalarExpr::expr, exprType(), JSCTOR_JSON_SCALAR, list_make1, JsonScalarExpr::location, makeJsonConstructorExpr(), JsonScalarExpr::output, output, transformExprRecurse(), and transformJsonReturning().

Referenced by transformExprRecurse().

◆ transformJsonSerializeExpr()

static Node * transformJsonSerializeExpr ( ParseState pstate,
JsonSerializeExpr expr 
)
static

Definition at line 4207 of file parse_expr.c.

4208 {
4209  JsonReturning *returning;
4210  Node *arg = transformJsonValueExpr(pstate, "JSON_SERIALIZE()",
4211  expr->expr,
4213  InvalidOid, false);
4214 
4215  if (expr->output)
4216  {
4217  returning = transformJsonOutput(pstate, expr->output, true);
4218 
4219  if (returning->typid != BYTEAOID)
4220  {
4221  char typcategory;
4222  bool typispreferred;
4223 
4224  get_type_category_preferred(returning->typid, &typcategory,
4225  &typispreferred);
4226  if (typcategory != TYPCATEGORY_STRING)
4227  ereport(ERROR,
4228  (errcode(ERRCODE_DATATYPE_MISMATCH),
4229  errmsg("cannot use RETURNING type %s in %s",
4230  format_type_be(returning->typid),
4231  "JSON_SERIALIZE()"),
4232  errhint("Try returning a string type or bytea.")));
4233  }
4234  }
4235  else
4236  {
4237  /* RETURNING TEXT FORMAT JSON is by default */
4238  returning = makeNode(JsonReturning);
4240  returning->typid = TEXTOID;
4241  returning->typmod = -1;
4242  }
4243 
4245  NULL, returning, false, false, expr->location);
4246 }
@ JSCTOR_JSON_SERIALIZE
Definition: primnodes.h:1692
JsonOutput * output
Definition: parsenodes.h:1915
JsonValueExpr * expr
Definition: parsenodes.h:1914

References arg, ereport, errcode(), errhint(), errmsg(), ERROR, JsonSerializeExpr::expr, JsonReturning::format, format_type_be(), get_type_category_preferred(), InvalidOid, JS_ENC_DEFAULT, JS_FORMAT_JSON, JSCTOR_JSON_SERIALIZE, list_make1, JsonSerializeExpr::location, makeJsonConstructorExpr(), makeJsonFormat(), makeNode, JsonSerializeExpr::output, transformJsonOutput(), transformJsonValueExpr(), JsonReturning::typid, and JsonReturning::typmod.

Referenced by transformExprRecurse().

◆ transformJsonValueExpr()

static Node* transformJsonValueExpr ( ParseState pstate,
const char *  constructName,
JsonValueExpr ve,
JsonFormatType  default_format,
Oid  targettype,
bool  isarg 
)
static

Definition at line 3278 of file parse_expr.c.

3281 {
3282  Node *expr = transformExprRecurse(pstate, (Node *) ve->raw_expr);
3283  Node *rawexpr;
3285  Oid exprtype;
3286  int location;
3287  char typcategory;
3288  bool typispreferred;
3289 
3290  if (exprType(expr) == UNKNOWNOID)
3291  expr = coerce_to_specific_type(pstate, expr, TEXTOID, constructName);
3292 
3293  rawexpr = expr;
3294  exprtype = exprType(expr);
3295  location = exprLocation(expr);
3296 
3297  get_type_category_preferred(exprtype, &typcategory, &typispreferred);
3298 
3299  if (ve->format->format_type != JS_FORMAT_DEFAULT)
3300  {
3301  if (ve->format->encoding != JS_ENC_DEFAULT && exprtype != BYTEAOID)
3302  ereport(ERROR,
3303  errcode(ERRCODE_DATATYPE_MISMATCH),
3304  errmsg("JSON ENCODING clause is only allowed for bytea input type"),
3305  parser_errposition(pstate, ve->format->location));
3306 
3307  if (exprtype == JSONOID || exprtype == JSONBOID)
3308  format = JS_FORMAT_DEFAULT; /* do not format json[b] types */
3309  else
3310  format = ve->format->format_type;
3311  }
3312  else if (isarg)
3313  {
3314  /*
3315  * Special treatment for PASSING arguments.
3316  *
3317  * Pass types supported by GetJsonPathVar() / JsonItemFromDatum()
3318  * directly without converting to json[b].
3319  */
3320  switch (exprtype)
3321  {
3322  case BOOLOID:
3323  case NUMERICOID:
3324  case INT2OID:
3325  case INT4OID:
3326  case INT8OID:
3327  case FLOAT4OID:
3328  case FLOAT8OID:
3329  case TEXTOID:
3330  case VARCHAROID:
3331  case DATEOID:
3332  case TIMEOID:
3333  case TIMETZOID:
3334  case TIMESTAMPOID:
3335  case TIMESTAMPTZOID:
3336  return expr;
3337 
3338  default:
3339  if (typcategory == TYPCATEGORY_STRING)
3340  return expr;
3341  /* else convert argument to json[b] type */
3342  break;
3343  }
3344 
3345  format = default_format;
3346  }
3347  else if (exprtype == JSONOID || exprtype == JSONBOID)
3348  format = JS_FORMAT_DEFAULT; /* do not format json[b] types */
3349  else
3350  format = default_format;
3351 
3352  if (format != JS_FORMAT_DEFAULT ||
3353  (OidIsValid(targettype) && exprtype != targettype))
3354  {
3355  Node *coerced;
3356  bool only_allow_cast = OidIsValid(targettype);
3357 
3358  /*
3359  * PASSING args are handled appropriately by GetJsonPathVar() /
3360  * JsonItemFromDatum().
3361  */
3362  if (!isarg &&
3363  !only_allow_cast &&
3364  exprtype != BYTEAOID && typcategory != TYPCATEGORY_STRING)
3365  ereport(ERROR,
3366  errcode(ERRCODE_DATATYPE_MISMATCH),
3368  errmsg("cannot use non-string types with implicit FORMAT JSON clause") :
3369  errmsg("cannot use non-string types with explicit FORMAT JSON clause"),
3370  parser_errposition(pstate, ve->format->location >= 0 ?
3371  ve->format->location : location));
3372 
3373  /* Convert encoded JSON text from bytea. */
3374  if (format == JS_FORMAT_JSON && exprtype == BYTEAOID)
3375  {
3376  expr = makeJsonByteaToTextConversion(expr, ve->format, location);
3377  exprtype = TEXTOID;
3378  }
3379 
3380  if (!OidIsValid(targettype))
3381  targettype = format == JS_FORMAT_JSONB ? JSONBOID : JSONOID;
3382 
3383  /* Try to coerce to the target type. */
3384  coerced = coerce_to_target_type(pstate, expr, exprtype,
3385  targettype, -1,
3388  location);
3389 
3390  if (!coerced)
3391  {
3392  /* If coercion failed, use to_json()/to_jsonb() functions. */
3393  FuncExpr *fexpr;
3394  Oid fnoid;
3395 
3396  /*
3397  * Though only allow a cast when the target type is specified by
3398  * the caller.
3399  */
3400  if (only_allow_cast)
3401  ereport(ERROR,
3402  (errcode(ERRCODE_CANNOT_COERCE),
3403  errmsg("cannot cast type %s to %s",
3404  format_type_be(exprtype),
3405  format_type_be(targettype)),
3406  parser_errposition(pstate, location)));
3407 
3408  fnoid = targettype == JSONOID ? F_TO_JSON : F_TO_JSONB;
3409  fexpr = makeFuncExpr(fnoid, targettype, list_make1(expr),
3411 
3412  fexpr->location = location;
3413 
3414  coerced = (Node *) fexpr;
3415  }
3416 
3417  if (coerced == expr)
3418  expr = rawexpr;
3419  else
3420  {
3421  ve = copyObject(ve);
3422  ve->raw_expr = (Expr *) rawexpr;
3423  ve->formatted_expr = (Expr *) coerced;
3424 
3425  expr = (Node *) ve;
3426  }
3427  }
3428 
3429  /* If returning a JsonValueExpr, formatted_expr must have been set. */
3430  Assert(!IsA(expr, JsonValueExpr) ||
3431  ((JsonValueExpr *) expr)->formatted_expr != NULL);
3432 
3433  return expr;
3434 }
Expr * formatted_expr
Definition: primnodes.h:1680

References Assert, COERCE_EXPLICIT_CALL, COERCE_EXPLICIT_CAST, coerce_to_specific_type(), coerce_to_target_type(), COERCION_EXPLICIT, copyObject, JsonFormat::encoding, ereport, errcode(), errmsg(), ERROR, exprLocation(), exprType(), format, JsonValueExpr::format, JsonFormat::format_type, format_type_be(), JsonValueExpr::formatted_expr, get_type_category_preferred(), InvalidOid, IsA, JS_ENC_DEFAULT, JS_FORMAT_DEFAULT, JS_FORMAT_JSON, JS_FORMAT_JSONB, list_make1, FuncExpr::location, JsonFormat::location, makeFuncExpr(), makeJsonByteaToTextConversion(), OidIsValid, parser_errposition(), JsonValueExpr::raw_expr, and transformExprRecurse().

Referenced by transformJsonArrayAgg(), transformJsonArrayConstructor(), transformJsonFuncExpr(), transformJsonObjectAgg(), transformJsonObjectConstructor(), transformJsonParseExpr(), transformJsonPassingArgs(), and transformJsonSerializeExpr().

◆ transformMergeSupportFunc()

static Node * transformMergeSupportFunc ( ParseState pstate,
MergeSupportFunc f 
)
static

Definition at line 1378 of file parse_expr.c.

1379 {
1380  /*
1381  * All we need to do is check that we're in the RETURNING list of a MERGE
1382  * command. If so, we just return the node as-is.
1383  */
1384  if (pstate->p_expr_kind != EXPR_KIND_MERGE_RETURNING)
1385  {
1386  ParseState *parent_pstate = pstate->parentParseState;
1387 
1388  while (parent_pstate &&
1389  parent_pstate->p_expr_kind != EXPR_KIND_MERGE_RETURNING)
1390  parent_pstate = parent_pstate->parentParseState;
1391 
1392  if (!parent_pstate)
1393  ereport(ERROR,
1394  errcode(ERRCODE_SYNTAX_ERROR),
1395  errmsg("MERGE_ACTION() can only be used in the RETURNING list of a MERGE command"),
1396  parser_errposition(pstate, f->location));
1397  }
1398 
1399  return (Node *) f;
1400 }
ParseLoc location
Definition: primnodes.h:636
ParseState * parentParseState
Definition: parse_node.h:192

References ereport, errcode(), errmsg(), ERROR, EXPR_KIND_MERGE_RETURNING, MergeSupportFunc::location, ParseState::p_expr_kind, ParseState::parentParseState, and parser_errposition().

Referenced by transformExprRecurse().

◆ transformMinMaxExpr()

static Node * transformMinMaxExpr ( ParseState pstate,
MinMaxExpr m 
)
static

Definition at line 2255 of file parse_expr.c.

2256 {
2257  MinMaxExpr *newm = makeNode(MinMaxExpr);
2258  List *newargs = NIL;
2259  List *newcoercedargs = NIL;
2260  const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
2261  ListCell *args;
2262 
2263  newm->op = m->op;
2264  foreach(args, m->args)
2265  {
2266  Node *e = (Node *) lfirst(args);
2267  Node *newe;
2268 
2269  newe = transformExprRecurse(pstate, e);
2270  newargs = lappend(newargs, newe);
2271  }
2272 
2273  newm->minmaxtype = select_common_type(pstate, newargs, funcname, NULL);
2274  /* minmaxcollid and inputcollid will be set by parse_collate.c */
2275 
2276  /* Convert arguments if necessary */
2277  foreach(args, newargs)
2278  {
2279  Node *e = (Node *) lfirst(args);
2280  Node *newe;
2281 
2282  newe = coerce_to_common_type(pstate, e,
2283  newm->minmaxtype,
2284  funcname);
2285  newcoercedargs = lappend(newcoercedargs, newe);
2286  }
2287 
2288  newm->args = newcoercedargs;
2289  newm->location = m->location;
2290  return (Node *) newm;
2291 }
#define funcname
Definition: indent_codes.h:69
@ IS_GREATEST
Definition: primnodes.h:1502
List * args
Definition: primnodes.h:1518
ParseLoc location
Definition: primnodes.h:1520
MinMaxOp op
Definition: primnodes.h:1516

References generate_unaccent_rules::args, MinMaxExpr::args, coerce_to_common_type(), funcname, IS_GREATEST, lappend(), lfirst, MinMaxExpr::location, makeNode, NIL, MinMaxExpr::op, select_common_type(), and transformExprRecurse().

Referenced by transformExprRecurse().

◆ transformMultiAssignRef()

static Node * transformMultiAssignRef ( ParseState pstate,
MultiAssignRef maref 
)
static

Definition at line 1484 of file parse_expr.c.

1485 {
1486  SubLink *sublink;
1487  RowExpr *rexpr;
1488  Query *qtree;
1489  TargetEntry *tle;
1490 
1491  /* We should only see this in first-stage processing of UPDATE tlists */
1493 
1494  /* We only need to transform the source if this is the first column */
1495  if (maref->colno == 1)
1496  {
1497  /*
1498  * For now, we only allow EXPR SubLinks and RowExprs as the source of
1499  * an UPDATE multiassignment. This is sufficient to cover interesting
1500  * cases; at worst, someone would have to write (SELECT * FROM expr)
1501  * to expand a composite-returning expression of another form.
1502  */
1503  if (IsA(maref->source, SubLink) &&
1504  ((SubLink *) maref->source)->subLinkType == EXPR_SUBLINK)
1505  {
1506  /* Relabel it as a MULTIEXPR_SUBLINK */
1507  sublink = (SubLink *) maref->source;
1508  sublink->subLinkType = MULTIEXPR_SUBLINK;
1509  /* And transform it */
1510  sublink = (SubLink *) transformExprRecurse(pstate,
1511  (Node *) sublink);
1512 
1513  qtree = castNode(Query, sublink->subselect);
1514 
1515  /* Check subquery returns required number of columns */
1516  if (count_nonjunk_tlist_entries(qtree->targetList) != maref->ncolumns)
1517  ereport(ERROR,
1518  (errcode(ERRCODE_SYNTAX_ERROR),
1519  errmsg("number of columns does not match number of values"),
1520  parser_errposition(pstate, sublink->location)));
1521 
1522  /*
1523  * Build a resjunk tlist item containing the MULTIEXPR SubLink,
1524  * and add it to pstate->p_multiassign_exprs, whence it will later
1525  * get appended to the completed targetlist. We needn't worry
1526  * about selecting a resno for it; transformUpdateStmt will do
1527  * that.
1528  */
1529  tle = makeTargetEntry((Expr *) sublink, 0, NULL, true);
1531  tle);
1532 
1533  /*
1534  * Assign a unique-within-this-targetlist ID to the MULTIEXPR
1535  * SubLink. We can just use its position in the
1536  * p_multiassign_exprs list.
1537  */
1538  sublink->subLinkId = list_length(pstate->p_multiassign_exprs);
1539  }
1540  else if (IsA(maref->source, RowExpr))
1541  {
1542  /* Transform the RowExpr, allowing SetToDefault items */
1543  rexpr = (RowExpr *) transformRowExpr(pstate,
1544  (RowExpr *) maref->source,
1545  true);
1546 
1547  /* Check it returns required number of columns */
1548  if (list_length(rexpr->args) != maref->ncolumns)
1549  ereport(ERROR,
1550  (errcode(ERRCODE_SYNTAX_ERROR),
1551  errmsg("number of columns does not match number of values"),
1552  parser_errposition(pstate, rexpr->location)));
1553 
1554  /*
1555  * Temporarily append it to p_multiassign_exprs, so we can get it
1556  * back when we come back here for additional columns.
1557  */
1558  tle = makeTargetEntry((Expr *) rexpr, 0, NULL, true);
1560  tle);
1561  }
1562  else
1563  ereport(ERROR,
1564  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1565  errmsg("source for a multiple-column UPDATE item must be a sub-SELECT or ROW() expression"),
1566  parser_errposition(pstate, exprLocation(maref->source))));
1567  }
1568  else
1569  {
1570  /*
1571  * Second or later column in a multiassignment. Re-fetch the
1572  * transformed SubLink or RowExpr, which we assume is still the last
1573  * entry in p_multiassign_exprs.
1574  */
1575  Assert(pstate->p_multiassign_exprs != NIL);
1576  tle = (TargetEntry *) llast(pstate->p_multiassign_exprs);
1577  }
1578 
1579  /*
1580  * Emit the appropriate output expression for the current column
1581  */
1582  if (IsA(tle->expr, SubLink))
1583  {
1584  Param *param;
1585 
1586  sublink = (SubLink *) tle->expr;
1587  Assert(sublink->subLinkType == MULTIEXPR_SUBLINK);
1588  qtree = castNode(Query, sublink->subselect);
1589 
1590  /* Build a Param representing the current subquery output column */
1591  tle = (TargetEntry *) list_nth(qtree->targetList, maref->colno - 1);
1592  Assert(!tle->resjunk);
1593 
1594  param = makeNode(Param);
1595  param->paramkind = PARAM_MULTIEXPR;
1596  param->paramid = (sublink->subLinkId << 16) | maref->colno;
1597  param->paramtype = exprType((Node *) tle->expr);
1598  param->paramtypmod = exprTypmod((Node *) tle->expr);
1599  param->paramcollid = exprCollation((Node *) tle->expr);
1600  param->location = exprLocation((Node *) tle->expr);
1601 
1602  return (Node *) param;
1603  }
1604 
1605  if (IsA(tle->expr, RowExpr))
1606  {
1607  Node *result;
1608 
1609  rexpr = (RowExpr *) tle->expr;
1610 
1611  /* Just extract and return the next element of the RowExpr */
1612  result = (Node *) list_nth(rexpr->args, maref->colno - 1);
1613 
1614  /*
1615  * If we're at the last column, delete the RowExpr from
1616  * p_multiassign_exprs; we don't need it anymore, and don't want it in
1617  * the finished UPDATE tlist. We assume this is still the last entry
1618  * in p_multiassign_exprs.
1619  */
1620  if (maref->colno == maref->ncolumns)
1621  pstate->p_multiassign_exprs =
1623 
1624  return result;
1625  }
1626 
1627  elog(ERROR, "unexpected expr type in multiassign list");
1628  return NULL; /* keep compiler quiet */
1629 }
List * list_delete_last(List *list)
Definition: list.c:957
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:240
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
@ MULTIEXPR_SUBLINK
Definition: primnodes.h:1002
@ PARAM_MULTIEXPR
Definition: primnodes.h:370
ParseLoc location
Definition: primnodes.h:384
List * p_multiassign_exprs
Definition: parse_node.h:213
ParseLoc location
Definition: primnodes.h:1435
Expr * expr
Definition: primnodes.h:2192

References RowExpr::args, Assert, castNode, MultiAssignRef::colno, count_nonjunk_tlist_entries(), elog, ereport, errcode(), errmsg(), ERROR, TargetEntry::expr, EXPR_KIND_UPDATE_SOURCE, EXPR_SUBLINK, exprCollation(), exprLocation(), exprType(), exprTypmod(), IsA, lappend(), list_delete_last(), list_length(), list_nth(), llast, Param::location, RowExpr::location, makeNode, makeTargetEntry(), MULTIEXPR_SUBLINK, MultiAssignRef::ncolumns, NIL, ParseState::p_expr_kind, ParseState::p_multiassign_exprs, PARAM_MULTIEXPR, Param::paramid, Param::paramkind, Param::paramtype, parser_errposition(), MultiAssignRef::source, SubLink::subLinkId, SubLink::subLinkType, SubLink::subselect, Query::targetList, transformExprRecurse(), and transformRowExpr().

Referenced by transformExprRecurse().

◆ transformParamRef()

static Node * transformParamRef ( ParseState pstate,
ParamRef pref 
)
static

Definition at line 887 of file parse_expr.c.

888 {
889  Node *result;
890 
891  /*
892  * The core parser knows nothing about Params. If a hook is supplied,
893  * call it. If not, or if the hook returns NULL, throw a generic error.
894  */
895  if (pstate->p_paramref_hook != NULL)
896  result = pstate->p_paramref_hook(pstate, pref);
897  else
898  result = NULL;
899 
900  if (result == NULL)
901  ereport(ERROR,
902  (errcode(ERRCODE_UNDEFINED_PARAMETER),
903  errmsg("there is no parameter $%d", pref->number),
904  parser_errposition(pstate, pref->location)));
905 
906  return result;
907 }
ParseLoc location
Definition: parsenodes.h:305
int number
Definition: parsenodes.h:304
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:237

References ereport, errcode(), errmsg(), ERROR, ParamRef::location, ParamRef::number, ParseState::p_paramref_hook, and parser_errposition().

Referenced by transformExprRecurse().

◆ transformRowExpr()

static Node * transformRowExpr ( ParseState pstate,
RowExpr r,
bool  allowDefault 
)
static

Definition at line 2168 of file parse_expr.c.

2169 {
2170  RowExpr *newr;
2171  char fname[16];
2172  int fnum;
2173 
2174  newr = makeNode(RowExpr);
2175 
2176  /* Transform the field expressions */
2177  newr->args = transformExpressionList(pstate, r->args,
2178  pstate->p_expr_kind, allowDefault);
2179 
2180  /* Disallow more columns than will fit in a tuple */
2182  ereport(ERROR,
2183  (errcode(ERRCODE_TOO_MANY_COLUMNS),
2184  errmsg("ROW expressions can have at most %d entries",
2186  parser_errposition(pstate, r->location)));
2187 
2188  /* Barring later casting, we consider the type RECORD */
2189  newr->row_typeid = RECORDOID;
2190  newr->row_format = COERCE_IMPLICIT_CAST;
2191 
2192  /* ROW() has anonymous columns, so invent some field names */
2193  newr->colnames = NIL;
2194  for (fnum = 1; fnum <= list_length(newr->args); fnum++)
2195  {
2196  snprintf(fname, sizeof(fname), "f%d", fnum);
2197  newr->colnames = lappend(newr->colnames, makeString(pstrdup(fname)));
2198  }
2199 
2200  newr->location = r->location;
2201 
2202  return (Node *) newr;
2203 }
#define MaxTupleAttributeNumber
Definition: htup_details.h:34
List * transformExpressionList(ParseState *pstate, List *exprlist, ParseExprKind exprKind, bool allowDefault)
Definition: parse_target.c:220
#define snprintf
Definition: port.h:238

References RowExpr::args, COERCE_IMPLICIT_CAST, ereport, errcode(), errmsg(), ERROR, lappend(), list_length(), RowExpr::location, makeNode, makeString(), MaxTupleAttributeNumber, NIL, ParseState::p_expr_kind, parser_errposition(), pstrdup(), snprintf, and transformExpressionList().

Referenced by transformExprRecurse(), and transformMultiAssignRef().

◆ transformSQLValueFunction()

static Node * transformSQLValueFunction ( ParseState pstate,
SQLValueFunction svf 
)
static

Definition at line 2294 of file parse_expr.c.

2295 {
2296  /*
2297  * All we need to do is insert the correct result type and (where needed)
2298  * validate the typmod, so we just modify the node in-place.
2299  */
2300  switch (svf->op)
2301  {
2302  case SVFOP_CURRENT_DATE:
2303  svf->type = DATEOID;
2304  break;
2305  case SVFOP_CURRENT_TIME:
2306  svf->type = TIMETZOID;
2307  break;
2308  case SVFOP_CURRENT_TIME_N:
2309  svf->type = TIMETZOID;
2310  svf->typmod = anytime_typmod_check(true, svf->typmod);
2311  break;
2313  svf->type = TIMESTAMPTZOID;
2314  break;
2316  svf->type = TIMESTAMPTZOID;
2317  svf->typmod = anytimestamp_typmod_check(true, svf->typmod);
2318  break;
2319  case SVFOP_LOCALTIME:
2320  svf->type = TIMEOID;
2321  break;
2322  case SVFOP_LOCALTIME_N:
2323  svf->type = TIMEOID;
2324  svf->typmod = anytime_typmod_check(false, svf->typmod);
2325  break;
2326  case SVFOP_LOCALTIMESTAMP:
2327  svf->type = TIMESTAMPOID;
2328  break;
2330  svf->type = TIMESTAMPOID;
2331  svf->typmod = anytimestamp_typmod_check(false, svf->typmod);
2332  break;
2333  case SVFOP_CURRENT_ROLE:
2334  case SVFOP_CURRENT_USER:
2335  case SVFOP_USER:
2336  case SVFOP_SESSION_USER:
2337  case SVFOP_CURRENT_CATALOG:
2338  case SVFOP_CURRENT_SCHEMA:
2339  svf->type = NAMEOID;
2340  break;
2341  }
2342 
2343  return (Node *) svf;
2344 }
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:123
int32 anytime_typmod_check(bool istz, int32 typmod)
Definition: date.c:71
@ SVFOP_CURRENT_CATALOG
Definition: primnodes.h:1549
@ SVFOP_LOCALTIME_N
Definition: primnodes.h:1542
@ SVFOP_CURRENT_TIMESTAMP
Definition: primnodes.h:1539
@ SVFOP_LOCALTIME
Definition: primnodes.h:1541
@ SVFOP_CURRENT_TIMESTAMP_N
Definition: primnodes.h:1540
@ SVFOP_CURRENT_ROLE
Definition: primnodes.h:1545
@ SVFOP_USER
Definition: primnodes.h:1547
@ SVFOP_CURRENT_SCHEMA
Definition: primnodes.h:1550
@ SVFOP_LOCALTIMESTAMP_N
Definition: primnodes.h:1544
@ SVFOP_CURRENT_DATE
Definition: primnodes.h:1536
@ SVFOP_CURRENT_TIME_N
Definition: primnodes.h:1538
@ SVFOP_CURRENT_TIME
Definition: primnodes.h:1537
@ SVFOP_LOCALTIMESTAMP
Definition: primnodes.h:1543
@ SVFOP_CURRENT_USER
Definition: primnodes.h:1546
@ SVFOP_SESSION_USER
Definition: primnodes.h:1548
SQLValueFunctionOp op
Definition: primnodes.h:1556

References anytime_typmod_check(), anytimestamp_typmod_check(), SQLValueFunction::op, SVFOP_CURRENT_CATALOG, SVFOP_CURRENT_DATE, SVFOP_CURRENT_ROLE, SVFOP_CURRENT_SCHEMA, SVFOP_CURRENT_TIME, SVFOP_CURRENT_TIME_N, SVFOP_CURRENT_TIMESTAMP, SVFOP_CURRENT_TIMESTAMP_N, SVFOP_CURRENT_USER, SVFOP_LOCALTIME, SVFOP_LOCALTIME_N, SVFOP_LOCALTIMESTAMP, SVFOP_LOCALTIMESTAMP_N, SVFOP_SESSION_USER, SVFOP_USER, and SQLValueFunction::typmod.

Referenced by transformExprRecurse().

◆ transformSubLink()

static Node * transformSubLink ( ParseState pstate,
SubLink sublink 
)
static

Definition at line 1772 of file parse_expr.c.

1773 {
1774  Node *result = (Node *) sublink;
1775  Query *qtree;
1776  const char *err;
1777 
1778  /*
1779  * Check to see if the sublink is in an invalid place within the query. We
1780  * allow sublinks everywhere in SELECT/INSERT/UPDATE/DELETE/MERGE, but
1781  * generally not in utility statements.
1782  */
1783  err = NULL;
1784  switch (pstate->p_expr_kind)
1785  {
1786  case EXPR_KIND_NONE:
1787  Assert(false); /* can't happen */
1788  break;
1789  case EXPR_KIND_OTHER:
1790  /* Accept sublink here; caller must throw error if wanted */
1791  break;
1792  case EXPR_KIND_JOIN_ON:
1793  case EXPR_KIND_JOIN_USING:
1796  case EXPR_KIND_WHERE:
1797  case EXPR_KIND_POLICY:
1798  case EXPR_KIND_HAVING:
1799  case EXPR_KIND_FILTER:
1809  case EXPR_KIND_MERGE_WHEN:
1810  case EXPR_KIND_GROUP_BY:
1811  case EXPR_KIND_ORDER_BY:
1812  case EXPR_KIND_DISTINCT_ON:
1813  case EXPR_KIND_LIMIT:
1814  case EXPR_KIND_OFFSET:
1815  case EXPR_KIND_RETURNING:
1817  case EXPR_KIND_VALUES:
1819  case EXPR_KIND_CYCLE_MARK:
1820  /* okay */
1821  break;
1824  err = _("cannot use subquery in check constraint");
1825  break;
1828  err = _("cannot use subquery in DEFAULT expression");
1829  break;
1831  err = _("cannot use subquery in index expression");
1832  break;
1834  err = _("cannot use subquery in index predicate");
1835  break;
1837  err = _("cannot use subquery in statistics expression");
1838  break;
1840  err = _("cannot use subquery in transform expression");
1841  break;
1843  err = _("cannot use subquery in EXECUTE parameter");
1844  break;
1846  err = _("cannot use subquery in trigger WHEN condition");
1847  break;
1849  err = _("cannot use subquery in partition bound");
1850  break;
1852  err = _("cannot use subquery in partition key expression");
1853  break;
1855  err = _("cannot use subquery in CALL argument");
1856  break;
1857  case EXPR_KIND_COPY_WHERE:
1858  err = _("cannot use subquery in COPY FROM WHERE condition");
1859  break;
1861  err = _("cannot use subquery in column generation expression");
1862  break;
1863 
1864  /*
1865  * There is intentionally no default: case here, so that the
1866  * compiler will warn if we add a new ParseExprKind without
1867  * extending this switch. If we do see an unrecognized value at
1868  * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
1869  * which is sane anyway.
1870  */
1871  }
1872  if (err)
1873  ereport(ERROR,
1874  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1875  errmsg_internal("%s", err),
1876  parser_errposition(pstate, sublink->location)));
1877 
1878  pstate->p_hasSubLinks = true;
1879 
1880  /*
1881  * OK, let's transform the sub-SELECT.
1882  */
1883  qtree = parse_sub_analyze(sublink->subselect, pstate, NULL, false, true);
1884 
1885  /*
1886  * Check that we got a SELECT. Anything else should be impossible given
1887  * restrictions of the grammar, but check anyway.
1888  */
1889  if (!IsA(qtree, Query) ||
1890  qtree->commandType != CMD_SELECT)
1891  elog(ERROR, "unexpected non-SELECT command in SubLink");
1892 
1893  sublink->subselect = (Node *) qtree;
1894 
1895  if (sublink->subLinkType == EXISTS_SUBLINK)
1896  {
1897  /*
1898  * EXISTS needs no test expression or combining operator. These fields
1899  * should be null already, but make sure.
1900  */
1901  sublink->testexpr = NULL;
1902  sublink->operName = NIL;
1903  }
1904  else if (sublink->subLinkType == EXPR_SUBLINK ||
1905  sublink->subLinkType == ARRAY_SUBLINK)
1906  {
1907  /*
1908  * Make sure the subselect delivers a single column (ignoring resjunk
1909  * targets).
1910  */
1911  if (count_nonjunk_tlist_entries(qtree->targetList) != 1)
1912  ereport(ERROR,
1913  (errcode(ERRCODE_SYNTAX_ERROR),
1914  errmsg("subquery must return only one column"),
1915  parser_errposition(pstate, sublink->location)));
1916 
1917  /*
1918  * EXPR and ARRAY need no test expression or combining operator. These
1919  * fields should be null already, but make sure.
1920  */
1921  sublink->testexpr = NULL;
1922  sublink->operName = NIL;
1923  }
1924  else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
1925  {
1926  /* Same as EXPR case, except no restriction on number of columns */
1927  sublink->testexpr = NULL;
1928  sublink->operName = NIL;
1929  }
1930  else
1931  {
1932  /* ALL, ANY, or ROWCOMPARE: generate row-comparing expression */
1933  Node *lefthand;
1934  List *left_list;
1935  List *right_list;
1936  ListCell *l;
1937 
1938  /*
1939  * If the source was "x IN (select)", convert to "x = ANY (select)".
1940  */
1941  if (sublink->operName == NIL)
1942  sublink->operName = list_make1(makeString("="));
1943 
1944  /*
1945  * Transform lefthand expression, and convert to a list
1946  */
1947  lefthand = transformExprRecurse(pstate, sublink->testexpr);
1948  if (lefthand && IsA(lefthand, RowExpr))
1949  left_list = ((RowExpr *) lefthand)->args;
1950  else
1951  left_list = list_make1(lefthand);
1952 
1953  /*
1954  * Build a list of PARAM_SUBLINK nodes representing the output columns
1955  * of the subquery.
1956  */
1957  right_list = NIL;
1958  foreach(l, qtree->targetList)
1959  {
1960  TargetEntry *tent = (TargetEntry *) lfirst(l);
1961  Param *param;
1962 
1963  if (tent->resjunk)
1964  continue;
1965 
1966  param = makeNode(Param);
1967  param->paramkind = PARAM_SUBLINK;
1968  param->paramid = tent->resno;
1969  param->paramtype = exprType((Node *) tent->expr);
1970  param->paramtypmod = exprTypmod((Node *) tent->expr);
1971  param->paramcollid = exprCollation((Node *) tent->expr);
1972  param->location = -1;
1973 
1974  right_list = lappend(right_list, param);
1975  }
1976 
1977  /*
1978  * We could rely on make_row_comparison_op to complain if the list
1979  * lengths differ, but we prefer to generate a more specific error
1980  * message.
1981  */
1982  if (list_length(left_list) < list_length(right_list))
1983  ereport(ERROR,
1984  (errcode(ERRCODE_SYNTAX_ERROR),
1985  errmsg("subquery has too many columns"),
1986  parser_errposition(pstate, sublink->location)));
1987  if (list_length(left_list) > list_length(right_list))
1988  ereport(ERROR,
1989  (errcode(ERRCODE_SYNTAX_ERROR),
1990  errmsg("subquery has too few columns"),
1991  parser_errposition(pstate, sublink->location)));
1992 
1993  /*
1994  * Identify the combining operator(s) and generate a suitable
1995  * row-comparison expression.
1996  */
1997  sublink->testexpr = make_row_comparison_op(pstate,
1998  sublink->operName,
1999  left_list,
2000  right_list,
2001  sublink->location);
2002  }
2003 
2004  return result;
2005 }
@ CMD_SELECT
Definition: nodes.h:265
Query * parse_sub_analyze(Node *parseTree, ParseState *parentParseState, CommonTableExpr *parentCTE, bool locked_from_parent, bool resolve_unknowns)
Definition: analyze.c:221
@ ARRAY_SUBLINK
Definition: primnodes.h:1003
@ EXISTS_SUBLINK
Definition: primnodes.h:997
@ PARAM_SUBLINK
Definition: primnodes.h:369
bool p_hasSubLinks
Definition: parse_node.h:226
CmdType commandType
Definition: parsenodes.h:121
AttrNumber resno
Definition: primnodes.h:2194

References _, ARRAY_SUBLINK, Assert, CMD_SELECT, Query::commandType, count_nonjunk_tlist_entries(), elog, ereport, err(), errcode(), errmsg(), errmsg_internal(), ERROR, EXISTS_SUBLINK, TargetEntry::expr, EXPR_KIND_ALTER_COL_TRANSFORM, EXPR_KIND_CALL_ARGUMENT, EXPR_KIND_CHECK_CONSTRAINT, EXPR_KIND_COLUMN_DEFAULT, EXPR_KIND_COPY_WHERE, EXPR_KIND_CYCLE_MARK, EXPR_KIND_DISTINCT_ON, EXPR_KIND_DOMAIN_CHECK, EXPR_KIND_EXECUTE_PARAMETER, EXPR_KIND_FILTER, EXPR_KIND_FROM_FUNCTION, EXPR_KIND_FROM_SUBSELECT, EXPR_KIND_FUNCTION_DEFAULT, EXPR_KIND_GENERATED_COLUMN, EXPR_KIND_GROUP_BY, EXPR_KIND_HAVING, EXPR_KIND_INDEX_EXPRESSION, EXPR_KIND_INDEX_PREDICATE, EXPR_KIND_INSERT_TARGET, EXPR_KIND_JOIN_ON, EXPR_KIND_JOIN_USING, EXPR_KIND_LIMIT, EXPR_KIND_MERGE_RETURNING, EXPR_KIND_MERGE_WHEN, EXPR_KIND_NONE, EXPR_KIND_OFFSET, EXPR_KIND_ORDER_BY, EXPR_KIND_OTHER, EXPR_KIND_PARTITION_BOUND, EXPR_KIND_PARTITION_EXPRESSION, EXPR_KIND_POLICY, EXPR_KIND_RETURNING, EXPR_KIND_SELECT_TARGET, EXPR_KIND_STATS_EXPRESSION, EXPR_KIND_TRIGGER_WHEN, EXPR_KIND_UPDATE_SOURCE, EXPR_KIND_UPDATE_TARGET, EXPR_KIND_VALUES, EXPR_KIND_VALUES_SINGLE, EXPR_KIND_WHERE, EXPR_KIND_WINDOW_FRAME_GROUPS, EXPR_KIND_WINDOW_FRAME_RANGE, EXPR_KIND_WINDOW_FRAME_ROWS, EXPR_KIND_WINDOW_ORDER, EXPR_KIND_WINDOW_PARTITION, EXPR_SUBLINK, exprCollation(), exprType(), exprTypmod(), IsA, lappend(), lfirst, list_length(), list_make1, Param::location, SubLink::location, make_row_comparison_op(), makeNode, makeString(), MULTIEXPR_SUBLINK, NIL, ParseState::p_expr_kind, ParseState::p_hasSubLinks, PARAM_SUBLINK, Param::paramid, Param::paramkind, Param::paramtype, parse_sub_analyze(), parser_errposition(), TargetEntry::resno, SubLink::subLinkType, SubLink::subselect, Query::targetList, SubLink::testexpr, and transformExprRecurse().

Referenced by transformExprRecurse().

◆ transformTypeCast()

static Node * transformTypeCast ( ParseState pstate,
TypeCast tc 
)
static

Definition at line 2684 of file parse_expr.c.

2685 {
2686  Node *result;
2687  Node *arg = tc->arg;
2688  Node *expr;
2689  Oid inputType;
2690  Oid targetType;
2691  int32 targetTypmod;
2692  int location;
2693 
2694  /* Look up the type name first */
2695  typenameTypeIdAndMod(pstate, tc->typeName, &targetType, &targetTypmod);
2696 
2697  /*
2698  * If the subject of the typecast is an ARRAY[] construct and the target
2699  * type is an array type, we invoke transformArrayExpr() directly so that
2700  * we can pass down the type information. This avoids some cases where
2701  * transformArrayExpr() might not infer the correct type. Otherwise, just
2702  * transform the argument normally.
2703  */
2704  if (IsA(arg, A_ArrayExpr))
2705  {
2706  Oid targetBaseType;
2707  int32 targetBaseTypmod;
2708  Oid elementType;
2709 
2710  /*
2711  * If target is a domain over array, work with the base array type
2712  * here. Below, we'll cast the array type to the domain. In the
2713  * usual case that the target is not a domain, the remaining steps
2714  * will be a no-op.
2715  */
2716  targetBaseTypmod = targetTypmod;
2717  targetBaseType = getBaseTypeAndTypmod(targetType, &targetBaseTypmod);
2718  elementType = get_element_type(targetBaseType);
2719  if (OidIsValid(elementType))
2720  {
2721  expr = transformArrayExpr(pstate,
2722  (A_ArrayExpr *) arg,
2723  targetBaseType,
2724  elementType,
2725  targetBaseTypmod);
2726  }
2727  else
2728  expr = transformExprRecurse(pstate, arg);
2729  }
2730  else
2731  expr = transformExprRecurse(pstate, arg);
2732 
2733  inputType = exprType(expr);
2734  if (inputType == InvalidOid)
2735  return expr; /* do nothing if NULL input */
2736 
2737  /*
2738  * Location of the coercion is preferentially the location of the :: or
2739  * CAST symbol, but if there is none then use the location of the type
2740  * name (this can happen in TypeName 'string' syntax, for instance).
2741  */
2742  location = tc->location;
2743  if (location < 0)
2744  location = tc->typeName->location;
2745 
2746  result = coerce_to_target_type(pstate, expr, inputType,
2747  targetType, targetTypmod,
2750  location);
2751  if (result == NULL)
2752  ereport(ERROR,
2753  (errcode(ERRCODE_CANNOT_COERCE),
2754  errmsg("cannot cast type %s to %s",
2755  format_type_be(inputType),
2756  format_type_be(targetType)),
2757  parser_coercion_errposition(pstate, location, expr)));
2758 
2759  return result;
2760 }
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition: lsyscache.c:2538
TypeName * typeName
Definition: parsenodes.h:374
ParseLoc location
Definition: parsenodes.h:375
Node * arg
Definition: parsenodes.h:373
ParseLoc location
Definition: parsenodes.h:275

References arg, TypeCast::arg, COERCE_EXPLICIT_CAST, coerce_to_target_type(), COERCION_EXPLICIT, ereport, errcode(), errmsg(), ERROR, exprType(), format_type_be(), get_element_type(), getBaseTypeAndTypmod(), InvalidOid, IsA, TypeName::location, TypeCast::location, OidIsValid, parser_coercion_errposition(), transformArrayExpr(), transformExprRecurse(), TypeCast::typeName, and typenameTypeIdAndMod().

Referenced by transformExprRecurse().

◆ transformWholeRowRef()

static Node * transformWholeRowRef ( ParseState pstate,
ParseNamespaceItem nsitem,
int  sublevels_up,
int  location 
)
static

Definition at line 2612 of file parse_expr.c.

2614 {
2615  /*
2616  * Build the appropriate referencing node. Normally this can be a
2617  * whole-row Var, but if the nsitem is a JOIN USING alias then it contains
2618  * only a subset of the columns of the underlying join RTE, so that will
2619  * not work. Instead we immediately expand the reference into a RowExpr.
2620  * Since the JOIN USING's common columns are fully determined at this
2621  * point, there seems no harm in expanding it now rather than during
2622  * planning.
2623  *
2624  * Note that if the RTE is a function returning scalar, we create just a
2625  * plain reference to the function value, not a composite containing a
2626  * single column. This is pretty inconsistent at first sight, but it's
2627  * what we've done historically. One argument for it is that "rel" and
2628  * "rel.*" mean the same thing for composite relations, so why not for
2629  * scalar functions...
2630  */
2631  if (nsitem->p_names == nsitem->p_rte->eref)
2632  {
2633  Var *result;
2634 
2635  result = makeWholeRowVar(nsitem->p_rte, nsitem->p_rtindex,
2636  sublevels_up, true);
2637 
2638  /* location is not filled in by makeWholeRowVar */
2639  result->location = location;
2640 
2641  /* mark Var if it's nulled by any outer joins */
2642  markNullableIfNeeded(pstate, result);
2643 
2644  /* mark relation as requiring whole-row SELECT access */
2645  markVarForSelectPriv(pstate, result);
2646 
2647  return (Node *) result;
2648  }
2649  else
2650  {
2651  RowExpr *rowexpr;
2652  List *fields;
2653 
2654  /*
2655  * We want only as many columns as are listed in p_names->colnames,
2656  * and we should use those names not whatever possibly-aliased names
2657  * are in the RTE. We needn't worry about marking the RTE for SELECT
2658  * access, as the common columns are surely so marked already.
2659  */
2660  expandRTE(nsitem->p_rte, nsitem->p_rtindex,
2661  sublevels_up, location, false,
2662  NULL, &fields);
2663  rowexpr = makeNode(RowExpr);
2664  rowexpr->args = list_truncate(fields,
2665  list_length(nsitem->p_names->colnames));
2666  rowexpr->row_typeid = RECORDOID;
2667  rowexpr->row_format = COERCE_IMPLICIT_CAST;
2668  rowexpr->colnames = copyObject(nsitem->p_names->colnames);
2669  rowexpr->location = location;
2670 
2671  /* XXX we ought to mark the row as possibly nullable */
2672 
2673  return (Node *) rowexpr;
2674  }
2675 }
List * list_truncate(List *list, int new_size)
Definition: list.c:631
Var * makeWholeRowVar(RangeTblEntry *rte, int varno, Index varlevelsup, bool allowScalar)
Definition: makefuncs.c:135
void markNullableIfNeeded(ParseState *pstate, Var *var)
void markVarForSelectPriv(ParseState *pstate, Var *var)
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, int location, bool include_dropped, List **colnames, List **colvars)
RangeTblEntry * p_rte
Definition: parse_node.h:287
Definition: primnodes.h:248
ParseLoc location
Definition: primnodes.h:293

References RowExpr::args, COERCE_IMPLICIT_CAST, Alias::colnames, copyObject, expandRTE(), list_length(), list_truncate(), Var::location, RowExpr::location, makeNode, makeWholeRowVar(), markNullableIfNeeded(), markVarForSelectPriv(), ParseNamespaceItem::p_names, ParseNamespaceItem::p_rte, and ParseNamespaceItem::p_rtindex.

Referenced by transformColumnRef().

◆ transformXmlExpr()

static Node * transformXmlExpr ( ParseState pstate,
XmlExpr x 
)
static

Definition at line 2347 of file parse_expr.c.

2348 {
2349  XmlExpr *newx;
2350  ListCell *lc;
2351  int i;
2352 
2353  newx = makeNode(XmlExpr);
2354  newx->op = x->op;
2355  if (x->name)
2356  newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
2357  else
2358  newx->name = NULL;
2359  newx->xmloption = x->xmloption;
2360  newx->type = XMLOID; /* this just marks the node as transformed */
2361  newx->typmod = -1;
2362  newx->location = x->location;
2363 
2364  /*
2365  * gram.y built the named args as a list of ResTarget. Transform each,
2366  * and break the names out as a separate list.
2367  */
2368  newx->named_args = NIL;
2369  newx->arg_names = NIL;
2370 
2371  foreach(lc, x->named_args)
2372  {
2373  ResTarget *r = lfirst_node(ResTarget, lc);
2374  Node *expr;
2375  char *argname;
2376 
2377  expr = transformExprRecurse(pstate, r->val);
2378 
2379  if (r->name)
2380  argname = map_sql_identifier_to_xml_name(r->name, false, false);
2381  else if (IsA(r->val, ColumnRef))
2383  true, false);
2384  else
2385  {
2386  ereport(ERROR,
2387  (errcode(ERRCODE_SYNTAX_ERROR),
2388  x->op == IS_XMLELEMENT
2389  ? errmsg("unnamed XML attribute value must be a column reference")
2390  : errmsg("unnamed XML element value must be a column reference"),
2391  parser_errposition(pstate, r->location)));
2392  argname = NULL; /* keep compiler quiet */
2393  }
2394 
2395  /* reject duplicate argnames in XMLELEMENT only */
2396  if (x->op == IS_XMLELEMENT)
2397  {
2398  ListCell *lc2;
2399 
2400  foreach(lc2, newx->arg_names)
2401  {
2402  if (strcmp(argname, strVal(lfirst(lc2))) == 0)
2403  ereport(ERROR,
2404  (errcode(ERRCODE_SYNTAX_ERROR),
2405  errmsg("XML attribute name \"%s\" appears more than once",
2406  argname),
2407  parser_errposition(pstate, r->location)));
2408  }
2409  }
2410 
2411  newx->named_args = lappend(newx->named_args, expr);
2412  newx->arg_names = lappend(newx->arg_names, makeString(argname));
2413  }
2414 
2415  /* The other arguments are of varying types depending on the function */
2416  newx->args = NIL;
2417  i = 0;
2418  foreach(lc, x->args)
2419  {
2420  Node *e = (Node *) lfirst(lc);
2421  Node *newe;
2422 
2423  newe = transformExprRecurse(pstate, e);
2424  switch (x->op)
2425  {
2426  case IS_XMLCONCAT:
2427  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2428  "XMLCONCAT");
2429  break;
2430  case IS_XMLELEMENT:
2431  /* no coercion necessary */
2432  break;
2433  case IS_XMLFOREST:
2434  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2435  "XMLFOREST");
2436  break;
2437  case IS_XMLPARSE:
2438  if (i == 0)
2439  newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2440  "XMLPARSE");
2441  else
2442  newe = coerce_to_boolean(pstate, newe, "XMLPARSE");
2443  break;
2444  case IS_XMLPI:
2445  newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2446  "XMLPI");
2447  break;
2448  case IS_XMLROOT:
2449  if (i == 0)
2450  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2451  "XMLROOT");
2452  else if (i == 1)
2453  newe = coerce_to_specific_type(pstate, newe, TEXTOID,
2454  "XMLROOT");
2455  else
2456  newe = coerce_to_specific_type(pstate, newe, INT4OID,
2457  "XMLROOT");
2458  break;
2459  case IS_XMLSERIALIZE:
2460  /* not handled here */
2461  Assert(false);
2462  break;
2463  case IS_DOCUMENT:
2464  newe = coerce_to_specific_type(pstate, newe, XMLOID,
2465  "IS DOCUMENT");
2466  break;
2467  }
2468  newx->args = lappend(newx->args, newe);
2469  i++;
2470  }
2471 
2472  return (Node *) newx;
2473 }
int x
Definition: isn.c:71
char * FigureColname(Node *node)
@ IS_DOCUMENT
Definition: primnodes.h:1587
@ IS_XMLFOREST
Definition: primnodes.h:1582
@ IS_XMLCONCAT
Definition: primnodes.h:1580
@ IS_XMLPI
Definition: primnodes.h:1584
@ IS_XMLPARSE
Definition: primnodes.h:1583
@ IS_XMLSERIALIZE
Definition: primnodes.h:1586
@ IS_XMLROOT
Definition: primnodes.h:1585
@ IS_XMLELEMENT
Definition: primnodes.h:1581
List * args
Definition: primnodes.h:1608
ParseLoc location
Definition: primnodes.h:1617
List * named_args
Definition: primnodes.h:1604
XmlExprOp op
Definition: primnodes.h:1600
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition: xml.c:2315

References XmlExpr::args, Assert, coerce_to_boolean(), coerce_to_specific_type(), ereport, errcode(), errmsg(), ERROR, FigureColname(), i, IS_DOCUMENT, IS_XMLCONCAT, IS_XMLELEMENT, IS_XMLFOREST, IS_XMLPARSE, IS_XMLPI, IS_XMLROOT, IS_XMLSERIALIZE, IsA, lappend(), lfirst, lfirst_node, ResTarget::location, XmlExpr::location, makeNode, makeString(), map_sql_identifier_to_xml_name(), ResTarget::name, XmlExpr::named_args, NIL, XmlExpr::op, parser_errposition(), strVal, transformExprRecurse(), ResTarget::val, and x.

Referenced by transformExprRecurse().

◆ transformXmlSerialize()

static Node * transformXmlSerialize ( ParseState pstate,
XmlSerialize xs 
)
static

Definition at line 2476 of file parse_expr.c.

2477 {
2478  Node *result;
2479  XmlExpr *xexpr;
2480  Oid targetType;
2481  int32 targetTypmod;
2482 
2483  xexpr = makeNode(XmlExpr);
2484  xexpr->op = IS_XMLSERIALIZE;
2485  xexpr->args = list_make1(coerce_to_specific_type(pstate,
2486  transformExprRecurse(pstate, xs->expr),
2487  XMLOID,
2488  "XMLSERIALIZE"));
2489 
2490  typenameTypeIdAndMod(pstate, xs->typeName, &targetType, &targetTypmod);
2491 
2492  xexpr->xmloption = xs->xmloption;
2493  xexpr->indent = xs->indent;
2494  xexpr->location = xs->location;
2495  /* We actually only need these to be able to parse back the expression. */
2496  xexpr->type = targetType;
2497  xexpr->typmod = targetTypmod;
2498 
2499  /*
2500  * The actual target type is determined this way. SQL allows char and
2501  * varchar as target types. We allow anything that can be cast implicitly
2502  * from text. This way, user-defined text-like data types automatically
2503  * fit in.
2504  */
2505  result = coerce_to_target_type(pstate, (Node *) xexpr,
2506  TEXTOID, targetType, targetTypmod,
2509  -1);
2510  if (result == NULL)
2511  ereport(ERROR,
2512  (errcode(ERRCODE_CANNOT_COERCE),
2513  errmsg("cannot cast XMLSERIALIZE result to %s",
2514  format_type_be(targetType)),
2515  parser_errposition(pstate, xexpr->location)));
2516  return result;
2517 }
bool indent
Definition: primnodes.h:1612
ParseLoc location
Definition: parsenodes.h:849
TypeName * typeName
Definition: parsenodes.h:847
Node * expr
Definition: parsenodes.h:846
XmlOptionType xmloption
Definition: parsenodes.h:845

References XmlExpr::args, COERCE_IMPLICIT_CAST, coerce_to_specific_type(), coerce_to_target_type(), COERCION_IMPLICIT, ereport, errcode(), errmsg(), ERROR, XmlSerialize::expr, format_type_be(), XmlSerialize::indent, XmlExpr::indent, IS_XMLSERIALIZE, list_make1, XmlSerialize::location, XmlExpr::location, makeNode, XmlExpr::op, parser_errposition(), transformExprRecurse(), XmlSerialize::typeName, typenameTypeIdAndMod(), and XmlSerialize::xmloption.

Referenced by transformExprRecurse().

◆ unknown_attribute()

static void unknown_attribute ( ParseState pstate,
Node relref,
const char *  attname,
int  location 
)
static

Definition at line 393 of file parse_expr.c.

395 {
396  RangeTblEntry *rte;
397 
398  if (IsA(relref, Var) &&
399  ((Var *) relref)->varattno == InvalidAttrNumber)
400  {
401  /* Reference the RTE by alias not by actual table name */
402  rte = GetRTEByRangeTablePosn(pstate,
403  ((Var *) relref)->varno,
404  ((Var *) relref)->varlevelsup);
405  ereport(ERROR,
406  (errcode(ERRCODE_UNDEFINED_COLUMN),
407  errmsg("column %s.%s does not exist",
408  rte->eref->aliasname, attname),
409  parser_errposition(pstate, location)));
410  }
411  else
412  {
413  /* Have to do it by reference to the type of the expression */
414  Oid relTypeId = exprType(relref);
415 
416  if (ISCOMPLEX(relTypeId))
417  ereport(ERROR,
418  (errcode(ERRCODE_UNDEFINED_COLUMN),
419  errmsg("column \"%s\" not found in data type %s",
420  attname, format_type_be(relTypeId)),
421  parser_errposition(pstate, location)));
422  else if (relTypeId == RECORDOID)
423  ereport(ERROR,
424  (errcode(ERRCODE_UNDEFINED_COLUMN),
425  errmsg("could not identify column \"%s\" in record data type",
426  attname),
427  parser_errposition(pstate, location)));
428  else
429  ereport(ERROR,
430  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
431  errmsg("column notation .%s applied to type %s, "
432  "which is not a composite type",
433  attname, format_type_be(relTypeId)),
434  parser_errposition(pstate, location)));
435  }
436 }
#define InvalidAttrNumber
Definition: attnum.h:23
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
#define ISCOMPLEX(typeid)
Definition: parse_type.h:59
NameData attname
Definition: pg_attribute.h:41

References attname, ereport, errcode(), errmsg(), ERROR, exprType(), format_type_be(), GetRTEByRangeTablePosn(), InvalidAttrNumber, IsA, ISCOMPLEX, and parser_errposition().

Referenced by transformIndirection().

◆ ValidJsonBehaviorDefaultExpr()

static bool ValidJsonBehaviorDefaultExpr ( Node expr,
void *  context 
)
static

Definition at line 4625 of file parse_expr.c.

4626 {
4627  if (expr == NULL)
4628  return false;
4629 
4630  switch (nodeTag(expr))
4631  {
4632  /* Acceptable expression nodes */
4633  case T_Const:
4634  case T_FuncExpr:
4635  case T_OpExpr:
4636  return true;
4637 
4638  /* Acceptable iff arg of the following nodes is one of the above */
4639  case T_CoerceViaIO:
4640  case T_CoerceToDomain:
4641  case T_ArrayCoerceExpr:
4642  case T_ConvertRowtypeExpr:
4643  case T_RelabelType:
4644  case T_CollateExpr:
4646  context);
4647  default:
4648  break;
4649  }
4650 
4651  return false;
4652 }
#define expression_tree_walker(n, w, c)
Definition: nodeFuncs.h:151
tree context
Definition: radixtree.h:1833

References context, expression_tree_walker, and nodeTag.

Referenced by transformJsonBehavior().

Variable Documentation

◆ Transform_null_equals

bool Transform_null_equals = false

Definition at line 46 of file parse_expr.c.

Referenced by transformAExprOp().