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

Generated by: LCOV version 1.14