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 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_relation.h"
15 : #include "sql_semantic.h"
16 : #include "sql_decimal.h"
17 : #include "rel_exp.h"
18 : #include "rel_rel.h"
19 : #include "rel_basetable.h"
20 : #include "rel_prop.h"
21 :
22 : comp_type
23 830642 : compare_str2type(const char *compare_op)
24 : {
25 830642 : comp_type type = cmp_filter;
26 :
27 830642 : if (compare_op[0] == '=') {
28 : type = cmp_equal;
29 162616 : } else if (compare_op[0] == '<') {
30 104557 : type = cmp_lt;
31 104557 : if (compare_op[1] == '>')
32 : type = cmp_notequal;
33 23354 : else if (compare_op[1] == '=')
34 4183 : type = cmp_lte;
35 58059 : } else if (compare_op[0] == '>') {
36 58059 : type = cmp_gt;
37 58059 : if (compare_op[1] == '=')
38 3318 : type = cmp_gte;
39 : }
40 830642 : return type;
41 : }
42 :
43 : comp_type
44 51646 : swap_compare( comp_type t )
45 : {
46 51646 : switch(t) {
47 : case cmp_equal:
48 : return cmp_equal;
49 : case cmp_lt:
50 : return cmp_gt;
51 : case cmp_lte:
52 : return cmp_gte;
53 : case cmp_gte:
54 : return cmp_lte;
55 : case cmp_gt:
56 : return cmp_lt;
57 : case cmp_notequal:
58 : return cmp_notequal;
59 : default:
60 : return cmp_equal;
61 : }
62 : }
63 :
64 : comp_type
65 7654 : negate_compare( comp_type t )
66 : {
67 7654 : switch(t) {
68 : case cmp_equal:
69 : return cmp_notequal;
70 24 : case cmp_notequal:
71 24 : return cmp_equal;
72 2 : case cmp_lt:
73 2 : return cmp_gte;
74 5 : case cmp_lte:
75 5 : return cmp_gt;
76 2 : case cmp_gte:
77 2 : return cmp_lt;
78 3 : case cmp_gt:
79 3 : return cmp_lte;
80 :
81 0 : case cmp_in:
82 0 : return cmp_notin;
83 0 : case cmp_notin:
84 0 : return cmp_in;
85 :
86 0 : default:
87 0 : return t;
88 : }
89 : }
90 :
91 : comp_type
92 3155 : range2lcompare( int r )
93 : {
94 3155 : if (r&1) {
95 : return cmp_gte;
96 : } else {
97 1169 : return cmp_gt;
98 : }
99 : }
100 :
101 : comp_type
102 3212 : range2rcompare( int r )
103 : {
104 3212 : if (r&2) {
105 : return cmp_lte;
106 : } else {
107 1206 : return cmp_lt;
108 : }
109 : }
110 :
111 : int
112 1903 : compare2range( int l, int r )
113 : {
114 1903 : if (l == cmp_gt) {
115 1780 : if (r == cmp_lt)
116 : return 0;
117 23 : else if (r == cmp_lte)
118 23 : return 2;
119 123 : } else if (l == cmp_gte) {
120 123 : if (r == cmp_lt)
121 : return 1;
122 85 : else if (r == cmp_lte)
123 85 : return 3;
124 : }
125 : return -1;
126 : }
127 :
128 : int
129 108 : compare_funcs2range(const char *l_op, const char *r_op)
130 : {
131 108 : assert(l_op[0] == '>' && r_op[0] == '<');
132 108 : if (!l_op[1] && !r_op[1])
133 : return 0;
134 105 : if (!l_op[1] && r_op[1] == '=')
135 : return 2;
136 105 : if (l_op[1] == '=' && !r_op[1])
137 : return 1;
138 13 : if (l_op[1] == '=' && r_op[1] == '=')
139 : return 3;
140 0 : assert(0);
141 : return 0;
142 : }
143 :
144 : static sql_exp *
145 20718068 : exp_create(allocator *sa, int type)
146 : {
147 20718068 : sql_exp *e = SA_NEW(sa, sql_exp);
148 :
149 20717952 : if (!e)
150 : return NULL;
151 20717952 : *e = (sql_exp) {
152 20717952 : .type = (expression_type) type,
153 : };
154 20717952 : return e;
155 : }
156 :
157 : sql_exp *
158 469576 : exp_compare(allocator *sa, sql_exp *l, sql_exp *r, int cmptype)
159 : {
160 469576 : sql_exp *e = exp_create(sa, e_cmp);
161 469576 : if (e == NULL)
162 : return NULL;
163 469576 : e->card = MAX(l->card,r->card);
164 469576 : e->l = l;
165 469576 : e->r = r;
166 469576 : e->flag = cmptype;
167 469576 : if (!has_nil(l) && !has_nil(r))
168 38010 : set_has_no_nil(e);
169 : return e;
170 : }
171 :
172 : sql_exp *
173 6593 : exp_compare2(allocator *sa, sql_exp *l, sql_exp *r, sql_exp *f, int cmptype, int symmetric)
174 : {
175 6593 : sql_exp *e = exp_create(sa, e_cmp);
176 6593 : if (e == NULL)
177 : return NULL;
178 6593 : assert(f);
179 6593 : e->card = MAX(MAX(l->card,r->card),f->card);
180 6593 : e->l = l;
181 6593 : e->r = r;
182 6593 : e->f = f;
183 6593 : e->flag = cmptype;
184 6593 : if (symmetric)
185 77 : set_symmetric(e);
186 6593 : if (!has_nil(l) && !has_nil(r) && !has_nil(f))
187 525 : set_has_no_nil(e);
188 : return e;
189 : }
190 :
191 : sql_exp *
192 6804 : exp_filter(allocator *sa, list *l, list *r, sql_subfunc *f, int anti)
193 : {
194 6804 : sql_exp *e = exp_create(sa, e_cmp);
195 :
196 6804 : if (e == NULL)
197 : return NULL;
198 6804 : e->card = MAX(exps_card(l),exps_card(r));
199 6804 : if (!r) { /* split l */
200 1410 : list *nl = sa_list(sa), *nr = sa_list(sa);
201 1410 : node *n = l->h;
202 1410 : append(nl, n->data); /* sofar only first is left */
203 3629 : for(n = n->next; n; n = n->next)
204 2219 : append(nr, n->data);
205 : l = nl;
206 : r = nr;
207 : }
208 6804 : e->l = l;
209 6804 : e->r = r;
210 6804 : e->f = f;
211 6804 : e->flag = cmp_filter;
212 6804 : if (anti)
213 766 : set_anti(e);
214 6804 : if (!have_nil(l) && !have_nil(r))
215 3261 : set_has_no_nil(e);
216 : return e;
217 : }
218 :
219 : sql_exp *
220 65026 : exp_or(allocator *sa, list *l, list *r, int anti)
221 : {
222 65026 : sql_exp *e = exp_create(sa, e_cmp);
223 :
224 65026 : if (e == NULL)
225 : return NULL;
226 65026 : e->card = MAX(exps_card(l),exps_card(r));
227 65026 : e->l = l;
228 65026 : e->r = r;
229 65026 : e->flag = cmp_or;
230 65026 : if (anti)
231 0 : set_anti(e);
232 65026 : if (!have_nil(l) && !have_nil(r))
233 2246 : set_has_no_nil(e);
234 : return e;
235 : }
236 :
237 : sql_exp *
238 28466 : exp_in(allocator *sa, sql_exp *l, list *r, int cmptype)
239 : {
240 28466 : sql_exp *e = exp_create(sa, e_cmp);
241 28466 : unsigned int exps_card = CARD_ATOM;
242 :
243 28466 : if (e == NULL)
244 : return NULL;
245 :
246 : /* ignore the cardinalites of sub-relations */
247 142363 : for (node *n = r->h; n ; n = n->next) {
248 113897 : sql_exp *next = n->data;
249 :
250 113897 : if (!exp_is_rel(next) && exps_card < next->card)
251 113897 : exps_card = next->card;
252 : }
253 28466 : e->card = MAX(l->card, exps_card);
254 28466 : e->l = l;
255 28466 : e->r = r;
256 28466 : assert( cmptype == cmp_in || cmptype == cmp_notin);
257 28466 : e->flag = cmptype;
258 28466 : if (!has_nil(l) && !have_nil(r))
259 1786 : set_has_no_nil(e);
260 : return e;
261 : }
262 :
263 : sql_exp *
264 37406 : exp_in_func(mvc *sql, sql_exp *le, sql_exp *vals, int anyequal, int is_tuple)
265 : {
266 37406 : sql_subfunc *a_func = NULL;
267 37406 : sql_exp *e = le;
268 :
269 37406 : if (is_tuple) {
270 5558 : list *l = exp_get_values(e);
271 5558 : e = l->h->data;
272 : }
273 44467 : if (!(a_func = sql_bind_func(sql, "sys", anyequal ? "sql_anyequal" : "sql_not_anyequal", exp_subtype(e), exp_subtype(e), F_FUNC, true, true)))
274 0 : return sql_error(sql, 02, SQLSTATE(42000) "(NOT) IN operator on type %s missing", exp_subtype(e) ? exp_subtype(e)->type->base.name : "unknown");
275 37406 : e = exp_binop(sql->sa, le, vals, a_func);
276 37406 : if (e) {
277 37406 : unsigned int exps_card = CARD_ATOM;
278 :
279 : /* ignore the cardinalites of sub-relations */
280 37406 : if (vals->type == e_atom && vals->f) {
281 165257 : for (node *n = ((list*)vals->f)->h ; n ; n = n->next) {
282 130770 : sql_exp *next = n->data;
283 :
284 130770 : if (!exp_is_rel(next) && exps_card < next->card)
285 130770 : exps_card = next->card;
286 : }
287 2919 : } else if (!exp_is_rel(vals))
288 2919 : exps_card = vals->card;
289 :
290 37406 : e->card = MAX(le->card, exps_card);
291 37406 : if (!has_nil(le) && !has_nil(vals))
292 608 : set_has_no_nil(e);
293 : }
294 : return e;
295 : }
296 :
297 : sql_exp *
298 5538 : exp_in_aggr(mvc *sql, sql_exp *le, sql_exp *vals, int anyequal, int is_tuple)
299 : {
300 5538 : sql_subfunc *a_func = NULL;
301 5538 : sql_exp *e = le;
302 :
303 5538 : if (is_tuple) {
304 0 : list *l = exp_get_values(e);
305 0 : e = l->h->data;
306 : }
307 5547 : if (!(a_func = sql_bind_func(sql, "sys", anyequal ? "anyequal" : "allnotequal", exp_subtype(e), exp_subtype(e), F_AGGR, true, true)))
308 0 : return sql_error(sql, 02, SQLSTATE(42000) "(NOT) IN operator on type %s missing", exp_subtype(e) ? exp_subtype(e)->type->base.name : "unknown");
309 5538 : e = exp_aggr2(sql->sa, le, vals, a_func, need_distinct(e), need_no_nil(e), e->card, has_nil(e));
310 5538 : if (e) {
311 5538 : unsigned int exps_card = CARD_ATOM;
312 :
313 : /* ignore the cardinalites of sub-relations */
314 5538 : if (vals->type == e_atom && vals->f) {
315 31336 : for (node *n = ((list*)vals->f)->h ; n ; n = n->next) {
316 25798 : sql_exp *next = n->data;
317 :
318 25798 : if (!exp_is_rel(next) && exps_card < next->card)
319 25798 : exps_card = next->card;
320 : }
321 0 : } else if (!exp_is_rel(vals))
322 0 : exps_card = vals->card;
323 :
324 5538 : e->card = MAX(le->card, exps_card);
325 5538 : if (!has_nil(le) && !has_nil(vals))
326 0 : set_has_no_nil(e);
327 : }
328 : return e;
329 : }
330 :
331 : sql_exp *
332 122509 : exp_compare_func(mvc *sql, sql_exp *le, sql_exp *re, const char *compareop, int quantifier)
333 : {
334 122509 : sql_subfunc *cmp_func = sql_bind_func(sql, "sys", compareop, exp_subtype(le), exp_subtype(le), F_FUNC, true, true);
335 122509 : sql_exp *e = NULL;
336 :
337 122509 : if (cmp_func == NULL)
338 : return NULL;
339 :
340 122509 : e = exp_binop(sql->sa, le, re, cmp_func);
341 122509 : if (e) {
342 122509 : e->flag = quantifier;
343 : /* At ANY and ALL operators, the cardinality on the right side is ignored if it is a sub-relation */
344 122509 : e->card = quantifier && exp_is_rel(re) ? le->card : MAX(le->card, re->card);
345 122509 : if (!has_nil(le) && !has_nil(re))
346 28776 : set_has_no_nil(e);
347 : }
348 : return e;
349 : }
350 :
351 : static sql_subtype*
352 749114 : dup_subtype(allocator *sa, sql_subtype *st)
353 : {
354 749114 : sql_subtype *res = SA_NEW(sa, sql_subtype);
355 :
356 749114 : if (res == NULL)
357 : return NULL;
358 749114 : *res = *st;
359 749114 : return res;
360 : }
361 :
362 : sql_exp *
363 374557 : exp_convert(allocator *sa, sql_exp *exp, sql_subtype *fromtype, sql_subtype *totype )
364 : {
365 374557 : sql_exp *e = exp_create(sa, e_convert);
366 374557 : if (e == NULL)
367 : return NULL;
368 374557 : e->card = exp->card;
369 374557 : e->l = exp;
370 374557 : totype = dup_subtype(sa, totype);
371 374557 : e->r = append(append(sa_list(sa), dup_subtype(sa, fromtype)),totype);
372 374557 : e->tpe = *totype;
373 374557 : e->alias = exp->alias;
374 374557 : if (!has_nil(exp))
375 99809 : set_has_no_nil(e);
376 : return e;
377 : }
378 :
379 : sql_exp *
380 1103579 : exp_op( allocator *sa, list *l, sql_subfunc *f )
381 : {
382 1103579 : if (f->func->type == F_FILT)
383 1410 : return exp_filter(sa, l, NULL, f, false);
384 1102169 : sql_exp *e = exp_create(sa, e_func);
385 1102169 : if (e == NULL)
386 : return NULL;
387 1102169 : e->card = exps_card(l);
388 1102169 : e->l = l;
389 1102169 : e->f = f;
390 1102169 : e->semantics = f->func->semantics;
391 1102169 : if (!is_semantics(e) && !is_any(e) && l && !have_nil(l))
392 85498 : set_has_no_nil(e);
393 : return e;
394 : }
395 :
396 : sql_exp *
397 18876 : exp_rank_op( allocator *sa, list *l, list *gbe, list *obe, sql_subfunc *f )
398 : {
399 18876 : sql_exp *e = exp_create(sa, e_func);
400 18876 : if (e == NULL)
401 : return NULL;
402 18876 : e->card = list_empty(l)?CARD_MULTI:exps_card(l);
403 18876 : e->l = l;
404 18876 : e->r = append(append(sa_list(sa), gbe), obe);
405 18876 : e->f = f;
406 18876 : if (!f->func->s && strcmp(f->func->base.name, "count") == 0)
407 179 : set_has_no_nil(e);
408 18876 : e->semantics = f->func->semantics;
409 18876 : return e;
410 : }
411 :
412 : sql_exp *
413 60847 : exp_aggr( allocator *sa, list *l, sql_subfunc *a, int distinct, int no_nils, unsigned int card, int has_nils )
414 : {
415 60847 : sql_exp *e = exp_create(sa, e_aggr);
416 60847 : if (e == NULL)
417 : return NULL;
418 60847 : e->card = card;
419 60847 : e->l = l;
420 60847 : e->f = a;
421 60847 : e->semantics = a->func->semantics;
422 60847 : if (distinct)
423 373 : set_distinct(e);
424 60847 : if (no_nils)
425 25298 : set_no_nil(e);
426 60847 : if ((!a->func->semantics && !has_nils) || (!a->func->s && strcmp(a->func->base.name, "count") == 0))
427 23801 : set_has_no_nil(e);
428 : return e;
429 : }
430 :
431 : sql_exp *
432 4873408 : exp_atom(allocator *sa, atom *a)
433 : {
434 4873408 : sql_exp *e = exp_create(sa, e_atom);
435 4873326 : if (e == NULL)
436 : return NULL;
437 4873326 : e->card = CARD_ATOM;
438 4873326 : e->tpe = a->tpe;
439 4873326 : e->l = a;
440 4873326 : if (!a->isnull)
441 4646011 : set_has_no_nil(e);
442 : return e;
443 : }
444 :
445 : sql_exp *
446 0 : exp_atom_max(allocator *sa, sql_subtype *tpe)
447 : {
448 0 : if (tpe->type->localtype == TYPE_bte) {
449 0 : return exp_atom_bte(sa, GDK_bte_max);
450 : } else if (tpe->type->localtype == TYPE_sht) {
451 0 : return exp_atom_sht(sa, GDK_sht_max);
452 : } else if (tpe->type->localtype == TYPE_int) {
453 0 : return exp_atom_int(sa, GDK_int_max);
454 : } else if (tpe->type->localtype == TYPE_lng) {
455 0 : return exp_atom_lng(sa, GDK_lng_max);
456 : #ifdef HAVE_HGE
457 : } else if (tpe->type->localtype == TYPE_hge) {
458 0 : return exp_atom_hge(sa, GDK_hge_max);
459 : #endif
460 : }
461 : return NULL;
462 : }
463 :
464 : sql_exp *
465 134260 : exp_atom_bool(allocator *sa, int b)
466 : {
467 134260 : sql_subtype bt;
468 :
469 134260 : sql_find_subtype(&bt, "boolean", 0, 0);
470 134259 : if (b)
471 84943 : return exp_atom(sa, atom_bool(sa, &bt, TRUE ));
472 : else
473 49316 : return exp_atom(sa, atom_bool(sa, &bt, FALSE ));
474 : }
475 :
476 : sql_exp *
477 0 : exp_atom_bte(allocator *sa, bte i)
478 : {
479 0 : sql_subtype it;
480 :
481 0 : sql_find_subtype(&it, "tinyint", 3, 0);
482 0 : return exp_atom(sa, atom_int(sa, &it, i ));
483 : }
484 :
485 : sql_exp *
486 0 : exp_atom_sht(allocator *sa, sht i)
487 : {
488 0 : sql_subtype it;
489 :
490 0 : sql_find_subtype(&it, "smallint", 5, 0);
491 0 : return exp_atom(sa, atom_int(sa, &it, i ));
492 : }
493 :
494 : sql_exp *
495 850525 : exp_atom_int(allocator *sa, int i)
496 : {
497 850525 : sql_subtype it;
498 :
499 850525 : sql_find_subtype(&it, "int", 9, 0);
500 850526 : return exp_atom(sa, atom_int(sa, &it, i ));
501 : }
502 :
503 : sql_exp *
504 16198 : exp_atom_lng(allocator *sa, lng i)
505 : {
506 16198 : sql_subtype it;
507 :
508 : #ifdef HAVE_HGE
509 16198 : sql_find_subtype(&it, "bigint", 18, 0);
510 : #else
511 : sql_find_subtype(&it, "bigint", 19, 0);
512 : #endif
513 16198 : return exp_atom(sa, atom_int(sa, &it, i ));
514 : }
515 :
516 : sql_exp *
517 17062 : exp_atom_oid(allocator *sa, oid i)
518 : {
519 17062 : sql_subtype it;
520 :
521 : #if SIZEOF_OID == SIZEOF_INT
522 : sql_find_subtype(&it, "oid", 31, 0);
523 : #else
524 17062 : sql_find_subtype(&it, "oid", 63, 0);
525 : #endif
526 17062 : return exp_atom(sa, atom_int(sa, &it, i ));
527 : }
528 :
529 : #ifdef HAVE_HGE
530 : sql_exp *
531 1 : exp_atom_hge(allocator *sa, hge i)
532 : {
533 1 : sql_subtype it;
534 :
535 1 : sql_find_subtype(&it, "hugeint", 39, 0);
536 1 : return exp_atom(sa, atom_int(sa, &it, i ));
537 : }
538 : #endif
539 :
540 : sql_exp *
541 0 : exp_atom_flt(allocator *sa, flt f)
542 : {
543 0 : sql_subtype it;
544 :
545 0 : sql_find_subtype(&it, "real", 24, 0);
546 0 : return exp_atom(sa, atom_float(sa, &it, (dbl)f ));
547 : }
548 :
549 : sql_exp *
550 0 : exp_atom_dbl(allocator *sa, dbl f)
551 : {
552 0 : sql_subtype it;
553 :
554 0 : sql_find_subtype(&it, "double", 53, 0);
555 0 : return exp_atom(sa, atom_float(sa, &it, (dbl)f ));
556 : }
557 :
558 : sql_exp *
559 85067 : exp_atom_str(allocator *sa, const char *s, sql_subtype *st)
560 : {
561 165592 : return exp_atom(sa, atom_string(sa, st, s?sa_strdup(sa, s):NULL));
562 : }
563 :
564 : sql_exp *
565 736270 : exp_atom_clob(allocator *sa, const char *s)
566 : {
567 736270 : sql_subtype clob;
568 :
569 736270 : sql_find_subtype(&clob, "varchar", 0, 0);
570 1471045 : return exp_atom(sa, atom_string(sa, &clob, s?sa_strdup(sa, s):NULL));
571 : }
572 :
573 : sql_exp *
574 267921 : exp_atom_ptr(allocator *sa, void *s)
575 : {
576 267921 : sql_subtype *t = sql_bind_localtype("ptr");
577 267921 : return exp_atom(sa, atom_ptr(sa, t, s));
578 : }
579 :
580 : sql_exp *
581 1668 : exp_atom_ref(allocator *sa, int i, sql_subtype *tpe)
582 : {
583 1668 : sql_exp *e = exp_create(sa, e_atom);
584 1668 : if (e == NULL)
585 : return NULL;
586 1668 : e->card = CARD_ATOM;
587 1668 : e->flag = i;
588 1668 : if (tpe)
589 1668 : e->tpe = *tpe;
590 : return e;
591 : }
592 :
593 : sql_exp *
594 111842 : exp_null(allocator *sa, sql_subtype *tpe)
595 : {
596 111842 : atom *a = atom_general(sa, tpe, NULL, 0);
597 111842 : return exp_atom(sa, a);
598 : }
599 :
600 : sql_exp *
601 2 : exp_zero(allocator *sa, sql_subtype *tpe)
602 : {
603 2 : atom *a = atom_zero_value(sa, tpe);
604 2 : return exp_atom(sa, a);
605 : }
606 :
607 : atom *
608 534 : exp_value(mvc *sql, sql_exp *e)
609 : {
610 534 : if (!e || e->type != e_atom)
611 : return NULL;
612 526 : if (e->l) { /* literal */
613 : return e->l;
614 12 : } else if (e->r) { /* param (ie not set) */
615 12 : sql_var_name *vname = (sql_var_name*) e->r;
616 :
617 12 : assert(e->flag != 0 || vname->sname); /* global variables must have a schema */
618 12 : sql_var *var = e->flag == 0 ? find_global_var(sql, mvc_bind_schema(sql, vname->sname), vname->name) :
619 12 : stack_find_var_at_level(sql, vname->name, e->flag);
620 12 : if (var)
621 0 : return &(var->var);
622 : }
623 : return NULL;
624 : }
625 :
626 : sql_exp *
627 125621 : exp_param_or_declared(allocator *sa, const char *sname, const char *name, sql_subtype *tpe, int frame)
628 : {
629 125621 : sql_var_name *vname;
630 125621 : sql_exp *e = exp_create(sa, e_atom);
631 125621 : if (e == NULL)
632 : return NULL;
633 :
634 125621 : e->r = sa_alloc(sa, sizeof(sql_var_name));
635 125621 : vname = (sql_var_name*) e->r;
636 125621 : vname->sname = sname;
637 125621 : vname->name = name;
638 125621 : e->card = CARD_ATOM;
639 125621 : e->flag = frame;
640 125621 : if (tpe)
641 125621 : e->tpe = *tpe;
642 : return e;
643 : }
644 :
645 : sql_exp *
646 211212 : exp_values(allocator *sa, list *exps)
647 : {
648 211212 : sql_exp *e = exp_create(sa, e_atom);
649 211213 : if (e == NULL)
650 : return NULL;
651 211213 : e->card = exps_card(exps);
652 211213 : e->f = exps;
653 211213 : return e;
654 : }
655 :
656 : list *
657 27809 : exp_get_values(sql_exp *e)
658 : {
659 27809 : if (is_atom(e->type) && e->f)
660 : return e->f;
661 : return NULL;
662 : }
663 :
664 : list *
665 41988 : exp_types(allocator *sa, list *exps)
666 : {
667 41988 : list *l = sa_list(sa);
668 :
669 41988 : if (exps)
670 94043 : for (node *n = exps->h; n; n = n->next)
671 52055 : list_append(l, exp_subtype(n->data));
672 41988 : return l;
673 : }
674 :
675 : int
676 951252 : have_nil(list *exps)
677 : {
678 951252 : int has_nil = 0;
679 :
680 951252 : if (exps)
681 2100607 : for (node *n = exps->h; n && !has_nil; n = n->next) {
682 1149355 : sql_exp *e = n->data;
683 1149355 : has_nil |= has_nil(e);
684 : }
685 951252 : return has_nil;
686 : }
687 :
688 : int
689 215 : have_semantics(list *exps)
690 : {
691 215 : int has_semantics = 0;
692 :
693 215 : if (exps)
694 69 : for (node *n = exps->h; n && !has_semantics; n = n->next) {
695 35 : sql_exp *e = n->data;
696 68 : has_semantics |= is_compare(e->type) && is_semantics(e);
697 : }
698 215 : return has_semantics;
699 : }
700 :
701 : sql_exp *
702 13231958 : exp_column(allocator *sa, const char *rname, const char *cname, sql_subtype *t, unsigned int card, int has_nils, int unique, int intern)
703 : {
704 13231958 : sql_exp *e = exp_create(sa, e_column);
705 :
706 13231795 : if (e == NULL)
707 : return NULL;
708 13231795 : assert(cname);
709 13231795 : e->card = card;
710 13231795 : e->alias.name = cname;
711 13231795 : e->alias.rname = rname;
712 13231795 : e->r = (char*)e->alias.name;
713 13231795 : e->l = (char*)e->alias.rname;
714 13231795 : if (t)
715 13231282 : e->tpe = *t;
716 13231795 : if (!has_nils)
717 2825689 : set_has_no_nil(e);
718 13231795 : if (unique)
719 1908042 : set_unique(e);
720 13231795 : if (intern)
721 851656 : set_intern(e);
722 : return e;
723 : }
724 :
725 : sql_exp *
726 9314118 : exp_propagate(allocator *sa, sql_exp *ne, sql_exp *oe)
727 : {
728 9314118 : if (has_label(oe) &&
729 534719 : (oe->alias.rname == ne->alias.rname || (oe->alias.rname && ne->alias.rname && strcmp(oe->alias.rname, ne->alias.rname) == 0)) &&
730 522606 : (oe->alias.name == ne->alias.name || (oe->alias.name && ne->alias.name && strcmp(oe->alias.name, ne->alias.name) == 0)))
731 522531 : ne->alias.label = oe->alias.label;
732 9314118 : if (is_intern(oe))
733 295862 : set_intern(ne);
734 9314118 : if (is_anti(oe))
735 4565 : set_anti(ne);
736 9314118 : if (is_semantics(oe))
737 666171 : set_semantics(ne);
738 9314118 : if (is_any(oe))
739 23 : set_any(ne);
740 9314118 : if (is_symmetric(oe))
741 16 : set_symmetric(ne);
742 9314118 : if (is_ascending(oe))
743 20928 : set_ascending(ne);
744 9314118 : if (nulls_last(oe))
745 4321 : set_nulls_last(ne);
746 9314118 : if (need_distinct(oe))
747 701 : set_distinct(ne);
748 9314118 : if (zero_if_empty(oe))
749 0 : set_zero_if_empty(ne);
750 9314118 : if (need_no_nil(oe))
751 81776 : set_no_nil(ne);
752 9314118 : if (!has_nil(oe))
753 2365807 : set_has_no_nil(ne);
754 9314118 : if (is_unique(oe))
755 1218388 : set_unique(ne);
756 9314118 : if (is_basecol(oe))
757 7694240 : set_basecol(ne);
758 9314118 : ne->p = prop_copy(sa, oe->p);
759 9314117 : return ne;
760 : }
761 :
762 : sql_exp *
763 585353 : exp_ref(mvc *sql, sql_exp *e)
764 : {
765 585353 : if (!exp_name(e))
766 6216 : exp_label(sql->sa, e, ++sql->label);
767 585353 : return exp_propagate(sql->sa, exp_column(sql->sa, exp_relname(e), exp_name(e), exp_subtype(e), exp_card(e), has_nil(e), is_unique(e), is_intern(e)), e);
768 : }
769 :
770 : sql_exp *
771 14470 : exp_ref_save(mvc *sql, sql_exp *e)
772 : {
773 14470 : if (is_atom(e->type))
774 5437 : return exp_copy(sql, e);
775 9033 : if (!exp_name(e) || is_convert(e->type))
776 71 : exp_label(sql->sa, e, ++sql->label);
777 9033 : if (e->type != e_column)
778 5322 : e->ref = 1;
779 9033 : sql_exp *ne = exp_ref(sql, e);
780 9033 : if (ne && is_freevar(e))
781 34 : set_freevar(ne, is_freevar(e)-1);
782 : return ne;
783 : }
784 :
785 : sql_exp *
786 3431578 : exp_alias(allocator *sa, const char *arname, const char *acname, const char *org_rname, const char *org_cname, sql_subtype *t, unsigned int card, int has_nils, int unique, int intern)
787 : {
788 3431578 : sql_exp *e = exp_column(sa, org_rname, org_cname, t, card, has_nils, unique, intern);
789 :
790 3431506 : if (e == NULL)
791 : return NULL;
792 3431506 : assert(acname && org_cname);
793 3431506 : exp_setname(sa, e, (arname)?arname:org_rname, acname);
794 3431506 : return e;
795 : }
796 :
797 : sql_exp *
798 6343496 : exp_alias_or_copy( mvc *sql, const char *tname, const char *cname, sql_rel *orel, sql_exp *old)
799 : {
800 6343496 : sql_exp *ne = NULL;
801 :
802 6343496 : if (!tname)
803 5602546 : tname = exp_relname(old);
804 :
805 6343495 : if (!cname && exp_name(old) && has_label(old)) {
806 0 : ne = exp_column(sql->sa, exp_relname(old), exp_name(old), exp_subtype(old), orel && old->card != CARD_ATOM?orel->card:CARD_ATOM, has_nil(old), is_unique(old), is_intern(old));
807 0 : return exp_propagate(sql->sa, ne, old);
808 6343495 : } else if (!cname) {
809 22205 : exp_label(sql->sa, old, ++sql->label);
810 22205 : ne = exp_column(sql->sa, exp_relname(old), exp_name(old), exp_subtype(old), orel && old->card != CARD_ATOM?orel->card:CARD_ATOM, has_nil(old), is_unique(old), is_intern(old));
811 22205 : return exp_propagate(sql->sa, ne, old);
812 6321290 : } else if (cname && !old->alias.name) {
813 1838 : exp_setname(sql->sa, old, tname, cname);
814 : }
815 6321290 : ne = exp_column(sql->sa, tname, cname, exp_subtype(old), orel && old->card != CARD_ATOM?orel->card:CARD_ATOM, has_nil(old), is_unique(old), is_intern(old));
816 6321292 : return exp_propagate(sql->sa, ne, old);
817 : }
818 :
819 : sql_exp *
820 0 : exp_alias_ref(mvc *sql, sql_exp *e)
821 : {
822 0 : sql_exp *ne = NULL;
823 0 : const char *tname = exp_relname(e);
824 0 : const char *cname = exp_name(e);
825 :
826 0 : if (!has_label(e))
827 0 : exp_label(sql->sa, e, ++sql->label);
828 0 : ne = exp_ref(sql, e);
829 0 : exp_setname(sql->sa, ne, tname, cname);
830 0 : return exp_propagate(sql->sa, ne, e);
831 : }
832 :
833 : sql_exp *
834 16231 : exp_set(allocator *sa, const char *sname, const char *name, sql_exp *val, int level)
835 : {
836 16231 : sql_exp *e = exp_create(sa, e_psm);
837 :
838 16231 : if (e == NULL)
839 : return NULL;
840 16231 : e->alias.rname = sname;
841 16231 : e->alias.name = name;
842 16231 : e->l = val;
843 16231 : e->flag = PSM_SET + SET_PSM_LEVEL(level);
844 16231 : return e;
845 : }
846 :
847 : sql_exp *
848 9559 : exp_var(allocator *sa, const char *sname, const char *name, sql_subtype *type, int level)
849 : {
850 9559 : sql_exp *e = exp_create(sa, e_psm);
851 :
852 9559 : if (e == NULL)
853 : return NULL;
854 9559 : e->alias.rname = sname;
855 9559 : e->alias.name = name;
856 9559 : e->tpe = *type;
857 9559 : e->flag = PSM_VAR + SET_PSM_LEVEL(level);
858 9559 : return e;
859 : }
860 :
861 : sql_exp *
862 119 : exp_table(allocator *sa, const char *name, sql_table *t, int level)
863 : {
864 119 : sql_exp *e = exp_create(sa, e_psm);
865 :
866 119 : if (e == NULL)
867 : return NULL;
868 119 : e->alias.rname = NULL;
869 119 : e->alias.name = name;
870 119 : e->f = t;
871 119 : e->flag = PSM_VAR + SET_PSM_LEVEL(level);
872 119 : return e;
873 : }
874 :
875 : sql_exp *
876 23438 : exp_return(allocator *sa, sql_exp *val, int level)
877 : {
878 23438 : sql_exp *e = exp_create(sa, e_psm);
879 :
880 23438 : if (e == NULL)
881 : return NULL;
882 23438 : e->l = val;
883 23438 : e->flag = PSM_RETURN + SET_PSM_LEVEL(level);
884 23438 : return e;
885 : }
886 :
887 : sql_exp *
888 1023 : exp_while(allocator *sa, sql_exp *cond, list *stmts)
889 : {
890 1023 : sql_exp *e = exp_create(sa, e_psm);
891 :
892 1023 : if (e == NULL)
893 : return NULL;
894 1023 : e->l = cond;
895 1023 : e->r = stmts;
896 1023 : e->flag = PSM_WHILE;
897 1023 : return e;
898 : }
899 :
900 : sql_exp *
901 11681 : exp_if(allocator *sa, sql_exp *cond, list *if_stmts, list *else_stmts)
902 : {
903 11681 : sql_exp *e = exp_create(sa, e_psm);
904 :
905 11681 : if (e == NULL)
906 : return NULL;
907 11681 : e->l = cond;
908 11681 : e->r = if_stmts;
909 11681 : e->f = else_stmts;
910 11681 : e->flag = PSM_IF;
911 11681 : return e;
912 : }
913 :
914 : sql_exp *
915 79293 : exp_rel(mvc *sql, sql_rel *rel)
916 : {
917 79293 : sql_exp *e = exp_create(sql->sa, e_psm);
918 :
919 79293 : if (e == NULL)
920 : return NULL;
921 79293 : e->l = rel;
922 79293 : e->flag = PSM_REL;
923 79293 : e->card = is_single(rel)?CARD_ATOM:rel->card;
924 79293 : assert(rel);
925 79293 : if (is_topn(rel->op))
926 3 : rel = rel->l;
927 79293 : if (is_project(rel->op)) {
928 60255 : sql_exp *last = rel->exps->t->data;
929 60255 : sql_subtype *t = exp_subtype(last);
930 60255 : e->tpe = t ? *t : (sql_subtype) {0};
931 : }
932 : return e;
933 : }
934 :
935 : sql_exp *
936 157 : exp_exception(allocator *sa, sql_exp *cond, const char *error_message)
937 : {
938 157 : sql_exp *e = exp_create(sa, e_psm);
939 :
940 157 : if (e == NULL)
941 : return NULL;
942 157 : e->l = cond;
943 157 : e->r = sa_strdup(sa, error_message);
944 157 : e->flag = PSM_EXCEPTION;
945 157 : return e;
946 : }
947 :
948 : /* Set a name (alias) for the expression, such that we can refer
949 : to this expression by this simple name.
950 : */
951 : void
952 4572023 : exp_setname(allocator *sa, sql_exp *e, const char *rname, const char *name )
953 : {
954 4572023 : (void)sa;
955 4572023 : e->alias.label = 0;
956 4572023 : if (name)
957 4406354 : e->alias.name = name;
958 4572023 : e->alias.rname = (rname);
959 4572023 : }
960 :
961 : void
962 162397 : noninternexp_setname(allocator *sa, sql_exp *e, const char *rname, const char *name )
963 : {
964 162397 : if (!is_intern(e))
965 162395 : exp_setname(sa, e, rname, name);
966 162397 : }
967 :
968 : void
969 584719 : exp_setalias(sql_exp *e, const char *rname, const char *name )
970 : {
971 584719 : e->alias.label = 0;
972 584719 : e->alias.name = name;
973 584719 : e->alias.rname = rname;
974 584719 : }
975 :
976 : void
977 1818128 : exp_prop_alias(allocator *sa, sql_exp *e, sql_exp *oe )
978 : {
979 1818128 : e->ref = oe->ref;
980 1818128 : if (oe->alias.name == NULL && exp_has_rel(oe)) {
981 8067 : sql_rel *r = exp_rel_get_rel(sa, oe);
982 8067 : if (!is_project(r->op))
983 : return ;
984 8067 : oe = r->exps->t->data;
985 : }
986 1818128 : e->alias = oe->alias;
987 : }
988 :
989 : str
990 4135228 : number2name(str s, int len, int i)
991 : {
992 4135228 : s[--len] = 0;
993 12479512 : while(i>0) {
994 8344284 : s[--len] = '0' + (i & 7);
995 8344284 : i >>= 3;
996 : }
997 4135228 : s[--len] = '%';
998 4135228 : return s + len;
999 : }
1000 :
1001 : void
1002 2072571 : exp_setrelname(allocator *sa, sql_exp *e, int nr)
1003 : {
1004 2072571 : char name[16], *nme;
1005 :
1006 2072571 : nme = number2name(name, sizeof(name), nr);
1007 2072571 : e->alias.label = 0;
1008 2072571 : e->alias.rname = sa_strdup(sa, nme);
1009 2072571 : }
1010 :
1011 : char *
1012 2002800 : make_label(allocator *sa, int nr)
1013 : {
1014 2002800 : char name[16], *nme;
1015 :
1016 2002800 : nme = number2name(name, sizeof(name), nr);
1017 2002746 : return sa_strdup(sa, nme);
1018 : }
1019 :
1020 : sql_exp*
1021 1993349 : exp_label(allocator *sa, sql_exp *e, int nr)
1022 : {
1023 1993349 : assert(nr > 0);
1024 1993349 : e->alias.label = nr;
1025 1993349 : e->alias.rname = e->alias.name = make_label(sa, nr);
1026 1993379 : return e;
1027 : }
1028 :
1029 : list*
1030 4293 : exps_label(allocator *sa, list *exps, int nr)
1031 : {
1032 4293 : if (!exps)
1033 : return NULL;
1034 11669 : for (node *n = exps->h; n; n = n->next)
1035 7376 : n->data = exp_label(sa, n->data, nr++);
1036 4293 : list_hash_clear(exps);
1037 4293 : return exps;
1038 : }
1039 :
1040 : void
1041 20685 : exp_swap( sql_exp *e )
1042 : {
1043 20685 : sql_exp *s = e->l;
1044 :
1045 20685 : e->l = e->r;
1046 20685 : e->r = s;
1047 20685 : e->flag = swap_compare((comp_type)e->flag);
1048 20685 : assert(!e->f);
1049 20685 : }
1050 :
1051 : sql_subtype *
1052 34026076 : exp_subtype( sql_exp *e )
1053 : {
1054 34026116 : switch(e->type) {
1055 8105871 : case e_atom: {
1056 8105871 : if (e->l) {
1057 7372301 : atom *a = e->l;
1058 7372301 : return atom_type(a);
1059 733570 : } else if (e->tpe.type) { /* atom reference */
1060 731726 : return &e->tpe;
1061 1844 : } else if (e->f) {
1062 40 : list *vals = exp_get_values(e);
1063 40 : if (!list_empty(vals))
1064 40 : return exp_subtype(vals->h->data);
1065 : }
1066 : break;
1067 : }
1068 21144514 : case e_convert:
1069 : case e_column:
1070 21144514 : if (e->tpe.type)
1071 21144400 : return &e->tpe;
1072 : break;
1073 4638715 : case e_aggr:
1074 : case e_func: {
1075 4638715 : if (e->f) {
1076 4638715 : sql_subfunc *f = e->f;
1077 4638715 : if (f->res && list_length(f->res) == 1)
1078 4628238 : return f->res->h->data;
1079 : }
1080 : return NULL;
1081 : }
1082 7463 : case e_cmp:
1083 7463 : return sql_bind_localtype("bit");
1084 129553 : case e_psm:
1085 129553 : if (e->tpe.type)
1086 129538 : return &e->tpe;
1087 : /* fall through */
1088 : default:
1089 : return NULL;
1090 : }
1091 : return NULL;
1092 : }
1093 :
1094 : const char *
1095 38204791 : exp_name( sql_exp *e )
1096 : {
1097 38221422 : if (e->alias.name)
1098 : return e->alias.name;
1099 1644133 : if (e->type == e_convert && e->l)
1100 : return exp_name(e->l);
1101 1638990 : if (e->type == e_psm && e->l) { /* subquery return name of last expression */
1102 11488 : sql_rel *r = e->l;
1103 11488 : if (is_project(r->op))
1104 11488 : return exp_name(r->exps->t->data);
1105 : }
1106 : return NULL;
1107 : }
1108 :
1109 : const char *
1110 16518270 : exp_relname( sql_exp *e )
1111 : {
1112 16520419 : if (e->alias.rname)
1113 : return e->alias.rname;
1114 631947 : if (!e->alias.name && e->type == e_convert && e->l)
1115 : return exp_relname(e->l);
1116 630968 : if (!e->alias.name && e->type == e_psm && e->l) { /* subquery return name of last expression */
1117 1170 : sql_rel *r = e->l;
1118 1170 : if (is_project(r->op))
1119 1170 : return exp_relname(r->exps->t->data);
1120 : }
1121 : return NULL;
1122 : }
1123 :
1124 : const char *
1125 16794 : exp_find_rel_name(sql_exp *e)
1126 : {
1127 16794 : if (e->alias.rname)
1128 : return e->alias.rname;
1129 472 : switch(e->type) {
1130 : case e_column:
1131 : break;
1132 0 : case e_convert:
1133 0 : return exp_find_rel_name(e->l);
1134 : default:
1135 : return NULL;
1136 : }
1137 : return NULL;
1138 : }
1139 :
1140 : unsigned int
1141 2411293 : exp_card( sql_exp *e )
1142 : {
1143 2411293 : return e->card;
1144 : }
1145 :
1146 : const char *
1147 0 : exp_func_name( sql_exp *e )
1148 : {
1149 0 : if (e->type == e_func && e->f) {
1150 0 : sql_subfunc *f = e->f;
1151 0 : return f->func->base.name;
1152 : }
1153 0 : if (e->alias.name)
1154 : return e->alias.name;
1155 0 : if (e->type == e_convert && e->l)
1156 0 : return exp_name(e->l);
1157 : return NULL;
1158 : }
1159 :
1160 : int
1161 51846694 : exp_cmp( sql_exp *e1, sql_exp *e2)
1162 : {
1163 51846694 : return (e1 == e2)?0:-1;
1164 : }
1165 :
1166 : int
1167 232429 : exp_equal( sql_exp *e1, sql_exp *e2)
1168 : {
1169 232429 : if (e1 == e2)
1170 : return 0;
1171 232429 : if (e1->alias.rname && e2->alias.rname && strcmp(e1->alias.rname, e2->alias.rname) == 0)
1172 174688 : return strcmp(e1->alias.name, e2->alias.name);
1173 57741 : if (!e1->alias.rname && !e2->alias.rname && e1->alias.label == e2->alias.label && e1->alias.name && e2->alias.name)
1174 585 : return strcmp(e1->alias.name, e2->alias.name);
1175 : return -1;
1176 : }
1177 :
1178 : int
1179 51846581 : exp_match( sql_exp *e1, sql_exp *e2)
1180 : {
1181 51846581 : if (exp_cmp(e1, e2) == 0)
1182 : return 1;
1183 51590923 : if (e1->type == e2->type && e1->type == e_column) {
1184 26299133 : if (e1->l != e2->l && (!e1->l || !e2->l || strcmp(e1->l, e2->l) != 0))
1185 : return 0;
1186 14891639 : if (!e1->r || !e2->r || strcmp(e1->r, e2->r) != 0)
1187 : return 0;
1188 : return 1;
1189 : }
1190 25291790 : if (e1->type == e2->type && e1->type == e_func) {
1191 8966573 : if (is_identity(e1, NULL) && is_identity(e2, NULL)) {
1192 0 : list *args1 = e1->l;
1193 0 : list *args2 = e2->l;
1194 :
1195 0 : if (list_length(args1) == list_length(args2) && list_length(args1) == 1) {
1196 0 : sql_exp *ne1 = args1->h->data;
1197 0 : sql_exp *ne2 = args2->h->data;
1198 :
1199 0 : if (exp_match(ne1,ne2))
1200 : return 1;
1201 : }
1202 : }
1203 : }
1204 : return 0;
1205 : }
1206 :
1207 : /* list already contains matching expression */
1208 : sql_exp*
1209 438252 : exps_find_exp( list *l, sql_exp *e)
1210 : {
1211 438252 : node *n;
1212 :
1213 438252 : if (!l || !l->h)
1214 : return NULL;
1215 :
1216 1068905 : for(n=l->h; n; n = n->next) {
1217 997236 : if (exp_match(n->data, e) || exp_refers(n->data, e))
1218 353450 : return n->data;
1219 : }
1220 : return NULL;
1221 : }
1222 :
1223 :
1224 : /* c refers to the parent p */
1225 : int
1226 4113961 : exp_refers( sql_exp *p, sql_exp *c)
1227 : {
1228 4113961 : if (c->type == e_column) {
1229 : // at first they need to have the same expression names
1230 904053 : if (!p->alias.name || !c->r || strcmp(p->alias.name, c->r) != 0)
1231 : return 0;
1232 82698 : if (!c->l)
1233 : return 1;
1234 : // then compare the relation names
1235 70620 : if (c->l && (p->alias.rname || p->l)) {
1236 : // if the parent has an alias for the relation name compare with the child's relation name
1237 70619 : if (p->alias.rname && strcmp(p->alias.rname, c->l) != 0)
1238 : return 0;
1239 : // if the parent does NOT have a relation name alias compare his relation name with the child's
1240 47162 : if (!p->alias.rname && p->l && (strcmp(p->l, c->l) != 0 || strcmp(p->alias.name, p->r) !=0))
1241 : return 0;
1242 47127 : return 1;
1243 : }
1244 : }
1245 : return 0;
1246 : }
1247 :
1248 : int
1249 0 : exp_match_col_exps( sql_exp *e, list *l)
1250 : {
1251 0 : node *n;
1252 :
1253 0 : for(n=l->h; n; n = n->next) {
1254 0 : sql_exp *re = n->data;
1255 0 : sql_exp *re_r = re->r;
1256 :
1257 0 : if (re->type == e_cmp && re->flag == cmp_or)
1258 0 : return exp_match_col_exps(e, re->l) &&
1259 0 : exp_match_col_exps(e, re->r);
1260 :
1261 0 : if (re->type != e_cmp || !re_r || re_r->card != 1 || !exp_match_exp(e, re->l))
1262 0 : return 0;
1263 : }
1264 : return 1;
1265 : }
1266 :
1267 : int
1268 10595 : exps_match_col_exps( sql_exp *e1, sql_exp *e2)
1269 : {
1270 10595 : sql_exp *e1_r = e1->r;
1271 10595 : sql_exp *e2_r = e2->r;
1272 :
1273 10595 : if (e1->type != e_cmp || e2->type != e_cmp)
1274 : return 0;
1275 :
1276 10534 : if (!is_complex_exp(e1->flag) && e1_r && e1_r->card == CARD_ATOM &&
1277 5994 : !is_complex_exp(e2->flag) && e2_r && e2_r->card == CARD_ATOM)
1278 5066 : return exp_match_exp(e1->l, e2->l);
1279 :
1280 5468 : if (!is_complex_exp(e1->flag) && e1_r && e1_r->card == CARD_ATOM &&
1281 928 : (e2->flag == cmp_in || e2->flag == cmp_notin))
1282 845 : return exp_match_exp(e1->l, e2->l);
1283 4623 : if ((e1->flag == cmp_in || e1->flag == cmp_notin) &&
1284 2115 : !is_complex_exp(e2->flag) && e2_r && e2_r->card == CARD_ATOM)
1285 1588 : return exp_match_exp(e1->l, e2->l);
1286 :
1287 3035 : if ((e1->flag == cmp_in || e1->flag == cmp_notin) &&
1288 527 : (e2->flag == cmp_in || e2->flag == cmp_notin))
1289 527 : return exp_match_exp(e1->l, e2->l);
1290 :
1291 2508 : if (!is_complex_exp(e1->flag) && e1_r && e1_r->card == CARD_ATOM &&
1292 83 : e2->flag == cmp_or)
1293 0 : return exp_match_col_exps(e1->l, e2->l) &&
1294 0 : exp_match_col_exps(e1->l, e2->r);
1295 :
1296 2508 : if (e1->flag == cmp_or &&
1297 0 : !is_complex_exp(e2->flag) && e2_r && e2_r->card == CARD_ATOM)
1298 0 : return exp_match_col_exps(e2->l, e1->l) &&
1299 0 : exp_match_col_exps(e2->l, e1->r);
1300 :
1301 2508 : if (e1->flag == cmp_or && e2->flag == cmp_or) {
1302 0 : list *l = e1->l, *r = e1->r;
1303 0 : sql_exp *el = l->h->data;
1304 0 : sql_exp *er = r->h->data;
1305 :
1306 0 : return list_length(l) == 1 && list_length(r) == 1 &&
1307 0 : exps_match_col_exps(el, e2) &&
1308 0 : exps_match_col_exps(er, e2);
1309 : }
1310 : return 0;
1311 : }
1312 :
1313 : int
1314 48019 : exp_match_list( list *l, list *r)
1315 : {
1316 48019 : node *n, *m;
1317 48019 : char *lu, *ru;
1318 48019 : int lc = 0, rc = 0, match = 0;
1319 :
1320 48019 : if (!l || !r)
1321 11 : return l == r;
1322 48008 : if (list_length(l) != list_length(r) || list_length(l) == 0 || list_length(r) == 0)
1323 587 : return 0;
1324 47421 : if (list_length(l) > 10 || list_length(r) > 10)
1325 5 : return 0;/* to expensive */
1326 :
1327 47416 : lu = ZNEW_ARRAY(char, list_length(l));
1328 47416 : ru = ZNEW_ARRAY(char, list_length(r));
1329 47416 : if (!lu || !ru) {
1330 0 : _DELETE(lu);
1331 0 : _DELETE(ru);
1332 0 : return 0;
1333 : }
1334 139192 : for (n = l->h, lc = 0; n; n = n->next, lc++) {
1335 91776 : sql_exp *le = n->data;
1336 :
1337 273238 : for ( m = r->h, rc = 0; m; m = m->next, rc++) {
1338 181462 : sql_exp *re = m->data;
1339 :
1340 181462 : if (!ru[rc] && exp_match_exp(le,re)) {
1341 6762 : lu[lc] = 1;
1342 6762 : ru[rc] = 1;
1343 6762 : match = 1;
1344 : }
1345 : }
1346 : }
1347 54639 : for (n = l->h, lc = 0; n && match; n = n->next, lc++)
1348 7223 : if (!lu[lc])
1349 1289 : match = 0;
1350 52858 : for (n = r->h, rc = 0; n && match; n = n->next, rc++)
1351 5442 : if (!ru[rc])
1352 0 : match = 0;
1353 47416 : _DELETE(lu);
1354 47416 : _DELETE(ru);
1355 47416 : return match;
1356 : }
1357 :
1358 : static int
1359 2468584 : exps_equal( list *l, list *r)
1360 : {
1361 2468584 : node *n, *m;
1362 :
1363 2468584 : if (!l || !r)
1364 51778 : return l == r;
1365 2416806 : if (list_length(l) != list_length(r))
1366 : return 0;
1367 3548318 : for (n = l->h, m = r->h; n && m; n = n->next, m = m->next) {
1368 3500175 : sql_exp *le = n->data, *re = m->data;
1369 :
1370 3500175 : if (!exp_match_exp(le,re))
1371 : return 0;
1372 : }
1373 : return 1;
1374 : }
1375 :
1376 : int
1377 47394738 : exp_match_exp_semantics( sql_exp *e1, sql_exp *e2, bool semantics)
1378 : {
1379 47394738 : if (exp_match(e1, e2))
1380 : return 1;
1381 46174653 : if (is_ascending(e1) != is_ascending(e2) || nulls_last(e1) != nulls_last(e2) || zero_if_empty(e1) != zero_if_empty(e2) ||
1382 46174649 : need_no_nil(e1) != need_no_nil(e2) || is_anti(e1) != is_anti(e2) || (semantics && is_semantics(e1) != is_semantics(e2)) ||
1383 33970544 : (semantics && is_any(e1) != is_any(e2)) ||
1384 34304268 : is_symmetric(e1) != is_symmetric(e2) || is_unique(e1) != is_unique(e2) || need_distinct(e1) != need_distinct(e2))
1385 : return 0;
1386 33607177 : if (e1->type == e2->type) {
1387 25303951 : switch(e1->type) {
1388 391840 : case e_cmp:
1389 647728 : if (e1->flag == e2->flag && !is_complex_exp(e1->flag) &&
1390 260195 : exp_match_exp(e1->l, e2->l) && exp_match_exp(e1->r, e2->r) &&
1391 949 : ((!e1->f && !e2->f) || (e1->f && e2->f && exp_match_exp(e1->f, e2->f))))
1392 942 : return 1;
1393 394336 : else if (e1->flag == e2->flag && e1->flag == cmp_or &&
1394 3450 : exp_match_list(e1->l, e2->l) && exp_match_list(e1->r, e2->r))
1395 : return 1;
1396 390893 : else if (e1->flag == e2->flag &&
1397 264382 : (e1->flag == cmp_in || e1->flag == cmp_notin) &&
1398 2947 : exp_match_exp(e1->l, e2->l) && exp_match_list(e1->r, e2->r))
1399 : return 1;
1400 642661 : else if (e1->flag == e2->flag && (e1->flag == cmp_equal || e1->flag == cmp_notequal) &&
1401 251812 : exp_match_exp(e1->l, e2->r) && exp_match_exp(e1->r, e2->l))
1402 : return 1; /* = and <> operations are reflective, so exp_match_exp can be called crossed */
1403 : break;
1404 301454 : case e_convert:
1405 373153 : if (!subtype_cmp(exp_totype(e1), exp_totype(e2)) &&
1406 102701 : !subtype_cmp(exp_fromtype(e1), exp_fromtype(e2)) &&
1407 31002 : exp_match_exp(e1->l, e2->l))
1408 : return 1;
1409 : break;
1410 58972 : case e_aggr:
1411 94617 : if (!subfunc_cmp(e1->f, e2->f) && /* equal aggregation*/
1412 35645 : exps_equal(e1->l, e2->l))
1413 : return 1;
1414 : break;
1415 4546569 : case e_func: {
1416 4546569 : sql_subfunc *e1f = (sql_subfunc*) e1->f;
1417 4546569 : const char *sname = e1f->func->s ? e1f->func->s->base.name : NULL;
1418 4546569 : int (*comp)(list*, list*) = is_commutative(sname, e1f->func->base.name) ? exp_match_list : exps_equal;
1419 :
1420 9093138 : if (!e1f->func->side_effect &&
1421 6967708 : !subfunc_cmp(e1f, e2->f) && /* equal functions */
1422 2471680 : comp(e1->l, e2->l) &&
1423 : /* optional order by expressions */
1424 50541 : exps_equal(e1->r, e2->r))
1425 : return 1;
1426 : } break;
1427 1090650 : case e_atom:
1428 1090650 : if (e1->l && e2->l && !atom_cmp(e1->l, e2->l))
1429 : return 1;
1430 1058757 : if (e1->f && e2->f && exps_equal(e1->f, e2->f))
1431 : return 1;
1432 1058729 : if (e1->r && e2->r && e1->flag == e2->flag && !subtype_cmp(&e1->tpe, &e2->tpe)) {
1433 0 : sql_var_name *v1 = (sql_var_name*) e1->r, *v2 = (sql_var_name*) e2->r;
1434 0 : if (((!v1->sname && !v2->sname) || (v1->sname && v2->sname && strcmp(v1->sname, v2->sname) == 0)) &&
1435 0 : ((!v1->name && !v2->name) || (v1->name && v2->name && strcmp(v1->name, v2->name) == 0)))
1436 : return 1;
1437 : }
1438 1058729 : if (!e1->l && !e1->r && !e1->f && !e2->l && !e2->r && !e2->f && e1->flag == e2->flag && !subtype_cmp(&e1->tpe, &e2->tpe))
1439 : return 1;
1440 : break;
1441 : default:
1442 : break;
1443 : }
1444 : }
1445 : return 0;
1446 : }
1447 :
1448 : int
1449 47057825 : exp_match_exp( sql_exp *e1, sql_exp *e2)
1450 : {
1451 47057825 : return exp_match_exp_semantics( e1, e2, true);
1452 : }
1453 :
1454 : sql_exp *
1455 144737 : exps_any_match(list *l, sql_exp *e)
1456 : {
1457 144737 : if (!l)
1458 : return NULL;
1459 564595 : for (node *n = l->h; n ; n = n->next) {
1460 511154 : sql_exp *ne = (sql_exp *) n->data;
1461 511154 : if (exp_match_exp(ne, e))
1462 91296 : return ne;
1463 : }
1464 : return NULL;
1465 : }
1466 :
1467 : static int
1468 24 : exps_are_joins( list *l )
1469 : {
1470 24 : if (l)
1471 52 : for (node *n = l->h; n; n = n->next) {
1472 28 : sql_exp *e = n->data;
1473 :
1474 28 : if (exp_is_join_exp(e))
1475 : return -1;
1476 : }
1477 : return 0;
1478 : }
1479 :
1480 : int
1481 284 : exp_is_join_exp(sql_exp *e)
1482 : {
1483 284 : if (exp_is_join(e, NULL) == 0)
1484 : return 0;
1485 29 : if (e->type == e_cmp && e->flag == cmp_or && e->card >= CARD_AGGR)
1486 12 : if (exps_are_joins(e->l) == 0 && exps_are_joins(e->r) == 0)
1487 : return 0;
1488 : return -1;
1489 : }
1490 :
1491 : static int
1492 799446 : exp_is_complex_select( sql_exp *e )
1493 : {
1494 818844 : switch (e->type) {
1495 531 : case e_atom: {
1496 531 : if (e->f) {
1497 0 : int r = (e->card == CARD_ATOM);
1498 0 : list *l = e->f;
1499 :
1500 0 : if (r)
1501 0 : for (node *n = l->h; n && !r; n = n->next)
1502 0 : r |= exp_is_complex_select(n->data);
1503 0 : return r;
1504 : }
1505 : return 0;
1506 : }
1507 19398 : case e_convert:
1508 19398 : return exp_is_complex_select(e->l);
1509 2092 : case e_func:
1510 : case e_aggr:
1511 : {
1512 2092 : int r = (e->card == CARD_ATOM);
1513 2092 : list *l = e->l;
1514 :
1515 2092 : if (r && l)
1516 22 : for (node *n = l->h; n && !r; n = n->next)
1517 0 : r |= exp_is_complex_select(n->data);
1518 : return r;
1519 : }
1520 : case e_psm:
1521 : return 1;
1522 : case e_column:
1523 : case e_cmp:
1524 : default:
1525 : return 0;
1526 : }
1527 : }
1528 :
1529 : static int
1530 399728 : complex_select(sql_exp *e)
1531 : {
1532 399728 : sql_exp *l = e->l, *r = e->r;
1533 :
1534 399728 : if (exp_is_complex_select(l) || exp_is_complex_select(r))
1535 22 : return 1;
1536 : return 0;
1537 : }
1538 :
1539 : static int
1540 961 : distinct_rel(sql_exp *e, const char **rname)
1541 : {
1542 1107 : const char *e_rname = NULL;
1543 :
1544 1107 : switch(e->type) {
1545 679 : case e_column:
1546 679 : e_rname = exp_relname(e);
1547 :
1548 679 : if (*rname && e_rname && strcmp(*rname, e_rname) == 0)
1549 : return 1;
1550 555 : if (!*rname) {
1551 336 : *rname = e_rname;
1552 336 : return 1;
1553 : }
1554 : break;
1555 147 : case e_aggr:
1556 : case e_func:
1557 147 : if (e->l) {
1558 147 : int m = 1;
1559 147 : list *l = e->l;
1560 147 : node *n;
1561 :
1562 444 : for(n=l->h; n && m; n = n->next) {
1563 297 : sql_exp *ae = n->data;
1564 :
1565 297 : m = distinct_rel(ae, rname);
1566 : }
1567 147 : return m;
1568 : }
1569 : return 0;
1570 : case e_atom:
1571 : return 1;
1572 146 : case e_convert:
1573 146 : return distinct_rel(e->l, rname);
1574 : default:
1575 : return 0;
1576 : }
1577 : return 0;
1578 : }
1579 :
1580 : int
1581 20755518 : rel_has_exp(sql_rel *rel, sql_exp *e, bool subexp)
1582 : {
1583 20755518 : if (rel_find_exp_and_corresponding_rel(rel, e, subexp, NULL, NULL))
1584 4522703 : return 0;
1585 : return -1;
1586 : }
1587 :
1588 : int
1589 0 : rel_has_exps(sql_rel *rel, list *exps, bool subexp)
1590 : {
1591 0 : if (list_empty(exps))
1592 : return 0;
1593 0 : for (node *n = exps->h; n; n = n->next)
1594 0 : if (rel_has_exp(rel, n->data, subexp) >= 0)
1595 : return 0;
1596 : return -1;
1597 : }
1598 :
1599 : int
1600 4856 : rel_has_all_exps(sql_rel *rel, list *exps)
1601 : {
1602 4856 : if (list_empty(exps))
1603 : return 1;
1604 9919 : for (node *n = exps->h; n; n = n->next)
1605 9711 : if (rel_has_exp(rel, n->data, false) < 0)
1606 : return 0;
1607 : return 1;
1608 : }
1609 :
1610 : static int
1611 15945311 : rel_has_exp2(sql_rel *rel, sql_exp *e)
1612 : {
1613 15945311 : return rel_has_exp(rel, e, false);
1614 : }
1615 :
1616 : sql_rel *
1617 6095082 : find_rel(list *rels, sql_exp *e)
1618 : {
1619 6095082 : node *n = list_find(rels, e, (fcmp)&rel_has_exp2);
1620 6095082 : if (n)
1621 3505258 : return n->data;
1622 : return NULL;
1623 : }
1624 :
1625 : sql_rel *
1626 0 : find_one_rel(list *rels, sql_exp *e)
1627 : {
1628 0 : node *n;
1629 0 : sql_rel *fnd = NULL;
1630 :
1631 0 : for(n = rels->h; n; n = n->next) {
1632 0 : if (rel_has_exp(n->data, e, false) == 0) {
1633 0 : if (fnd)
1634 : return NULL;
1635 0 : fnd = n->data;
1636 : }
1637 : }
1638 : return fnd;
1639 : }
1640 :
1641 : static int
1642 341 : exp_is_rangejoin(sql_exp *e, list *rels)
1643 : {
1644 : /* assume e is a e_cmp with 3 args
1645 : * Need to check e->r and e->f only touch one table.
1646 : */
1647 341 : const char *rname = 0;
1648 :
1649 341 : if (distinct_rel(e->r, &rname) && distinct_rel(e->f, &rname))
1650 : return 0;
1651 219 : if (rels) {
1652 151 : sql_rel *r = find_rel(rels, e->r);
1653 151 : sql_rel *f = find_rel(rels, e->f);
1654 151 : if (r && f && r == f)
1655 : return 0;
1656 : }
1657 : return -1;
1658 : }
1659 :
1660 : int
1661 401435 : exp_is_join(sql_exp *e, list *rels)
1662 : {
1663 : /* only simple compare expressions, ie not or lists
1664 : or range expressions (e->f)
1665 : */
1666 401435 : if (e->type == e_cmp && !is_complex_exp(e->flag) && e->l && e->r && !e->f && e->card >= CARD_AGGR && !complex_select(e))
1667 : return 0;
1668 2070 : if (e->type == e_cmp && e->flag == cmp_filter && e->l && e->r && e->card >= CARD_AGGR)
1669 : return 0;
1670 : /* range expression */
1671 1900 : if (e->type == e_cmp && !is_complex_exp(e->flag) && e->l && e->r && e->f && e->card >= CARD_AGGR && !complex_select(e))
1672 341 : return exp_is_rangejoin(e, rels);
1673 : return -1;
1674 : }
1675 :
1676 : int
1677 395293 : exp_is_eqjoin(sql_exp *e)
1678 : {
1679 395293 : if (e->flag == cmp_equal) {
1680 385438 : sql_exp *l = e->l;
1681 385438 : sql_exp *r = e->r;
1682 :
1683 385438 : if (!is_func(l->type) && !is_func(r->type))
1684 384314 : return 0;
1685 : }
1686 : return -1;
1687 : }
1688 :
1689 : sql_exp *
1690 274255 : exps_find_prop(list *exps, rel_prop kind)
1691 : {
1692 274255 : if (list_empty(exps))
1693 : return NULL;
1694 547173 : for (node *n = exps->h ; n ; n = n->next) {
1695 274255 : sql_exp *e = n->data;
1696 :
1697 274255 : if (find_prop(e->p, kind))
1698 1337 : return e;
1699 : }
1700 : return NULL;
1701 : }
1702 :
1703 : static sql_exp *
1704 79089427 : rel_find_exp_and_corresponding_rel_(sql_rel *rel, sql_exp *e, bool subexp, sql_rel **res)
1705 : {
1706 79089427 : sql_exp *ne = NULL;
1707 :
1708 79089427 : if (!rel)
1709 : return NULL;
1710 79089346 : switch(e->type) {
1711 76955931 : case e_column:
1712 76955931 : if (is_basetable(rel->op) && !rel->exps) {
1713 16484 : if (e->l) {
1714 16482 : if (rel_base_bind_column2_(rel, e->l, e->r))
1715 14403 : ne = e;
1716 2 : } else if (rel_base_bind_column_(rel, e->r))
1717 14403 : ne = e;
1718 97527410 : } else if ((!list_empty(rel->exps) && (is_project(rel->op) || is_base(rel->op))) ||
1719 21091367 : (!list_empty(rel->attr) && is_join(rel->op))) {
1720 56854850 : list *l = rel->attr ? rel->attr : rel->exps;
1721 56854850 : if (e->l) {
1722 55357541 : ne = exps_bind_column2(l, e->l, e->r, NULL);
1723 : } else {
1724 1497309 : ne = exps_bind_column(l, e->r, NULL, NULL, 1);
1725 : }
1726 : }
1727 76955881 : if (ne && res)
1728 70448 : *res = rel;
1729 : return ne;
1730 1002957 : case e_convert:
1731 1002957 : return rel_find_exp_and_corresponding_rel_(rel, e->l, subexp, res);
1732 588809 : case e_aggr:
1733 : case e_func:
1734 588809 : if (e->l) {
1735 585489 : list *l = e->l;
1736 585489 : node *n = l->h;
1737 :
1738 585489 : ne = n->data;
1739 1577404 : while ((subexp || ne != NULL) && n != NULL) {
1740 1000662 : ne = rel_find_exp_and_corresponding_rel_(rel, n->data, subexp, res);
1741 1000662 : if (subexp && ne)
1742 : break;
1743 991915 : n = n->next;
1744 : }
1745 585489 : return ne;
1746 : }
1747 : break;
1748 : /* fall through */
1749 : case e_cmp:
1750 : case e_psm:
1751 : return NULL;
1752 540679 : case e_atom:
1753 540679 : if (e->f) { /* values */
1754 7588 : list *l = e->f;
1755 7588 : node *n = l->h;
1756 :
1757 7588 : ne = n->data;
1758 17424 : while ((subexp || ne != NULL) && n != NULL) {
1759 9836 : ne = rel_find_exp_and_corresponding_rel_(rel, n->data, subexp, res);
1760 9836 : if (subexp && ne)
1761 : break;
1762 9836 : n = n->next;
1763 : }
1764 7588 : return ne;
1765 : }
1766 : return e;
1767 : }
1768 : return ne;
1769 : }
1770 :
1771 : sql_exp *
1772 77075954 : rel_find_exp_and_corresponding_rel(sql_rel *rel, sql_exp *e, bool subexp, sql_rel **res, bool *under_join)
1773 : {
1774 77075954 : sql_exp *ne = rel_find_exp_and_corresponding_rel_(rel, e, subexp, res);
1775 :
1776 77075922 : if (rel && !ne) {
1777 55670179 : switch(rel->op) {
1778 13620345 : case op_left:
1779 : case op_right:
1780 : case op_full:
1781 : case op_join:
1782 : case op_semi:
1783 : case op_anti:
1784 13620345 : ne = rel_find_exp_and_corresponding_rel(rel->l, e, subexp, res, under_join);
1785 13620345 : if (!ne && is_join(rel->op))
1786 8740460 : ne = rel_find_exp_and_corresponding_rel(rel->r, e, subexp, res, under_join);
1787 : break;
1788 : case op_table:
1789 : case op_basetable:
1790 : break;
1791 14400594 : default:
1792 14400594 : if (!is_project(rel->op) && rel->l)
1793 6955185 : ne = rel_find_exp_and_corresponding_rel(rel->l, e, subexp, res, under_join);
1794 : }
1795 : }
1796 77075922 : if (ne && under_join && is_join(rel->op))
1797 2945041 : *under_join = true;
1798 77075922 : return ne;
1799 : }
1800 :
1801 : sql_exp *
1802 21867187 : rel_find_exp(sql_rel *rel, sql_exp *e)
1803 : {
1804 21867187 : return rel_find_exp_and_corresponding_rel(rel, e, false, NULL, NULL);
1805 : }
1806 :
1807 : int
1808 4192418 : exp_is_true(sql_exp *e)
1809 : {
1810 4192418 : if (e->type == e_atom && e->l)
1811 20414 : return atom_is_true(e->l);
1812 4172004 : if (e->type == e_cmp && e->flag == cmp_equal)
1813 3397111 : return (exp_is_true(e->l) && exp_is_true(e->r) && exp_match_exp(e->l, e->r));
1814 : return 0;
1815 : }
1816 :
1817 : static inline bool
1818 327518 : exp_is_cmp_exp_is_false(sql_exp* e)
1819 : {
1820 327518 : sql_exp *l = e->l;
1821 327518 : sql_exp *r = e->r;
1822 327518 : assert(e->type == e_cmp && e->f == NULL && l && r);
1823 :
1824 : /* Handle 'v is x' and 'v is not x' expressions.
1825 : * Other cases in is-semantics are unspecified.
1826 : */
1827 327518 : if (e->flag != cmp_equal && e->flag != cmp_notequal)
1828 : return false;
1829 327518 : if (e->flag == cmp_equal && !is_anti(e))
1830 504626 : return ((exp_is_null(l) && exp_is_not_null(r)) || (exp_is_not_null(l) && exp_is_null(r)));
1831 75205 : if ((e->flag == cmp_notequal && !is_anti(e)) || (e->flag == cmp_equal && is_anti(e)))
1832 150358 : return exp_is_null(l) && exp_is_null(r);
1833 : return false;
1834 : }
1835 :
1836 : static inline bool
1837 5719597 : exp_single_bound_cmp_exp_is_false(sql_exp* e) {
1838 5719597 : assert(e->type == e_cmp);
1839 5719597 : sql_exp* l = e->l;
1840 5719597 : sql_exp* r = e->r;
1841 5719597 : assert(e->f == NULL);
1842 5719597 : assert (l && r);
1843 :
1844 5719597 : return exp_is_null(l) || exp_is_null(r);
1845 : }
1846 :
1847 : static inline bool
1848 75857 : exp_two_sided_bound_cmp_exp_is_false(sql_exp* e) {
1849 75857 : assert(e->type == e_cmp);
1850 75857 : sql_exp* v = e->l;
1851 75857 : sql_exp* l = e->r;
1852 75857 : sql_exp* h = e->f;
1853 75857 : assert (v && l && h);
1854 :
1855 75857 : return is_anti(e) ? exp_is_null(v) || (exp_is_null(l) && exp_is_null(h)) : false;
1856 : }
1857 :
1858 : static inline bool
1859 6133720 : exp_regular_cmp_exp_is_false(sql_exp* e) {
1860 6133720 : assert(e->type == e_cmp);
1861 :
1862 6133720 : if (is_semantics(e) && !is_any(e)) return exp_is_cmp_exp_is_false(e);
1863 5806202 : if (is_any(e)) return false;
1864 5795454 : if (e -> f) return exp_two_sided_bound_cmp_exp_is_false(e);
1865 5719597 : else return exp_single_bound_cmp_exp_is_false(e);
1866 : }
1867 :
1868 : static inline bool
1869 606838 : exp_or_exp_is_false(sql_exp* e) {
1870 606838 : assert(e->type == e_cmp && e->flag == cmp_or);
1871 :
1872 606838 : list* left = e->l;
1873 606838 : list* right = e->r;
1874 :
1875 606838 : bool left_is_false = false;
1876 1269144 : for(node* n = left->h; n; n=n->next) {
1877 662695 : if (exp_is_false(n->data)) {
1878 : left_is_false=true;
1879 : break;
1880 : }
1881 : }
1882 :
1883 606838 : if (!left_is_false) {
1884 : return false;
1885 : }
1886 :
1887 733 : for(node* n = right->h; n; n=n->next) {
1888 409 : if (exp_is_false(n->data)) {
1889 : return true;
1890 : }
1891 : }
1892 :
1893 : return false;
1894 : }
1895 :
1896 : static inline bool
1897 7080782 : exp_cmp_exp_is_false(sql_exp* e) {
1898 7080782 : assert(e->type == e_cmp);
1899 :
1900 7080782 : switch (e->flag) {
1901 6133720 : case cmp_gt:
1902 : case cmp_gte:
1903 : case cmp_lte:
1904 : case cmp_lt:
1905 : case cmp_equal:
1906 : case cmp_notequal:
1907 6133720 : return exp_regular_cmp_exp_is_false(e);
1908 606838 : case cmp_or:
1909 606838 : return exp_or_exp_is_false(e);
1910 : default:
1911 : return false;
1912 : }
1913 : }
1914 :
1915 : int
1916 7159505 : exp_is_false(sql_exp *e)
1917 : {
1918 7159505 : if (e->type == e_atom && e->l)
1919 34064 : return atom_is_false(e->l);
1920 7125441 : else if (e->type == e_cmp)
1921 7080782 : return exp_cmp_exp_is_false(e);
1922 : return 0;
1923 : }
1924 :
1925 : int
1926 17610 : exp_is_zero(sql_exp *e)
1927 : {
1928 17610 : if (e->type == e_atom && e->l)
1929 17309 : return atom_is_zero(e->l);
1930 : return 0;
1931 : }
1932 :
1933 : int
1934 379389 : exp_is_not_null(sql_exp *e)
1935 : {
1936 379847 : if (!has_nil(e))
1937 : return true;
1938 :
1939 228891 : switch (e->type) {
1940 2847 : case e_atom:
1941 2847 : if (e->f) /* values list */
1942 : return false;
1943 2847 : if (e->l)
1944 2647 : return !(atom_null(e->l));
1945 : return false;
1946 458 : case e_convert:
1947 458 : return exp_is_not_null(e->l);
1948 826 : case e_func:
1949 826 : if (!is_semantics(e) && e->l) {
1950 435 : list *l = e->l;
1951 485 : for (node *n = l->h; n; n=n->next) {
1952 485 : sql_exp *p = n->data;
1953 485 : if (!exp_is_not_null(p))
1954 : return false;
1955 : }
1956 : return true;
1957 : }
1958 : return false;
1959 : case e_aggr:
1960 : case e_column:
1961 : case e_cmp:
1962 : case e_psm:
1963 : return false;
1964 : }
1965 : return false;
1966 : }
1967 :
1968 : int
1969 12967495 : exp_is_null(sql_exp *e )
1970 : {
1971 13049300 : if (!has_nil(e))
1972 : return false;
1973 :
1974 7457172 : switch (e->type) {
1975 205615 : case e_atom:
1976 205615 : if (e->f) /* values list */
1977 : return 0;
1978 205615 : if (e->l)
1979 127181 : return (atom_null(e->l));
1980 : return 0;
1981 81805 : case e_convert:
1982 81805 : return exp_is_null(e->l);
1983 174273 : case e_func:
1984 174273 : if (!is_semantics(e) && e->l) {
1985 : /* This is a call to a function with no-nil semantics.
1986 : * If one of the parameters is null the expression itself is null
1987 : */
1988 161038 : list* l = e->l;
1989 480590 : for(node* n = l->h; n; n=n->next) {
1990 319713 : sql_exp* p = n->data;
1991 319713 : if (exp_is_null(p)) {
1992 : return true;
1993 : }
1994 : }
1995 : }
1996 : return 0;
1997 : case e_aggr:
1998 : case e_column:
1999 : case e_cmp:
2000 : case e_psm:
2001 : return 0;
2002 : }
2003 : return 0;
2004 : }
2005 :
2006 : int
2007 1847964 : exp_is_rel( sql_exp *e )
2008 : {
2009 1857432 : if (e) {
2010 1857432 : switch(e->type){
2011 9468 : case e_convert:
2012 9468 : return exp_is_rel(e->l);
2013 343370 : case e_psm:
2014 343370 : return e->flag == PSM_REL && e->l;
2015 : default:
2016 : return 0;
2017 : }
2018 : }
2019 : return 0;
2020 : }
2021 :
2022 : int
2023 7470 : exps_one_is_rel(list *exps)
2024 : {
2025 7470 : if (list_empty(exps))
2026 : return 0;
2027 22309 : for(node *n = exps->h ; n ; n = n->next)
2028 14847 : if (exp_is_rel(n->data))
2029 : return 1;
2030 : return 0;
2031 : }
2032 :
2033 : int
2034 8684125 : exp_is_atom( sql_exp *e )
2035 : {
2036 9026186 : switch (e->type) {
2037 1224319 : case e_atom:
2038 1224319 : if (e->f) /* values list */
2039 : return 0;
2040 : return 1;
2041 342061 : case e_convert:
2042 342061 : return exp_is_atom(e->l);
2043 1248416 : case e_func:
2044 : case e_aggr:
2045 1248416 : return e->card == CARD_ATOM && exps_are_atoms(e->l);
2046 2762 : case e_cmp:
2047 2762 : if (e->card != CARD_ATOM)
2048 : return 0;
2049 140 : if (e->flag == cmp_or || e->flag == cmp_filter)
2050 79 : return exps_are_atoms(e->l) && exps_are_atoms(e->r);
2051 65 : if (e->flag == cmp_in || e->flag == cmp_notin)
2052 0 : return exp_is_atom(e->l) && exps_are_atoms(e->r);
2053 65 : return exp_is_atom(e->l) && exp_is_atom(e->r) && (!e->f || exp_is_atom(e->f));
2054 : case e_column:
2055 : case e_psm:
2056 : return 0;
2057 : }
2058 : return 0;
2059 : }
2060 :
2061 : int
2062 30712778 : exp_has_rel( sql_exp *e )
2063 : {
2064 31348782 : if (!e)
2065 : return 0;
2066 31347235 : switch(e->type){
2067 4534356 : case e_func:
2068 : case e_aggr:
2069 4534356 : return exps_have_rel_exp(e->l);
2070 682057 : case e_cmp:
2071 682057 : if (e->flag == cmp_or || e->flag == cmp_filter) {
2072 75225 : return (exps_have_rel_exp(e->l) || exps_have_rel_exp(e->r));
2073 607311 : } else if (e->flag == cmp_in || e->flag == cmp_notin) {
2074 37265 : return (exp_has_rel(e->l) || exps_have_rel_exp(e->r));
2075 : } else {
2076 1140092 : return (exp_has_rel(e->l) || exp_has_rel(e->r) || (e->f && exp_has_rel(e->f)));
2077 : }
2078 636004 : case e_convert:
2079 636004 : return exp_has_rel(e->l);
2080 170287 : case e_psm:
2081 170287 : return exp_is_rel(e);
2082 13620173 : case e_atom:
2083 13620173 : return (e->f && exps_have_rel_exp(e->f));
2084 : case e_column:
2085 : return 0;
2086 : }
2087 : return 0;
2088 : }
2089 :
2090 : int
2091 5290679 : exps_have_rel_exp( list *exps)
2092 : {
2093 5290679 : if (list_empty(exps))
2094 : return 0;
2095 17868474 : for(node *n=exps->h; n; n=n->next) {
2096 12706620 : sql_exp *e = n->data;
2097 :
2098 12706620 : if (exp_has_rel(e))
2099 : return 1;
2100 : }
2101 : return 0;
2102 : }
2103 :
2104 : static sql_rel *
2105 562 : exps_rel_get_rel(allocator *sa, list *exps )
2106 : {
2107 562 : sql_rel *r = NULL, *xp = NULL;
2108 :
2109 562 : if (list_empty(exps))
2110 : return NULL;
2111 1655 : for (node *n = exps->h; n; n=n->next){
2112 1093 : sql_exp *e = n->data;
2113 :
2114 1093 : if (exp_has_rel(e)) {
2115 567 : if (!(r = exp_rel_get_rel(sa, e)))
2116 : return NULL;
2117 567 : if (xp) {
2118 5 : xp = rel_crossproduct(sa, xp, r, op_full);
2119 5 : set_processed(xp);
2120 : } else {
2121 : xp = r;
2122 : }
2123 : }
2124 : }
2125 : return xp;
2126 : }
2127 :
2128 : int
2129 47 : exp_rel_depth(sql_exp *e)
2130 : {
2131 47 : if (!e)
2132 : return 0;
2133 47 : switch(e->type){
2134 : case e_func:
2135 : case e_aggr:
2136 : case e_cmp:
2137 : return 1;
2138 : case e_convert:
2139 : return 0;
2140 33 : case e_psm:
2141 33 : if (exp_is_rel(e))
2142 : return 0;
2143 : return 1;
2144 : case e_atom:
2145 : case e_column:
2146 : return 0;
2147 : }
2148 : return 0;
2149 : }
2150 :
2151 : sql_rel *
2152 75451 : exp_rel_get_rel(allocator *sa, sql_exp *e)
2153 : {
2154 76366 : if (!e)
2155 : return NULL;
2156 :
2157 76366 : switch(e->type){
2158 534 : case e_func:
2159 : case e_aggr:
2160 534 : return exps_rel_get_rel(sa, e->l);
2161 37 : case e_cmp: {
2162 37 : sql_rel *r = NULL, *xp = NULL;
2163 :
2164 37 : if (e->flag == cmp_or || e->flag == cmp_filter) {
2165 12 : if (exps_have_rel_exp(e->l))
2166 6 : xp = exps_rel_get_rel(sa, e->l);
2167 12 : if (exps_have_rel_exp(e->r)) {
2168 7 : if (!(r = exps_rel_get_rel(sa, e->r)))
2169 : return NULL;
2170 7 : if (xp) {
2171 1 : xp = rel_crossproduct(sa, xp, r, op_join);
2172 1 : set_processed(xp);
2173 : } else {
2174 : xp = r;
2175 : }
2176 : }
2177 25 : } else if (e->flag == cmp_in || e->flag == cmp_notin) {
2178 0 : if (exp_has_rel(e->l))
2179 0 : xp = exp_rel_get_rel(sa, e->l);
2180 0 : if (exps_have_rel_exp(e->r)) {
2181 0 : if (!(r = exps_rel_get_rel(sa, e->r)))
2182 : return NULL;
2183 0 : if (xp) {
2184 0 : xp = rel_crossproduct(sa, xp, r, op_join);
2185 0 : set_processed(xp);
2186 : } else {
2187 : xp = r;
2188 : }
2189 : }
2190 : } else {
2191 25 : if (exp_has_rel(e->l))
2192 23 : xp = exp_rel_get_rel(sa, e->l);
2193 25 : if (exp_has_rel(e->r)) {
2194 7 : if (!(r = exp_rel_get_rel(sa, e->r)))
2195 : return NULL;
2196 7 : if (xp) {
2197 5 : xp = rel_crossproduct(sa, xp, r, op_join);
2198 5 : set_processed(xp);
2199 : } else {
2200 : xp = r;
2201 : }
2202 : }
2203 25 : if (e->f && exp_has_rel(e->f)) {
2204 0 : if (!(r = exp_rel_get_rel(sa, e->f)))
2205 : return NULL;
2206 0 : if (xp) {
2207 0 : xp = rel_crossproduct(sa, xp, r, op_join);
2208 0 : set_processed(xp);
2209 : } else {
2210 : xp = r;
2211 : }
2212 : }
2213 : }
2214 : return xp;
2215 : }
2216 915 : case e_convert:
2217 915 : return exp_rel_get_rel(sa, e->l);
2218 74865 : case e_psm:
2219 74865 : if (exp_is_rel(e))
2220 74865 : return e->l;
2221 : return NULL;
2222 15 : case e_atom:
2223 15 : if (e->f && exps_have_rel_exp(e->f))
2224 15 : return exps_rel_get_rel(sa, e->f);
2225 : return NULL;
2226 : case e_column:
2227 : return NULL;
2228 : }
2229 : return NULL;
2230 : }
2231 :
2232 : static void exp_rel_update_set_freevar(sql_exp *e);
2233 :
2234 : static void
2235 921 : exps_rel_update_set_freevar(list *exps)
2236 : {
2237 921 : if (!list_empty(exps))
2238 2995 : for (node *n=exps->h; n ; n=n->next)
2239 2074 : exp_rel_update_set_freevar(n->data);
2240 921 : }
2241 :
2242 : static void
2243 2363 : exp_rel_update_set_freevar(sql_exp *e)
2244 : {
2245 2373 : if (!e)
2246 : return ;
2247 :
2248 2373 : switch(e->type){
2249 918 : case e_func:
2250 : case e_aggr:
2251 918 : exps_rel_update_set_freevar(e->l);
2252 918 : break;
2253 8 : case e_cmp:
2254 8 : if (e->flag == cmp_or || e->flag == cmp_filter) {
2255 0 : exps_rel_update_set_freevar(e->l);
2256 0 : exps_rel_update_set_freevar(e->r);
2257 8 : } else if (e->flag == cmp_in || e->flag == cmp_notin) {
2258 0 : exp_rel_update_set_freevar(e->l);
2259 0 : exps_rel_update_set_freevar(e->r);
2260 : } else {
2261 8 : exp_rel_update_set_freevar(e->l);
2262 8 : exp_rel_update_set_freevar(e->r);
2263 8 : if (e->f)
2264 : exp_rel_update_set_freevar(e->f);
2265 : }
2266 : break;
2267 7 : case e_convert:
2268 7 : exp_rel_update_set_freevar(e->l);
2269 7 : break;
2270 1160 : case e_atom:
2271 1160 : if (e->f)
2272 3 : exps_rel_update_set_freevar(e->f);
2273 : break;
2274 280 : case e_column:
2275 280 : set_freevar(e, 1);
2276 280 : break;
2277 : case e_psm:
2278 : break;
2279 : }
2280 : }
2281 :
2282 : static list *
2283 562 : exp_rel_update_exps(mvc *sql, list *exps, bool up)
2284 : {
2285 562 : if (list_empty(exps))
2286 : return exps;
2287 1655 : for (node *n = exps->h; n; n=n->next){
2288 1093 : sql_exp *e = n->data;
2289 :
2290 1093 : if (exp_has_rel(e))
2291 567 : n->data = exp_rel_update_exp(sql, e, up);
2292 526 : else if (!exp_is_atom(e) && !up)
2293 267 : exp_rel_update_set_freevar(e);
2294 : }
2295 : return exps;
2296 : }
2297 :
2298 : static sql_exp *
2299 53 : exp_rel_update_exp_(mvc *sql, sql_exp *e, bool up)
2300 : {
2301 53 : if (exp_has_rel(e))
2302 29 : e = exp_rel_update_exp(sql, e, up);
2303 24 : else if (!exp_is_atom(e) && !up)
2304 6 : exp_rel_update_set_freevar(e);
2305 53 : return e;
2306 : }
2307 :
2308 : sql_exp *
2309 13098 : exp_rel_update_exp(mvc *sql, sql_exp *e, bool up)
2310 : {
2311 13098 : if (!e)
2312 : return NULL;
2313 :
2314 13098 : switch(e->type){
2315 534 : case e_func:
2316 : case e_aggr:
2317 534 : if (exps_have_rel_exp(e->l))
2318 534 : e->l = exp_rel_update_exps(sql, e->l, up);
2319 : return e;
2320 36 : case e_cmp:
2321 36 : if (e->flag == cmp_or || e->flag == cmp_filter) {
2322 12 : if (exps_have_rel_exp(e->l))
2323 6 : e->l = exp_rel_update_exps(sql, e->l, up);
2324 12 : if (exps_have_rel_exp(e->r))
2325 7 : e->r = exp_rel_update_exps(sql, e->r, up);
2326 24 : } else if (e->flag == cmp_in || e->flag == cmp_notin) {
2327 0 : if (exp_has_rel(e->l))
2328 0 : e->l = exp_rel_update_exp(sql, e->l, up);
2329 0 : if (exps_have_rel_exp(e->r))
2330 0 : e->r = exp_rel_update_exps(sql, e->r, up);
2331 : } else {
2332 : //if (exp_has_rel(e->l))
2333 24 : e->l = exp_rel_update_exp_(sql, e->l, up);
2334 : //if (exp_has_rel(e->r))
2335 24 : e->r = exp_rel_update_exp_(sql, e->r, up);
2336 24 : if (e->f /*&& exp_has_rel(e->f)*/)
2337 5 : e->f = exp_rel_update_exp_(sql, e->f, up);
2338 : }
2339 : return e;
2340 915 : case e_convert:
2341 915 : if (exp_has_rel(e->l))
2342 915 : e->l = exp_rel_update_exp(sql, e->l, up);
2343 : return e;
2344 11598 : case e_psm:
2345 11598 : if (exp_is_rel(e)) {
2346 11598 : sql_rel *r = exp_rel_get_rel(sql->sa, e), *nr = r;
2347 11598 : if (is_topn(r->op)) {
2348 2 : nr = r->l;
2349 2 : if (nr && !is_project(nr->op))
2350 0 : r->l = nr = rel_project(sql->sa, nr, rel_projections(sql, nr, NULL, 1, 0));
2351 : }
2352 11598 : e = nr->exps->t->data;
2353 11598 : e = exp_ref(sql, e);
2354 11598 : if (up)
2355 0 : set_freevar(e, 1);
2356 11598 : return e;
2357 : }
2358 : return e;
2359 15 : case e_atom:
2360 15 : if (e->f && exps_have_rel_exp(e->f))
2361 15 : e->f = exp_rel_update_exps(sql, e->f, up);
2362 : return e;
2363 : case e_column:
2364 : return e;
2365 : }
2366 : return e;
2367 : }
2368 :
2369 : sql_exp *
2370 3802 : exp_rel_label(mvc *sql, sql_exp *e)
2371 : {
2372 3802 : if (exp_is_rel(e))
2373 3802 : e->l = rel_label(sql, e->l, 1);
2374 3802 : return e;
2375 : }
2376 :
2377 : int
2378 167664 : exps_are_atoms( list *exps)
2379 : {
2380 167664 : int atoms = 1;
2381 167664 : if (!list_empty(exps))
2382 400434 : for(node *n=exps->h; n && atoms; n=n->next)
2383 283840 : atoms &= exp_is_atom(n->data);
2384 167664 : return atoms;
2385 : }
2386 :
2387 : int
2388 91 : exps_have_func(list *exps)
2389 : {
2390 91 : if (list_empty(exps))
2391 : return 0;
2392 141 : for(node *n=exps->h; n; n=n->next) {
2393 106 : sql_exp *e = n->data;
2394 :
2395 106 : if (exp_has_func(e))
2396 : return 1;
2397 : }
2398 : return 0;
2399 : }
2400 :
2401 : static int exp_has_func_or_cmp(sql_exp *e, bool cmp);
2402 :
2403 : static int
2404 64633 : exps_have_func_or_cmp(list *exps, bool cmp)
2405 : {
2406 64633 : if (list_empty(exps))
2407 : return 0;
2408 188842 : for(node *n=exps->h; n; n=n->next) {
2409 133499 : sql_exp *e = n->data;
2410 :
2411 133499 : if (exp_has_func_or_cmp(e, cmp))
2412 : return 1;
2413 : }
2414 : return 0;
2415 : }
2416 :
2417 : static int
2418 2119350 : exp_has_func_or_cmp(sql_exp *e, bool cmp)
2419 : {
2420 2119350 : if (!e)
2421 : return 0;
2422 2119350 : switch (e->type) {
2423 314641 : case e_atom:
2424 314641 : if (e->f)
2425 0 : return exps_have_func_or_cmp(e->f, true);
2426 : return 0;
2427 15528 : case e_convert:
2428 15528 : return exp_has_func_or_cmp(e->l, cmp);
2429 : case e_func:
2430 : return 1;
2431 15885 : case e_aggr:
2432 15885 : if (e->l)
2433 13349 : return exps_have_func_or_cmp(e->l, true);
2434 : return 0;
2435 277096 : case e_cmp:
2436 277096 : if (cmp)
2437 : return 1;
2438 267898 : if (e->flag == cmp_or || e->flag == cmp_filter) {
2439 22067 : return (exps_have_func_or_cmp(e->l, true) || exps_have_func_or_cmp(e->r, true));
2440 255056 : } else if (e->flag == cmp_in || e->flag == cmp_notin) {
2441 34868 : return (exp_has_func_or_cmp(e->l, true) || exps_have_func_or_cmp(e->r, true));
2442 : } else {
2443 440470 : return (exp_has_func_or_cmp(e->l, true) || exp_has_func_or_cmp(e->r, true) ||
2444 204499 : (e->f && exp_has_func_or_cmp(e->f, true)));
2445 : }
2446 : case e_column:
2447 : case e_psm:
2448 : return 0;
2449 : }
2450 : return 0;
2451 : }
2452 :
2453 : int
2454 1508027 : exp_has_func(sql_exp *e)
2455 : {
2456 1508027 : return exp_has_func_or_cmp(e, false);
2457 : }
2458 :
2459 : static int
2460 741557 : exps_has_sideeffect( list *exps)
2461 : {
2462 741557 : node *n;
2463 741557 : int has_sideeffect = 0;
2464 :
2465 2273424 : for(n=exps->h; n && !has_sideeffect; n=n->next)
2466 1531867 : has_sideeffect |= exp_has_sideeffect(n->data);
2467 741557 : return has_sideeffect;
2468 : }
2469 :
2470 : int
2471 1720680 : exp_has_sideeffect( sql_exp *e )
2472 : {
2473 1745751 : switch (e->type) {
2474 25071 : case e_convert:
2475 25071 : return exp_has_sideeffect(e->l);
2476 741580 : case e_func:
2477 : {
2478 741580 : sql_subfunc *f = e->f;
2479 :
2480 741580 : if (f->func->side_effect)
2481 : return 1;
2482 741566 : if (e->l)
2483 741557 : return exps_has_sideeffect(e->l);
2484 : return 0;
2485 : }
2486 483000 : case e_atom:
2487 483000 : if (e->f)
2488 0 : return exps_has_sideeffect(e->f);
2489 : return 0;
2490 : case e_aggr:
2491 : case e_cmp:
2492 : case e_column:
2493 : case e_psm:
2494 : return 0;
2495 : }
2496 : return 0;
2497 : }
2498 :
2499 : int
2500 1007306 : exps_have_unsafe(list *exps, int allow_identity)
2501 : {
2502 1007306 : int unsafe = 0;
2503 :
2504 1007306 : if (list_empty(exps))
2505 : return 0;
2506 3500237 : for (node *n = exps->h; n && !unsafe; n = n->next)
2507 2500601 : unsafe |= exp_unsafe(n->data, allow_identity);
2508 : return unsafe;
2509 : }
2510 :
2511 : int
2512 11644602 : exp_unsafe(sql_exp *e, int allow_identity)
2513 : {
2514 12356328 : switch (e->type) {
2515 711726 : case e_convert:
2516 711726 : return exp_unsafe(e->l, allow_identity);
2517 1069411 : case e_aggr:
2518 : case e_func: {
2519 1069411 : sql_subfunc *f = e->f;
2520 :
2521 1069411 : if (IS_ANALYTIC(f->func) || !LANG_INT_OR_MAL(f->func->lang) || f->func->side_effect || (!allow_identity && is_identity(e, NULL)))
2522 120320 : return 1;
2523 949091 : return exps_have_unsafe(e->l, allow_identity);
2524 644 : } break;
2525 644 : case e_cmp: {
2526 644 : if (e->flag == cmp_in || e->flag == cmp_notin) {
2527 0 : return exp_unsafe(e->l, allow_identity) || exps_have_unsafe(e->r, allow_identity);
2528 644 : } else if (e->flag == cmp_or || e->flag == cmp_filter) {
2529 324 : return exps_have_unsafe(e->l, allow_identity) || exps_have_unsafe(e->r, allow_identity);
2530 : } else {
2531 640 : return exp_unsafe(e->l, allow_identity) || exp_unsafe(e->r, allow_identity) || (e->f && exp_unsafe(e->f, allow_identity));
2532 : }
2533 937634 : } break;
2534 937634 : case e_atom: {
2535 937634 : if (e->f)
2536 0 : return exps_have_unsafe(e->f, allow_identity);
2537 : return 0;
2538 : } break;
2539 : case e_column:
2540 : case e_psm:
2541 : return 0;
2542 : }
2543 : return 0;
2544 : }
2545 :
2546 : static inline int
2547 9931637 : exp_key( sql_exp *e )
2548 : {
2549 9931637 : if (e->alias.name)
2550 9931305 : return hash_key(e->alias.name);
2551 : return 0;
2552 : }
2553 :
2554 : sql_exp *
2555 3534869 : exps_bind_column(list *exps, const char *cname, int *ambiguous, int *multiple, int no_tname)
2556 : {
2557 3534869 : sql_exp *res = NULL;
2558 :
2559 3534869 : if (exps && cname) {
2560 3533894 : node *en;
2561 :
2562 3533894 : if (exps) {
2563 3533894 : if (!exps->ht && list_length(exps) > HASH_MIN_SIZE) {
2564 127839 : exps->ht = hash_new(exps->sa, list_length(exps), (fkeyvalue)&exp_key);
2565 127839 : if (exps->ht == NULL)
2566 : return NULL;
2567 1210261 : for (en = exps->h; en; en = en->next ) {
2568 1082422 : sql_exp *e = en->data;
2569 1082422 : if (e->alias.name) {
2570 1082422 : int key = exp_key(e);
2571 :
2572 1082422 : if (hash_add(exps->ht, key, e) == NULL)
2573 : return NULL;
2574 : }
2575 : }
2576 : }
2577 3533894 : if (exps->ht) {
2578 2640145 : int key = hash_key(cname);
2579 2640145 : sql_hash_e *he = exps->ht->buckets[key&(exps->ht->size-1)];
2580 :
2581 6216018 : for (; he; he = he->chain) {
2582 3575877 : sql_exp *e = he->value;
2583 :
2584 3575877 : if (e->alias.name && strcmp(e->alias.name, cname) == 0 && (!no_tname || !e->alias.rname)) {
2585 1365133 : if (res && multiple)
2586 4 : *multiple = 1;
2587 1365133 : if (!res)
2588 1365133 : res = e;
2589 :
2590 1365133 : if (res && res != e && e->alias.rname && res->alias.rname && strcmp(e->alias.rname, res->alias.rname) != 0 ) {
2591 4 : if (ambiguous)
2592 4 : *ambiguous = 1;
2593 4 : return NULL;
2594 : }
2595 : res = e;
2596 : }
2597 : }
2598 2640141 : return res;
2599 : }
2600 : }
2601 3052192 : for (en = exps->h; en; en = en->next ) {
2602 2158448 : sql_exp *e = en->data;
2603 :
2604 2158448 : if (e->alias.name && strcmp(e->alias.name, cname) == 0 && (!no_tname || !e->alias.rname)) {
2605 83864 : if (res && multiple)
2606 8 : *multiple = 1;
2607 83864 : if (!res)
2608 83864 : res = e;
2609 :
2610 83864 : if (res && res != e && e->alias.rname && res->alias.rname && strcmp(e->alias.rname, res->alias.rname) != 0 ) {
2611 5 : if (ambiguous)
2612 5 : *ambiguous = 1;
2613 5 : return NULL;
2614 : }
2615 : res = e;
2616 : }
2617 : }
2618 : }
2619 : return res;
2620 : }
2621 :
2622 : sql_exp *
2623 67095743 : exps_bind_column2(list *exps, const char *rname, const char *cname, int *multiple)
2624 : {
2625 67095743 : sql_exp *res = NULL;
2626 :
2627 67095743 : if (exps) {
2628 66859667 : node *en;
2629 :
2630 66859667 : if (!exps->ht && list_length(exps) > HASH_MIN_SIZE) {
2631 596573 : exps->ht = hash_new(exps->sa, list_length(exps), (fkeyvalue)&exp_key);
2632 596573 : if (exps->ht == NULL)
2633 : return res;
2634 :
2635 7829157 : for (en = exps->h; en; en = en->next ) {
2636 7232588 : sql_exp *e = en->data;
2637 7232588 : if (e->alias.name) {
2638 7221520 : int key = exp_key(e);
2639 :
2640 7221521 : if (hash_add(exps->ht, key, e) == NULL)
2641 : return res;
2642 : }
2643 : }
2644 : }
2645 66859662 : if (exps->ht) {
2646 30851536 : int key = hash_key(cname);
2647 30851536 : sql_hash_e *he = exps->ht->buckets[key&(exps->ht->size-1)];
2648 :
2649 76407219 : for (; he; he = he->chain) {
2650 46247456 : sql_exp *e = he->value;
2651 :
2652 46247456 : if (e && e->alias.name && e->alias.rname && strcmp(e->alias.name, cname) == 0 && strcmp(e->alias.rname, rname) == 0) {
2653 18481036 : if (res && multiple)
2654 0 : *multiple = 1;
2655 18481036 : if (!res)
2656 : res = e;
2657 18481036 : if (res && res->alias.label) /* aliases maybe used multiple times without problems */
2658 691773 : return res;
2659 : }
2660 : }
2661 30159763 : return res;
2662 : }
2663 152199294 : for (en = exps->h; en; en = en->next ) {
2664 117467318 : sql_exp *e = en->data;
2665 :
2666 117467318 : if (e && e->alias.name && e->alias.rname && strcmp(e->alias.name, cname) == 0 && strcmp(e->alias.rname, rname) == 0) {
2667 8082194 : if (res && multiple)
2668 2 : *multiple = 1;
2669 8082194 : if (!res)
2670 : res = e;
2671 8082194 : if (res && res->alias.label) /* aliases maybe used multiple times without problems */
2672 1276150 : return res;
2673 : }
2674 : }
2675 : }
2676 : return res;
2677 : }
2678 :
2679 : /* find an column based on the original name, not the alias it got */
2680 : sql_exp *
2681 335 : exps_bind_alias( list *exps, const char *rname, const char *cname )
2682 : {
2683 335 : if (exps) {
2684 335 : node *en;
2685 :
2686 750 : for (en = exps->h; en; en = en->next ) {
2687 419 : sql_exp *e = en->data;
2688 :
2689 419 : if (e && is_column(e->type) && !rname && e->r && strcmp(e->r, cname) == 0)
2690 0 : return e;
2691 419 : if (e && e->type == e_column && rname && e->l && e->r && strcmp(e->r, cname) == 0 && strcmp(e->l, rname) == 0) {
2692 4 : return e;
2693 : }
2694 : }
2695 : }
2696 : return NULL;
2697 : }
2698 :
2699 : unsigned int
2700 3273462 : exps_card( list *l )
2701 : {
2702 3273462 : node *n;
2703 3273462 : unsigned int card = CARD_ATOM;
2704 :
2705 14474180 : if (l) for(n = l->h; n; n = n->next) {
2706 11200718 : sql_exp *e = n->data;
2707 :
2708 11200718 : if (e && card < e->card)
2709 11200718 : card = e->card;
2710 : }
2711 3273462 : return card;
2712 : }
2713 :
2714 : void
2715 40754 : exps_fix_card( list *exps, unsigned int card)
2716 : {
2717 40754 : if (exps)
2718 991824 : for (node *n = exps->h; n; n = n->next) {
2719 951070 : sql_exp *e = n->data;
2720 :
2721 951070 : if (e && e->card > card)
2722 0 : e->card = card;
2723 : }
2724 40754 : }
2725 :
2726 : void
2727 4114 : exps_setcard( list *exps, unsigned int card)
2728 : {
2729 4114 : if (exps)
2730 24097 : for (node *n = exps->h; n; n = n->next) {
2731 19983 : sql_exp *e = n->data;
2732 :
2733 19983 : if (e && e->card != CARD_ATOM)
2734 19950 : e->card = card;
2735 : }
2736 4114 : }
2737 :
2738 : int
2739 0 : exps_intern(list *exps)
2740 : {
2741 0 : if (exps)
2742 0 : for (node *n=exps->h; n; n = n->next) {
2743 0 : sql_exp *e = n->data;
2744 :
2745 0 : if (is_intern(e))
2746 : return 1;
2747 : }
2748 : return 0;
2749 : }
2750 :
2751 : sql_exp *
2752 3866 : exps_find_one_multi_exp(list *exps)
2753 : {
2754 3866 : sql_exp *l = NULL;
2755 3866 : int skip = 0;
2756 :
2757 : /* Find one and only 1 expression with card > CARD_ATOM */
2758 3866 : if (!list_empty(exps)) {
2759 7917 : for (node *m = exps->h ; m && !skip ; m = m->next) {
2760 4051 : sql_exp *e = m->data;
2761 :
2762 4051 : if (e->card > CARD_ATOM) {
2763 3832 : skip |= l != NULL;
2764 3832 : l = e;
2765 : }
2766 : }
2767 : }
2768 3866 : if (skip)
2769 4 : l = NULL;
2770 3866 : return l;
2771 : }
2772 :
2773 : const char *
2774 129845 : compare_func( comp_type t, int anti )
2775 : {
2776 129845 : switch(t) {
2777 74970 : case cmp_equal:
2778 74970 : return anti?"<>":"=";
2779 8112 : case cmp_lt:
2780 8112 : return anti?">":"<";
2781 2121 : case cmp_lte:
2782 2121 : return anti?">=":"<=";
2783 1157 : case cmp_gte:
2784 1157 : return anti?"<=":">=";
2785 31414 : case cmp_gt:
2786 31414 : return anti?"<":">";
2787 12071 : case cmp_notequal:
2788 12071 : return anti?"=":"<>";
2789 : default:
2790 : return NULL;
2791 : }
2792 : }
2793 :
2794 : int
2795 9930025 : is_identity( sql_exp *e, sql_rel *r)
2796 : {
2797 9941829 : switch(e->type) {
2798 37371 : case e_column:
2799 37371 : if (r && is_project(r->op)) {
2800 13999 : sql_exp *re = NULL;
2801 13999 : if (e->l)
2802 13888 : re = exps_bind_column2(r->exps, e->l, e->r, NULL);
2803 13999 : if (!re && has_label(e))
2804 263 : re = exps_bind_column(r->exps, e->r, NULL, NULL, 1);
2805 2195 : if (re)
2806 11804 : return is_identity(re, r->l);
2807 : }
2808 : return 0;
2809 9898457 : case e_func: {
2810 9898457 : sql_subfunc *f = e->f;
2811 9898457 : return !f->func->s && strcmp(f->func->base.name, "identity") == 0;
2812 : }
2813 : default:
2814 : return 0;
2815 : }
2816 : }
2817 :
2818 : list *
2819 144 : exps_alias(mvc *sql, list *exps)
2820 : {
2821 144 : list *nl = new_exp_list(sql->sa);
2822 :
2823 144 : if (exps)
2824 882 : for (node *n = exps->h; n; n = n->next) {
2825 738 : sql_exp *e = n->data, *ne;
2826 :
2827 738 : assert(exp_name(e));
2828 738 : ne = exp_ref(sql, e);
2829 738 : append(nl, ne);
2830 : }
2831 144 : return nl;
2832 : }
2833 :
2834 : list *
2835 134025 : exps_copy(mvc *sql, list *exps)
2836 : {
2837 134025 : list *nl;
2838 :
2839 134025 : if (mvc_highwater(sql))
2840 0 : return sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
2841 :
2842 134025 : if (!exps)
2843 : return NULL;
2844 117209 : nl = new_exp_list(sql->sa);
2845 522457 : for (node *n = exps->h; n; n = n->next) {
2846 405248 : sql_exp *arg = n->data;
2847 :
2848 405248 : arg = exp_copy(sql, arg);
2849 405248 : if (!arg)
2850 : return NULL;
2851 405248 : append(nl, arg);
2852 : }
2853 : return nl;
2854 : }
2855 :
2856 : sql_exp *
2857 666058 : exp_copy(mvc *sql, sql_exp * e)
2858 : {
2859 666058 : sql_exp *l, *r, *r2, *ne = NULL;
2860 :
2861 666058 : if (mvc_highwater(sql))
2862 0 : return sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
2863 :
2864 666058 : if (!e)
2865 : return NULL;
2866 666058 : switch(e->type){
2867 408315 : case e_column:
2868 408315 : ne = exp_column(sql->sa, e->l, e->r, exp_subtype(e), e->card, has_nil(e), is_unique(e), is_intern(e));
2869 408315 : ne->flag = e->flag;
2870 408315 : break;
2871 42523 : case e_cmp:
2872 42523 : if (e->flag == cmp_or || e->flag == cmp_filter) {
2873 3362 : list *l = exps_copy(sql, e->l);
2874 3362 : list *r = exps_copy(sql, e->r);
2875 :
2876 3362 : if (e->flag == cmp_filter)
2877 612 : ne = exp_filter(sql->sa, l, r, e->f, is_anti(e));
2878 : else
2879 2750 : ne = exp_or(sql->sa, l, r, is_anti(e));
2880 39161 : } else if (e->flag == cmp_in || e->flag == cmp_notin) {
2881 1264 : sql_exp *l = exp_copy(sql, e->l);
2882 1264 : list *r = exps_copy(sql, e->r);
2883 :
2884 1264 : ne = exp_in(sql->sa, l, r, e->flag);
2885 : } else {
2886 37897 : l = exp_copy(sql, e->l);
2887 37897 : r = exp_copy(sql, e->r);
2888 :
2889 37897 : if (e->f) {
2890 664 : r2 = exp_copy(sql, e->f);
2891 664 : ne = exp_compare2(sql->sa, l, r, r2, e->flag, is_symmetric(e));
2892 : } else {
2893 37233 : ne = exp_compare(sql->sa, l, r, e->flag);
2894 : }
2895 : }
2896 : break;
2897 27361 : case e_convert:
2898 27361 : ne = exp_convert(sql->sa, exp_copy(sql, e->l), exp_fromtype(e), exp_totype(e));
2899 27361 : break;
2900 12913 : case e_aggr:
2901 : case e_func: {
2902 12913 : list *l = exps_copy(sql, e->l);
2903 :
2904 12913 : if (e->type == e_func)
2905 11140 : ne = exp_op(sql->sa, l, e->f);
2906 : else
2907 1773 : ne = exp_aggr(sql->sa, l, e->f, need_distinct(e), need_no_nil(e), e->card, has_nil(e));
2908 12913 : if (e->r) { /* copy obe and gbe lists */
2909 2 : list *er = (list*) e->r;
2910 2 : assert(list_length(er) <= 2);
2911 2 : if (list_length(er) == 2)
2912 0 : ne->r = list_append(list_append(sa_list(sql->sa), exps_copy(sql, er->h->data)), exps_copy(sql, er->h->next->data));
2913 : else
2914 2 : ne->r = list_append(sa_list(sql->sa), exps_copy(sql, er->h->data));
2915 : }
2916 : break;
2917 : }
2918 174942 : case e_atom:
2919 174942 : if (e->l)
2920 170944 : ne = exp_atom(sql->sa, e->l);
2921 3998 : else if (e->r) {
2922 3937 : sql_var_name *vname = (sql_var_name*) e->r;
2923 3937 : ne = exp_param_or_declared(sql->sa, vname->sname, vname->name, &e->tpe, e->flag);
2924 61 : } else if (e->f)
2925 3 : ne = exp_values(sql->sa, exps_copy(sql, e->f));
2926 : else
2927 58 : ne = exp_atom_ref(sql->sa, e->flag, &e->tpe);
2928 : break;
2929 4 : case e_psm:
2930 4 : if (e->flag & PSM_SET) {
2931 0 : ne = exp_set(sql->sa, e->alias.rname, e->alias.name, exp_copy(sql, e->l), GET_PSM_LEVEL(e->flag));
2932 4 : } else if (e->flag & PSM_VAR) {
2933 0 : if (e->f)
2934 0 : ne = exp_table(sql->sa, e->alias.name, e->f, GET_PSM_LEVEL(e->flag));
2935 : else
2936 0 : ne = exp_var(sql->sa, e->alias.rname, e->alias.name, &e->tpe, GET_PSM_LEVEL(e->flag));
2937 4 : } else if (e->flag & PSM_RETURN) {
2938 0 : ne = exp_return(sql->sa, exp_copy(sql, e->l), GET_PSM_LEVEL(e->flag));
2939 4 : } else if (e->flag & PSM_WHILE) {
2940 0 : ne = exp_while(sql->sa, exp_copy(sql, e->l), exps_copy(sql, e->r));
2941 4 : } else if (e->flag & PSM_IF) {
2942 0 : ne = exp_if(sql->sa, exp_copy(sql, e->l), exps_copy(sql, e->r), exps_copy(sql, e->f));
2943 4 : } else if (e->flag & PSM_REL) {
2944 4 : return exp_ref(sql, e);
2945 0 : } else if (e->flag & PSM_EXCEPTION) {
2946 0 : ne = exp_exception(sql->sa, exp_copy(sql, e->l), (const char *) e->r);
2947 : }
2948 : break;
2949 : }
2950 666054 : if (!ne)
2951 0 : return ne;
2952 666054 : if (e->alias.name)
2953 481924 : exp_prop_alias(sql->sa, ne, e);
2954 666054 : ne = exp_propagate(sql->sa, ne, e);
2955 666054 : if (is_freevar(e))
2956 8305 : set_freevar(ne, is_freevar(e)-1);
2957 : return ne;
2958 : }
2959 :
2960 : /* scaling for the division operator */
2961 : static sql_exp *
2962 2606 : exp_scale_algebra(mvc *sql, sql_subfunc *f, sql_rel *rel, sql_exp *l, sql_exp *r)
2963 : {
2964 2606 : sql_subtype *lt = exp_subtype(l);
2965 2606 : sql_subtype *rt = exp_subtype(r);
2966 :
2967 2606 : if (lt->type->scale == SCALE_FIX && (lt->scale || rt->scale) &&
2968 344 : strcmp(sql_func_imp(f->func), "/") == 0) {
2969 173 : sql_subtype *res = f->res->h->data;
2970 173 : unsigned int scale, digits, digL, scaleL;
2971 173 : sql_subtype nlt;
2972 :
2973 : /* scale fixing may require a larger type ! */
2974 : /* TODO make '3' setable by user (division_minimal_scale or so) */
2975 173 : scaleL = (lt->scale < 3) ? 3 : lt->scale;
2976 173 : scaleL += (scaleL < rt->scale)?(rt->scale - scaleL):0;
2977 173 : scale = scaleL;
2978 173 : scaleL += rt->scale;
2979 173 : digL = lt->digits + (scaleL - lt->scale);
2980 173 : digits = (digL > rt->digits) ? digL : rt->digits;
2981 :
2982 : /* HACK alert: digits should be less than max */
2983 : #ifdef HAVE_HGE
2984 173 : if (res->type->radix == 10 && digits > 38)
2985 173 : digits = 38;
2986 173 : if (res->type->radix == 2 && digits > 127)
2987 173 : digits = 127;
2988 : #else
2989 : if (res->type->radix == 10 && digits > 18)
2990 : digits = 18;
2991 : if (res->type->radix == 2 && digits > 63)
2992 : digits = 63;
2993 : #endif
2994 :
2995 173 : sql_find_subtype(&nlt, lt->type->base.name, digL, scaleL);
2996 173 : if (nlt.digits < scaleL)
2997 2 : return sql_error(sql, 01, SQLSTATE(42000) "Scale (%d) overflows type", scaleL);
2998 171 : l = exp_check_type(sql, &nlt, rel, l, type_equal);
2999 :
3000 171 : sql_find_subtype(res, lt->type->base.name, digits, scale);
3001 2433 : } else if (lt->type->scale == SCALE_FIX) {
3002 2235 : sql_subtype *res = f->res->h->data;
3003 2235 : if (res->type->eclass == EC_NUM)
3004 2219 : res->digits = MAX(lt->digits, rt->digits);
3005 : }
3006 : return l;
3007 : }
3008 :
3009 : sql_exp *
3010 2606 : exps_scale_algebra(mvc *sql, sql_subfunc *f, sql_rel *rel, list *exps)
3011 : {
3012 2606 : if (list_length(exps) != 2)
3013 : return NULL;
3014 2606 : sql_exp *e = exp_scale_algebra(sql, f, rel, exps->h->data, exps->h->next->data);
3015 2606 : if (e)
3016 2604 : exps->h->data = e;
3017 : return e;
3018 : }
3019 :
3020 : void
3021 186987 : exps_digits_add(sql_subfunc *f, list *exps)
3022 : {
3023 : /* concat and friends need larger results */
3024 186987 : if (!f->func->res)
3025 : return;
3026 186987 : int digits = 0;
3027 333522 : for(node *n = exps->h; n; n = n->next) {
3028 272775 : sql_subtype *t = exp_subtype(n->data);
3029 :
3030 272775 : if (!t->digits) {
3031 : digits = 0;
3032 : break;
3033 : }
3034 146535 : digits += t->digits;
3035 : }
3036 186987 : sql_subtype *res = f->res->h->data;
3037 186987 : res->digits = digits;
3038 : }
3039 :
3040 : void
3041 25171 : exps_sum_scales(sql_subfunc *f, list *exps)
3042 : {
3043 : /* sum scales and digits for multiply operation */
3044 25171 : sql_arg *ares = f->func->res->h->data;
3045 :
3046 25171 : if (ares->type.type->scale == SCALE_FIX && strcmp(sql_func_imp(f->func), "*") == 0) {
3047 23874 : unsigned int digits = 0, scale = 0;
3048 23874 : sql_type *largesttype = ares->type.type;
3049 :
3050 71622 : for(node *n = exps->h; n; n = n->next) {
3051 47748 : sql_exp *e = n->data;
3052 47748 : sql_subtype *t = exp_subtype(e);
3053 :
3054 47748 : scale += t->scale;
3055 47748 : digits += t->digits;
3056 47748 : if (largesttype->localtype < t->type->localtype)
3057 0 : largesttype = t->type;
3058 : }
3059 23874 : sql_subtype *res = f->res->h->data;
3060 :
3061 23874 : res->scale = scale;
3062 23874 : res->digits = digits;
3063 :
3064 : /* HACK alert: digits should be less than max */
3065 : #ifdef HAVE_HGE
3066 23874 : if (ares->type.type->radix == 10 && res->digits > 38) {
3067 2465 : res->digits = 38;
3068 2465 : res->scale = MIN(res->scale, res->digits - 1);
3069 : }
3070 23874 : if (ares->type.type->radix == 2 && res->digits > 127) {
3071 24 : res->digits = 127;
3072 24 : res->scale = MIN(res->scale, res->digits - 1);
3073 : }
3074 : #else
3075 : if (ares->type.type->radix == 10 && res->digits > 18) {
3076 : res->digits = 18;
3077 : res->scale = MIN(res->scale, res->digits - 1);
3078 : }
3079 : if (ares->type.type->radix == 2 && res->digits > 63) {
3080 : res->digits = 63;
3081 : res->scale = MIN(res->scale, res->digits - 1);
3082 : }
3083 : #endif
3084 :
3085 23874 : sql_subtype t;
3086 : /* numeric types are fixed length */
3087 23874 : if (ares->type.type->eclass == EC_NUM) {
3088 : #ifdef HAVE_HGE
3089 21121 : if (ares->type.type->localtype == TYPE_hge && res->digits == 127)
3090 24 : t = *sql_bind_localtype("hge");
3091 : else
3092 : #endif
3093 21097 : if (ares->type.type->localtype == TYPE_lng && res->digits == 63)
3094 3 : t = *sql_bind_localtype("lng");
3095 21094 : else if (res->type->digits >= res->digits)
3096 8851 : t = *res; /* we cannot reduce types! */
3097 : else
3098 12243 : sql_find_numeric(&t, ares->type.type->localtype, res->digits);
3099 : } else {
3100 2753 : if (res->digits > largesttype->digits)
3101 217 : sql_find_subtype(&t, largesttype->base.name, res->digits, res->scale);
3102 : else
3103 2536 : sql_init_subtype(&t, largesttype, res->digits, res->scale);
3104 : }
3105 23874 : *res = t;
3106 : }
3107 25171 : }
3108 :
3109 : void
3110 112554 : exps_max_bits(sql_subfunc *f, list *exps)
3111 : {
3112 : /* + and - have max_bits + 1 */
3113 112554 : if (!f->func->res)
3114 : return;
3115 112554 : unsigned int digits = 0;
3116 337662 : for(node *n = exps->h; n; n = n->next) {
3117 225108 : sql_subtype *t = exp_subtype(n->data);
3118 :
3119 225108 : if (!t)
3120 0 : continue;
3121 225108 : if (digits < t->digits)
3122 225108 : digits = t->digits;
3123 : }
3124 : /* + and - (because of negative numbers) could need one extra bit (or digit) */
3125 112554 : digits += 1;
3126 112554 : sql_subtype *res = f->res->h->data;
3127 112554 : if (digits > res->type->digits)
3128 48261 : res = sql_find_numeric(res, res->type->localtype, digits);
3129 : else
3130 64293 : res->digits = digits;
3131 : }
3132 :
3133 : void
3134 19234 : exps_inout(sql_subfunc *f, list *exps)
3135 : {
3136 : /* output == first input */
3137 19234 : if (!f->func->res)
3138 : return;
3139 19234 : sql_subtype *res = f->res->h->data;
3140 19234 : bool is_decimal = (res->type->eclass == EC_DEC);
3141 19234 : unsigned int digits = 0, scale = 0;
3142 19234 : sql_type *largesttype = NULL;
3143 19234 : for(node *n = exps->h; n; n = n->next) {
3144 19234 : sql_subtype *t = exp_subtype(n->data);
3145 :
3146 19234 : if (!t)
3147 0 : continue;
3148 :
3149 19234 : if (is_decimal && t->type->eclass == EC_DEC && (!largesttype || largesttype->localtype < t->type->localtype))
3150 392 : largesttype = t->type;
3151 392 : if (is_decimal && t->type->eclass == EC_NUM) {
3152 0 : unsigned int d = bits2digits(t->digits);
3153 0 : digits = d>digits?d:digits;
3154 19234 : } else if (digits < t->digits)
3155 : digits = t->digits;
3156 19234 : if (scale < t->scale)
3157 : scale = t->scale;
3158 : break;
3159 : }
3160 19234 : if (digits > res->digits || scale > res->scale) {
3161 9278 : if (largesttype)
3162 378 : sql_init_subtype(res, largesttype, digits, scale);
3163 : else
3164 8900 : sql_find_subtype(res, res->type->base.name, digits, scale);
3165 : } else
3166 9956 : res->digits = digits;
3167 : }
3168 :
3169 : /* for aggregates we can reduce the result types size based on real digits/bits used number of known input rows */
3170 : void
3171 9196 : exps_largest_int(sql_subfunc *f, list *exps, lng cnt)
3172 : {
3173 9196 : if (!f->func->res || cnt == 0)
3174 : return;
3175 526 : sql_subtype *res = f->res->h->data;
3176 526 : if (res->type->eclass != EC_DEC && res->type->eclass != EC_NUM)
3177 : return;
3178 450 : bool is_decimal = (res->type->eclass == EC_DEC);
3179 450 : unsigned int digits = 0, scale = 0, mdigits = is_decimal ? decimal_digits(cnt) : number_bits(cnt);
3180 450 : sql_type *largesttype = NULL;
3181 450 : for(node *n = exps->h; n; n = n->next) {
3182 450 : sql_subtype *t = exp_subtype(n->data);
3183 :
3184 450 : if (!t)
3185 0 : continue;
3186 :
3187 450 : largesttype = t->type;
3188 450 : if (is_decimal && t->type->eclass == EC_NUM) {
3189 0 : unsigned int d = bits2digits(t->digits);
3190 0 : digits = d>digits?d:digits;
3191 450 : } else if (digits < t->digits)
3192 : digits = t->digits;
3193 450 : if (scale < t->scale)
3194 : scale = t->scale;
3195 : break;
3196 : }
3197 450 : digits += mdigits;
3198 450 : if (largesttype && digits <= largesttype->digits)
3199 386 : sql_init_subtype(res, largesttype, digits, scale);
3200 64 : else if (is_decimal)
3201 8 : sql_find_subtype(res, res->type->base.name, digits, scale);
3202 : else
3203 56 : sql_find_numeric(res, 1, digits);
3204 : }
3205 :
3206 : #define is_addition(fname) (strcmp(fname, "sql_add") == 0)
3207 : #define is_subtraction(fname) (strcmp(fname, "sql_sub") == 0)
3208 : void
3209 277043 : exps_scale_fix(sql_subfunc *f, list *exps, sql_subtype *atp)
3210 : {
3211 277043 : if (!f->func->res)
3212 : return;
3213 :
3214 277043 : sql_subtype *res = f->res->h->data;
3215 277043 : if (res->type->eclass != EC_ANY && res->type->eclass != EC_DEC)
3216 : return;
3217 :
3218 63440 : unsigned int digits = 0, scale = 0;
3219 63440 : sql_type *largesttype = NULL;
3220 246749 : for(node *n = exps->h; n; n = n->next) {
3221 183309 : sql_subtype *t = exp_subtype(n->data);
3222 :
3223 183309 : if (!t)
3224 0 : continue;
3225 183309 : if (digits < t->digits)
3226 : digits = t->digits;
3227 183309 : if (scale < t->scale)
3228 : scale = t->scale;
3229 183309 : if (t->type->eclass == EC_DEC && (!largesttype || largesttype->localtype < t->type->localtype))
3230 183309 : largesttype = t->type;
3231 : }
3232 63440 : res->scale = scale;
3233 63440 : if (res->type->eclass == EC_DEC)
3234 647 : digits += (is_addition(f->func->base.name) || is_subtraction(f->func->base.name));
3235 63440 : if (digits > res->type->digits) {
3236 63116 : if (largesttype && largesttype->localtype > res->type->localtype)
3237 80 : sql_init_subtype(res, largesttype, digits, scale);
3238 : else
3239 63036 : sql_find_subtype(res, res->type->localtype?res->type->base.name:atp->type->base.name, digits, scale);
3240 324 : } else if (res->type->eclass == EC_DEC || res->type->eclass == EC_NUM)
3241 253 : res->digits = digits;
3242 : }
3243 :
3244 : int
3245 138777 : exp_aggr_is_count(sql_exp *e)
3246 : {
3247 138777 : if (e->type == e_aggr && !((sql_subfunc *)e->f)->func->s && strcmp(((sql_subfunc *)e->f)->func->base.name, "count") == 0)
3248 39175 : return 1;
3249 : return 0;
3250 : }
3251 :
3252 : list *
3253 66290 : check_distinct_exp_names(mvc *sql, list *exps)
3254 : {
3255 66290 : list *distinct_exps = NULL;
3256 66290 : bool duplicates = false;
3257 :
3258 66290 : if (list_length(exps) < 2) {
3259 : return exps; /* always true */
3260 64604 : } else if (list_length(exps) < 5) {
3261 14523 : distinct_exps = list_distinct(exps, (fcmp) exp_equal, (fdup) NULL);
3262 : } else { /* for longer lists, use hashing */
3263 50081 : sql_hash *ht = hash_new(sql->ta, list_length(exps), (fkeyvalue)&exp_key);
3264 :
3265 489700 : for (node *n = exps->h; n && !duplicates; n = n->next) {
3266 439618 : sql_exp *e = n->data;
3267 439618 : int key = ht->key(e);
3268 439619 : sql_hash_e *he = ht->buckets[key&(ht->size-1)];
3269 :
3270 565674 : for (; he && !duplicates; he = he->chain) {
3271 126055 : sql_exp *f = he->value;
3272 :
3273 126055 : if (!exp_equal(e, f))
3274 1 : duplicates = true;
3275 : }
3276 439619 : hash_add(ht, key, e);
3277 : }
3278 : }
3279 64605 : if ((distinct_exps && list_length(distinct_exps) != list_length(exps)) || duplicates)
3280 5 : return NULL;
3281 : return exps;
3282 : }
3283 :
3284 : static int rel_find_parameter(mvc *sql, sql_subtype *type, sql_rel *rel, const char *relname, const char *expname);
3285 :
3286 : static int
3287 1633 : set_exp_type(mvc *sql, sql_subtype *type, sql_rel *rel, sql_exp *e)
3288 : {
3289 1639 : if (mvc_highwater(sql)) {
3290 0 : (void) sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
3291 0 : return -1;
3292 : }
3293 1639 : if (e->type == e_column) {
3294 60 : const char *nrname = (const char*) e->l, *nename = (const char*) e->r;
3295 : /* find all the column references and set the type */
3296 60 : e->tpe = *type;
3297 60 : return rel_find_parameter(sql, type, rel, nrname, nename);
3298 1579 : } else if (e->type == e_atom && e->f) {
3299 25 : list *atoms = e->f;
3300 25 : if (!list_empty(atoms))
3301 61 : for (node *n = atoms->h; n; n = n->next)
3302 36 : if (set_exp_type(sql, type, rel, n->data) < 0) /* set recursively */
3303 : return -1;
3304 25 : e->tpe = *type;
3305 25 : return 1; /* on a list of atoms, everything should be found */
3306 1554 : } else if (e->type == e_atom && !e->l && !e->r && !e->f) {
3307 1548 : e->tpe = *type;
3308 1548 : return set_type_param(sql, type, e->flag) == 0 ? 1 : 0;
3309 6 : } else if (exp_is_rel(e)) { /* for relation expressions, restart cycle */
3310 6 : rel = (sql_rel*) e->l;
3311 : /* limiting to these cases */
3312 6 : if (!is_project(rel->op) || list_length(rel->exps) != 1)
3313 0 : return 0;
3314 6 : sql_exp *re = rel->exps->h->data;
3315 :
3316 6 : e->tpe = *type;
3317 6 : return set_exp_type(sql, type, rel, re); /* set recursively */
3318 : }
3319 : return 0;
3320 : }
3321 :
3322 : int
3323 1541 : rel_set_type_param(mvc *sql, sql_subtype *type, sql_rel *rel, sql_exp *exp, int upcast)
3324 : {
3325 1541 : if (!type || !exp || (exp->type != e_atom && exp->type != e_column && !exp_is_rel(exp)))
3326 0 : return -1;
3327 :
3328 : /* use largest numeric types */
3329 1541 : if (upcast && type->type->eclass == EC_NUM)
3330 : #ifdef HAVE_HGE
3331 8 : type = sql_bind_localtype("hge");
3332 : #else
3333 : type = sql_bind_localtype("lng");
3334 : #endif
3335 6 : else if (upcast && type->type->eclass == EC_FLT)
3336 1 : type = sql_bind_localtype("dbl");
3337 :
3338 : /* TODO we could use the sql_query* struct to set paremeters used as freevars,
3339 : but it requires to change a lot of interfaces */
3340 : /* if (is_freevar(exp))
3341 : rel = query_fetch_outer(query, is_freevar(exp)-1); */
3342 1541 : return set_exp_type(sql, type, rel, exp);
3343 : }
3344 :
3345 : /* try to do an in-place conversion
3346 : *
3347 : * in-place conversion is only possible if the exp is a variable.
3348 : * This is only done to be able to map more cached queries onto the same
3349 : * interface.
3350 : */
3351 : sql_exp *
3352 5096930 : exp_convert_inplace(mvc *sql, sql_subtype *t, sql_exp *exp)
3353 : {
3354 5096930 : atom *a, *na;
3355 :
3356 : /* exclude named variables and variable lists */
3357 5096930 : if (exp->type != e_atom || exp->r /* named */ || exp->f /* list */ || !exp->l /* not direct atom */)
3358 : return NULL;
3359 :
3360 2922880 : a = exp->l;
3361 2922880 : if (!a->isnull && t->scale && t->type->eclass != EC_FLT)
3362 : return NULL;
3363 :
3364 2917588 : if ((na = atom_cast(sql->sa, a, t))) {
3365 2914862 : exp->l = na;
3366 2914862 : return exp;
3367 : }
3368 : return NULL;
3369 : }
3370 :
3371 : sql_exp *
3372 0 : exp_numeric_supertype(mvc *sql, sql_exp *e )
3373 : {
3374 0 : sql_subtype *tp = exp_subtype(e);
3375 :
3376 0 : if (tp->type->eclass == EC_DEC) {
3377 0 : sql_subtype *dtp = sql_bind_localtype("dbl");
3378 :
3379 0 : return exp_check_type(sql, dtp, NULL, e, type_cast);
3380 : }
3381 0 : if (tp->type->eclass == EC_NUM) {
3382 : #ifdef HAVE_HGE
3383 0 : sql_subtype *ltp = sql_bind_localtype("hge");
3384 : #else
3385 : sql_subtype *ltp = sql_bind_localtype("lng");
3386 : #endif
3387 :
3388 0 : return exp_check_type(sql, ltp, NULL, e, type_cast);
3389 : }
3390 : return e;
3391 : }
3392 :
3393 : sql_exp *
3394 5096889 : exp_check_type(mvc *sql, sql_subtype *t, sql_rel *rel, sql_exp *exp, check_type tpe)
3395 : {
3396 5096889 : int c, err = 0;
3397 5096889 : sql_exp* nexp = NULL;
3398 5096889 : sql_subtype *fromtype = exp_subtype(exp);
3399 :
3400 5096860 : if ((!fromtype || !fromtype->type) && rel_set_type_param(sql, t, rel, exp, 0) == 0)
3401 : return exp;
3402 :
3403 : /* first try cheap internal (in-place) conversions ! */
3404 5096859 : if ((nexp = exp_convert_inplace(sql, t, exp)) != NULL)
3405 : return nexp;
3406 :
3407 2182071 : if (fromtype && subtype_cmp(t, fromtype) != 0) {
3408 257167 : if (EC_INTERVAL(fromtype->type->eclass) && (t->type->eclass == EC_NUM || t->type->eclass == EC_POS) && t->digits < fromtype->digits) {
3409 : err = 1; /* conversion from interval to num depends on the number of digits */
3410 : } else {
3411 257167 : c = sql_type_convert(fromtype->type->eclass, t->type->eclass);
3412 257167 : if (!c || (c == 2 && tpe == type_set) || (c == 3 && tpe != type_cast)) {
3413 : err = 1;
3414 : } else {
3415 256653 : exp = exp_convert(sql->sa, exp, fromtype, t);
3416 : }
3417 : }
3418 : }
3419 256653 : if (err) {
3420 514 : const char *name = (exp->type == e_column && !has_label(exp)) ? exp_name(exp) : "%";
3421 576 : sql_exp *res = sql_error( sql, 03, SQLSTATE(42000) "types %s(%u,%u) and %s(%u,%u) are not equal%s%s%s",
3422 514 : fromtype->type->base.name,
3423 : fromtype->digits,
3424 : fromtype->scale,
3425 514 : t->type->base.name,
3426 : t->digits,
3427 : t->scale,
3428 : (name[0] != '%' ? " for column '" : ""),
3429 : (name[0] != '%' ? name : ""),
3430 514 : (name[0] != '%' ? "'" : "")
3431 : );
3432 514 : return res;
3433 : }
3434 : return exp;
3435 : }
3436 :
3437 : sql_exp *
3438 6719 : exp_values_set_supertype(mvc *sql, sql_exp *values, sql_subtype *opt_super)
3439 : {
3440 6719 : assert(is_values(values));
3441 6719 : list *vals = exp_get_values(values), *nexps;
3442 6719 : sql_subtype *tpe = opt_super?opt_super:exp_subtype(vals->h->data);
3443 :
3444 6719 : if (!opt_super && tpe)
3445 6619 : values->tpe = *tpe;
3446 :
3447 29762 : for (node *m = vals->h; m; m = m->next) {
3448 23043 : sql_exp *e = m->data;
3449 23043 : sql_subtype super, *ttpe;
3450 :
3451 : /* if the expression is a parameter set its type */
3452 23043 : if (tpe && e->type == e_atom && !e->l && !e->r && !e->f && !e->tpe.type) {
3453 4 : if (set_type_param(sql, tpe, e->flag) == 0)
3454 4 : e->tpe = *tpe;
3455 : else
3456 0 : return NULL;
3457 : }
3458 23043 : ttpe = exp_subtype(e);
3459 23043 : if (tpe && ttpe) {
3460 22990 : supertype(&super, ttpe, tpe);
3461 22990 : values->tpe = super;
3462 22990 : tpe = &values->tpe;
3463 : } else {
3464 : tpe = ttpe;
3465 : }
3466 : }
3467 :
3468 6719 : if (tpe) {
3469 : /* if the expression is a parameter set its type */
3470 29675 : for (node *m = vals->h; m; m = m->next) {
3471 22994 : sql_exp *e = m->data;
3472 22994 : if (e->type == e_atom && !e->l && !e->r && !e->f && !e->tpe.type) {
3473 1 : if (set_type_param(sql, tpe, e->flag) == 0)
3474 1 : e->tpe = *tpe;
3475 : else
3476 : return NULL;
3477 : }
3478 : }
3479 6681 : values->tpe = *tpe;
3480 6681 : nexps = sa_list(sql->sa);
3481 29670 : for (node *m = vals->h; m; m = m->next) {
3482 22992 : sql_exp *e = m->data;
3483 22992 : e = exp_check_type(sql, &values->tpe, NULL, e, type_equal);
3484 22992 : if (!e)
3485 : return NULL;
3486 22989 : exp_label(sql->sa, e, ++sql->label);
3487 22989 : append(nexps, e);
3488 : }
3489 6678 : values->f = nexps;
3490 : }
3491 : return values;
3492 : }
3493 :
3494 : /* return -1 on error, 0 not found, 1 found */
3495 : static int
3496 86 : rel_find_parameter(mvc *sql, sql_subtype *type, sql_rel *rel, const char *relname, const char *expname)
3497 : {
3498 138 : int res = 0;
3499 :
3500 138 : if (mvc_highwater(sql)) {
3501 0 : (void) sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
3502 0 : return -1;
3503 : }
3504 138 : if (!rel)
3505 : return 0;
3506 :
3507 135 : const char *nrname = relname, *nename = expname;
3508 135 : if (is_project(rel->op) && !list_empty(rel->exps)) {
3509 111 : sql_exp *e = NULL;
3510 :
3511 111 : if (nrname && nename) { /* find the column reference and propagate type setting */
3512 111 : e = exps_bind_column2(rel->exps, nrname, nename, NULL);
3513 0 : } else if (nename) {
3514 0 : e = exps_bind_column(rel->exps, nename, NULL, NULL, 1);
3515 : }
3516 111 : if (!e)
3517 3 : return 0; /* not found */
3518 108 : if (is_set(rel->op)) { /* TODO for set relations this needs further improvement */
3519 0 : (void) sql_error(sql, 10, SQLSTATE(42000) "Cannot set parameter types under set relations at the moment");
3520 0 : return -1;
3521 : }
3522 : /* set order by column types */
3523 108 : if (is_simple_project(rel->op) && !list_empty(rel->r)) {
3524 0 : sql_exp *ordere = NULL;
3525 0 : if (nrname && nename) {
3526 0 : ordere = exps_bind_column2(rel->r, nrname, nename, NULL);
3527 0 : } else if (nename) {
3528 0 : ordere = exps_bind_column(rel->r, nename, NULL, NULL, 1);
3529 : }
3530 0 : if (ordere && ordere->type == e_column)
3531 0 : ordere->tpe = *type;
3532 : }
3533 108 : if (e->type == e_column) {
3534 52 : nrname = (const char*) e->l;
3535 52 : nename = (const char*) e->r;
3536 52 : e->tpe = *type;
3537 52 : res = 1; /* found */
3538 56 : } else if ((e->type == e_atom || exp_is_rel(e)) && (res = set_exp_type(sql, type, rel, e)) <= 0) {
3539 : return res; /* don't search further */
3540 : }
3541 : /* group by columns can have aliases! */
3542 108 : if (is_groupby(rel->op) && !list_empty(rel->r)) {
3543 0 : if (nrname && nename) {
3544 0 : e = exps_bind_column2(rel->r, nrname, nename, NULL);
3545 0 : } else if (nename) {
3546 0 : e = exps_bind_column(rel->r, nename, NULL, NULL, 1);
3547 : }
3548 0 : if (!e)
3549 : return res; /* don't search further */
3550 0 : if (e->type == e_column) {
3551 0 : nrname = (const char*) e->l;
3552 0 : nename = (const char*) e->r;
3553 0 : e->tpe = *type;
3554 0 : } else if ((e->type == e_atom || exp_is_rel(e)) && (res = set_exp_type(sql, type, rel, e)) <= 0) {
3555 : return res; /* don't search further */
3556 : }
3557 : }
3558 108 : if (e->type != e_column)
3559 : return res; /* don't search further */
3560 : }
3561 :
3562 76 : switch (rel->op) {
3563 14 : case op_join:
3564 : case op_left:
3565 : case op_right:
3566 : case op_full:
3567 14 : if (rel->l)
3568 14 : res = rel_find_parameter(sql, type, rel->l, nrname, nename);
3569 14 : if (rel->r && res <= 0) { /* try other relation if not found */
3570 12 : int err = sql->session->status, lres = res;
3571 12 : char buf[ERRSIZE];
3572 :
3573 12 : strcpy(buf, sql->errstr); /* keep error found and try other join relation */
3574 12 : sql->session->status = 0;
3575 12 : sql->errstr[0] = '\0';
3576 12 : res = rel_find_parameter(sql, type, rel->r, nrname, nename);
3577 12 : if (res == 0) { /* parameter wasn't found, set error */
3578 1 : res = lres;
3579 1 : sql->session->status = err;
3580 1 : strcpy(sql->errstr, buf);
3581 : }
3582 : }
3583 : break;
3584 52 : case op_semi:
3585 : case op_anti:
3586 : case op_groupby:
3587 : case op_project:
3588 : case op_select:
3589 : case op_topn:
3590 : case op_sample:
3591 52 : if (rel->l)
3592 : res = rel_find_parameter(sql, type, rel->l, nrname, nename);
3593 : break;
3594 0 : case op_union: /* TODO for set relations this needs further improvement */
3595 : case op_inter:
3596 : case op_except: {
3597 0 : (void) sql_error(sql, 10, SQLSTATE(42000) "Cannot set parameter types under set relations at the moment");
3598 0 : return -1;
3599 : }
3600 : default: /* For table returning functions, the type must be set when the relation is created */
3601 : return 0;
3602 : }
3603 : return res;
3604 : }
3605 :
3606 : sql_exp *
3607 18178 : list_find_exp( list *exps, sql_exp *e)
3608 : {
3609 18178 : sql_exp *ne = NULL;
3610 :
3611 18178 : if (e->type != e_column)
3612 : return NULL;
3613 18135 : if (( e->l && (ne=exps_bind_column2(exps, e->l, e->r, NULL)) != NULL) ||
3614 17588 : ((!e->l && (ne=exps_bind_column(exps, e->r, NULL, NULL, 1)) != NULL)))
3615 637 : return ne;
3616 : return NULL;
3617 : }
|