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 11992501 : symbol_init(symbol *s, tokens token, symtype type )
20 : {
21 11992501 : s->token = token;
22 11992501 : s->type = type;
23 11992501 : return s;
24 : }
25 :
26 : symbol *
27 279419 : symbol_create(allocator *sa, tokens token, char *data)
28 : {
29 279419 : symbol *s = SA_NEW(sa, symbol);
30 :
31 279419 : if (s) {
32 279419 : symbol_init(s, token, type_string);
33 279419 : s->data.sval = data;
34 : }
35 279419 : return s;
36 : }
37 :
38 : symbol *
39 8453189 : symbol_create_list(allocator *sa, tokens token, dlist *data)
40 : {
41 8453189 : symbol *s = SA_NEW(sa, symbol);
42 :
43 8453321 : if (s) {
44 8453321 : symbol_init(s, token, type_list);
45 8453321 : s->data.lval = data;
46 : }
47 8453321 : return s;
48 : }
49 :
50 : symbol *
51 6456 : symbol_create_int(allocator *sa, tokens token, int data)
52 : {
53 6456 : symbol *s = SA_NEW(sa, symbol);
54 :
55 6456 : if (s) {
56 6456 : symbol_init(s, token, type_int);
57 6456 : s->data.i_val = data;
58 : }
59 6456 : 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 138202 : symbol_create_symbol(allocator *sa, tokens token, symbol *data)
76 : {
77 138202 : symbol *s = SA_NEW(sa, symbol);
78 :
79 138216 : if (s) {
80 138216 : symbol_init(s, token, type_symbol);
81 138216 : s->data.sym = data;
82 : }
83 138216 : return s;
84 : }
85 :
86 : static dnode *
87 33513180 : dnode_create(allocator *sa )
88 : {
89 33513180 : dnode *n = SA_NEW(sa, dnode);
90 :
91 33514822 : if (n)
92 33514822 : n->next = NULL;
93 33514822 : return n;
94 : }
95 :
96 : static dnode *
97 9934333 : dnode_create_string(allocator *sa, const char *data)
98 : {
99 19868742 : dnode *n = dnode_create(sa);
100 :
101 9934409 : if (n) {
102 9934409 : n->data.sval = (char*)data;
103 3569910 : n->type = type_string;
104 : }
105 9934409 : return n;
106 : }
107 :
108 : static dnode *
109 5598495 : dnode_create_list(allocator *sa, dlist *data)
110 : {
111 11197739 : dnode *n = dnode_create(sa);
112 :
113 5599244 : if (n) {
114 5599244 : n->data.lval = data;
115 5599244 : n->type = type_list;
116 : }
117 5599244 : return n;
118 : }
119 : static dnode *
120 3379325 : dnode_create_int(allocator *sa, int data)
121 : {
122 6758695 : dnode *n = dnode_create(sa);
123 :
124 3379370 : if (n) {
125 3379370 : n->data.i_val = data;
126 3379370 : n->type = type_int;
127 : }
128 3379370 : return n;
129 : }
130 : static dnode *
131 2639 : dnode_create_lng(allocator *sa, lng data)
132 : {
133 5278 : dnode *n = dnode_create(sa);
134 :
135 2639 : if (n) {
136 2639 : n->data.l_val = data;
137 2639 : n->type = type_lng;
138 : }
139 2639 : return n;
140 : }
141 : static dnode *
142 13492218 : dnode_create_symbol(allocator *sa, symbol *data)
143 : {
144 26986948 : dnode *n = dnode_create(sa);
145 :
146 13494730 : if (n) {
147 13494730 : n->data.sym = data;
148 51 : n->type = type_symbol;
149 : }
150 13494730 : return n;
151 : }
152 :
153 : static dnode *
154 1112547 : dnode_create_type(allocator *sa, sql_subtype *data)
155 : {
156 1112547 : dnode *n = dnode_create(sa);
157 :
158 1112547 : if (n) {
159 1112547 : if (data)
160 1112543 : n->data.typeval = *data;
161 : else
162 4 : n->data.typeval.type = NULL;
163 1112547 : n->type = type_type;
164 : }
165 1112547 : return n;
166 : }
167 :
168 : dnode *
169 3569910 : node_string(allocator *sa, const char *s)
170 : {
171 3569910 : 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 13727455 : dlist_create(allocator *sa)
182 : {
183 13727455 : dlist *l = SA_NEW(sa, dlist);
184 :
185 13727657 : if (l) {
186 13727657 : l->h = l->t = NULL;
187 13727657 : l->cnt = 0;
188 : }
189 13727657 : return l;
190 : }
191 :
192 : int
193 10278526 : dlist_length(dlist *l)
194 : {
195 10278526 : return l->cnt;
196 : }
197 :
198 : static dlist *
199 31737981 : dlist_append_default(dlist *l, dnode *n)
200 : {
201 31737981 : if (l->cnt) {
202 18012537 : l->t->next = n;
203 : } else {
204 13725444 : l->h = n;
205 : }
206 31737981 : l->t = n;
207 31737981 : l->cnt++;
208 31737981 : return l;
209 : }
210 :
211 : dlist *
212 1785003 : append_node(dlist *l, dnode *n)
213 : {
214 1785003 : return dlist_append_default(l, n);
215 : }
216 :
217 : dlist *
218 1784944 : prepend_node(dlist *l, dnode *n)
219 : {
220 1784944 : n->next = l->h;
221 1784944 : l->h = n;
222 1784944 : if (!l->cnt)
223 0 : l->t = n;
224 1784944 : l->cnt++;
225 1784944 : return l;
226 : }
227 :
228 : dlist *
229 6364423 : dlist_append_string(allocator *sa, dlist *l, const char *data)
230 : {
231 6364423 : dnode *n = dnode_create_string(sa, data);
232 :
233 6364499 : if (!n)
234 : return NULL;
235 12728998 : return dlist_append_default(l, n);
236 : }
237 :
238 : dlist *
239 5598495 : dlist_append_list(allocator *sa, dlist *l, dlist *data)
240 : {
241 5598495 : dnode *n = dnode_create_list(sa, data);
242 :
243 5599244 : if (!n)
244 : return NULL;
245 11198488 : return dlist_append_default(l, n);
246 : }
247 :
248 : dlist *
249 3379325 : dlist_append_int(allocator *sa, dlist *l, int data)
250 : {
251 3379325 : dnode *n = dnode_create_int(sa, data);
252 :
253 3379370 : if (!n)
254 : return NULL;
255 6758740 : return dlist_append_default(l, n);
256 : }
257 :
258 : dlist *
259 2639 : dlist_append_lng(allocator *sa, dlist *l, lng data)
260 : {
261 2639 : dnode *n = dnode_create_lng(sa, data);
262 :
263 2639 : if (!n)
264 : return NULL;
265 5278 : return dlist_append_default(l, n);
266 : }
267 :
268 : dlist *
269 13492167 : dlist_append_symbol(allocator *sa, dlist *l, symbol *data)
270 : {
271 13492167 : dnode *n = dnode_create_symbol(sa, data);
272 :
273 13494679 : if (!n)
274 : return NULL;
275 26989358 : return dlist_append_default(l, n);
276 : }
277 :
278 : dlist *
279 1112547 : dlist_append_type(allocator *sa, dlist *l, sql_subtype *data)
280 : {
281 1112547 : dnode *n = dnode_create_type(sa, data);
282 :
283 1112547 : if (!n)
284 : return NULL;
285 2225094 : return dlist_append_default(l, n);
286 : }
287 :
288 : symbol *
289 382276 : 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 382276 : SelectNode *sn = SA_NEW(sa, SelectNode);
292 382288 : symbol *s = (symbol *) sn;
293 :
294 382288 : if (s) {
295 382288 : symbol_init(s, SQL_SELECT, type_symbol);
296 382288 : sn->distinct = distinct;
297 382288 : sn->lateral = 0;
298 382288 : sn->limit = limit;
299 382288 : sn->offset = offset;
300 382288 : sn->sample = sample;
301 382288 : sn->seed = seed;
302 382288 : sn->selection = selection;
303 382288 : sn->into = into;
304 382288 : sn->from = from;
305 382288 : sn->where = where;
306 382288 : sn->groupby = groupby;
307 382288 : sn->having = having;
308 382288 : sn->orderby = orderby;
309 382288 : sn->name = name;
310 382288 : sn->window = window;
311 382288 : sn->qualify = qualify;
312 : }
313 382288 : return s;
314 : }
315 :
316 : symbol *
317 2732998 : newAtomNode(allocator *sa, atom *data)
318 : {
319 2732998 : AtomNode *an = SA_NEW(sa, AtomNode);
320 2732629 : symbol *s = (symbol *) an;
321 :
322 2732629 : if (s) {
323 2732629 : symbol_init(s, SQL_ATOM, type_symbol);
324 2732629 : an->a = data;
325 : }
326 2732629 : return s;
327 : }
|