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:1319
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#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:1608
@ JS_FORMAT_JSON
Definition: primnodes.h:1609
JsonEncoding
Definition: primnodes.h:1595
@ JS_ENC_DEFAULT
Definition: primnodes.h:1596
@ JS_ENC_UTF8
Definition: primnodes.h:1597

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 4509 of file parse_expr.c.

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

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 4743 of file parse_expr.c.

4744 {
4745  Datum val = (Datum) 0;
4746  Oid typid = JSONBOID;
4747  int len = -1;
4748  bool isbyval = false;
4749  bool isnull = false;
4750  Const *con;
4751 
4752  switch (btype)
4753  {
4756  break;
4757 
4760  break;
4761 
4762  case JSON_BEHAVIOR_TRUE:
4763  val = BoolGetDatum(true);
4764  typid = BOOLOID;
4765  len = sizeof(bool);
4766  isbyval = true;
4767  break;
4768 
4769  case JSON_BEHAVIOR_FALSE:
4770  val = BoolGetDatum(false);
4771  typid = BOOLOID;
4772  len = sizeof(bool);
4773  isbyval = true;
4774  break;
4775 
4776  case JSON_BEHAVIOR_NULL:
4777  case JSON_BEHAVIOR_UNKNOWN:
4778  case JSON_BEHAVIOR_EMPTY:
4779  val = (Datum) 0;
4780  isnull = true;
4781  typid = INT4OID;
4782  len = sizeof(int32);
4783  isbyval = true;
4784  break;
4785 
4786  /* These two behavior types are handled by the caller. */
4787  case JSON_BEHAVIOR_DEFAULT:
4788  case JSON_BEHAVIOR_ERROR:
4789  Assert(false);
4790  break;
4791 
4792  default:
4793  elog(ERROR, "unrecognized SQL/JSON behavior %d", btype);
4794  break;
4795  }
4796 
4797  con = makeConst(typid, -1, InvalidOid, len, val, isnull, isbyval);
4798  con->location = location;
4799 
4800  return (Node *) con;
4801 }
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:1732
@ JSON_BEHAVIOR_TRUE
Definition: primnodes.h:1734
@ JSON_BEHAVIOR_DEFAULT
Definition: primnodes.h:1739
@ JSON_BEHAVIOR_EMPTY
Definition: primnodes.h:1733
@ JSON_BEHAVIOR_FALSE
Definition: primnodes.h:1735
@ JSON_BEHAVIOR_NULL
Definition: primnodes.h:1731
@ JSON_BEHAVIOR_EMPTY_OBJECT
Definition: primnodes.h:1738
@ JSON_BEHAVIOR_UNKNOWN
Definition: primnodes.h:1736
@ JSON_BEHAVIOR_EMPTY_ARRAY
Definition: primnodes.h:1737

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:1599
@ JS_ENC_UTF16
Definition: primnodes.h:1598
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:1924
@ IS_NOT_NULL
Definition: primnodes.h:1924
ParseLoc location
Definition: parsenodes.h:338
A_Expr_Kind kind
Definition: parsenodes.h:334
NullTestType nulltesttype
Definition: primnodes.h:1931
ParseLoc location
Definition: primnodes.h:1934
Expr * arg
Definition: primnodes.h:1930

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:1205
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:901
@ OR_EXPR
Definition: primnodes.h:901
RowCompareType
Definition: primnodes.h:1423
@ ROWCOMPARE_NE
Definition: primnodes.h:1430
@ ROWCOMPARE_EQ
Definition: primnodes.h:1427
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:1438
#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:1381

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:1610
JsonReturning * returning
Definition: primnodes.h:1676
JsonConstructorType type
Definition: primnodes.h:1672
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:901

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:1354
List * elements
Definition: primnodes.h:1350
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:806

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:971
@ ROWCOMPARE_SUBLINK
Definition: primnodes.h:970

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:1948
@ IS_NOT_FALSE
Definition: primnodes.h:1948
@ IS_NOT_UNKNOWN
Definition: primnodes.h:1948
@ IS_TRUE
Definition: primnodes.h:1948
@ IS_UNKNOWN
Definition: primnodes.h:1948
@ IS_FALSE
Definition: primnodes.h:1948

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:1283
ParseLoc location
Definition: primnodes.h:1286
Expr * defresult
Definition: primnodes.h:1285
List * args
Definition: primnodes.h:1284
Expr * result
Definition: primnodes.h:1296
Expr * expr
Definition: primnodes.h:1295
ParseLoc location
Definition: primnodes.h:1297

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:1462
ParseLoc location
Definition: primnodes.h:1464

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:1249
ParseLoc location
Definition: primnodes.h:1251

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:3153
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1159
#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:2070
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:3992
static Node * transformJsonScalarExpr(ParseState *pstate, JsonScalarExpr *expr)
Definition: parse_expr.c:4183
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:3952
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:4134
static Node * transformIndirection(ParseState *pstate, A_Indirection *ind)
Definition: parse_expr.c:439
static Node * transformJsonSerializeExpr(ParseState *pstate, JsonSerializeExpr *expr)
Definition: parse_expr.c:4206
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:4072
static Node * transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *p)
Definition: parse_expr.c:4252
static Node * transformJsonObjectAgg(ParseState *pstate, JsonObjectAgg *agg)
Definition: parse_expr.c:3890
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:761

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  /* winref will be set by transformWindowFuncCall */
3830  wfunc->winstar = false;
3831  wfunc->winagg = true;
3832  wfunc->location = agg_ctor->location;
3833 
3834  /*
3835  * ordered aggs not allowed in windows yet
3836  */
3837  if (agg_ctor->agg_order != NIL)
3838  ereport(ERROR,
3839  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3840  errmsg("aggregate ORDER BY is not implemented for window functions"),
3841  parser_errposition(pstate, agg_ctor->location));
3842 
3843  /* parse_agg.c does additional window-func-specific processing */
3844  transformWindowFuncCall(pstate, wfunc, agg_ctor->over);
3845 
3846  node = (Node *) wfunc;
3847  }
3848  else
3849  {
3850  Aggref *aggref = makeNode(Aggref);
3851 
3852  aggref->aggfnoid = aggfnoid;
3853  aggref->aggtype = aggtype;
3854 
3855  /* aggcollid and inputcollid will be set by parse_collate.c */
3856  /* aggtranstype will be set by planner */
3857  /* aggargtypes will be set by transformAggregateCall */
3858  /* aggdirectargs and args will be set by transformAggregateCall */
3859  /* aggorder and aggdistinct will be set by transformAggregateCall */
3860  aggref->aggfilter = aggfilter;
3861  aggref->aggstar = false;
3862  aggref->aggvariadic = false;
3863  aggref->aggkind = AGGKIND_NORMAL;
3864  aggref->aggpresorted = false;
3865  /* agglevelsup will be set by transformAggregateCall */
3866  aggref->aggsplit = AGGSPLIT_SIMPLE; /* planner might change this */
3867  aggref->aggno = -1; /* planner will set aggno and aggtransno */
3868  aggref->aggtransno = -1;
3869  aggref->location = agg_ctor->location;
3870 
3871  transformAggregateCall(pstate, aggref, args, agg_ctor->agg_order, false);
3872 
3873  node = (Node *) aggref;
3874  }
3875 
3876  return makeJsonConstructorExpr(pstate, ctor_type, NIL, (Expr *) node,
3877  returning, unique, absent_on_null,
3878  agg_ctor->location);
3879 }
@ 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:1973
List * args
Definition: primnodes.h:575
Expr * aggfilter
Definition: primnodes.h:577
ParseLoc location
Definition: primnodes.h:585
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 3952 of file parse_expr.c.

3953 {
3954  JsonReturning *returning;
3955  Node *arg;
3956  Oid aggfnoid;
3957  Oid aggtype;
3958 
3959  arg = transformJsonValueExpr(pstate, "JSON_ARRAYAGG()", agg->arg,
3960  JS_FORMAT_DEFAULT, InvalidOid, false);
3961 
3962  returning = transformJsonConstructorOutput(pstate, agg->constructor->output,
3963  list_make1(arg));
3964 
3965  if (returning->format->format_type == JS_FORMAT_JSONB)
3966  {
3967  aggfnoid = agg->absent_on_null ? F_JSONB_AGG_STRICT : F_JSONB_AGG;
3968  aggtype = JSONBOID;
3969  }
3970  else
3971  {
3972  aggfnoid = agg->absent_on_null ? F_JSON_AGG_STRICT : F_JSON_AGG;
3973  aggtype = JSONOID;
3974  }
3975 
3976  return transformJsonAggConstructor(pstate, agg->constructor, returning,
3977  list_make1(arg), aggfnoid, aggtype,
3979  false, agg->absent_on_null);
3980 }
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:1659
JsonOutput * output
Definition: parsenodes.h:1970
bool absent_on_null
Definition: parsenodes.h:1999
JsonValueExpr * arg
Definition: parsenodes.h:1998
JsonAggConstructor * constructor
Definition: parsenodes.h:1997

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 3992 of file parse_expr.c.

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

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 4657 of file parse_expr.c.

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

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

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 4072 of file parse_expr.c.

4073 {
4074  Oid exprtype;
4075  Node *expr = transformJsonParseArg(pstate, pred->expr, pred->format,
4076  &exprtype);
4077 
4078  /* make resulting expression */
4079  if (exprtype != TEXTOID && exprtype != JSONOID && exprtype != JSONBOID)
4080  ereport(ERROR,
4081  (errcode(ERRCODE_DATATYPE_MISMATCH),
4082  errmsg("cannot use type %s in IS JSON predicate",
4083  format_type_be(exprtype))));
4084 
4085  /* This intentionally(?) drops the format clause. */
4086  return makeJsonIsPredicate(expr, NULL, pred->item_type,
4087  pred->unique_keys, pred->location);
4088 }
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:4022
JsonFormat * format
Definition: primnodes.h:1702
JsonValueType item_type
Definition: primnodes.h:1703
ParseLoc location
Definition: primnodes.h:1705

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 3890 of file parse_expr.c.

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

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:1656
JsonOutput * output
Definition: parsenodes.h:1929

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 4022 of file parse_expr.c.

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

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 4134 of file parse_expr.c.

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

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 4483 of file parse_expr.c.

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

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 4095 of file parse_expr.c.

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

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 4183 of file parse_expr.c.

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

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 4206 of file parse_expr.c.

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

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  }