1/*-------------------------------------------------------------------------
4 * creator functions for various nodes. The functions here are for the
5 * most frequently created nodes.
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * src/backend/nodes/makefuncs.c
14 *-------------------------------------------------------------------------
27 * makes an A_Expr node
31 Node *lexpr,
Node *rexpr,
int location)
39 a->location = location;
45 * As above, given a simple (unqualified) operator name
49 Node *lexpr,
Node *rexpr,
int location)
57 a->location = location;
77 var->vartype = vartype;
78 var->vartypmod = vartypmod;
79 var->varcollid = varcollid;
83 * Only a few callers need to make Var nodes with varreturningtype
84 * different from VAR_RETURNING_DEFAULT, non-null varnullingrels, or with
85 * varnosyn/varattnosyn different from varno/varattno. We don't provide
86 * separate arguments for them, but just initialize them to sensible
87 * default values. This reduces code clutter and chance of error for most
91 var->varnullingrels = NULL;
92 var->varnosyn = (
Index) varno;
93 var->varattnosyn = varattno;
95 /* Likewise, we just set location to "unknown" here */
102 * makeVarFromTargetEntry -
103 * convenience function to create a same-level Var node from a
120 * creates a Var node representing a whole row of the specified RTE
122 * A whole-row reference is a Var with varno set to the correct range
123 * table entry, and varattno == 0 to signal that it references the whole
124 * tuple. (Use of zero here is unclean, since it could easily be confused
125 * with error cases, but it's not worth changing now.) The vartype indicates
126 * a rowtype; either a named composite type, or a domain over a named
127 * composite type (only possible if the RTE is a function returning that),
128 * or RECORD. This function encapsulates the logic for determining the
129 * correct rowtype OID to use.
131 * If allowScalar is true, then for the case where the RTE is a single function
132 * returning a non-composite result type, we produce a normal Var referencing
133 * the function's result directly, instead of the single-column composite
134 * value that the whole-row notation might otherwise suggest.
149 /* relation: the rowtype is a named composite type */
153 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
154 errmsg(
"relation \"%s\" does not have a composite type",
167 * For a standard subquery, the Var should be of RECORD type.
168 * However, if we're looking at a subquery that was expanded from
169 * a view or SRF (only possible during planning), we must use the
170 * appropriate rowtype, so that the resulting Var has the same
171 * type that we would have produced from the original RTE.
175 /* Subquery was expanded from a view */
179 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
180 errmsg(
"relation \"%s\" does not have a composite type",
186 * Subquery was expanded from a set-returning function. That
187 * would not have happened if there's more than one function
188 * or ordinality was requested. We also needn't worry about
189 * the allowScalar case, since the planner doesn't use that.
190 * Otherwise this must match the RTE_FUNCTION code below.
200 /* Normal subquery-in-FROM */
214 * If there's more than one function, or ordinality is requested,
215 * force a RECORD result, since there's certainly more than one
216 * column involved and it can't be a known named type.
220 /* always produces an anonymous RECORD result */
234 /* func returns composite; same as relation case */
242 else if (allowScalar)
244 /* func returns scalar; just return its output as-is */
254 /* func returns scalar, but we want a composite result */
267 * RTE is a join, tablefunc, VALUES, CTE, etc. We represent these
268 * cases as a whole-row Var of RECORD type. (Note that in most
269 * cases the Var will be expanded to a RowExpr during planning,
270 * but that is not our concern here.)
286 * creates a TargetEntry node
298 tle->resname = resname;
301 * We always set these fields to 0. If the caller wants to change them he
302 * must do so explicitly. Few callers do that, so omitting these
303 * arguments reduces the chance of error.
309 tle->resjunk = resjunk;
315 * flatCopyTargetEntry -
316 * duplicate a TargetEntry, but don't copy substructure
318 * This is commonly used when we just want to modify the resno or substitute
333 * creates a FromExpr node
347 * creates a Const node
361 * If it's a varlena value, force it to be in non-expanded (non-toasted)
362 * format; this avoids any possible dependency on external values and
363 * improves consistency of representation, which is important for equal().
365 if (!constisnull && constlen == -1)
369 cnst->consttypmod = consttypmod;
370 cnst->constcollid = constcollid;
371 cnst->constlen = constlen;
372 cnst->constvalue = constvalue;
373 cnst->constisnull = constisnull;
374 cnst->constbyval = constbyval;
375 cnst->location = -1;
/* "unknown" */
382 * creates a Const node representing a NULL of the specified type/typmod
384 * This is a convenience routine that just saves a lookup of the type's
385 * storage properties.
405 * creates a Const node representing a boolean value (can be NULL too)
410 /* note that pg_type.h hardwires size of bool as 1 ... duplicate it */
417 * creates a BoolExpr node
426 b->location = location;
433 * creates an Alias node
435 * NOTE: the given name is copied, but the colnames list (if any) isn't.
443 a->colnames = colnames;
450 * creates a RelabelType node
460 r->resulttypmod = rtypmod;
461 r->resultcollid = rcollid;
462 r->relabelformat = rformat;
470 * creates a RangeVar node (rather oversimplified case)
489 * makeNotNullConstraint -
490 * creates a Constraint node for NOT NULL constraints
514 * build a TypeName node for an unqualified name.
516 * typmod is defaulted, but can be changed later by caller.
525 * makeTypeNameFromNameList -
526 * build a TypeName node for a String list representing a qualified name.
528 * typmod is defaulted, but can be changed later by caller.
543 * makeTypeNameFromOid -
544 * build a TypeName node to represent a type already known by OID/typmod.
559 * build a ColumnDef node to represent a simple column definition.
561 * Type and collation are specified by OID.
562 * Other properties are all basic to start with.
589 * build an expression tree representing a function call.
591 * The argument expressions must have been transformed already.
600 funcexpr->
funcid = funcid;
601 funcexpr->funcresulttype = rettype;
602 funcexpr->funcretset =
false;
/* only allowed case here */
603 funcexpr->funcvariadic =
false;
/* only allowed case here */
604 funcexpr->funcformat = fformat;
605 funcexpr->funccollid = funccollid;
606 funcexpr->inputcollid = inputcollid;
615 * build a A_Const node of type T_String for given string
631 * build a DefElem node
633 * This is sufficient for the "typical" case with an unqualified option name
634 * and no special action.
651 * makeDefElemExtended -
652 * build a DefElem node with all fields available to be specified
672 * Initialize a FuncCall struct with the information every caller must
673 * supply. Any non-default parameters have to be inserted by the caller.
696 * Creates an operator clause given its operator info, left operand
697 * and right operand (pass NULL to create single-operand clause),
698 * and collation info.
703 Oid opcollid,
Oid inputcollid)
709 expr->opresulttype = opresulttype;
710 expr->opretset = opretset;
711 expr->opcollid = opcollid;
712 expr->inputcollid = inputcollid;
718 return (
Expr *) expr;
724 * Creates an 'and' clause given a list of its subclauses.
732 expr->
args = andclauses;
734 return (
Expr *) expr;
740 * Creates an 'or' clause given a list of its subclauses.
748 expr->
args = orclauses;
750 return (
Expr *) expr;
756 * Create a 'not' clause given the expression to be negated.
766 return (
Expr *) expr;
772 * Variant of make_andclause for ANDing two qual conditions together.
773 * Qual conditions have the property that a NULL nodetree is interpreted
776 * NB: this makes no attempt to preserve AND/OR flatness; so it should not
777 * be used on a qual that has already been run through prepqual.c.
790 * The planner and executor usually represent qualification expressions
791 * as lists of boolean expressions with implicit AND semantics.
793 * These functions convert between an AND-semantics expression list and the
794 * ordinary representation of a boolean expression.
796 * Note that an empty list is considered equivalent to TRUE.
801 if (andclauses ==
NIL)
813 * NB: because the parser sets the qual field to NULL in a query that has
814 * no WHERE clause, we must consider a NULL input clause as TRUE, even
815 * though one might more reasonably think it FALSE.
818 return NIL;
/* NULL -> NIL list == TRUE */
822 !((
Const *) clause)->constisnull &&
824 return NIL;
/* constant TRUE input -> NIL list */
831 * create an IndexInfo node
835 List *predicates,
bool unique,
bool nulls_not_distinct,
836 bool isready,
bool concurrent,
bool summarizing,
837 bool withoutoverlaps)
854 /* summarizing indexes cannot contain non-key attributes */
855 Assert(!summarizing || (numkeyattrs == numattrs));
865 /* exclusion constraints */
870 /* speculative inserts */
875 /* initialize index-build state to default */
879 /* set up for possible use by index AM */
903 * makeVacuumRelation -
904 * create a VacuumRelation node
919 * creates a JsonFormat node
934 * makeJsonValueExpr -
935 * creates a JsonValueExpr node
952 * creates a JsonBehavior node
959 behavior->
btype = btype;
960 behavior->
expr = expr;
968 * creates a JsonKeyValue node
982 * makeJsonIsPredicate -
983 * creates a JsonIsPredicate node
987 bool unique_keys,
int location)
1001 * makeJsonTablePathSpec -
1002 * Make JsonTablePathSpec node from given path string and name (if any)
1016 pathspec->
location = string_location;
1022 * makeJsonTablePath -
1023 * Make JsonTablePath node for given path string and name
1031 path->
value = pathvalue;
1032 path->
name = pathname;
Datum boolop(PG_FUNCTION_ARGS)
#define InvalidAttrNumber
#define OidIsValid(objectId)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define PG_DETOAST_DATUM(datum)
Assert(PointerIsAligned(start, uint64))
char * get_rel_name(Oid relid)
bool type_is_rowtype(Oid typid)
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Oid get_rel_type_id(Oid relid)
Alias * makeAlias(const char *aliasname, List *colnames)
Expr * make_orclause(List *orclauses)
Expr * make_ands_explicit(List *andclauses)
Expr * makeBoolExpr(BoolExprType boolop, List *args, int location)
VacuumRelation * makeVacuumRelation(RangeVar *relation, Oid oid, List *va_cols)
DefElem * makeDefElem(char *name, Node *arg, int location)
A_Expr * makeA_Expr(A_Expr_Kind kind, List *name, Node *lexpr, Node *rexpr, int location)
TypeName * makeTypeNameFromNameList(List *names)
Var * makeVarFromTargetEntry(int varno, TargetEntry *tle)
FromExpr * makeFromExpr(List *fromlist, Node *quals)
JsonBehavior * makeJsonBehavior(JsonBehaviorType btype, Node *expr, int location)
IndexInfo * makeIndexInfo(int numattrs, int numkeyattrs, Oid amoid, List *expressions, List *predicates, bool unique, bool nulls_not_distinct, bool isready, bool concurrent, bool summarizing, bool withoutoverlaps)
JsonTablePath * makeJsonTablePath(Const *pathvalue, char *pathname)
DefElem * makeDefElemExtended(char *nameSpace, char *name, Node *arg, DefElemAction defaction, int location)
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
Node * makeJsonKeyValue(Node *key, Node *value)
ColumnDef * makeColumnDef(const char *colname, Oid typeOid, int32 typmod, Oid collOid)
Const * makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
Var * makeWholeRowVar(RangeTblEntry *rte, int varno, Index varlevelsup, bool allowScalar)
Node * makeBoolConst(bool value, bool isnull)
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
Node * makeStringConst(char *str, int location)
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
TypeName * makeTypeName(char *typnam)
Expr * make_andclause(List *andclauses)
FuncCall * makeFuncCall(List *name, List *args, CoercionForm funcformat, int location)
JsonTablePathSpec * makeJsonTablePathSpec(char *string, char *name, int string_location, int name_location)
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
FuncExpr * makeFuncExpr(Oid funcid, Oid rettype, List *args, Oid funccollid, Oid inputcollid, CoercionForm fformat)
Expr * make_opclause(Oid opno, Oid opresulttype, bool opretset, Expr *leftop, Expr *rightop, Oid opcollid, Oid inputcollid)
Constraint * makeNotNullConstraint(String *colname)
Expr * make_notclause(Expr *notclause)
JsonValueExpr * makeJsonValueExpr(Expr *raw_expr, Expr *formatted_expr, JsonFormat *format)
Node * make_and_qual(Node *qual1, Node *qual2)
JsonFormat * makeJsonFormat(JsonFormatType type, JsonEncoding encoding, int location)
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
GroupingSet * makeGroupingSet(GroupingSetKind kind, List *content, int location)
List * make_ands_implicit(Expr *clause)
TypeName * makeTypeNameFromOid(Oid typeOid, int32 typmod)
TargetEntry * flatCopyTargetEntry(TargetEntry *src_tle)
Node * makeJsonIsPredicate(Node *expr, JsonFormat *format, JsonValueType item_type, bool unique_keys, int location)
char * pstrdup(const char *in)
MemoryContext CurrentMemoryContext
Oid exprType(const Node *expr)
int32 exprTypmod(const Node *expr)
Oid exprCollation(const Node *expr)
static bool is_andclause(const void *clause)
#define IsA(nodeptr, _type_)
#define castNode(_type_, nodeptr)
static int list_length(const List *l)
#define list_make2(x1, x2)
static bool DatumGetBool(Datum X)
static Datum PointerGetDatum(const void *X)
static Datum BoolGetDatum(bool X)
CollateClause * collClause
uint16 * ii_ExclusionStrats
ExprState * ii_PredicateState
List * ii_ExpressionsState
VarReturningType varreturningtype
String * makeString(char *str)