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 *jsexpr)
 
static NodetransformJsonScalarExpr (ParseState *pstate, JsonScalarExpr *jsexpr)
 
static NodetransformJsonSerializeExpr (ParseState *pstate, JsonSerializeExpr *expr)
 
static NodetransformJsonFuncExpr (ParseState *pstate, JsonFuncExpr *func)
 
static void transformJsonPassingArgs (ParseState *pstate, const char *constructName, JsonFormatType format, List *args, List **passing_values, List **passing_names)
 
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 3439 of file parse_expr.c.

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

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

Referenced by transformJsonOutput().

◆ coerceJsonFuncExpr()

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

Definition at line 3579 of file parse_expr.c.

3581 {
3582  Node *res;
3583  int location;
3584  Oid exprtype = exprType(expr);
3585 
3586  /* if output type is not specified or equals to function type, return */
3587  if (!OidIsValid(returning->typid) || returning->typid == exprtype)
3588  return expr;
3589 
3590  location = exprLocation(expr);
3591 
3592  if (location < 0)
3593  location = returning->format->location;
3594 
3595  /* special case for RETURNING bytea FORMAT json */
3596  if (returning->format->format_type == JS_FORMAT_JSON &&
3597  returning->typid == BYTEAOID)
3598  {
3599  /* encode json text into bytea using pg_convert_to() */
3600  Node *texpr = coerce_to_specific_type(pstate, expr, TEXTOID,
3601  "JSON_FUNCTION");
3602  Const *enc = getJsonEncodingConst(returning->format);
3603  FuncExpr *fexpr = makeFuncExpr(F_CONVERT_TO, BYTEAOID,
3604  list_make2(texpr, enc),
3607 
3608  fexpr->location = location;
3609 
3610  return (Node *) fexpr;
3611  }
3612 
3613  /*
3614  * For other cases, try to coerce expression to the output type using
3615  * assignment-level casts, erroring out if none available. This basically
3616  * allows coercing the jsonb value to any string type (typcategory = 'S').
3617  *
3618  * Requesting assignment-level here means that typmod / length coercion
3619  * assumes implicit coercion which is the behavior we want; see
3620  * build_coercion_expression().
3621  */
3622  res = coerce_to_target_type(pstate, expr, exprtype,
3623  returning->typid, returning->typmod,
3626  location);
3627 
3628  if (!res && report_error)
3629  ereport(ERROR,
3630  errcode(ERRCODE_CANNOT_COERCE),
3631  errmsg("cannot cast type %s to %s",
3632  format_type_be(exprtype),
3633  format_type_be(returning->typid)),
3634  parser_coercion_errposition(pstate, location, expr));
3635 
3636  return res;
3637 }
#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
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int exprLocation(const Node *expr)
Definition: nodeFuncs.c:1380
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:3216
#define list_make2(x1, x2)
Definition: pg_list.h:214
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:736
@ COERCE_EXPLICIT_CALL
Definition: primnodes.h:734
@ COERCION_ASSIGNMENT
Definition: primnodes.h:715
ParseLoc location
Definition: primnodes.h:770
ParseLoc location
Definition: primnodes.h:1653
JsonFormatType format_type
Definition: primnodes.h:1651
JsonFormat * format
Definition: primnodes.h:1663
Definition: nodes.h:129

References COERCE_EXPLICIT_CALL, COERCE_IMPLICIT_CAST, coerce_to_specific_type(), coerce_to_target_type(), COERCION_ASSIGNMENT, 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 makeJsonConstructorExpr().

◆ exprIsNullConstant()

static bool exprIsNullConstant ( Node arg)
static

Definition at line 910 of file parse_expr.c.

911 {
912  if (arg && IsA(arg, A_Const))
913  {
914  A_Const *con = (A_Const *) arg;
915 
916  if (con->isnull)
917  return true;
918  }
919  return false;
920 }
#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 4750 of file parse_expr.c.

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

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

Referenced by transformJsonBehavior().

◆ getJsonEncodingConst()

static Const* getJsonEncodingConst ( JsonFormat format)
static

Definition at line 3216 of file parse_expr.c.

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

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

Referenced by coerceJsonFuncExpr(), and makeJsonByteaToTextConversion().

◆ make_distinct_op()

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

Definition at line 3053 of file parse_expr.c.

3055 {
3056  Expr *result;
3057 
3058  result = make_op(pstate, opname, ltree, rtree,
3059  pstate->p_last_srf, location);
3060  if (((OpExpr *) result)->opresulttype != BOOLOID)
3061  ereport(ERROR,
3062  (errcode(ERRCODE_DATATYPE_MISMATCH),
3063  errmsg("IS DISTINCT FROM requires = operator to yield boolean"),
3064  parser_errposition(pstate, location)));
3065  if (((OpExpr *) result)->opretset)
3066  ereport(ERROR,
3067  (errcode(ERRCODE_DATATYPE_MISMATCH),
3068  /* translator: %s is name of a SQL construct, eg NULLIF */
3069  errmsg("%s must not return a set", "IS DISTINCT FROM"),
3070  parser_errposition(pstate, location)));
3071 
3072  /*
3073  * We rely on DistinctExpr and OpExpr being same struct
3074  */
3075  NodeSetTag(result, T_DistinctExpr);
3076 
3077  return result;
3078 }
#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 3086 of file parse_expr.c.

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

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

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

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

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

◆ make_row_distinct_op()

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

Definition at line 3009 of file parse_expr.c.

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

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

Referenced by transformAExprDistinct().

◆ makeJsonByteaToTextConversion()

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

Definition at line 3255 of file parse_expr.c.

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

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

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

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

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

◆ ParseExprKindName()

const char* ParseExprKindName ( ParseExprKind  exprKind)

Definition at line 3110 of file parse_expr.c.

3111 {
3112  switch (exprKind)
3113  {
3114  case EXPR_KIND_NONE:
3115  return "invalid expression context";
3116  case EXPR_KIND_OTHER:
3117  return "extension expression";
3118  case EXPR_KIND_JOIN_ON:
3119  return "JOIN/ON";
3120  case EXPR_KIND_JOIN_USING:
3121  return "JOIN/USING";
3123  return "sub-SELECT in FROM";
3125  return "function in FROM";
3126  case EXPR_KIND_WHERE:
3127  return "WHERE";
3128  case EXPR_KIND_POLICY:
3129  return "POLICY";
3130  case EXPR_KIND_HAVING:
3131  return "HAVING";
3132  case EXPR_KIND_FILTER:
3133  return "FILTER";
3135  return "window PARTITION BY";
3137  return "window ORDER BY";
3139  return "window RANGE";
3141  return "window ROWS";
3143  return "window GROUPS";
3145  return "SELECT";
3147  return "INSERT";
3150  return "UPDATE";
3151  case EXPR_KIND_MERGE_WHEN:
3152  return "MERGE WHEN";
3153  case EXPR_KIND_GROUP_BY:
3154  return "GROUP BY";
3155  case EXPR_KIND_ORDER_BY:
3156  return "ORDER BY";
3157  case EXPR_KIND_DISTINCT_ON:
3158  return "DISTINCT ON";
3159  case EXPR_KIND_LIMIT:
3160  return "LIMIT";
3161  case EXPR_KIND_OFFSET:
3162  return "OFFSET";
3163  case EXPR_KIND_RETURNING:
3165  return "RETURNING";
3166  case EXPR_KIND_VALUES:
3168  return "VALUES";
3171  return "CHECK";
3174  return "DEFAULT";
3176  return "index expression";
3178  return "index predicate";
3180  return "statistics expression";
3182  return "USING";
3184  return "EXECUTE";
3186  return "WHEN";
3188  return "partition bound";
3190  return "PARTITION BY";
3192  return "CALL";
3193  case EXPR_KIND_COPY_WHERE:
3194  return "WHERE";
3196  return "GENERATED AS";
3197  case EXPR_KIND_CYCLE_MARK:
3198  return "CYCLE";
3199 
3200  /*
3201  * There is intentionally no default: case here, so that the
3202  * compiler will warn if we add a new ParseExprKind without
3203  * extending this switch. If we do see an unrecognized value at
3204  * runtime, we'll fall through to the "unrecognized" return.
3205  */
3206  }
3207  return "unrecognized expression kind";
3208 }
@ 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 1283 of file parse_expr.c.

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

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

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

Referenced by transformExprRecurse().

◆ transformAExprIn()

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

Definition at line 1125 of file parse_expr.c.

1126 {
1127  Node *result = NULL;
1128  Node *lexpr;
1129  List *rexprs;
1130  List *rvars;
1131  List *rnonvars;
1132  bool useOr;
1133  ListCell *l;
1134 
1135  /*
1136  * If the operator is <>, combine with AND not OR.
1137  */
1138  if (strcmp(strVal(linitial(a->name)), "<>") == 0)
1139  useOr = false;
1140  else
1141  useOr = true;
1142 
1143  /*
1144  * We try to generate a ScalarArrayOpExpr from IN/NOT IN, but this is only
1145  * possible if there is a suitable array type available. If not, we fall
1146  * back to a boolean condition tree with multiple copies of the lefthand
1147  * expression. Also, any IN-list items that contain Vars are handled as
1148  * separate boolean conditions, because that gives the planner more scope
1149  * for optimization on such clauses.
1150  *
1151  * First step: transform all the inputs, and detect whether any contain
1152  * Vars.
1153  */
1154  lexpr = transformExprRecurse(pstate, a->lexpr);
1155  rexprs = rvars = rnonvars = NIL;
1156  foreach(l, (List *) a->rexpr)
1157  {
1158  Node *rexpr = transformExprRecurse(pstate, lfirst(l));
1159 
1160  rexprs = lappend(rexprs, rexpr);
1161  if (contain_vars_of_level(rexpr, 0))
1162  rvars = lappend(rvars, rexpr);
1163  else
1164  rnonvars = lappend(rnonvars, rexpr);
1165  }
1166 
1167  /*
1168  * ScalarArrayOpExpr is only going to be useful if there's more than one
1169  * non-Var righthand item.
1170  */
1171  if (list_length(rnonvars) > 1)
1172  {
1173  List *allexprs;
1174  Oid scalar_type;
1175  Oid array_type;
1176 
1177  /*
1178  * Try to select a common type for the array elements. Note that
1179  * since the LHS' type is first in the list, it will be preferred when
1180  * there is doubt (eg, when all the RHS items are unknown literals).
1181  *
1182  * Note: use list_concat here not lcons, to avoid damaging rnonvars.
1183  */
1184  allexprs = list_concat(list_make1(lexpr), rnonvars);
1185  scalar_type = select_common_type(pstate, allexprs, NULL, NULL);
1186 
1187  /* We have to verify that the selected type actually works */
1188  if (OidIsValid(scalar_type) &&
1189  !verify_common_type(scalar_type, allexprs))
1190  scalar_type = InvalidOid;
1191 
1192  /*
1193  * Do we have an array type to use? Aside from the case where there
1194  * isn't one, we don't risk using ScalarArrayOpExpr when the common
1195  * type is RECORD, because the RowExpr comparison logic below can cope
1196  * with some cases of non-identical row types.
1197  */
1198  if (OidIsValid(scalar_type) && scalar_type != RECORDOID)
1199  array_type = get_array_type(scalar_type);
1200  else
1201  array_type = InvalidOid;
1202  if (array_type != InvalidOid)
1203  {
1204  /*
1205  * OK: coerce all the right-hand non-Var inputs to the common type
1206  * and build an ArrayExpr for them.
1207  */
1208  List *aexprs;
1209  ArrayExpr *newa;
1210 
1211  aexprs = NIL;
1212  foreach(l, rnonvars)
1213  {
1214  Node *rexpr = (Node *) lfirst(l);
1215 
1216  rexpr = coerce_to_common_type(pstate, rexpr,
1217  scalar_type,
1218  "IN");
1219  aexprs = lappend(aexprs, rexpr);
1220  }
1221  newa = makeNode(ArrayExpr);
1222  newa->array_typeid = array_type;
1223  /* array_collid will be set by parse_collate.c */
1224  newa->element_typeid = scalar_type;
1225  newa->elements = aexprs;
1226  newa->multidims = false;
1227  newa->location = -1;
1228 
1229  result = (Node *) make_scalar_array_op(pstate,
1230  a->name,
1231  useOr,
1232  lexpr,
1233  (Node *) newa,
1234  a->location);
1235 
1236  /* Consider only the Vars (if any) in the loop below */
1237  rexprs = rvars;
1238  }
1239  }
1240 
1241  /*
1242  * Must do it the hard way, ie, with a boolean expression tree.
1243  */
1244  foreach(l, rexprs)
1245  {
1246  Node *rexpr = (Node *) lfirst(l);
1247  Node *cmp;
1248 
1249  if (IsA(lexpr, RowExpr) &&
1250  IsA(rexpr, RowExpr))
1251  {
1252  /* ROW() op ROW() is handled specially */
1253  cmp = make_row_comparison_op(pstate,
1254  a->name,
1255  copyObject(((RowExpr *) lexpr)->args),
1256  ((RowExpr *) rexpr)->args,
1257  a->location);
1258  }
1259  else
1260  {
1261  /* Ordinary scalar operator */
1262  cmp = (Node *) make_op(pstate,
1263  a->name,
1264  copyObject(lexpr),
1265  rexpr,
1266  pstate->p_last_srf,
1267  a->location);
1268  }
1269 
1270  cmp = coerce_to_boolean(pstate, cmp, "IN");
1271  if (result == NULL)
1272  result = cmp;
1273  else
1274  result = (Node *) makeBoolExpr(useOr ? OR_EXPR : AND_EXPR,
1275  list_make2(result, cmp),
1276  a->location);
1277  }
1278 
1279  return result;
1280 }
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:2807
Expr * make_scalar_array_op(ParseState *pstate, List *opname, bool useOr, Node *ltree, Node *rtree, int location)
Definition: parse_oper.c:770
ParseLoc location
Definition: primnodes.h:1384
List * elements
Definition: primnodes.h:1380
bool contain_vars_of_level(Node *node, int levelsup)
Definition: var.c:441

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

Referenced by transformExprRecurse().

◆ transformAExprNullIf()

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

Definition at line 1083 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformAExprOp()

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

Definition at line 923 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformAExprOpAll()

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

Definition at line 1018 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformAExprOpAny()

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

Definition at line 1004 of file parse_expr.c.

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

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

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

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

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

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

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

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

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

Referenced by transformExprRecurse().

◆ transformCoalesceExpr()

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

Definition at line 2205 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformCollateClause()

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

Definition at line 2767 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformColumnRef()

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

Definition at line 510 of file parse_expr.c.

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

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

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

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

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

◆ transformFuncCall()

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

Definition at line 1438 of file parse_expr.c.

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

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

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

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

Referenced by transformJsonArrayAgg(), and transformJsonObjectAgg().

◆ transformJsonArrayAgg()

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

Definition at line 3961 of file parse_expr.c.

3962 {
3963  JsonReturning *returning;
3964  Node *arg;
3965  Oid aggfnoid;
3966  Oid aggtype;
3967 
3968  arg = transformJsonValueExpr(pstate, "JSON_ARRAYAGG()", agg->arg,
3969  JS_FORMAT_DEFAULT, InvalidOid, false);
3970 
3971  returning = transformJsonConstructorOutput(pstate, agg->constructor->output,
3972  list_make1(arg));
3973 
3974  if (returning->format->format_type == JS_FORMAT_JSONB)
3975  {
3976  aggfnoid = agg->absent_on_null ? F_JSONB_AGG_STRICT : F_JSONB_AGG;
3977  aggtype = JSONBOID;
3978  }
3979  else
3980  {
3981  aggfnoid = agg->absent_on_null ? F_JSON_AGG_STRICT : F_JSON_AGG;
3982  aggtype = JSONOID;
3983  }
3984 
3985  return transformJsonAggConstructor(pstate, agg->constructor, returning,
3986  list_make1(arg), aggfnoid, aggtype,
3988  false, agg->absent_on_null);
3989 }
static Node * transformJsonValueExpr(ParseState *pstate, const char *constructName, JsonValueExpr *ve, JsonFormatType default_format, Oid targettype, bool isarg)
Definition: parse_expr.c:3277
static JsonReturning * transformJsonConstructorOutput(ParseState *pstate, JsonOutput *output, List *args)
Definition: parse_expr.c:3536
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:3811
@ JSCTOR_JSON_ARRAYAGG
Definition: primnodes.h:1689
JsonOutput * output
Definition: parsenodes.h:1968
bool absent_on_null
Definition: parsenodes.h:1997
JsonValueExpr * arg
Definition: parsenodes.h:1996
JsonAggConstructor * constructor
Definition: parsenodes.h:1995

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

Referenced by transformExprRecurse().

◆ transformJsonArrayConstructor()

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

Definition at line 3999 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformJsonArrayQueryConstructor()

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

Definition at line 3740 of file parse_expr.c.

3742 {
3743  SubLink *sublink = makeNode(SubLink);
3746  Alias *alias = makeNode(Alias);
3747  ResTarget *target = makeNode(ResTarget);
3749  ColumnRef *colref = makeNode(ColumnRef);
3750  Query *query;
3751  ParseState *qpstate;
3752 
3753  /* Transform query only for counting target list entries. */
3754  qpstate = make_parsestate(pstate);
3755 
3756  query = transformStmt(qpstate, ctor->query);
3757 
3758  if (count_nonjunk_tlist_entries(query->targetList) != 1)
3759  ereport(ERROR,
3760  errcode(ERRCODE_SYNTAX_ERROR),
3761  errmsg("subquery must return only one column"),
3762  parser_errposition(pstate, ctor->location));
3763 
3764  free_parsestate(qpstate);
3765 
3766  colref->fields = list_make2(makeString(pstrdup("q")),
3767  makeString(pstrdup("a")));
3768  colref->location = ctor->location;
3769 
3770  /*
3771  * No formatting necessary, so set formatted_expr to be the same as
3772  * raw_expr.
3773  */
3774  agg->arg = makeJsonValueExpr((Expr *) colref, (Expr *) colref,
3775  ctor->format);
3776  agg->absent_on_null = ctor->absent_on_null;
3778  agg->constructor->agg_order = NIL;
3779  agg->constructor->output = ctor->output;
3780  agg->constructor->location = ctor->location;
3781 
3782  target->name = NULL;
3783  target->indirection = NIL;
3784  target->val = (Node *) agg;
3785  target->location = ctor->location;
3786 
3787  alias->aliasname = pstrdup("q");
3788  alias->colnames = list_make1(makeString(pstrdup("a")));
3789 
3790  range->lateral = false;
3791  range->subquery = ctor->query;
3792  range->alias = alias;
3793 
3794  select->targetList = list_make1(target);
3795  select->fromClause = list_make1(range);
3796 
3797  sublink->subLinkType = EXPR_SUBLINK;
3798  sublink->subLinkId = 0;
3799  sublink->testexpr = NULL;
3800  sublink->operName = NIL;
3801  sublink->subselect = (Node *) select;
3802  sublink->location = ctor->location;
3803 
3804  return transformExprRecurse(pstate, (Node *) sublink);
3805 }
JsonValueExpr * makeJsonValueExpr(Expr *raw_expr, Expr *formatted_expr, JsonFormat *format)
Definition: makefuncs.c:863
char * pstrdup(const char *in)
Definition: mcxt.c:1696
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:500

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

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

References JsonBehavior::btype, JsonBehavior::coerce, COERCE_EXPLICIT_CAST, COERCE_IMPLICIT_CAST, coerce_to_specific_type(), coerce_to_target_type(), COERCION_EXPLICIT, COERCION_IMPLICIT, contain_var_clause(), ereport, errcode(), errmsg(), ERROR, JsonBehavior::expr, expression_returns_set(), exprLocation(), exprType(), format_type_be(), get_typtype(), getBaseTypeAndTypmod(), 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 3536 of file parse_expr.c.

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

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 func 
)
static

Definition at line 4259 of file parse_expr.c.

4260 {
4261  JsonExpr *jsexpr;
4262  Node *path_spec;
4263  const char *func_name = NULL;
4264  JsonFormatType default_format;
4265 
4266  switch (func->op)
4267  {
4268  case JSON_EXISTS_OP:
4269  func_name = "JSON_EXISTS";
4270  default_format = JS_FORMAT_DEFAULT;
4271  break;
4272  case JSON_QUERY_OP:
4273  func_name = "JSON_QUERY";
4274  default_format = JS_FORMAT_JSONB;
4275  break;
4276  case JSON_VALUE_OP:
4277  func_name = "JSON_VALUE";
4278  default_format = JS_FORMAT_DEFAULT;
4279  break;
4280  case JSON_TABLE_OP:
4281  func_name = "JSON_TABLE";
4282  default_format = JS_FORMAT_JSONB;
4283  break;
4284  default:
4285  elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4286  default_format = JS_FORMAT_DEFAULT; /* keep compiler quiet */
4287  break;
4288  }
4289 
4290  /*
4291  * Even though the syntax allows it, FORMAT JSON specification in
4292  * RETURNING is meaningless except for JSON_QUERY(). Flag if not
4293  * JSON_QUERY().
4294  */
4295  if (func->output && func->op != JSON_QUERY_OP)
4296  {
4298 
4299  if (format->format_type != JS_FORMAT_DEFAULT ||
4300  format->encoding != JS_ENC_DEFAULT)
4301  ereport(ERROR,
4302  errcode(ERRCODE_SYNTAX_ERROR),
4303  errmsg("cannot specify FORMAT JSON in RETURNING clause of %s()",
4304  func_name),
4305  parser_errposition(pstate, format->location));
4306  }
4307 
4308  /* OMIT QUOTES is meaningless when strings are wrapped. */
4309  if (func->op == JSON_QUERY_OP)
4310  {
4311  if (func->quotes == JS_QUOTES_OMIT &&
4312  (func->wrapper == JSW_CONDITIONAL ||
4313  func->wrapper == JSW_UNCONDITIONAL))
4314  ereport(ERROR,
4315  errcode(ERRCODE_SYNTAX_ERROR),
4316  errmsg("SQL/JSON QUOTES behavior must not be specified when WITH WRAPPER is used"),
4317  parser_errposition(pstate, func->location));
4318  if (func->on_empty != NULL &&
4319  func->on_empty->btype != JSON_BEHAVIOR_ERROR &&
4320  func->on_empty->btype != JSON_BEHAVIOR_NULL &&
4321  func->on_empty->btype != JSON_BEHAVIOR_EMPTY &&
4325  {
4326  if (func->column_name == NULL)
4327  ereport(ERROR,
4328  errcode(ERRCODE_SYNTAX_ERROR),
4329  errmsg("invalid ON EMPTY behavior"),
4330  errdetail("Only ERROR, NULL, EMPTY [ ARRAY ], EMPTY OBJECT, or DEFAULT expression is allowed in ON EMPTY for JSON_QUERY()."),
4331  parser_errposition(pstate, func->on_empty->location));
4332  else
4333  ereport(ERROR,
4334  errcode(ERRCODE_SYNTAX_ERROR),
4335  errmsg("invalid ON EMPTY behavior for column \"%s\"",
4336  func->column_name),
4337  errdetail("Only ERROR, NULL, EMPTY [ ARRAY ], EMPTY OBJECT, or DEFAULT expression is allowed in ON EMPTY for formatted columns."),
4338  parser_errposition(pstate, func->on_empty->location));
4339  }
4340  if (func->on_error != NULL &&
4341  func->on_error->btype != JSON_BEHAVIOR_ERROR &&
4342  func->on_error->btype != JSON_BEHAVIOR_NULL &&
4343  func->on_error->btype != JSON_BEHAVIOR_EMPTY &&
4347  {
4348  if (func->column_name == NULL)
4349  ereport(ERROR,
4350  errcode(ERRCODE_SYNTAX_ERROR),
4351  errmsg("invalid ON ERROR behavior"),
4352  errdetail("Only ERROR, NULL, EMPTY [ ARRAY ], EMPTY OBJECT, or DEFAULT expression is allowed in ON ERROR for JSON_QUERY()."),
4353  parser_errposition(pstate, func->on_error->location));
4354  else
4355  ereport(ERROR,
4356  errcode(ERRCODE_SYNTAX_ERROR),
4357  errmsg("invalid ON ERROR behavior for column \"%s\"",
4358  func->column_name),
4359  errdetail("Only ERROR, NULL, EMPTY [ ARRAY ], EMPTY OBJECT, or DEFAULT expression is allowed in ON ERROR for formatted columns."),
4360  parser_errposition(pstate, func->on_error->location));
4361  }
4362  }
4363 
4364  /* Check that ON ERROR/EMPTY behavior values are valid for the function. */
4365  if (func->op == JSON_EXISTS_OP &&
4366  func->on_error != NULL &&
4367  func->on_error->btype != JSON_BEHAVIOR_ERROR &&
4368  func->on_error->btype != JSON_BEHAVIOR_TRUE &&
4369  func->on_error->btype != JSON_BEHAVIOR_FALSE &&
4371  {
4372  if (func->column_name == NULL)
4373  ereport(ERROR,
4374  errcode(ERRCODE_SYNTAX_ERROR),
4375  errmsg("invalid ON ERROR behavior"),
4376  errdetail("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in ON ERROR for JSON_EXISTS()."),
4377  parser_errposition(pstate, func->on_error->location));
4378  else
4379  ereport(ERROR,
4380  errcode(ERRCODE_SYNTAX_ERROR),
4381  errmsg("invalid ON ERROR behavior for column \"%s\"",
4382  func->column_name),
4383  errdetail("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in ON ERROR for EXISTS columns."),
4384  parser_errposition(pstate, func->on_error->location));
4385  }
4386  if (func->op == JSON_VALUE_OP)
4387  {
4388  if (func->on_empty != NULL &&
4389  func->on_empty->btype != JSON_BEHAVIOR_ERROR &&
4390  func->on_empty->btype != JSON_BEHAVIOR_NULL &&
4392  {
4393  if (func->column_name == NULL)
4394  ereport(ERROR,
4395  errcode(ERRCODE_SYNTAX_ERROR),
4396  errmsg("invalid ON EMPTY behavior"),
4397  errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in ON EMPTY for JSON_VALUE()."),
4398  parser_errposition(pstate, func->on_empty->location));
4399  else
4400  ereport(ERROR,
4401  errcode(ERRCODE_SYNTAX_ERROR),
4402  errmsg("invalid ON EMPTY behavior for column \"%s\"",
4403  func->column_name),
4404  errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in ON EMPTY for scalar columns."),
4405  parser_errposition(pstate, func->on_empty->location));
4406  }
4407  if (func->on_error != NULL &&
4408  func->on_error->btype != JSON_BEHAVIOR_ERROR &&
4409  func->on_error->btype != JSON_BEHAVIOR_NULL &&
4411  {
4412  if (func->column_name == NULL)
4413  ereport(ERROR,
4414  errcode(ERRCODE_SYNTAX_ERROR),
4415  errmsg("invalid ON ERROR behavior"),
4416  errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in ON ERROR for JSON_VALUE()."),
4417  parser_errposition(pstate, func->on_error->location));
4418  else
4419  ereport(ERROR,
4420  errcode(ERRCODE_SYNTAX_ERROR),
4421  errmsg("invalid ON ERROR behavior for column \"%s\"",
4422  func->column_name),
4423  errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in ON ERROR for scalar columns."),
4424  parser_errposition(pstate, func->on_error->location));
4425  }
4426  }
4427 
4428  jsexpr = makeNode(JsonExpr);
4429  jsexpr->location = func->location;
4430  jsexpr->op = func->op;
4431  jsexpr->column_name = func->column_name;
4432 
4433  /*
4434  * jsonpath machinery can only handle jsonb documents, so coerce the input
4435  * if not already of jsonb type.
4436  */
4437  jsexpr->formatted_expr = transformJsonValueExpr(pstate, func_name,
4438  func->context_item,
4439  default_format,
4440  JSONBOID,
4441  false);
4442  jsexpr->format = func->context_item->format;
4443 
4444  path_spec = transformExprRecurse(pstate, func->pathspec);
4445  path_spec = coerce_to_target_type(pstate, path_spec, exprType(path_spec),
4446  JSONPATHOID, -1,
4448  exprLocation(path_spec));
4449  if (path_spec == NULL)
4450  ereport(ERROR,
4451  (errcode(ERRCODE_DATATYPE_MISMATCH),
4452  errmsg("JSON path expression must be of type %s, not of type %s",
4453  "jsonpath", format_type_be(exprType(path_spec))),
4454  parser_errposition(pstate, exprLocation(path_spec))));
4455  jsexpr->path_spec = path_spec;
4456 
4457  /* Transform and coerce the PASSING arguments to jsonb. */
4458  transformJsonPassingArgs(pstate, func_name,
4460  func->passing,
4461  &jsexpr->passing_values,
4462  &jsexpr->passing_names);
4463 
4464  /* Transform the JsonOutput into JsonReturning. */
4465  jsexpr->returning = transformJsonOutput(pstate, func->output, false);
4466 
4467  switch (func->op)
4468  {
4469  case JSON_EXISTS_OP:
4470  /* JSON_EXISTS returns boolean by default. */
4471  if (!OidIsValid(jsexpr->returning->typid))
4472  {
4473  jsexpr->returning->typid = BOOLOID;
4474  jsexpr->returning->typmod = -1;
4475  }
4476 
4477  /* JSON_TABLE() COLUMNS can specify a non-boolean type. */
4478  if (jsexpr->returning->typid != BOOLOID)
4479  jsexpr->use_json_coercion = true;
4480 
4481  jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4483  jsexpr->returning);
4484  break;
4485 
4486  case JSON_QUERY_OP:
4487  /* JSON_QUERY returns jsonb by default. */
4488  if (!OidIsValid(jsexpr->returning->typid))
4489  {
4490  JsonReturning *ret = jsexpr->returning;
4491 
4492  ret->typid = JSONBOID;
4493  ret->typmod = -1;
4494  }
4495 
4496  /*
4497  * Keep quotes on scalar strings by default, omitting them only if
4498  * OMIT QUOTES is specified.
4499  */
4500  jsexpr->omit_quotes = (func->quotes == JS_QUOTES_OMIT);
4501  jsexpr->wrapper = func->wrapper;
4502 
4503  /*
4504  * Set up to coerce the result value of JsonPathValue() to the
4505  * RETURNING type (default or user-specified), if needed. Also if
4506  * OMIT QUOTES is specified.
4507  */
4508  if (jsexpr->returning->typid != JSONBOID || jsexpr->omit_quotes)
4509  jsexpr->use_json_coercion = true;
4510 
4511  /* Assume NULL ON EMPTY when ON EMPTY is not specified. */
4512  jsexpr->on_empty = transformJsonBehavior(pstate, func->on_empty,
4514  jsexpr->returning);
4515  /* Assume NULL ON ERROR when ON ERROR is not specified. */
4516  jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4518  jsexpr->returning);
4519  break;
4520 
4521  case JSON_VALUE_OP:
4522  /* JSON_VALUE returns text by default. */
4523  if (!OidIsValid(jsexpr->returning->typid))
4524  {
4525  jsexpr->returning->typid = TEXTOID;
4526  jsexpr->returning->typmod = -1;
4527  }
4528 
4529  /*
4530  * Override whatever transformJsonOutput() set these to, which
4531  * assumes that output type to be jsonb.
4532  */
4535 
4536  /* Always omit quotes from scalar strings. */
4537  jsexpr->omit_quotes = true;
4538 
4539  /*
4540  * Set up to coerce the result value of JsonPathValue() to the
4541  * RETURNING type (default or user-specified), if needed.
4542  */
4543  if (jsexpr->returning->typid != TEXTOID)
4544  {
4545  if (get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN &&
4547  jsexpr->use_json_coercion = true;
4548  else
4549  jsexpr->use_io_coercion = true;
4550  }
4551 
4552  /* Assume NULL ON EMPTY when ON EMPTY is not specified. */
4553  jsexpr->on_empty = transformJsonBehavior(pstate, func->on_empty,
4555  jsexpr->returning);
4556  /* Assume NULL ON ERROR when ON ERROR is not specified. */
4557  jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4559  jsexpr->returning);
4560  break;
4561 
4562  case JSON_TABLE_OP:
4563  if (!OidIsValid(jsexpr->returning->typid))
4564  {
4565  jsexpr->returning->typid = exprType(jsexpr->formatted_expr);
4566  jsexpr->returning->typmod = -1;
4567  }
4568 
4569  /*
4570  * Assume EMPTY ON ERROR when ON ERROR is not specified.
4571  *
4572  * ON EMPTY cannot be specified at the top level but it can be for
4573  * the individual columns.
4574  */
4575  jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4577  jsexpr->returning);
4578  break;
4579 
4580  default:
4581  elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4582  break;
4583  }
4584 
4585  return (Node *) jsexpr;
4586 }
static void transformJsonPassingArgs(ParseState *pstate, const char *constructName, JsonFormatType format, List *args, List **passing_values, List **passing_names)
Definition: parse_expr.c:4592
static JsonBehavior * transformJsonBehavior(ParseState *pstate, JsonBehavior *behavior, JsonBehaviorType default_behavior, JsonReturning *returning)
Definition: parse_expr.c:4651
@ JS_QUOTES_OMIT
Definition: parsenodes.h:1780
JsonFormatType
Definition: primnodes.h:1637
@ JSW_UNCONDITIONAL
Definition: primnodes.h:1749
@ JSW_CONDITIONAL
Definition: primnodes.h:1748
@ JSON_QUERY_OP
Definition: primnodes.h:1799
@ JSON_TABLE_OP
Definition: primnodes.h:1801
@ JSON_EXISTS_OP
Definition: primnodes.h:1798
@ JSON_VALUE_OP
Definition: primnodes.h:1800
char * column_name
Definition: primnodes.h:1815
Node * formatted_expr
Definition: primnodes.h:1819
ParseLoc location
Definition: primnodes.h:1855
List * passing_values
Definition: primnodes.h:1832
JsonBehavior * on_empty
Definition: primnodes.h:1835
JsonFormat * format
Definition: primnodes.h:1822
List * passing_names
Definition: primnodes.h:1831
Node * path_spec
Definition: primnodes.h:1825
bool use_io_coercion
Definition: primnodes.h:1842
JsonReturning * returning
Definition: primnodes.h:1828
bool use_json_coercion
Definition: primnodes.h:1843
JsonWrapper wrapper
Definition: primnodes.h:1846
JsonExprOp op
Definition: primnodes.h:1813
JsonBehavior * on_error
Definition: primnodes.h:1836
bool omit_quotes
Definition: primnodes.h:1849
JsonEncoding encoding
Definition: primnodes.h:1652
JsonOutput * output
Definition: parsenodes.h:1797
char * column_name
Definition: parsenodes.h:1792
JsonWrapper wrapper
Definition: parsenodes.h:1800
JsonQuotes quotes
Definition: parsenodes.h:1801
JsonExprOp op
Definition: parsenodes.h:1791
List * passing
Definition: parsenodes.h:1796
JsonBehavior * on_empty
Definition: parsenodes.h:1798
ParseLoc location
Definition: parsenodes.h:1802
Node * pathspec
Definition: parsenodes.h:1795
JsonBehavior * on_error
Definition: parsenodes.h:1799
JsonValueExpr * context_item
Definition: parsenodes.h:1794
JsonReturning * returning
Definition: parsenodes.h:1758
JsonFormat * format
Definition: primnodes.h:1681
bool DomainHasConstraints(Oid type_id)
Definition: typcache.c:1400

References JsonBehavior::btype, COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_EXPLICIT, JsonFuncExpr::column_name, JsonExpr::column_name, JsonFuncExpr::context_item, DomainHasConstraints(), elog, JsonFormat::encoding, ereport, errcode(), errdetail(), errmsg(), ERROR, exprLocation(), exprType(), format, JsonReturning::format, JsonValueExpr::format, JsonExpr::format, JsonFormat::format_type, format_type_be(), JsonExpr::formatted_expr, get_typtype(), JS_ENC_DEFAULT, JS_FORMAT_DEFAULT, JS_FORMAT_JSONB, JS_QUOTES_OMIT, 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, JSON_EXISTS_OP, JSON_QUERY_OP, JSON_TABLE_OP, JSON_VALUE_OP, JSW_CONDITIONAL, JSW_UNCONDITIONAL, JsonFuncExpr::location, JsonBehavior::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_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(), JsonReturning::typid, JsonReturning::typmod, JsonExpr::use_io_coercion, JsonExpr::use_json_coercion, JsonFuncExpr::wrapper, and JsonExpr::wrapper.

Referenced by transformExprRecurse().

◆ transformJsonIsPredicate()

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

Definition at line 4079 of file parse_expr.c.

4080 {
4081  Oid exprtype;
4082  Node *expr = transformJsonParseArg(pstate, pred->expr, pred->format,
4083  &exprtype);
4084 
4085  /* make resulting expression */
4086  if (exprtype != TEXTOID && exprtype != JSONOID && exprtype != JSONBOID)
4087  ereport(ERROR,
4088  (errcode(ERRCODE_DATATYPE_MISMATCH),
4089  errmsg("cannot use type %s in IS JSON predicate",
4090  format_type_be(exprtype))));
4091 
4092  /* This intentionally(?) drops the format clause. */
4093  return makeJsonIsPredicate(expr, NULL, pred->item_type,
4094  pred->unique_keys, pred->location);
4095 }
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:4029
JsonFormat * format
Definition: primnodes.h:1732
JsonValueType item_type
Definition: primnodes.h:1733
ParseLoc location
Definition: primnodes.h:1735

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

Referenced by transformExprRecurse().

◆ transformJsonObjectAgg()

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

Definition at line 3897 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformJsonObjectConstructor()

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

Definition at line 3703 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformJsonOutput()

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

Definition at line 3489 of file parse_expr.c.

3491 {
3492  JsonReturning *ret;
3493 
3494  /* if output clause is not specified, make default clause value */
3495  if (!output)
3496  {
3497  ret = makeNode(JsonReturning);
3498 
3500  ret->typid = InvalidOid;
3501  ret->typmod = -1;
3502 
3503  return ret;
3504  }
3505 
3506  ret = copyObject(output->returning);
3507 
3508  typenameTypeIdAndMod(pstate, output->typeName, &ret->typid, &ret->typmod);
3509 
3510  if (output->typeName->setof)
3511  ereport(ERROR,
3512  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3513  errmsg("returning SETOF types is not supported in SQL/JSON functions"));
3514 
3515  if (get_typtype(ret->typid) == TYPTYPE_PSEUDO)
3516  ereport(ERROR,
3517  errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3518  errmsg("returning pseudo-types is not supported in SQL/JSON functions"));
3519 
3520  if (ret->format->format_type == JS_FORMAT_DEFAULT)
3521  /* assign JSONB format when returning jsonb, or JSON format otherwise */
3522  ret->format->format_type =
3523  ret->typid == JSONBOID ? JS_FORMAT_JSONB : JS_FORMAT_JSON;
3524  else
3525  checkJsonOutputFormat(pstate, ret->format, ret->typid, allow_format);
3526 
3527  return ret;
3528 }
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:3439
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 4029 of file parse_expr.c.

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

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 jsexpr 
)
static

Definition at line 4141 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformJsonPassingArgs()

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

Definition at line 4592 of file parse_expr.c.

4595 {
4596  ListCell *lc;
4597 
4598  *passing_values = NIL;
4599  *passing_names = NIL;
4600 
4601  foreach(lc, args)
4602  {
4604  Node *expr = transformJsonValueExpr(pstate, constructName,
4605  arg->val, format,
4606  InvalidOid, true);
4607 
4608  *passing_values = lappend(*passing_values, expr);
4609  *passing_names = lappend(*passing_names, makeString(arg->name));
4610  }
4611 }

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

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

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 jsexpr 
)
static

Definition at line 4190 of file parse_expr.c.

4191 {
4192  Node *arg = transformExprRecurse(pstate, (Node *) jsexpr->expr);
4193  JsonOutput *output = jsexpr->output;
4194  JsonReturning *returning;
4195 
4196  returning = transformJsonReturning(pstate, output, "JSON_SCALAR()");
4197 
4198  if (exprType(arg) == UNKNOWNOID)
4199  arg = coerce_to_specific_type(pstate, arg, TEXTOID, "JSON_SCALAR");
4200 
4202  returning, false, false, jsexpr->location);
4203 }
@ JSCTOR_JSON_SCALAR
Definition: primnodes.h:1691
ParseLoc location
Definition: parsenodes.h:1904
JsonOutput * output
Definition: parsenodes.h:1903

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

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

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

Referenced by transformExprRecurse().

◆ transformJsonValueExpr()

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

Definition at line 3277 of file parse_expr.c.

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

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

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

◆ transformMergeSupportFunc()

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

Definition at line 1377 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformMinMaxExpr()

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

Definition at line 2254 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformMultiAssignRef()

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

Definition at line 1483 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformParamRef()

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

Definition at line 886 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformRowExpr()

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

Definition at line 2167 of file parse_expr.c.

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

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

Referenced by transformExprRecurse(), and transformMultiAssignRef().

◆ transformSQLValueFunction()

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

Definition at line 2293 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformSubLink()

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

Definition at line 1771 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformTypeCast()

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

Definition at line 2683 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformWholeRowRef()

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

Definition at line 2611 of file parse_expr.c.

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

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

Referenced by transformColumnRef().

◆ transformXmlExpr()

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

Definition at line 2346 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformXmlSerialize()

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

Definition at line 2475 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ unknown_attribute()

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

Definition at line 392 of file parse_expr.c.

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

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

Referenced by transformIndirection().

◆ ValidJsonBehaviorDefaultExpr()

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

Definition at line 4618 of file parse_expr.c.

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

References context, expression_tree_walker, and nodeTag.

Referenced by transformJsonBehavior().

Variable Documentation

◆ Transform_null_equals

bool Transform_null_equals = false

Definition at line 46 of file parse_expr.c.

Referenced by transformAExprOp().