| 1 | /* |
| 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | * |
| 6 | * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V. |
| 7 | */ |
| 8 | |
| 9 | /* Author(s) Martin Kersten |
| 10 | * This module contains the framework for inclusion query transformers, i.e. |
| 11 | * C-programs geared at optimizing a piece of MAL. |
| 12 | * The query transformer appears at the language level as an ordinary function, |
| 13 | * but it is effective only at a specific execution phase. |
| 14 | * |
| 15 | * Each optimizer function has access to the runtime scope of the |
| 16 | * routine in which it is called. This can be used to maintain status |
| 17 | * information between successive calls. |
| 18 | * |
| 19 | * The routines below are linked with the kernel by default |
| 20 | */ |
| 21 | #include "monetdb_config.h" |
| 22 | #include "optimizer.h" |
| 23 | #include "mal_debugger.h" |
| 24 | #include "optimizer_private.h" |
| 25 | #include "opt_pipes.h" |
| 26 | |
| 27 | /* |
| 28 | * Upon loading the module it should inspect the scenario table |
| 29 | * for any unresolved references to the MALoptimizer and set the |
| 30 | * callback function. |
| 31 | */ |
| 32 | str |
| 33 | optimizer_prelude(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr p) |
| 34 | { |
| 35 | (void) cntxt; |
| 36 | (void) stk; |
| 37 | (void) mb; |
| 38 | (void) p; |
| 39 | updateScenario("mal" , "MALoptimizer" , (MALfcn) MALoptimizer); |
| 40 | optimizerInit(); |
| 41 | //return compileAllOptimizers(cntxt); causes problems |
| 42 | return MAL_SUCCEED; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | * MAL functions can be optimized explicitly using the routines below. |
| 48 | * Beware, the function names should be known as literal strings, because |
| 49 | * you may not know the runtime situation. |
| 50 | */ |
| 51 | |
| 52 | str |
| 53 | QOToptimize(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) |
| 54 | { |
| 55 | str modnme; |
| 56 | str fcnnme; |
| 57 | Symbol s; |
| 58 | |
| 59 | (void) stk; |
| 60 | if (stk != 0) { |
| 61 | modnme = *getArgReference_str(stk, pci, 1); |
| 62 | fcnnme = *getArgReference_str(stk, pci, 2); |
| 63 | } else { |
| 64 | modnme = getArgDefault(mb, pci, 1); |
| 65 | fcnnme = getArgDefault(mb, pci, 2); |
| 66 | } |
| 67 | s = findSymbol(cntxt->usermodule, putName(modnme), fcnnme); |
| 68 | if (s == NULL) |
| 69 | throw(MAL, "optimizer.optimize" , SQLSTATE(HY002) SEMANTIC_OPERATION_MISSING); |
| 70 | removeInstruction(mb, pci); |
| 71 | addtoMalBlkHistory(s->def); |
| 72 | return optimizeMALBlock(cntxt, s->def); |
| 73 | } |
| 74 | |
| 75 | |