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