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

Generated by: LCOV version 1.14