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 : /* (c) M. Kersten
14 : */
15 : #include "monetdb_config.h"
16 : #include "opt_prelude.h"
17 : #include "opt_support.h"
18 : #include "mal_interpreter.h"
19 : #include "mal_listing.h"
20 : #include "opt_multiplex.h"
21 : #include "optimizer_private.h"
22 : #include "manifold.h"
23 :
24 : /* Some optimizers can/need only be applied once.
25 : * The optimizer trace at the end of the MAL block
26 : * can be used to check for this.
27 : */
28 : int
29 485816 : optimizerIsApplied(MalBlkPtr mb, const char *opt)
30 : {
31 485816 : InstrPtr p;
32 485816 : int i;
33 105264200 : for (i = mb->stop; i < mb->ssize; i++) {
34 104778384 : p = getInstrPtr(mb, i);
35 104778384 : if (p && getModuleId(p) == optimizerRef && p->token == REMsymbol
36 0 : && getFunctionId(p) == opt)
37 : return 1;
38 : }
39 : return 0;
40 : }
41 :
42 : /*
43 : * Some optimizers are interdependent (e.g. mitosis ), which
44 : * requires inspection of the pipeline attached to a MAL block.
45 : */
46 : int
47 487581 : isOptimizerEnabled(MalBlkPtr mb, const char *opt)
48 : {
49 487581 : int i;
50 487581 : InstrPtr q;
51 :
52 9943585 : for (i = mb->stop - 1; i > 0; i--) {
53 9943585 : q = getInstrPtr(mb, i);
54 9943585 : if (q->token == ENDsymbol)
55 : break;
56 9782066 : if (q->token != REMsymbol && getModuleId(q) == optimizerRef
57 8457007 : && getFunctionId(q) == opt)
58 : return 1;
59 : }
60 : return 0;
61 : }
62 :
63 : /*
64 : * Find if an optimizer 'opt' has run before the instruction 'p'.
65 : */
66 : int
67 1462695 : isOptimizerUsed(MalBlkPtr mb, InstrPtr p, const char *opt)
68 : {
69 1462695 : bool p_found = false;
70 :
71 24371919 : for (int i = mb->stop - 1; i > 0; i--) {
72 24371930 : InstrPtr q = getInstrPtr(mb, i);
73 :
74 24371930 : p_found |= q == p; /* the optimizer to find must come before p */
75 24371930 : if (q && q->token == ENDsymbol)
76 : return 0;
77 24272401 : if (p_found && q && q != p && getModuleId(q) == optimizerRef
78 6871860 : && getFunctionId(q) == opt)
79 : return 1;
80 : }
81 : return 0;
82 : }
83 :
84 : /* Simple insertion statements do not require complex optimizer steps */
85 : int
86 1007167 : isSimpleSQL(MalBlkPtr mb)
87 : {
88 1007167 : int cnt = 0;
89 :
90 32899111 : for (int i = 0; i < mb->stop; i++) {
91 32451163 : InstrPtr p = getInstrPtr(mb, i);
92 :
93 32451163 : if (p && getModuleId(p) == sqlRef && getFunctionId(p) == appendRef)
94 1134604 : cnt++;
95 32451163 : if (p && getModuleId(p) == sqlRef && getFunctionId(p) == setVariableRef)
96 : return 1;
97 32450432 : if (p && getModuleId(p) == sqlcatalogRef)
98 : return 1;
99 : }
100 447948 : return cnt > 0.63 * mb->stop;
101 : }
102 :
103 : /* Only used by opt_commonTerms! */
104 : int
105 136355 : hasSameSignature(MalBlkPtr mb, InstrPtr p, InstrPtr q)
106 : {
107 136355 : int i;
108 :
109 136355 : if (q->retc != p->retc || q->argc != p->argc)
110 : return FALSE;
111 769157 : for (i = 0; i < p->argc; i++)
112 632820 : if (getArgType(mb, p, i) != getArgType(mb, q, i))
113 : return FALSE;
114 : return TRUE;
115 : }
116 :
117 : /* Only used by opt_commonTerms! */
118 : int
119 26678216 : hasSameArguments(MalBlkPtr mb, InstrPtr p, InstrPtr q)
120 : {
121 26678216 : int k;
122 26678216 : int (*cmp)(const void *, const void *);
123 26678216 : VarPtr w, u;
124 :
125 26678216 : (void) mb;
126 26678216 : if (p->retc != q->retc || p->argc != q->argc)
127 : return FALSE;
128 : /* heuristic, because instructions are linked using last constant argument */
129 37492572 : for (k = p->argc - 1; k >= p->retc; k--)
130 37356217 : if (q->argv[k] != p->argv[k]) {
131 30188277 : if (isVarConstant(mb, getArg(p, k))
132 28834321 : && isVarConstant(mb, getArg(q, k))) {
133 28834185 : w = getVar(mb, getArg(p, k));
134 28834185 : u = getVar(mb, getArg(q, k));
135 28834185 : cmp = ATOMcompare(w->value.vtype);
136 28834185 : if (w->value.vtype == u->value.vtype
137 28441887 : && (*cmp) (VALptr(&w->value), VALptr(&u->value)) == 0)
138 13313110 : continue;
139 : }
140 16873625 : return FALSE;
141 : }
142 : return TRUE;
143 : }
144 :
145 : /*
146 : * If two instructions have elements in common in their target list,
147 : * it means a variable is re-initialized and should not be considered
148 : * an alias.
149 : */
150 : int
151 136337 : hasCommonResults(InstrPtr p, InstrPtr q)
152 : {
153 136337 : int k, l;
154 :
155 280474 : for (k = 0; k < p->retc; k++)
156 305214 : for (l = 0; l < q->retc; l++)
157 161077 : if (p->argv[k] == q->argv[l])
158 : return TRUE;
159 : return FALSE;
160 : }
161 :
162 : /*
163 : * Dependency between target variables and arguments can be
164 : * checked with isDependent().
165 : */
166 : static int
167 122548 : isDependent(InstrPtr p, InstrPtr q)
168 : {
169 122548 : int i, j;
170 252896 : for (i = 0; i < q->retc; i++)
171 628600 : for (j = p->retc; j < p->argc; j++)
172 498252 : if (getArg(q, i) == getArg(p, j))
173 : return TRUE;
174 : return FALSE;
175 : }
176 :
177 : /*
178 : * The safety property should be relatively easy to determine for
179 : * each MAL function. This calls for accessing the function MAL block
180 : * and to inspect the arguments of the signature.
181 : */
182 : inline int
183 128105099 : isUnsafeFunction(InstrPtr q)
184 : {
185 128105099 : InstrPtr p;
186 :
187 128105099 : if (q->unsafeProp)
188 : return TRUE;
189 126178764 : if (q->fcn == 0 || getFunctionId(q) == 0 || q->blk == NULL)
190 : return FALSE;
191 0 : p = getInstrPtr(q->blk, 0);
192 0 : if (p->retc == 0)
193 0 : return TRUE;
194 : return FALSE;
195 : }
196 :
197 : /*
198 : * Instructions are unsafe if one of the arguments is also mentioned
199 : * in the result list. Alternatively, the 'unsafe' property is set
200 : * for the function call itself.
201 : */
202 : int
203 0 : isUnsafeInstruction(InstrPtr q)
204 : {
205 0 : int j, k;
206 :
207 0 : for (j = 0; j < q->retc; j++)
208 0 : for (k = q->retc; k < q->argc; k++)
209 0 : if (q->argv[k] == q->argv[j])
210 : return TRUE;
211 : return FALSE;
212 : }
213 :
214 : /*
215 : * Any instruction may block identification of a common
216 : * subexpression. It suffices to stumble upon an unsafe function
217 : * whose parameter lists has a non-empty intersection with the
218 : * targeted instruction.
219 : * To illustrate, consider the sequence
220 : * @example
221 : * L1 := f(A,B,C);
222 : * ...
223 : * G1 := g(D,E,F);
224 : * ...
225 : * l2:= f(A,B,C);
226 : * ...
227 : * L2:= h()
228 : * @end example
229 : *
230 : * The instruction G1:=g(D,E,F) is blocking if G1 is an alias
231 : * for @verb{ { }A,B,C@verb{ } }.
232 : * Alternatively, function g() may be unsafe and @verb{ { }D,E,F@verb{ } }
233 : * has a non-empty intersection with @verb{ { }A,B,C@verb{ } }.
234 : * An alias can only be used later on for readonly (and not be used for a function with side effects).
235 : */
236 : int
237 122548 : safetyBarrier(InstrPtr p, InstrPtr q)
238 : {
239 122548 : int i, j;
240 122548 : if (isDependent(q, p))
241 : return TRUE;
242 122548 : if (isUnsafeFunction(q)) {
243 0 : for (i = p->retc; i < p->argc; i++)
244 0 : for (j = q->retc; j < q->argc; j++)
245 0 : if (p->argv[i] == q->argv[j]) {
246 : /* TODO check safety property of the argument */
247 : return TRUE;
248 : }
249 : }
250 : return FALSE;
251 : }
252 :
253 : inline int
254 131781023 : isUpdateInstruction(InstrPtr p)
255 : {
256 131781023 : if (getModuleId(p) == sqlRef &&
257 29882636 : (getFunctionId(p) == appendRef || getFunctionId(p) == updateRef
258 27103325 : || getFunctionId(p) == deleteRef || getFunctionId(p) == claimRef
259 27020184 : || getFunctionId(p) == growRef || getFunctionId(p) == clear_tableRef
260 27019742 : || getFunctionId(p) == setVariableRef || getFunctionId(p) == dependRef
261 26932704 : || getFunctionId(p) == predicateRef))
262 : return TRUE;
263 128782792 : if (getModuleId(p) == batRef
264 9368054 : && (getFunctionId(p) == appendRef || getFunctionId(p) == replaceRef
265 7861626 : || getFunctionId(p) == deleteRef))
266 1506472 : return TRUE;
267 : return FALSE;
268 : }
269 :
270 : int
271 106778038 : hasSideEffects(MalBlkPtr mb, InstrPtr p, int strict)
272 : {
273 106778038 : if (getFunctionId(p) == NULL)
274 : return FALSE;
275 :
276 : /*
277 : * Void-returning operations have side-effects and
278 : * should be considered as such
279 : */
280 103968056 : if (p->retc == 0 || (p->retc == 1 && getArgType(mb, p, 0) == TYPE_void))
281 : return TRUE;
282 :
283 : /*
284 : * Any function marked as unsafe can not be moved around without
285 : * affecting its behavior on the program. For example, because they
286 : * check for volatile resource levels.
287 : */
288 98222940 : if (isUnsafeFunction(p))
289 : return TRUE;
290 :
291 : /* update instructions have side effects, they can be marked as unsafe */
292 96983139 : if (isUpdateInstruction(p))
293 : return TRUE;
294 :
295 94391780 : if ((getModuleId(p) == batRef || getModuleId(p) == sqlRef)
296 15661399 : && (getFunctionId(p) == setAccessRef))
297 : return TRUE;
298 :
299 94391780 : if (getModuleId(p) == malRef && getFunctionId(p) == multiplexRef)
300 : return FALSE;
301 :
302 94387509 : if (getModuleId(p) == ioRef ||
303 94387509 : getModuleId(p) == streamsRef ||
304 94387509 : getModuleId(p) == bstreamRef ||
305 94387509 : getModuleId(p) == mdbRef ||
306 94386878 : getModuleId(p) == malRef ||
307 94386878 : getModuleId(p) == remapRef ||
308 94386878 : getModuleId(p) == optimizerRef ||
309 47896264 : getModuleId(p) == lockRef ||
310 47896264 : getModuleId(p) == semaRef ||
311 47896264 : getModuleId(p) == alarmRef)
312 : return TRUE;
313 :
314 47896264 : if( getModuleId(p) == pyapi3Ref ||
315 47896264 : getModuleId(p) == rapiRef ||
316 47896124 : getModuleId(p) == capiRef)
317 : return TRUE;
318 :
319 47895973 : if (getModuleId(p) == sqlcatalogRef)
320 : return TRUE;
321 47895973 : if (getModuleId(p) == sqlRef) {
322 11729964 : if (getFunctionId(p) == tidRef)
323 : return FALSE;
324 10243017 : if (getFunctionId(p) == deltaRef)
325 : return FALSE;
326 9134113 : if (getFunctionId(p) == subdeltaRef)
327 : return FALSE;
328 8919537 : if (getFunctionId(p) == projectdeltaRef)
329 : return FALSE;
330 8343387 : if (getFunctionId(p) == bindRef)
331 : return FALSE;
332 1145747 : if (getFunctionId(p) == bindidxRef)
333 : return FALSE;
334 1129844 : if (getFunctionId(p) == binddbatRef)
335 : return FALSE;
336 1129844 : if (getFunctionId(p) == columnBindRef)
337 : return FALSE;
338 1129844 : if (getFunctionId(p) == copy_fromRef)
339 : return FALSE;
340 : /* assertions are the end-point of a flow path */
341 1129844 : if (getFunctionId(p) == not_uniqueRef)
342 : return FALSE;
343 1129844 : if (getFunctionId(p) == zero_or_oneRef)
344 : return FALSE;
345 1129844 : if (getFunctionId(p) == mvcRef)
346 : return FALSE;
347 12573 : if (getFunctionId(p) == singleRef)
348 : return FALSE;
349 12573 : if (getFunctionId(p) == importColumnRef)
350 : return FALSE;
351 11493 : return TRUE;
352 : }
353 36166009 : if (getModuleId(p) == mapiRef) {
354 0 : if (getFunctionId(p) == rpcRef)
355 : return TRUE;
356 0 : if (getFunctionId(p) == reconnectRef)
357 : return TRUE;
358 0 : if (getFunctionId(p) == disconnectRef)
359 : return TRUE;
360 : }
361 36166009 : if (strict && getFunctionId(p) == newRef && getModuleId(p) != groupRef)
362 : return TRUE;
363 :
364 35855314 : if (getModuleId(p) == sqlcatalogRef)
365 : return TRUE;
366 35855314 : if (getModuleId(p) == remoteRef)
367 36856 : return TRUE;
368 : return FALSE;
369 : }
370 :
371 : /* Void returning functions always have side-effects.
372 : */
373 : int
374 16466363 : mayhaveSideEffects(Client cntxt, MalBlkPtr mb, InstrPtr p, int strict)
375 : {
376 16466363 : int tpe;
377 16466363 : tpe = getVarType(mb, getArg(p, 0));
378 16466363 : if (tpe == TYPE_void)
379 : return TRUE;
380 16021900 : if (getModuleId(p) != malRef || getFunctionId(p) != multiplexRef)
381 16020125 : return hasSideEffects(mb, p, strict);
382 : // a manifold instruction can also have side effects.
383 : // for this to check we need the function signature, not its function address.
384 : // The easy way out now is to consider all manifold instructions as potentially having side effects.
385 1775 : if (getModuleId(p) == malRef && getFunctionId(p) == manifoldRef)
386 : return TRUE;
387 1775 : if (MANIFOLDtypecheck(cntxt, mb, p, 1) == NULL)
388 : return TRUE;
389 : return FALSE;
390 : }
391 :
392 : /*
393 : * Side-effect free functions are crucial for several operators.
394 : */
395 : int
396 260 : isSideEffectFree(MalBlkPtr mb)
397 : {
398 260 : int i;
399 4206 : for (i = 1; i < mb->stop && getInstrPtr(mb, i)->token != ENDsymbol; i++) {
400 3994 : if (hasSideEffects(mb, getInstrPtr(mb, i), TRUE))
401 : return FALSE;
402 : }
403 : return TRUE;
404 : }
405 :
406 : /*
407 : * Breaking up a MAL program into pieces for distributed processing requires
408 : * identification of (partial) blocking instructions. A conservative
409 : * definition can be used.
410 : */
411 : inline int
412 0 : isBlocking(InstrPtr p)
413 : {
414 0 : if (blockStart(p) || blockExit(p) || blockCntrl(p))
415 : return TRUE;
416 :
417 0 : if (getFunctionId(p) == sortRef)
418 : return TRUE;
419 :
420 0 : if (getModuleId(p) == aggrRef || getModuleId(p) == groupRef
421 0 : || getModuleId(p) == sqlcatalogRef)
422 0 : return TRUE;
423 : return FALSE;
424 : }
425 :
426 : /*
427 : * Used in the merge table optimizer. It is built incrementally
428 : * and should be conservative.
429 : */
430 :
431 : static int
432 220139 : isOrderDepenent(InstrPtr p)
433 : {
434 220139 : if (getModuleId(p) != batsqlRef)
435 : return 0;
436 1095 : if (getFunctionId(p) == differenceRef || getFunctionId(p) == window_boundRef
437 993 : || getFunctionId(p) == row_numberRef || getFunctionId(p) == rankRef
438 850 : || getFunctionId(p) == dense_rankRef
439 838 : || getFunctionId(p) == percent_rankRef
440 833 : || getFunctionId(p) == cume_distRef || getFunctionId(p) == ntileRef
441 813 : || getFunctionId(p) == first_valueRef
442 805 : || getFunctionId(p) == last_valueRef || getFunctionId(p) == nth_valueRef
443 802 : || getFunctionId(p) == lagRef || getFunctionId(p) == leadRef
444 797 : || getFunctionId(p) == corrRef)
445 306 : return 1;
446 : return 0;
447 : }
448 :
449 : inline int
450 15128973 : isMapOp(InstrPtr p)
451 : {
452 15128973 : if (isUnsafeFunction(p))
453 : return 0;
454 14980894 : return getModuleId(p)
455 14617760 : && ((getModuleId(p) == malRef && getFunctionId(p) == multiplexRef)
456 535 : || (getModuleId(p) == malRef && getFunctionId(p) == manifoldRef)
457 14617225 : || (getModuleId(p) == batcalcRef)
458 14470527 : || (getModuleId(p) != batcalcRef && getModuleId(p) != batRef
459 13902804 : && strncmp(getModuleId(p), "bat", 3) == 0)
460 14617760 : || (getModuleId(p) == batmkeyRef)) && !isOrderDepenent(p)
461 161788 : && getModuleId(p) != batrapiRef && getModuleId(p) != batpyapi3Ref
462 15142669 : && getModuleId(p) != batcapiRef;
463 : }
464 :
465 : inline int
466 159142 : isMap2Op(InstrPtr p)
467 : {
468 159142 : if (isUnsafeFunction(p))
469 : return 0;
470 159072 : return getModuleId(p)
471 159071 : && ((getModuleId(p) == malRef && getFunctionId(p) == multiplexRef)
472 162 : || (getModuleId(p) == malRef && getFunctionId(p) == manifoldRef)
473 158909 : || (getModuleId(p) == batcalcRef)
474 104383 : || (getModuleId(p) != batcalcRef && getModuleId(p) != batRef
475 79819 : && strncmp(getModuleId(p), "bat", 3) == 0)
476 159071 : || (getModuleId(p) == batmkeyRef)) && !isOrderDepenent(p)
477 58045 : && getModuleId(p) != batrapiRef && getModuleId(p) != batpyapi3Ref
478 217113 : && getModuleId(p) != batcapiRef;
479 : }
480 :
481 : inline int
482 18387846 : isLikeOp(InstrPtr p)
483 : {
484 18387846 : return (getModuleId(p) == batalgebraRef
485 18387846 : && (getFunctionId(p) == likeRef
486 127 : || getFunctionId(p) == not_likeRef));
487 : }
488 :
489 : inline int
490 159061 : isTopn(InstrPtr p)
491 : {
492 51134 : return ((getModuleId(p) == algebraRef && getFunctionId(p) == firstnRef)
493 210069 : || isSlice(p));
494 : }
495 :
496 : inline int
497 37179293 : isSlice(InstrPtr p)
498 : {
499 37179293 : return (getModuleId(p) == algebraRef
500 37179293 : && (getFunctionId(p) == subsliceRef
501 3478615 : || getFunctionId(p) == sliceRef));
502 : }
503 :
504 : int
505 19647274 : isSample(InstrPtr p)
506 : {
507 19647274 : return (getModuleId(p) == sampleRef && getFunctionId(p) == subuniformRef);
508 : }
509 :
510 : inline int
511 0 : isOrderby(InstrPtr p)
512 : {
513 0 : return getModuleId(p) == algebraRef && getFunctionId(p) == sortRef;
514 : }
515 :
516 : inline int
517 1422621 : isMatJoinOp(InstrPtr p)
518 : {
519 1422621 : return (isSubJoin(p)
520 1422621 : || (getModuleId(p) == algebraRef
521 915347 : && (getFunctionId(p) == crossRef || getFunctionId(p) == joinRef
522 897377 : || getFunctionId(p) == thetajoinRef
523 897377 : || getFunctionId(p) == bandjoinRef
524 897377 : || getFunctionId(p) == rangejoinRef)
525 : ));
526 : }
527 :
528 : inline int
529 1245016 : isMatLeftJoinOp(InstrPtr p)
530 : {
531 1245016 : return (getModuleId(p) == algebraRef
532 1245016 : && (getFunctionId(p) == leftjoinRef
533 995179 : || getFunctionId(p) == outerjoinRef
534 995172 : || getFunctionId(p) == markjoinRef));
535 : }
536 :
537 : inline int
538 107716 : isDelta(InstrPtr p)
539 : {
540 107716 : return (getModuleId(p) == sqlRef
541 107716 : && (getFunctionId(p) == deltaRef
542 33725 : || getFunctionId(p) == projectdeltaRef
543 11949 : || getFunctionId(p) == subdeltaRef)
544 : );
545 : }
546 :
547 : int
548 18325 : isFragmentGroup2(InstrPtr p)
549 : {
550 18325 : if (getModuleId(p) == batRef && getFunctionId(p) == replaceRef)
551 : return TRUE;
552 11 : return (getModuleId(p) == algebraRef && (getFunctionId(p) == projectionRef))
553 8959 : || (getModuleId(p) == batRef
554 8772 : && (getFunctionId(p) == mergecandRef
555 0 : || getFunctionId(p) == intersectcandRef
556 0 : || getFunctionId(p) == diffcandRef)
557 : );
558 : }
559 :
560 : inline int
561 36269110 : isSelect(InstrPtr p)
562 : {
563 36269110 : const char *func = getFunctionId(p);
564 69903546 : size_t l = func ? strlen(func) : 0;
565 :
566 34891783 : return (l >= 6 && strcmp(func + l - 6, "select") == 0);
567 : }
568 :
569 : inline int
570 1422621 : isSubJoin(InstrPtr p)
571 : {
572 1422621 : const char *func = getFunctionId(p);
573 2736603 : size_t l = func ? strlen(func) : 0;
574 :
575 1422540 : return (l >= 4 && strcmp(func + l - 4, "join") == 0);
576 : }
577 :
578 : inline int
579 57307250 : isMultiplex(InstrPtr p)
580 : {
581 57307250 : return (malRef && (getModuleId(p) == malRef || getModuleId(p) == batmalRef)
582 57478151 : && getFunctionId(p) == multiplexRef);
583 : }
584 :
585 : int
586 100957 : isFragmentGroup(InstrPtr p)
587 : {
588 100957 : return (getModuleId(p) == algebraRef
589 76219 : && (getFunctionId(p) == projectRef
590 84999 : || getFunctionId(p) == selectNotNilRef)) || isSelect(p)
591 125695 : || (getModuleId(p) == batRef && (getFunctionId(p) == mirrorRef));
592 : }
|