1/*--------------------------------------------------------------------
2 * Symbols referenced in this file:
3 * - makeDefElem
4 * - makeTypeNameFromNameList
5 * - makeDefElemExtended
6 * - makeAlias
7 * - makeSimpleAExpr
8 * - makeGroupingSet
9 * - makeTypeName
10 * - makeFuncCall
11 * - makeAExpr
12 * - makeRangeVar
13 * - makeBoolExpr
14 *--------------------------------------------------------------------
15 */
16
17/*-------------------------------------------------------------------------
18 *
19 * makefuncs.c
20 * creator functions for primitive nodes. The functions here are for
21 * the most frequently created nodes.
22 *
23 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development PGGroup
24 * Portions Copyright (c) 1994, Regents of the University of California
25 *
26 *
27 * IDENTIFICATION
28 * src/backend/nodes/makefuncs.c
29 *
30 *-------------------------------------------------------------------------
31 */
32#include "pg_functions.hpp"
33
34
35#include "fmgr.hpp"
36#include "nodes/makefuncs.hpp"
37#include "nodes/nodeFuncs.hpp"
38//#include "utils/lsyscache.hpp"
39
40
41/*
42 * makeAExpr -
43 * makes an PGAExpr node
44 */
45PGAExpr *
46makeAExpr(PGAExpr_Kind kind, PGList *name,
47 PGNode *lexpr, PGNode *rexpr, int location)
48{
49 PGAExpr *a = makeNode(PGAExpr);
50
51 a->kind = kind;
52 a->name = name;
53 a->lexpr = lexpr;
54 a->rexpr = rexpr;
55 a->location = location;
56 return a;
57}
58
59/*
60 * makeSimpleAExpr -
61 * As above, given a simple (unqualified) operator name
62 */
63PGAExpr *
64makeSimpleAExpr(PGAExpr_Kind kind, const char *name,
65 PGNode *lexpr, PGNode *rexpr, int location)
66{
67 PGAExpr *a = makeNode(PGAExpr);
68
69 a->kind = kind;
70 a->name = list_make1(makeString((char *) name));
71 a->lexpr = lexpr;
72 a->rexpr = rexpr;
73 a->location = location;
74 return a;
75}
76
77/*
78 * makeVar -
79 * creates a PGVar node
80 */
81
82
83/*
84 * makeVarFromTargetEntry -
85 * convenience function to create a same-level PGVar node from a
86 * PGTargetEntry
87 */
88
89
90/*
91 * makeWholeRowVar -
92 * creates a PGVar node representing a whole row of the specified RTE
93 *
94 * A whole-row reference is a PGVar with varno set to the correct range
95 * table entry, and varattno == 0 to signal that it references the whole
96 * tuple. (Use of zero here is unclean, since it could easily be confused
97 * with error cases, but it's not worth changing now.) The vartype indicates
98 * a rowtype; either a named composite type, or RECORD. This function
99 * encapsulates the logic for determining the correct rowtype OID to use.
100 *
101 * If allowScalar is true, then for the case where the RTE is a single function
102 * returning a non-composite result type, we produce a normal PGVar referencing
103 * the function's result directly, instead of the single-column composite
104 * value that the whole-row notation might otherwise suggest.
105 */
106
107
108/*
109 * makeTargetEntry -
110 * creates a PGTargetEntry node
111 */
112
113
114/*
115 * flatCopyTargetEntry -
116 * duplicate a PGTargetEntry, but don't copy substructure
117 *
118 * This is commonly used when we just want to modify the resno or substitute
119 * a new expression.
120 */
121
122
123/*
124 * makeFromExpr -
125 * creates a PGFromExpr node
126 */
127
128
129/*
130 * makeConst -
131 * creates a PGConst node
132 */
133
134
135/*
136 * makeNullConst -
137 * creates a PGConst node representing a NULL of the specified type/typmod
138 *
139 * This is a convenience routine that just saves a lookup of the type's
140 * storage properties.
141 */
142
143
144/*
145 * makeBoolConst -
146 * creates a PGConst node representing a boolean value (can be NULL too)
147 */
148
149
150/*
151 * makeBoolExpr -
152 * creates a PGBoolExpr node
153 */
154PGExpr *
155makeBoolExpr(PGBoolExprType boolop, PGList *args, int location)
156{
157 PGBoolExpr *b = makeNode(PGBoolExpr);
158
159 b->boolop = boolop;
160 b->args = args;
161 b->location = location;
162
163 return (PGExpr *) b;
164}
165
166/*
167 * makeAlias -
168 * creates an PGAlias node
169 *
170 * NOTE: the given name is copied, but the colnames list (if any) isn't.
171 */
172PGAlias *
173makeAlias(const char *aliasname, PGList *colnames)
174{
175 PGAlias *a = makeNode(PGAlias);
176
177 a->aliasname = pstrdup(aliasname);
178 a->colnames = colnames;
179
180 return a;
181}
182
183/*
184 * makeRelabelType -
185 * creates a PGRelabelType node
186 */
187
188
189/*
190 * makeRangeVar -
191 * creates a PGRangeVar node (rather oversimplified case)
192 */
193PGRangeVar *
194makeRangeVar(char *schemaname, char *relname, int location)
195{
196 PGRangeVar *r = makeNode(PGRangeVar);
197
198 r->catalogname = NULL;
199 r->schemaname = schemaname;
200 r->relname = relname;
201 r->inh = true;
202 r->relpersistence = RELPERSISTENCE_PERMANENT;
203 r->alias = NULL;
204 r->location = location;
205
206 return r;
207}
208
209/*
210 * makeTypeName -
211 * build a PGTypeName node for an unqualified name.
212 *
213 * typmod is defaulted, but can be changed later by caller.
214 */
215PGTypeName *
216makeTypeName(char *typnam)
217{
218 return makeTypeNameFromNameList(list_make1(makeString(typnam)));
219}
220
221/*
222 * makeTypeNameFromNameList -
223 * build a PGTypeName node for a String list representing a qualified name.
224 *
225 * typmod is defaulted, but can be changed later by caller.
226 */
227PGTypeName *
228makeTypeNameFromNameList(PGList *names)
229{
230 PGTypeName *n = makeNode(PGTypeName);
231
232 n->names = names;
233 n->typmods = NIL;
234 n->typemod = -1;
235 n->location = -1;
236 return n;
237}
238
239/*
240 * makeTypeNameFromOid -
241 * build a PGTypeName node to represent a type already known by OID/typmod.
242 */
243
244
245/*
246 * makeColumnDef -
247 * build a PGColumnDef node to represent a simple column definition.
248 *
249 * Type and collation are specified by OID.
250 * Other properties are all basic to start with.
251 */
252
253
254/*
255 * makeFuncExpr -
256 * build an expression tree representing a function call.
257 *
258 * The argument expressions must have been transformed already.
259 */
260
261
262/*
263 * makeDefElem -
264 * build a PGDefElem node
265 *
266 * This is sufficient for the "typical" case with an unqualified option name
267 * and no special action.
268 */
269PGDefElem *
270makeDefElem(const char *name, PGNode *arg, int location)
271{
272 PGDefElem *res = makeNode(PGDefElem);
273
274 res->defnamespace = NULL;
275 res->defname = (char*) name;
276 res->arg = arg;
277 res->defaction = PG_DEFELEM_UNSPEC;
278 res->location = location;
279
280 return res;
281}
282
283/*
284 * makeDefElemExtended -
285 * build a PGDefElem node with all fields available to be specified
286 */
287PGDefElem *
288makeDefElemExtended(const char *nameSpace, const char *name, PGNode *arg,
289 PGDefElemAction defaction, int location)
290{
291 PGDefElem *res = makeNode(PGDefElem);
292
293 res->defnamespace = (char*) nameSpace;
294 res->defname = (char*) name;
295 res->arg = arg;
296 res->defaction = defaction;
297 res->location = location;
298
299 return res;
300}
301
302/*
303 * makeFuncCall -
304 *
305 * Initialize a PGFuncCall struct with the information every caller must
306 * supply. Any non-default parameters have to be inserted by the caller.
307 */
308PGFuncCall *
309makeFuncCall(PGList *name, PGList *args, int location)
310{
311 PGFuncCall *n = makeNode(PGFuncCall);
312
313 n->funcname = name;
314 n->args = args;
315 n->agg_order = NIL;
316 n->agg_filter = NULL;
317 n->agg_within_group = false;
318 n->agg_star = false;
319 n->agg_distinct = false;
320 n->func_variadic = false;
321 n->over = NULL;
322 n->location = location;
323 return n;
324}
325
326/*
327 * makeGroupingSet
328 *
329 */
330PGGroupingSet *
331makeGroupingSet(GroupingSetKind kind, PGList *content, int location)
332{
333 PGGroupingSet *n = makeNode(PGGroupingSet);
334
335 n->kind = kind;
336 n->content = content;
337 n->location = location;
338 return n;
339}
340