Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024, 2025 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : #include "monetdb_config.h"
14 : #include "sql_mem.h"
15 : #include "sql_symbol.h"
16 : #include "sql_parser.h"
17 :
18 : static symbol *
19 12026134 : symbol_init(symbol *s, tokens token, symtype type )
20 : {
21 12026134 : s->token = token;
22 12026134 : s->type = type;
23 12026134 : return s;
24 : }
25 :
26 : symbol *
27 280354 : symbol_create(allocator *sa, tokens token, char *data)
28 : {
29 280354 : symbol *s = SA_NEW(sa, symbol);
30 :
31 280354 : if (s) {
32 280354 : symbol_init(s, token, type_string);
33 280354 : s->data.sval = data;
34 : }
35 280354 : return s;
36 : }
37 :
38 : symbol *
39 8479340 : symbol_create_list(allocator *sa, tokens token, dlist *data)
40 : {
41 8479340 : symbol *s = SA_NEW(sa, symbol);
42 :
43 8479441 : if (s) {
44 8479441 : symbol_init(s, token, type_list);
45 8479441 : s->data.lval = data;
46 : }
47 8479441 : return s;
48 : }
49 :
50 : symbol *
51 6466 : symbol_create_int(allocator *sa, tokens token, int data)
52 : {
53 6466 : symbol *s = SA_NEW(sa, symbol);
54 :
55 6466 : if (s) {
56 6466 : symbol_init(s, token, type_int);
57 6466 : s->data.i_val = data;
58 : }
59 6466 : return s;
60 : }
61 :
62 : symbol *
63 172 : symbol_create_lng(allocator *sa, tokens token, lng data)
64 : {
65 172 : symbol *s = SA_NEW(sa, symbol);
66 :
67 172 : if (s) {
68 172 : symbol_init(s, token, type_lng);
69 172 : s->data.l_val = data;
70 : }
71 172 : return s;
72 : }
73 :
74 : symbol *
75 138539 : symbol_create_symbol(allocator *sa, tokens token, symbol *data)
76 : {
77 138539 : symbol *s = SA_NEW(sa, symbol);
78 :
79 138553 : if (s) {
80 138553 : symbol_init(s, token, type_symbol);
81 138553 : s->data.sym = data;
82 : }
83 138553 : return s;
84 : }
85 :
86 : static dnode *
87 33616180 : dnode_create(allocator *sa )
88 : {
89 33616180 : dnode *n = SA_NEW(sa, dnode);
90 :
91 33617126 : if (n)
92 33617126 : n->next = NULL;
93 33617126 : return n;
94 : }
95 :
96 : static dnode *
97 9966540 : dnode_create_string(allocator *sa, const char *data)
98 : {
99 19933153 : dnode *n = dnode_create(sa);
100 :
101 9966613 : if (n) {
102 9966613 : n->data.sval = (char*)data;
103 3582498 : n->type = type_string;
104 : }
105 9966613 : return n;
106 : }
107 :
108 : static dnode *
109 5617954 : dnode_create_list(allocator *sa, dlist *data)
110 : {
111 11236582 : dnode *n = dnode_create(sa);
112 :
113 5618628 : if (n) {
114 5618628 : n->data.lval = data;
115 5618628 : n->type = type_list;
116 : }
117 5618628 : return n;
118 : }
119 : static dnode *
120 3391055 : dnode_create_int(allocator *sa, int data)
121 : {
122 6782130 : dnode *n = dnode_create(sa);
123 :
124 3391075 : if (n) {
125 3391075 : n->data.i_val = data;
126 3391075 : n->type = type_int;
127 : }
128 3391075 : return n;
129 : }
130 : static dnode *
131 2642 : dnode_create_lng(allocator *sa, lng data)
132 : {
133 5284 : dnode *n = dnode_create(sa);
134 :
135 2642 : if (n) {
136 2642 : n->data.l_val = data;
137 2642 : n->type = type_lng;
138 : }
139 2642 : return n;
140 : }
141 : static dnode *
142 13527106 : dnode_create_symbol(allocator *sa, symbol *data)
143 : {
144 27056497 : dnode *n = dnode_create(sa);
145 :
146 13529391 : if (n) {
147 13529391 : n->data.sym = data;
148 51 : n->type = type_symbol;
149 : }
150 13529391 : return n;
151 : }
152 :
153 : static dnode *
154 1117152 : dnode_create_type(allocator *sa, sql_subtype *data)
155 : {
156 1117152 : dnode *n = dnode_create(sa);
157 :
158 1117152 : if (n) {
159 1117152 : if (data)
160 1117148 : n->data.typeval = *data;
161 : else
162 4 : n->data.typeval.type = NULL;
163 1117152 : n->type = type_type;
164 : }
165 1117152 : return n;
166 : }
167 :
168 : dnode *
169 3582498 : node_string(allocator *sa, const char *s)
170 : {
171 3582498 : return dnode_create_string(sa, s);
172 : }
173 :
174 : dnode *
175 51 : node_symbol(allocator *sa, symbol *s)
176 : {
177 51 : return dnode_create_symbol(sa, s);
178 : }
179 :
180 : dlist *
181 13769714 : dlist_create(allocator *sa)
182 : {
183 13769714 : dlist *l = SA_NEW(sa, dlist);
184 :
185 13769859 : if (l) {
186 13769859 : l->h = l->t = NULL;
187 13769859 : l->cnt = 0;
188 : }
189 13769859 : return l;
190 : }
191 :
192 : int
193 10311709 : dlist_length(dlist *l)
194 : {
195 10311709 : return l->cnt;
196 : }
197 :
198 : static dlist *
199 31834263 : dlist_append_default(dlist *l, dnode *n)
200 : {
201 31834263 : if (l->cnt) {
202 18066562 : l->t->next = n;
203 : } else {
204 13767701 : l->h = n;
205 : }
206 31834263 : l->t = n;
207 31834263 : l->cnt++;
208 31834263 : return l;
209 : }
210 :
211 : dlist *
212 1791311 : append_node(dlist *l, dnode *n)
213 : {
214 1791311 : return dlist_append_default(l, n);
215 : }
216 :
217 : dlist *
218 1791224 : prepend_node(dlist *l, dnode *n)
219 : {
220 1791224 : n->next = l->h;
221 1791224 : l->h = n;
222 1791224 : if (!l->cnt)
223 0 : l->t = n;
224 1791224 : l->cnt++;
225 1791224 : return l;
226 : }
227 :
228 : dlist *
229 6384042 : dlist_append_string(allocator *sa, dlist *l, const char *data)
230 : {
231 6384042 : dnode *n = dnode_create_string(sa, data);
232 :
233 6384115 : if (!n)
234 : return NULL;
235 12768230 : return dlist_append_default(l, n);
236 : }
237 :
238 : dlist *
239 5617954 : dlist_append_list(allocator *sa, dlist *l, dlist *data)
240 : {
241 5617954 : dnode *n = dnode_create_list(sa, data);
242 :
243 5618628 : if (!n)
244 : return NULL;
245 11237256 : return dlist_append_default(l, n);
246 : }
247 :
248 : dlist *
249 3391055 : dlist_append_int(allocator *sa, dlist *l, int data)
250 : {
251 3391055 : dnode *n = dnode_create_int(sa, data);
252 :
253 3391075 : if (!n)
254 : return NULL;
255 6782150 : return dlist_append_default(l, n);
256 : }
257 :
258 : dlist *
259 2642 : dlist_append_lng(allocator *sa, dlist *l, lng data)
260 : {
261 2642 : dnode *n = dnode_create_lng(sa, data);
262 :
263 2642 : if (!n)
264 : return NULL;
265 5284 : return dlist_append_default(l, n);
266 : }
267 :
268 : dlist *
269 13527055 : dlist_append_symbol(allocator *sa, dlist *l, symbol *data)
270 : {
271 13527055 : dnode *n = dnode_create_symbol(sa, data);
272 :
273 13529340 : if (!n)
274 : return NULL;
275 27058680 : return dlist_append_default(l, n);
276 : }
277 :
278 : dlist *
279 1117152 : dlist_append_type(allocator *sa, dlist *l, sql_subtype *data)
280 : {
281 1117152 : dnode *n = dnode_create_type(sa, data);
282 :
283 1117152 : if (!n)
284 : return NULL;
285 2234304 : return dlist_append_default(l, n);
286 : }
287 :
288 : symbol *
289 382815 : newSelectNode(allocator *sa, int distinct, struct dlist *selection, struct dlist *into, symbol *from, symbol *where, symbol *groupby, symbol *having, symbol *orderby, symbol *name, symbol *limit, symbol *offset, symbol *sample, symbol *seed, symbol *window, symbol *qualify)
290 : {
291 382815 : SelectNode *sn = SA_NEW(sa, SelectNode);
292 382823 : symbol *s = (symbol *) sn;
293 :
294 382823 : if (s) {
295 382823 : symbol_init(s, SQL_SELECT, type_symbol);
296 382823 : sn->distinct = distinct;
297 382823 : sn->lateral = 0;
298 382823 : sn->limit = limit;
299 382823 : sn->offset = offset;
300 382823 : sn->sample = sample;
301 382823 : sn->seed = seed;
302 382823 : sn->selection = selection;
303 382823 : sn->into = into;
304 382823 : sn->from = from;
305 382823 : sn->where = where;
306 382823 : sn->groupby = groupby;
307 382823 : sn->having = having;
308 382823 : sn->orderby = orderby;
309 382823 : sn->name = name;
310 382823 : sn->window = window;
311 382823 : sn->qualify = qualify;
312 : }
313 382823 : return s;
314 : }
315 :
316 : symbol *
317 2738488 : newAtomNode(allocator *sa, atom *data)
318 : {
319 2738488 : AtomNode *an = SA_NEW(sa, AtomNode);
320 2738325 : symbol *s = (symbol *) an;
321 :
322 2738325 : if (s) {
323 2738325 : symbol_init(s, SQL_ATOM, type_symbol);
324 2738325 : an->a = data;
325 : }
326 2738325 : return s;
327 : }
|