TLA 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 GIC 48596 : makeA_Expr(A_Expr_Kind kind, List *name,
32 ECB : Node *lexpr, Node *rexpr, int location)
33 : {
34 GIC 48596 : A_Expr *a = makeNode(A_Expr);
35 ECB :
36 GIC 48596 : a->kind = kind;
37 CBC 48596 : a->name = name;
38 48596 : a->lexpr = lexpr;
39 48596 : a->rexpr = rexpr;
40 48596 : a->location = location;
41 48596 : return a;
42 ECB : }
43 :
44 : /*
45 : * makeSimpleA_Expr -
46 : * As above, given a simple (unqualified) operator name
47 : */
48 : A_Expr *
49 GIC 429944 : makeSimpleA_Expr(A_Expr_Kind kind, char *name,
50 ECB : Node *lexpr, Node *rexpr, int location)
51 : {
52 GIC 429944 : A_Expr *a = makeNode(A_Expr);
53 ECB :
54 GIC 429944 : a->kind = kind;
55 CBC 429944 : a->name = list_make1(makeString((char *) name));
56 429944 : a->lexpr = lexpr;
57 429944 : a->rexpr = rexpr;
58 429944 : a->location = location;
59 429944 : return a;
60 ECB : }
61 :
62 : /*
63 : * makeVar -
64 : * creates a Var node
65 : */
66 : Var *
67 GIC 6727283 : makeVar(int varno,
68 ECB : AttrNumber varattno,
69 : Oid vartype,
70 : int32 vartypmod,
71 : Oid varcollid,
72 : Index varlevelsup)
73 : {
74 GIC 6727283 : Var *var = makeNode(Var);
75 ECB :
76 GIC 6727283 : var->varno = varno;
77 CBC 6727283 : var->varattno = varattno;
78 6727283 : var->vartype = vartype;
79 6727283 : var->vartypmod = vartypmod;
80 6727283 : var->varcollid = varcollid;
81 6727283 : var->varlevelsup = varlevelsup;
82 ECB :
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 GNC 6727283 : var->varnullingrels = NULL;
91 GIC 6727283 : var->varnosyn = (Index) varno;
92 6727283 : var->varattnosyn = varattno;
93 ECB :
94 : /* Likewise, we just set location to "unknown" here */
95 CBC 6727283 : var->location = -1;
96 :
97 GIC 6727283 : return var;
98 ECB : }
99 :
100 : /*
101 : * makeVarFromTargetEntry -
102 : * convenience function to create a same-level Var node from a
103 : * TargetEntry
104 : */
105 : Var *
106 GIC 72571 : makeVarFromTargetEntry(int varno,
107 : TargetEntry *tle)
108 : {
109 CBC 362855 : return makeVar(varno,
110 GIC 72571 : tle->resno,
111 72571 : exprType((Node *) tle->expr),
112 CBC 72571 : exprTypmod((Node *) tle->expr),
113 72571 : exprCollation((Node *) tle->expr),
114 ECB : 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 GIC 18740 : makeWholeRowVar(RangeTblEntry *rte,
137 : int varno,
138 : Index varlevelsup,
139 ECB : bool allowScalar)
140 : {
141 : Var *result;
142 : Oid toid;
143 : Node *fexpr;
144 :
145 GIC 18740 : switch (rte->rtekind)
146 : {
147 18217 : case RTE_RELATION:
148 ECB : /* relation: the rowtype is a named composite type */
149 GIC 18217 : toid = get_rel_type_id(rte->relid);
150 CBC 18217 : if (!OidIsValid(toid))
151 UIC 0 : ereport(ERROR,
152 ECB : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
153 : errmsg("relation \"%s\" does not have a composite type",
154 EUB : get_rel_name(rte->relid))));
155 GIC 18217 : result = makeVar(varno,
156 : InvalidAttrNumber,
157 : toid,
158 ECB : -1,
159 : InvalidOid,
160 : varlevelsup);
161 GIC 18217 : break;
162 :
163 46 : case RTE_FUNCTION:
164 ECB :
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 GIC 46 : if (rte->funcordinality || list_length(rte->functions) != 1)
171 : {
172 : /* always produces an anonymous RECORD result */
173 CBC 3 : result = makeVar(varno,
174 : InvalidAttrNumber,
175 : RECORDOID,
176 ECB : -1,
177 : InvalidOid,
178 : varlevelsup);
179 GIC 3 : break;
180 : }
181 :
182 CBC 43 : fexpr = ((RangeTblFunction *) linitial(rte->functions))->funcexpr;
183 GIC 43 : toid = exprType(fexpr);
184 43 : if (type_is_rowtype(toid))
185 ECB : {
186 : /* func returns composite; same as relation case */
187 CBC 30 : result = makeVar(varno,
188 : InvalidAttrNumber,
189 : toid,
190 ECB : -1,
191 : InvalidOid,
192 : varlevelsup);
193 : }
194 GIC 13 : else if (allowScalar)
195 : {
196 : /* func returns scalar; just return its output as-is */
197 CBC 13 : result = makeVar(varno,
198 : 1,
199 : toid,
200 ECB : -1,
201 : exprCollation(fexpr),
202 : varlevelsup);
203 : }
204 : else
205 : {
206 : /* func returns scalar, but we want a composite result */
207 UIC 0 : result = makeVar(varno,
208 : InvalidAttrNumber,
209 : RECORDOID,
210 EUB : -1,
211 : InvalidOid,
212 : varlevelsup);
213 : }
214 GIC 43 : break;
215 :
216 477 : default:
217 ECB :
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 GIC 477 : result = makeVar(varno,
225 : InvalidAttrNumber,
226 : RECORDOID,
227 ECB : -1,
228 : InvalidOid,
229 : varlevelsup);
230 GIC 477 : break;
231 : }
232 :
233 CBC 18740 : return result;
234 : }
235 :
236 ECB : /*
237 : * makeTargetEntry -
238 : * creates a TargetEntry node
239 : */
240 : TargetEntry *
241 GIC 3864179 : makeTargetEntry(Expr *expr,
242 : AttrNumber resno,
243 : char *resname,
244 ECB : bool resjunk)
245 : {
246 GIC 3864179 : TargetEntry *tle = makeNode(TargetEntry);
247 :
248 3864179 : tle->expr = expr;
249 CBC 3864179 : tle->resno = resno;
250 GIC 3864179 : tle->resname = resname;
251 ECB :
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 GIC 3864179 : tle->ressortgroupref = 0;
258 3864179 : tle->resorigtbl = InvalidOid;
259 3864179 : tle->resorigcol = 0;
260 ECB :
261 CBC 3864179 : tle->resjunk = resjunk;
262 ECB :
263 GIC 3864179 : return tle;
264 ECB : }
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 GIC 223216 : flatCopyTargetEntry(TargetEntry *src_tle)
275 : {
276 223216 : TargetEntry *tle = makeNode(TargetEntry);
277 ECB :
278 GIC 223216 : Assert(IsA(src_tle, TargetEntry));
279 CBC 223216 : memcpy(tle, src_tle, sizeof(TargetEntry));
280 GIC 223216 : return tle;
281 ECB : }
282 :
283 : /*
284 : * makeFromExpr -
285 : * creates a FromExpr node
286 : */
287 : FromExpr *
288 GIC 357057 : makeFromExpr(List *fromlist, Node *quals)
289 : {
290 357057 : FromExpr *f = makeNode(FromExpr);
291 ECB :
292 GIC 357057 : f->fromlist = fromlist;
293 CBC 357057 : f->quals = quals;
294 GIC 357057 : return f;
295 ECB : }
296 :
297 : /*
298 : * makeConst -
299 : * creates a Const node
300 : */
301 : Const *
302 GIC 1674021 : makeConst(Oid consttype,
303 : int32 consttypmod,
304 : Oid constcollid,
305 ECB : int constlen,
306 : Datum constvalue,
307 : bool constisnull,
308 : bool constbyval)
309 : {
310 GIC 1674021 : Const *cnst = makeNode(Const);
311 :
312 : /*
313 ECB : * 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 GIC 1674021 : if (!constisnull && constlen == -1)
318 62308 : constvalue = PointerGetDatum(PG_DETOAST_DATUM(constvalue));
319 :
320 CBC 1674021 : cnst->consttype = consttype;
321 1674021 : cnst->consttypmod = consttypmod;
322 GIC 1674021 : cnst->constcollid = constcollid;
323 CBC 1674021 : cnst->constlen = constlen;
324 1674021 : cnst->constvalue = constvalue;
325 1674021 : cnst->constisnull = constisnull;
326 1674021 : cnst->constbyval = constbyval;
327 1674021 : cnst->location = -1; /* "unknown" */
328 ECB :
329 CBC 1674021 : return cnst;
330 ECB : }
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 GIC 5002 : makeNullConst(Oid consttype, int32 consttypmod, Oid constcollid)
341 : {
342 : int16 typLen;
343 ECB : bool typByVal;
344 :
345 GIC 5002 : get_typlenbyval(consttype, &typLen, &typByVal);
346 5002 : return makeConst(consttype,
347 : consttypmod,
348 ECB : 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 GIC 2525 : makeBoolConst(bool value, bool isnull)
361 : {
362 : /* note that pg_type.h hardwires size of bool as 1 ... duplicate it */
363 CBC 2525 : return (Node *) makeConst(BOOLOID, -1, InvalidOid, 1,
364 : BoolGetDatum(value), isnull, true);
365 : }
366 ECB :
367 : /*
368 : * makeBoolExpr -
369 : * creates a BoolExpr node
370 : */
371 : Expr *
372 GIC 235082 : makeBoolExpr(BoolExprType boolop, List *args, int location)
373 : {
374 235082 : BoolExpr *b = makeNode(BoolExpr);
375 ECB :
376 GIC 235082 : b->boolop = boolop;
377 CBC 235082 : b->args = args;
378 GIC 235082 : b->location = location;
379 ECB :
380 CBC 235082 : return (Expr *) b;
381 ECB : }
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 GIC 699570 : makeAlias(const char *aliasname, List *colnames)
391 : {
392 699570 : Alias *a = makeNode(Alias);
393 ECB :
394 GIC 699570 : a->aliasname = pstrdup(aliasname);
395 CBC 699570 : a->colnames = colnames;
396 :
397 699570 : return a;
398 ECB : }
399 :
400 : /*
401 : * makeRelabelType -
402 : * creates a RelabelType node
403 : */
404 : RelabelType *
405 GIC 89048 : makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid,
406 : CoercionForm rformat)
407 : {
408 CBC 89048 : RelabelType *r = makeNode(RelabelType);
409 :
410 GIC 89048 : r->arg = arg;
411 CBC 89048 : r->resulttype = rtype;
412 GIC 89048 : r->resulttypmod = rtypmod;
413 CBC 89048 : r->resultcollid = rcollid;
414 89048 : r->relabelformat = rformat;
415 89048 : r->location = -1;
416 ECB :
417 CBC 89048 : return r;
418 ECB : }
419 :
420 : /*
421 : * makeRangeVar -
422 : * creates a RangeVar node (rather oversimplified case)
423 : */
424 : RangeVar *
425 GIC 628337 : makeRangeVar(char *schemaname, char *relname, int location)
426 : {
427 628337 : RangeVar *r = makeNode(RangeVar);
428 ECB :
429 GIC 628337 : r->catalogname = NULL;
430 CBC 628337 : r->schemaname = schemaname;
431 GIC 628337 : r->relname = relname;
432 CBC 628337 : r->inh = true;
433 628337 : r->relpersistence = RELPERSISTENCE_PERMANENT;
434 628337 : r->alias = NULL;
435 628337 : r->location = location;
436 ECB :
437 CBC 628337 : return r;
438 ECB : }
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 GIC 420939 : makeTypeName(char *typnam)
448 : {
449 420939 : return makeTypeNameFromNameList(list_make1(makeString(typnam)));
450 ECB : }
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 GIC 580124 : makeTypeNameFromNameList(List *names)
460 : {
461 580124 : TypeName *n = makeNode(TypeName);
462 ECB :
463 GIC 580124 : n->names = names;
464 CBC 580124 : n->typmods = NIL;
465 GIC 580124 : n->typemod = -1;
466 CBC 580124 : n->location = -1;
467 580124 : return n;
468 ECB : }
469 :
470 : /*
471 : * makeTypeNameFromOid -
472 : * build a TypeName node to represent a type already known by OID/typmod.
473 : */
474 : TypeName *
475 GIC 453631 : makeTypeNameFromOid(Oid typeOid, int32 typmod)
476 : {
477 453631 : TypeName *n = makeNode(TypeName);
478 ECB :
479 GIC 453631 : n->typeOid = typeOid;
480 CBC 453631 : n->typemod = typmod;
481 GIC 453631 : n->location = -1;
482 CBC 453631 : return n;
483 ECB : }
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 GIC 441281 : makeColumnDef(const char *colname, Oid typeOid, int32 typmod, Oid collOid)
494 : {
495 441281 : ColumnDef *n = makeNode(ColumnDef);
496 ECB :
497 GIC 441281 : n->colname = pstrdup(colname);
498 CBC 441281 : n->typeName = makeTypeNameFromOid(typeOid, typmod);
499 GIC 441281 : n->inhcount = 0;
500 CBC 441281 : n->is_local = true;
501 441281 : n->is_not_null = false;
502 441281 : n->is_from_type = false;
503 441281 : n->storage = 0;
504 441281 : n->raw_default = NULL;
505 441281 : n->cooked_default = NULL;
506 441281 : n->collClause = NULL;
507 441281 : n->collOid = collOid;
508 441281 : n->constraints = NIL;
509 441281 : n->fdwoptions = NIL;
510 441281 : n->location = -1;
511 ECB :
512 CBC 441281 : return n;
513 ECB : }
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 GIC 110624 : makeFuncExpr(Oid funcid, Oid rettype, List *args,
523 : Oid funccollid, Oid inputcollid, CoercionForm fformat)
524 : {
525 ECB : FuncExpr *funcexpr;
526 :
527 GIC 110624 : funcexpr = makeNode(FuncExpr);
528 110624 : funcexpr->funcid = funcid;
529 110624 : funcexpr->funcresulttype = rettype;
530 CBC 110624 : funcexpr->funcretset = false; /* only allowed case here */
531 110624 : funcexpr->funcvariadic = false; /* only allowed case here */
532 110624 : funcexpr->funcformat = fformat;
533 110624 : funcexpr->funccollid = funccollid;
534 110624 : funcexpr->inputcollid = inputcollid;
535 110624 : funcexpr->args = args;
536 110624 : funcexpr->location = -1;
537 ECB :
538 CBC 110624 : return funcexpr;
539 ECB : }
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 GIC 239954 : makeDefElem(char *name, Node *arg, int location)
550 : {
551 239954 : DefElem *res = makeNode(DefElem);
552 ECB :
553 GIC 239954 : res->defnamespace = NULL;
554 CBC 239954 : res->defname = name;
555 GIC 239954 : res->arg = arg;
556 CBC 239954 : res->defaction = DEFELEM_UNSPEC;
557 239954 : res->location = location;
558 ECB :
559 CBC 239954 : return res;
560 ECB : }
561 :
562 : /*
563 : * makeDefElemExtended -
564 : * build a DefElem node with all fields available to be specified
565 : */
566 : DefElem *
567 GIC 96 : makeDefElemExtended(char *nameSpace, char *name, Node *arg,
568 : DefElemAction defaction, int location)
569 : {
570 CBC 96 : DefElem *res = makeNode(DefElem);
571 :
572 GIC 96 : res->defnamespace = nameSpace;
573 CBC 96 : res->defname = name;
574 GIC 96 : res->arg = arg;
575 CBC 96 : res->defaction = defaction;
576 96 : res->location = location;
577 ECB :
578 CBC 96 : return res;
579 ECB : }
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 GIC 321673 : makeFuncCall(List *name, List *args, CoercionForm funcformat, int location)
589 : {
590 321673 : FuncCall *n = makeNode(FuncCall);
591 ECB :
592 GIC 321673 : n->funcname = name;
593 CBC 321673 : n->args = args;
594 GIC 321673 : n->agg_order = NIL;
595 CBC 321673 : n->agg_filter = NULL;
596 321673 : n->over = NULL;
597 321673 : n->agg_within_group = false;
598 321673 : n->agg_star = false;
599 321673 : n->agg_distinct = false;
600 321673 : n->func_variadic = false;
601 321673 : n->funcformat = funcformat;
602 321673 : n->location = location;
603 321673 : return n;
604 ECB : }
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 GIC 58209 : make_opclause(Oid opno, Oid opresulttype, bool opretset,
614 : Expr *leftop, Expr *rightop,
615 : Oid opcollid, Oid inputcollid)
616 ECB : {
617 GIC 58209 : OpExpr *expr = makeNode(OpExpr);
618 :
619 58209 : expr->opno = opno;
620 CBC 58209 : expr->opfuncid = InvalidOid;
621 GIC 58209 : expr->opresulttype = opresulttype;
622 CBC 58209 : expr->opretset = opretset;
623 58209 : expr->opcollid = opcollid;
624 58209 : expr->inputcollid = inputcollid;
625 58209 : if (rightop)
626 58209 : expr->args = list_make2(leftop, rightop);
627 ECB : else
628 LBC 0 : expr->args = list_make1(leftop);
629 CBC 58209 : expr->location = -1;
630 GIC 58209 : return (Expr *) expr;
631 EUB : }
632 ECB :
633 : /*
634 : * make_andclause
635 : *
636 : * Creates an 'and' clause given a list of its subclauses.
637 : */
638 : Expr *
639 GIC 114482 : make_andclause(List *andclauses)
640 : {
641 114482 : BoolExpr *expr = makeNode(BoolExpr);
642 ECB :
643 GIC 114482 : expr->boolop = AND_EXPR;
644 CBC 114482 : expr->args = andclauses;
645 GIC 114482 : expr->location = -1;
646 CBC 114482 : return (Expr *) expr;
647 ECB : }
648 :
649 : /*
650 : * make_orclause
651 : *
652 : * Creates an 'or' clause given a list of its subclauses.
653 : */
654 : Expr *
655 GIC 14320 : make_orclause(List *orclauses)
656 : {
657 14320 : BoolExpr *expr = makeNode(BoolExpr);
658 ECB :
659 GIC 14320 : expr->boolop = OR_EXPR;
660 CBC 14320 : expr->args = orclauses;
661 GIC 14320 : expr->location = -1;
662 CBC 14320 : return (Expr *) expr;
663 ECB : }
664 :
665 : /*
666 : * make_notclause
667 : *
668 : * Create a 'not' clause given the expression to be negated.
669 : */
670 : Expr *
671 GIC 4051 : make_notclause(Expr *notclause)
672 : {
673 4051 : BoolExpr *expr = makeNode(BoolExpr);
674 ECB :
675 GIC 4051 : expr->boolop = NOT_EXPR;
676 CBC 4051 : expr->args = list_make1(notclause);
677 GIC 4051 : expr->location = -1;
678 CBC 4051 : return (Expr *) expr;
679 ECB : }
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 GIC 1313 : make_and_qual(Node *qual1, Node *qual2)
693 : {
694 1313 : if (qual1 == NULL)
695 CBC 602 : return qual2;
696 GIC 711 : if (qual2 == NULL)
697 LBC 0 : return qual1;
698 CBC 711 : return (Node *) make_andclause(list_make2(qual1, qual2));
699 ECB : }
700 EUB :
701 ECB : /*
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 GIC 22797 : make_ands_explicit(List *andclauses)
712 : {
713 22797 : if (andclauses == NIL)
714 LBC 0 : return (Expr *) makeBoolConst(true, false);
715 GIC 22797 : else if (list_length(andclauses) == 1)
716 CBC 16441 : return (Expr *) linitial(andclauses);
717 EUB : else
718 CBC 6356 : return make_andclause(andclauses);
719 ECB : }
720 :
721 : List *
722 GIC 204441 : make_ands_implicit(Expr *clause)
723 : {
724 : /*
725 ECB : * 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 GIC 204441 : if (clause == NULL)
730 63166 : return NIL; /* NULL -> NIL list == TRUE */
731 141275 : else if (is_andclause(clause))
732 CBC 43134 : return ((BoolExpr *) clause)->args;
733 98141 : else if (IsA(clause, Const) &&
734 2175 : !((Const *) clause)->constisnull &&
735 1053 : DatumGetBool(((Const *) clause)->constvalue))
736 584 : return NIL; /* constant TRUE input -> NIL list */
737 ECB : else
738 CBC 97557 : return list_make1(clause);
739 ECB : }
740 :
741 : /*
742 : * makeIndexInfo
743 : * create an IndexInfo node
744 : */
745 : IndexInfo *
746 GIC 4226653 : 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 CBC 4226653 : IndexInfo *n = makeNode(IndexInfo);
751 :
752 GIC 4226653 : n->ii_NumIndexAttrs = numattrs;
753 4226653 : n->ii_NumIndexKeyAttrs = numkeyattrs;
754 CBC 4226653 : Assert(n->ii_NumIndexKeyAttrs != 0);
755 GIC 4226653 : Assert(n->ii_NumIndexKeyAttrs <= n->ii_NumIndexAttrs);
756 CBC 4226653 : n->ii_Unique = unique;
757 4226653 : n->ii_NullsNotDistinct = nulls_not_distinct;
758 4226653 : n->ii_ReadyForInserts = isready;
759 4226653 : n->ii_CheckedUnchanged = false;
760 4226653 : n->ii_IndexUnchanged = false;
761 4226653 : n->ii_Concurrent = concurrent;
762 GNC 4226653 : n->ii_Summarizing = summarizing;
763 :
764 : /* summarizing indexes cannot contain non-key attributes */
765 4226653 : Assert(!summarizing || (numkeyattrs == numattrs));
766 ECB :
767 : /* expressions */
768 CBC 4226653 : n->ii_Expressions = expressions;
769 4226653 : n->ii_ExpressionsState = NIL;
770 ECB :
771 : /* predicates */
772 GIC 4226653 : n->ii_Predicate = predicates;
773 CBC 4226653 : n->ii_PredicateState = NULL;
774 :
775 : /* exclusion constraints */
776 4226653 : n->ii_ExclusionOps = NULL;
777 4226653 : n->ii_ExclusionProcs = NULL;
778 GIC 4226653 : n->ii_ExclusionStrats = NULL;
779 :
780 ECB : /* opclass options */
781 CBC 4226653 : n->ii_OpclassOptions = NULL;
782 :
783 : /* speculative inserts */
784 4226653 : n->ii_UniqueOps = NULL;
785 4226653 : n->ii_UniqueProcs = NULL;
786 4226653 : n->ii_UniqueStrats = NULL;
787 :
788 : /* initialize index-build state to default */
789 4226653 : n->ii_BrokenHotChain = false;
790 GIC 4226653 : n->ii_ParallelWorkers = 0;
791 :
792 ECB : /* set up for possible use by index AM */
793 CBC 4226653 : n->ii_Am = amoid;
794 4226653 : n->ii_AmCache = NULL;
795 GIC 4226653 : n->ii_Context = CurrentMemoryContext;
796 :
797 CBC 4226653 : return n;
798 ECB : }
799 :
800 : /*
801 : * makeGroupingSet
802 : *
803 : */
804 : GroupingSet *
805 CBC 2381 : makeGroupingSet(GroupingSetKind kind, List *content, int location)
806 : {
807 GIC 2381 : GroupingSet *n = makeNode(GroupingSet);
808 :
809 2381 : n->kind = kind;
810 2381 : n->content = content;
811 2381 : n->location = location;
812 2381 : return n;
813 ECB : }
814 :
815 : /*
816 : * makeVacuumRelation -
817 : * create a VacuumRelation node
818 : */
819 : VacuumRelation *
820 CBC 51635 : makeVacuumRelation(RangeVar *relation, Oid oid, List *va_cols)
821 : {
822 GIC 51635 : VacuumRelation *v = makeNode(VacuumRelation);
823 :
824 51635 : v->relation = relation;
825 51635 : v->oid = oid;
826 51635 : v->va_cols = va_cols;
827 51635 : return v;
828 ECB : }
829 :
830 : /*
831 : * makeJsonFormat -
832 : * creates a JsonFormat node
833 : */
834 : JsonFormat *
835 GNC 1102 : makeJsonFormat(JsonFormatType type, JsonEncoding encoding, int location)
836 : {
837 1102 : JsonFormat *jf = makeNode(JsonFormat);
838 :
839 1102 : jf->format_type = type;
840 1102 : jf->encoding = encoding;
841 1102 : jf->location = location;
842 :
843 1102 : return jf;
844 : }
845 :
846 : /*
847 : * makeJsonValueExpr -
848 : * creates a JsonValueExpr node
849 : */
850 : JsonValueExpr *
851 547 : makeJsonValueExpr(Expr *expr, JsonFormat *format)
852 : {
853 547 : JsonValueExpr *jve = makeNode(JsonValueExpr);
854 :
855 547 : jve->raw_expr = expr;
856 547 : jve->formatted_expr = NULL;
857 547 : jve->format = format;
858 :
859 547 : return jve;
860 : }
861 :
862 : /*
863 : * makeJsonEncoding -
864 : * converts JSON encoding name to enum JsonEncoding
865 : */
866 : JsonEncoding
867 39 : makeJsonEncoding(char *name)
868 : {
869 39 : if (!pg_strcasecmp(name, "utf8"))
870 21 : return JS_ENC_UTF8;
871 18 : if (!pg_strcasecmp(name, "utf16"))
872 6 : return JS_ENC_UTF16;
873 12 : if (!pg_strcasecmp(name, "utf32"))
874 6 : return JS_ENC_UTF32;
875 :
876 6 : ereport(ERROR,
877 : errcode(ERRCODE_INVALID_PARAMETER_VALUE),
878 : errmsg("unrecognized JSON encoding: %s", name));
879 :
880 : return JS_ENC_DEFAULT;
881 : }
882 :
883 : /*
884 : * makeJsonKeyValue -
885 : * creates a JsonKeyValue node
886 : */
887 : Node *
888 313 : makeJsonKeyValue(Node *key, Node *value)
889 : {
890 313 : JsonKeyValue *n = makeNode(JsonKeyValue);
891 :
892 313 : n->key = (Expr *) key;
893 313 : n->value = castNode(JsonValueExpr, value);
894 :
895 313 : return (Node *) n;
896 : }
897 :
898 : /*
899 : * makeJsonIsPredicate -
900 : * creates a JsonIsPredicate node
901 : */
902 : Node *
903 325 : makeJsonIsPredicate(Node *expr, JsonFormat *format, JsonValueType item_type,
904 : bool unique_keys, int location)
905 : {
906 325 : JsonIsPredicate *n = makeNode(JsonIsPredicate);
907 :
908 325 : n->expr = expr;
909 325 : n->format = format;
910 325 : n->item_type = item_type;
911 325 : n->unique_keys = unique_keys;
912 325 : n->location = location;
913 :
914 325 : return (Node *) n;
915 : }
|