PostgreSQL Source Code git master
Loading...
Searching...
No Matches
parse_expr.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_type.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_graphtable.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/lsyscache.h"
#include "utils/timestamp.h"
#include "utils/typcache.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, JsonExpr *jsexpr, 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 charParseExprKindName (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 3484 of file parse_expr.c.

3486{
3488 format->format_type != JS_FORMAT_DEFAULT &&
3489 (targettype != BYTEAOID &&
3490 targettype != JSONOID &&
3491 targettype != JSONBOID))
3492 {
3493 char typcategory;
3494 bool typispreferred;
3495
3497
3499 ereport(ERROR,
3501 parser_errposition(pstate, format->location),
3502 errmsg("cannot use JSON format with non-string output types"));
3503 }
3504
3505 if (format->format_type == JS_FORMAT_JSON)
3506 {
3507 JsonEncoding enc = format->encoding != JS_ENC_DEFAULT ?
3508 format->encoding : JS_ENC_UTF8;
3509
3510 if (targettype != BYTEAOID &&
3511 format->encoding != JS_ENC_DEFAULT)
3512 ereport(ERROR,
3514 parser_errposition(pstate, format->location),
3515 errmsg("cannot set JSON encoding for non-bytea output types"));
3516
3517 if (enc != JS_ENC_UTF8)
3518 ereport(ERROR,
3520 errmsg("unsupported JSON encoding"),
3521 errhint("Only UTF8 JSON encoding is supported."),
3522 parser_errposition(pstate, format->location));
3523 }
3524}
int errcode(int sqlerrcode)
Definition elog.c:874
int errhint(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
Definition lsyscache.c:2932
static char * errmsg
int parser_errposition(ParseState *pstate, int location)
Definition parse_node.c:106
static char format
static int fb(int x)
@ JS_FORMAT_DEFAULT
Definition primnodes.h:1664
@ JS_FORMAT_JSON
Definition primnodes.h:1665
JsonEncoding
Definition primnodes.h:1651
@ JS_ENC_DEFAULT
Definition primnodes.h:1652
@ JS_ENC_UTF8
Definition primnodes.h:1653

References enc, ereport, errcode(), errhint(), errmsg, ERROR, fb(), 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 3624 of file parse_expr.c.

3626{
3627 Node *res;
3628 int location;
3629 Oid exprtype = exprType(expr);
3630
3631 /* if output type is not specified or equals to function type, return */
3632 if (!OidIsValid(returning->typid) || returning->typid == exprtype)
3633 return expr;
3634
3635 location = exprLocation(expr);
3636
3637 if (location < 0)
3638 location = returning->format->location;
3639
3640 /* special case for RETURNING bytea FORMAT json */
3641 if (returning->format->format_type == JS_FORMAT_JSON &&
3642 returning->typid == BYTEAOID)
3643 {
3644 /* encode json text into bytea using pg_convert_to() */
3645 Node *texpr = coerce_to_specific_type(pstate, expr, TEXTOID,
3646 "JSON_FUNCTION");
3647 Const *enc = getJsonEncodingConst(returning->format);
3652
3653 fexpr->location = location;
3654
3655 return (Node *) fexpr;
3656 }
3657
3658 /*
3659 * For other cases, try to coerce expression to the output type using
3660 * assignment-level casts, erroring out if none available. This basically
3661 * allows coercing the jsonb value to any string type (typcategory = 'S').
3662 *
3663 * Requesting assignment-level here means that typmod / length coercion
3664 * assumes implicit coercion which is the behavior we want; see
3665 * build_coercion_expression().
3666 */
3667 res = coerce_to_target_type(pstate, expr, exprtype,
3668 returning->typid, returning->typmod,
3671 location);
3672
3673 if (!res && report_error)
3674 ereport(ERROR,
3676 errmsg("cannot cast type %s to %s",
3677 format_type_be(exprtype),
3678 format_type_be(returning->typid)),
3679 parser_coercion_errposition(pstate, location, expr));
3680
3681 return res;
3682}
#define OidIsValid(objectId)
Definition c.h:860
char * format_type_be(Oid type_oid)
FuncExpr * makeFuncExpr(Oid funcid, Oid rettype, List *args, Oid funccollid, Oid inputcollid, CoercionForm fformat)
Definition makefuncs.c:594
Oid exprType(const Node *expr)
Definition nodeFuncs.c:42
int exprLocation(const Node *expr)
Definition nodeFuncs.c:1392
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)
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
static Const * getJsonEncodingConst(JsonFormat *format)
#define list_make2(x1, x2)
Definition pg_list.h:214
#define InvalidOid
unsigned int Oid
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:769
@ COERCE_EXPLICIT_CALL
Definition primnodes.h:767
@ COERCION_ASSIGNMENT
Definition primnodes.h:748
ParseLoc location
Definition primnodes.h:1679
JsonFormatType format_type
Definition primnodes.h:1677
JsonFormat * format
Definition primnodes.h:1689
Definition nodes.h:135

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

Referenced by makeJsonConstructorExpr().

◆ exprIsNullConstant()

static bool exprIsNullConstant ( Node arg)
static

Definition at line 916 of file parse_expr.c.

917{
918 if (arg && IsA(arg, A_Const))
919 {
920 A_Const *con = (A_Const *) arg;
921
922 if (con->isnull)
923 return true;
924 }
925 return false;
926}
Datum arg
Definition elog.c:1322
#define IsA(nodeptr, _type_)
Definition nodes.h:164
bool isnull
Definition parsenodes.h:388

References arg, IsA, and A_Const::isnull.

Referenced by transformAExprDistinct(), and transformAExprOp().

◆ GetJsonBehaviorConst()

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

Definition at line 4917 of file parse_expr.c.

4918{
4919 Datum val = (Datum) 0;
4920 Oid typid = JSONBOID;
4921 int len = -1;
4922 bool isbyval = false;
4923 bool isnull = false;
4924 Const *con;
4925
4926 switch (btype)
4927 {
4930 break;
4931
4934 break;
4935
4936 case JSON_BEHAVIOR_TRUE:
4937 val = BoolGetDatum(true);
4938 typid = BOOLOID;
4939 len = sizeof(bool);
4940 isbyval = true;
4941 break;
4942
4944 val = BoolGetDatum(false);
4945 typid = BOOLOID;
4946 len = sizeof(bool);
4947 isbyval = true;
4948 break;
4949
4950 case JSON_BEHAVIOR_NULL:
4953 val = (Datum) 0;
4954 isnull = true;
4955 typid = INT4OID;
4956 len = sizeof(int32);
4957 isbyval = true;
4958 break;
4959
4960 /* These two behavior types are handled by the caller. */
4963 Assert(false);
4964 break;
4965
4966 default:
4967 elog(ERROR, "unrecognized SQL/JSON behavior %d", btype);
4968 break;
4969 }
4970
4971 con = makeConst(typid, -1, InvalidOid, len, val, isnull, isbyval);
4972 con->location = location;
4973
4974 return (Node *) con;
4975}
#define Assert(condition)
Definition c.h:945
int32_t int32
Definition c.h:614
#define elog(elevel,...)
Definition elog.h:226
#define DirectFunctionCall1(func, arg1)
Definition fmgr.h:684
long val
Definition informix.c:689
Datum jsonb_in(PG_FUNCTION_ARGS)
Definition jsonb.c:64
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition makefuncs.c:350
const void size_t len
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
uint64_t Datum
Definition postgres.h:70
static Datum CStringGetDatum(const char *X)
Definition postgres.h:370
@ JSON_BEHAVIOR_ERROR
Definition primnodes.h:1793
@ JSON_BEHAVIOR_TRUE
Definition primnodes.h:1795
@ JSON_BEHAVIOR_DEFAULT
Definition primnodes.h:1800
@ JSON_BEHAVIOR_EMPTY
Definition primnodes.h:1794
@ JSON_BEHAVIOR_FALSE
Definition primnodes.h:1796
@ JSON_BEHAVIOR_NULL
Definition primnodes.h:1792
@ JSON_BEHAVIOR_EMPTY_OBJECT
Definition primnodes.h:1799
@ JSON_BEHAVIOR_UNKNOWN
Definition primnodes.h:1797
@ JSON_BEHAVIOR_EMPTY_ARRAY
Definition primnodes.h:1798

References Assert, BoolGetDatum(), CStringGetDatum(), DirectFunctionCall1, elog, ERROR, fb(), 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 3261 of file parse_expr.c.

3262{
3264 const char *enc;
3266
3267 if (!format ||
3268 format->format_type == JS_FORMAT_DEFAULT ||
3269 format->encoding == JS_ENC_DEFAULT)
3271 else
3272 encoding = format->encoding;
3273
3274 switch (encoding)
3275 {
3276 case JS_ENC_UTF16:
3277 enc = "UTF16";
3278 break;
3279 case JS_ENC_UTF32:
3280 enc = "UTF32";
3281 break;
3282 case JS_ENC_UTF8:
3283 enc = "UTF8";
3284 break;
3285 default:
3286 elog(ERROR, "invalid JSON encoding: %d", encoding);
3287 break;
3288 }
3289
3291
3293 NameGetDatum(encname), false, false);
3294}
#define palloc_object(type)
Definition fe_memutils.h:74
static char * encoding
Definition initdb.c:139
void namestrcpy(Name name, const char *str)
Definition name.c:233
#define NAMEDATALEN
static Datum NameGetDatum(const NameData *X)
Definition postgres.h:393
@ JS_ENC_UTF32
Definition primnodes.h:1655
@ JS_ENC_UTF16
Definition primnodes.h:1654
Definition c.h:832

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

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

3096{
3097 Expr *result;
3098
3099 result = make_op(pstate, opname, ltree, rtree,
3100 pstate->p_last_srf, location);
3101 if (((OpExpr *) result)->opresulttype != BOOLOID)
3102 ereport(ERROR,
3104 /* translator: %s is name of a SQL construct, eg NULLIF */
3105 errmsg("%s requires = operator to yield boolean",
3106 "IS DISTINCT FROM"),
3107 parser_errposition(pstate, location)));
3108 if (((OpExpr *) result)->opretset)
3109 ereport(ERROR,
3111 /* translator: %s is name of a SQL construct, eg NULLIF */
3112 errmsg("%s must not return a set", "IS DISTINCT FROM"),
3113 parser_errposition(pstate, location)));
3114
3115 /*
3116 * We rely on DistinctExpr and OpExpr being same struct
3117 */
3118 NodeSetTag(result, T_DistinctExpr);
3119
3120 return result;
3121}
#define NodeSetTag(nodeptr, t)
Definition nodes.h:162
Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, Node *last_srf, int location)
Definition parse_oper.c:704
Node * p_last_srf
Definition parse_node.h:248
Definition ltree.h:43

References ereport, errcode(), errmsg, ERROR, fb(), 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 3129 of file parse_expr.c.

3130{
3132
3133 nt->arg = (Expr *) transformExprRecurse(pstate, arg);
3134 /* the argument can be any type, so don't coerce it */
3135 if (distincta->kind == AEXPR_NOT_DISTINCT)
3136 nt->nulltesttype = IS_NULL;
3137 else
3138 nt->nulltesttype = IS_NOT_NULL;
3139 /* argisrow = false is correct whether or not arg is composite */
3140 nt->argisrow = false;
3141 nt->location = distincta->location;
3142 return (Node *) nt;
3143}
#define makeNode(_type_)
Definition nodes.h:161
static Node * transformExprRecurse(ParseState *pstate, Node *expr)
Definition parse_expr.c:139
@ AEXPR_NOT_DISTINCT
Definition parsenodes.h:334
@ IS_NULL
Definition primnodes.h:1979
@ IS_NOT_NULL
Definition primnodes.h:1979

References AEXPR_NOT_DISTINCT, arg, fb(), IS_NOT_NULL, IS_NULL, makeNode, 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 2848 of file parse_expr.c.

2850{
2852 CompareType cmptype;
2853 List *opexprs;
2854 List *opnos;
2856 ListCell *l,
2857 *r;
2860 int nopers;
2861 int i;
2862
2863 nopers = list_length(largs);
2864 if (nopers != list_length(rargs))
2865 ereport(ERROR,
2867 errmsg("unequal number of entries in row expressions"),
2868 parser_errposition(pstate, location)));
2869
2870 /*
2871 * We can't compare zero-length rows because there is no principled basis
2872 * for figuring out what the operator is.
2873 */
2874 if (nopers == 0)
2875 ereport(ERROR,
2877 errmsg("cannot compare rows of zero length"),
2878 parser_errposition(pstate, location)));
2879
2880 /*
2881 * Identify all the pairwise operators, using make_op so that behavior is
2882 * the same as in the simple scalar case.
2883 */
2884 opexprs = NIL;
2885 forboth(l, largs, r, rargs)
2886 {
2887 Node *larg = (Node *) lfirst(l);
2888 Node *rarg = (Node *) lfirst(r);
2889 OpExpr *cmp;
2890
2891 cmp = castNode(OpExpr, make_op(pstate, opname, larg, rarg,
2892 pstate->p_last_srf, location));
2893
2894 /*
2895 * We don't use coerce_to_boolean here because we insist on the
2896 * operator yielding boolean directly, not via coercion. If it
2897 * doesn't yield bool it won't be in any index opfamilies...
2898 */
2899 if (cmp->opresulttype != BOOLOID)
2900 ereport(ERROR,
2902 errmsg("row comparison operator must yield type boolean, "
2903 "not type %s",
2904 format_type_be(cmp->opresulttype)),
2905 parser_errposition(pstate, location)));
2907 ereport(ERROR,
2909 errmsg("row comparison operator must not return a set"),
2910 parser_errposition(pstate, location)));
2912 }
2913
2914 /*
2915 * If rows are length 1, just return the single operator. In this case we
2916 * don't insist on identifying btree semantics for the operator (but we
2917 * still require it to return boolean).
2918 */
2919 if (nopers == 1)
2920 return (Node *) linitial(opexprs);
2921
2922 /*
2923 * Now we must determine which row comparison semantics (= <> < <= > >=)
2924 * apply to this set of operators. We look for opfamilies containing the
2925 * operators, and see which interpretations (cmptypes) exist for each
2926 * operator.
2927 */
2929 cmptypes = NULL;
2930 i = 0;
2931 foreach(l, opexprs)
2932 {
2933 Oid opno = ((OpExpr *) lfirst(l))->opno;
2935 ListCell *j;
2936
2938
2939 /*
2940 * convert comparison types into a Bitmapset to make the intersection
2941 * calculation easy.
2942 */
2944 foreach(j, opinfo_lists[i])
2945 {
2947
2949 }
2950 if (i == 0)
2952 else
2954 i++;
2955 }
2956
2957 /*
2958 * If there are multiple common interpretations, we may use any one of
2959 * them ... this coding arbitrarily picks the lowest comparison type
2960 * number.
2961 */
2962 i = bms_next_member(cmptypes, -1);
2963 if (i < 0)
2964 {
2965 /* No common interpretation, so fail */
2966 ereport(ERROR,
2968 errmsg("could not determine interpretation of row comparison operator %s",
2969 strVal(llast(opname))),
2970 errhint("Row comparison operators must be associated with btree operator families."),
2971 parser_errposition(pstate, location)));
2972 }
2973 cmptype = (CompareType) i;
2974
2975 /*
2976 * For = and <> cases, we just combine the pairwise operators with AND or
2977 * OR respectively.
2978 */
2979 if (cmptype == COMPARE_EQ)
2980 return (Node *) makeBoolExpr(AND_EXPR, opexprs, location);
2981 if (cmptype == COMPARE_NE)
2982 return (Node *) makeBoolExpr(OR_EXPR, opexprs, location);
2983
2984 /*
2985 * Otherwise we need to choose exactly which opfamily to associate with
2986 * each operator.
2987 */
2988 opfamilies = NIL;
2989 for (i = 0; i < nopers; i++)
2990 {
2991 Oid opfamily = InvalidOid;
2992 ListCell *j;
2993
2994 foreach(j, opinfo_lists[i])
2995 {
2997
2998 if (opinfo->cmptype == cmptype)
2999 {
3000 opfamily = opinfo->opfamily_id;
3001 break;
3002 }
3003 }
3004 if (OidIsValid(opfamily))
3005 opfamilies = lappend_oid(opfamilies, opfamily);
3006 else /* should not happen */
3007 ereport(ERROR,
3009 errmsg("could not determine interpretation of row comparison operator %s",
3010 strVal(llast(opname))),
3011 errdetail("There are multiple equally-plausible candidates."),
3012 parser_errposition(pstate, location)));
3013 }
3014
3015 /*
3016 * Now deconstruct the OpExprs and create a RowCompareExpr.
3017 *
3018 * Note: can't just reuse the passed largs/rargs lists, because of
3019 * possibility that make_op inserted coercion operations.
3020 */
3021 opnos = NIL;
3022 largs = NIL;
3023 rargs = NIL;
3024 foreach(l, opexprs)
3025 {
3026 OpExpr *cmp = (OpExpr *) lfirst(l);
3027
3028 opnos = lappend_oid(opnos, cmp->opno);
3029 largs = lappend(largs, linitial(cmp->args));
3030 rargs = lappend(rargs, lsecond(cmp->args));
3031 }
3032
3034 rcexpr->cmptype = cmptype;
3035 rcexpr->opnos = opnos;
3036 rcexpr->opfamilies = opfamilies;
3037 rcexpr->inputcollids = NIL; /* assign_expr_collations will fix this */
3038 rcexpr->largs = largs;
3039 rcexpr->rargs = rargs;
3040
3041 return (Node *) rcexpr;
3042}
Bitmapset * bms_int_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:1093
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
CompareType
Definition cmptype.h:32
@ COMPARE_EQ
Definition cmptype.h:36
@ COMPARE_NE
Definition cmptype.h:39
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define palloc_array(type, count)
Definition fe_memutils.h:76
int j
Definition isn.c:78
int i
Definition isn.c:77
List * lappend(List *list, void *datum)
Definition list.c:339
List * lappend_oid(List *list, Oid datum)
Definition list.c:375
List * get_op_index_interpretation(Oid opno)
Definition lsyscache.c:668
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
Definition makefuncs.c:420
bool expression_returns_set(Node *clause)
Definition nodeFuncs.c:768
#define castNode(_type_, nodeptr)
Definition nodes.h:182
#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:964
@ OR_EXPR
Definition primnodes.h:964
static int cmp(const chr *x, const chr *y, size_t len)
Definition pg_list.h:54
#define strVal(v)
Definition value.h:82

References AND_EXPR, bms_add_member(), bms_int_members(), bms_next_member(), castNode, cmp(), COMPARE_EQ, COMPARE_NE, ereport, errcode(), errdetail(), errhint(), errmsg, ERROR, expression_returns_set(), fb(), forboth, format_type_be(), get_op_index_interpretation(), i, InvalidOid, j, lappend(), lappend_oid(), lfirst, linitial, list_length(), llast, lsecond, make_op(), makeBoolExpr(), makeNode, NIL, OidIsValid, OR_EXPR, ParseState::p_last_srf, palloc_array, parser_errposition(), 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 3050 of file parse_expr.c.

3053{
3054 Node *result = NULL;
3055 List *largs = lrow->args;
3056 List *rargs = rrow->args;
3057 ListCell *l,
3058 *r;
3059
3060 if (list_length(largs) != list_length(rargs))
3061 ereport(ERROR,
3063 errmsg("unequal number of entries in row expressions"),
3064 parser_errposition(pstate, location)));
3065
3066 forboth(l, largs, r, rargs)
3067 {
3068 Node *larg = (Node *) lfirst(l);
3069 Node *rarg = (Node *) lfirst(r);
3070 Node *cmp;
3071
3072 cmp = (Node *) make_distinct_op(pstate, opname, larg, rarg, location);
3073 if (result == NULL)
3074 result = cmp;
3075 else
3076 result = (Node *) makeBoolExpr(OR_EXPR,
3077 list_make2(result, cmp),
3078 location);
3079 }
3080
3081 if (result == NULL)
3082 {
3083 /* zero-length rows? Generate constant FALSE */
3084 result = makeBoolConst(false, false);
3085 }
3086
3087 return result;
3088}
Node * makeBoolConst(bool value, bool isnull)
Definition makefuncs.c:408
static Expr * make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location)

References cmp(), ereport, errcode(), errmsg, ERROR, fb(), 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 3300 of file parse_expr.c.

3301{
3304 list_make2(expr, encoding),
3307
3308 fexpr->location = location;
3309
3310 return (Node *) fexpr;
3311}

References COERCE_EXPLICIT_CALL, encoding, fb(), format, getJsonEncodingConst(), InvalidOid, list_make2, 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 3688 of file parse_expr.c.

3691{
3694 Node *coercion;
3695
3696 jsctor->args = args;
3697 jsctor->func = fexpr;
3698 jsctor->type = type;
3699 jsctor->returning = returning;
3700 jsctor->unique = unique;
3701 jsctor->absent_on_null = absent_on_null;
3702 jsctor->location = location;
3703
3704 /*
3705 * Coerce to the RETURNING type and format, if needed. We abuse
3706 * CaseTestExpr here as placeholder to pass the result of either
3707 * evaluating 'fexpr' or whatever is produced by ExecEvalJsonConstructor()
3708 * that is of type JSON or JSONB to the coercion function.
3709 */
3710 if (fexpr)
3711 {
3713
3714 cte->typeId = exprType((Node *) fexpr);
3715 cte->typeMod = exprTypmod((Node *) fexpr);
3716 cte->collation = exprCollation((Node *) fexpr);
3717
3718 placeholder = (Node *) cte;
3719 }
3720 else
3721 {
3723
3724 cte->typeId = returning->format->format_type == JS_FORMAT_JSONB ?
3725 JSONBOID : JSONOID;
3726 cte->typeMod = -1;
3727 cte->collation = InvalidOid;
3728
3729 placeholder = (Node *) cte;
3730 }
3731
3732 coercion = coerceJsonFuncExpr(pstate, placeholder, returning, true);
3733
3734 if (coercion != placeholder)
3735 jsctor->coercion = (Expr *) coercion;
3736
3737 return (Node *) jsctor;
3738}
int32 exprTypmod(const Node *expr)
Definition nodeFuncs.c:304
Oid exprCollation(const Node *expr)
Definition nodeFuncs.c:826
static Node * coerceJsonFuncExpr(ParseState *pstate, Node *expr, const JsonReturning *returning, bool report_error)
@ JS_FORMAT_JSONB
Definition primnodes.h:1666
const char * type

References coerceJsonFuncExpr(), exprCollation(), exprType(), exprTypmod(), fb(), JsonReturning::format, JsonFormat::format_type, InvalidOid, JS_FORMAT_JSONB, makeNode, type, and CaseTestExpr::typeId.

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

◆ ParseExprKindName()

const char * ParseExprKindName ( ParseExprKind  exprKind)

Definition at line 3153 of file parse_expr.c.

3154{
3155 switch (exprKind)
3156 {
3157 case EXPR_KIND_NONE:
3158 return "invalid expression context";
3159 case EXPR_KIND_OTHER:
3160 return "extension expression";
3161 case EXPR_KIND_JOIN_ON:
3162 return "JOIN/ON";
3164 return "JOIN/USING";
3166 return "sub-SELECT in FROM";
3168 return "function in FROM";
3169 case EXPR_KIND_WHERE:
3170 return "WHERE";
3171 case EXPR_KIND_POLICY:
3172 return "POLICY";
3173 case EXPR_KIND_HAVING:
3174 return "HAVING";
3175 case EXPR_KIND_FILTER:
3176 return "FILTER";
3178 return "window PARTITION BY";
3180 return "window ORDER BY";
3182 return "window RANGE";
3184 return "window ROWS";
3186 return "window GROUPS";
3188 return "SELECT";
3190 return "INSERT";
3193 return "UPDATE";
3195 return "MERGE WHEN";
3196 case EXPR_KIND_GROUP_BY:
3197 return "GROUP BY";
3198 case EXPR_KIND_ORDER_BY:
3199 return "ORDER BY";
3201 return "DISTINCT ON";
3202 case EXPR_KIND_LIMIT:
3203 return "LIMIT";
3204 case EXPR_KIND_OFFSET:
3205 return "OFFSET";
3208 return "RETURNING";
3209 case EXPR_KIND_VALUES:
3211 return "VALUES";
3214 return "CHECK";
3217 return "DEFAULT";
3219 return "index expression";
3221 return "index predicate";
3223 return "statistics expression";
3225 return "USING";
3227 return "EXECUTE";
3229 return "WHEN";
3231 return "partition bound";
3233 return "PARTITION BY";
3235 return "CALL";
3237 return "WHERE";
3239 return "GENERATED AS";
3241 return "CYCLE";
3243 return "property definition expression";
3244
3245 /*
3246 * There is intentionally no default: case here, so that the
3247 * compiler will warn if we add a new ParseExprKind without
3248 * extending this switch. If we do see an unrecognized value at
3249 * runtime, we'll fall through to the "unrecognized" return.
3250 */
3251 }
3252 return "unrecognized expression kind";
3253}
@ 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_PROPGRAPH_PROPERTY
Definition parse_node.h:85
@ 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_PROPGRAPH_PROPERTY, 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, and fb().

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

1302{
1303 Node *aexpr;
1304 Node *bexpr;
1305 Node *cexpr;
1306 Node *result;
1307 Node *sub1;
1308 Node *sub2;
1309 List *args;
1310
1311 /* Deconstruct A_Expr into three subexprs */
1312 aexpr = a->lexpr;
1313 args = castNode(List, a->rexpr);
1314 Assert(list_length(args) == 2);
1315 bexpr = (Node *) linitial(args);
1316 cexpr = (Node *) lsecond(args);
1317
1318 /*
1319 * Build the equivalent comparison expression. Make copies of
1320 * multiply-referenced subexpressions for safety. (XXX this is really
1321 * wrong since it results in multiple runtime evaluations of what may be
1322 * volatile expressions ...)
1323 *
1324 * Ideally we would not use hard-wired operators here but instead use
1325 * opclasses. However, mixed data types and other issues make this
1326 * difficult:
1327 * http://archives.postgresql.org/pgsql-hackers/2008-08/msg01142.php
1328 */
1329 switch (a->kind)
1330 {
1331 case AEXPR_BETWEEN:
1333 aexpr, bexpr,
1334 a->location),
1336 copyObject(aexpr), cexpr,
1337 a->location));
1338 result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1339 break;
1340 case AEXPR_NOT_BETWEEN:
1342 aexpr, bexpr,
1343 a->location),
1345 copyObject(aexpr), cexpr,
1346 a->location));
1347 result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1348 break;
1349 case AEXPR_BETWEEN_SYM:
1351 aexpr, bexpr,
1352 a->location),
1354 copyObject(aexpr), cexpr,
1355 a->location));
1356 sub1 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1358 copyObject(aexpr), copyObject(cexpr),
1359 a->location),
1362 a->location));
1363 sub2 = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1365 result = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1366 break;
1369 aexpr, bexpr,
1370 a->location),
1372 copyObject(aexpr), cexpr,
1373 a->location));
1374 sub1 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1376 copyObject(aexpr), copyObject(cexpr),
1377 a->location),
1380 a->location));
1381 sub2 = (Node *) makeBoolExpr(OR_EXPR, args, a->location);
1383 result = (Node *) makeBoolExpr(AND_EXPR, args, a->location);
1384 break;
1385 default:
1386 elog(ERROR, "unrecognized A_Expr kind: %d", a->kind);
1387 result = NULL; /* keep compiler quiet */
1388 break;
1389 }
1390
1391 return transformExprRecurse(pstate, result);
1392}
int a
Definition isn.c:73
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:232
@ AEXPR_BETWEEN
Definition parsenodes.h:340
@ AEXPR_BETWEEN_SYM
Definition parsenodes.h:342
@ AEXPR_NOT_BETWEEN_SYM
Definition parsenodes.h:343
@ AEXPR_NOT_BETWEEN
Definition parsenodes.h:341
@ AEXPR_OP
Definition parsenodes.h:330

References a, AEXPR_BETWEEN, AEXPR_BETWEEN_SYM, AEXPR_NOT_BETWEEN, AEXPR_NOT_BETWEEN_SYM, AEXPR_OP, AND_EXPR, Assert, castNode, copyObject, elog, ERROR, fb(), 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 1038 of file parse_expr.c.

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

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

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

References a, AND_EXPR, cmp(), coerce_to_boolean(), coerce_to_common_type(), contain_vars_of_level(), copyObject, fb(), get_array_type(), InvalidOid, IsA, lappend(), lfirst, linitial, list_concat(), list_length(), list_make1, list_make2, 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 1089 of file parse_expr.c.

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

References a, OpExpr::args, ereport, errcode(), errmsg, ERROR, exprType(), fb(), 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 929 of file parse_expr.c.

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

References a, NullTest::arg, castNode, EXPR_SUBLINK, exprIsNullConstant(), fb(), 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 1024 of file parse_expr.c.

1025{
1026 Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1027 Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1028
1029 return (Node *) make_scalar_array_op(pstate,
1030 a->name,
1031 false,
1032 lexpr,
1033 rexpr,
1034 a->location);
1035}

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

Referenced by transformExprRecurse().

◆ transformAExprOpAny()

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

Definition at line 1010 of file parse_expr.c.

1011{
1012 Node *lexpr = transformExprRecurse(pstate, a->lexpr);
1013 Node *rexpr = transformExprRecurse(pstate, a->rexpr);
1014
1015 return (Node *) make_scalar_array_op(pstate,
1016 a->name,
1017 true,
1018 lexpr,
1019 rexpr,
1020 a->location);
1021}

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

2037{
2039 List *newelems = NIL;
2043 bool coerce_hard;
2044
2045 /*
2046 * Transform the element expressions
2047 *
2048 * Assume that the array is one-dimensional unless we find an array-type
2049 * element expression.
2050 */
2051 newa->multidims = false;
2052 foreach(element, a->elements)
2053 {
2054 Node *e = (Node *) lfirst(element);
2055 Node *newe;
2056
2057 /*
2058 * If an element is itself an A_ArrayExpr, recurse directly so that we
2059 * can pass down any target type we were given.
2060 */
2061 if (IsA(e, A_ArrayExpr))
2062 {
2063 newe = transformArrayExpr(pstate,
2064 (A_ArrayExpr *) e,
2065 array_type,
2066 element_type,
2067 typmod);
2068 /* we certainly have an array here */
2069 Assert(array_type == InvalidOid || array_type == exprType(newe));
2070 newa->multidims = true;
2071 }
2072 else
2073 {
2074 newe = transformExprRecurse(pstate, e);
2075
2076 /*
2077 * Check for sub-array expressions, if we haven't already found
2078 * one. Note we don't accept domain-over-array as a sub-array,
2079 * nor int2vector nor oidvector; those have constraints that don't
2080 * map well to being treated as a sub-array.
2081 */
2082 if (!newa->multidims)
2083 {
2085
2088 newa->multidims = true;
2089 }
2090 }
2091
2093 }
2094
2095 /*
2096 * Select a target type for the elements.
2097 *
2098 * If we haven't been given a target array type, we must try to deduce a
2099 * common type based on the types of the individual elements present.
2100 */
2101 if (OidIsValid(array_type))
2102 {
2103 /* Caller must ensure array_type matches element_type */
2104 Assert(OidIsValid(element_type));
2105 coerce_type = (newa->multidims ? array_type : element_type);
2106 coerce_hard = true;
2107 }
2108 else
2109 {
2110 /* Can't handle an empty array without a target type */
2111 if (newelems == NIL)
2112 ereport(ERROR,
2114 errmsg("cannot determine type of empty array"),
2115 errhint("Explicitly cast to the desired type, "
2116 "for example ARRAY[]::integer[]."),
2117 parser_errposition(pstate, a->location)));
2118
2119 /* Select a common type for the elements */
2120 coerce_type = select_common_type(pstate, newelems, "ARRAY", NULL);
2121
2122 if (newa->multidims)
2123 {
2124 array_type = coerce_type;
2125 element_type = get_element_type(array_type);
2126 if (!OidIsValid(element_type))
2127 ereport(ERROR,
2129 errmsg("could not find element type for data type %s",
2130 format_type_be(array_type)),
2131 parser_errposition(pstate, a->location)));
2132 }
2133 else
2134 {
2135 element_type = coerce_type;
2136 array_type = get_array_type(element_type);
2137 if (!OidIsValid(array_type))
2138 ereport(ERROR,
2140 errmsg("could not find array type for data type %s",
2141 format_type_be(element_type)),
2142 parser_errposition(pstate, a->location)));
2143 }
2144 coerce_hard = false;
2145 }
2146
2147 /*
2148 * Coerce elements to target type
2149 *
2150 * If the array has been explicitly cast, then the elements are in turn
2151 * explicitly coerced.
2152 *
2153 * If the array's type was merely derived from the common type of its
2154 * elements, then the elements are implicitly coerced to the common type.
2155 * This is consistent with other uses of select_common_type().
2156 */
2157 foreach(element, newelems)
2158 {
2159 Node *e = (Node *) lfirst(element);
2160 Node *newe;
2161
2162 if (coerce_hard)
2163 {
2164 newe = coerce_to_target_type(pstate, e,
2165 exprType(e),
2167 typmod,
2170 -1);
2171 if (newe == NULL)
2172 ereport(ERROR,
2174 errmsg("cannot cast type %s to %s",
2177 parser_errposition(pstate, exprLocation(e))));
2178 }
2179 else
2180 newe = coerce_to_common_type(pstate, e,
2182 "ARRAY");
2184 }
2185
2186 newa->array_typeid = array_type;
2187 /* array_collid will be set by parse_collate.c */
2188 newa->element_typeid = element_type;
2189 newa->elements = newcoercedelems;
2190 newa->list_start = a->list_start;
2191 newa->list_end = a->list_end;
2192 newa->location = a->location;
2193
2194 return (Node *) newa;
2195}
Oid get_element_type(Oid typid)
Definition lsyscache.c:2981
#define type_is_array(typid)
Definition lsyscache.h:219
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
static Node * transformArrayExpr(ParseState *pstate, A_ArrayExpr *a, Oid array_type, Oid element_type, int32 typmod)
e
@ COERCE_EXPLICIT_CAST
Definition primnodes.h:768
@ COERCION_EXPLICIT
Definition primnodes.h:750
static chr element(struct vars *v, const chr *startp, const chr *endp)

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

Referenced by transformArrayExpr(), transformExprRecurse(), and transformTypeCast().

◆ transformBooleanTest()

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

Definition at line 2550 of file parse_expr.c.

2551{
2552 const char *clausename;
2553
2554 switch (b->booltesttype)
2555 {
2556 case IS_TRUE:
2557 clausename = "IS TRUE";
2558 break;
2559 case IS_NOT_TRUE:
2560 clausename = "IS NOT TRUE";
2561 break;
2562 case IS_FALSE:
2563 clausename = "IS FALSE";
2564 break;
2565 case IS_NOT_FALSE:
2566 clausename = "IS NOT FALSE";
2567 break;
2568 case IS_UNKNOWN:
2569 clausename = "IS UNKNOWN";
2570 break;
2571 case IS_NOT_UNKNOWN:
2572 clausename = "IS NOT UNKNOWN";
2573 break;
2574 default:
2575 elog(ERROR, "unrecognized booltesttype: %d",
2576 (int) b->booltesttype);
2577 clausename = NULL; /* keep compiler quiet */
2578 }
2579
2580 b->arg = (Expr *) transformExprRecurse(pstate, (Node *) b->arg);
2581
2582 b->arg = (Expr *) coerce_to_boolean(pstate,
2583 (Node *) b->arg,
2584 clausename);
2585
2586 return (Node *) b;
2587}
int b
Definition isn.c:74
@ IS_NOT_TRUE
Definition primnodes.h:2003
@ IS_NOT_FALSE
Definition primnodes.h:2003
@ IS_NOT_UNKNOWN
Definition primnodes.h:2003
@ IS_TRUE
Definition primnodes.h:2003
@ IS_UNKNOWN
Definition primnodes.h:2003
@ IS_FALSE
Definition primnodes.h:2003

References b, coerce_to_boolean(), elog, ERROR, fb(), 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 1420 of file parse_expr.c.

1421{
1422 List *args = NIL;
1423 const char *opname;
1424 ListCell *lc;
1425
1426 switch (a->boolop)
1427 {
1428 case AND_EXPR:
1429 opname = "AND";
1430 break;
1431 case OR_EXPR:
1432 opname = "OR";
1433 break;
1434 case NOT_EXPR:
1435 opname = "NOT";
1436 break;
1437 default:
1438 elog(ERROR, "unrecognized boolop: %d", (int) a->boolop);
1439 opname = NULL; /* keep compiler quiet */
1440 break;
1441 }
1442
1443 foreach(lc, a->args)
1444 {
1445 Node *arg = (Node *) lfirst(lc);
1446
1447 arg = transformExprRecurse(pstate, arg);
1448 arg = coerce_to_boolean(pstate, arg, opname);
1449 args = lappend(args, arg);
1450 }
1451
1452 return (Node *) makeBoolExpr(a->boolop, args, a->location);
1453}

References a, AND_EXPR, arg, coerce_to_boolean(), elog, ERROR, fb(), 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 1649 of file parse_expr.c.

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

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

Referenced by transformExprRecurse().

◆ transformCoalesceExpr()

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

Definition at line 2236 of file parse_expr.c.

2237{
2239 Node *last_srf = pstate->p_last_srf;
2240 List *newargs = NIL;
2242 ListCell *args;
2243
2244 foreach(args, c->args)
2245 {
2246 Node *e = (Node *) lfirst(args);
2247 Node *newe;
2248
2249 newe = transformExprRecurse(pstate, e);
2251 }
2252
2253 newc->coalescetype = select_common_type(pstate, newargs, "COALESCE", NULL);
2254 /* coalescecollid will be set by parse_collate.c */
2255
2256 /* Convert arguments if necessary */
2257 foreach(args, newargs)
2258 {
2259 Node *e = (Node *) lfirst(args);
2260 Node *newe;
2261
2262 newe = coerce_to_common_type(pstate, e,
2263 newc->coalescetype,
2264 "COALESCE");
2266 }
2267
2268 /* if any subexpression contained a SRF, complain */
2269 if (pstate->p_last_srf != last_srf)
2270 ereport(ERROR,
2272 /* translator: %s is name of a SQL construct, eg GROUP BY */
2273 errmsg("set-returning functions are not allowed in %s",
2274 "COALESCE"),
2275 errhint("You might be able to move the set-returning function into a LATERAL FROM item."),
2276 parser_errposition(pstate,
2277 exprLocation(pstate->p_last_srf))));
2278
2279 newc->args = newcoercedargs;
2280 newc->location = c->location;
2281 return (Node *) newc;
2282}

References coerce_to_common_type(), ereport, errcode(), errhint(), errmsg, ERROR, exprLocation(), fb(), lappend(), lfirst, 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 2808 of file parse_expr.c.

2809{
2811 Oid argtype;
2812
2814 newc->arg = (Expr *) transformExprRecurse(pstate, c->arg);
2815
2816 argtype = exprType((Node *) newc->arg);
2817
2818 /*
2819 * The unknown type is not collatable, but coerce_type() takes care of it
2820 * separately, so we'll let it go here.
2821 */
2822 if (!type_is_collatable(argtype) && argtype != UNKNOWNOID)
2823 ereport(ERROR,
2825 errmsg("collations are not supported by type %s",
2826 format_type_be(argtype)),
2827 parser_errposition(pstate, c->location)));
2828
2829 newc->collOid = LookupCollation(pstate, c->collname, c->location);
2830 newc->location = c->location;
2831
2832 return (Node *) newc;
2833}
bool type_is_collatable(Oid typid)
Definition lsyscache.c:3303
Oid LookupCollation(ParseState *pstate, List *collnames, int location)
Definition parse_type.c:515

References ereport, errcode(), errmsg, ERROR, exprType(), fb(), format_type_be(), LookupCollation(), makeNode, parser_errposition(), transformExprRecurse(), and type_is_collatable().

Referenced by transformExprRecurse().

◆ transformColumnRef()

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

Definition at line 511 of file parse_expr.c.

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

Referenced by transformExprRecurse().

◆ transformCurrentOfExpr()

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

Definition at line 2590 of file parse_expr.c.

2591{
2592 /* CURRENT OF can only appear at top level of UPDATE/DELETE */
2593 Assert(pstate->p_target_nsitem != NULL);
2594 cexpr->cvarno = pstate->p_target_nsitem->p_rtindex;
2595
2596 /*
2597 * Check to see if the cursor name matches a parameter of type REFCURSOR.
2598 * If so, replace the raw name reference with a parameter reference. (This
2599 * is a hack for the convenience of plpgsql.)
2600 */
2601 if (cexpr->cursor_name != NULL) /* in case already transformed */
2602 {
2604 Node *node = NULL;
2605
2606 /* Build an unqualified ColumnRef with the given name */
2607 cref->fields = list_make1(makeString(cexpr->cursor_name));
2608 cref->location = -1;
2609
2610 /* See if there is a translation available from a parser hook */
2611 if (pstate->p_pre_columnref_hook != NULL)
2612 node = pstate->p_pre_columnref_hook(pstate, cref);
2613 if (node == NULL && pstate->p_post_columnref_hook != NULL)
2614 node = pstate->p_post_columnref_hook(pstate, cref, NULL);
2615
2616 /*
2617 * XXX Should we throw an error if we get a translation that isn't a
2618 * refcursor Param? For now it seems best to silently ignore false
2619 * matches.
2620 */
2621 if (node != NULL && IsA(node, Param))
2622 {
2623 Param *p = (Param *) node;
2624
2625 if (p->paramkind == PARAM_EXTERN &&
2626 p->paramtype == REFCURSOROID)
2627 {
2628 /* Matches, so convert CURRENT OF to a param reference */
2629 cexpr->cursor_name = NULL;
2630 cexpr->cursor_param = p->paramid;
2631 }
2632 }
2633 }
2634
2635 return (Node *) cexpr;
2636}
@ PARAM_EXTERN
Definition primnodes.h:385
char * cursor_name
Definition primnodes.h:2125
int paramid
Definition primnodes.h:397
Oid paramtype
Definition primnodes.h:398
ParamKind paramkind
Definition primnodes.h:396
ParseNamespaceItem * p_target_nsitem
Definition parse_node.h:225

References Assert, CurrentOfExpr::cursor_name, CurrentOfExpr::cursor_param, CurrentOfExpr::cvarno, fb(), IsA, list_make1, 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 
)

Definition at line 121 of file parse_expr.c.

122{
123 Node *result;
125
126 /* Save and restore identity of expression type we're parsing */
128 sv_expr_kind = pstate->p_expr_kind;
129 pstate->p_expr_kind = exprKind;
130
131 result = transformExprRecurse(pstate, expr);
132
133 pstate->p_expr_kind = sv_expr_kind;
134
135 return result;
136}
ParseExprKind
Definition parse_node.h:39

References Assert, EXPR_KIND_NONE, fb(), ParseState::p_expr_kind, and transformExprRecurse().

Referenced by analyzeCTE(), array_subscript_transform(), cookConstraint(), cookDefault(), DoCopy(), domainAddCheckConstraint(), EvaluateParams(), ExpandIndirectionStar(), findTargetlistEntrySQL99(), hstore_subscript_transform(), interpret_function_parameter_list(), jsonb_subscript_transform(), resolve_unique_index_expr(), transformAlterTableStmt(), transformCallStmt(), transformExpressionList(), transformFrameOffset(), transformFuncCall(), transformGraphElementPattern(), transformGraphPattern(), transformGroupingFunc(), transformIndexStmt(), transformJoinUsingClause(), transformJsonTable(), transformJsonTableColumns(), transformLimitClause(), transformMergeStmt(), transformOnConflictArbiter(), transformPartitionBoundValue(), transformPartitionSpec(), transformPLAssignStmt(), transformRangeFunction(), transformRangeGraphTable(), transformRangeTableFunc(), transformRangeTableSample(), transformReturnStmt(), transformStatsStmt(), transformTargetEntry(), and transformWhereClause().

◆ transformExprRecurse()

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

Definition at line 139 of file parse_expr.c.

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

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, NullTest::arg, check_stack_depth(), elog, ereport, errcode(), errmsg, ERROR, exprType(), fb(), InvalidOid, make_const(), nodeTag, parser_errposition(), transformAExprBetween(), transformAExprDistinct(), transformAExprIn(), transformAExprNullIf(), transformAExprOp(), transformAExprOpAll(), transformAExprOpAny(), transformArrayExpr(), transformBooleanTest(), transformBoolExpr(), transformCaseExpr(), transformCoalesceExpr(), transformCollateClause(), transformColumnRef(), transformCurrentOfExpr(), transformExprRecurse(), 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(), transformExprRecurse(), 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 1456 of file parse_expr.c.

1457{
1458 Node *last_srf = pstate->p_last_srf;
1459 List *targs;
1460 ListCell *args;
1461
1462 /* Transform the list of arguments ... */
1463 targs = NIL;
1464 foreach(args, fn->args)
1465 {
1467 (Node *) lfirst(args)));
1468 }
1469
1470 /*
1471 * When WITHIN GROUP is used, we treat its ORDER BY expressions as
1472 * additional arguments to the function, for purposes of function lookup
1473 * and argument type coercion. So, transform each such expression and add
1474 * them to the targs list. We don't explicitly mark where each argument
1475 * came from, but ParseFuncOrColumn can tell what's what by reference to
1476 * list_length(fn->agg_order).
1477 */
1478 if (fn->agg_within_group)
1479 {
1480 Assert(fn->agg_order != NIL);
1481 foreach(args, fn->agg_order)
1482 {
1483 SortBy *arg = (SortBy *) lfirst(args);
1484
1485 targs = lappend(targs, transformExpr(pstate, arg->node,
1487 }
1488 }
1489
1490 /* ... and hand off to ParseFuncOrColumn */
1491 return ParseFuncOrColumn(pstate,
1492 fn->funcname,
1493 targs,
1494 last_srf,
1495 fn,
1496 false,
1497 fn->location);
1498}
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
Definition parse_expr.c:121
static void * fn(void *arg)

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

Referenced by transformExprRecurse().

◆ transformIndirection()

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

Definition at line 439 of file parse_expr.c.

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

References Assert, ereport, errcode(), errmsg, ERROR, exprLocation(), exprType(), exprTypmod(), fb(), 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 3856 of file parse_expr.c.

3861{
3862 Node *node;
3863 Expr *aggfilter;
3864
3865 aggfilter = agg_ctor->agg_filter ? (Expr *)
3866 transformWhereClause(pstate, agg_ctor->agg_filter,
3867 EXPR_KIND_FILTER, "FILTER") : NULL;
3868
3869 if (agg_ctor->over)
3870 {
3871 /* window function */
3872 WindowFunc *wfunc = makeNode(WindowFunc);
3873
3874 wfunc->winfnoid = aggfnoid;
3875 wfunc->wintype = aggtype;
3876 /* wincollid and inputcollid will be set by parse_collate.c */
3877 wfunc->args = args;
3878 wfunc->aggfilter = aggfilter;
3879 wfunc->runCondition = NIL;
3880 /* winref will be set by transformWindowFuncCall */
3881 wfunc->winstar = false;
3882 wfunc->winagg = true;
3883 wfunc->location = agg_ctor->location;
3884
3885 /*
3886 * ordered aggs not allowed in windows yet
3887 */
3888 if (agg_ctor->agg_order != NIL)
3889 ereport(ERROR,
3891 errmsg("aggregate ORDER BY is not implemented for window functions"),
3892 parser_errposition(pstate, agg_ctor->location));
3893
3894 /* parse_agg.c does additional window-func-specific processing */
3895 transformWindowFuncCall(pstate, wfunc, agg_ctor->over);
3896
3897 node = (Node *) wfunc;
3898 }
3899 else
3900 {
3901 Aggref *aggref = makeNode(Aggref);
3902
3903 aggref->aggfnoid = aggfnoid;
3904 aggref->aggtype = aggtype;
3905
3906 /* aggcollid and inputcollid will be set by parse_collate.c */
3907 /* aggtranstype will be set by planner */
3908 /* aggargtypes will be set by transformAggregateCall */
3909 /* aggdirectargs and args will be set by transformAggregateCall */
3910 /* aggorder and aggdistinct will be set by transformAggregateCall */
3911 aggref->aggfilter = aggfilter;
3912 aggref->aggstar = false;
3913 aggref->aggvariadic = false;
3914 aggref->aggkind = AGGKIND_NORMAL;
3915 aggref->aggpresorted = false;
3916 /* agglevelsup will be set by transformAggregateCall */
3917 aggref->aggsplit = AGGSPLIT_SIMPLE; /* planner might change this */
3918 aggref->aggno = -1; /* planner will set aggno and aggtransno */
3919 aggref->aggtransno = -1;
3920 aggref->location = agg_ctor->location;
3921
3922 transformAggregateCall(pstate, aggref, args, agg_ctor->agg_order, false);
3923
3924 node = (Node *) aggref;
3925 }
3926
3927 return makeJsonConstructorExpr(pstate, ctor_type, NIL, (Expr *) node,
3928 returning, unique, absent_on_null,
3929 agg_ctor->location);
3930}
@ AGGSPLIT_SIMPLE
Definition nodes.h:387
void transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, WindowDef *windef)
Definition parse_agg.c:887
void transformAggregateCall(ParseState *pstate, Aggref *agg, List *args, List *aggorder, bool agg_distinct)
Definition parse_agg.c:113
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)
Oid aggfnoid
Definition primnodes.h:464
Expr * aggfilter
Definition primnodes.h:497
ParseLoc location
Definition primnodes.h:527
List * args
Definition primnodes.h:606
Expr * aggfilter
Definition primnodes.h:608
ParseLoc location
Definition primnodes.h:620

References Aggref::aggfilter, WindowFunc::aggfilter, Aggref::aggfnoid, AGGSPLIT_SIMPLE, WindowFunc::args, ereport, errcode(), errmsg, ERROR, EXPR_KIND_FILTER, fb(), Aggref::location, WindowFunc::location, makeJsonConstructorExpr(), makeNode, NIL, 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 4006 of file parse_expr.c.

4007{
4008 JsonReturning *returning;
4009 Node *arg;
4010 Oid aggfnoid;
4011 Oid aggtype;
4012
4013 arg = transformJsonValueExpr(pstate, "JSON_ARRAYAGG()", agg->arg,
4015
4016 returning = transformJsonConstructorOutput(pstate, agg->constructor->output,
4017 list_make1(arg));
4018
4019 if (returning->format->format_type == JS_FORMAT_JSONB)
4020 {
4021 aggfnoid = agg->absent_on_null ? F_JSONB_AGG_STRICT : F_JSONB_AGG;
4022 aggtype = JSONBOID;
4023 }
4024 else
4025 {
4026 aggfnoid = agg->absent_on_null ? F_JSON_AGG_STRICT : F_JSON_AGG;
4027 aggtype = JSONOID;
4028 }
4029
4030 return transformJsonAggConstructor(pstate, agg->constructor, returning,
4031 list_make1(arg), aggfnoid, aggtype,
4033 false, agg->absent_on_null);
4034}
static JsonReturning * transformJsonConstructorOutput(ParseState *pstate, JsonOutput *output, List *args)
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 Node * transformJsonValueExpr(ParseState *pstate, const char *constructName, JsonValueExpr *ve, JsonFormatType default_format, Oid targettype, bool isarg)
@ JSCTOR_JSON_ARRAYAGG
Definition primnodes.h:1719

References arg, fb(), JsonReturning::format, JsonFormat::format_type, InvalidOid, JS_FORMAT_DEFAULT, JS_FORMAT_JSONB, JSCTOR_JSON_ARRAYAGG, list_make1, transformJsonAggConstructor(), transformJsonConstructorOutput(), and transformJsonValueExpr().

Referenced by transformExprRecurse().

◆ transformJsonArrayConstructor()

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

Definition at line 4044 of file parse_expr.c.

4045{
4046 JsonReturning *returning;
4047 List *args = NIL;
4048
4049 /* transform element expressions, if any */
4050 if (ctor->exprs)
4051 {
4052 ListCell *lc;
4053
4054 /* transform and append element arguments */
4055 foreach(lc, ctor->exprs)
4056 {
4058 Node *val = transformJsonValueExpr(pstate, "JSON_ARRAY()",
4060 InvalidOid, false);
4061
4062 args = lappend(args, val);
4063 }
4064 }
4065
4066 returning = transformJsonConstructorOutput(pstate, ctor->output, args);
4067
4068 return makeJsonConstructorExpr(pstate, JSCTOR_JSON_ARRAY, args, NULL,
4069 returning, false, ctor->absent_on_null,
4070 ctor->location);
4071}
@ JSCTOR_JSON_ARRAY
Definition primnodes.h:1717

References castNode, fb(), InvalidOid, JS_FORMAT_DEFAULT, JSCTOR_JSON_ARRAY, lappend(), lfirst, makeJsonConstructorExpr(), NIL, transformJsonConstructorOutput(), transformJsonValueExpr(), and val.

Referenced by transformExprRecurse().

◆ transformJsonArrayQueryConstructor()

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

Definition at line 3785 of file parse_expr.c.

3787{
3791 Alias *alias = makeNode(Alias);
3792 ResTarget *target = makeNode(ResTarget);
3795 Query *query;
3797
3798 /* Transform query only for counting target list entries. */
3799 qpstate = make_parsestate(pstate);
3800
3801 query = transformStmt(qpstate, copyObject(ctor->query));
3802
3803 if (count_nonjunk_tlist_entries(query->targetList) != 1)
3804 ereport(ERROR,
3806 errmsg("subquery must return only one column"),
3807 parser_errposition(pstate, ctor->location));
3808
3810
3811 colref->fields = list_make2(makeString(pstrdup("q")),
3812 makeString(pstrdup("a")));
3813 colref->location = ctor->location;
3814
3815 /*
3816 * No formatting necessary, so set formatted_expr to be the same as
3817 * raw_expr.
3818 */
3819 agg->arg = makeJsonValueExpr((Expr *) colref, (Expr *) colref,
3820 ctor->format);
3821 agg->absent_on_null = ctor->absent_on_null;
3822 agg->constructor = makeNode(JsonAggConstructor);
3823 agg->constructor->agg_order = NIL;
3824 agg->constructor->output = ctor->output;
3825 agg->constructor->location = ctor->location;
3826
3827 target->name = NULL;
3828 target->indirection = NIL;
3829 target->val = (Node *) agg;
3830 target->location = ctor->location;
3831
3832 alias->aliasname = pstrdup("q");
3833 alias->colnames = list_make1(makeString(pstrdup("a")));
3834
3835 range->lateral = false;
3836 range->subquery = ctor->query;
3837 range->alias = alias;
3838
3839 select->targetList = list_make1(target);
3840 select->fromClause = list_make1(range);
3841
3842 sublink->subLinkType = EXPR_SUBLINK;
3843 sublink->subLinkId = 0;
3844 sublink->testexpr = NULL;
3845 sublink->operName = NIL;
3846 sublink->subselect = (Node *) select;
3847 sublink->location = ctor->location;
3848
3849 return transformExprRecurse(pstate, (Node *) sublink);
3850}
JsonValueExpr * makeJsonValueExpr(Expr *raw_expr, Expr *formatted_expr, JsonFormat *format)
Definition makefuncs.c:938
char * pstrdup(const char *in)
Definition mcxt.c:1781
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:324
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
char * aliasname
Definition primnodes.h:52
List * colnames
Definition primnodes.h:53
List * targetList
Definition parsenodes.h:198
Node * val
Definition parsenodes.h:547
ParseLoc location
Definition parsenodes.h:548
List * indirection
Definition parsenodes.h:546
char * name
Definition parsenodes.h:545
int count_nonjunk_tlist_entries(List *tlist)
Definition tlist.c:195
#define select(n, r, w, e, timeout)
Definition win32_port.h:500

References Alias::aliasname, Alias::colnames, copyObject, count_nonjunk_tlist_entries(), ereport, errcode(), errmsg, ERROR, EXPR_SUBLINK, fb(), free_parsestate(), ResTarget::indirection, list_make1, list_make2, ResTarget::location, make_parsestate(), makeJsonValueExpr(), makeNode, makeString(), ResTarget::name, NIL, parser_errposition(), pstrdup(), range(), select, Query::targetList, transformExprRecurse(), transformStmt(), and ResTarget::val.

Referenced by transformExprRecurse().

◆ transformJsonBehavior()

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

Definition at line 4755 of file parse_expr.c.

4759{
4761 Node *expr = NULL;
4762 bool coerce_at_runtime = false;
4763 int location = -1;
4764
4765 if (behavior)
4766 {
4767 btype = behavior->btype;
4768 location = behavior->location;
4769 if (btype == JSON_BEHAVIOR_DEFAULT)
4770 {
4771 Oid targetcoll = jsexpr->collation;
4772 Oid exprcoll;
4773
4774 expr = transformExprRecurse(pstate, behavior->expr);
4775
4777 ereport(ERROR,
4779 errmsg("can only specify a constant, non-aggregate function, or operator expression for DEFAULT"),
4780 parser_errposition(pstate, exprLocation(expr))));
4781 if (contain_var_clause(expr))
4782 ereport(ERROR,
4784 errmsg("DEFAULT expression must not contain column references"),
4785 parser_errposition(pstate, exprLocation(expr))));
4786 if (expression_returns_set(expr))
4787 ereport(ERROR,
4789 errmsg("DEFAULT expression must not return a set"),
4790 parser_errposition(pstate, exprLocation(expr))));
4791
4792 /*
4793 * Reject a DEFAULT expression whose collation differs from the
4794 * enclosing JSON expression's result collation
4795 * (jsexpr->collation), as chosen by the RETURNING clause.
4796 */
4797 exprcoll = exprCollation(expr);
4798 if (!OidIsValid(exprcoll))
4802 ereport(ERROR,
4804 errmsg("collation of DEFAULT expression conflicts with RETURNING clause"),
4805 errdetail("\"%s\" versus \"%s\"",
4808 parser_errposition(pstate, exprLocation(expr)));
4809 }
4810 }
4811
4812 if (expr == NULL && btype != JSON_BEHAVIOR_ERROR)
4813 expr = GetJsonBehaviorConst(btype, location);
4814
4815 /*
4816 * Try to coerce the expression if needed.
4817 *
4818 * Use runtime coercion using json_populate_type() if the expression is
4819 * NULL, jsonb-valued, or boolean-valued (unless the target type is
4820 * integer or domain over integer, in which case use the
4821 * boolean-to-integer cast function).
4822 *
4823 * For other non-NULL expressions, try to find a cast and error out if one
4824 * is not found.
4825 */
4826 if (expr && exprType(expr) != returning->typid)
4827 {
4828 bool isnull = (IsA(expr, Const) && ((Const *) expr)->constisnull);
4829
4830 if (isnull ||
4831 exprType(expr) == JSONBOID ||
4832 (exprType(expr) == BOOLOID &&
4833 getBaseType(returning->typid) != INT4OID))
4834 {
4835 coerce_at_runtime = true;
4836
4837 /*
4838 * json_populate_type() expects to be passed a jsonb value, so gin
4839 * up a Const containing the appropriate boolean value represented
4840 * as jsonb, discarding the original Const containing a plain
4841 * boolean.
4842 */
4843 if (exprType(expr) == BOOLOID)
4844 {
4845 char *val = btype == JSON_BEHAVIOR_TRUE ? "true" : "false";
4846
4847 expr = (Node *) makeConst(JSONBOID, -1, InvalidOid, -1,
4850 false, false);
4851 }
4852 }
4853 else
4854 {
4856 char typcategory = TypeCategory(returning->typid);
4857
4858 /*
4859 * Use an assignment cast if coercing to a string type so that
4860 * build_coercion_expression() assumes implicit coercion when
4861 * coercing the typmod, so that inputs exceeding length cause an
4862 * error instead of silent truncation.
4863 */
4864 coerced_expr =
4865 coerce_to_target_type(pstate, expr, exprType(expr),
4866 returning->typid, returning->typmod,
4872 exprLocation((Node *) behavior));
4873
4874 if (coerced_expr == NULL)
4875 {
4876 /*
4877 * Provide a HINT if the expression comes from a DEFAULT
4878 * clause.
4879 */
4880 if (btype == JSON_BEHAVIOR_DEFAULT)
4881 ereport(ERROR,
4883 errmsg("cannot cast behavior expression of type %s to %s",
4884 format_type_be(exprType(expr)),
4885 format_type_be(returning->typid)),
4886 errhint("You will need to explicitly cast the expression to type %s.",
4887 format_type_be(returning->typid)),
4888 parser_errposition(pstate, exprLocation(expr)));
4889 else
4890 ereport(ERROR,
4892 errmsg("cannot cast behavior expression of type %s to %s",
4893 format_type_be(exprType(expr)),
4894 format_type_be(returning->typid)),
4895 parser_errposition(pstate, exprLocation(expr)));
4896 }
4897
4898 expr = coerced_expr;
4899 }
4900 }
4901
4902 if (behavior)
4903 behavior->expr = expr;
4904 else
4905 behavior = makeJsonBehavior(btype, expr, location);
4906
4907 behavior->coerce = coerce_at_runtime;
4908
4909 return behavior;
4910}
Oid get_typcollation(Oid typid)
Definition lsyscache.c:3278
char * get_collation_name(Oid colloid)
Definition lsyscache.c:1181
Oid getBaseType(Oid typid)
Definition lsyscache.c:2743
JsonBehavior * makeJsonBehavior(JsonBehaviorType btype, Node *expr, int location)
Definition makefuncs.c:955
TYPCATEGORY TypeCategory(Oid type)
static bool ValidJsonBehaviorDefaultExpr(Node *expr, void *context)
static Node * GetJsonBehaviorConst(JsonBehaviorType btype, int location)
JsonBehaviorType
Definition primnodes.h:1791
ParseLoc location
Definition primnodes.h:1820
JsonBehaviorType btype
Definition primnodes.h:1817
Oid collation
Definition primnodes.h:1883
bool contain_var_clause(Node *node)
Definition var.c:406

References JsonBehavior::btype, JsonBehavior::coerce, COERCE_EXPLICIT_CAST, coerce_to_target_type(), COERCION_ASSIGNMENT, COERCION_EXPLICIT, JsonExpr::collation, contain_var_clause(), CStringGetDatum(), DirectFunctionCall1, ereport, errcode(), errdetail(), errhint(), errmsg, ERROR, JsonBehavior::expr, exprCollation(), expression_returns_set(), exprLocation(), exprType(), fb(), format_type_be(), get_collation_name(), get_typcollation(), getBaseType(), GetJsonBehaviorConst(), InvalidOid, IsA, JSON_BEHAVIOR_DEFAULT, JSON_BEHAVIOR_ERROR, JSON_BEHAVIOR_TRUE, jsonb_in(), JsonBehavior::location, makeConst(), makeJsonBehavior(), OidIsValid, parser_errposition(), transformExprRecurse(), TypeCategory(), JsonReturning::typid, JsonReturning::typmod, val, and ValidJsonBehaviorDefaultExpr().

Referenced by transformJsonFuncExpr().

◆ transformJsonConstructorOutput()

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

Definition at line 3581 of file parse_expr.c.

3583{
3584 JsonReturning *returning = transformJsonOutput(pstate, output, true);
3585
3586 if (!OidIsValid(returning->typid))
3587 {
3588 ListCell *lc;
3589 bool have_jsonb = false;
3590
3591 foreach(lc, args)
3592 {
3593 Node *expr = lfirst(lc);
3594 Oid typid = exprType(expr);
3595
3596 have_jsonb |= typid == JSONBOID;
3597
3598 if (have_jsonb)
3599 break;
3600 }
3601
3602 if (have_jsonb)
3603 {
3604 returning->typid = JSONBOID;
3605 returning->format->format_type = JS_FORMAT_JSONB;
3606 }
3607 else
3608 {
3609 /* XXX TEXT is default by the standard, but we return JSON */
3610 returning->typid = JSONOID;
3611 returning->format->format_type = JS_FORMAT_JSON;
3612 }
3613
3614 returning->typmod = -1;
3615 }
3616
3617 return returning;
3618}
FILE * output
static JsonReturning * transformJsonOutput(ParseState *pstate, const JsonOutput *output, bool allow_format)

References exprType(), fb(), 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 4306 of file parse_expr.c.

4307{
4308 JsonExpr *jsexpr;
4309 Node *path_spec;
4311 int pathspec_loc;
4313 const char *func_name = NULL;
4315
4316 switch (func->op)
4317 {
4318 case JSON_EXISTS_OP:
4319 func_name = "JSON_EXISTS";
4321 break;
4322 case JSON_QUERY_OP:
4323 func_name = "JSON_QUERY";
4325 break;
4326 case JSON_VALUE_OP:
4327 func_name = "JSON_VALUE";
4329 break;
4330 case JSON_TABLE_OP:
4331 func_name = "JSON_TABLE";
4333 break;
4334 default:
4335 elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4336 default_format = JS_FORMAT_DEFAULT; /* keep compiler quiet */
4337 break;
4338 }
4339
4340 /*
4341 * Even though the syntax allows it, FORMAT JSON specification in
4342 * RETURNING is meaningless except for JSON_QUERY(). Flag if not
4343 * JSON_QUERY().
4344 */
4345 if (func->output && func->op != JSON_QUERY_OP)
4346 {
4348
4349 if (format->format_type != JS_FORMAT_DEFAULT ||
4350 format->encoding != JS_ENC_DEFAULT)
4351 ereport(ERROR,
4353 errmsg("cannot specify FORMAT JSON in RETURNING clause of %s()",
4354 func_name),
4355 parser_errposition(pstate, format->location));
4356 }
4357
4358 /* OMIT QUOTES is meaningless when strings are wrapped. */
4359 if (func->op == JSON_QUERY_OP)
4360 {
4361 if (func->quotes == JS_QUOTES_OMIT &&
4362 (func->wrapper == JSW_CONDITIONAL ||
4363 func->wrapper == JSW_UNCONDITIONAL))
4364 ereport(ERROR,
4366 errmsg("SQL/JSON QUOTES behavior must not be specified when WITH WRAPPER is used"),
4367 parser_errposition(pstate, func->location));
4368 if (func->on_empty != NULL &&
4370 func->on_empty->btype != JSON_BEHAVIOR_NULL &&
4375 {
4376 if (func->column_name == NULL)
4377 ereport(ERROR,
4379 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4380 errmsg("invalid %s behavior", "ON EMPTY"),
4381 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4382 second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4383 errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for %s.",
4384 "ON EMPTY", "JSON_QUERY()"),
4385 parser_errposition(pstate, func->on_empty->location));
4386 else
4387 ereport(ERROR,
4389 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4390 errmsg("invalid %s behavior for column \"%s\"",
4391 "ON EMPTY", func->column_name),
4392 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4393 errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for formatted columns.",
4394 "ON EMPTY"),
4395 parser_errposition(pstate, func->on_empty->location));
4396 }
4397 if (func->on_error != NULL &&
4399 func->on_error->btype != JSON_BEHAVIOR_NULL &&
4404 {
4405 if (func->column_name == NULL)
4406 ereport(ERROR,
4408 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4409 errmsg("invalid %s behavior", "ON ERROR"),
4410 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4411 second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4412 errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for %s.",
4413 "ON ERROR", "JSON_QUERY()"),
4414 parser_errposition(pstate, func->on_error->location));
4415 else
4416 ereport(ERROR,
4418 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4419 errmsg("invalid %s behavior for column \"%s\"",
4420 "ON ERROR", func->column_name),
4421 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4422 errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for formatted columns.",
4423 "ON ERROR"),
4424 parser_errposition(pstate, func->on_error->location));
4425 }
4426 }
4427
4428 /* Check that ON ERROR/EMPTY behavior values are valid for the function. */
4429 if (func->op == JSON_EXISTS_OP &&
4430 func->on_error != NULL &&
4432 func->on_error->btype != JSON_BEHAVIOR_TRUE &&
4435 {
4436 if (func->column_name == NULL)
4437 ereport(ERROR,
4439 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4440 errmsg("invalid %s behavior", "ON ERROR"),
4441 errdetail("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in %s for %s.",
4442 "ON ERROR", "JSON_EXISTS()"),
4443 parser_errposition(pstate, func->on_error->location));
4444 else
4445 ereport(ERROR,
4447 /*- translator: first %s is name a SQL/JSON clause (eg. ON EMPTY) */
4448 errmsg("invalid %s behavior for column \"%s\"",
4449 "ON ERROR", func->column_name),
4450 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4451 errdetail("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in %s for EXISTS columns.",
4452 "ON ERROR"),
4453 parser_errposition(pstate, func->on_error->location));
4454 }
4455 if (func->op == JSON_VALUE_OP)
4456 {
4457 if (func->on_empty != NULL &&
4459 func->on_empty->btype != JSON_BEHAVIOR_NULL &&
4461 {
4462 if (func->column_name == NULL)
4463 ereport(ERROR,
4465 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4466 errmsg("invalid %s behavior", "ON EMPTY"),
4467 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4468 second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4469 errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for %s.",
4470 "ON EMPTY", "JSON_VALUE()"),
4471 parser_errposition(pstate, func->on_empty->location));
4472 else
4473 ereport(ERROR,
4475 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4476 errmsg("invalid %s behavior for column \"%s\"",
4477 "ON EMPTY", func->column_name),
4478 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4479 errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for scalar columns.",
4480 "ON EMPTY"),
4481 parser_errposition(pstate, func->on_empty->location));
4482 }
4483 if (func->on_error != NULL &&
4485 func->on_error->btype != JSON_BEHAVIOR_NULL &&
4487 {
4488 if (func->column_name == NULL)
4489 ereport(ERROR,
4491 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4492 errmsg("invalid %s behavior", "ON ERROR"),
4493 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
4494 second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
4495 errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for %s.",
4496 "ON ERROR", "JSON_VALUE()"),
4497 parser_errposition(pstate, func->on_error->location));
4498 else
4499 ereport(ERROR,
4501 /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4502 errmsg("invalid %s behavior for column \"%s\"",
4503 "ON ERROR", func->column_name),
4504 /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
4505 errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for scalar columns.",
4506 "ON ERROR"),
4507 parser_errposition(pstate, func->on_error->location));
4508 }
4509 }
4510
4511 jsexpr = makeNode(JsonExpr);
4512 jsexpr->location = func->location;
4513 jsexpr->op = func->op;
4514 jsexpr->column_name = func->column_name;
4515
4516 /*
4517 * jsonpath machinery can only handle jsonb documents, so coerce the input
4518 * if not already of jsonb type.
4519 */
4520 jsexpr->formatted_expr = transformJsonValueExpr(pstate, func_name,
4521 func->context_item,
4523 JSONBOID,
4524 false);
4525 jsexpr->format = func->context_item->format;
4526
4527 path_spec = transformExprRecurse(pstate, func->pathspec);
4528 pathspec_type = exprType(path_spec);
4529 pathspec_loc = exprLocation(path_spec);
4530 coerced_path_spec = coerce_to_target_type(pstate, path_spec,
4532 JSONPATHOID, -1,
4535 pathspec_loc);
4536 if (coerced_path_spec == NULL)
4537 ereport(ERROR,
4539 errmsg("JSON path expression must be of type %s, not of type %s",
4540 "jsonpath", format_type_be(pathspec_type)),
4542 jsexpr->path_spec = coerced_path_spec;
4543
4544 /* Transform and coerce the PASSING arguments to jsonb. */
4545 transformJsonPassingArgs(pstate, func_name,
4547 func->passing,
4548 &jsexpr->passing_values,
4549 &jsexpr->passing_names);
4550
4551 /* Transform the JsonOutput into JsonReturning. */
4552 jsexpr->returning = transformJsonOutput(pstate, func->output, false);
4553
4554 switch (func->op)
4555 {
4556 case JSON_EXISTS_OP:
4557 /* JSON_EXISTS returns boolean by default. */
4558 if (!OidIsValid(jsexpr->returning->typid))
4559 {
4560 jsexpr->returning->typid = BOOLOID;
4561 jsexpr->returning->typmod = -1;
4562 jsexpr->collation = InvalidOid;
4563 }
4564
4565 /* JSON_TABLE() COLUMNS can specify a non-boolean type. */
4566 if (jsexpr->returning->typid != BOOLOID)
4567 jsexpr->use_json_coercion = true;
4568
4569 jsexpr->on_error = transformJsonBehavior(pstate,
4570 jsexpr,
4571 func->on_error,
4573 jsexpr->returning);
4574 break;
4575
4576 case JSON_QUERY_OP:
4577 /* JSON_QUERY returns jsonb by default. */
4578 if (!OidIsValid(jsexpr->returning->typid))
4579 {
4580 JsonReturning *ret = jsexpr->returning;
4581
4582 ret->typid = JSONBOID;
4583 ret->typmod = -1;
4584 }
4585
4586 jsexpr->collation = get_typcollation(jsexpr->returning->typid);
4587
4588 /*
4589 * Keep quotes on scalar strings by default, omitting them only if
4590 * OMIT QUOTES is specified.
4591 */
4592 jsexpr->omit_quotes = (func->quotes == JS_QUOTES_OMIT);
4593 jsexpr->wrapper = func->wrapper;
4594
4595 /*
4596 * Set up to coerce the result value of JsonPathValue() to the
4597 * RETURNING type (default or user-specified), if needed. Also if
4598 * OMIT QUOTES is specified.
4599 */
4600 if (jsexpr->returning->typid != JSONBOID || jsexpr->omit_quotes)
4601 jsexpr->use_json_coercion = true;
4602
4603 /* Assume NULL ON EMPTY when ON EMPTY is not specified. */
4604 jsexpr->on_empty = transformJsonBehavior(pstate,
4605 jsexpr,
4606 func->on_empty,
4608 jsexpr->returning);
4609 /* Assume NULL ON ERROR when ON ERROR is not specified. */
4610 jsexpr->on_error = transformJsonBehavior(pstate,
4611 jsexpr,
4612 func->on_error,
4614 jsexpr->returning);
4615 break;
4616
4617 case JSON_VALUE_OP:
4618 /* JSON_VALUE returns text by default. */
4619 if (!OidIsValid(jsexpr->returning->typid))
4620 {
4621 jsexpr->returning->typid = TEXTOID;
4622 jsexpr->returning->typmod = -1;
4623 }
4624 jsexpr->collation = get_typcollation(jsexpr->returning->typid);
4625
4626 /*
4627 * Override whatever transformJsonOutput() set these to, which
4628 * assumes that output type to be jsonb.
4629 */
4632
4633 /* Always omit quotes from scalar strings. */
4634 jsexpr->omit_quotes = true;
4635
4636 /*
4637 * Set up to coerce the result value of JsonPathValue() to the
4638 * RETURNING type (default or user-specified), if needed.
4639 */
4640 if (jsexpr->returning->typid != TEXTOID)
4641 {
4642 if (get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN &&
4644 jsexpr->use_json_coercion = true;
4645 else
4646 jsexpr->use_io_coercion = true;
4647 }
4648
4649 /* Assume NULL ON EMPTY when ON EMPTY is not specified. */
4650 jsexpr->on_empty = transformJsonBehavior(pstate,
4651 jsexpr,
4652 func->on_empty,
4654 jsexpr->returning);
4655 /* Assume NULL ON ERROR when ON ERROR is not specified. */
4656 jsexpr->on_error = transformJsonBehavior(pstate,
4657 jsexpr,
4658 func->on_error,
4660 jsexpr->returning);
4661 break;
4662
4663 case JSON_TABLE_OP:
4664 if (!OidIsValid(jsexpr->returning->typid))
4665 {
4666 jsexpr->returning->typid = exprType(jsexpr->formatted_expr);
4667 jsexpr->returning->typmod = -1;
4668 }
4669 jsexpr->collation = get_typcollation(jsexpr->returning->typid);
4670
4671 /*
4672 * Assume EMPTY ARRAY ON ERROR when ON ERROR is not specified.
4673 *
4674 * ON EMPTY cannot be specified at the top level but it can be for
4675 * the individual columns.
4676 */
4677 jsexpr->on_error = transformJsonBehavior(pstate,
4678 jsexpr,
4679 func->on_error,
4681 jsexpr->returning);
4682 break;
4683
4684 default:
4685 elog(ERROR, "invalid JsonFuncExpr op %d", (int) func->op);
4686 break;
4687 }
4688
4689 return (Node *) jsexpr;
4690}
char get_typtype(Oid typid)
Definition lsyscache.c:2851
static JsonBehavior * transformJsonBehavior(ParseState *pstate, JsonExpr *jsexpr, JsonBehavior *behavior, JsonBehaviorType default_behavior, JsonReturning *returning)
static void transformJsonPassingArgs(ParseState *pstate, const char *constructName, JsonFormatType format, List *args, List **passing_values, List **passing_names)
@ JS_QUOTES_OMIT
JsonFormatType
Definition primnodes.h:1663
@ JSW_UNCONDITIONAL
Definition primnodes.h:1780
@ JSW_CONDITIONAL
Definition primnodes.h:1779
@ JSON_QUERY_OP
Definition primnodes.h:1830
@ JSON_TABLE_OP
Definition primnodes.h:1832
@ JSON_EXISTS_OP
Definition primnodes.h:1829
@ JSON_VALUE_OP
Definition primnodes.h:1831
char * column_name
Definition primnodes.h:1846
Node * formatted_expr
Definition primnodes.h:1850
ParseLoc location
Definition primnodes.h:1886
List * passing_values
Definition primnodes.h:1863
JsonBehavior * on_empty
Definition primnodes.h:1866
JsonFormat * format
Definition primnodes.h:1853
List * passing_names
Definition primnodes.h:1862
Node * path_spec
Definition primnodes.h:1856
bool use_io_coercion
Definition primnodes.h:1873
JsonReturning * returning
Definition primnodes.h:1859
bool use_json_coercion
Definition primnodes.h:1874
JsonWrapper wrapper
Definition primnodes.h:1877
JsonExprOp op
Definition primnodes.h:1844
JsonBehavior * on_error
Definition primnodes.h:1867
bool omit_quotes
Definition primnodes.h:1880
JsonEncoding encoding
Definition primnodes.h:1678
JsonOutput * output
char * column_name
JsonWrapper wrapper
JsonQuotes quotes
JsonExprOp op
JsonBehavior * on_empty
ParseLoc location
Node * pathspec
JsonBehavior * on_error
JsonValueExpr * context_item
JsonReturning * returning
JsonFormat * format
Definition primnodes.h:1711
bool DomainHasConstraints(Oid type_id, bool *has_volatile)
Definition typcache.c:1495

References JsonBehavior::btype, COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_EXPLICIT, JsonExpr::collation, JsonFuncExpr::column_name, JsonExpr::column_name, JsonFuncExpr::context_item, DomainHasConstraints(), elog, JsonFormat::encoding, ereport, errcode(), errdetail(), errmsg, ERROR, exprLocation(), exprType(), fb(), format, JsonReturning::format, JsonValueExpr::format, JsonExpr::format, JsonFormat::format_type, format_type_be(), JsonExpr::formatted_expr, get_typcollation(), get_typtype(), InvalidOid, 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 4124 of file parse_expr.c.

4125{
4126 Oid exprtype;
4127 Node *expr = transformJsonParseArg(pstate, pred->expr, pred->format,
4128 &exprtype);
4129
4130 /* make resulting expression */
4131 if (exprtype != TEXTOID && exprtype != JSONOID && exprtype != JSONBOID)
4132 ereport(ERROR,
4134 errmsg("cannot use type %s in IS JSON predicate",
4135 format_type_be(exprType(expr))),
4136 parser_errposition(pstate, exprLocation(expr)));
4137
4138 /* This intentionally(?) drops the format clause. */
4139 return makeJsonIsPredicate(expr, NULL, pred->item_type,
4140 pred->unique_keys, exprtype, pred->location);
4141}
Node * makeJsonIsPredicate(Node *expr, JsonFormat *format, JsonValueType item_type, bool unique_keys, Oid exprBaseType, int location)
Definition makefuncs.c:986
static Node * transformJsonParseArg(ParseState *pstate, Node *jsexpr, JsonFormat *format, Oid *exprtype)
JsonFormat * format
Definition primnodes.h:1762
JsonValueType item_type
Definition primnodes.h:1763
ParseLoc location
Definition primnodes.h:1766

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

Referenced by transformExprRecurse().

◆ transformJsonObjectAgg()

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

Definition at line 3942 of file parse_expr.c.

3943{
3944 JsonReturning *returning;
3945 Node *key;
3946 Node *val;
3947 List *args;
3948 Oid aggfnoid;
3949 Oid aggtype;
3950
3951 key = transformExprRecurse(pstate, (Node *) agg->arg->key);
3952 val = transformJsonValueExpr(pstate, "JSON_OBJECTAGG()",
3953 agg->arg->value,
3955 InvalidOid, false);
3956 args = list_make2(key, val);
3957
3958 returning = transformJsonConstructorOutput(pstate, agg->constructor->output,
3959 args);
3960
3961 if (returning->format->format_type == JS_FORMAT_JSONB)
3962 {
3963 if (agg->absent_on_null)
3964 if (agg->unique)
3966 else
3967 aggfnoid = F_JSONB_OBJECT_AGG_STRICT;
3968 else if (agg->unique)
3969 aggfnoid = F_JSONB_OBJECT_AGG_UNIQUE;
3970 else
3971 aggfnoid = F_JSONB_OBJECT_AGG;
3972
3973 aggtype = JSONBOID;
3974 }
3975 else
3976 {
3977 if (agg->absent_on_null)
3978 if (agg->unique)
3980 else
3981 aggfnoid = F_JSON_OBJECT_AGG_STRICT;
3982 else if (agg->unique)
3983 aggfnoid = F_JSON_OBJECT_AGG_UNIQUE;
3984 else
3985 aggfnoid = F_JSON_OBJECT_AGG;
3986
3987 aggtype = JSONOID;
3988 }
3989
3990 return transformJsonAggConstructor(pstate, agg->constructor, returning,
3991 args, aggfnoid, aggtype,
3993 agg->unique, agg->absent_on_null);
3994}
@ JSCTOR_JSON_OBJECTAGG
Definition primnodes.h:1718

References fb(), JsonReturning::format, JsonFormat::format_type, InvalidOid, JS_FORMAT_DEFAULT, JS_FORMAT_JSONB, JSCTOR_JSON_OBJECTAGG, list_make2, transformExprRecurse(), transformJsonAggConstructor(), transformJsonConstructorOutput(), transformJsonValueExpr(), and val.

Referenced by transformExprRecurse().

◆ transformJsonObjectConstructor()

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

Definition at line 3748 of file parse_expr.c.

3749{
3750 JsonReturning *returning;
3751 List *args = NIL;
3752
3753 /* transform key-value pairs, if any */
3754 if (ctor->exprs)
3755 {
3756 ListCell *lc;
3757
3758 /* transform and append key-value arguments */
3759 foreach(lc, ctor->exprs)
3760 {
3762 Node *key = transformExprRecurse(pstate, (Node *) kv->key);
3763 Node *val = transformJsonValueExpr(pstate, "JSON_OBJECT()",
3764 kv->value,
3766 InvalidOid, false);
3767
3768 args = lappend(args, key);
3769 args = lappend(args, val);
3770 }
3771 }
3772
3773 returning = transformJsonConstructorOutput(pstate, ctor->output, args);
3774
3775 return makeJsonConstructorExpr(pstate, JSCTOR_JSON_OBJECT, args, NULL,
3776 returning, ctor->unique,
3777 ctor->absent_on_null, ctor->location);
3778}
@ JSCTOR_JSON_OBJECT
Definition primnodes.h:1716

References castNode, fb(), InvalidOid, JS_FORMAT_DEFAULT, JSCTOR_JSON_OBJECT, lappend(), lfirst, makeJsonConstructorExpr(), NIL, transformExprRecurse(), transformJsonConstructorOutput(), transformJsonValueExpr(), and val.

Referenced by transformExprRecurse().

◆ transformJsonOutput()

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

Definition at line 3534 of file parse_expr.c.

3536{
3537 JsonReturning *ret;
3538
3539 /* if output clause is not specified, make default clause value */
3540 if (!output)
3541 {
3542 ret = makeNode(JsonReturning);
3543
3545 ret->typid = InvalidOid;
3546 ret->typmod = -1;
3547
3548 return ret;
3549 }
3550
3551 ret = copyObject(output->returning);
3552
3553 typenameTypeIdAndMod(pstate, output->typeName, &ret->typid, &ret->typmod);
3554
3555 if (output->typeName->setof)
3556 ereport(ERROR,
3558 errmsg("returning SETOF types is not supported in SQL/JSON functions"));
3559
3560 if (get_typtype(ret->typid) == TYPTYPE_PSEUDO)
3561 ereport(ERROR,
3563 errmsg("returning pseudo-types is not supported in SQL/JSON functions"));
3564
3566 /* assign JSONB format when returning jsonb, or JSON format otherwise */
3567 ret->format->format_type =
3569 else
3570 checkJsonOutputFormat(pstate, ret->format, ret->typid, allow_format);
3571
3572 return ret;
3573}
JsonFormat * makeJsonFormat(JsonFormatType type, JsonEncoding encoding, int location)
Definition makefuncs.c:922
static void checkJsonOutputFormat(ParseState *pstate, const JsonFormat *format, Oid targettype, bool allow_format_for_non_strings)
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, fb(), 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 4074 of file parse_expr.c.

4076{
4077 Node *raw_expr = transformExprRecurse(pstate, jsexpr);
4078 Node *expr = raw_expr;
4079
4080 *exprtype = getBaseType(exprType(expr));
4081
4082 /* prepare input document */
4083 if (*exprtype == BYTEAOID)
4084 {
4086
4087 expr = raw_expr;
4089 *exprtype = TEXTOID;
4090
4091 jve = makeJsonValueExpr((Expr *) raw_expr, (Expr *) expr, format);
4092 expr = (Node *) jve;
4093 }
4094 else
4095 {
4096 char typcategory;
4097 bool typispreferred;
4098
4100
4101 if (*exprtype == UNKNOWNOID || typcategory == TYPCATEGORY_STRING)
4102 {
4103 expr = coerce_to_target_type(pstate, expr, *exprtype,
4104 TEXTOID, -1,
4107 *exprtype = TEXTOID;
4108 }
4109
4110 if (format->encoding != JS_ENC_DEFAULT)
4111 ereport(ERROR,
4113 parser_errposition(pstate, format->location),
4114 errmsg("cannot use JSON FORMAT ENCODING clause for non-bytea input types")));
4115 }
4116
4117 return expr;
4118}
static Node * makeJsonByteaToTextConversion(Node *expr, JsonFormat *format, int location)
@ COERCION_IMPLICIT
Definition primnodes.h:747

References COERCE_IMPLICIT_CAST, coerce_to_target_type(), COERCION_IMPLICIT, ereport, errcode(), errmsg, ERROR, exprLocation(), exprType(), fb(), format, get_type_category_preferred(), getBaseType(), 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 4188 of file parse_expr.c.

4189{
4190 JsonOutput *output = jsexpr->output;
4191 JsonReturning *returning;
4192 Node *arg;
4193
4194 returning = transformJsonReturning(pstate, output, "JSON()");
4195
4196 if (jsexpr->unique_keys)
4197 {
4198 /*
4199 * Coerce string argument to text and then to json[b] in the executor
4200 * node with key uniqueness check.
4201 */
4202 JsonValueExpr *jve = jsexpr->expr;
4203 Oid arg_type;
4204
4205 arg = transformJsonParseArg(pstate, (Node *) jve->raw_expr, jve->format,
4206 &arg_type);
4207
4208 if (arg_type != TEXTOID)
4209 ereport(ERROR,
4211 errmsg("cannot use non-string types with WITH UNIQUE KEYS clause"),
4212 parser_errposition(pstate, jsexpr->location)));
4213 }
4214 else
4215 {
4216 /*
4217 * Coerce argument to target type using CAST for compatibility with PG
4218 * function-like CASTs.
4219 */
4220 arg = transformJsonValueExpr(pstate, "JSON()", jsexpr->expr,
4221 JS_FORMAT_JSON, returning->typid, false);
4222 }
4223
4225 returning, jsexpr->unique_keys, false,
4226 jsexpr->location);
4227}
static JsonReturning * transformJsonReturning(ParseState *pstate, JsonOutput *output, const char *fname)
@ JSCTOR_JSON_PARSE
Definition primnodes.h:1720
JsonValueExpr * expr
ParseLoc location
JsonOutput * output

References arg, ereport, errcode(), errmsg, ERROR, JsonParseExpr::expr, fb(), JS_FORMAT_JSON, JSCTOR_JSON_PARSE, list_make1, JsonParseExpr::location, makeJsonConstructorExpr(), JsonParseExpr::output, output, parser_errposition(), 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 4696 of file parse_expr.c.

4699{
4700 ListCell *lc;
4701
4702 *passing_values = NIL;
4703 *passing_names = NIL;
4704
4705 foreach(lc, args)
4706 {
4709 arg->val, format,
4710 InvalidOid, true);
4711
4712 *passing_values = lappend(*passing_values, expr);
4713 *passing_names = lappend(*passing_names, makeString(arg->name));
4714 }
4715}

References arg, castNode, fb(), 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 4148 of file parse_expr.c.

4149{
4150 JsonReturning *returning;
4151
4152 if (output)
4153 {
4154 returning = transformJsonOutput(pstate, output, false);
4155
4156 Assert(OidIsValid(returning->typid));
4157
4158 if (returning->typid != JSONOID && returning->typid != JSONBOID)
4159 ereport(ERROR,
4161 errmsg("cannot use type %s in RETURNING clause of %s",
4162 format_type_be(returning->typid), fname),
4163 errhint("Try returning json or jsonb."),
4164 parser_errposition(pstate, output->typeName->location)));
4165 }
4166 else
4167 {
4168 /* Output type is JSON by default. */
4169 Oid targettype = JSONOID;
4171
4172 returning = makeNode(JsonReturning);
4173 returning->format = makeJsonFormat(format, JS_ENC_DEFAULT, -1);
4174 returning->typid = targettype;
4175 returning->typmod = -1;
4176 }
4177
4178 return returning;
4179}

References Assert, ereport, errcode(), errhint(), errmsg, ERROR, fb(), 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 4237 of file parse_expr.c.

4238{
4239 Node *arg = transformExprRecurse(pstate, (Node *) jsexpr->expr);
4240 JsonOutput *output = jsexpr->output;
4241 JsonReturning *returning;
4242
4243 returning = transformJsonReturning(pstate, output, "JSON_SCALAR()");
4244
4245 if (exprType(arg) == UNKNOWNOID)
4246 arg = coerce_to_specific_type(pstate, arg, TEXTOID, "JSON_SCALAR");
4247
4249 returning, false, false, jsexpr->location);
4250}
@ JSCTOR_JSON_SCALAR
Definition primnodes.h:1721
ParseLoc location
JsonOutput * output

References arg, coerce_to_specific_type(), JsonScalarExpr::expr, exprType(), fb(), 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 4260 of file parse_expr.c.

4261{
4262 JsonReturning *returning;
4263 Node *arg = transformJsonValueExpr(pstate, "JSON_SERIALIZE()",
4264 expr->expr,
4266 InvalidOid, false);
4267
4268 if (expr->output)
4269 {
4270 returning = transformJsonOutput(pstate, expr->output, true);
4271
4272 if (returning->typid != BYTEAOID)
4273 {
4274 char typcategory;
4275 bool typispreferred;
4276
4280 ereport(ERROR,
4282 errmsg("cannot use type %s in RETURNING clause of %s",
4283 format_type_be(returning->typid),
4284 "JSON_SERIALIZE()"),
4285 errhint("Try returning a string type or bytea.")));
4286 }
4287 }
4288 else
4289 {
4290 /* RETURNING TEXT FORMAT JSON is by default */
4291 returning = makeNode(JsonReturning);
4293 returning->typid = TEXTOID;
4294 returning->typmod = -1;
4295 }
4296
4298 NULL, returning, false, false, expr->location);
4299}
@ JSCTOR_JSON_SERIALIZE
Definition primnodes.h:1722
JsonOutput * output
JsonValueExpr * expr

References arg, ereport, errcode(), errhint(), errmsg, ERROR, JsonSerializeExpr::expr, fb(), 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 3322 of file parse_expr.c.

3325{
3326 Node *expr = transformExprRecurse(pstate, (Node *) ve->raw_expr);
3327 Node *rawexpr;
3329 Oid exprtype;
3330 int location;
3331 char typcategory;
3332 bool typispreferred;
3333
3334 if (exprType(expr) == UNKNOWNOID)
3335 expr = coerce_to_specific_type(pstate, expr, TEXTOID, constructName);
3336
3337 rawexpr = expr;
3338 exprtype = exprType(expr);
3339 location = exprLocation(expr);
3340
3342
3343 if (ve->format->format_type != JS_FORMAT_DEFAULT)
3344 {
3345 if (ve->format->encoding != JS_ENC_DEFAULT && exprtype != BYTEAOID)
3346 ereport(ERROR,
3348 errmsg("JSON ENCODING clause is only allowed for bytea input type"),
3349 parser_errposition(pstate, ve->format->location));
3350
3351 if (exprtype == JSONOID || exprtype == JSONBOID)
3352 format = JS_FORMAT_DEFAULT; /* do not format json[b] types */
3353 else
3354 format = ve->format->format_type;
3355 }
3356 else if (isarg)
3357 {
3358 /*
3359 * Special treatment for PASSING arguments.
3360 *
3361 * Pass types supported by GetJsonPathVar() / JsonItemFromDatum()
3362 * directly without converting to json[b].
3363 */
3364 switch (exprtype)
3365 {
3366 case BOOLOID:
3367 case NUMERICOID:
3368 case INT2OID:
3369 case INT4OID:
3370 case INT8OID:
3371 case FLOAT4OID:
3372 case FLOAT8OID:
3373 case TEXTOID:
3374 case VARCHAROID:
3375 case DATEOID:
3376 case TIMEOID:
3377 case TIMETZOID:
3378 case TIMESTAMPOID:
3379 case TIMESTAMPTZOID:
3380 return expr;
3381
3382 default:
3384 return expr;
3385 /* else convert argument to json[b] type */
3386 break;
3387 }
3388
3390 }
3391 else if (exprtype == JSONOID || exprtype == JSONBOID)
3392 format = JS_FORMAT_DEFAULT; /* do not format json[b] types */
3393 else
3395
3396 if (format != JS_FORMAT_DEFAULT ||
3397 (OidIsValid(targettype) && exprtype != targettype))
3398 {
3399 Node *coerced;
3400 bool only_allow_cast = OidIsValid(targettype);
3401
3402 /*
3403 * PASSING args are handled appropriately by GetJsonPathVar() /
3404 * JsonItemFromDatum().
3405 */
3406 if (!isarg &&
3407 !only_allow_cast &&
3408 exprtype != BYTEAOID && typcategory != TYPCATEGORY_STRING)
3409 ereport(ERROR,
3411 ve->format->format_type == JS_FORMAT_DEFAULT ?
3412 errmsg("cannot use non-string types with implicit FORMAT JSON clause") :
3413 errmsg("cannot use non-string types with explicit FORMAT JSON clause"),
3414 parser_errposition(pstate, ve->format->location >= 0 ?
3415 ve->format->location : location));
3416
3417 /* Convert encoded JSON text from bytea. */
3418 if (format == JS_FORMAT_JSON && exprtype == BYTEAOID)
3419 {
3420 expr = makeJsonByteaToTextConversion(expr, ve->format, location);
3421 exprtype = TEXTOID;
3422 }
3423
3424 if (!OidIsValid(targettype))
3425 targettype = format == JS_FORMAT_JSONB ? JSONBOID : JSONOID;
3426
3427 /* Try to coerce to the target type. */
3428 coerced = coerce_to_target_type(pstate, expr, exprtype,
3429 targettype, -1,
3432 location);
3433
3434 if (!coerced)
3435 {
3436 /* If coercion failed, use to_json()/to_jsonb() functions. */
3437 FuncExpr *fexpr;
3438 Oid fnoid;
3439
3440 /*
3441 * Though only allow a cast when the target type is specified by
3442 * the caller.
3443 */
3444 if (only_allow_cast)
3445 ereport(ERROR,
3447 errmsg("cannot cast type %s to %s",
3448 format_type_be(exprtype),
3449 format_type_be(targettype)),
3450 parser_errposition(pstate, location)));
3451
3452 fnoid = targettype == JSONOID ? F_TO_JSON : F_TO_JSONB;
3453 fexpr = makeFuncExpr(fnoid, targettype, list_make1(expr),
3455
3456 fexpr->location = location;
3457
3458 coerced = (Node *) fexpr;
3459 }
3460
3461 if (coerced == expr)
3462 expr = rawexpr;
3463 else
3464 {
3465 ve = copyObject(ve);
3466 ve->raw_expr = (Expr *) rawexpr;
3467 ve->formatted_expr = (Expr *) coerced;
3468
3469 expr = (Node *) ve;
3470 }
3471 }
3472
3473 /* If returning a JsonValueExpr, formatted_expr must have been set. */
3474 Assert(!IsA(expr, JsonValueExpr) ||
3475 ((JsonValueExpr *) expr)->formatted_expr != NULL);
3476
3477 return expr;
3478}

References Assert, COERCE_EXPLICIT_CALL, COERCE_EXPLICIT_CAST, coerce_to_specific_type(), coerce_to_target_type(), COERCION_EXPLICIT, copyObject, ereport, errcode(), errmsg, ERROR, exprLocation(), exprType(), fb(), format, format_type_be(), get_type_category_preferred(), InvalidOid, IsA, JS_ENC_DEFAULT, JS_FORMAT_DEFAULT, JS_FORMAT_JSON, JS_FORMAT_JSONB, list_make1, makeFuncExpr(), makeJsonByteaToTextConversion(), OidIsValid, parser_errposition(), 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 1395 of file parse_expr.c.

1396{
1397 /*
1398 * All we need to do is check that we're in the RETURNING list of a MERGE
1399 * command. If so, we just return the node as-is.
1400 */
1402 {
1404
1405 while (parent_pstate &&
1408
1409 if (!parent_pstate)
1410 ereport(ERROR,
1412 errmsg("MERGE_ACTION() can only be used in the RETURNING list of a MERGE command"),
1413 parser_errposition(pstate, f->location));
1414 }
1415
1416 return (Node *) f;
1417}
ParseLoc location
Definition primnodes.h:669
ParseState * parentParseState
Definition parse_node.h:209

References ereport, errcode(), errmsg, ERROR, EXPR_KIND_MERGE_RETURNING, fb(), 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 2285 of file parse_expr.c.

2286{
2288 List *newargs = NIL;
2290 const char *funcname = (m->op == IS_GREATEST) ? "GREATEST" : "LEAST";
2291 ListCell *args;
2292
2293 newm->op = m->op;
2294 foreach(args, m->args)
2295 {
2296 Node *e = (Node *) lfirst(args);
2297 Node *newe;
2298
2299 newe = transformExprRecurse(pstate, e);
2301 }
2302
2303 newm->minmaxtype = select_common_type(pstate, newargs, funcname, NULL);
2304 /* minmaxcollid and inputcollid will be set by parse_collate.c */
2305
2306 /* Convert arguments if necessary */
2307 foreach(args, newargs)
2308 {
2309 Node *e = (Node *) lfirst(args);
2310 Node *newe;
2311
2312 newe = coerce_to_common_type(pstate, e,
2313 newm->minmaxtype,
2314 funcname);
2316 }
2317
2318 newm->args = newcoercedargs;
2319 newm->location = m->location;
2320 return (Node *) newm;
2321}
#define funcname
@ IS_GREATEST
Definition primnodes.h:1528
List * args
Definition primnodes.h:1544
ParseLoc location
Definition primnodes.h:1546
MinMaxOp op
Definition primnodes.h:1542

References MinMaxExpr::args, coerce_to_common_type(), fb(), 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 1501 of file parse_expr.c.

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

References RowExpr::args, Assert, castNode, count_nonjunk_tlist_entries(), elog, ereport, errcode(), errmsg, ERROR, EXPR_KIND_UPDATE_SOURCE, EXPR_SUBLINK, exprCollation(), exprLocation(), exprType(), exprTypmod(), fb(), IsA, lappend(), list_delete_last(), list_length(), list_nth(), llast, Param::location, RowExpr::location, makeNode, makeTargetEntry(), MULTIEXPR_SUBLINK, NIL, ParseState::p_expr_kind, ParseState::p_multiassign_exprs, PARAM_MULTIEXPR, Param::paramcollid, Param::paramid, Param::paramkind, Param::paramtype, Param::paramtypmod, parser_errposition(), transformExprRecurse(), and transformRowExpr().

Referenced by transformExprRecurse().

◆ transformParamRef()

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

Definition at line 892 of file parse_expr.c.

893{
894 Node *result;
895
896 /*
897 * The core parser knows nothing about Params. If a hook is supplied,
898 * call it. If not, or if the hook returns NULL, throw a generic error.
899 */
900 if (pstate->p_paramref_hook != NULL)
901 result = pstate->p_paramref_hook(pstate, pref);
902 else
903 result = NULL;
904
905 if (result == NULL)
908 errmsg("there is no parameter $%d", pref->number),
909 parser_errposition(pstate, pref->location)));
910
911 return result;
912}
ParseParamRefHook p_paramref_hook
Definition parse_node.h:256

References ereport, errcode(), errmsg, ERROR, fb(), ParseState::p_paramref_hook, and parser_errposition().

Referenced by transformExprRecurse().

◆ transformRowExpr()

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

Definition at line 2198 of file parse_expr.c.

2199{
2200 RowExpr *newr;
2201 char fname[16];
2202 int fnum;
2203
2205
2206 /* Transform the field expressions */
2207 newr->args = transformExpressionList(pstate, r->args,
2208 pstate->p_expr_kind, allowDefault);
2209
2210 /* Disallow more columns than will fit in a tuple */
2212 ereport(ERROR,
2214 errmsg("ROW expressions can have at most %d entries",
2216 parser_errposition(pstate, r->location)));
2217
2218 /* Barring later casting, we consider the type RECORD */
2219 newr->row_typeid = RECORDOID;
2220 newr->row_format = COERCE_IMPLICIT_CAST;
2221
2222 /* ROW() has anonymous columns, so invent some field names */
2223 newr->colnames = NIL;
2224 for (fnum = 1; fnum <= list_length(newr->args); fnum++)
2225 {
2226 snprintf(fname, sizeof(fname), "f%d", fnum);
2227 newr->colnames = lappend(newr->colnames, makeString(pstrdup(fname)));
2228 }
2229
2230 newr->location = r->location;
2231
2232 return (Node *) newr;
2233}
#define MaxTupleAttributeNumber
List * transformExpressionList(ParseState *pstate, List *exprlist, ParseExprKind exprKind, bool allowDefault)
#define snprintf
Definition port.h:260

References RowExpr::args, COERCE_IMPLICIT_CAST, ereport, errcode(), errmsg, ERROR, fb(), 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 2324 of file parse_expr.c.

2325{
2326 /*
2327 * All we need to do is insert the correct result type and (where needed)
2328 * validate the typmod, so we just modify the node in-place.
2329 */
2330 switch (svf->op)
2331 {
2332 case SVFOP_CURRENT_DATE:
2333 svf->type = DATEOID;
2334 break;
2335 case SVFOP_CURRENT_TIME:
2336 svf->type = TIMETZOID;
2337 break;
2339 svf->type = TIMETZOID;
2340 svf->typmod = anytime_typmod_check(true, svf->typmod);
2341 break;
2343 svf->type = TIMESTAMPTZOID;
2344 break;
2346 svf->type = TIMESTAMPTZOID;
2347 svf->typmod = anytimestamp_typmod_check(true, svf->typmod);
2348 break;
2349 case SVFOP_LOCALTIME:
2350 svf->type = TIMEOID;
2351 break;
2352 case SVFOP_LOCALTIME_N:
2353 svf->type = TIMEOID;
2354 svf->typmod = anytime_typmod_check(false, svf->typmod);
2355 break;
2357 svf->type = TIMESTAMPOID;
2358 break;
2360 svf->type = TIMESTAMPOID;
2361 svf->typmod = anytimestamp_typmod_check(false, svf->typmod);
2362 break;
2363 case SVFOP_CURRENT_ROLE:
2364 case SVFOP_CURRENT_USER:
2365 case SVFOP_USER:
2366 case SVFOP_SESSION_USER:
2369 svf->type = NAMEOID;
2370 break;
2371 }
2372
2373 return (Node *) svf;
2374}
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition timestamp.c:116
int32 anytime_typmod_check(bool istz, int32 typmod)
Definition date.c:65
@ SVFOP_CURRENT_CATALOG
Definition primnodes.h:1575
@ SVFOP_LOCALTIME_N
Definition primnodes.h:1568
@ SVFOP_CURRENT_TIMESTAMP
Definition primnodes.h:1565
@ SVFOP_LOCALTIME
Definition primnodes.h:1567
@ SVFOP_CURRENT_TIMESTAMP_N
Definition primnodes.h:1566
@ SVFOP_CURRENT_ROLE
Definition primnodes.h:1571
@ SVFOP_USER
Definition primnodes.h:1573
@ SVFOP_CURRENT_SCHEMA
Definition primnodes.h:1576
@ SVFOP_LOCALTIMESTAMP_N
Definition primnodes.h:1570
@ SVFOP_CURRENT_DATE
Definition primnodes.h:1562
@ SVFOP_CURRENT_TIME_N
Definition primnodes.h:1564
@ SVFOP_CURRENT_TIME
Definition primnodes.h:1563
@ SVFOP_LOCALTIMESTAMP
Definition primnodes.h:1569
@ SVFOP_CURRENT_USER
Definition primnodes.h:1572
@ SVFOP_SESSION_USER
Definition primnodes.h:1574
SQLValueFunctionOp op
Definition primnodes.h:1582

References anytime_typmod_check(), anytimestamp_typmod_check(), fb(), 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 1789 of file parse_expr.c.

1790{
1791 Node *result = (Node *) sublink;
1792 Query *qtree;
1793 const char *err;
1794
1795 /*
1796 * Check to see if the sublink is in an invalid place within the query. We
1797 * allow sublinks everywhere in SELECT/INSERT/UPDATE/DELETE/MERGE, but
1798 * generally not in utility statements.
1799 */
1800 err = NULL;
1801 switch (pstate->p_expr_kind)
1802 {
1803 case EXPR_KIND_NONE:
1804 Assert(false); /* can't happen */
1805 break;
1806 case EXPR_KIND_OTHER:
1807 /* Accept sublink here; caller must throw error if wanted */
1808 break;
1809 case EXPR_KIND_JOIN_ON:
1813 case EXPR_KIND_WHERE:
1814 case EXPR_KIND_POLICY:
1815 case EXPR_KIND_HAVING:
1816 case EXPR_KIND_FILTER:
1827 case EXPR_KIND_GROUP_BY:
1828 case EXPR_KIND_ORDER_BY:
1830 case EXPR_KIND_LIMIT:
1831 case EXPR_KIND_OFFSET:
1834 case EXPR_KIND_VALUES:
1837 /* okay */
1838 break;
1841 err = _("cannot use subquery in check constraint");
1842 break;
1845 err = _("cannot use subquery in DEFAULT expression");
1846 break;
1848 err = _("cannot use subquery in index expression");
1849 break;
1851 err = _("cannot use subquery in index predicate");
1852 break;
1854 err = _("cannot use subquery in statistics expression");
1855 break;
1857 err = _("cannot use subquery in transform expression");
1858 break;
1860 err = _("cannot use subquery in EXECUTE parameter");
1861 break;
1863 err = _("cannot use subquery in trigger WHEN condition");
1864 break;
1866 err = _("cannot use subquery in partition bound");
1867 break;
1869 err = _("cannot use subquery in partition key expression");
1870 break;
1872 err = _("cannot use subquery in CALL argument");
1873 break;
1875 err = _("cannot use subquery in COPY FROM WHERE condition");
1876 break;
1878 err = _("cannot use subquery in column generation expression");
1879 break;
1881 err = _("cannot use subquery in property definition expression");
1882 break;
1883
1884 /*
1885 * There is intentionally no default: case here, so that the
1886 * compiler will warn if we add a new ParseExprKind without
1887 * extending this switch. If we do see an unrecognized value at
1888 * runtime, the behavior will be the same as for EXPR_KIND_OTHER,
1889 * which is sane anyway.
1890 */
1891 }
1892 if (err)
1893 ereport(ERROR,
1895 errmsg_internal("%s", err),
1896 parser_errposition(pstate, sublink->location)));
1897
1898 pstate->p_hasSubLinks = true;
1899
1900 /*
1901 * OK, let's transform the sub-SELECT.
1902 */
1903 qtree = parse_sub_analyze(sublink->subselect, pstate, NULL, false, true);
1904
1905 /*
1906 * Check that we got a SELECT. Anything else should be impossible given
1907 * restrictions of the grammar, but check anyway.
1908 */
1909 if (!IsA(qtree, Query) ||
1910 qtree->commandType != CMD_SELECT)
1911 elog(ERROR, "unexpected non-SELECT command in SubLink");
1912
1913 sublink->subselect = (Node *) qtree;
1914
1915 if (sublink->subLinkType == EXISTS_SUBLINK)
1916 {
1917 /*
1918 * EXISTS needs no test expression or combining operator. These fields
1919 * should be null already, but make sure.
1920 */
1921 sublink->testexpr = NULL;
1922 sublink->operName = NIL;
1923 }
1924 else if (sublink->subLinkType == EXPR_SUBLINK ||
1925 sublink->subLinkType == ARRAY_SUBLINK)
1926 {
1927 /*
1928 * Make sure the subselect delivers a single column (ignoring resjunk
1929 * targets).
1930 */
1931 if (count_nonjunk_tlist_entries(qtree->targetList) != 1)
1932 ereport(ERROR,
1934 errmsg("subquery must return only one column"),
1935 parser_errposition(pstate, sublink->location)));
1936
1937 /*
1938 * EXPR and ARRAY need no test expression or combining operator. These
1939 * fields should be null already, but make sure.
1940 */
1941 sublink->testexpr = NULL;
1942 sublink->operName = NIL;
1943 }
1944 else if (sublink->subLinkType == MULTIEXPR_SUBLINK)
1945 {
1946 /* Same as EXPR case, except no restriction on number of columns */
1947 sublink->testexpr = NULL;
1948 sublink->operName = NIL;
1949 }
1950 else
1951 {
1952 /* ALL, ANY, or ROWCOMPARE: generate row-comparing expression */
1953 Node *lefthand;
1954 List *left_list;
1956 ListCell *l;
1957
1958 /*
1959 * If the source was "x IN (select)", convert to "x = ANY (select)".
1960 */
1961 if (sublink->operName == NIL)
1962 sublink->operName = list_make1(makeString("="));
1963
1964 /*
1965 * Transform lefthand expression, and convert to a list
1966 */
1967 lefthand = transformExprRecurse(pstate, sublink->testexpr);
1968 if (lefthand && IsA(lefthand, RowExpr))
1969 left_list = ((RowExpr *) lefthand)->args;
1970 else
1972
1973 /*
1974 * Build a list of PARAM_SUBLINK nodes representing the output columns
1975 * of the subquery.
1976 */
1977 right_list = NIL;
1978 foreach(l, qtree->targetList)
1979 {
1981 Param *param;
1982
1983 if (tent->resjunk)
1984 continue;
1985
1986 param = makeNode(Param);
1987 param->paramkind = PARAM_SUBLINK;
1988 param->paramid = tent->resno;
1989 param->paramtype = exprType((Node *) tent->expr);
1990 param->paramtypmod = exprTypmod((Node *) tent->expr);
1991 param->paramcollid = exprCollation((Node *) tent->expr);
1992 param->location = -1;
1993
1994 right_list = lappend(right_list, param);
1995 }
1996
1997 /*
1998 * We could rely on make_row_comparison_op to complain if the list
1999 * lengths differ, but we prefer to generate a more specific error
2000 * message.
2001 */
2003 ereport(ERROR,
2005 errmsg("subquery has too many columns"),
2006 parser_errposition(pstate, sublink->location)));
2008 ereport(ERROR,
2010 errmsg("subquery has too few columns"),
2011 parser_errposition(pstate, sublink->location)));
2012
2013 /*
2014 * Identify the combining operator(s) and generate a suitable
2015 * row-comparison expression.
2016 */
2017 sublink->testexpr = make_row_comparison_op(pstate,
2018 sublink->operName,
2019 left_list,
2020 right_list,
2021 sublink->location);
2022 }
2023
2024 return result;
2025}
@ CMD_SELECT
Definition nodes.h:275
Query * parse_sub_analyze(Node *parseTree, ParseState *parentParseState, CommonTableExpr *parentCTE, bool locked_from_parent, bool resolve_unknowns)
Definition analyze.c:234
@ ARRAY_SUBLINK
Definition primnodes.h:1036
@ EXISTS_SUBLINK
Definition primnodes.h:1030
@ PARAM_SUBLINK
Definition primnodes.h:387
ParseLoc location
Definition primnodes.h:404
int32 paramtypmod
Definition primnodes.h:400
Oid paramcollid
Definition primnodes.h:402
bool p_hasSubLinks
Definition parse_node.h:245

References _, ARRAY_SUBLINK, Assert, CMD_SELECT, count_nonjunk_tlist_entries(), elog, ereport, err(), errcode(), errmsg, errmsg_internal(), ERROR, EXISTS_SUBLINK, 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_PROPGRAPH_PROPERTY, 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(), fb(), IsA, lappend(), lfirst, list_length(), list_make1, Param::location, make_row_comparison_op(), makeNode, makeString(), MULTIEXPR_SUBLINK, NIL, ParseState::p_expr_kind, ParseState::p_hasSubLinks, PARAM_SUBLINK, Param::paramcollid, Param::paramid, Param::paramkind, Param::paramtype, Param::paramtypmod, parse_sub_analyze(), parser_errposition(), and transformExprRecurse().

Referenced by transformExprRecurse().

◆ transformTypeCast()

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

Definition at line 2724 of file parse_expr.c.

2725{
2726 Node *result;
2727 Node *arg = tc->arg;
2728 Node *expr;
2729 Oid inputType;
2732 int location;
2733
2734 /* Look up the type name first */
2735 typenameTypeIdAndMod(pstate, tc->typeName, &targetType, &targetTypmod);
2736
2737 /*
2738 * If the subject of the typecast is an ARRAY[] construct and the target
2739 * type is an array type, we invoke transformArrayExpr() directly so that
2740 * we can pass down the type information. This avoids some cases where
2741 * transformArrayExpr() might not infer the correct type. Otherwise, just
2742 * transform the argument normally.
2743 */
2744 if (IsA(arg, A_ArrayExpr))
2745 {
2749
2750 /*
2751 * If target is a domain over array, work with the base array type
2752 * here. Below, we'll cast the array type to the domain. In the
2753 * usual case that the target is not a domain, the remaining steps
2754 * will be a no-op.
2755 */
2760 {
2761 expr = transformArrayExpr(pstate,
2762 (A_ArrayExpr *) arg,
2766 }
2767 else
2768 expr = transformExprRecurse(pstate, arg);
2769 }
2770 else
2771 expr = transformExprRecurse(pstate, arg);
2772
2773 inputType = exprType(expr);
2774 if (inputType == InvalidOid)
2775 return expr; /* do nothing if NULL input */
2776
2777 /*
2778 * Location of the coercion is preferentially the location of the :: or
2779 * CAST symbol, but if there is none then use the location of the type
2780 * name (this can happen in TypeName 'string' syntax, for instance).
2781 */
2782 location = tc->location;
2783 if (location < 0)
2784 location = tc->typeName->location;
2785
2786 result = coerce_to_target_type(pstate, expr, inputType,
2790 location);
2791 if (result == NULL)
2792 ereport(ERROR,
2794 errmsg("cannot cast type %s to %s",
2797 parser_coercion_errposition(pstate, location, expr)));
2798
2799 return result;
2800}
Oid getBaseTypeAndTypmod(Oid typid, int32 *typmod)
Definition lsyscache.c:2760

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

Referenced by transformExprRecurse().

◆ transformWholeRowRef()

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

Definition at line 2642 of file parse_expr.c.

2644{
2645 /*
2646 * Build the appropriate referencing node. Normally this can be a
2647 * whole-row Var, but if the nsitem is a JOIN USING alias then it contains
2648 * only a subset of the columns of the underlying join RTE, so that will
2649 * not work. Instead we immediately expand the reference into a RowExpr.
2650 * Since the JOIN USING's common columns are fully determined at this
2651 * point, there seems no harm in expanding it now rather than during
2652 * planning.
2653 *
2654 * Note that if the nsitem is an OLD/NEW alias for the target RTE (as can
2655 * appear in a RETURNING list), its alias won't match the target RTE's
2656 * alias, but we still want to make a whole-row Var here rather than a
2657 * RowExpr, for consistency with direct references to the target RTE, and
2658 * so that any dropped columns are handled correctly. Thus we also check
2659 * p_returning_type here.
2660 *
2661 * Note that if the RTE is a function returning scalar, we create just a
2662 * plain reference to the function value, not a composite containing a
2663 * single column. This is pretty inconsistent at first sight, but it's
2664 * what we've done historically. One argument for it is that "rel" and
2665 * "rel.*" mean the same thing for composite relations, so why not for
2666 * scalar functions...
2667 */
2668 if (nsitem->p_names == nsitem->p_rte->eref ||
2669 nsitem->p_returning_type != VAR_RETURNING_DEFAULT)
2670 {
2671 Var *result;
2672
2673 result = makeWholeRowVar(nsitem->p_rte, nsitem->p_rtindex,
2674 sublevels_up, true);
2675
2676 /* mark Var for RETURNING OLD/NEW, as necessary */
2677 result->varreturningtype = nsitem->p_returning_type;
2678
2679 /* location is not filled in by makeWholeRowVar */
2680 result->location = location;
2681
2682 /* mark Var if it's nulled by any outer joins */
2683 markNullableIfNeeded(pstate, result);
2684
2685 /* mark relation as requiring whole-row SELECT access */
2686 markVarForSelectPriv(pstate, result);
2687
2688 return (Node *) result;
2689 }
2690 else
2691 {
2692 RowExpr *rowexpr;
2693 List *fields;
2694
2695 /*
2696 * We want only as many columns as are listed in p_names->colnames,
2697 * and we should use those names not whatever possibly-aliased names
2698 * are in the RTE. We needn't worry about marking the RTE for SELECT
2699 * access, as the common columns are surely so marked already.
2700 */
2701 expandRTE(nsitem->p_rte, nsitem->p_rtindex, sublevels_up,
2702 nsitem->p_returning_type, location, false, NULL, &fields);
2703 rowexpr = makeNode(RowExpr);
2704 rowexpr->args = list_truncate(fields,
2705 list_length(nsitem->p_names->colnames));
2706 rowexpr->row_typeid = RECORDOID;
2707 rowexpr->row_format = COERCE_IMPLICIT_CAST;
2708 rowexpr->colnames = copyObject(nsitem->p_names->colnames);
2709 rowexpr->location = location;
2710
2711 /* XXX we ought to mark the row as possibly nullable */
2712
2713 return (Node *) rowexpr;
2714 }
2715}
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:137
void markNullableIfNeeded(ParseState *pstate, Var *var)
void markVarForSelectPriv(ParseState *pstate, Var *var)
void expandRTE(RangeTblEntry *rte, int rtindex, int sublevels_up, VarReturningType returning_type, int location, bool include_dropped, List **colnames, List **colvars)
@ VAR_RETURNING_DEFAULT
Definition primnodes.h:257
ParseLoc location
Definition primnodes.h:311
VarReturningType varreturningtype
Definition primnodes.h:298

References RowExpr::args, COERCE_IMPLICIT_CAST, copyObject, expandRTE(), fb(), list_length(), list_truncate(), Var::location, RowExpr::location, makeNode, makeWholeRowVar(), markNullableIfNeeded(), markVarForSelectPriv(), VAR_RETURNING_DEFAULT, and Var::varreturningtype.

Referenced by transformColumnRef().

◆ transformXmlExpr()

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

Definition at line 2377 of file parse_expr.c.

2378{
2379 XmlExpr *newx;
2380 ListCell *lc;
2381 int i;
2382
2384 newx->op = x->op;
2385 if (x->name)
2386 newx->name = map_sql_identifier_to_xml_name(x->name, false, false);
2387 else
2388 newx->name = NULL;
2389 newx->xmloption = x->xmloption;
2390 newx->type = XMLOID; /* this just marks the node as transformed */
2391 newx->typmod = -1;
2392 newx->location = x->location;
2393
2394 /*
2395 * gram.y built the named args as a list of ResTarget. Transform each,
2396 * and break the names out as a separate list.
2397 */
2398 newx->named_args = NIL;
2399 newx->arg_names = NIL;
2400
2401 foreach(lc, x->named_args)
2402 {
2404 Node *expr;
2405 char *argname;
2406
2407 expr = transformExprRecurse(pstate, r->val);
2408
2409 if (r->name)
2410 argname = map_sql_identifier_to_xml_name(r->name, false, false);
2411 else if (IsA(r->val, ColumnRef))
2413 true, false);
2414 else
2415 {
2416 ereport(ERROR,
2418 x->op == IS_XMLELEMENT
2419 ? errmsg("unnamed XML attribute value must be a column reference")
2420 : errmsg("unnamed XML element value must be a column reference"),
2421 parser_errposition(pstate, r->location)));
2422 argname = NULL; /* keep compiler quiet */
2423 }
2424
2425 /* reject duplicate argnames in XMLELEMENT only */
2426 if (x->op == IS_XMLELEMENT)
2427 {
2428 ListCell *lc2;
2429
2430 foreach(lc2, newx->arg_names)
2431 {
2432 if (strcmp(argname, strVal(lfirst(lc2))) == 0)
2433 ereport(ERROR,
2435 errmsg("XML attribute name \"%s\" appears more than once",
2436 argname),
2437 parser_errposition(pstate, r->location)));
2438 }
2439 }
2440
2441 newx->named_args = lappend(newx->named_args, expr);
2442 newx->arg_names = lappend(newx->arg_names, makeString(argname));
2443 }
2444
2445 /* The other arguments are of varying types depending on the function */
2446 newx->args = NIL;
2447 i = 0;
2448 foreach(lc, x->args)
2449 {
2450 Node *e = (Node *) lfirst(lc);
2451 Node *newe;
2452
2453 newe = transformExprRecurse(pstate, e);
2454 switch (x->op)
2455 {
2456 case IS_XMLCONCAT:
2458 "XMLCONCAT");
2459 break;
2460 case IS_XMLELEMENT:
2461 /* no coercion necessary */
2462 break;
2463 case IS_XMLFOREST:
2465 "XMLFOREST");
2466 break;
2467 case IS_XMLPARSE:
2468 if (i == 0)
2470 "XMLPARSE");
2471 else
2472 newe = coerce_to_boolean(pstate, newe, "XMLPARSE");
2473 break;
2474 case IS_XMLPI:
2476 "XMLPI");
2477 break;
2478 case IS_XMLROOT:
2479 if (i == 0)
2481 "XMLROOT");
2482 else if (i == 1)
2484 "XMLROOT");
2485 else
2487 "XMLROOT");
2488 break;
2489 case IS_XMLSERIALIZE:
2490 /* not handled here */
2491 Assert(false);
2492 break;
2493 case IS_DOCUMENT:
2495 "IS DOCUMENT");
2496 break;
2497 }
2498 newx->args = lappend(newx->args, newe);
2499 i++;
2500 }
2501
2502 return (Node *) newx;
2503}
int x
Definition isn.c:75
char * FigureColname(Node *node)
@ IS_DOCUMENT
Definition primnodes.h:1613
@ IS_XMLFOREST
Definition primnodes.h:1608
@ IS_XMLCONCAT
Definition primnodes.h:1606
@ IS_XMLPI
Definition primnodes.h:1610
@ IS_XMLPARSE
Definition primnodes.h:1609
@ IS_XMLSERIALIZE
Definition primnodes.h:1612
@ IS_XMLROOT
Definition primnodes.h:1611
@ IS_XMLELEMENT
Definition primnodes.h:1607
char * map_sql_identifier_to_xml_name(const char *ident, bool fully_escaped, bool escape_period)
Definition xml.c:2418

References Assert, coerce_to_boolean(), coerce_to_specific_type(), ereport, errcode(), errmsg, ERROR, fb(), 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, makeNode, makeString(), map_sql_identifier_to_xml_name(), ResTarget::name, NIL, parser_errposition(), strVal, transformExprRecurse(), ResTarget::val, and x.

Referenced by transformExprRecurse().

◆ transformXmlSerialize()

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

Definition at line 2506 of file parse_expr.c.

2507{
2508 Node *result;
2509 XmlExpr *xexpr;
2512
2513 xexpr = makeNode(XmlExpr);
2514 xexpr->op = IS_XMLSERIALIZE;
2515 xexpr->args = list_make1(coerce_to_specific_type(pstate,
2516 transformExprRecurse(pstate, xs->expr),
2517 XMLOID,
2518 "XMLSERIALIZE"));
2519
2520 typenameTypeIdAndMod(pstate, xs->typeName, &targetType, &targetTypmod);
2521
2522 xexpr->xmloption = xs->xmloption;
2523 xexpr->indent = xs->indent;
2524 xexpr->location = xs->location;
2525 /* We actually only need these to be able to parse back the expression. */
2526 xexpr->type = targetType;
2527 xexpr->typmod = targetTypmod;
2528
2529 /*
2530 * The actual target type is determined this way. SQL allows char and
2531 * varchar as target types. We allow anything that can be cast implicitly
2532 * from text. This way, user-defined text-like data types automatically
2533 * fit in.
2534 */
2535 result = coerce_to_target_type(pstate, (Node *) xexpr,
2539 -1);
2540 if (result == NULL)
2541 ereport(ERROR,
2543 errmsg("cannot cast XMLSERIALIZE result to %s",
2545 parser_errposition(pstate, xexpr->location)));
2546 return result;
2547}
List * args
Definition primnodes.h:1634
ParseLoc location
Definition primnodes.h:1643
bool indent
Definition primnodes.h:1638
XmlExprOp op
Definition primnodes.h:1626

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

Referenced by transformExprRecurse().

◆ unknown_attribute()

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

Definition at line 393 of file parse_expr.c.

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

References attname, ereport, errcode(), errmsg, ERROR, exprType(), fb(), 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 4722 of file parse_expr.c.

4723{
4724 if (expr == NULL)
4725 return false;
4726
4727 switch (nodeTag(expr))
4728 {
4729 /* Acceptable expression nodes */
4730 case T_Const:
4731 case T_FuncExpr:
4732 case T_OpExpr:
4733 return true;
4734
4735 /* Acceptable iff arg of the following nodes is one of the above */
4736 case T_CoerceViaIO:
4737 case T_CoerceToDomain:
4738 case T_ArrayCoerceExpr:
4740 case T_RelabelType:
4741 case T_CollateExpr:
4743 context);
4744 default:
4745 break;
4746 }
4747
4748 return false;
4749}
#define expression_tree_walker(n, w, c)
Definition nodeFuncs.h:153

References expression_tree_walker, fb(), nodeTag, and ValidJsonBehaviorDefaultExpr().

Referenced by transformJsonBehavior(), and ValidJsonBehaviorDefaultExpr().

Variable Documentation

◆ Transform_null_equals

bool Transform_null_equals = false

Definition at line 46 of file parse_expr.c.

Referenced by transformAExprOp().