LCOV - code coverage report
Current view: top level - src/backend/nodes - makefuncs.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 353 357 98.9 %
Date: 2025-01-29 20:17:04 Functions: 42 42 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * makefuncs.c
       4             :  *    creator functions for various nodes. The functions here are for the
       5             :  *    most frequently created nodes.
       6             :  *
       7             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  *
      11             :  * IDENTIFICATION
      12             :  *    src/backend/nodes/makefuncs.c
      13             :  *
      14             :  *-------------------------------------------------------------------------
      15             :  */
      16             : #include "postgres.h"
      17             : 
      18             : #include "catalog/pg_class.h"
      19             : #include "catalog/pg_type.h"
      20             : #include "nodes/makefuncs.h"
      21             : #include "nodes/nodeFuncs.h"
      22             : #include "utils/lsyscache.h"
      23             : 
      24             : 
      25             : /*
      26             :  * makeA_Expr -
      27             :  *      makes an A_Expr node
      28             :  */
      29             : A_Expr *
      30       71704 : makeA_Expr(A_Expr_Kind kind, List *name,
      31             :            Node *lexpr, Node *rexpr, int location)
      32             : {
      33       71704 :     A_Expr     *a = makeNode(A_Expr);
      34             : 
      35       71704 :     a->kind = kind;
      36       71704 :     a->name = name;
      37       71704 :     a->lexpr = lexpr;
      38       71704 :     a->rexpr = rexpr;
      39       71704 :     a->location = location;
      40       71704 :     return a;
      41             : }
      42             : 
      43             : /*
      44             :  * makeSimpleA_Expr -
      45             :  *      As above, given a simple (unqualified) operator name
      46             :  */
      47             : A_Expr *
      48      547384 : makeSimpleA_Expr(A_Expr_Kind kind, char *name,
      49             :                  Node *lexpr, Node *rexpr, int location)
      50             : {
      51      547384 :     A_Expr     *a = makeNode(A_Expr);
      52             : 
      53      547384 :     a->kind = kind;
      54      547384 :     a->name = list_make1(makeString((char *) name));
      55      547384 :     a->lexpr = lexpr;
      56      547384 :     a->rexpr = rexpr;
      57      547384 :     a->location = location;
      58      547384 :     return a;
      59             : }
      60             : 
      61             : /*
      62             :  * makeVar -
      63             :  *    creates a Var node
      64             :  */
      65             : Var *
      66     9145172 : makeVar(int varno,
      67             :         AttrNumber varattno,
      68             :         Oid vartype,
      69             :         int32 vartypmod,
      70             :         Oid varcollid,
      71             :         Index varlevelsup)
      72             : {
      73     9145172 :     Var        *var = makeNode(Var);
      74             : 
      75     9145172 :     var->varno = varno;
      76     9145172 :     var->varattno = varattno;
      77     9145172 :     var->vartype = vartype;
      78     9145172 :     var->vartypmod = vartypmod;
      79     9145172 :     var->varcollid = varcollid;
      80     9145172 :     var->varlevelsup = varlevelsup;
      81             : 
      82             :     /*
      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
      88             :      * callers.
      89             :      */
      90     9145172 :     var->varreturningtype = VAR_RETURNING_DEFAULT;
      91     9145172 :     var->varnullingrels = NULL;
      92     9145172 :     var->varnosyn = (Index) varno;
      93     9145172 :     var->varattnosyn = varattno;
      94             : 
      95             :     /* Likewise, we just set location to "unknown" here */
      96     9145172 :     var->location = -1;
      97             : 
      98     9145172 :     return var;
      99             : }
     100             : 
     101             : /*
     102             :  * makeVarFromTargetEntry -
     103             :  *      convenience function to create a same-level Var node from a
     104             :  *      TargetEntry
     105             :  */
     106             : Var *
     107      120258 : makeVarFromTargetEntry(int varno,
     108             :                        TargetEntry *tle)
     109             : {
     110      601290 :     return makeVar(varno,
     111      120258 :                    tle->resno,
     112      120258 :                    exprType((Node *) tle->expr),
     113      120258 :                    exprTypmod((Node *) tle->expr),
     114      120258 :                    exprCollation((Node *) tle->expr),
     115             :                    0);
     116             : }
     117             : 
     118             : /*
     119             :  * makeWholeRowVar -
     120             :  *    creates a Var node representing a whole row of the specified RTE
     121             :  *
     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.
     130             :  *
     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.
     135             :  */
     136             : Var *
     137        9392 : makeWholeRowVar(RangeTblEntry *rte,
     138             :                 int varno,
     139             :                 Index varlevelsup,
     140             :                 bool allowScalar)
     141             : {
     142             :     Var        *result;
     143             :     Oid         toid;
     144             :     Node       *fexpr;
     145             : 
     146        9392 :     switch (rte->rtekind)
     147             :     {
     148        7924 :         case RTE_RELATION:
     149             :             /* relation: the rowtype is a named composite type */
     150        7924 :             toid = get_rel_type_id(rte->relid);
     151        7924 :             if (!OidIsValid(toid))
     152           0 :                 ereport(ERROR,
     153             :                         (errcode(ERRCODE_WRONG_OBJECT_TYPE),
     154             :                          errmsg("relation \"%s\" does not have a composite type",
     155             :                                 get_rel_name(rte->relid))));
     156        7924 :             result = makeVar(varno,
     157             :                              InvalidAttrNumber,
     158             :                              toid,
     159             :                              -1,
     160             :                              InvalidOid,
     161             :                              varlevelsup);
     162        7924 :             break;
     163             : 
     164         174 :         case RTE_FUNCTION:
     165             : 
     166             :             /*
     167             :              * If there's more than one function, or ordinality is requested,
     168             :              * force a RECORD result, since there's certainly more than one
     169             :              * column involved and it can't be a known named type.
     170             :              */
     171         174 :             if (rte->funcordinality || list_length(rte->functions) != 1)
     172             :             {
     173             :                 /* always produces an anonymous RECORD result */
     174           6 :                 result = makeVar(varno,
     175             :                                  InvalidAttrNumber,
     176             :                                  RECORDOID,
     177             :                                  -1,
     178             :                                  InvalidOid,
     179             :                                  varlevelsup);
     180           6 :                 break;
     181             :             }
     182             : 
     183         168 :             fexpr = ((RangeTblFunction *) linitial(rte->functions))->funcexpr;
     184         168 :             toid = exprType(fexpr);
     185         168 :             if (type_is_rowtype(toid))
     186             :             {
     187             :                 /* func returns composite; same as relation case */
     188          62 :                 result = makeVar(varno,
     189             :                                  InvalidAttrNumber,
     190             :                                  toid,
     191             :                                  -1,
     192             :                                  InvalidOid,
     193             :                                  varlevelsup);
     194             :             }
     195         106 :             else if (allowScalar)
     196             :             {
     197             :                 /* func returns scalar; just return its output as-is */
     198          26 :                 result = makeVar(varno,
     199             :                                  1,
     200             :                                  toid,
     201             :                                  -1,
     202             :                                  exprCollation(fexpr),
     203             :                                  varlevelsup);
     204             :             }
     205             :             else
     206             :             {
     207             :                 /* func returns scalar, but we want a composite result */
     208          80 :                 result = makeVar(varno,
     209             :                                  InvalidAttrNumber,
     210             :                                  RECORDOID,
     211             :                                  -1,
     212             :                                  InvalidOid,
     213             :                                  varlevelsup);
     214             :             }
     215         168 :             break;
     216             : 
     217        1294 :         default:
     218             : 
     219             :             /*
     220             :              * RTE is a join, subselect, tablefunc, or VALUES.  We represent
     221             :              * this as a whole-row Var of RECORD type. (Note that in most
     222             :              * cases the Var will be expanded to a RowExpr during planning,
     223             :              * but that is not our concern here.)
     224             :              */
     225        1294 :             result = makeVar(varno,
     226             :                              InvalidAttrNumber,
     227             :                              RECORDOID,
     228             :                              -1,
     229             :                              InvalidOid,
     230             :                              varlevelsup);
     231        1294 :             break;
     232             :     }
     233             : 
     234        9392 :     return result;
     235             : }
     236             : 
     237             : /*
     238             :  * makeTargetEntry -
     239             :  *    creates a TargetEntry node
     240             :  */
     241             : TargetEntry *
     242     7618784 : makeTargetEntry(Expr *expr,
     243             :                 AttrNumber resno,
     244             :                 char *resname,
     245             :                 bool resjunk)
     246             : {
     247     7618784 :     TargetEntry *tle = makeNode(TargetEntry);
     248             : 
     249     7618784 :     tle->expr = expr;
     250     7618784 :     tle->resno = resno;
     251     7618784 :     tle->resname = resname;
     252             : 
     253             :     /*
     254             :      * We always set these fields to 0. If the caller wants to change them he
     255             :      * must do so explicitly.  Few callers do that, so omitting these
     256             :      * arguments reduces the chance of error.
     257             :      */
     258     7618784 :     tle->ressortgroupref = 0;
     259     7618784 :     tle->resorigtbl = InvalidOid;
     260     7618784 :     tle->resorigcol = 0;
     261             : 
     262     7618784 :     tle->resjunk = resjunk;
     263             : 
     264     7618784 :     return tle;
     265             : }
     266             : 
     267             : /*
     268             :  * flatCopyTargetEntry -
     269             :  *    duplicate a TargetEntry, but don't copy substructure
     270             :  *
     271             :  * This is commonly used when we just want to modify the resno or substitute
     272             :  * a new expression.
     273             :  */
     274             : TargetEntry *
     275      565854 : flatCopyTargetEntry(TargetEntry *src_tle)
     276             : {
     277      565854 :     TargetEntry *tle = makeNode(TargetEntry);
     278             : 
     279             :     Assert(IsA(src_tle, TargetEntry));
     280      565854 :     memcpy(tle, src_tle, sizeof(TargetEntry));
     281      565854 :     return tle;
     282             : }
     283             : 
     284             : /*
     285             :  * makeFromExpr -
     286             :  *    creates a FromExpr node
     287             :  */
     288             : FromExpr *
     289      604546 : makeFromExpr(List *fromlist, Node *quals)
     290             : {
     291      604546 :     FromExpr   *f = makeNode(FromExpr);
     292             : 
     293      604546 :     f->fromlist = fromlist;
     294      604546 :     f->quals = quals;
     295      604546 :     return f;
     296             : }
     297             : 
     298             : /*
     299             :  * makeConst -
     300             :  *    creates a Const node
     301             :  */
     302             : Const *
     303     2534614 : makeConst(Oid consttype,
     304             :           int32 consttypmod,
     305             :           Oid constcollid,
     306             :           int constlen,
     307             :           Datum constvalue,
     308             :           bool constisnull,
     309             :           bool constbyval)
     310             : {
     311     2534614 :     Const      *cnst = makeNode(Const);
     312             : 
     313             :     /*
     314             :      * If it's a varlena value, force it to be in non-expanded (non-toasted)
     315             :      * format; this avoids any possible dependency on external values and
     316             :      * improves consistency of representation, which is important for equal().
     317             :      */
     318     2534614 :     if (!constisnull && constlen == -1)
     319      136462 :         constvalue = PointerGetDatum(PG_DETOAST_DATUM(constvalue));
     320             : 
     321     2534614 :     cnst->consttype = consttype;
     322     2534614 :     cnst->consttypmod = consttypmod;
     323     2534614 :     cnst->constcollid = constcollid;
     324     2534614 :     cnst->constlen = constlen;
     325     2534614 :     cnst->constvalue = constvalue;
     326     2534614 :     cnst->constisnull = constisnull;
     327     2534614 :     cnst->constbyval = constbyval;
     328     2534614 :     cnst->location = -1;     /* "unknown" */
     329             : 
     330     2534614 :     return cnst;
     331             : }
     332             : 
     333             : /*
     334             :  * makeNullConst -
     335             :  *    creates a Const node representing a NULL of the specified type/typmod
     336             :  *
     337             :  * This is a convenience routine that just saves a lookup of the type's
     338             :  * storage properties.
     339             :  */
     340             : Const *
     341        9034 : makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
     342             : {
     343             :     int16       typLen;
     344             :     bool        typByVal;
     345             : 
     346        9034 :     get_typlenbyval(consttype, &typLen, &typByVal);
     347        9034 :     return makeConst(consttype,
     348             :                      consttypmod,
     349             :                      constcollid,
     350             :                      (int) typLen,
     351             :                      (Datum) 0,
     352             :                      true,
     353             :                      typByVal);
     354             : }
     355             : 
     356             : /*
     357             :  * makeBoolConst -
     358             :  *    creates a Const node representing a boolean value (can be NULL too)
     359             :  */
     360             : Node *
     361        7308 : makeBoolConst(bool value, bool isnull)
     362             : {
     363             :     /* note that pg_type.h hardwires size of bool as 1 ... duplicate it */
     364        7308 :     return (Node *) makeConst(BOOLOID, -1, InvalidOid, 1,
     365             :                               BoolGetDatum(value), isnull, true);
     366             : }
     367             : 
     368             : /*
     369             :  * makeBoolExpr -
     370             :  *    creates a BoolExpr node
     371             :  */
     372             : Expr *
     373      289166 : makeBoolExpr(BoolExprType boolop, List *args, int location)
     374             : {
     375      289166 :     BoolExpr   *b = makeNode(BoolExpr);
     376             : 
     377      289166 :     b->boolop = boolop;
     378      289166 :     b->args = args;
     379      289166 :     b->location = location;
     380             : 
     381      289166 :     return (Expr *) b;
     382             : }
     383             : 
     384             : /*
     385             :  * makeAlias -
     386             :  *    creates an Alias node
     387             :  *
     388             :  * NOTE: the given name is copied, but the colnames list (if any) isn't.
     389             :  */
     390             : Alias *
     391     1010074 : makeAlias(const char *aliasname, List *colnames)
     392             : {
     393     1010074 :     Alias      *a = makeNode(Alias);
     394             : 
     395     1010074 :     a->aliasname = pstrdup(aliasname);
     396     1010074 :     a->colnames = colnames;
     397             : 
     398     1010074 :     return a;
     399             : }
     400             : 
     401             : /*
     402             :  * makeRelabelType -
     403             :  *    creates a RelabelType node
     404             :  */
     405             : RelabelType *
     406      135858 : makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid,
     407             :                 CoercionForm rformat)
     408             : {
     409      135858 :     RelabelType *r = makeNode(RelabelType);
     410             : 
     411      135858 :     r->arg = arg;
     412      135858 :     r->resulttype = rtype;
     413      135858 :     r->resulttypmod = rtypmod;
     414      135858 :     r->resultcollid = rcollid;
     415      135858 :     r->relabelformat = rformat;
     416      135858 :     r->location = -1;
     417             : 
     418      135858 :     return r;
     419             : }
     420             : 
     421             : /*
     422             :  * makeRangeVar -
     423             :  *    creates a RangeVar node (rather oversimplified case)
     424             :  */
     425             : RangeVar *
     426      818740 : makeRangeVar(char *schemaname, char *relname, int location)
     427             : {
     428      818740 :     RangeVar   *r = makeNode(RangeVar);
     429             : 
     430      818740 :     r->catalogname = NULL;
     431      818740 :     r->schemaname = schemaname;
     432      818740 :     r->relname = relname;
     433      818740 :     r->inh = true;
     434      818740 :     r->relpersistence = RELPERSISTENCE_PERMANENT;
     435      818740 :     r->alias = NULL;
     436      818740 :     r->location = location;
     437             : 
     438      818740 :     return r;
     439             : }
     440             : 
     441             : /*
     442             :  * makeNotNullConstraint -
     443             :  *      creates a Constraint node for NOT NULL constraints
     444             :  */
     445             : Constraint *
     446       14966 : makeNotNullConstraint(String *colname)
     447             : {
     448             :     Constraint *notnull;
     449             : 
     450       14966 :     notnull = makeNode(Constraint);
     451       14966 :     notnull->contype = CONSTR_NOTNULL;
     452       14966 :     notnull->conname = NULL;
     453       14966 :     notnull->is_no_inherit = false;
     454       14966 :     notnull->deferrable = false;
     455       14966 :     notnull->initdeferred = false;
     456       14966 :     notnull->location = -1;
     457       14966 :     notnull->keys = list_make1(colname);
     458       14966 :     notnull->is_enforced = true;
     459       14966 :     notnull->skip_validation = false;
     460       14966 :     notnull->initially_valid = true;
     461             : 
     462       14966 :     return notnull;
     463             : }
     464             : 
     465             : /*
     466             :  * makeTypeName -
     467             :  *  build a TypeName node for an unqualified name.
     468             :  *
     469             :  * typmod is defaulted, but can be changed later by caller.
     470             :  */
     471             : TypeName *
     472      260984 : makeTypeName(char *typnam)
     473             : {
     474      260984 :     return makeTypeNameFromNameList(list_make1(makeString(typnam)));
     475             : }
     476             : 
     477             : /*
     478             :  * makeTypeNameFromNameList -
     479             :  *  build a TypeName node for a String list representing a qualified name.
     480             :  *
     481             :  * typmod is defaulted, but can be changed later by caller.
     482             :  */
     483             : TypeName *
     484      528748 : makeTypeNameFromNameList(List *names)
     485             : {
     486      528748 :     TypeName   *n = makeNode(TypeName);
     487             : 
     488      528748 :     n->names = names;
     489      528748 :     n->typmods = NIL;
     490      528748 :     n->typemod = -1;
     491      528748 :     n->location = -1;
     492      528748 :     return n;
     493             : }
     494             : 
     495             : /*
     496             :  * makeTypeNameFromOid -
     497             :  *  build a TypeName node to represent a type already known by OID/typmod.
     498             :  */
     499             : TypeName *
     500      170056 : makeTypeNameFromOid(Oid typeOid, int32 typmod)
     501             : {
     502      170056 :     TypeName   *n = makeNode(TypeName);
     503             : 
     504      170056 :     n->typeOid = typeOid;
     505      170056 :     n->typemod = typmod;
     506      170056 :     n->location = -1;
     507      170056 :     return n;
     508             : }
     509             : 
     510             : /*
     511             :  * makeColumnDef -
     512             :  *  build a ColumnDef node to represent a simple column definition.
     513             :  *
     514             :  * Type and collation are specified by OID.
     515             :  * Other properties are all basic to start with.
     516             :  */
     517             : ColumnDef *
     518      168790 : makeColumnDef(const char *colname, Oid typeOid, int32 typmod, Oid collOid)
     519             : {
     520      168790 :     ColumnDef  *n = makeNode(ColumnDef);
     521             : 
     522      168790 :     n->colname = pstrdup(colname);
     523      168790 :     n->typeName = makeTypeNameFromOid(typeOid, typmod);
     524      168790 :     n->inhcount = 0;
     525      168790 :     n->is_local = true;
     526      168790 :     n->is_not_null = false;
     527      168790 :     n->is_from_type = false;
     528      168790 :     n->storage = 0;
     529      168790 :     n->raw_default = NULL;
     530      168790 :     n->cooked_default = NULL;
     531      168790 :     n->collClause = NULL;
     532      168790 :     n->collOid = collOid;
     533      168790 :     n->constraints = NIL;
     534      168790 :     n->fdwoptions = NIL;
     535      168790 :     n->location = -1;
     536             : 
     537      168790 :     return n;
     538             : }
     539             : 
     540             : /*
     541             :  * makeFuncExpr -
     542             :  *  build an expression tree representing a function call.
     543             :  *
     544             :  * The argument expressions must have been transformed already.
     545             :  */
     546             : FuncExpr *
     547      157048 : makeFuncExpr(Oid funcid, Oid rettype, List *args,
     548             :              Oid funccollid, Oid inputcollid, CoercionForm fformat)
     549             : {
     550             :     FuncExpr   *funcexpr;
     551             : 
     552      157048 :     funcexpr = makeNode(FuncExpr);
     553      157048 :     funcexpr->funcid = funcid;
     554      157048 :     funcexpr->funcresulttype = rettype;
     555      157048 :     funcexpr->funcretset = false;    /* only allowed case here */
     556      157048 :     funcexpr->funcvariadic = false; /* only allowed case here */
     557      157048 :     funcexpr->funcformat = fformat;
     558      157048 :     funcexpr->funccollid = funccollid;
     559      157048 :     funcexpr->inputcollid = inputcollid;
     560      157048 :     funcexpr->args = args;
     561      157048 :     funcexpr->location = -1;
     562             : 
     563      157048 :     return funcexpr;
     564             : }
     565             : 
     566             : /*
     567             :  * makeStringConst -
     568             :  *  build a A_Const node of type T_String for given string
     569             :  */
     570             : Node *
     571      627306 : makeStringConst(char *str, int location)
     572             : {
     573      627306 :     A_Const    *n = makeNode(A_Const);
     574             : 
     575      627306 :     n->val.sval.type = T_String;
     576      627306 :     n->val.sval.sval = str;
     577      627306 :     n->location = location;
     578             : 
     579      627306 :     return (Node *) n;
     580             : }
     581             : 
     582             : /*
     583             :  * makeDefElem -
     584             :  *  build a DefElem node
     585             :  *
     586             :  * This is sufficient for the "typical" case with an unqualified option name
     587             :  * and no special action.
     588             :  */
     589             : DefElem *
     590      223252 : makeDefElem(char *name, Node *arg, int location)
     591             : {
     592      223252 :     DefElem    *res = makeNode(DefElem);
     593             : 
     594      223252 :     res->defnamespace = NULL;
     595      223252 :     res->defname = name;
     596      223252 :     res->arg = arg;
     597      223252 :     res->defaction = DEFELEM_UNSPEC;
     598      223252 :     res->location = location;
     599             : 
     600      223252 :     return res;
     601             : }
     602             : 
     603             : /*
     604             :  * makeDefElemExtended -
     605             :  *  build a DefElem node with all fields available to be specified
     606             :  */
     607             : DefElem *
     608         194 : makeDefElemExtended(char *nameSpace, char *name, Node *arg,
     609             :                     DefElemAction defaction, int location)
     610             : {
     611         194 :     DefElem    *res = makeNode(DefElem);
     612             : 
     613         194 :     res->defnamespace = nameSpace;
     614         194 :     res->defname = name;
     615         194 :     res->arg = arg;
     616         194 :     res->defaction = defaction;
     617         194 :     res->location = location;
     618             : 
     619         194 :     return res;
     620             : }
     621             : 
     622             : /*
     623             :  * makeFuncCall -
     624             :  *
     625             :  * Initialize a FuncCall struct with the information every caller must
     626             :  * supply.  Any non-default parameters have to be inserted by the caller.
     627             :  */
     628             : FuncCall *
     629      361518 : makeFuncCall(List *name, List *args, CoercionForm funcformat, int location)
     630             : {
     631      361518 :     FuncCall   *n = makeNode(FuncCall);
     632             : 
     633      361518 :     n->funcname = name;
     634      361518 :     n->args = args;
     635      361518 :     n->agg_order = NIL;
     636      361518 :     n->agg_filter = NULL;
     637      361518 :     n->over = NULL;
     638      361518 :     n->agg_within_group = false;
     639      361518 :     n->agg_star = false;
     640      361518 :     n->agg_distinct = false;
     641      361518 :     n->func_variadic = false;
     642      361518 :     n->funcformat = funcformat;
     643      361518 :     n->location = location;
     644      361518 :     return n;
     645             : }
     646             : 
     647             : /*
     648             :  * make_opclause
     649             :  *    Creates an operator clause given its operator info, left operand
     650             :  *    and right operand (pass NULL to create single-operand clause),
     651             :  *    and collation info.
     652             :  */
     653             : Expr *
     654      138718 : make_opclause(Oid opno, Oid opresulttype, bool opretset,
     655             :               Expr *leftop, Expr *rightop,
     656             :               Oid opcollid, Oid inputcollid)
     657             : {
     658      138718 :     OpExpr     *expr = makeNode(OpExpr);
     659             : 
     660      138718 :     expr->opno = opno;
     661      138718 :     expr->opfuncid = InvalidOid;
     662      138718 :     expr->opresulttype = opresulttype;
     663      138718 :     expr->opretset = opretset;
     664      138718 :     expr->opcollid = opcollid;
     665      138718 :     expr->inputcollid = inputcollid;
     666      138718 :     if (rightop)
     667      138718 :         expr->args = list_make2(leftop, rightop);
     668             :     else
     669           0 :         expr->args = list_make1(leftop);
     670      138718 :     expr->location = -1;
     671      138718 :     return (Expr *) expr;
     672             : }
     673             : 
     674             : /*
     675             :  * make_andclause
     676             :  *
     677             :  * Creates an 'and' clause given a list of its subclauses.
     678             :  */
     679             : Expr *
     680      281272 : make_andclause(List *andclauses)
     681             : {
     682      281272 :     BoolExpr   *expr = makeNode(BoolExpr);
     683             : 
     684      281272 :     expr->boolop = AND_EXPR;
     685      281272 :     expr->args = andclauses;
     686      281272 :     expr->location = -1;
     687      281272 :     return (Expr *) expr;
     688             : }
     689             : 
     690             : /*
     691             :  * make_orclause
     692             :  *
     693             :  * Creates an 'or' clause given a list of its subclauses.
     694             :  */
     695             : Expr *
     696       36330 : make_orclause(List *orclauses)
     697             : {
     698       36330 :     BoolExpr   *expr = makeNode(BoolExpr);
     699             : 
     700       36330 :     expr->boolop = OR_EXPR;
     701       36330 :     expr->args = orclauses;
     702       36330 :     expr->location = -1;
     703       36330 :     return (Expr *) expr;
     704             : }
     705             : 
     706             : /*
     707             :  * make_notclause
     708             :  *
     709             :  * Create a 'not' clause given the expression to be negated.
     710             :  */
     711             : Expr *
     712       11266 : make_notclause(Expr *notclause)
     713             : {
     714       11266 :     BoolExpr   *expr = makeNode(BoolExpr);
     715             : 
     716       11266 :     expr->boolop = NOT_EXPR;
     717       11266 :     expr->args = list_make1(notclause);
     718       11266 :     expr->location = -1;
     719       11266 :     return (Expr *) expr;
     720             : }
     721             : 
     722             : /*
     723             :  * make_and_qual
     724             :  *
     725             :  * Variant of make_andclause for ANDing two qual conditions together.
     726             :  * Qual conditions have the property that a NULL nodetree is interpreted
     727             :  * as 'true'.
     728             :  *
     729             :  * NB: this makes no attempt to preserve AND/OR flatness; so it should not
     730             :  * be used on a qual that has already been run through prepqual.c.
     731             :  */
     732             : Node *
     733        3784 : make_and_qual(Node *qual1, Node *qual2)
     734             : {
     735        3784 :     if (qual1 == NULL)
     736        1444 :         return qual2;
     737        2340 :     if (qual2 == NULL)
     738           0 :         return qual1;
     739        2340 :     return (Node *) make_andclause(list_make2(qual1, qual2));
     740             : }
     741             : 
     742             : /*
     743             :  * The planner and executor usually represent qualification expressions
     744             :  * as lists of boolean expressions with implicit AND semantics.
     745             :  *
     746             :  * These functions convert between an AND-semantics expression list and the
     747             :  * ordinary representation of a boolean expression.
     748             :  *
     749             :  * Note that an empty list is considered equivalent to TRUE.
     750             :  */
     751             : Expr *
     752       51672 : make_ands_explicit(List *andclauses)
     753             : {
     754       51672 :     if (andclauses == NIL)
     755           0 :         return (Expr *) makeBoolConst(true, false);
     756       51672 :     else if (list_length(andclauses) == 1)
     757       37618 :         return (Expr *) linitial(andclauses);
     758             :     else
     759       14054 :         return make_andclause(andclauses);
     760             : }
     761             : 
     762             : List *
     763      390640 : make_ands_implicit(Expr *clause)
     764             : {
     765             :     /*
     766             :      * NB: because the parser sets the qual field to NULL in a query that has
     767             :      * no WHERE clause, we must consider a NULL input clause as TRUE, even
     768             :      * though one might more reasonably think it FALSE.
     769             :      */
     770      390640 :     if (clause == NULL)
     771       65008 :         return NIL;             /* NULL -> NIL list == TRUE */
     772      325632 :     else if (is_andclause(clause))
     773      107952 :         return ((BoolExpr *) clause)->args;
     774      217680 :     else if (IsA(clause, Const) &&
     775        5424 :              !((Const *) clause)->constisnull &&
     776        2634 :              DatumGetBool(((Const *) clause)->constvalue))
     777        1588 :         return NIL;             /* constant TRUE input -> NIL list */
     778             :     else
     779      216092 :         return list_make1(clause);
     780             : }
     781             : 
     782             : /*
     783             :  * makeIndexInfo
     784             :  *    create an IndexInfo node
     785             :  */
     786             : IndexInfo *
     787     3265188 : makeIndexInfo(int numattrs, int numkeyattrs, Oid amoid, List *expressions,
     788             :               List *predicates, bool unique, bool nulls_not_distinct,
     789             :               bool isready, bool concurrent, bool summarizing,
     790             :               bool withoutoverlaps)
     791             : {
     792     3265188 :     IndexInfo  *n = makeNode(IndexInfo);
     793             : 
     794     3265188 :     n->ii_NumIndexAttrs = numattrs;
     795     3265188 :     n->ii_NumIndexKeyAttrs = numkeyattrs;
     796             :     Assert(n->ii_NumIndexKeyAttrs != 0);
     797             :     Assert(n->ii_NumIndexKeyAttrs <= n->ii_NumIndexAttrs);
     798     3265188 :     n->ii_Unique = unique;
     799     3265188 :     n->ii_NullsNotDistinct = nulls_not_distinct;
     800     3265188 :     n->ii_ReadyForInserts = isready;
     801     3265188 :     n->ii_CheckedUnchanged = false;
     802     3265188 :     n->ii_IndexUnchanged = false;
     803     3265188 :     n->ii_Concurrent = concurrent;
     804     3265188 :     n->ii_Summarizing = summarizing;
     805     3265188 :     n->ii_WithoutOverlaps = withoutoverlaps;
     806             : 
     807             :     /* summarizing indexes cannot contain non-key attributes */
     808             :     Assert(!summarizing || (numkeyattrs == numattrs));
     809             : 
     810             :     /* expressions */
     811     3265188 :     n->ii_Expressions = expressions;
     812     3265188 :     n->ii_ExpressionsState = NIL;
     813             : 
     814             :     /* predicates  */
     815     3265188 :     n->ii_Predicate = predicates;
     816     3265188 :     n->ii_PredicateState = NULL;
     817             : 
     818             :     /* exclusion constraints */
     819     3265188 :     n->ii_ExclusionOps = NULL;
     820     3265188 :     n->ii_ExclusionProcs = NULL;
     821     3265188 :     n->ii_ExclusionStrats = NULL;
     822             : 
     823             :     /* speculative inserts */
     824     3265188 :     n->ii_UniqueOps = NULL;
     825     3265188 :     n->ii_UniqueProcs = NULL;
     826     3265188 :     n->ii_UniqueStrats = NULL;
     827             : 
     828             :     /* initialize index-build state to default */
     829     3265188 :     n->ii_BrokenHotChain = false;
     830     3265188 :     n->ii_ParallelWorkers = 0;
     831             : 
     832             :     /* set up for possible use by index AM */
     833     3265188 :     n->ii_Am = amoid;
     834     3265188 :     n->ii_AmCache = NULL;
     835     3265188 :     n->ii_Context = CurrentMemoryContext;
     836             : 
     837     3265188 :     return n;
     838             : }
     839             : 
     840             : /*
     841             :  * makeGroupingSet
     842             :  *
     843             :  */
     844             : GroupingSet *
     845        5326 : makeGroupingSet(GroupingSetKind kind, List *content, int location)
     846             : {
     847        5326 :     GroupingSet *n = makeNode(GroupingSet);
     848             : 
     849        5326 :     n->kind = kind;
     850        5326 :     n->content = content;
     851        5326 :     n->location = location;
     852        5326 :     return n;
     853             : }
     854             : 
     855             : /*
     856             :  * makeVacuumRelation -
     857             :  *    create a VacuumRelation node
     858             :  */
     859             : VacuumRelation *
     860      133962 : makeVacuumRelation(RangeVar *relation, Oid oid, List *va_cols)
     861             : {
     862      133962 :     VacuumRelation *v = makeNode(VacuumRelation);
     863             : 
     864      133962 :     v->relation = relation;
     865      133962 :     v->oid = oid;
     866      133962 :     v->va_cols = va_cols;
     867      133962 :     return v;
     868             : }
     869             : 
     870             : /*
     871             :  * makeJsonFormat -
     872             :  *    creates a JsonFormat node
     873             :  */
     874             : JsonFormat *
     875       10022 : makeJsonFormat(JsonFormatType type, JsonEncoding encoding, int location)
     876             : {
     877       10022 :     JsonFormat *jf = makeNode(JsonFormat);
     878             : 
     879       10022 :     jf->format_type = type;
     880       10022 :     jf->encoding = encoding;
     881       10022 :     jf->location = location;
     882             : 
     883       10022 :     return jf;
     884             : }
     885             : 
     886             : /*
     887             :  * makeJsonValueExpr -
     888             :  *    creates a JsonValueExpr node
     889             :  */
     890             : JsonValueExpr *
     891        5410 : makeJsonValueExpr(Expr *raw_expr, Expr *formatted_expr,
     892             :                   JsonFormat *format)
     893             : {
     894        5410 :     JsonValueExpr *jve = makeNode(JsonValueExpr);
     895             : 
     896        5410 :     jve->raw_expr = raw_expr;
     897        5410 :     jve->formatted_expr = formatted_expr;
     898        5410 :     jve->format = format;
     899             : 
     900        5410 :     return jve;
     901             : }
     902             : 
     903             : /*
     904             :  * makeJsonBehavior -
     905             :  *    creates a JsonBehavior node
     906             :  */
     907             : JsonBehavior *
     908        5346 : makeJsonBehavior(JsonBehaviorType btype, Node *expr, int location)
     909             : {
     910        5346 :     JsonBehavior *behavior = makeNode(JsonBehavior);
     911             : 
     912        5346 :     behavior->btype = btype;
     913        5346 :     behavior->expr = expr;
     914        5346 :     behavior->location = location;
     915             : 
     916        5346 :     return behavior;
     917             : }
     918             : 
     919             : /*
     920             :  * makeJsonKeyValue -
     921             :  *    creates a JsonKeyValue node
     922             :  */
     923             : Node *
     924         802 : makeJsonKeyValue(Node *key, Node *value)
     925             : {
     926         802 :     JsonKeyValue *n = makeNode(JsonKeyValue);
     927             : 
     928         802 :     n->key = (Expr *) key;
     929         802 :     n->value = castNode(JsonValueExpr, value);
     930             : 
     931         802 :     return (Node *) n;
     932             : }
     933             : 
     934             : /*
     935             :  * makeJsonIsPredicate -
     936             :  *    creates a JsonIsPredicate node
     937             :  */
     938             : Node *
     939         694 : makeJsonIsPredicate(Node *expr, JsonFormat *format, JsonValueType item_type,
     940             :                     bool unique_keys, int location)
     941             : {
     942         694 :     JsonIsPredicate *n = makeNode(JsonIsPredicate);
     943             : 
     944         694 :     n->expr = expr;
     945         694 :     n->format = format;
     946         694 :     n->item_type = item_type;
     947         694 :     n->unique_keys = unique_keys;
     948         694 :     n->location = location;
     949             : 
     950         694 :     return (Node *) n;
     951             : }
     952             : 
     953             : /*
     954             :  * makeJsonTablePathSpec -
     955             :  *      Make JsonTablePathSpec node from given path string and name (if any)
     956             :  */
     957             : JsonTablePathSpec *
     958        1642 : makeJsonTablePathSpec(char *string, char *name, int string_location,
     959             :                       int name_location)
     960             : {
     961        1642 :     JsonTablePathSpec *pathspec = makeNode(JsonTablePathSpec);
     962             : 
     963             :     Assert(string != NULL);
     964        1642 :     pathspec->string = makeStringConst(string, string_location);
     965        1642 :     if (name != NULL)
     966         208 :         pathspec->name = pstrdup(name);
     967             : 
     968        1642 :     pathspec->name_location = name_location;
     969        1642 :     pathspec->location = string_location;
     970             : 
     971        1642 :     return pathspec;
     972             : }
     973             : 
     974             : /*
     975             :  * makeJsonTablePath -
     976             :  *      Make JsonTablePath node for given path string and name
     977             :  */
     978             : JsonTablePath *
     979         706 : makeJsonTablePath(Const *pathvalue, char *pathname)
     980             : {
     981         706 :     JsonTablePath *path = makeNode(JsonTablePath);
     982             : 
     983             :     Assert(IsA(pathvalue, Const));
     984         706 :     path->value = pathvalue;
     985         706 :     path->name = pathname;
     986             : 
     987         706 :     return path;
     988             : }

Generated by: LCOV version 1.14