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 "rel_optimizer_private.h"
15 : #include "rel_planner.h"
16 : #include "rel_exp.h"
17 : #include "rel_select.h"
18 : #include "rel_rewriter.h"
19 :
20 : /* Split_select optimizer splits case statements in select expressions. This is a step needed for cse */
21 : static void select_split_exps(mvc *sql, list *exps, sql_rel *rel);
22 :
23 : static sql_exp *
24 224580 : select_split_exp(mvc *sql, sql_exp *e, sql_rel *rel)
25 : {
26 224580 : switch(e->type) {
27 : case e_column:
28 : return e;
29 11639 : case e_convert:
30 11639 : e->l = select_split_exp(sql, e->l, rel);
31 11639 : return e;
32 25328 : case e_aggr:
33 : case e_func:
34 25328 : if (!is_analytic(e) && !exp_has_sideeffect(e)) {
35 25323 : sql_subfunc *f = e->f;
36 25323 : if (e->type == e_func && !f->func->s && is_caselike_func(f) /*is_ifthenelse_func(f)*/)
37 277 : return add_exp_too_project(sql, e, rel);
38 : }
39 : return e;
40 75452 : case e_cmp:
41 75452 : if (e->flag == cmp_or || e->flag == cmp_filter) {
42 18721 : select_split_exps(sql, e->l, rel);
43 18721 : select_split_exps(sql, e->r, rel);
44 56731 : } else if (e->flag == cmp_in || e->flag == cmp_notin) {
45 2794 : e->l = select_split_exp(sql, e->l, rel);
46 2794 : select_split_exps(sql, e->r, rel);
47 : } else {
48 53937 : e->l = select_split_exp(sql, e->l, rel);
49 53937 : e->r = select_split_exp(sql, e->r, rel);
50 53937 : if (e->f)
51 3599 : e->f = select_split_exp(sql, e->f, rel);
52 : }
53 : return e;
54 : case e_atom:
55 : case e_psm:
56 : return e;
57 : }
58 : return e;
59 : }
60 :
61 : static void
62 65332 : select_split_exps(mvc *sql, list *exps, sql_rel *rel)
63 : {
64 65332 : node *n;
65 :
66 65332 : if (!exps)
67 : return;
68 164006 : for(n=exps->h; n; n = n->next){
69 98674 : sql_exp *e = n->data;
70 :
71 98674 : e = select_split_exp(sql, e, rel);
72 98674 : n->data = e;
73 : }
74 : }
75 :
76 : static sql_rel *
77 1685794 : rel_split_select_(visitor *v, sql_rel *rel)
78 : {
79 1685794 : if (!rel || !is_select(rel->op) || list_empty(rel->exps) || !rel->l || mvc_highwater(v->sql))
80 1462649 : return rel;
81 :
82 223145 : bool funcs = false;
83 :
84 : /* are there functions */
85 478139 : for (node *n = rel->exps->h; n && !funcs; n = n->next)
86 254994 : funcs = exp_has_func(n->data);
87 :
88 : /* introduce extra project */
89 223145 : if (funcs) {
90 25096 : sql_rel *nrel = rel_project(v->sql->sa, rel->l,
91 25096 : rel_projections(v->sql, rel->l, NULL, 1, 1));
92 25096 : if (!nrel || !nrel->exps)
93 : return NULL;
94 25096 : rel->l = nrel;
95 : /* recursively split all functions and add those to the projection list */
96 25096 : select_split_exps(v->sql, rel->exps, nrel);
97 25096 : return rel;
98 : }
99 : return rel;
100 : }
101 :
102 : static sql_rel *
103 50050 : rel_split_select(visitor *v, global_props *gp, sql_rel *rel)
104 : {
105 50050 : (void) gp;
106 50050 : return rel_visitor_bottomup(v, rel, &rel_split_select_);
107 : }
108 :
109 : run_optimizer
110 596802 : bind_split_select(visitor *v, global_props *gp)
111 : {
112 596802 : int flag = v->sql->sql_optimizer;
113 530868 : return gp->opt_cycle == 0 && gp->opt_level == 1 && (flag & split_select)
114 1127661 : && gp->cnt[op_select] ? rel_split_select : NULL;
115 : }
116 :
117 :
118 : /*
119 : * Remove a redundant join
120 : *
121 : * join (L, Distinct Project(join(L,P) [ p.key == l.lkey]) [p.key]) [ p.key == l.lkey]
122 : * =>
123 : * join(L, P) [p.key==l.lkey]
124 : */
125 : static sql_rel *
126 1538803 : rel_remove_redundant_join_(visitor *v, sql_rel *rel)
127 : {
128 1538803 : if ((is_join(rel->op) || is_semi(rel->op)) && !list_empty(rel->exps)) {
129 240097 : sql_rel *l = rel->l, *r = rel->r, *b, *p = NULL, *j;
130 :
131 240097 : if (is_basetable(l->op) && is_simple_project(r->op) && need_distinct(r)) {
132 8 : b = l;
133 8 : p = r;
134 8 : j = p->l;
135 240089 : } else if (is_basetable(r->op) && is_simple_project(l->op) && need_distinct(l)) {
136 2333 : b = r;
137 2333 : p = l;
138 2333 : j = p->l;
139 : }
140 240097 : if (!p || !j || j->op != rel->op)
141 : return rel;
142 : /* j must have b->l (ie table) */
143 12 : sql_rel *jl = j->l, *jr = j->r;
144 12 : if ((is_basetable(jl->op) && jl->l == b->l) ||
145 8 : (is_basetable(jr->op) && jr->l == b->l)) {
146 11 : int left = 0;
147 11 : if (is_basetable(jl->op) && jl->l == b->l)
148 11 : left = 1;
149 11 : if (!list_empty(p->exps)) {
150 19 : for (node *n=p->exps->h; n; n = n->next) { /* all exps of 'p' must be bound to the opposite side */
151 11 : sql_exp *e = n->data;
152 :
153 18 : if (!rel_rebind_exp(v->sql, left ? jr : jl, e))
154 : return rel;
155 : }
156 : }
157 8 : if (exp_match_list(j->exps, rel->exps)) {
158 1 : p->l = (left)?rel_dup(jr):rel_dup(jl);
159 1 : rel_destroy(j);
160 1 : set_nodistinct(p);
161 1 : v->changes++;
162 1 : return rel;
163 : }
164 : }
165 : }
166 : return rel;
167 : }
168 :
169 : static sql_rel *
170 40150 : rel_remove_redundant_join(visitor *v, global_props *gp, sql_rel *rel)
171 : {
172 40150 : (void) gp;
173 40150 : return rel_visitor_bottomup(v, rel, &rel_remove_redundant_join_); /* this optimizer has to run before rel_first_level_optimizations */
174 : }
175 :
176 : run_optimizer
177 596798 : bind_remove_redundant_join(visitor *v, global_props *gp)
178 : {
179 596798 : int flag = v->sql->sql_optimizer;
180 530869 : return gp->opt_cycle == 0 && gp->opt_level == 1 && (gp->cnt[op_left] || gp->cnt[op_right]
181 517456 : || gp->cnt[op_full] || gp->cnt[op_join] || gp->cnt[op_semi] || gp->cnt[op_anti]) &&
182 636948 : (flag & remove_redundant_join) ? rel_remove_redundant_join : NULL;
183 : }
184 :
185 :
186 : static list *
187 1312496 : exp_merge_range(visitor *v, sql_rel *rel, list *exps)
188 : {
189 1312496 : node *n, *m;
190 2830196 : for (n=exps->h; n; n = n->next) {
191 1519600 : sql_exp *e = n->data;
192 1519600 : sql_exp *le = e->l;
193 1519600 : sql_exp *re = e->r;
194 :
195 : /* handle the and's in the or lists */
196 1519600 : if (e->type == e_cmp && e->flag == cmp_or && !is_anti(e)) {
197 44013 : e->l = exp_merge_range(v, rel, e->l);
198 44013 : e->r = exp_merge_range(v, rel, e->r);
199 : /* only look for gt, gte, lte, lt */
200 1475587 : } else if (n->next &&
201 203405 : e->type == e_cmp && e->flag < cmp_equal && !e->f &&
202 6909 : re->card == CARD_ATOM && !is_anti(e)) {
203 2274 : for (m=n->next; m; m = m->next) {
204 1391 : sql_exp *f = m->data;
205 1391 : sql_exp *lf = f->l;
206 1391 : sql_exp *rf = f->r;
207 1391 : int c_le = is_numeric_upcast(le), c_lf = is_numeric_upcast(lf);
208 :
209 1391 : if (f->type == e_cmp && f->flag < cmp_equal && !f->f &&
210 1308 : rf->card == CARD_ATOM && !is_anti(f) &&
211 654 : exp_match_exp(c_le?le->l:le, c_lf?lf->l:lf)) {
212 218 : sql_exp *ne;
213 218 : int swap = 0, lt = 0, gt = 0;
214 218 : sql_subtype super;
215 : /* for now only c1 <[=] x <[=] c2 */
216 :
217 218 : swap = lt = (e->flag == cmp_lt || e->flag == cmp_lte);
218 218 : gt = !lt;
219 :
220 218 : if (gt &&
221 169 : (f->flag == cmp_gt ||
222 : f->flag == cmp_gte))
223 71 : continue;
224 70 : if (lt &&
225 49 : (f->flag == cmp_lt ||
226 : f->flag == cmp_lte))
227 21 : continue;
228 :
229 147 : supertype(&super, exp_subtype(le), exp_subtype(lf));
230 147 : if (!(rf = exp_check_type(v->sql, &super, rel, rf, type_equal)) ||
231 147 : !(le = exp_check_type(v->sql, &super, rel, le, type_equal)) ||
232 147 : !(re = exp_check_type(v->sql, &super, rel, re, type_equal))) {
233 0 : v->sql->session->status = 0;
234 0 : v->sql->errstr[0] = 0;
235 0 : continue;
236 : }
237 147 : if (!swap)
238 119 : ne = exp_compare2(v->sql->sa, le, re, rf, compare2range(e->flag, f->flag), 0);
239 : else
240 28 : ne = exp_compare2(v->sql->sa, le, rf, re, compare2range(f->flag, e->flag), 0);
241 :
242 147 : list_remove_data(exps, NULL, e);
243 147 : list_remove_data(exps, NULL, f);
244 147 : list_append(exps, ne);
245 147 : v->changes++;
246 147 : return exp_merge_range(v, rel, exps);
247 : }
248 : }
249 1474557 : } else if (n->next &&
250 202375 : e->type == e_cmp && e->flag < cmp_equal && !e->f &&
251 5879 : re->card > CARD_ATOM && !is_anti(e)) {
252 10595 : for (m=n->next; m; m = m->next) {
253 6469 : sql_exp *f = m->data;
254 6469 : sql_exp *lf = f->l;
255 6469 : sql_exp *rf = f->r;
256 :
257 6469 : if (f->type == e_cmp && f->flag < cmp_equal && !f->f &&
258 4725 : rf->card > CARD_ATOM && !is_anti(f)) {
259 4725 : sql_exp *ne, *t;
260 4725 : int swap = 0, lt = 0, gt = 0;
261 4725 : comp_type ef = (comp_type) e->flag, ff = (comp_type) f->flag;
262 4725 : int c_re = is_numeric_upcast(re), c_rf = is_numeric_upcast(rf);
263 4725 : int c_le = is_numeric_upcast(le), c_lf = is_numeric_upcast(lf), c;
264 4725 : sql_subtype super;
265 :
266 : /* both swapped ? */
267 4725 : if (exp_match_exp(c_re?re->l:re, c_rf?rf->l:rf)) {
268 19 : t = re;
269 19 : re = le;
270 19 : le = t;
271 19 : c = c_re; c_re = c_le; c_le = c;
272 19 : ef = swap_compare(ef);
273 19 : t = rf;
274 19 : rf = lf;
275 19 : lf = t;
276 19 : c = c_rf; c_rf = c_lf; c_lf = c;
277 19 : ff = swap_compare(ff);
278 : }
279 :
280 : /* is left swapped ? */
281 4725 : if (exp_match_exp(c_re?re->l:re, c_lf?lf->l:lf)) {
282 105 : t = re;
283 105 : re = le;
284 105 : le = t;
285 105 : c = c_re; c_re = c_le; c_le = c;
286 105 : ef = swap_compare(ef);
287 : }
288 :
289 : /* is right swapped ? */
290 4725 : if (exp_match_exp(c_le?le->l:le, c_rf?rf->l:rf)) {
291 177 : t = rf;
292 177 : rf = lf;
293 177 : lf = t;
294 177 : c = c_rf; c_rf = c_lf; c_lf = c;
295 177 : ff = swap_compare(ff);
296 : }
297 :
298 4725 : if (!exp_match_exp(c_le?le->l:le, c_lf?lf->l:lf))
299 2972 : continue;
300 :
301 : /* for now only c1 <[=] x <[=] c2 */
302 1832 : swap = lt = (ef == cmp_lt || ef == cmp_lte);
303 1832 : gt = !lt;
304 :
305 1832 : if (gt && (ff == cmp_gt || ff == cmp_gte))
306 73 : continue;
307 1759 : if (lt && (ff == cmp_lt || ff == cmp_lte))
308 6 : continue;
309 :
310 1753 : supertype(&super, exp_subtype(le), exp_subtype(lf));
311 1753 : if (!(rf = exp_check_type(v->sql, &super, rel, rf, type_equal)) ||
312 1753 : !(le = exp_check_type(v->sql, &super, rel, le, type_equal)) ||
313 1753 : !(re = exp_check_type(v->sql, &super, rel, re, type_equal))) {
314 0 : v->sql->session->status = 0;
315 0 : v->sql->errstr[0] = 0;
316 0 : continue;
317 : }
318 1753 : if (!swap)
319 1653 : ne = exp_compare2(v->sql->sa, le, re, rf, compare2range(ef, ff), 0);
320 : else
321 100 : ne = exp_compare2(v->sql->sa, le, rf, re, compare2range(ff, ef), 0);
322 :
323 1753 : list_remove_data(exps, NULL, e);
324 1753 : list_remove_data(exps, NULL, f);
325 1753 : list_append(exps, ne);
326 1753 : v->changes++;
327 1753 : return exp_merge_range(v, rel, exps);
328 : }
329 : }
330 : }
331 : }
332 : return exps;
333 : }
334 :
335 : static int
336 41047 : exps_cse( mvc *sql, list *oexps, list *l, list *r )
337 : {
338 41047 : list *nexps;
339 41047 : node *n, *m;
340 41047 : char *lu, *ru;
341 41047 : int lc = 0, rc = 0, match = 0, res = 0;
342 :
343 41047 : if (list_length(l) == 0 || list_length(r) == 0)
344 0 : return 0;
345 :
346 : /* first recusive exps_cse */
347 41047 : nexps = new_exp_list(sql->sa);
348 86944 : for (n = l->h; n; n = n->next) {
349 45897 : sql_exp *e = n->data;
350 :
351 45897 : if (e->type == e_cmp && e->flag == cmp_or && !is_anti(e)) {
352 16668 : res = exps_cse(sql, nexps, e->l, e->r);
353 : } else {
354 29229 : append(nexps, e);
355 : }
356 : }
357 41047 : l = nexps;
358 :
359 41047 : nexps = new_exp_list(sql->sa);
360 89732 : for (n = r->h; n; n = n->next) {
361 48685 : sql_exp *e = n->data;
362 :
363 48685 : if (e->type == e_cmp && e->flag == cmp_or && !is_anti(e)) {
364 3259 : res = exps_cse(sql, nexps, e->l, e->r);
365 : } else {
366 45426 : append(nexps, e);
367 : }
368 : }
369 41047 : r = nexps;
370 :
371 : /* simplify true or .. and .. or true */
372 41047 : if (list_length(l) == list_length(r) && list_length(l) == 1) {
373 35889 : sql_exp *le = l->h->data, *re = r->h->data;
374 :
375 35889 : if (exp_is_true(le)) {
376 7 : append(oexps, le);
377 7 : return 1;
378 : }
379 35882 : if (exp_is_true(re)) {
380 4 : append(oexps, re);
381 4 : return 1;
382 : }
383 : }
384 :
385 41036 : lu = SA_ZNEW_ARRAY(sql->ta, char, list_length(l));
386 41036 : ru = SA_ZNEW_ARRAY(sql->ta, char, list_length(r));
387 87648 : for (n = l->h, lc = 0; n; n = n->next, lc++) {
388 46612 : sql_exp *le = n->data;
389 :
390 105082 : for ( m = r->h, rc = 0; m; m = m->next, rc++) {
391 58470 : sql_exp *re = m->data;
392 :
393 58470 : if (!ru[rc] && exp_match_exp(le,re)) {
394 788 : lu[lc] = 1;
395 788 : ru[rc] = 1;
396 788 : match = 1;
397 : }
398 : }
399 : }
400 41036 : if (match) {
401 408 : list *nl = new_exp_list(sql->sa);
402 408 : list *nr = new_exp_list(sql->sa);
403 :
404 1612 : for (n = l->h, lc = 0; n; n = n->next, lc++)
405 1204 : if (!lu[lc])
406 419 : append(nl, n->data);
407 1627 : for (n = r->h, rc = 0; n; n = n->next, rc++)
408 1219 : if (!ru[rc])
409 431 : append(nr, n->data);
410 :
411 408 : if (list_length(nl) && list_length(nr))
412 387 : append(oexps, exp_or(sql->sa, nl, nr, 0));
413 :
414 1612 : for (n = l->h, lc = 0; n; n = n->next, lc++) {
415 1204 : if (lu[lc])
416 785 : append(oexps, n->data);
417 : }
418 : res = 1;
419 : } else {
420 40628 : append(oexps, exp_or(sql->sa, list_dup(l, (fdup)NULL),
421 : list_dup(r, (fdup)NULL), 0));
422 : }
423 : return res;
424 : }
425 :
426 : static int
427 117659 : are_equality_exps( list *exps, sql_exp **L)
428 : {
429 117659 : sql_exp *l = *L;
430 :
431 117659 : if (list_length(exps) == 1) {
432 113476 : sql_exp *e = exps->h->data, *le = e->l, *re = e->r;
433 :
434 113476 : if (e->type == e_cmp && e->flag == cmp_equal && le->card != CARD_ATOM && re->card == CARD_ATOM && !is_semantics(e)) {
435 23024 : if (!l) {
436 11672 : *L = l = le;
437 11672 : if (!is_column(le->type))
438 : return 0;
439 : }
440 23024 : return (exp_match(l, le));
441 : }
442 90452 : if (e->type == e_cmp && e->flag == cmp_or && !is_anti(e) && !is_semantics(e))
443 123082 : return (are_equality_exps(e->l, L) && are_equality_exps(e->r, L));
444 : }
445 : return 0;
446 : }
447 :
448 : static void
449 3544 : get_exps( list *n, list *l )
450 : {
451 4534 : sql_exp *e = l->h->data, *re = e->r;
452 :
453 4534 : if (e->type == e_cmp && e->flag == cmp_equal && re->card == CARD_ATOM)
454 3544 : list_append(n, re);
455 4534 : if (e->type == e_cmp && e->flag == cmp_or) {
456 990 : get_exps(n, e->l);
457 990 : get_exps(n, e->r);
458 : }
459 3544 : }
460 :
461 : static sql_exp *
462 1277 : equality_exps_2_in( mvc *sql, sql_exp *ce, list *l, list *r)
463 : {
464 1277 : list *nl = new_exp_list(sql->sa);
465 :
466 1277 : get_exps(nl, l);
467 1277 : get_exps(nl, r);
468 :
469 1277 : return exp_in( sql->sa, ce, nl, cmp_in);
470 : }
471 :
472 : static list *
473 630885 : merge_ors(mvc *sql, list *exps, int *changes)
474 : {
475 630885 : list *nexps = NULL;
476 630885 : int needed = 0;
477 :
478 1367374 : for (node *n = exps->h; n && !needed; n = n->next) {
479 736489 : sql_exp *e = n->data;
480 :
481 736489 : if (e->type == e_cmp && e->flag == cmp_or && !is_anti(e))
482 736489 : needed = 1;
483 : }
484 :
485 630885 : if (needed) {
486 41388 : nexps = new_exp_list(sql->sa);
487 91013 : for (node *n = exps->h; n; n = n->next) {
488 49625 : sql_exp *e = n->data, *l = NULL;
489 :
490 49625 : if (e->type == e_cmp && e->flag == cmp_or && !is_anti(e) && are_equality_exps(e->l, &l) && are_equality_exps(e->r, &l) && l) {
491 1277 : (*changes)++;
492 1277 : append(nexps, equality_exps_2_in(sql, l, e->l, e->r));
493 : } else {
494 48348 : append(nexps, e);
495 : }
496 : }
497 : } else {
498 : nexps = exps;
499 : }
500 :
501 1374575 : for (node *n = nexps->h; n ; n = n->next) {
502 743690 : sql_exp *e = n->data;
503 :
504 743690 : if (e->type == e_cmp && e->flag == cmp_or) {
505 41220 : e->l = merge_ors(sql, e->l, changes);
506 41220 : e->r = merge_ors(sql, e->r, changes);
507 : }
508 : }
509 :
510 630885 : return nexps;
511 : }
512 :
513 : #define TRIVIAL_NOT_EQUAL_CMP(e) \
514 : ((e)->type == e_cmp && (e)->flag == cmp_notequal && !is_anti((e)) && !is_semantics((e)) && ((sql_exp*)(e)->l)->card != CARD_ATOM && ((sql_exp*)(e)->r)->card == CARD_ATOM)
515 :
516 : static list *
517 630885 : merge_notequal(mvc *sql, list *exps, int *changes)
518 : {
519 630885 : list *inequality_groups = NULL, *nexps = NULL;
520 630885 : int needed = 0;
521 :
522 1374575 : for (node *n = exps->h; n; n = n->next) {
523 743690 : sql_exp *e = n->data;
524 :
525 743690 : if (TRIVIAL_NOT_EQUAL_CMP(e)) {
526 100316 : bool appended = false;
527 :
528 100316 : if (inequality_groups) {
529 9299 : for (node *m = inequality_groups->h; m && !appended; m = m->next) {
530 5308 : list *next = m->data;
531 5308 : sql_exp *first = (sql_exp*) next->h->data;
532 :
533 5308 : if (exp_match(first->l, e->l)) {
534 646 : list_append(next, e);
535 646 : appended = true;
536 : }
537 : }
538 : }
539 3991 : if (!appended) {
540 99670 : if (!inequality_groups)
541 96325 : inequality_groups = new_exp_list(sql->sa);
542 99670 : list_append(inequality_groups, list_append(new_exp_list(sql->sa), e));
543 : }
544 : }
545 : }
546 :
547 630885 : if (inequality_groups) { /* if one list of inequalities has more than one entry, then the re-write is needed */
548 195995 : for (node *n = inequality_groups->h; n; n = n->next) {
549 99670 : list *next = n->data;
550 :
551 99670 : if (list_length(next) > 1)
552 561 : needed = 1;
553 : }
554 : }
555 :
556 96325 : if (needed) {
557 479 : nexps = new_exp_list(sql->sa);
558 1574 : for (node *n = inequality_groups->h; n; n = n->next) {
559 1095 : list *next = n->data;
560 1095 : sql_exp *first = (sql_exp*) next->h->data;
561 :
562 1095 : if (list_length(next) > 1) {
563 561 : list *notin = new_exp_list(sql->sa);
564 :
565 1768 : for (node *m = next->h; m; m = m->next) {
566 1207 : sql_exp *e = m->data;
567 1207 : list_append(notin, e->r);
568 : }
569 561 : list_append(nexps, exp_in(sql->sa, first->l, notin, cmp_notin));
570 : } else {
571 534 : list_append(nexps, first);
572 : }
573 : }
574 :
575 2578 : for (node *n = exps->h; n; n = n->next) {
576 2099 : sql_exp *e = n->data;
577 :
578 2099 : if (!TRIVIAL_NOT_EQUAL_CMP(e))
579 358 : list_append(nexps, e);
580 : }
581 479 : (*changes)++;
582 : } else {
583 : nexps = exps;
584 : }
585 :
586 1373929 : for (node *n = nexps->h; n ; n = n->next) {
587 743044 : sql_exp *e = n->data;
588 :
589 743044 : if (e->type == e_cmp && e->flag == cmp_or) {
590 41220 : e->l = merge_notequal(sql, e->l, changes);
591 41220 : e->r = merge_notequal(sql, e->r, changes);
592 : }
593 : }
594 :
595 630885 : return nexps;
596 : }
597 :
598 : int
599 178699 : is_numeric_upcast(sql_exp *e)
600 : {
601 178699 : if (is_convert(e->type)) {
602 10718 : sql_subtype *f = exp_fromtype(e);
603 10718 : sql_subtype *t = exp_totype(e);
604 :
605 10718 : if (f->type->eclass == t->type->eclass && EC_COMPUTE(f->type->eclass)) {
606 1998 : if (f->type->localtype < t->type->localtype)
607 1911 : return 1;
608 : }
609 : }
610 : return 0;
611 : }
612 :
613 : /* optimize (a = b) or (a is null and b is null) -> a = b with null semantics */
614 : static sql_exp *
615 42766 : try_rewrite_equal_or_is_null(mvc *sql, sql_rel *rel, sql_exp *or, list *l1, list *l2)
616 : {
617 42766 : if (list_length(l1) == 1) {
618 39208 : bool valid = true, first_is_null_found = false, second_is_null_found = false;
619 39208 : sql_exp *cmp = l1->h->data;
620 39208 : sql_exp *first = cmp->l, *second = cmp->r;
621 :
622 39208 : if (is_compare(cmp->type) && !is_anti(cmp) && !cmp->f && cmp->flag == cmp_equal) {
623 7437 : int fupcast = is_numeric_upcast(first), supcast = is_numeric_upcast(second);
624 15139 : for(node *n = l2->h ; n && valid; n = n->next) {
625 7702 : sql_exp *e = n->data, *l = e->l, *r = e->r;
626 :
627 7702 : if (is_compare(e->type) && e->flag == cmp_equal && !e->f &&
628 4551 : !is_anti(e) && is_semantics(e)) {
629 1840 : int lupcast = is_numeric_upcast(l);
630 1840 : int rupcast = is_numeric_upcast(r);
631 1840 : sql_exp *rr = rupcast ? r->l : r;
632 :
633 1840 : if (rr->type == e_atom && rr->l && atom_null(rr->l)) {
634 1840 : if (exp_match_exp(fupcast?first->l:first, lupcast?l->l:l))
635 : first_is_null_found = true;
636 606 : else if (exp_match_exp(supcast?second->l:second, lupcast?l->l:l))
637 : second_is_null_found = true;
638 : else
639 6203 : valid = false;
640 : } else {
641 : valid = false;
642 : }
643 : } else {
644 : valid = false;
645 : }
646 : }
647 7437 : if (valid && first_is_null_found && second_is_null_found) {
648 263 : sql_subtype super;
649 :
650 263 : supertype(&super, exp_subtype(first), exp_subtype(second)); /* first and second must have the same type */
651 263 : if (!(first = exp_check_type(sql, &super, rel, first, type_equal)) ||
652 263 : !(second = exp_check_type(sql, &super, rel, second, type_equal))) {
653 0 : sql->session->status = 0;
654 0 : sql->errstr[0] = 0;
655 0 : return or;
656 : }
657 263 : sql_exp *res = exp_compare(sql->sa, first, second, cmp->flag);
658 263 : set_semantics(res);
659 263 : if (exp_name(or))
660 0 : exp_prop_alias(sql->sa, res, or);
661 263 : return res;
662 : }
663 : }
664 : }
665 : return or;
666 : }
667 :
668 : static list *
669 1222570 : merge_cmp_or_null(mvc *sql, sql_rel *rel, list *exps, int *changes)
670 : {
671 2637928 : for (node *n = exps->h; n ; n = n->next) {
672 1415358 : sql_exp *e = n->data;
673 :
674 1415358 : if (is_compare(e->type) && e->flag == cmp_or && !is_anti(e)) {
675 21383 : sql_exp *ne = try_rewrite_equal_or_is_null(sql, rel, e, e->l, e->r);
676 21383 : if (ne != e) {
677 261 : (*changes)++;
678 261 : n->data = ne;
679 : }
680 21383 : ne = try_rewrite_equal_or_is_null(sql, rel, e, e->r, e->l);
681 21383 : if (ne != e) {
682 2 : (*changes)++;
683 2 : n->data = ne;
684 : }
685 : }
686 : }
687 1222570 : return exps;
688 : }
689 :
690 : static list *
691 1222570 : cleanup_equal_exps(mvc *sql, sql_rel *rel, list *exps, int *changes)
692 : {
693 1222570 : if (list_length(exps) <= 1)
694 : return exps;
695 175450 : if (is_join(rel->op)) {
696 249162 : for(node *n = exps->h; n; n = n->next) {
697 167862 : sql_exp *e = n->data;
698 335546 : if (e->type == e_cmp && !e->f && e->flag <= cmp_notequal &&
699 209158 : !rel_find_exp(rel->l, e->l) && !rel_find_exp(rel->r, e->r) &&
700 41335 : !find_prop(e->p, PROP_HASHCOL) && !find_prop(e->p, PROP_JOINIDX)) {
701 20613 : exp_swap(e);
702 : }
703 : }
704 : }
705 175450 : bool needed = false;
706 544258 : for(node *n = exps->h; !needed && n; n = n->next) {
707 701990 : for (node *m = n->next; !needed && m; m = m->next) {
708 333182 : if (exp_match_exp_semantics(n->data, m->data, false))
709 71 : needed = true;
710 : }
711 : }
712 175450 : if (needed) {
713 71 : list *nexps = sa_list(sql->sa);
714 :
715 239 : for(node *n = exps->h; n; n = n->next) {
716 168 : bool done = false;
717 542 : for (node *m = exps->h; m && !done; m = m->next) {
718 374 : if (n != m && exp_match_exp_semantics(n->data, m->data, false)) {
719 142 : sql_exp *e1 = n->data, *e2 = m->data;
720 142 : if ((is_any(e1) || is_semantics(e1)) || (!is_any(e2) && !is_semantics(e2))) {
721 128 : append(nexps, e1);
722 128 : if ((!is_any(e2) && !is_semantics(e2)) && is_left(rel->op) && list_length(rel->attr) == 1) {
723 : /* nil is false */
724 14 : sql_exp *m = rel->attr->h->data;
725 14 : if (exp_is_atom(m))
726 14 : set_no_nil(m);
727 : }
728 : }
729 : done = true;
730 : }
731 : }
732 168 : if (!done)
733 26 : append(nexps, n->data);
734 : }
735 : return nexps;
736 : }
737 : (void)changes;
738 : return exps;
739 : }
740 :
741 : static inline sql_rel *
742 1222570 : rel_select_cse(visitor *v, sql_rel *rel)
743 : {
744 1222570 : if ((is_select(rel->op) || is_join(rel->op) || is_semi(rel->op)) && rel->exps) /* cleanup equal expressions */
745 1222570 : rel->exps = cleanup_equal_exps(v->sql, rel, rel->exps, &v->changes); /* (a = b) and (a += b) */
746 :
747 1222570 : if (is_select(rel->op) && rel->exps)
748 548445 : rel->exps = merge_ors(v->sql, rel->exps, &v->changes); /* x = 1 or x = 2 => x in (1, 2)*/
749 :
750 1222570 : if (is_select(rel->op) && rel->exps)
751 548445 : rel->exps = merge_notequal(v->sql, rel->exps, &v->changes); /* x <> 1 and x <> 2 => x not in (1, 2)*/
752 :
753 1222570 : if ((is_select(rel->op) || is_join(rel->op) || is_semi(rel->op)) && rel->exps)
754 1222570 : rel->exps = merge_cmp_or_null(v->sql, rel, rel->exps, &v->changes); /* (a = b) or (a is null and b is null) -> a = b with null semantics */
755 :
756 1222570 : if ((is_select(rel->op) || is_join(rel->op) || is_semi(rel->op)) && rel->exps) {
757 1222570 : node *n;
758 1222570 : list *nexps;
759 1222570 : int needed = 0;
760 :
761 2631514 : for (n=rel->exps->h; n && !needed; n = n->next) {
762 1408944 : sql_exp *e = n->data;
763 :
764 1408944 : if (e->type == e_cmp && e->flag == cmp_or && !is_anti(e))
765 1408944 : needed = 1;
766 : }
767 1222570 : if (!needed)
768 : return rel;
769 20315 : nexps = new_exp_list(v->sql->sa);
770 48424 : for (n=rel->exps->h; n; n = n->next) {
771 28109 : sql_exp *e = n->data;
772 :
773 28109 : if (e->type == e_cmp && e->flag == cmp_or && !is_anti(e)) {
774 : /* split the common expressions */
775 21120 : v->changes += exps_cse(v->sql, nexps, e->l, e->r);
776 : } else {
777 6989 : append(nexps, e);
778 : }
779 : }
780 20315 : rel->exps = nexps;
781 : }
782 : return rel;
783 : }
784 :
785 : static list *
786 14861 : exps_merge_select_rse( mvc *sql, list *l, list *r, bool *merged)
787 : {
788 14861 : node *n, *m, *o;
789 14861 : list *nexps = NULL, *lexps, *rexps;
790 14861 : bool lmerged = true, rmerged = true;
791 :
792 14861 : lexps = new_exp_list(sql->sa);
793 31484 : for (n = l->h; n; n = n->next) {
794 16623 : sql_exp *e = n->data;
795 :
796 16623 : if (e->type == e_cmp && e->flag == cmp_or && !is_anti(e) && !is_semantics(e)) {
797 6274 : lmerged = false;
798 6274 : list *nexps = exps_merge_select_rse(sql, e->l, e->r, &lmerged);
799 7509 : for (o = nexps->h; o; o = o->next)
800 1235 : append(lexps, o->data);
801 : } else {
802 10349 : append(lexps, e);
803 : }
804 : }
805 14861 : if (lmerged)
806 9366 : lmerged = (list_length(lexps) == 1);
807 14861 : rexps = new_exp_list(sql->sa);
808 32164 : for (n = r->h; n; n = n->next) {
809 17303 : sql_exp *e = n->data;
810 :
811 17303 : if (e->type == e_cmp && e->flag == cmp_or && !is_anti(e) && !is_semantics(e)) {
812 1117 : rmerged = false;
813 1117 : list *nexps = exps_merge_select_rse(sql, e->l, e->r, &rmerged);
814 1122 : for (o = nexps->h; o; o = o->next)
815 5 : append(rexps, o->data);
816 : } else {
817 16186 : append(rexps, e);
818 : }
819 : }
820 14861 : if (rmerged)
821 13751 : rmerged = (list_length(r) == 1);
822 :
823 14861 : nexps = new_exp_list(sql->sa);
824 :
825 : /* merge merged lists first ? */
826 26445 : for (n = lexps->h; n; n = n->next) {
827 11584 : sql_exp *le = n->data, *re, *fnd = NULL;
828 :
829 11584 : if (le->type != e_cmp || le->flag == cmp_or || is_anti(le) || is_semantics(le) || is_symmetric(le))
830 655 : continue;
831 22310 : for (m = rexps->h; !fnd && m; m = m->next) {
832 11381 : re = m->data;
833 11381 : if (exps_match_col_exps(le, re))
834 2546 : fnd = re;
835 : }
836 10929 : if (fnd && (is_anti(fnd) || is_semantics(fnd)))
837 117 : continue;
838 : /* cases
839 : * 1) 2 values (cmp_equal)
840 : * 2) 1 value (cmp_equal), and cmp_in
841 : * (also cmp_in, cmp_equal)
842 : * 3) 2 cmp_in
843 : * 4) ranges
844 : */
845 2429 : if (fnd) {
846 2429 : re = fnd;
847 2429 : fnd = NULL;
848 2429 : if (is_anti(le) || is_anti(re) || is_symmetric(re))
849 0 : continue;
850 2999 : if (le->flag == cmp_equal && re->flag == cmp_equal) {
851 570 : list *exps = new_exp_list(sql->sa);
852 :
853 570 : append(exps, le->r);
854 570 : append(exps, re->r);
855 570 : fnd = exp_in(sql->sa, le->l, exps, cmp_in);
856 2030 : } else if (le->flag == cmp_equal && re->flag == cmp_in){
857 171 : list *exps = new_exp_list(sql->sa);
858 :
859 171 : append(exps, le->r);
860 171 : list_merge(exps, re->r, NULL);
861 171 : fnd = exp_in(sql->sa, le->l, exps, cmp_in);
862 2743 : } else if (le->flag == cmp_in && re->flag == cmp_equal){
863 1055 : list *exps = new_exp_list(sql->sa);
864 :
865 1055 : append(exps, re->r);
866 1055 : list_merge(exps, le->r, NULL);
867 1055 : fnd = exp_in(sql->sa, le->l, exps, cmp_in);
868 753 : } else if (le->flag == cmp_in && re->flag == cmp_in){
869 120 : list *exps = new_exp_list(sql->sa);
870 :
871 120 : list_merge(exps, le->r, NULL);
872 120 : list_merge(exps, re->r, NULL);
873 120 : fnd = exp_in(sql->sa, le->l, exps, cmp_in);
874 513 : } else if (le->f && re->f && /* merge ranges */
875 21 : le->flag == re->flag && le->flag <= cmp_lt) {
876 21 : sql_exp *mine = NULL, *maxe = NULL;
877 :
878 21 : if (!(mine = rel_binop_(sql, NULL, exp_copy(sql, le->r), exp_copy(sql, re->r), "sys", "sql_min", card_value))) {
879 0 : sql->session->status = 0;
880 0 : sql->errstr[0] = '\0';
881 0 : continue;
882 : }
883 21 : if (!(maxe = rel_binop_(sql, NULL, exp_copy(sql, le->f), exp_copy(sql, re->f), "sys", "sql_max", card_value))) {
884 0 : sql->session->status = 0;
885 0 : sql->errstr[0] = '\0';
886 0 : continue;
887 : }
888 21 : fnd = exp_compare2(sql->sa, exp_copy(sql, le->l), mine, maxe, le->flag, 0);
889 21 : lmerged = false;
890 : }
891 1937 : if (fnd) {
892 1937 : append(nexps, fnd);
893 2977 : *merged = (fnd && lmerged && rmerged);
894 : }
895 : }
896 : }
897 14861 : return nexps;
898 : }
899 :
900 : /* merge related sub expressions
901 : *
902 : * ie (x = a and y > 1 and y < 5) or
903 : * (x = c and y > 1 and y < 10) or
904 : * (x = e and y > 1 and y < 20)
905 : * ->
906 : * ((x = a and y > 1 and y < 5) or
907 : * (x = c and y > 1 and y < 10) or
908 : * (x = e and y > 1 and y < 20)) and
909 : * x in (a,c,e) and
910 : * y > 1 and y < 20
911 : *
912 : * for single expression or's we can do better
913 : * x in (a, b, c) or x in (d, e, f)
914 : * ->
915 : * x in (a, b, c, d, e, f)
916 : * */
917 : static inline sql_rel *
918 418954 : rel_merge_select_rse(visitor *v, sql_rel *rel)
919 : {
920 : /* only execute once per select */
921 418954 : if ((is_select(rel->op) || is_join(rel->op) || is_semi(rel->op)) && rel->exps && !is_rel_merge_select_rse_used(rel->used)) {
922 134614 : node *n, *o;
923 134614 : list *nexps = new_exp_list(v->sql->sa);
924 :
925 289987 : for (n=rel->exps->h; n; n = n->next) {
926 155373 : sql_exp *e = n->data;
927 :
928 162843 : if (e->type == e_cmp && e->flag == cmp_or && !is_anti(e) && !is_semantics(e)) {
929 : /* possibly merge related expressions */
930 7470 : bool merged = false;
931 :
932 7470 : list *ps = exps_merge_select_rse(v->sql, e->l, e->r, &merged);
933 8167 : for (o = ps->h; o; o = o->next)
934 697 : append(nexps, o->data);
935 7470 : if (merged)
936 120 : v->changes++;
937 : else
938 7350 : append(nexps, e);
939 : } else {
940 147903 : append(nexps, e);
941 : }
942 : }
943 134614 : rel->exps = nexps;
944 134614 : rel->used |= rel_merge_select_rse_used;
945 : }
946 418954 : return rel;
947 : }
948 :
949 : /* pack optimizers into a single function call to avoid iterations in the AST */
950 : static sql_rel *
951 4133866 : rel_optimize_select_and_joins_bottomup_(visitor *v, sql_rel *rel)
952 : {
953 4133866 : if (!rel || (!is_join(rel->op) && !is_semi(rel->op) && !is_select(rel->op)) || list_empty(rel->exps))
954 2911296 : return rel;
955 1222570 : uint8_t cycle = *(uint8_t*) v->data;
956 :
957 1222570 : rel->exps = exp_merge_range(v, rel, rel->exps);
958 1222570 : rel = rel_select_cse(v, rel);
959 1222570 : if (cycle == 1)
960 418954 : rel = rel_merge_select_rse(v, rel);
961 1222570 : rel = rewrite_simplify(v, cycle, v->value_based_opt, rel);
962 1222570 : return rel;
963 : }
964 :
965 : static sql_rel *
966 105949 : rel_optimize_select_and_joins_bottomup(visitor *v, global_props *gp, sql_rel *rel)
967 : {
968 105949 : v->data = &gp->opt_cycle;
969 105949 : rel = rel_visitor_bottomup(v, rel, &rel_optimize_select_and_joins_bottomup_);
970 105949 : v->data = gp;
971 105949 : return rel;
972 : }
973 :
974 : run_optimizer
975 596801 : bind_optimize_select_and_joins_bottomup(visitor *v, global_props *gp)
976 : {
977 596801 : int flag = v->sql->sql_optimizer;
978 596613 : return gp->opt_level == 1 && (gp->cnt[op_join] || gp->cnt[op_left] || gp->cnt[op_right] ||
979 526940 : gp->cnt[op_full] || gp->cnt[op_semi] || gp->cnt[op_anti] ||
980 1193414 : gp->cnt[op_select]) && (flag & optimize_select_and_joins_bottomup) ? rel_optimize_select_and_joins_bottomup : NULL;
981 : }
982 :
983 :
984 : static inline sql_rel *
985 3953906 : rel_push_join_exps_down(visitor *v, sql_rel *rel)
986 : {
987 : /* push select exps part of join expressions down */
988 : /* TODO CHECK WHY not semi enabled */
989 3953906 : if ((is_innerjoin(rel->op) || is_left(rel->op) || is_right(rel->op) /*|| is_semi(rel->op)*/) && !list_empty(rel->exps)) {
990 650077 : int left = is_innerjoin(rel->op) || is_right(rel->op) || rel->op == op_semi;
991 650077 : int right = is_innerjoin(rel->op) || is_left(rel->op) || is_semi(rel->op);
992 650077 : sql_rel *jl = rel->l, *ojl = jl, *jr = rel->r, *ojr = jr;
993 :
994 650077 : set_processed(jl);
995 650077 : set_processed(jr);
996 1387091 : for (node *n = rel->exps->h; n;) {
997 737014 : node *next = n->next;
998 737014 : sql_exp *e = n->data;
999 :
1000 737014 : if (left && rel_rebind_exp(v->sql, jl, e) && !is_any(e)) { /* select expressions on left */
1001 1325 : if (!is_select(jl->op) || rel_is_ref(jl))
1002 1312 : rel->l = jl = rel_select(v->sql->sa, jl, NULL);
1003 1325 : rel_select_add_exp(v->sql->sa, jl, e);
1004 1325 : list_remove_node(rel->exps, NULL, n);
1005 1325 : v->changes++;
1006 735689 : } else if (right && rel_rebind_exp(v->sql, jr, e) && !is_any(e)) { /* select expressions on right */
1007 37 : if (!is_select(jr->op) || rel_is_ref(jr))
1008 36 : rel->r = jr = rel_select(v->sql->sa, jr, NULL);
1009 37 : rel_select_add_exp(v->sql->sa, jr, e);
1010 37 : list_remove_node(rel->exps, NULL, n);
1011 37 : v->changes++;
1012 : }
1013 : n = next;
1014 : }
1015 650077 : if (ojl != jl)
1016 1312 : set_processed(jl);
1017 650077 : if (ojr != jr)
1018 36 : set_processed(jr);
1019 : }
1020 3953906 : return rel;
1021 : }
1022 :
1023 : static inline bool
1024 3953906 : is_non_trivial_select_applied_to_outer_join(sql_rel *rel)
1025 : {
1026 3953906 : return is_select(rel->op) && rel->exps && is_outerjoin(((sql_rel*) rel->l)->op);
1027 : }
1028 :
1029 : extern list *list_append_before(list *l, node *n, void *data);
1030 :
1031 : static void replace_column_references_with_nulls_2(mvc *sql, list* crefs, sql_exp* e);
1032 :
1033 : static void
1034 5359 : replace_column_references_with_nulls_1(mvc *sql, list* crefs, list* exps) {
1035 5359 : if (list_empty(exps))
1036 : return;
1037 13544 : for(node* n = exps->h; n; n=n->next) {
1038 8185 : sql_exp* e = n->data;
1039 8185 : replace_column_references_with_nulls_2(sql, crefs, e);
1040 : }
1041 : }
1042 :
1043 : static void
1044 28517 : replace_column_references_with_nulls_2(mvc *sql, list* crefs, sql_exp* e) {
1045 39102 : if (e == NULL) {
1046 : return;
1047 : }
1048 :
1049 32579 : switch (e->type) {
1050 9136 : case e_column:
1051 : {
1052 9136 : sql_exp *c = NULL;
1053 9136 : if (e->l) {
1054 9136 : c = exps_bind_column2(crefs, e->l, e->r, NULL);
1055 : } else {
1056 0 : c = exps_bind_column(crefs, e->r, NULL, NULL, 1);
1057 : }
1058 9136 : if (c) {
1059 2069 : e->type = e_atom;
1060 2069 : e->l = atom_general(sql->sa, &e->tpe, NULL);
1061 2069 : e->r = e->f = NULL;
1062 : }
1063 : }
1064 : break;
1065 9442 : case e_cmp:
1066 9442 : switch (e->flag) {
1067 6968 : case cmp_gt:
1068 : case cmp_gte:
1069 : case cmp_lte:
1070 : case cmp_lt:
1071 : case cmp_equal:
1072 : case cmp_notequal:
1073 : {
1074 6968 : sql_exp* l = e->l;
1075 6968 : sql_exp* r = e->r;
1076 6968 : sql_exp* f = e->f;
1077 :
1078 6968 : replace_column_references_with_nulls_2(sql, crefs, l);
1079 6968 : replace_column_references_with_nulls_2(sql, crefs, r);
1080 6968 : replace_column_references_with_nulls_2(sql, crefs, f);
1081 6968 : break;
1082 : }
1083 1863 : case cmp_filter:
1084 : case cmp_or:
1085 : {
1086 1863 : list* l = e->l;
1087 1863 : list* r = e->r;
1088 1863 : replace_column_references_with_nulls_1(sql, crefs, l);
1089 1863 : replace_column_references_with_nulls_1(sql, crefs, r);
1090 1863 : break;
1091 : }
1092 611 : case cmp_in:
1093 : case cmp_notin:
1094 : {
1095 611 : sql_exp* l = e->l;
1096 611 : list* r = e->r;
1097 611 : replace_column_references_with_nulls_2(sql, crefs, l);
1098 611 : replace_column_references_with_nulls_1(sql, crefs, r);
1099 611 : break;
1100 : }
1101 : default:
1102 : break;
1103 : }
1104 : break;
1105 1022 : case e_func:
1106 : {
1107 1022 : list* l = e->l;
1108 1022 : replace_column_references_with_nulls_1(sql, crefs, l);
1109 1022 : break;
1110 : }
1111 3617 : case e_convert:
1112 : {
1113 3617 : sql_exp* l = e->l;
1114 3617 : replace_column_references_with_nulls_2(sql, crefs, l);
1115 3617 : break;
1116 : }
1117 : default:
1118 : break;
1119 : }
1120 : }
1121 :
1122 : static sql_rel *
1123 5006 : out2inner(visitor *v, sql_rel* sel, sql_rel* join, sql_rel* inner_join_side, operator_type new_type) {
1124 :
1125 : /* handle inner_join relations with a simple select */
1126 5006 : if (is_select(inner_join_side->op) && inner_join_side->l)
1127 5006 : inner_join_side = inner_join_side->l;
1128 5006 : if (!is_base(inner_join_side->op) && !is_simple_project(inner_join_side->op)) {
1129 : // Nothing to do here.
1130 : return sel;
1131 : }
1132 :
1133 4907 : list* inner_join_column_references = inner_join_side->exps;
1134 4907 : list* select_predicates = exps_copy(v->sql, sel->exps);
1135 :
1136 10625 : for(node* n = select_predicates->h; n; n=n->next) {
1137 5785 : sql_exp* e = n->data;
1138 5785 : replace_column_references_with_nulls_2(v->sql, inner_join_column_references, e);
1139 :
1140 5785 : if (exp_is_false(e)) {
1141 67 : join->op = new_type;
1142 67 : v->changes++;
1143 67 : break;
1144 : }
1145 : }
1146 :
1147 : return sel;
1148 : }
1149 :
1150 : static inline sql_rel *
1151 3953906 : rel_out2inner(visitor *v, sql_rel *rel) {
1152 :
1153 3953906 : if (!is_non_trivial_select_applied_to_outer_join(rel)) {
1154 : // Nothing to do here.
1155 : return rel;
1156 : }
1157 :
1158 5009 : sql_rel* join = (sql_rel*) rel->l;
1159 :
1160 5009 : if (rel_is_ref(join)) {
1161 : /* Do not alter a multi-referenced join relation.
1162 : * This is problematic (e.g. in the case of the plan of a merge statement)
1163 : * basically because there are no guarantees on the other container relations.
1164 : * In particular there is no guarantee that the other referencing relations are
1165 : * select relations with null-rejacting predicates on the inner join side.
1166 : */
1167 : return rel;
1168 : }
1169 :
1170 4982 : sql_rel* inner_join_side;
1171 4982 : if (is_left(join->op)) {
1172 4934 : inner_join_side = join->r;
1173 4934 : return out2inner(v, rel, join, inner_join_side, op_join);
1174 : }
1175 48 : else if (is_right(join->op)) {
1176 24 : inner_join_side = join->l;
1177 24 : return out2inner(v, rel, join, inner_join_side, op_join);
1178 : }
1179 : else /*full outer join*/ {
1180 : // First check if left side can degenerate from full outer join to just right outer join.
1181 24 : inner_join_side = join->r;
1182 24 : rel = out2inner(v, rel, join, inner_join_side, op_right);
1183 : /* Now test if the right side can degenerate to
1184 : * a normal inner join or a left outer join
1185 : * depending on the result of previous call to out2inner.
1186 : */
1187 :
1188 24 : inner_join_side = join->l;
1189 40 : return out2inner(v, rel, join, inner_join_side, is_right(join->op)? op_join: op_left);
1190 : }
1191 : }
1192 :
1193 : static bool
1194 2413 : exps_uses_any(list *exps, list *l)
1195 : {
1196 2413 : bool uses_any = false;
1197 :
1198 2413 : if (list_empty(exps) || list_empty(l))
1199 9 : return false;
1200 4814 : for (node *n = l->h; n && !uses_any; n = n->next) {
1201 2410 : sql_exp *e = n->data;
1202 2410 : uses_any |= list_exps_uses_exp(exps, exp_relname(e), exp_name(e)) != NULL;
1203 : }
1204 :
1205 : return uses_any;
1206 : }
1207 :
1208 : /* TODO At the moment I have to disable the new join2semi because the join order optimizer doesn't take semi-joins into account,
1209 : so plans get deteriorated if more joins are optimized into semi-joins. Later I will review the join order with semi-joins and hopefully,
1210 : I will be able to re-enable the new join2semi. */
1211 : #if 0
1212 : #define NO_EXP_FOUND 0
1213 : #define FOUND_WITH_DUPLICATES 1
1214 : #define MAY_HAVE_DUPLICATE_NULLS 2
1215 : #define ALL_VALUES_DISTINCT 3
1216 :
1217 : static int
1218 : find_projection_for_join2semi(sql_rel *rel, sql_exp *jc)
1219 : {
1220 : sql_rel *res = NULL;
1221 : sql_exp *e = NULL;
1222 : bool underjoin = false;
1223 :
1224 : if ((e = rel_find_exp_and_corresponding_rel(rel, jc, &res, &underjoin))) {
1225 : if (underjoin || e->type != e_column)
1226 : return FOUND_WITH_DUPLICATES;
1227 : /* if just one groupby column is projected or the relation needs distinct values and one column is projected or is a primary key, it will be distinct */
1228 : if (is_unique(e) ||
1229 : (is_groupby(res->op) && list_length(res->r) == 1 && exps_find_exp(res->r, e)) ||
1230 : ((is_project(res->op) || is_base(res->op)) && ((need_distinct(res) && list_length(res->exps) == 1) || res->card < CARD_AGGR)))
1231 : return has_nil(e) ? MAY_HAVE_DUPLICATE_NULLS : ALL_VALUES_DISTINCT;
1232 : return FOUND_WITH_DUPLICATES;
1233 : }
1234 : return NO_EXP_FOUND;
1235 : }
1236 :
1237 : static int
1238 : subrel_uses_exp_outside_subrel(visitor *v, sql_rel *rel, list *l, sql_rel *j)
1239 : {
1240 : if (rel == j)
1241 : return 0;
1242 : if (mvc_highwater(v->sql))
1243 : return 1;
1244 : switch(rel->op){
1245 : case op_join:
1246 : case op_left:
1247 : case op_right:
1248 : case op_full:
1249 : return exps_uses_any(rel->exps, l) ||
1250 : subrel_uses_exp_outside_subrel(v, rel->l, l, j) || subrel_uses_exp_outside_subrel(v, rel->r, l, j);
1251 : case op_semi:
1252 : case op_anti:
1253 : case op_select:
1254 : return exps_uses_any(rel->exps, l) ||
1255 : subrel_uses_exp_outside_subrel(v, rel->l, l, j);
1256 : case op_project:
1257 : case op_groupby:
1258 : return exps_uses_any(rel->exps, l) || exps_uses_any(rel->r, l);
1259 : case op_basetable:
1260 : case op_table:
1261 : case op_union:
1262 : case op_except:
1263 : case op_inter:
1264 : return exps_uses_any(rel->exps, l);
1265 : case op_topn:
1266 : case op_sample:
1267 : return subrel_uses_exp_outside_subrel(v, rel->l, l, j);
1268 : default:
1269 : return 1;
1270 : }
1271 : }
1272 :
1273 : static int
1274 : projrel_uses_exp_outside_subrel(visitor *v, sql_rel *rel, list *l, sql_rel *j)
1275 : {
1276 : /* test if projecting relation uses any of the join expressions */
1277 : assert((is_simple_project(rel->op) || is_groupby(rel->op)) && rel->l);
1278 : return exps_uses_any(rel->exps, l) || exps_uses_any(rel->r, l) || subrel_uses_exp_outside_subrel(v, rel->l, l, j);
1279 : }
1280 :
1281 : static sql_rel *
1282 : rewrite_joins2semi(visitor *v, sql_rel *proj, sql_rel *rel)
1283 : {
1284 : /* generalize possibility : we need the visitor 'step' here */
1285 : if (rel_is_ref(rel) || mvc_highwater(v->sql)) /* if the join has multiple references, it's dangerous to convert it into a semijoin */
1286 : return rel;
1287 : if (is_innerjoin(rel->op) && !list_empty(rel->exps)) {
1288 : sql_rel *l = rel->l, *r = rel->r;
1289 : bool left_unique = true, right_unique = true;
1290 :
1291 : /* these relations don't project anything, so skip them */
1292 : while (is_topn(l->op) || is_sample(l->op) || is_select(l->op) || is_semi(l->op))
1293 : l = l->l;
1294 : /* joins will expand values, so don't search on those */
1295 : if (!is_base(l->op) && !is_project(l->op))
1296 : left_unique = false;
1297 : while (is_topn(r->op) || is_sample(r->op) || is_select(r->op) || is_semi(r->op))
1298 : r = r->l;
1299 : if (!is_base(r->op) && !is_project(r->op))
1300 : right_unique = false;
1301 : /* if all columns used in equi-joins from one of the sides are unique, the join can be rewritten into a semijoin */
1302 : for (node *n=rel->exps->h; n && (left_unique || right_unique); n = n->next) {
1303 : sql_exp *e = n->data, *el = e->l, *er = e->r;
1304 :
1305 : if (!is_compare(e->type) || e->flag != cmp_equal || exp_has_func(el) || exp_has_func(er)) {
1306 : left_unique = right_unique = false;
1307 : } else {
1308 : int found = 0;
1309 :
1310 : if (left_unique && (found = find_projection_for_join2semi(l, el)) > NO_EXP_FOUND)
1311 : left_unique &= (found == ALL_VALUES_DISTINCT || (found == MAY_HAVE_DUPLICATE_NULLS && (!is_semantics(e) || !has_nil(er))));
1312 : if (left_unique && (found = find_projection_for_join2semi(l, er)) > NO_EXP_FOUND)
1313 : left_unique &= (found == ALL_VALUES_DISTINCT || (found == MAY_HAVE_DUPLICATE_NULLS && (!is_semantics(e) || !has_nil(el))));
1314 : if (right_unique && (found = find_projection_for_join2semi(r, el)) > NO_EXP_FOUND)
1315 : right_unique &= (found == ALL_VALUES_DISTINCT || (found == MAY_HAVE_DUPLICATE_NULLS && (!is_semantics(e) || !has_nil(er))));
1316 : if (right_unique && (found = find_projection_for_join2semi(r, er)) > NO_EXP_FOUND)
1317 : right_unique &= (found == ALL_VALUES_DISTINCT || (found == MAY_HAVE_DUPLICATE_NULLS && (!is_semantics(e) || !has_nil(el))));
1318 : }
1319 : }
1320 :
1321 : /* now we need to check relation's expressions are not used */
1322 : if (left_unique && !projrel_uses_exp_outside_subrel(v, proj, l->exps, rel)) {
1323 : sql_rel *tmp = rel->r;
1324 : rel->r = rel->l;
1325 : rel->l = tmp;
1326 : rel->op = op_semi;
1327 : v->changes++;
1328 : } else if (right_unique && !projrel_uses_exp_outside_subrel(v, proj, r->exps, rel)) {
1329 : rel->op = op_semi;
1330 : v->changes++;
1331 : }
1332 : }
1333 : if (is_join(rel->op)) {
1334 : rel->l = rewrite_joins2semi(v, proj, rel->l);
1335 : rel->r = rewrite_joins2semi(v, proj, rel->r);
1336 : } else if (is_topn(rel->op) || is_sample(rel->op) || is_select(rel->op) || is_semi(rel->op)) {
1337 : rel->l = rewrite_joins2semi(v, proj, rel->l);
1338 : }
1339 : return rel;
1340 : }
1341 :
1342 : static inline sql_rel *
1343 : rel_join2semijoin(visitor *v, sql_rel *rel)
1344 : {
1345 : if ((is_simple_project(rel->op) || is_groupby(rel->op)) && rel->l)
1346 : rel->l = rewrite_joins2semi(v, rel, rel->l);
1347 : return rel;
1348 : }
1349 : #endif
1350 :
1351 : #define NO_PROJECTION_FOUND 0
1352 : #define MAY_HAVE_DUPLICATE_NULLS 1
1353 : #define ALL_VALUES_DISTINCT 2
1354 :
1355 : static int
1356 857373 : find_projection_for_join2semi(sql_rel *rel)
1357 : {
1358 857373 : if (is_simple_project(rel->op) || is_groupby(rel->op) || is_inter(rel->op) || is_except(rel->op) || is_base(rel->op) || (is_union(rel->op) && need_distinct(rel))) {
1359 422062 : if (rel->card < CARD_AGGR) /* const or groupby without group by exps */
1360 : return ALL_VALUES_DISTINCT;
1361 420902 : if (list_length(rel->exps) == 1) {
1362 7300 : sql_exp *e = rel->exps->h->data;
1363 : /* a single group by column in the projection list from a group by relation is guaranteed to be unique, but not an aggregate */
1364 7300 : if (e->type == e_column) {
1365 7250 : sql_rel *res = NULL;
1366 7250 : sql_exp *found = NULL;
1367 7250 : bool underjoin = false;
1368 :
1369 : /* if just one groupby column is projected or the relation needs distinct values and one column is projected or is a primary key, it will be distinct */
1370 7250 : if ((is_groupby(rel->op) && list_length(rel->r) == 1 && exps_find_exp(rel->r, e)) || (need_distinct(rel) && list_length(rel->exps) == 1))
1371 9133 : return ALL_VALUES_DISTINCT;
1372 2761 : if (is_unique(e))
1373 148 : return has_nil(e) ? MAY_HAVE_DUPLICATE_NULLS : ALL_VALUES_DISTINCT;
1374 :
1375 2613 : if ((is_simple_project(rel->op) || is_groupby(rel->op) || is_inter(rel->op) || is_except(rel->op)) &&
1376 1156 : (found = rel_find_exp_and_corresponding_rel(rel->l, e, false, &res, &underjoin)) && !underjoin) { /* grouping column on inner relation */
1377 1135 : if (need_distinct(res) && list_length(res->exps) == 1)
1378 : return ALL_VALUES_DISTINCT;
1379 1134 : if (is_unique(found))
1380 1 : return has_nil(e) ? MAY_HAVE_DUPLICATE_NULLS : ALL_VALUES_DISTINCT;
1381 1133 : if (found->type == e_column && found->card <= CARD_AGGR) {
1382 5 : if (!is_groupby(res->op) && list_length(res->exps) != 1)
1383 : return NO_PROJECTION_FOUND;
1384 9 : for (node *n = res->exps->h ; n ; n = n->next) { /* must be the single column in the group by expression list */
1385 5 : sql_exp *e = n->data;
1386 5 : if (e != found && e->type == e_column)
1387 : return NO_PROJECTION_FOUND;
1388 : }
1389 : return ALL_VALUES_DISTINCT;
1390 : }
1391 : }
1392 : }
1393 : }
1394 : }
1395 : return NO_PROJECTION_FOUND;
1396 : }
1397 :
1398 : static sql_rel *
1399 2612370 : find_candidate_join2semi(visitor *v, sql_rel *rel, bool *swap)
1400 : {
1401 : /* generalize possibility : we need the visitor 'step' here */
1402 2612965 : if (rel_is_ref(rel)) /* if the join has multiple references, it's dangerous to convert it into a semijoin */
1403 : return NULL;
1404 2478725 : if (rel->op == op_join && !list_empty(rel->exps) && list_empty(rel->attr)) {
1405 430636 : sql_rel *l = rel->l, *r = rel->r;
1406 430636 : int foundr = NO_PROJECTION_FOUND, foundl = NO_PROJECTION_FOUND, found = NO_PROJECTION_FOUND;
1407 430636 : bool ok = false;
1408 :
1409 430636 : foundr = find_projection_for_join2semi(r);
1410 430636 : if (foundr < ALL_VALUES_DISTINCT)
1411 426737 : foundl = find_projection_for_join2semi(l);
1412 430636 : if (foundr && foundr > foundl) {
1413 3902 : *swap = false;
1414 3902 : found = foundr;
1415 426734 : } else if (foundl) {
1416 1901 : *swap = true;
1417 1901 : found = foundl;
1418 : }
1419 :
1420 5803 : if (found > NO_PROJECTION_FOUND) {
1421 : /* if all join expressions can be pushed down or have function calls, then it cannot be rewritten into a semijoin */
1422 11610 : for (node *n=rel->exps->h; n && !ok; n = n->next) {
1423 5807 : sql_exp *e = n->data;
1424 :
1425 9336 : ok |= e->type == e_cmp && e->flag == cmp_equal && !exp_has_func(e) && !rel_rebind_exp(v->sql, l, e) && !rel_rebind_exp(v->sql, r, e) &&
1426 3 : (found == ALL_VALUES_DISTINCT || !is_semantics(e) || !has_nil((sql_exp *)e->l) || !has_nil((sql_exp *)e->r));
1427 : }
1428 : }
1429 :
1430 430636 : if (ok)
1431 : return rel;
1432 : }
1433 2476445 : if (is_join(rel->op) || is_semi(rel->op)) {
1434 652493 : sql_rel *c;
1435 :
1436 652493 : if ((c=find_candidate_join2semi(v, rel->l, swap)) != NULL ||
1437 652363 : (c=find_candidate_join2semi(v, rel->r, swap)) != NULL)
1438 132 : if (list_empty(c->attr))
1439 : return c;
1440 : }
1441 2476313 : if (is_topn(rel->op) || is_sample(rel->op))
1442 595 : return find_candidate_join2semi(v, rel->l, swap);
1443 : return NULL;
1444 : }
1445 :
1446 : static int
1447 592 : subrel_uses_exp_outside_subrel(sql_rel *rel, list *l, sql_rel *c)
1448 : {
1449 592 : if (rel == c)
1450 : return 0;
1451 : /* for subrel only expect joins (later possibly selects) */
1452 213 : if (is_join(rel->op) || is_semi(rel->op)) {
1453 107 : if (exps_uses_any(rel->exps, l))
1454 : return 1;
1455 212 : if (subrel_uses_exp_outside_subrel(rel->l, l, c) ||
1456 106 : subrel_uses_exp_outside_subrel(rel->r, l, c))
1457 0 : return 1;
1458 : }
1459 212 : if (is_topn(rel->op) || is_sample(rel->op))
1460 0 : return subrel_uses_exp_outside_subrel(rel->l, l, c);
1461 : return 0;
1462 : }
1463 :
1464 : static int
1465 2280 : rel_uses_exp_outside_subrel(sql_rel *rel, list *l, sql_rel *c)
1466 : {
1467 : /* for now we only expect sub relations of type project, selects (rel) or join/semi */
1468 2280 : if (is_simple_project(rel->op) || is_groupby(rel->op) || is_select(rel->op)) {
1469 2280 : if (!list_empty(rel->exps) && exps_uses_any(rel->exps, l))
1470 : return 1;
1471 380 : if ((is_simple_project(rel->op) || is_groupby(rel->op)) && !list_empty(rel->r) && exps_uses_any(rel->r, l))
1472 : return 1;
1473 380 : if (rel->l)
1474 380 : return subrel_uses_exp_outside_subrel(rel->l, l, c);
1475 : }
1476 0 : if (is_topn(rel->op) || is_sample(rel->op))
1477 0 : return subrel_uses_exp_outside_subrel(rel->l, l, c);
1478 : return 1;
1479 : }
1480 :
1481 : static inline sql_rel *
1482 3953906 : rel_join2semijoin(visitor *v, sql_rel *rel)
1483 : {
1484 3953906 : if ((is_simple_project(rel->op) || is_groupby(rel->op)) && rel->l) {
1485 1307514 : bool swap = false;
1486 1307514 : sql_rel *l = rel->l;
1487 1307514 : sql_rel *c = find_candidate_join2semi(v, l, &swap);
1488 :
1489 1307514 : if (c) {
1490 : /* 'p' is a project */
1491 2280 : sql_rel *p = swap ? c->l : c->r;
1492 :
1493 : /* now we need to check if ce is only used at the level of c */
1494 2280 : if (!rel_uses_exp_outside_subrel(rel, p->exps, c)) {
1495 379 : c->op = op_semi;
1496 379 : if (swap) {
1497 6 : sql_rel *tmp = c->r;
1498 6 : c->r = c->l;
1499 6 : c->l = tmp;
1500 : }
1501 379 : v->changes++;
1502 : }
1503 : }
1504 : }
1505 3953906 : return rel;
1506 : }
1507 :
1508 : static inline sql_rel *
1509 3953906 : rel_push_join_down_outer(visitor *v, sql_rel *rel)
1510 : {
1511 3953906 : if (is_join(rel->op) && !is_outerjoin(rel->op) && !is_single(rel) && !list_empty(rel->exps) && !rel_is_ref(rel)) {
1512 441973 : sql_rel *l = rel->l, *r = rel->r;
1513 :
1514 441973 : if (is_left(r->op) && (is_select(l->op) || (is_join(l->op) && !is_outerjoin(l->op))) && !rel_is_ref(l) &&
1515 1015 : !rel_is_ref(r)) {
1516 1015 : sql_rel *rl = r->l;
1517 1015 : sql_rel *rr = r->r;
1518 1015 : if (rel_is_ref(rl) || rel_is_ref(rr))
1519 : return rel;
1520 : /* join exps should only include l and r.l */
1521 1015 : list *njexps = sa_list(v->sql->sa);
1522 2493 : for(node *n = rel->exps->h; n; n = n->next) {
1523 1482 : sql_exp *je = n->data;
1524 :
1525 1482 : assert(je->type == e_cmp);
1526 1482 : if (je->f)
1527 : return rel;
1528 1482 : if ((rel_find_exp(l, je->l) && rel_find_exp(rl, je->r)) || (rel_find_exp(l, je->r) && rel_find_exp(rl, je->l))) {
1529 1478 : list_append(njexps, je);
1530 : } else {
1531 4 : return rel;
1532 : }
1533 : }
1534 1011 : sql_rel *nl = rel_crossproduct(v->sql->sa, rel_dup(l), rl, rel->op);
1535 1011 : r->l = nl;
1536 1011 : nl->exps = njexps;
1537 1011 : nl->attr = rel->attr;
1538 1011 : rel->attr = NULL;
1539 1011 : set_processed(nl);
1540 1011 : rel_dup(r);
1541 1011 : rel_destroy(rel);
1542 1011 : rel = r;
1543 1011 : v->changes++;
1544 : }
1545 : }
1546 : return rel;
1547 : }
1548 :
1549 : static sql_rel *
1550 3953906 : rel_optimize_joins_(visitor *v, sql_rel *rel)
1551 : {
1552 3953906 : rel = rel_push_join_exps_down(v, rel);
1553 3953906 : rel = rel_out2inner(v, rel);
1554 3953906 : rel = rel_join2semijoin(v, rel);
1555 3953906 : rel = rel_push_join_down_outer(v, rel);
1556 3953906 : return rel;
1557 : }
1558 :
1559 : static sql_rel *
1560 74205 : rel_optimize_joins(visitor *v, global_props *gp, sql_rel *rel)
1561 : {
1562 74205 : (void) gp;
1563 74205 : return rel_visitor_topdown(v, rel, &rel_optimize_joins_);
1564 : }
1565 :
1566 : run_optimizer
1567 596802 : bind_optimize_joins(visitor *v, global_props *gp)
1568 : {
1569 596802 : int flag = v->sql->sql_optimizer;
1570 596614 : return gp->opt_level == 1 && (gp->cnt[op_join] || gp->cnt[op_left] || gp->cnt[op_right]
1571 1193416 : || gp->cnt[op_full] || gp->cnt[op_semi] || gp->cnt[op_anti]) && (flag & optimize_joins) ? rel_optimize_joins : NULL;
1572 : }
1573 :
1574 :
1575 : static sql_rel *rel_join_order_(visitor *v, sql_rel *rel);
1576 :
1577 : static void
1578 693401 : get_relations(visitor *v, sql_rel *rel, list *rels)
1579 : {
1580 968498 : if (list_empty(rel->attr) && !rel_is_ref(rel) && rel->op == op_join && rel->exps == NULL) {
1581 275097 : sql_rel *l = rel->l;
1582 275097 : sql_rel *r = rel->r;
1583 :
1584 275097 : get_relations(v, l, rels);
1585 275097 : get_relations(v, r, rels);
1586 275097 : rel->l = NULL;
1587 275097 : rel->r = NULL;
1588 275097 : rel_destroy(rel);
1589 : } else {
1590 418304 : rel = rel_join_order_(v, rel);
1591 418304 : append(rels, rel);
1592 : }
1593 693401 : }
1594 :
1595 : static void
1596 193522 : get_inner_relations(mvc *sql, sql_rel *rel, list *rels)
1597 : {
1598 291078 : if (!rel_is_ref(rel) && is_join(rel->op)) {
1599 97556 : sql_rel *l = rel->l;
1600 97556 : sql_rel *r = rel->r;
1601 :
1602 97556 : get_inner_relations(sql, l, rels);
1603 97556 : get_inner_relations(sql, r, rels);
1604 : } else {
1605 193522 : append(rels, rel);
1606 : }
1607 193522 : }
1608 :
1609 : static int
1610 3175370 : exp_count(int *cnt, sql_exp *e)
1611 : {
1612 3175370 : int flag;
1613 3175370 : if (!e)
1614 : return 0;
1615 3175370 : if (find_prop(e->p, PROP_JOINIDX))
1616 6753 : *cnt += 100;
1617 3175370 : if (find_prop(e->p, PROP_HASHCOL))
1618 538917 : *cnt += 100;
1619 3175370 : if (find_prop(e->p, PROP_HASHIDX))
1620 0 : *cnt += 100;
1621 3175370 : switch(e->type) {
1622 1106444 : case e_cmp:
1623 1106444 : if (!is_complex_exp(e->flag)) {
1624 1032860 : exp_count(cnt, e->l);
1625 1032860 : exp_count(cnt, e->r);
1626 1032860 : if (e->f)
1627 3208 : exp_count(cnt, e->f);
1628 : }
1629 1106444 : flag = e->flag;
1630 1106444 : switch (flag) {
1631 999914 : case cmp_equal:
1632 999914 : *cnt += 90;
1633 999914 : return 90;
1634 26337 : case cmp_notequal:
1635 26337 : *cnt += 7;
1636 26337 : return 7;
1637 6609 : case cmp_gt:
1638 : case cmp_gte:
1639 : case cmp_lt:
1640 : case cmp_lte:
1641 6609 : *cnt += 6;
1642 6609 : if (e->l) {
1643 6609 : sql_exp *l = e->l;
1644 6609 : sql_subtype *t = exp_subtype(l);
1645 6609 : if (EC_TEMP(t->type->eclass)) /* give preference to temporal ranges */
1646 150 : *cnt += 90;
1647 : }
1648 6609 : if (e->f){ /* range */
1649 3208 : *cnt += 6;
1650 3208 : return 12;
1651 : }
1652 : return 6;
1653 1090 : case cmp_filter:
1654 1090 : if (exps_card(e->r) > CARD_AGGR) {
1655 : /* filters for joins are special */
1656 14 : *cnt += 1000;
1657 14 : return 1000;
1658 : }
1659 1076 : *cnt += 2;
1660 1076 : return 2;
1661 66766 : case cmp_in:
1662 : case cmp_notin: {
1663 66766 : list *l = e->r;
1664 66766 : int c = 9 - 10*list_length(l);
1665 66766 : *cnt += c;
1666 66766 : return c;
1667 : }
1668 5728 : case cmp_or: /* prefer or over functions */
1669 5728 : *cnt += 3;
1670 5728 : return 3;
1671 : default:
1672 : return 0;
1673 : }
1674 1870170 : case e_column:
1675 1870170 : *cnt += 20;
1676 1870170 : return 20;
1677 139531 : case e_atom:
1678 139531 : *cnt += 10;
1679 139531 : return 10;
1680 6603 : case e_func:
1681 : /* functions are more expensive, depending on the number of columns involved. */
1682 6603 : if (e->card == CARD_ATOM)
1683 : return 0;
1684 6332 : *cnt -= 5*list_length(e->l);
1685 6332 : return 5*list_length(e->l);
1686 52622 : case e_convert:
1687 : /* functions are more expensive, depending on the number of columns involved. */
1688 52622 : if (e->card == CARD_ATOM)
1689 : return 0;
1690 : /* fall through */
1691 : default:
1692 51592 : *cnt -= 5;
1693 51592 : return -5;
1694 : }
1695 : }
1696 :
1697 : int
1698 956261 : exp_keyvalue(sql_exp *e)
1699 : {
1700 956261 : int cnt = 0;
1701 66329 : exp_count(&cnt, e);
1702 956261 : return cnt;
1703 : }
1704 :
1705 : static sql_exp *
1706 784826 : joinexp_col(sql_exp *e, sql_rel *r)
1707 : {
1708 784826 : if (e->type == e_cmp) {
1709 784826 : if (rel_has_exp(r, e->l, false) >= 0)
1710 444223 : return e->l;
1711 340603 : return e->r;
1712 : }
1713 0 : assert(0);
1714 : return NULL;
1715 : }
1716 :
1717 : static int
1718 9865539 : rel_has_exp2(sql_rel *r, sql_exp *e)
1719 : {
1720 9865539 : return rel_has_exp(r, e, false);
1721 : }
1722 :
1723 : static sql_column *
1724 481684 : table_colexp(sql_exp *e, sql_rel *r)
1725 : {
1726 481684 : sql_table *t = r->l;
1727 :
1728 481684 : if (e->type == e_column) {
1729 470791 : const char *name = exp_name(e);
1730 470791 : node *cn;
1731 :
1732 470791 : if (r->exps) { /* use alias */
1733 861391 : for (cn = r->exps->h; cn; cn = cn->next) {
1734 857134 : sql_exp *ce = cn->data;
1735 857134 : if (strcmp(exp_name(ce), name) == 0) {
1736 466534 : name = ce->r;
1737 466534 : break;
1738 : }
1739 : }
1740 : }
1741 995724 : for (cn = ol_first_node(t->columns); cn; cn = cn->next) {
1742 991467 : sql_column *c = cn->data;
1743 991467 : if (strcmp(c->base.name, name) == 0)
1744 466534 : return c;
1745 : }
1746 : }
1747 : return NULL;
1748 : }
1749 :
1750 : static list *
1751 374775 : matching_joins(sql_allocator *sa, list *rels, list *exps, sql_exp *je)
1752 : {
1753 374775 : sql_rel *l, *r;
1754 :
1755 374775 : assert (je->type == e_cmp);
1756 :
1757 374775 : l = find_rel(rels, je->l);
1758 374775 : r = find_rel(rels, je->r);
1759 374775 : if (l && r) {
1760 374621 : list *res;
1761 374621 : list *n_rels = sa_list(sa);
1762 :
1763 374621 : append(n_rels, l);
1764 374621 : append(n_rels, r);
1765 374621 : res = list_select(exps, n_rels, (fcmp) &exp_joins_rels, (fdup)NULL);
1766 374621 : return res;
1767 : }
1768 154 : return sa_list(sa);
1769 : }
1770 :
1771 : static int
1772 6341 : sql_column_kc_cmp(sql_column *c, sql_kc *kc)
1773 : {
1774 : /* return on equality */
1775 6341 : return (c->colnr - kc->c->colnr);
1776 : }
1777 :
1778 : static sql_idx *
1779 457187 : find_fk_index(mvc *sql, sql_table *l, list *lcols, sql_table *r, list *rcols)
1780 : {
1781 457187 : sql_trans *tr = sql->session->tr;
1782 :
1783 457187 : if (l->idxs) {
1784 457187 : node *in;
1785 538965 : for (in = ol_first_node(l->idxs); in; in = in->next){
1786 82582 : sql_idx *li = in->data;
1787 82582 : if (li->type == join_idx) {
1788 8611 : sql_key *rk = (sql_key*)os_find_id(tr->cat->objects, tr, ((sql_fkey*)li->key)->rkey);
1789 8611 : fcmp cmp = (fcmp)&sql_column_kc_cmp;
1790 :
1791 9517 : if (rk->t == r &&
1792 1710 : list_match(lcols, li->columns, cmp) == 0 &&
1793 804 : list_match(rcols, rk->columns, cmp) == 0) {
1794 804 : return li;
1795 : }
1796 : }
1797 : }
1798 : }
1799 : return NULL;
1800 : }
1801 :
1802 : static sql_rel *
1803 749550 : find_basetable( sql_rel *r)
1804 : {
1805 1118543 : if (!r)
1806 : return NULL;
1807 1113520 : switch(r->op) {
1808 564411 : case op_basetable:
1809 564411 : if (!r->l)
1810 0 : return NULL;
1811 : return r;
1812 368993 : case op_semi:
1813 : case op_anti:
1814 : case op_project:
1815 : case op_select:
1816 : case op_topn:
1817 : case op_sample:
1818 368993 : return find_basetable(r->l);
1819 : default:
1820 : return NULL;
1821 : }
1822 : }
1823 :
1824 : static int
1825 113820 : exps_count(list *exps)
1826 : {
1827 113820 : node *n;
1828 113820 : int cnt = 0;
1829 :
1830 113820 : if (!exps)
1831 : return 0;
1832 264001 : for (n = exps->h; n; n=n->next)
1833 150181 : exp_count(&cnt, n->data);
1834 113820 : return cnt;
1835 : }
1836 :
1837 : static list *
1838 346414 : order_join_expressions(mvc *sql, list *dje, list *rels)
1839 : {
1840 346414 : node *n;
1841 346414 : int cnt = list_length(dje);
1842 :
1843 346414 : if (cnt <= 1)
1844 : return dje;
1845 :
1846 152239 : list *res = sa_list(sql->sa);
1847 152239 : int i, *keys = SA_NEW_ARRAY(sql->ta, int, cnt);
1848 152239 : void **data = SA_NEW_ARRAY(sql->ta, void*, cnt);
1849 :
1850 1042171 : for (n = dje->h, i = 0; n; n = n->next, i++) {
1851 889932 : sql_exp *e = n->data;
1852 :
1853 889932 : keys[i] = exp_keyvalue(e);
1854 : /* add some weight for the selections */
1855 889932 : if (e->type == e_cmp && !is_complex_exp(e->flag)) {
1856 889896 : sql_rel *l = find_rel(rels, e->l);
1857 889896 : sql_rel *r = find_rel(rels, e->r);
1858 :
1859 889896 : if (l && is_select(l->op) && l->exps)
1860 63411 : keys[i] += list_length(l->exps)*10 + exps_count(l->exps);
1861 889896 : if (r && is_select(r->op) && r->exps)
1862 50409 : keys[i] += list_length(r->exps)*10 + exps_count(r->exps);
1863 : }
1864 889932 : data[i] = n->data;
1865 : }
1866 : /* sort descending */
1867 152239 : GDKqsort(keys, data, NULL, cnt, sizeof(int), sizeof(void *), TYPE_int, true, true);
1868 1194410 : for(i=0; i<cnt; i++) {
1869 889932 : list_append(res, data[i]);
1870 : }
1871 : return res;
1872 : }
1873 :
1874 : static int
1875 244993 : find_join_rels(list **L, list **R, list *exps, list *rels)
1876 : {
1877 244993 : node *n;
1878 :
1879 244993 : *L = sa_list(exps->sa);
1880 244993 : *R = sa_list(exps->sa);
1881 244993 : if (!exps || list_length(exps) <= 1)
1882 : return -1;
1883 314317 : for(n = exps->h; n; n = n->next) {
1884 237865 : sql_exp *e = n->data;
1885 237865 : sql_rel *l = NULL, *r = NULL;
1886 :
1887 237865 : if (!is_complex_exp(e->flag)){
1888 237853 : l = find_rel(rels, e->l);
1889 237853 : r = find_rel(rels, e->r);
1890 : }
1891 237853 : if (l<r) {
1892 150378 : list_append(*L, l);
1893 150378 : list_append(*R, r);
1894 : } else {
1895 87487 : list_append(*L, r);
1896 87487 : list_append(*R, l);
1897 : }
1898 : }
1899 : return 0;
1900 : }
1901 :
1902 : static list *
1903 76452 : distinct_join_exps(list *aje, list *lrels, list *rrels)
1904 : {
1905 76452 : node *n, *m, *o, *p;
1906 76452 : int len = list_length(aje), i, j;
1907 76452 : char *used = SA_ZNEW_ARRAY(aje->sa, char, len);
1908 76452 : list *res = sa_list(aje->sa);
1909 :
1910 76452 : assert(len == list_length(lrels));
1911 314317 : for(n = lrels->h, m = rrels->h, j = 0; n && m;
1912 237865 : n = n->next, m = m->next, j++) {
1913 237865 : if (n->data && m->data)
1914 989450 : for(o = n->next, p = m->next, i = j+1; o && p;
1915 751650 : o = o->next, p = p->next, i++) {
1916 751650 : if (o->data == n->data && p->data == m->data)
1917 31731 : used[i] = 1;
1918 : }
1919 : }
1920 314317 : for (i = 0, n = aje->h; i < len; n = n->next, i++) {
1921 237865 : if (!used[i])
1922 209896 : list_append(res, n->data);
1923 : }
1924 76452 : return res;
1925 : }
1926 :
1927 : static list *
1928 244993 : find_fk( mvc *sql, list *rels, list *exps)
1929 : {
1930 244993 : node *djn;
1931 244993 : list *sdje, *aje, *dje;
1932 244993 : list *lrels, *rrels;
1933 :
1934 : /* first find the distinct join expressions */
1935 244993 : aje = list_select(exps, rels, (fcmp) &exp_is_join, (fdup)NULL);
1936 : /* add left/right relation */
1937 244993 : if (find_join_rels(&lrels, &rrels, aje, rels) < 0)
1938 : dje = aje;
1939 : else
1940 76452 : dje = distinct_join_exps(aje, lrels, rrels);
1941 623070 : for(djn=dje->h; djn; djn = djn->next) {
1942 : /* equal join expressions */
1943 378162 : sql_idx *idx = NULL;
1944 378162 : sql_exp *je = djn->data, *le = je->l, *re = je->r;
1945 :
1946 378162 : if (is_complex_exp(je->flag))
1947 : break;
1948 378077 : if (!find_prop(je->p, PROP_JOINIDX)) {
1949 374775 : int swapped = 0;
1950 374775 : list *aaje = matching_joins(sql->sa, rels, aje, je);
1951 374775 : list *eje = list_select(aaje, (void*)1, (fcmp) &exp_is_eqjoin, (fdup)NULL);
1952 374775 : sql_rel *lr = find_rel(rels, le), *olr = lr;
1953 374775 : sql_rel *rr = find_rel(rels, re), *orr = rr;
1954 374775 : sql_rel *bt = NULL;
1955 374775 : char *iname;
1956 :
1957 374775 : sql_table *l, *r;
1958 374775 : list *lexps = list_map(eje, lr, (fmap) &joinexp_col);
1959 374775 : list *rexps = list_map(eje, rr, (fmap) &joinexp_col);
1960 374775 : list *lcols, *rcols;
1961 :
1962 374775 : lr = find_basetable(lr);
1963 374775 : rr = find_basetable(rr);
1964 374775 : if (!lr || !rr)
1965 276812 : continue;
1966 243823 : l = lr->l;
1967 243823 : r = rr->l;
1968 243823 : lcols = list_map(lexps, lr, (fmap) &table_colexp);
1969 243823 : rcols = list_map(rexps, rr, (fmap) &table_colexp);
1970 243823 : lcols->destroy = NULL;
1971 243823 : rcols->destroy = NULL;
1972 243823 : if (list_length(lcols) != list_length(rcols))
1973 14907 : continue;
1974 :
1975 228916 : idx = find_fk_index(sql, l, lcols, r, rcols);
1976 228916 : if (!idx) {
1977 228271 : idx = find_fk_index(sql, r, rcols, l, lcols);
1978 228271 : swapped = 1;
1979 : }
1980 :
1981 228916 : if (idx && (iname = sa_strconcat( sql->sa, "%", idx->base.name)) != NULL &&
1982 645 : ((!swapped && name_find_column(olr, NULL, iname, -2, &bt) == NULL) ||
1983 159 : ( swapped && name_find_column(orr, NULL, iname, -2, &bt) == NULL)))
1984 : idx = NULL;
1985 :
1986 : if (idx) {
1987 741 : prop *p;
1988 741 : node *n;
1989 741 : sql_exp *t = NULL, *i = NULL;
1990 :
1991 741 : if (list_length(lcols) > 1 || !mvc_debug_on(sql, 512)) {
1992 :
1993 : /* Add join between idx and TID */
1994 741 : if (swapped) {
1995 143 : sql_exp *s = je->l, *l = je->r;
1996 :
1997 143 : t = rel_find_column(sql->sa, olr, s->l, TID);
1998 143 : i = rel_find_column(sql->sa, orr, l->l, iname);
1999 143 : if (!t || !i)
2000 1 : continue;
2001 142 : je = exp_compare(sql->sa, i, t, cmp_equal);
2002 : } else {
2003 598 : sql_exp *s = je->r, *l = je->l;
2004 :
2005 598 : t = rel_find_column(sql->sa, orr, s->l, TID);
2006 598 : i = rel_find_column(sql->sa, olr, l->l, iname);
2007 598 : if (!t || !i)
2008 0 : continue;
2009 598 : je = exp_compare(sql->sa, i, t, cmp_equal);
2010 : }
2011 :
2012 : /* Remove all join expressions */
2013 1482 : for (n = eje->h; n; n = n->next)
2014 742 : list_remove_data(exps, NULL, n->data);
2015 740 : append(exps, je);
2016 740 : djn->data = je;
2017 0 : } else if (swapped) { /* else keep je for single column expressions */
2018 0 : je = exp_compare(sql->sa, je->r, je->l, cmp_equal);
2019 : /* Remove all join expressions */
2020 0 : for (n = eje->h; n; n = n->next)
2021 0 : list_remove_data(exps, NULL, n->data);
2022 0 : append(exps, je);
2023 0 : djn->data = je;
2024 : }
2025 740 : je->p = p = prop_create(sql->sa, PROP_JOINIDX, je->p);
2026 740 : p->value.pval = idx;
2027 : }
2028 : }
2029 : }
2030 :
2031 : /* sort expressions on weighted number of reducing operators */
2032 244993 : sdje = order_join_expressions(sql, dje, rels);
2033 244993 : return sdje;
2034 : }
2035 :
2036 : static sql_rel *
2037 143207 : order_joins(visitor *v, list *rels, list *exps)
2038 : {
2039 143207 : sql_rel *top = NULL, *l = NULL, *r = NULL;
2040 143207 : sql_exp *cje;
2041 143207 : node *djn;
2042 143207 : list *sdje, *n_rels = sa_list(v->sql->sa);
2043 143207 : int fnd = 0;
2044 143207 : unsigned int rsingle;
2045 :
2046 : /* find foreign keys and reorder the expressions on reducing quality */
2047 143207 : sdje = find_fk(v->sql, rels, exps);
2048 :
2049 143207 : if (list_length(rels) > 2 && mvc_debug_on(v->sql, 256)) {
2050 0 : for(djn = sdje->h; djn; djn = djn->next ) {
2051 0 : sql_exp *e = djn->data;
2052 0 : list_remove_data(exps, NULL, e);
2053 : }
2054 0 : top = rel_planner(v->sql, rels, sdje, exps);
2055 0 : return top;
2056 : }
2057 :
2058 : /* open problem, some expressions use more than 2 relations */
2059 : /* For example a.x = b.y * c.z; */
2060 143207 : if (list_length(rels) >= 2 && sdje->h) {
2061 : /* get the first expression */
2062 143179 : cje = sdje->h->data;
2063 :
2064 : /* find the involved relations */
2065 :
2066 : /* complex expressions may touch multiple base tables
2067 : * Should be pushed up to extra selection.
2068 : * */
2069 143179 : if (cje->type != e_cmp || is_complex_exp(cje->flag) || !find_prop(cje->p, PROP_HASHCOL) ||
2070 29 : (cje->type == e_cmp && cje->f == NULL)) {
2071 143179 : l = find_one_rel(rels, cje->l);
2072 143179 : r = find_one_rel(rels, cje->r);
2073 : }
2074 :
2075 143179 : if (l && r && l != r) {
2076 143056 : list_remove_data(sdje, NULL, cje);
2077 143056 : list_remove_data(exps, NULL, cje);
2078 : }
2079 : }
2080 143207 : if (l && r && l != r) {
2081 143056 : list_remove_data(rels, NULL, l);
2082 143056 : list_remove_data(rels, NULL, r);
2083 143056 : list_append(n_rels, l);
2084 143056 : list_append(n_rels, r);
2085 :
2086 : /* Create a relation between l and r. Since the calling
2087 : functions rewrote the join tree, into a list of expressions
2088 : and a list of (simple) relations, there are no outer joins
2089 : involved, we can simply do a crossproduct here.
2090 : */
2091 143056 : rsingle = is_single(r);
2092 143056 : reset_single(r);
2093 143056 : top = rel_crossproduct(v->sql->sa, l, r, op_join);
2094 143056 : if (rsingle)
2095 7238 : set_single(r);
2096 143056 : rel_join_add_exp(v->sql->sa, top, cje);
2097 :
2098 : /* all other join expressions on these 2 relations */
2099 279660 : for (node *en = exps->h; en; ) {
2100 136604 : node *next = en->next;
2101 136604 : sql_exp *e = en->data;
2102 136604 : if (rel_rebind_exp(v->sql, top, e)) {
2103 3884 : rel_join_add_exp(v->sql->sa, top, e);
2104 3884 : list_remove_data(exps, NULL, e);
2105 : }
2106 : en = next;
2107 : }
2108 : /* Remove other joins on the current 'n_rels' set in the distinct list too */
2109 275631 : for (node *en = sdje->h; en; ) {
2110 132575 : node *next = en->next;
2111 132575 : sql_exp *e = en->data;
2112 132575 : if (rel_rebind_exp(v->sql, top, e))
2113 25 : list_remove_data(sdje, NULL, en->data);
2114 : en = next;
2115 : }
2116 : fnd = 1;
2117 : }
2118 : /* build join tree using the ordered list */
2119 272755 : while(list_length(exps) && fnd) {
2120 129548 : fnd = 0;
2121 : /* find the first expression which could be added */
2122 129548 : if (list_length(sdje) > 1)
2123 81255 : sdje = order_join_expressions(v->sql, sdje, rels);
2124 916008 : for(djn = sdje->h; djn && !fnd && rels->h; djn = (!fnd)?djn->next:NULL) {
2125 393230 : node *ln, *rn, *en;
2126 :
2127 393230 : cje = djn->data;
2128 393230 : ln = list_find(n_rels, cje->l, (fcmp)&rel_has_exp2);
2129 393230 : rn = list_find(n_rels, cje->r, (fcmp)&rel_has_exp2);
2130 :
2131 393230 : if (ln && rn) {
2132 0 : assert(0);
2133 : /* create a selection on the current */
2134 : l = ln->data;
2135 : r = rn->data;
2136 : rel_join_add_exp(v->sql->sa, top, cje);
2137 : fnd = 1;
2138 393230 : } else if (ln || rn) {
2139 129499 : if (ln) {
2140 75005 : l = ln->data;
2141 75005 : r = find_rel(rels, cje->r);
2142 : } else {
2143 54494 : l = rn->data;
2144 54494 : r = find_rel(rels, cje->l);
2145 : }
2146 129499 : if (!r) {
2147 0 : fnd = 1; /* not really, but this bails out */
2148 0 : list_remove_data(sdje, NULL, cje); /* handle later as select */
2149 0 : continue;
2150 : }
2151 :
2152 : /* remove the expression from the lists */
2153 129499 : list_remove_data(sdje, NULL, cje);
2154 129499 : list_remove_data(exps, NULL, cje);
2155 :
2156 129499 : list_remove_data(rels, NULL, r);
2157 129499 : append(n_rels, r);
2158 :
2159 : /* create a join using the current expression */
2160 129499 : rsingle = is_single(r);
2161 129499 : reset_single(r);
2162 129499 : top = rel_crossproduct(v->sql->sa, top, r, op_join);
2163 129499 : if (rsingle)
2164 0 : set_single(r);
2165 129499 : rel_join_add_exp(v->sql->sa, top, cje);
2166 :
2167 : /* all join expressions on these tables */
2168 713205 : for (en = exps->h; en; ) {
2169 583706 : node *next = en->next;
2170 583706 : sql_exp *e = en->data;
2171 583706 : if (rel_rebind_exp(v->sql, top, e)) {
2172 3172 : rel_join_add_exp(v->sql->sa, top, e);
2173 3172 : list_remove_data(exps, NULL, e);
2174 : }
2175 : en = next;
2176 : }
2177 : /* Remove other joins on the current 'n_rels'
2178 : set in the distinct list too */
2179 712823 : for (en = sdje->h; en; ) {
2180 583324 : node *next = en->next;
2181 583324 : sql_exp *e = en->data;
2182 583324 : if (rel_rebind_exp(v->sql, top, e))
2183 3004 : list_remove_data(sdje, NULL, en->data);
2184 : en = next;
2185 : }
2186 : fnd = 1;
2187 : }
2188 : }
2189 : }
2190 143207 : if (list_length(rels)) { /* more relations */
2191 1123 : node *n;
2192 3816 : for(n=rels->h; n; n = n->next) {
2193 2693 : sql_rel *nr = n->data;
2194 :
2195 2693 : if (top) {
2196 2542 : rsingle = is_single(nr);
2197 2542 : reset_single(nr);
2198 2542 : top = rel_crossproduct(v->sql->sa, top, nr, op_join);
2199 2542 : if (rsingle)
2200 0 : set_single(nr);
2201 : } else
2202 : top = nr;
2203 : }
2204 : }
2205 143207 : if (list_length(exps)) { /* more expressions (add selects) */
2206 200 : top = rel_select(v->sql->sa, top, NULL);
2207 415 : for(node *n=exps->h; n; n = n->next) {
2208 215 : sql_exp *e = n->data;
2209 :
2210 : /* find the involved relations */
2211 :
2212 : /* complex expressions may touch multiple base tables
2213 : * Should be push up to extra selection. */
2214 : /*
2215 : l = find_one_rel(rels, e->l);
2216 : r = find_one_rel(rels, e->r);
2217 :
2218 : if (l && r)
2219 : */
2220 215 : if (exp_is_join_exp(e) == 0) {
2221 198 : sql_rel *nr = NULL;
2222 198 : if (is_theta_exp(e->flag)) {
2223 121 : nr = rel_push_join(v->sql, top->l, e->l, e->r, e->f, e, 0);
2224 77 : } else if (e->flag == cmp_filter || e->flag == cmp_or) {
2225 77 : sql_exp *l = exps_find_one_multi_exp(e->l), *r = exps_find_one_multi_exp(e->r);
2226 77 : if (l && r)
2227 71 : nr = rel_push_join(v->sql, top->l, l, r, NULL, e, 0);
2228 : }
2229 192 : if (!nr)
2230 6 : rel_join_add_exp(v->sql->sa, top->l, e);
2231 : } else
2232 17 : rel_select_add_exp(v->sql->sa, top, e);
2233 : }
2234 200 : if (list_empty(top->exps)) { /* empty select */
2235 183 : sql_rel *l = top->l;
2236 183 : top->l = NULL;
2237 183 : rel_destroy(top);
2238 183 : top = l;
2239 : }
2240 : }
2241 : return top;
2242 : }
2243 :
2244 : static int
2245 418304 : rel_neg_in_size(sql_rel *r)
2246 : {
2247 418304 : if (is_union(r->op) && r->nrcols == 0)
2248 0 : return -1 + rel_neg_in_size(r->l);
2249 418304 : if (is_project(r->op) && r->nrcols == 0)
2250 0 : return -1;
2251 : return 0;
2252 : }
2253 :
2254 418304 : static void _rel_destroy(void *dummy, sql_rel *rel)
2255 : {
2256 418304 : (void)dummy;
2257 418304 : rel_destroy(rel);
2258 418304 : }
2259 :
2260 : static list *
2261 143207 : push_in_join_down(mvc *sql, list *rels, list *exps)
2262 : {
2263 143207 : node *n;
2264 143207 : int restart = 1;
2265 143207 : list *nrels;
2266 :
2267 : /* we should sort these first, ie small in's before large one's */
2268 143207 : nrels = list_sort(rels, (fkeyvalue)&rel_neg_in_size, (fdup)&rel_dup);
2269 :
2270 : /* we need to cleanup, the new refs ! */
2271 143207 : rels->destroy = (fdestroy)_rel_destroy;
2272 143207 : list_destroy(rels);
2273 143207 : rels = nrels;
2274 :
2275 : /* one of the rels should be a op_union with nrcols == 0 */
2276 429621 : while (restart) {
2277 561511 : for (n = rels->h; n; n = n->next) {
2278 418304 : sql_rel *r = n->data;
2279 :
2280 418304 : restart = 0;
2281 418304 : if (is_project(r->op) && r->nrcols == 0) {
2282 : /* next step find expression on this relation */
2283 0 : node *m;
2284 0 : sql_rel *l = NULL;
2285 0 : sql_exp *je = NULL;
2286 :
2287 0 : for(m = exps->h; !je && m; m = m->next) {
2288 0 : sql_exp *e = m->data;
2289 :
2290 0 : if (e->type == e_cmp && e->flag == cmp_equal) {
2291 : /* in values are on
2292 : the right of the join */
2293 0 : if (rel_has_exp(r, e->r, false) >= 0)
2294 0 : je = e;
2295 : }
2296 : }
2297 : /* with this expression find other relation */
2298 0 : if (je && (l = find_rel(rels, je->l)) != NULL) {
2299 0 : unsigned int rsingle = is_single(r);
2300 0 : reset_single(r);
2301 0 : sql_rel *nr = rel_crossproduct(sql->sa, l, r, op_join);
2302 0 : if (rsingle)
2303 0 : set_single(r);
2304 0 : rel_join_add_exp(sql->sa, nr, je);
2305 0 : list_append(rels, nr);
2306 0 : list_remove_data(rels, NULL, l);
2307 0 : list_remove_data(rels, NULL, r);
2308 0 : list_remove_data(exps, NULL, je);
2309 0 : restart = 1;
2310 0 : break;
2311 : }
2312 :
2313 : }
2314 : }
2315 : }
2316 143207 : return rels;
2317 : }
2318 :
2319 : static list *
2320 719441 : push_up_join_exps( mvc *sql, sql_rel *rel)
2321 : {
2322 719441 : if (rel_is_ref(rel))
2323 : return NULL;
2324 :
2325 683838 : switch(rel->op) {
2326 286267 : case op_join: {
2327 286267 : sql_rel *rl = rel->l;
2328 286267 : sql_rel *rr = rel->r;
2329 286267 : list *l, *r;
2330 :
2331 286267 : if (rel_is_ref(rl) && rel_is_ref(rr)) {
2332 1 : l = rel->exps;
2333 1 : rel->exps = NULL;
2334 1 : return l;
2335 : }
2336 286266 : l = push_up_join_exps(sql, rl);
2337 286266 : r = push_up_join_exps(sql, rr);
2338 286266 : if (l && r) {
2339 244 : l = list_merge(l, r, (fdup)NULL);
2340 244 : r = NULL;
2341 286022 : } else if (!l) {
2342 162987 : l = r;
2343 162987 : r = NULL;
2344 : }
2345 286266 : if (rel->exps) {
2346 256946 : if (l && !r)
2347 113466 : r = l;
2348 256946 : l = list_merge(rel->exps, r, (fdup)NULL);
2349 : }
2350 286266 : rel->exps = NULL;
2351 286266 : return l;
2352 : }
2353 : default:
2354 : return NULL;
2355 : }
2356 : }
2357 :
2358 : static sql_rel *
2359 272894 : reorder_join(visitor *v, sql_rel *rel)
2360 : {
2361 272894 : list *exps, *rels;
2362 :
2363 272894 : if (is_innerjoin(rel->op) && !is_single(rel) && !rel_is_ref(rel) && list_empty(rel->attr)) {
2364 167722 : if (list_empty(rel->exps)) {
2365 25250 : sql_rel *l = rel->l, *r = rel->r;
2366 25250 : if (!is_innerjoin(l->op) && !is_innerjoin(r->op))
2367 : return rel;
2368 : }
2369 146909 : rel->exps = push_up_join_exps(v->sql, rel);
2370 : }
2371 :
2372 252081 : if (!is_innerjoin(rel->op) || is_single(rel) || rel_is_ref(rel) || list_empty(rel->exps) || !list_empty(rel->attr)) {
2373 108874 : if (!list_empty(rel->exps)) { /* cannot add join idxs to cross products */
2374 95966 : exps = rel->exps;
2375 95966 : rel->exps = NULL; /* should be all crosstables by now */
2376 95966 : rels = sa_list(v->sql->ta);
2377 : /* try to use an join index also for outer joins */
2378 95966 : get_inner_relations(v->sql, rel, rels);
2379 95966 : int cnt = list_length(exps);
2380 95966 : rel->exps = find_fk(v->sql, rels, exps);
2381 95966 : if (list_length(rel->exps) != cnt)
2382 20166 : rel->exps = order_join_expressions(v->sql, exps, rels);
2383 : }
2384 108874 : rel->l = rel_join_order_(v, rel->l);
2385 108874 : rel->r = rel_join_order_(v, rel->r);
2386 : } else {
2387 143207 : exps = rel->exps;
2388 143207 : rel->exps = NULL; /* should be all crosstables by now */
2389 143207 : rels = sa_list(v->sql->ta);
2390 143207 : get_relations(v, rel, rels);
2391 143207 : if (list_length(rels) > 1) {
2392 143207 : rels = push_in_join_down(v->sql, rels, exps);
2393 143207 : rel = order_joins(v, rels, exps);
2394 : } else {
2395 0 : rel->exps = exps;
2396 : }
2397 : }
2398 : return rel;
2399 : }
2400 :
2401 : static sql_rel *
2402 2166722 : rel_join_order_(visitor *v, sql_rel *rel)
2403 : {
2404 2166722 : if (!rel)
2405 : return rel;
2406 :
2407 2156744 : switch (rel->op) {
2408 : case op_basetable:
2409 : break;
2410 3475 : case op_table:
2411 3475 : if (IS_TABLE_PROD_FUNC(rel->flag) || rel->flag == TABLE_FROM_RELATION)
2412 3475 : rel->l = rel_join_order_(v, rel->l);
2413 : break;
2414 : case op_join:
2415 : case op_left:
2416 : case op_right:
2417 : case op_full:
2418 : break;
2419 :
2420 182412 : case op_semi:
2421 : case op_anti:
2422 :
2423 : case op_union:
2424 : case op_inter:
2425 : case op_except:
2426 : case op_merge:
2427 182412 : rel->l = rel_join_order_(v, rel->l);
2428 182412 : rel->r = rel_join_order_(v, rel->r);
2429 182412 : break;
2430 1091941 : case op_project:
2431 : case op_select:
2432 : case op_groupby:
2433 : case op_topn:
2434 : case op_sample:
2435 1091941 : rel->l = rel_join_order_(v, rel->l);
2436 1091941 : break;
2437 4008 : case op_ddl:
2438 4008 : if (rel->flag == ddl_output || rel->flag == ddl_create_seq || rel->flag == ddl_alter_seq || rel->flag == ddl_alter_table || rel->flag == ddl_create_table || rel->flag == ddl_create_view) {
2439 0 : rel->l = rel_join_order_(v, rel->l);
2440 : } else if (rel->flag == ddl_list || rel->flag == ddl_exception) {
2441 53 : rel->l = rel_join_order_(v, rel->l);
2442 53 : rel->r = rel_join_order_(v, rel->r);
2443 : }
2444 : break;
2445 2215 : case op_insert:
2446 : case op_update:
2447 : case op_delete:
2448 2215 : rel->r = rel_join_order_(v, rel->r);
2449 2215 : break;
2450 : case op_truncate:
2451 : break;
2452 : }
2453 2156744 : if (is_join(rel->op))
2454 272894 : rel = reorder_join(v, rel);
2455 : return rel;
2456 : }
2457 :
2458 : static sql_rel *
2459 68109 : rel_join_order(visitor *v, global_props *gp, sql_rel *rel)
2460 : {
2461 68109 : (void) gp;
2462 68109 : sql_rel *r = rel_join_order_(v, rel);
2463 68109 : sa_reset(v->sql->ta);
2464 68109 : return r;
2465 : }
2466 :
2467 : run_optimizer
2468 596802 : bind_join_order(visitor *v, global_props *gp)
2469 : {
2470 596802 : int flag = v->sql->sql_optimizer;
2471 596614 : return gp->opt_level == 1 && !gp->cnt[op_update] && (gp->cnt[op_join] || gp->cnt[op_left] ||
2472 1187761 : gp->cnt[op_right] || gp->cnt[op_full]) && (flag & join_order) ? rel_join_order : NULL;
2473 : }
2474 :
2475 : /* this join order is to be done once after statistics are gathered */
2476 : run_optimizer
2477 686775 : bind_join_order2(visitor *v, global_props *gp)
2478 : {
2479 : /*int flag = v->sql->sql_optimizer;
2480 : return gp->opt_level == 1 && !gp->has_special_modify && !gp->cnt[op_update] && (gp->cnt[op_join] || gp->cnt[op_left] ||
2481 : gp->cnt[op_right] || gp->cnt[op_full]) && (flag & join_order) ? rel_join_order : NULL;*/
2482 : /* TODO we have to propagate count statistics here */
2483 686775 : (void) v;
2484 686775 : (void) gp;
2485 686775 : return NULL;
2486 : }
2487 :
2488 :
2489 : static int
2490 10046 : is_identity_of(sql_exp *e, sql_rel *l)
2491 : {
2492 10046 : if (e->type != e_cmp)
2493 : return 0;
2494 10020 : if (!is_identity(e->l, l) || !is_identity(e->r, l) || (e->f && !is_identity(e->f, l)))
2495 10020 : return 0;
2496 : return 1;
2497 : }
2498 :
2499 : static inline sql_rel *
2500 14829 : rel_rewrite_semijoin(visitor *v, sql_rel *rel)
2501 : {
2502 14829 : assert(is_semi(rel->op));
2503 : {
2504 14829 : sql_rel *l = rel->l;
2505 14829 : sql_rel *r = rel->r;
2506 14829 : sql_rel *rl = (r->l)?r->l:NULL;
2507 14829 : int on_identity = 1;
2508 :
2509 14829 : if (!rel->exps || list_length(rel->exps) != 1 || !is_identity_of(rel->exps->h->data, l))
2510 : on_identity = 0;
2511 :
2512 : /* rewrite {semi,anti}join (A, join(A,B)) into {semi,anti}join (A,B)
2513 : * and {semi,anti}join (A, join(B,A)) into {semi,anti}join (A,B)
2514 : * Where the semi/anti join is done using the identity */
2515 0 : if (on_identity && l->ref.refcnt == 2 && ((is_join(r->op) && (l == r->l || l == r->r)) ||
2516 0 : (is_project(r->op) && rl && is_join(rl->op) && (l == rl->l || l == rl->r)))){
2517 0 : sql_rel *or = r;
2518 :
2519 0 : if (is_project(r->op))
2520 0 : r = rl;
2521 :
2522 0 : if (l == r->r)
2523 0 : rel->r = rel_dup(r->l);
2524 : else
2525 0 : rel->r = rel_dup(r->r);
2526 :
2527 0 : rel->exps = r->exps;
2528 0 : r->exps = NULL;
2529 0 : rel->attr = r->attr;
2530 0 : r->attr = NULL;
2531 0 : rel_destroy(or);
2532 0 : v->changes++;
2533 : }
2534 : }
2535 : {
2536 14829 : sql_rel *l = rel->l, *rl = NULL;
2537 14829 : sql_rel *r = rel->r, *or = r;
2538 :
2539 14829 : if (r)
2540 14829 : rl = r->l;
2541 14829 : if (r && is_project(r->op)) {
2542 14543 : r = rl;
2543 14543 : if (r)
2544 14436 : rl = r->l;
2545 : }
2546 :
2547 : /* More general case is (join reduction)
2548 : {semi,anti}join (A, join(A,B) [A.c1 == B.c1]) [ A.c1 == B.c1 ]
2549 : into {semi,anti}join (A,B) [ A.c1 == B.c1 ]
2550 :
2551 : for semijoin also A.c1 == B.k1 ] [ A.c1 == B.k2 ] could be rewriten
2552 : */
2553 14829 : if (l && r && rl &&
2554 14467 : is_basetable(l->op) && is_basetable(rl->op) &&
2555 2133 : is_join(r->op) && l->l == rl->l)
2556 : {
2557 18 : node *n, *m;
2558 18 : list *exps;
2559 :
2560 35 : if (!rel->exps || !r->exps ||
2561 17 : list_length(rel->exps) != list_length(r->exps))
2562 1 : return rel;
2563 17 : exps = new_exp_list(v->sql->sa);
2564 :
2565 : /* are the join conditions equal */
2566 17 : for (n = rel->exps->h, m = r->exps->h;
2567 17 : n && m; n = n->next, m = m->next)
2568 : {
2569 17 : sql_exp *le = NULL, *oe = n->data;
2570 17 : sql_exp *re = NULL, *ne = m->data;
2571 17 : sql_column *cl;
2572 17 : int equal = 0;
2573 :
2574 17 : if (oe->type != e_cmp || ne->type != e_cmp ||
2575 17 : oe->flag != cmp_equal ||
2576 17 : ne->flag != cmp_equal || is_anti(oe) || is_anti(ne))
2577 : return rel;
2578 :
2579 17 : if ((cl = exp_find_column(rel->l, oe->l, -2)) != NULL) {
2580 17 : le = oe->l;
2581 17 : re = oe->r;
2582 0 : } else if ((cl = exp_find_column(rel->l, oe->r, -2)) != NULL) {
2583 0 : le = oe->r;
2584 0 : re = oe->l;
2585 : } else
2586 : return rel;
2587 :
2588 17 : if (exp_find_column(rl, ne->l, -2) == cl) {
2589 17 : sql_exp *e = (or != r)?rel_find_exp(or, re):re;
2590 :
2591 17 : if (e)
2592 16 : equal = exp_match_exp(ne->r, e);
2593 17 : if (!e || !equal)
2594 : return rel;
2595 0 : re = ne->r;
2596 0 : } else if (exp_find_column(rl, ne->r, -2) == cl) {
2597 0 : sql_exp *e = (or != r)?rel_find_exp(or, re):re;
2598 :
2599 0 : if (e)
2600 0 : equal = exp_match_exp(ne->l, e);
2601 0 : if (!e || !equal)
2602 : return rel;
2603 0 : re = ne->l;
2604 : } else
2605 : return rel;
2606 :
2607 0 : ne = exp_compare(v->sql->sa, le, re, cmp_equal);
2608 0 : append(exps, ne);
2609 : }
2610 :
2611 0 : rel->r = rel_dup(r->r);
2612 0 : rel->exps = exps;
2613 0 : rel_destroy(or);
2614 0 : v->changes++;
2615 : }
2616 : }
2617 : return rel;
2618 : }
2619 :
2620 : /*
2621 : * Push semijoins down, pushes the semijoin through a join.
2622 : *
2623 : * semijoin( join(A, B) [ A.x == B.y ], C ) [ A.z == C.c ]
2624 : * ->
2625 : * join( semijoin(A, C) [ A.z == C.c ], B ) [ A.x == B.y ]
2626 : *
2627 : * also push simple expressions of a semijoin down if they only
2628 : * involve the left sided of the semijoin.
2629 : *
2630 : * in some cases the other way is useful, ie push join down
2631 : * semijoin. When the join reduces (ie when there are selects on it).
2632 : *
2633 : * At the moment, we only flag changes by this optimizer on the first level of optimization
2634 : */
2635 : static inline sql_rel *
2636 545855 : rel_push_semijoin_down_or_up(visitor *v, sql_rel *rel)
2637 : {
2638 545855 : uint8_t cycle = *(uint8_t*) v->data;
2639 :
2640 545855 : if (rel->op == op_join && rel->exps && rel->l) {
2641 510962 : sql_rel *l = rel->l, *r = rel->r;
2642 :
2643 510962 : if (is_semi(l->op) && !rel_is_ref(l) && is_select(r->op) && !rel_is_ref(r)) {
2644 28 : rel->l = l->l;
2645 28 : l->l = rel;
2646 28 : if (cycle <= 0)
2647 0 : v->changes++;
2648 28 : return l;
2649 : }
2650 : }
2651 : /* also case with 2 joins */
2652 : /* join ( join ( semijoin(), table), select (table)); */
2653 545827 : if (rel->op == op_join && rel->exps && rel->l) {
2654 510934 : sql_rel *l = rel->l, *r = rel->r;
2655 510934 : sql_rel *ll;
2656 :
2657 510934 : if (is_join(l->op) && !rel_is_ref(l) && is_select(r->op) && !rel_is_ref(r)) {
2658 9562 : ll = l->l;
2659 9562 : if (is_semi(ll->op) && !rel_is_ref(ll)) {
2660 42 : l->l = ll->l;
2661 42 : ll->l = rel;
2662 42 : if (cycle <= 0)
2663 12 : v->changes++;
2664 42 : return ll;
2665 : }
2666 : }
2667 : }
2668 : /* first push down the expressions involving only A */
2669 545785 : if (rel->op == op_semi && rel->exps && rel->l) {
2670 7970 : sql_rel *jl = rel->l, *ojl = jl;
2671 :
2672 7970 : set_processed(jl);
2673 19788 : for (node *n = rel->exps->h; n;) {
2674 11818 : node *next = n->next;
2675 11818 : sql_exp *e = n->data;
2676 :
2677 11818 : if (n != rel->exps->h && e->type == e_cmp && rel_rebind_exp(v->sql, jl, e)) {
2678 19 : if (!is_select(jl->op) || rel_is_ref(jl))
2679 16 : rel->l = jl = rel_select(v->sql->sa, jl, NULL);
2680 19 : rel_select_add_exp(v->sql->sa, jl, e);
2681 19 : list_remove_node(rel->exps, NULL, n);
2682 19 : if (cycle <= 0)
2683 19 : v->changes++;
2684 : }
2685 : n = next;
2686 : }
2687 7970 : if (ojl != jl)
2688 16 : set_processed(jl);
2689 : }
2690 545785 : if (rel->op == op_semi && rel->exps && rel->l) {
2691 7970 : operator_type op = rel->op, lop;
2692 7970 : node *n;
2693 7970 : sql_rel *l = rel->l, *ll = NULL, *lr = NULL;
2694 7970 : sql_rel *r = rel->r;
2695 7970 : list *exps = rel->exps, *nsexps, *njexps, *nsattr, *njattr;
2696 7970 : int left = 1, right = 1;
2697 :
2698 : /* handle project
2699 : if (l->op == op_project && !need_distinct(l))
2700 : l = l->l;
2701 : */
2702 :
2703 7970 : if (!is_join(l->op) || is_full(l->op) || rel_is_ref(l) || is_single(l))
2704 : return rel;
2705 :
2706 580 : lop = l->op;
2707 580 : ll = l->l;
2708 580 : lr = l->r;
2709 :
2710 : /* check which side is used and other exps are atoms or from right of semijoin */
2711 1076 : for(n = exps->h; n; n = n->next) {
2712 597 : sql_exp *sje = n->data;
2713 :
2714 597 : if (sje->type != e_cmp || is_complex_exp(sje->flag))
2715 : return rel;
2716 : /* sje->l from ll and sje->r/f from semijoin r ||
2717 : * sje->l from semijoin r and sje->r/f from ll ||
2718 : * sje->l from lr and sje->r/f from semijoin r ||
2719 : * sje->l from semijoin r and sje->r/f from lr */
2720 1139 : if (left &&
2721 1139 : ((rel_rebind_exp(v->sql, ll, sje->l) && rel_rebind_exp(v->sql, rel->r, sje->r) && (!sje->f || rel_rebind_exp(v->sql, rel->r, sje->f))) ||
2722 407 : (rel_rebind_exp(v->sql, rel->r, sje->l) && rel_rebind_exp(v->sql, ll, sje->r) && (!sje->f || rel_rebind_exp(v->sql, ll, sje->f)))))
2723 : right = 0;
2724 : else
2725 343 : left = 0;
2726 672 : if (right &&
2727 659 : ((rel_rebind_exp(v->sql, lr, sje->l) && rel_rebind_exp(v->sql, rel->r, sje->r) && (!sje->f || rel_rebind_exp(v->sql, rel->r, sje->f))) ||
2728 68 : (rel_rebind_exp(v->sql, rel->r, sje->l) && rel_rebind_exp(v->sql, lr, sje->r) && (!sje->f || rel_rebind_exp(v->sql, lr, sje->f)))))
2729 : left = 0;
2730 : else
2731 : right = 0;
2732 570 : if (!right && !left)
2733 : return rel;
2734 : }
2735 479 : if (left && is_right(lop))
2736 : return rel;
2737 479 : if (right && is_left(lop))
2738 : return rel;
2739 477 : nsexps = exps_copy(v->sql, rel->exps);
2740 477 : nsattr = exps_copy(v->sql, rel->attr);
2741 477 : njexps = exps_copy(v->sql, l->exps);
2742 477 : njattr = exps_copy(v->sql, l->attr);
2743 477 : if (left)
2744 211 : l = rel_crossproduct(v->sql->sa, rel_dup(ll), rel_dup(r), op);
2745 : else
2746 266 : l = rel_crossproduct(v->sql->sa, rel_dup(lr), rel_dup(r), op);
2747 477 : l->exps = nsexps;
2748 477 : l->attr = nsattr;
2749 477 : set_processed(l);
2750 477 : if (left)
2751 211 : l = rel_crossproduct(v->sql->sa, l, rel_dup(lr), lop);
2752 : else
2753 266 : l = rel_crossproduct(v->sql->sa, rel_dup(ll), l, lop);
2754 477 : l->exps = njexps;
2755 477 : l->attr = njattr;
2756 477 : set_processed(l);
2757 477 : rel_destroy(rel);
2758 477 : rel = l;
2759 477 : if (cycle <= 0)
2760 328 : v->changes++;
2761 : }
2762 : return rel;
2763 : }
2764 :
2765 : /* antijoin(a, union(b,c)) -> antijoin(antijoin(a,b), c) */
2766 : static inline sql_rel *
2767 6799 : rel_rewrite_antijoin(visitor *v, sql_rel *rel)
2768 : {
2769 6799 : sql_rel *l = rel->l;
2770 6799 : sql_rel *r = rel->r;
2771 :
2772 6799 : assert(rel->op == op_anti);
2773 6799 : if (l && !rel_is_ref(l) && r && !rel_is_ref(r) && is_union(r->op) && !is_single(r)) {
2774 310 : sql_rel *rl = rel_dup(r->l), *nl;
2775 310 : sql_rel *rr = rel_dup(r->r);
2776 :
2777 310 : if (!is_project(rl->op))
2778 0 : rl = rel_project(v->sql->sa, rl,
2779 : rel_projections(v->sql, rl, NULL, 1, 1));
2780 310 : if (!is_project(rr->op))
2781 0 : rr = rel_project(v->sql->sa, rr,
2782 : rel_projections(v->sql, rr, NULL, 1, 1));
2783 310 : rel_rename_exps(v->sql, r->exps, rl->exps);
2784 310 : rel_rename_exps(v->sql, r->exps, rr->exps);
2785 :
2786 310 : nl = rel_crossproduct(v->sql->sa, rel->l, rl, op_anti);
2787 310 : nl->exps = exps_copy(v->sql, rel->exps);
2788 310 : nl->attr = exps_copy(v->sql, rel->attr);
2789 310 : set_processed(nl);
2790 310 : rel->l = nl;
2791 310 : rel->r = rr;
2792 310 : rel_destroy(r);
2793 310 : v->changes++;
2794 310 : return rel;
2795 : }
2796 : return rel;
2797 : }
2798 :
2799 : static sql_rel *
2800 3953927 : rel_optimize_semi_and_anti_(visitor *v, sql_rel *rel)
2801 : {
2802 : /* rewrite semijoin (A, join(A,B)) into semijoin (A,B) */
2803 3953927 : if (rel && is_semi(rel->op))
2804 14829 : rel = rel_rewrite_semijoin(v, rel);
2805 : /* push semijoin through join */
2806 3953927 : if (rel && (is_semi(rel->op) || is_innerjoin(rel->op)))
2807 545855 : rel = rel_push_semijoin_down_or_up(v, rel);
2808 : /* antijoin(a, union(b,c)) -> antijoin(antijoin(a,b), c) */
2809 3953927 : if (rel && rel->op == op_anti)
2810 6799 : rel = rel_rewrite_antijoin(v, rel);
2811 3953927 : return rel;
2812 : }
2813 :
2814 : static sql_rel *
2815 74205 : rel_optimize_semi_and_anti(visitor *v, global_props *gp, sql_rel *rel)
2816 : {
2817 74205 : v->data = &gp->opt_cycle;
2818 74205 : rel = rel_visitor_bottomup(v, rel, &rel_optimize_semi_and_anti_);
2819 74205 : v->data = gp;
2820 74205 : return rel;
2821 : }
2822 :
2823 : run_optimizer
2824 596801 : bind_optimize_semi_and_anti(visitor *v, global_props *gp)
2825 : {
2826 : /* Important -> Re-write semijoins after rel_join_order */
2827 596801 : int flag = v->sql->sql_optimizer;
2828 596613 : return gp->opt_level == 1 && (gp->cnt[op_join] || gp->cnt[op_left] || gp->cnt[op_right]
2829 1193414 : || gp->cnt[op_full] || gp->cnt[op_semi] || gp->cnt[op_anti]) && (flag & optimize_semi_and_anti) ? rel_optimize_semi_and_anti : NULL;
2830 : }
2831 :
2832 :
2833 : static sql_rel *
2834 1650685 : rel_semijoin_use_fk(visitor *v, sql_rel *rel)
2835 : {
2836 1650685 : if (is_semi(rel->op) && rel->exps) {
2837 5820 : list *exps = rel->exps;
2838 5820 : list *rels = sa_list(v->sql->sa);
2839 :
2840 5820 : rel->exps = NULL;
2841 5820 : append(rels, rel->l);
2842 5820 : append(rels, rel->r);
2843 5820 : (void) find_fk( v->sql, rels, exps);
2844 :
2845 5820 : rel->exps = exps;
2846 : }
2847 1650685 : return rel;
2848 : }
2849 :
2850 : /*
2851 : * Push {semi}joins down, pushes the joins through group by expressions.
2852 : * When the join is on the group by columns, we can push the joins left
2853 : * under the group by. This should only be done, iff the new semijoin would
2854 : * reduce the input table to the groupby. So there should be a reduction
2855 : * (selection) on the table A and this should be propagated to the groupby via
2856 : * for example a primary key.
2857 : *
2858 : * {semi}join( A, groupby( B ) [gbe][aggrs] ) [ gbe == A.x ]
2859 : * ->
2860 : * {semi}join( A, groupby( semijoin(B,A) [gbe == A.x] ) [gbe][aggrs] ) [ gbe == A.x ]
2861 : */
2862 : static inline sql_rel *
2863 1650685 : rel_push_join_down(visitor *v, sql_rel *rel)
2864 : {
2865 1650685 : if (!rel_is_ref(rel) && ((is_left(rel->op) || rel->op == op_join || is_semi(rel->op)) && rel->l && rel->exps)) {
2866 233737 : sql_rel *gb = rel->r, *ogb = gb, *l = NULL, *rell = rel->l;
2867 :
2868 233737 : if (is_simple_project(gb->op) && !rel_is_ref(gb))
2869 46413 : gb = gb->l;
2870 :
2871 233737 : if (rel_is_ref(rell) || !gb || rel_is_ref(gb))
2872 : return rel;
2873 :
2874 224415 : if (is_groupby(gb->op) && gb->r && list_length(gb->r)) {
2875 193 : list *exps = rel->exps, *jes = new_exp_list(v->sql->sa), *gbes = gb->r;
2876 193 : node *n, *m;
2877 : /* find out if all group by expressions are used in the join */
2878 194 : for(n = gbes->h; n; n = n->next) {
2879 193 : sql_exp *gbe = n->data;
2880 193 : int fnd = 0;
2881 193 : const char *rname = NULL, *name = NULL;
2882 :
2883 : /* project in between, ie find alias */
2884 : /* first find expression in expression list */
2885 193 : gbe = exps_uses_exp( gb->exps, gbe);
2886 193 : if (!gbe)
2887 0 : continue;
2888 193 : if (ogb != gb)
2889 172 : gbe = exps_uses_exp( ogb->exps, gbe);
2890 193 : if (gbe) {
2891 152 : rname = exp_find_rel_name(gbe);
2892 152 : name = exp_name(gbe);
2893 : }
2894 :
2895 152 : if (!name)
2896 41 : return rel;
2897 :
2898 378 : for (m = exps->h; m && !fnd; m = m->next) {
2899 226 : sql_exp *je = m->data;
2900 :
2901 226 : if (je->card >= CARD_ATOM && je->type == e_cmp &&
2902 226 : !is_complex_exp(je->flag)) {
2903 : /* expect right expression to match */
2904 221 : sql_exp *r = je->r;
2905 :
2906 221 : if (r == 0 || r->type != e_column)
2907 11 : continue;
2908 210 : if (r->l && rname && strcmp(r->l, rname) == 0 && strcmp(r->r, name)==0) {
2909 : fnd = 1;
2910 85 : } else if (!r->l && !rname && strcmp(r->r, name)==0) {
2911 : fnd = 1;
2912 : }
2913 : if (fnd) {
2914 125 : sql_exp *le = je->l;
2915 125 : sql_exp *re = exp_push_down_prj(v->sql, r, gb, gb->l);
2916 125 : if (!re || (list_length(jes) == 0 && !find_prop(le->p, PROP_HASHCOL))) {
2917 : fnd = 0;
2918 : } else {
2919 1 : int anti = is_anti(je), semantics = is_semantics(je);
2920 :
2921 1 : je = exp_compare(v->sql->sa, le, re, je->flag);
2922 1 : if (anti) set_anti(je);
2923 1 : if (semantics) set_semantics(je);
2924 1 : list_append(jes, je);
2925 : }
2926 : }
2927 : }
2928 : }
2929 152 : if (!fnd)
2930 : return rel;
2931 : }
2932 1 : l = rel_dup(rel->l);
2933 :
2934 : /* push join's left side (as semijoin) down group by */
2935 1 : l = gb->l = rel_crossproduct(v->sql->sa, gb->l, l, op_semi);
2936 1 : l->exps = jes;
2937 1 : set_processed(l);
2938 1 : v->changes++;
2939 1 : return rel;
2940 : }
2941 : }
2942 : return rel;
2943 : }
2944 :
2945 : static bool
2946 724 : check_projection_on_foreignside(sql_rel *r, list *pexps, int fk_left)
2947 : {
2948 : /* projection columns from the foreign side */
2949 724 : if (list_empty(pexps))
2950 : return true;
2951 2304 : for (node *n = pexps->h; n; n = n->next) {
2952 2238 : sql_exp *pe = n->data;
2953 :
2954 2238 : if (pe && is_atom(pe->type))
2955 23 : continue;
2956 2215 : if (pe && !is_alias(pe->type))
2957 : return false;
2958 : /* check for columns from the pk side, then keep the join with the pk */
2959 2203 : if ((fk_left && rel_find_exp(r->r, pe)) || (!fk_left && rel_find_exp(r->l, pe)))
2960 597 : return false;
2961 : }
2962 : return true;
2963 : }
2964 :
2965 : static sql_rel *
2966 273835 : rel_simplify_project_fk_join(mvc *sql, sql_rel *r, list *pexps, list *orderexps, int *changes)
2967 : {
2968 273835 : sql_rel *rl = r->l, *rr = r->r, *nr = NULL;
2969 273835 : sql_exp *je, *le, *nje, *re;
2970 273835 : int fk_left = 1;
2971 :
2972 : /* check for foreign key join */
2973 273835 : if (list_length(r->exps) != 1 || !list_empty(r->attr))
2974 7239 : return r;
2975 266596 : if (!(je = exps_find_prop(r->exps, PROP_JOINIDX)) || je->flag != cmp_equal)
2976 : return r;
2977 : /* je->l == foreign expression, je->r == primary expression */
2978 1276 : if (rel_find_exp(r->l, je->l)) {
2979 : fk_left = 1;
2980 33 : } else if (rel_find_exp(r->r, je->l)) {
2981 : fk_left = 0;
2982 : } else { /* not found */
2983 : return r;
2984 : }
2985 :
2986 : /* primary side must be a full table */
2987 1243 : if ((fk_left && (!is_left(r->op) && !is_full(r->op)) && !is_basetable(rr->op)) ||
2988 33 : (!fk_left && (!is_right(r->op) && !is_full(r->op)) && !is_basetable(rl->op)))
2989 631 : return r;
2990 :
2991 645 : if (!check_projection_on_foreignside(r, pexps, fk_left) || !check_projection_on_foreignside(r, orderexps, fk_left))
2992 607 : return r;
2993 :
2994 : /* rewrite, ie remove pkey side if possible */
2995 38 : le = (sql_exp*)je->l, re = (sql_exp*)je->l;
2996 :
2997 : /* both have NULL and there are semantics, the join cannot be removed */
2998 38 : if (is_semantics(je) && has_nil(le) && has_nil(re))
2999 : return r;
3000 :
3001 38 : (*changes)++;
3002 : /* if the foreign key column doesn't have NULL values, then return it */
3003 38 : if (!has_nil(le) || is_full(r->op) || (fk_left && is_left(r->op)) || (!fk_left && is_right(r->op))) {
3004 : /* if ->attr, introduce group by on index */
3005 32 : if (fk_left) {
3006 24 : nr = rel_dup(r->l);
3007 : } else {
3008 8 : nr = rel_dup(r->r);
3009 : }
3010 32 : if (!list_empty(r->attr)) {
3011 0 : nr = rel_groupby(sql, nr, NULL);
3012 0 : if (nr) {
3013 : // printf("# introduced groupby \n");
3014 0 : nr->r = append(sa_list(sql->sa), le);
3015 0 : nr->exps = r->attr;
3016 : }
3017 : }
3018 32 : return nr;
3019 : }
3020 :
3021 : /* remove NULL values, ie generate a select not null */
3022 6 : nje = exp_compare(sql->sa, exp_ref(sql, le), exp_atom(sql->sa, atom_general(sql->sa, exp_subtype(le), NULL)), cmp_equal);
3023 6 : set_anti(nje);
3024 6 : set_has_no_nil(nje);
3025 6 : set_semantics(nje);
3026 6 : if (fk_left) {
3027 6 : nr = rel_dup(r->l);
3028 : } else {
3029 0 : nr = rel_dup(r->r);
3030 : }
3031 6 : nr = rel_select(sql->sa, nr, nje);
3032 6 : set_processed(nr);
3033 6 : return nr;
3034 : }
3035 :
3036 : static sql_rel *
3037 1597 : rel_simplify_count_fk_join(mvc *sql, sql_rel *r, list *gexps, list *gcols, int *changes)
3038 : {
3039 1597 : sql_rel *rl = r->l, *rr = r->r, *nr = NULL;
3040 1597 : sql_exp *je, *le, *nje, *re, *oce;
3041 1597 : int fk_left = 1;
3042 :
3043 : /* check for foreign key join */
3044 1597 : if (list_length(r->exps) != 1)
3045 : return r;
3046 1596 : if (!(je = exps_find_prop(r->exps, PROP_JOINIDX)) || je->flag != cmp_equal)
3047 : return r;
3048 : /* je->l == foreign expression, je->r == primary expression */
3049 61 : if (rel_find_exp(r->l, je->l)) {
3050 : fk_left = 1;
3051 7 : } else if (rel_find_exp(r->r, je->l)) {
3052 : fk_left = 0;
3053 : } else { /* not found */
3054 : return r;
3055 : }
3056 :
3057 61 : oce = gexps->h->data;
3058 61 : if (oce->l) /* we only handle COUNT(*) */
3059 : return r;
3060 :
3061 : /* primary side must be a full table */
3062 60 : if ((fk_left && (!is_left(r->op) && !is_full(r->op)) && !is_basetable(rr->op)) ||
3063 6 : (!fk_left && (!is_right(r->op) && !is_full(r->op)) && !is_basetable(rl->op)))
3064 : return r;
3065 :
3066 33 : if (fk_left && is_join(rl->op) && !rel_is_ref(rl)) {
3067 12 : r->l = rel_simplify_count_fk_join(sql, rl, gexps, gcols, changes);
3068 12 : if (rl != r->l)
3069 9 : rel_destroy(rl);
3070 : }
3071 39 : if (!fk_left && is_join(rr->op) && !rel_is_ref(rr)) {
3072 2 : r->r = rel_simplify_count_fk_join(sql, rr, gexps, gcols, changes);
3073 2 : if (rr != r->r)
3074 2 : rel_destroy(rr);
3075 : }
3076 :
3077 39 : if (!check_projection_on_foreignside(r, gcols, fk_left))
3078 : return r;
3079 :
3080 : /* rewrite, ie remove pkey side if possible */
3081 37 : le = (sql_exp*)je->l, re = (sql_exp*)je->l;
3082 :
3083 : /* both have NULL and there are semantics, the join cannot be removed */
3084 37 : if (is_semantics(je) && has_nil(le) && has_nil(re))
3085 : return r;
3086 :
3087 37 : (*changes)++;
3088 : /* if the foreign key column doesn't have NULL values, then return it */
3089 37 : if (!has_nil(le) || is_full(r->op) || (fk_left && is_left(r->op)) || (!fk_left && is_right(r->op))) {
3090 31 : if (fk_left) {
3091 25 : nr = rel_dup(r->l);
3092 : } else {
3093 6 : nr = rel_dup(r->r);
3094 : }
3095 31 : return nr;
3096 : }
3097 :
3098 : /* remove NULL values, ie generate a select not null */
3099 6 : nje = exp_compare(sql->sa, exp_ref(sql, le), exp_atom(sql->sa, atom_general(sql->sa, exp_subtype(le), NULL)), cmp_equal);
3100 6 : set_anti(nje);
3101 6 : set_has_no_nil(nje);
3102 6 : set_semantics(nje);
3103 6 : if (fk_left) {
3104 6 : nr = rel_dup(r->l);
3105 : } else {
3106 0 : nr = rel_dup(r->r);
3107 : }
3108 6 : nr = rel_select(sql->sa, nr, nje);
3109 6 : set_processed(nr);
3110 6 : return nr;
3111 : }
3112 :
3113 : /*
3114 : * Handle (left/right/outer/natural) join fk-pk rewrites
3115 : * 1 group by ( fk-pk-join () ) [ count(*) ] -> group by ( fk )
3116 : * 2 project ( fk-pk-join () ) [ fk-column ] -> project (fk table)[ fk-column ]
3117 : * 3 project ( fk1-pk1-join( fk2-pk2-join()) [ fk-column, pk1 column ] -> project (fk1-pk1-join)[ fk-column, pk1 column ]
3118 : */
3119 : static inline sql_rel *
3120 4148265 : rel_simplify_fk_joins(visitor *v, sql_rel *rel)
3121 : {
3122 4148265 : sql_rel *r = NULL;
3123 :
3124 4148265 : if (is_simple_project(rel->op))
3125 1285800 : r = rel->l;
3126 :
3127 4148303 : while (is_simple_project(rel->op) && r && list_length(r->exps) == 1 && (is_join(r->op) || r->op == op_semi) && !(rel_is_ref(r))) {
3128 273835 : sql_rel *or = r;
3129 :
3130 273835 : r = rel_simplify_project_fk_join(v->sql, r, rel->exps, rel->r, &v->changes);
3131 273835 : if (r == or)
3132 : return rel;
3133 38 : rel_destroy(rel->l);
3134 38 : rel->l = r;
3135 : }
3136 :
3137 3874468 : if (!is_groupby(rel->op))
3138 : return rel;
3139 :
3140 127848 : r = rel->l;
3141 193057 : while(r && is_simple_project(r->op))
3142 65209 : r = r->l;
3143 :
3144 142872 : while (is_groupby(rel->op) && !rel_is_ref(rel) && r && (is_join(r->op) || r->op == op_semi) && list_length(r->exps) == 1 && !(rel_is_ref(r)) &&
3145 : /* currently only single count aggregation is handled, no other projects or aggregation */
3146 148302 : list_length(rel->exps) == 1 && exp_aggr_is_count(rel->exps->h->data)) {
3147 1583 : sql_rel *or = r;
3148 :
3149 1583 : r = rel_simplify_count_fk_join(v->sql, r, rel->exps, rel->r, &v->changes);
3150 1583 : if (r == or)
3151 : return rel;
3152 26 : rel_destroy(rel->l);
3153 26 : rel->l = r;
3154 : }
3155 : return rel;
3156 : }
3157 :
3158 : /*
3159 : * Gets the column expressions of a diff function and adds them to "columns".
3160 : * The diff function has two possible argument types: either a sql_exp representing a column
3161 : * or a sql_exp representing another diff function, therefore this function is recursive.
3162 : */
3163 : static void
3164 196 : get_diff_function_columns(sql_exp *diffExp, list *columns) {
3165 196 : list *args = diffExp->l;
3166 :
3167 441 : for (node *arg = args->h; arg; arg = arg->next) {
3168 245 : sql_exp *exp = arg->data;
3169 :
3170 : // diff function
3171 245 : if (exp->type == e_func) {
3172 49 : get_diff_function_columns(exp, columns);
3173 : }
3174 : // column
3175 : else {
3176 196 : list_append(columns, exp);
3177 : }
3178 : }
3179 196 : }
3180 :
3181 : /*
3182 : * Builds a list of aggregation key columns to be used by the select push down algorithm, namely for
3183 : * window functions. Returns NULL if the window function does not partition by any column
3184 : */
3185 : static list *
3186 5472 : get_aggregation_key_columns(sql_allocator *sa, sql_rel *r) {
3187 47341 : for (node* n = r->exps->h; n; n = n->next) {
3188 42122 : sql_exp *e = n->data;
3189 :
3190 42122 : if (e->type == e_func) {
3191 9715 : sql_subfunc *f = e->f;
3192 :
3193 : // aggregation function
3194 9715 : if (!strcmp(f->func->base.name, "rank")) {
3195 253 : list* rankArguments = e->l;
3196 : // the partition key is the second argument
3197 253 : sql_exp *partitionExp = rankArguments->h->next->data;
3198 :
3199 : // check if the key contains any columns, i.e., is a diff function
3200 253 : if (partitionExp->type == e_func) {
3201 : // get columns to list
3202 147 : list *aggColumns = sa_list(sa);
3203 147 : get_diff_function_columns(partitionExp, aggColumns);
3204 147 : return aggColumns;
3205 : }
3206 : // the function has no aggregation columns (e_atom of boolean)
3207 : else {
3208 : return NULL;
3209 : }
3210 :
3211 : }
3212 : }
3213 : }
3214 : return NULL;
3215 : }
3216 :
3217 : /*
3218 : * Checks if a filter column is also used as an aggregation key, so it can be later safely pushed down.
3219 : */
3220 : static int
3221 200 : filter_column_in_aggregation_columns(sql_exp *column, list *aggColumns) {
3222 : /* check if it is a column or an e_convert, and get the actual column if it is the latter */
3223 200 : if (column->type == e_convert) {
3224 2 : column = column->l;
3225 : }
3226 :
3227 200 : char *tableName = column->l;
3228 200 : char *columnName = column->r;
3229 :
3230 445 : for (node *n = aggColumns->h; n; n = n->next) {
3231 254 : sql_exp *aggCol = n->data;
3232 254 : char *aggColTableName = aggCol->l;
3233 254 : char *aggColColumnName = aggCol->r;
3234 :
3235 254 : if (!strcmp(tableName, aggColTableName) && !strcmp(columnName, aggColColumnName)) {
3236 : /* match */
3237 : return 1;
3238 : }
3239 : }
3240 :
3241 : /* no matches found */
3242 : return 0;
3243 : }
3244 :
3245 :
3246 : /*
3247 : * Push select down, pushes the selects through (simple) projections. Also
3248 : * it cleans up the projections which become useless.
3249 : *
3250 : * WARNING - Make sure to call try_remove_empty_select macro before returning so we ensure
3251 : * possible generated empty selects won't never be generated
3252 : */
3253 : static sql_rel *
3254 8275770 : rel_push_select_down(visitor *v, sql_rel *rel)
3255 : {
3256 8275770 : list *exps = NULL;
3257 8275770 : sql_rel *r = NULL;
3258 8275770 : node *n;
3259 :
3260 8275770 : if (rel_is_ref(rel)) {
3261 453000 : if (is_select(rel->op) && rel->exps) {
3262 : /* add inplace empty select */
3263 1742 : sql_rel *l = rel_select(v->sql->sa, rel->l, NULL);
3264 :
3265 1742 : l->exps = rel->exps;
3266 1742 : set_processed(l);
3267 1742 : rel->exps = NULL;
3268 1742 : rel->l = l;
3269 1742 : v->changes++;
3270 : }
3271 453000 : return rel;
3272 : }
3273 :
3274 : /* don't make changes for empty selects */
3275 7822770 : if (is_select(rel->op) && list_empty(rel->exps))
3276 13 : return try_remove_empty_select(v, rel);
3277 :
3278 : /* merge 2 selects */
3279 7822757 : r = rel->l;
3280 7822757 : if (is_select(rel->op) && r && r->exps && is_select(r->op) && !(rel_is_ref(r)) && !exps_have_func(rel->exps)) {
3281 34 : (void)list_merge(r->exps, rel->exps, (fdup)NULL);
3282 34 : rel->l = NULL;
3283 34 : rel_destroy(rel);
3284 34 : v->changes++;
3285 34 : return try_remove_empty_select(v, r);
3286 : }
3287 : /*
3288 : * Push select through semi/anti join
3289 : * select (semi(A,B)) == semi(select(A), B)
3290 : */
3291 7822723 : if (is_select(rel->op) && r && is_semi(r->op) && !(rel_is_ref(r))) {
3292 280 : rel->l = r->l;
3293 280 : r->l = rel;
3294 280 : v->changes++;
3295 : /*
3296 : * if A has 2 references (ie used on both sides of
3297 : * the semi join), we also push the select into A.
3298 : */
3299 280 : if (rel_is_ref(rel->l) && rel->l == rel_find_ref(r->r)){
3300 0 : sql_rel *lx = rel->l;
3301 0 : sql_rel *rx = r->r;
3302 0 : if (lx->ref.refcnt == 2 && !rel_is_ref(rx)) {
3303 0 : while (rx->l && !rel_is_ref(rx->l) &&
3304 0 : (is_project(rx->op) ||
3305 : is_select(rx->op) ||
3306 : is_join(rx->op)))
3307 : rx = rx->l;
3308 : /* probably we need to introduce a project */
3309 0 : rel_destroy(rel->l);
3310 0 : lx = rel_project(v->sql->sa, rel, rel_projections(v->sql, rel, NULL, 1, 1));
3311 0 : r->l = lx;
3312 0 : rx->l = rel_dup(lx);
3313 : }
3314 : }
3315 280 : return r;
3316 : }
3317 7822443 : exps = rel->exps;
3318 :
3319 : /* push select through join */
3320 7822443 : if (is_select(rel->op) && r && is_join(r->op) && !rel_is_ref(r) && !is_single(r)){
3321 20900 : sql_rel *jl = r->l, *ojl = jl, *jr = r->r, *ojr = jr;
3322 20900 : int left = r->op == op_join || r->op == op_left;
3323 20900 : int right = r->op == op_join || r->op == op_right;
3324 :
3325 20900 : if (r->op == op_full)
3326 : return rel;
3327 :
3328 : /* introduce selects under the join (if needed) */
3329 20872 : set_processed(jl);
3330 20872 : set_processed(jr);
3331 46650 : for (n = exps->h; n;) {
3332 25778 : node *next = n->next;
3333 25778 : sql_exp *e = n->data;
3334 :
3335 25778 : if (left && rel_rebind_exp(v->sql, jl, e)) {
3336 13516 : if (!is_select(jl->op) || rel_is_ref(jl))
3337 10590 : r->l = jl = rel_select(v->sql->sa, jl, NULL);
3338 13516 : rel_select_add_exp(v->sql->sa, jl, e);
3339 13516 : list_remove_node(exps, NULL, n);
3340 13516 : v->changes++;
3341 12262 : } else if (right && rel_rebind_exp(v->sql, jr, e)) {
3342 4728 : if (!is_select(jr->op) || rel_is_ref(jr))
3343 3975 : r->r = jr = rel_select(v->sql->sa, jr, NULL);
3344 4728 : rel_select_add_exp(v->sql->sa, jr, e);
3345 4728 : list_remove_node(exps, NULL, n);
3346 4728 : v->changes++;
3347 : }
3348 : n = next;
3349 : }
3350 20872 : if (ojl != jl)
3351 10590 : set_processed(jl);
3352 20872 : if (ojr != jr)
3353 3975 : set_processed(jr);
3354 : }
3355 :
3356 : /* merge select and cross product ? */
3357 7822415 : if (is_select(rel->op) && r && r->op == op_join && !rel_is_ref(r) && !is_single(r)){
3358 14072 : for (n = exps->h; n;) {
3359 1983 : node *next = n->next;
3360 1983 : sql_exp *e = n->data;
3361 :
3362 1983 : if (exp_is_join(e, NULL) == 0) {
3363 676 : if (!r->exps)
3364 100 : r->exps = sa_list(v->sql->sa);
3365 676 : append(r->exps, e);
3366 676 : list_remove_node(exps, NULL, n);
3367 676 : v->changes++;
3368 : }
3369 : n = next;
3370 : }
3371 : }
3372 :
3373 7822415 : if (is_select(rel->op) && r && (is_simple_project(r->op) || (is_groupby(r->op) && !list_empty(r->r))) && !rel_is_ref(r) && !is_single(r)){
3374 54857 : sql_rel *pl = r->l, *opl = pl;
3375 : /* we cannot push through window functions (for safety I disabled projects over DDL too) */
3376 54857 : if (pl && pl->op != op_ddl && !exps_have_unsafe(r->exps, 0)) {
3377 : /* introduce selects under the project (if needed) */
3378 45537 : set_processed(pl);
3379 45537 : if (!pl->exps)
3380 418 : pl->exps = sa_list(v->sql->sa);
3381 98808 : for (n = exps->h; n;) {
3382 53271 : node *next = n->next;
3383 53271 : sql_exp *e = n->data, *ne = NULL;
3384 :
3385 : /* can we move it down */
3386 53271 : if (e->type == e_cmp && (ne = exp_push_down_prj(v->sql, e, r, pl)) && ne != e) {
3387 26652 : if (!(is_select(pl->op) && is_join(pl->op) && is_semi(pl->op)) || rel_is_ref(pl))
3388 26652 : r->l = pl = rel_select(v->sql->sa, pl, NULL);
3389 26652 : rel_select_add_exp(v->sql->sa, pl, ne);
3390 26652 : list_remove_node(exps, NULL, n);
3391 26652 : v->changes++;
3392 : }
3393 : n = next;
3394 : }
3395 45537 : if (opl != pl)
3396 18600 : set_processed(pl);
3397 : }
3398 :
3399 : /* push filters if they match the aggregation key on a window function */
3400 5472 : else if (pl && pl->op != op_ddl && exps_have_unsafe(r->exps, 0)) {
3401 5472 : set_processed(pl);
3402 : /* list of aggregation key columns */
3403 5472 : list *aggColumns = get_aggregation_key_columns(v->sql->sa, r);
3404 :
3405 : /* aggregation keys found, check if any filter matches them */
3406 5472 : if (aggColumns) {
3407 351 : for (n = exps->h; n;) {
3408 204 : node *next = n->next;
3409 204 : sql_exp *e = n->data, *ne = NULL;
3410 :
3411 204 : if (e->type == e_cmp) {
3412 : /* simple comparison filter */
3413 204 : if (e->flag == cmp_gt || e->flag == cmp_gte || e->flag == cmp_lte || e->flag == cmp_lt
3414 204 : || e->flag == cmp_equal || e->flag == cmp_notequal || e->flag == cmp_in || e->flag == cmp_notin
3415 5 : || (e->flag == cmp_filter && ((list*)e->l)->cnt == 1)) {
3416 200 : sql_exp* column;
3417 : /* the column in 'like' filters is stored inside a list */
3418 200 : if (e->flag == cmp_filter) {
3419 1 : column = ((list*)e->l)->h->data;
3420 : }
3421 : else {
3422 199 : column = e->l;
3423 : }
3424 :
3425 : /* check if the expression matches any aggregation key, meaning we can
3426 : try to safely push it down */
3427 200 : if (filter_column_in_aggregation_columns(column, aggColumns)) {
3428 9 : ne = exp_push_down_prj(v->sql, e, r, pl);
3429 :
3430 : /* can we move it down */
3431 9 : if (ne && ne != e && pl->exps) {
3432 9 : if (!is_select(pl->op) || rel_is_ref(pl))
3433 8 : r->l = pl = rel_select(v->sql->sa, pl, NULL);
3434 9 : rel_select_add_exp(v->sql->sa, pl, ne);
3435 9 : list_remove_node(exps, NULL, n);
3436 9 : v->changes++;
3437 : }
3438 : }
3439 : }
3440 : }
3441 : n = next;
3442 : }
3443 :
3444 : /* cleanup list */
3445 147 : list_destroy(aggColumns);
3446 : }
3447 : }
3448 : }
3449 :
3450 : /* try push select under set relation */
3451 7822415 : if (is_select(rel->op) && r && is_set(r->op) && !list_empty(r->exps) && !rel_is_ref(r) && !is_single(r) && !list_empty(exps)) {
3452 4668 : sql_rel *u = r, *ul = u->l, *ur = u->r;
3453 :
3454 4668 : ul = rel_dup(ul);
3455 4668 : ur = rel_dup(ur);
3456 4668 : if (!is_project(ul->op))
3457 0 : ul = rel_project(v->sql->sa, ul,
3458 : rel_projections(v->sql, ul, NULL, 1, 1));
3459 4668 : if (!is_project(ur->op))
3460 0 : ur = rel_project(v->sql->sa, ur,
3461 : rel_projections(v->sql, ur, NULL, 1, 1));
3462 4668 : rel_rename_exps(v->sql, u->exps, ul->exps);
3463 4668 : rel_rename_exps(v->sql, u->exps, ur->exps);
3464 :
3465 : /* introduce selects under the set */
3466 4668 : ul = rel_select(v->sql->sa, ul, NULL);
3467 4668 : ul->exps = exps_copy(v->sql, exps);
3468 4668 : set_processed(ul);
3469 4668 : ur = rel_select(v->sql->sa, ur, NULL);
3470 4668 : ur->exps = exps_copy(v->sql, exps);
3471 4668 : set_processed(ur);
3472 :
3473 4668 : rel = rel_inplace_setop(v->sql, rel, ul, ur, u->op, rel_projections(v->sql, rel, NULL, 1, 1));
3474 4668 : if (need_distinct(u))
3475 9 : set_distinct(rel);
3476 4668 : v->changes++;
3477 : }
3478 :
3479 7822415 : return try_remove_empty_select(v, rel);
3480 : }
3481 :
3482 : static int
3483 12222 : index_exp(sql_exp *e, sql_idx *i)
3484 : {
3485 12222 : if (e->type == e_cmp && !is_complex_exp(e->flag)) {
3486 6976 : switch(i->type) {
3487 6976 : case hash_idx:
3488 : case oph_idx:
3489 6976 : if (e->flag == cmp_equal)
3490 : return 0;
3491 : /* fall through */
3492 : case join_idx:
3493 : default:
3494 1296 : return -1;
3495 : }
3496 : }
3497 : return -1;
3498 : }
3499 :
3500 : /* find column for the select/join expression */
3501 : static sql_column *
3502 5680 : sjexp_col(sql_exp *e, sql_rel *r)
3503 : {
3504 5680 : sql_column *res = NULL;
3505 :
3506 5680 : if (e->type == e_cmp && !is_complex_exp(e->flag)) {
3507 5680 : res = exp_find_column(r, e->l, -2);
3508 5680 : if (!res)
3509 585 : res = exp_find_column(r, e->r, -2);
3510 : }
3511 5680 : return res;
3512 : }
3513 :
3514 : static sql_idx *
3515 2091260 : find_index(sql_allocator *sa, sql_rel *rel, sql_rel *sub, list **EXPS)
3516 : {
3517 2091260 : node *n;
3518 :
3519 : /* any (partial) match of the expressions with the index columns */
3520 : /* Depending on the index type we may need full matches and only
3521 : limited number of cmp types (hash only equality etc) */
3522 : /* Depending on the index type we should (in the rel_bin) generate
3523 : more code, ie for spatial index add post filter etc, for hash
3524 : compute hash value and use index */
3525 :
3526 2091260 : if (sub->exps && rel->exps)
3527 8830389 : for(n = sub->exps->h; n; n = n->next) {
3528 6850843 : prop *p;
3529 6850843 : sql_exp *e = n->data;
3530 :
3531 6850843 : if ((p = find_prop(e->p, PROP_HASHIDX)) != NULL) {
3532 9538 : list *exps, *cols;
3533 9538 : sql_idx *i = p->value.pval;
3534 9538 : fcmp cmp = (fcmp)&sql_column_kc_cmp;
3535 :
3536 : /* join indices are only interesting for joins */
3537 9538 : if (i->type == join_idx || list_length(i->columns) <= 1)
3538 0 : continue;
3539 : /* based on the index type, find qualifying exps */
3540 9538 : exps = list_select(rel->exps, i, (fcmp) &index_exp, (fdup)NULL);
3541 9538 : if (list_empty(exps))
3542 4864 : continue;
3543 : /* now we obtain the columns, move into sql_column_kc_cmp! */
3544 4674 : cols = list_map(exps, sub, (fmap) &sjexp_col);
3545 :
3546 : /* TODO check that at most 2 relations are involved */
3547 :
3548 : /* Match the index columns with the expression columns.
3549 : TODO, Allow partial matches ! */
3550 4674 : if (list_match(cols, i->columns, cmp) == 0) {
3551 : /* re-order exps in index order */
3552 302 : node *n, *m;
3553 302 : list *es = sa_list(sa);
3554 :
3555 995 : for(n = i->columns->h; n; n = n->next) {
3556 693 : int i = 0;
3557 2167 : for(m = cols->h; m; m = m->next, i++) {
3558 2167 : if (cmp(m->data, n->data) == 0){
3559 693 : sql_exp *e = list_fetch(exps, i);
3560 693 : list_append(es, e);
3561 693 : break;
3562 : }
3563 : }
3564 : }
3565 : /* fix the destroy function */
3566 302 : cols->destroy = NULL;
3567 302 : *EXPS = es;
3568 302 : e->used = 1;
3569 302 : return i;
3570 : }
3571 4372 : cols->destroy = NULL;
3572 : }
3573 : }
3574 : return NULL;
3575 : }
3576 :
3577 : static inline sql_rel *
3578 1330306 : rel_use_index(visitor *v, sql_rel *rel)
3579 : {
3580 1330306 : list *exps = NULL;
3581 1330306 : sql_idx *i = find_index(v->sql->sa, rel, rel->l, &exps);
3582 1330306 : int left = 1;
3583 :
3584 1330306 : assert(is_select(rel->op) || is_join(rel->op));
3585 1330306 : if (!i && is_join(rel->op)) {
3586 760954 : left = 0;
3587 760954 : i = find_index(v->sql->sa, rel, rel->r, &exps);
3588 : }
3589 :
3590 1330095 : if (i) {
3591 302 : prop *p;
3592 302 : node *n;
3593 302 : int single_table = 1;
3594 302 : sql_exp *re = NULL;
3595 :
3596 980 : for( n = exps->h; n && single_table; n = n->next) {
3597 684 : sql_exp *e = n->data, *nre = e->l;
3598 :
3599 684 : if (!is_compare(e->type) || is_anti(e) || e->flag != cmp_equal)
3600 : return rel;
3601 678 : if (is_join(rel->op) && ((left && !rel_find_exp(rel->l, nre)) || (!left && rel_find_exp(rel->r, nre))))
3602 63 : nre = e->r;
3603 678 : single_table = (!re || (exp_relname(nre) && exp_relname(re) && strcmp(exp_relname(nre), exp_relname(re)) == 0));
3604 678 : re = nre;
3605 : }
3606 296 : if (single_table) { /* add PROP_HASHCOL to all column exps */
3607 951 : for( n = exps->h; n; n = n->next) {
3608 662 : sql_exp *e = n->data;
3609 :
3610 : /* swapped ? */
3611 662 : if (is_join(rel->op) && ((left && !rel_find_exp(rel->l, e->l)) || (!left && !rel_find_exp(rel->r, e->l)))) {
3612 161 : exp_swap(e);
3613 : }
3614 662 : p = find_prop(e->p, PROP_HASHCOL);
3615 662 : if (!p)
3616 269 : e->p = p = prop_create(v->sql->sa, PROP_HASHCOL, e->p);
3617 662 : p->value.pval = i;
3618 : }
3619 : }
3620 : /* add the remaining exps to the new exp list */
3621 296 : if (list_length(rel->exps) > list_length(exps)) {
3622 76 : for( n = rel->exps->h; n; n = n->next) {
3623 57 : sql_exp *e = n->data;
3624 57 : if (!list_find(exps, e, (fcmp)&exp_cmp))
3625 19 : list_append(exps, e);
3626 : }
3627 : }
3628 296 : rel->exps = exps;
3629 : }
3630 : return rel;
3631 : }
3632 :
3633 : static sql_rel *
3634 4148265 : rel_select_leftgroup_2_semi(visitor *v, sql_rel *rel)
3635 : {
3636 4148265 : if (rel_is_ref(rel) || !is_select(rel->op) || list_empty(rel->exps))
3637 3588204 : return rel;
3638 560061 : sql_rel *l = rel->l;
3639 :
3640 560061 : if (!l || rel_is_ref(l) || !is_left(l->op) || list_empty(l->attr))
3641 558985 : return rel;
3642 :
3643 2144 : for(node *n = rel->exps->h; n; n = n->next) {
3644 1076 : sql_exp *e = n->data;
3645 :
3646 1076 : if (e->type == e_cmp && !is_semantics(e) && !e->f) {
3647 1063 : list *attrs = l->attr;
3648 1063 : sql_exp *a = attrs->h->data;
3649 :
3650 1063 : if (exps_find_exp(l->attr, e->l) && exp_is_true(e->r) && e->flag == cmp_equal /*&& exp_is_true(a)*/) {
3651 : // printf("# optimize select leftgroup -> semi\n");
3652 8 : if (!list_empty(l->exps)) {
3653 12 : for(node *m = l->exps->h; m; m = m->next) {
3654 6 : sql_exp *j = m->data;
3655 6 : reset_any(j);
3656 : }
3657 : }
3658 8 : l->attr = NULL;
3659 8 : l->op = exp_is_true(a)?op_semi:op_anti;
3660 8 : list_remove_node(rel->exps, NULL, n);
3661 8 : rel = rel_project(v->sql->sa, rel, rel_projections(v->sql, rel, NULL, 1, 1));
3662 8 : list_append(rel->exps, attrs->h->data);
3663 8 : v->changes++;
3664 8 : return rel;
3665 : }
3666 : }
3667 : }
3668 : return rel;
3669 : }
3670 :
3671 : static sql_rel *
3672 4148265 : rel_optimize_select_and_joins_topdown_(visitor *v, sql_rel *rel)
3673 : {
3674 : /* push_join_down introduces semijoins */
3675 4148265 : uint8_t cycle = *(uint8_t*) v->data;
3676 4148265 : if (cycle <= 0) {
3677 1650685 : rel = rel_semijoin_use_fk(v, rel);
3678 1650685 : rel = rel_push_join_down(v, rel);
3679 : }
3680 :
3681 4148265 : rel = rel_simplify_fk_joins(v, rel);
3682 4148265 : rel = rel_push_select_down(v, rel);
3683 4148265 : rel = rel_select_leftgroup_2_semi(v, rel);
3684 4148265 : if (rel && rel->l && (is_select(rel->op) || is_join(rel->op)))
3685 1330306 : rel = rel_use_index(v, rel);
3686 4148265 : return rel;
3687 : }
3688 :
3689 : static sql_rel *
3690 105949 : rel_optimize_select_and_joins_topdown(visitor *v, global_props *gp, sql_rel *rel)
3691 : {
3692 105949 : v->data = &gp->opt_cycle;
3693 105949 : rel = rel_visitor_topdown(v, rel, &rel_optimize_select_and_joins_topdown_);
3694 105949 : v->data = gp;
3695 105949 : return rel;
3696 : }
3697 :
3698 : run_optimizer
3699 596802 : bind_optimize_select_and_joins_topdown(visitor *v, global_props *gp)
3700 : {
3701 596802 : int flag = v->sql->sql_optimizer;
3702 596614 : return gp->opt_level == 1 && (gp->cnt[op_join] || gp->cnt[op_left] || gp->cnt[op_right]
3703 526941 : || gp->cnt[op_full] || gp->cnt[op_semi] || gp->cnt[op_anti] ||
3704 1193416 : gp->cnt[op_select]) && (flag & optimize_select_and_joins_topdown) ? rel_optimize_select_and_joins_topdown : NULL;
3705 : }
3706 :
3707 :
3708 : static int
3709 1522991 : can_push_func(sql_exp *e, sql_rel *rel, int *must, int depth)
3710 : {
3711 1576799 : switch(e->type) {
3712 1343985 : case e_cmp: {
3713 1343985 : sql_exp *l = e->l, *r = e->r, *f = e->f;
3714 :
3715 : /* don't push down functions inside attribute joins */
3716 1343985 : if (e->flag == cmp_or || e->flag == cmp_in || e->flag == cmp_notin || e->flag == cmp_filter || (is_join(rel->op) && is_any(e)))
3717 : return 0;
3718 1270737 : if (depth > 0) { /* for comparisons under the top ones, they become functions */
3719 76 : int lmust = 0;
3720 76 : int res = can_push_func(l, rel, &lmust, depth + 1) && can_push_func(r, rel, &lmust, depth + 1) &&
3721 29 : (!f || can_push_func(f, rel, &lmust, depth + 1));
3722 13 : if (res && !lmust)
3723 : return 1;
3724 76 : (*must) |= lmust;
3725 76 : return res;
3726 : } else {
3727 1270661 : int mustl = 0, mustr = 0, mustf = 0;
3728 1270661 : return ((l->type == e_column || can_push_func(l, rel, &mustl, depth + 1)) && (*must = mustl)) ||
3729 2528071 : ((r->type == e_column || can_push_func(r, rel, &mustr, depth + 1)) && (*must = mustr)) ||
3730 2647 : ((f && (f->type == e_column || can_push_func(f, rel, &mustf, depth + 1)) && (*must = mustf)));
3731 : }
3732 : }
3733 53808 : case e_convert:
3734 53808 : return can_push_func(e->l, rel, must, depth + 1);
3735 10245 : case e_aggr:
3736 : case e_func: {
3737 10245 : list *l = e->l;
3738 10245 : int res = 1, lmust = 0;
3739 :
3740 10245 : if (exp_unsafe(e, 0))
3741 : return 0;
3742 28493 : if (l) for (node *n = l->h; n && res; n = n->next)
3743 18252 : res &= can_push_func(n->data, rel, &lmust, depth + 1);
3744 10241 : if (res && !lmust)
3745 : return 1;
3746 9389 : (*must) |= lmust;
3747 9389 : return res;
3748 : }
3749 55852 : case e_column:
3750 55852 : if (rel && !rel_find_exp(rel, e))
3751 : return 0;
3752 35495 : (*must) = 1;
3753 : /* fall through */
3754 : default:
3755 : return 1;
3756 : }
3757 : }
3758 :
3759 : static int
3760 1112046 : exps_can_push_func(list *exps, sql_rel *rel)
3761 : {
3762 4638516 : for(node *n = exps->h; n; n = n->next) {
3763 3555650 : sql_exp *e = n->data;
3764 3555650 : int mustl = 0, mustr = 0;
3765 :
3766 3555650 : if ((is_joinop(rel->op) || is_select(rel->op)) && ((can_push_func(e, rel->l, &mustl, 0) && mustl)))
3767 29180 : return 1;
3768 3545662 : if (is_joinop(rel->op) && can_push_func(e, rel->r, &mustr, 0) && mustr)
3769 : return 1;
3770 : }
3771 : return 0;
3772 : }
3773 :
3774 : static int
3775 118789 : exp_needs_push_down(sql_rel *rel, sql_exp *e)
3776 : {
3777 149640 : switch(e->type) {
3778 41130 : case e_cmp:
3779 : /* don't push down functions inside attribute joins */
3780 41130 : if (e->flag == cmp_or || e->flag == cmp_in || e->flag == cmp_notin || e->flag == cmp_filter || (is_join(rel->op) && is_any(e)))
3781 : return 0;
3782 38225 : return exp_needs_push_down(rel, e->l) || exp_needs_push_down(rel, e->r) || (e->f && exp_needs_push_down(rel, e->f));
3783 30851 : case e_convert:
3784 30851 : return exp_needs_push_down(rel, e->l);
3785 2385 : case e_aggr:
3786 : case e_func:
3787 2385 : if (!e->l || exps_are_atoms(e->l))
3788 40 : return 0;
3789 : return 1;
3790 12488 : case e_atom:
3791 12488 : if (!e->f || exps_are_atoms(e->f))
3792 12488 : return 0;
3793 : return 1;
3794 : case e_column:
3795 : default:
3796 : return 0;
3797 : }
3798 : }
3799 :
3800 : static int
3801 29180 : exps_need_push_down(sql_rel *rel, list *exps )
3802 : {
3803 67954 : for(node *n = exps->h; n; n = n->next)
3804 41119 : if (exp_needs_push_down(rel, n->data))
3805 : return 1;
3806 : return 0;
3807 : }
3808 :
3809 : static sql_exp *exp_push_single_func_down(visitor *v, sql_rel *rel, sql_rel *ol, sql_rel *or, sql_exp *e, int depth);
3810 :
3811 : static list *
3812 2842 : exps_push_single_func_down(visitor *v, sql_rel *rel, sql_rel *ol, sql_rel *or, list *exps, int depth)
3813 : {
3814 5684 : if (mvc_highwater(v->sql))
3815 : return exps;
3816 :
3817 8080 : for (node *n = exps->h; n; n = n->next)
3818 5238 : if ((n->data = exp_push_single_func_down(v, rel, ol, or, n->data, depth)) == NULL)
3819 : return NULL;
3820 : return exps;
3821 : }
3822 :
3823 : static sql_exp *
3824 18950 : exp_push_single_func_down(visitor *v, sql_rel *rel, sql_rel *ol, sql_rel *or, sql_exp *e, int depth)
3825 : {
3826 33496 : if (mvc_highwater(v->sql))
3827 : return e;
3828 :
3829 18950 : switch(e->type) {
3830 5217 : case e_cmp: {
3831 5217 : if (e->flag == cmp_or || e->flag == cmp_filter) {
3832 245 : if ((e->l = exps_push_single_func_down(v, rel, ol, or, e->l, depth + 1)) == NULL)
3833 : return NULL;
3834 245 : if ((e->r = exps_push_single_func_down(v, rel, ol, or, e->r, depth + 1)) == NULL)
3835 : return NULL;
3836 4972 : } else if (e->flag == cmp_in || e->flag == cmp_notin) {
3837 7 : if ((e->l = exp_push_single_func_down(v, rel, ol, or, e->l, depth + 1)) == NULL)
3838 : return NULL;
3839 7 : if ((e->r = exps_push_single_func_down(v, rel, ol, or, e->r, depth + 1)) == NULL)
3840 : return NULL;
3841 : } else {
3842 4965 : if ((e->l = exp_push_single_func_down(v, rel, ol, or, e->l, depth + 1)) == NULL)
3843 : return NULL;
3844 4965 : if ((e->r = exp_push_single_func_down(v, rel, ol, or, e->r, depth + 1)) == NULL)
3845 : return NULL;
3846 4965 : if (e->f && (e->f = exp_push_single_func_down(v, rel, ol, or, e->f, depth + 1)) == NULL)
3847 : return NULL;
3848 : }
3849 : } break;
3850 2332 : case e_convert:
3851 2332 : if ((e->l = exp_push_single_func_down(v, rel, ol, or, e->l, depth + 1)) == NULL)
3852 : return NULL;
3853 : break;
3854 4433 : case e_aggr:
3855 : case e_func: {
3856 4433 : sql_rel *l = rel->l, *r = rel->r;
3857 4433 : int must = 0, mustl = 0, mustr = 0;
3858 :
3859 4433 : if (exp_unsafe(e, 0))
3860 29 : return e;
3861 4427 : if (!e->l || exps_are_atoms(e->l))
3862 23 : return e;
3863 4404 : if ((is_joinop(rel->op) && ((can_push_func(e, l, &mustl, depth + 1) && mustl) || (can_push_func(e, r, &mustr, depth + 1) && mustr))) ||
3864 3417 : (is_select(rel->op) && can_push_func(e, l, &must, depth + 1) && must)) {
3865 4389 : exp_label(v->sql->sa, e, ++v->sql->label);
3866 : /* we need a full projection, group by's and unions cannot be extended with more expressions */
3867 4389 : if (mustr) {
3868 353 : if (r == or) /* don't project twice */
3869 331 : rel->r = r = rel_project(v->sql->sa, r, rel_projections(v->sql, r, NULL, 1, 1));
3870 353 : list_append(r->exps, e);
3871 : } else {
3872 4036 : if (l == ol) /* don't project twice */
3873 2278 : rel->l = l = rel_project(v->sql->sa, l, rel_projections(v->sql, l, NULL, 1, 1));
3874 4036 : list_append(l->exps, e);
3875 : }
3876 4389 : e = exp_ref(v->sql, e);
3877 4389 : v->changes++;
3878 : }
3879 4404 : } break;
3880 2094 : case e_atom: {
3881 2094 : if (e->f && (e->f = exps_push_single_func_down(v, rel, ol, or, e->f, depth + 1)) == NULL)
3882 : return NULL;
3883 : } break;
3884 : case e_column:
3885 : case e_psm:
3886 : break;
3887 : }
3888 : return e;
3889 : }
3890 :
3891 : static inline sql_rel *
3892 4127505 : rel_push_func_down(visitor *v, sql_rel *rel)
3893 : {
3894 4127505 : if ((is_select(rel->op) || is_joinop(rel->op)) && rel->l && rel->exps && !(rel_is_ref(rel))) {
3895 1213414 : int changes = v->changes;
3896 1213414 : sql_rel *l = rel->l, *r = rel->r;
3897 :
3898 : /* only push down when is useful */
3899 1213414 : if ((is_select(rel->op) && list_length(rel->exps) <= 1) || rel_is_ref(l) || (is_joinop(rel->op) && rel_is_ref(r)))
3900 529299 : return rel;
3901 684115 : if (exps_can_push_func(rel->exps, rel) && exps_need_push_down(rel, rel->exps) && !exps_push_single_func_down(v, rel, l, r, rel->exps, 0))
3902 : return NULL;
3903 684115 : if (v->changes > changes) /* once we get a better join order, we can try to remove this projection */
3904 2339 : return rel_project(v->sql->sa, rel, rel_projections(v->sql, rel, NULL, 1, 1));
3905 : }
3906 3595867 : if (is_simple_project(rel->op) && rel->l && rel->exps) {
3907 1206781 : sql_rel *pl = rel->l;
3908 :
3909 1206781 : if (is_joinop(pl->op) && exps_can_push_func(rel->exps, rel)) {
3910 0 : sql_rel *l = pl->l, *r = pl->r, *ol = l, *or = r;
3911 :
3912 0 : for (node *n = rel->exps->h; n; ) {
3913 0 : node *next = n->next;
3914 0 : sql_exp *e = n->data;
3915 0 : int mustl = 0, mustr = 0;
3916 :
3917 0 : if ((can_push_func(e, l, &mustl, 0) && mustl) || (can_push_func(e, r, &mustr, 0) && mustr)) {
3918 0 : if (mustl) {
3919 0 : if (l == ol) /* don't project twice */
3920 0 : pl->l = l = rel_project(v->sql->sa, l, rel_projections(v->sql, l, NULL, 1, 1));
3921 0 : list_append(l->exps, e);
3922 0 : list_remove_node(rel->exps, NULL, n);
3923 0 : v->changes++;
3924 : } else {
3925 0 : if (r == or) /* don't project twice */
3926 0 : pl->r = r = rel_project(v->sql->sa, r, rel_projections(v->sql, r, NULL, 1, 1));
3927 0 : list_append(r->exps, e);
3928 0 : list_remove_node(rel->exps, NULL, n);
3929 0 : v->changes++;
3930 : }
3931 : }
3932 0 : n = next;
3933 : }
3934 : }
3935 : }
3936 : return rel;
3937 : }
3938 :
3939 : static sql_rel *
3940 4127505 : rel_push_func_and_select_down_(visitor *v, sql_rel *rel)
3941 : {
3942 4127505 : if (rel)
3943 4127505 : rel = rel_push_func_down(v, rel);
3944 4127505 : if (rel)
3945 4127505 : rel = rel_push_select_down(v, rel);
3946 4127505 : return rel;
3947 : }
3948 :
3949 : static sql_rel *
3950 105948 : rel_push_func_and_select_down(visitor *v, global_props *gp, sql_rel *rel)
3951 : {
3952 105948 : (void) gp;
3953 105948 : return rel_visitor_topdown(v, rel, &rel_push_func_and_select_down_);
3954 : }
3955 :
3956 : run_optimizer
3957 596797 : bind_push_func_and_select_down(visitor *v, global_props *gp)
3958 : {
3959 596797 : int flag = v->sql->sql_optimizer;
3960 596609 : return gp->opt_level == 1 && (gp->cnt[op_join] || gp->cnt[op_left] || gp->cnt[op_right]
3961 526936 : || gp->cnt[op_full] || gp->cnt[op_semi] || gp->cnt[op_anti] || gp->cnt[op_select])
3962 702746 : && (flag & push_func_and_select_down) ? rel_push_func_and_select_down : NULL;
3963 : }
|