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 : /* Author(s) Martin Kersten
14 : * This module contains the framework for inclusion query transformers, i.e.
15 : * C-programs geared at optimizing a piece of MAL.
16 : * The query transformer appears at the language level as an ordinary function,
17 : * but it is effective only at a specific execution phase.
18 : *
19 : * Each optimizer function has access to the runtime scope of the
20 : * routine in which it is called. This can be used to maintain status
21 : * information between successive calls.
22 : *
23 : * The routines below are linked with the kernel by default
24 : */
25 : #include "monetdb_config.h"
26 : #include "mal_scenario.h"
27 : #include "optimizer.h"
28 : #include "optimizer_private.h"
29 : #include "opt_pipes.h"
30 : #include "mal_session.h"
31 :
32 : str
33 350 : optimizer_epilogue(void *ret)
34 : {
35 350 : (void) ret;
36 350 : opt_pipes_reset();
37 350 : return MAL_SUCCEED;
38 : }
39 :
40 : /*
41 : * MAL functions can be optimized explicitly using the routines below.
42 : * Beware, the function names should be known as literal strings, because
43 : * you may not know the runtime situation.
44 : */
45 :
46 : str
47 0 : QOToptimize(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
48 : {
49 0 : str modnme;
50 0 : str fcnnme;
51 0 : Symbol s;
52 :
53 0 : (void) stk;
54 0 : if (stk != 0) {
55 0 : modnme = *getArgReference_str(stk, pci, 1);
56 0 : fcnnme = *getArgReference_str(stk, pci, 2);
57 : } else {
58 0 : modnme = getArgDefault(mb, pci, 1);
59 0 : fcnnme = getArgDefault(mb, pci, 2);
60 : }
61 0 : s = findSymbol(cntxt->usermodule, putName(modnme), fcnnme);
62 0 : if (s == NULL)
63 0 : throw(MAL, "optimizer.optimize",
64 : SQLSTATE(HY002) SEMANTIC_OPERATION_MISSING);
65 0 : removeInstruction(mb, pci);
66 0 : return optimizeMALBlock(cntxt, s->def);
67 : }
68 :
69 : #define optwrapper_pattern(NAME, DESC) \
70 : pattern("optimizer", NAME, OPTwrapper, false, "", args(1,1, arg("",str))), \
71 : pattern("optimizer", NAME, OPTwrapper, false, DESC, args(1,3, arg("",str),arg("mod",str),arg("fcn",str)))
72 :
73 :
74 : #include "mel.h"
75 : static mel_func optimizer_init_funcs[] = {
76 : optwrapper_pattern("aliases", "Alias removal optimizer"),
77 : optwrapper_pattern("coercions", "Handle simple type coercions"),
78 : optwrapper_pattern("commonTerms", "Common sub-expression optimizer"),
79 : optwrapper_pattern("candidates", "Mark candidate list variables"),
80 : optwrapper_pattern("constants", "Duplicate constant removal optimizer"),
81 : optwrapper_pattern("profiler", "Collect properties for the profiler"),
82 : optwrapper_pattern("costModel",
83 : "Estimate the cost of a relational expression"),
84 : optwrapper_pattern("dataflow", "Dataflow bracket code injection"),
85 : optwrapper_pattern("deadcode", "Dead code optimizer"),
86 : optwrapper_pattern("emptybind", "Evaluate empty set expressions"),
87 : optwrapper_pattern("evaluate", "Evaluate constant expressions once"),
88 : optwrapper_pattern("garbageCollector", "Garbage collector optimizer"),
89 : optwrapper_pattern("generator", "Sequence generator optimizer"),
90 : optwrapper_pattern("querylog", "Collect SQL query statistics"),
91 : optwrapper_pattern("minimalfast", "Fast compound minimal optimizer pipe"),
92 : optwrapper_pattern("defaultfast", "Fast compound default optimizer pipe"),
93 : optwrapper_pattern("wrapper", "Fake optimizer"),
94 : command("optimizer", "epilogue", optimizer_epilogue, false,
95 : "release the resources held by the optimizer module",
96 : args(1, 1, arg("", void))),
97 : pattern("optimizer", "optimize", QOToptimize, false,
98 : "Optimize a specific operation",
99 : args(0, 2, arg("mod", str), arg("fcn", str))),
100 : optwrapper_pattern("inline", "Expand inline functions"),
101 : optwrapper_pattern("projectionpath", "Join path constructor"),
102 : optwrapper_pattern("mergetable", "Resolve the multi-table definitions"),
103 : optwrapper_pattern("mitosis",
104 : "Modify the plan to exploit parallel processing on multiple cores"),
105 : optwrapper_pattern("multiplex", "Compiler for multiplexed instructions"),
106 : optwrapper_pattern("matpack", "Unroll the mat.pack operation"),
107 : optwrapper_pattern("reduce", "Reduce the stack space claims"),
108 : optwrapper_pattern("remap",
109 : "Remapping function calls to a their multiplex variant"),
110 : optwrapper_pattern("remoteQueries", "Resolve the multi-table definitions"),
111 : optwrapper_pattern("reorder", "Reorder by dataflow dependencies"),
112 : pattern("inspect", "optimizer_stats", OPTstatistics, false,
113 : "Get optimizer use statistics, i.e. calls and total time",
114 : args(3, 3, batarg("", str), batarg("", int), batarg("", lng))),
115 : optwrapper_pattern("pushselect", "Push selects down projections"),
116 : optwrapper_pattern("postfix", "Postfix the plan,e.g. pushing projections"),
117 : optwrapper_pattern("strimps", "Use strimps index if appropriate"),
118 : optwrapper_pattern("for", "Push for decompress down"),
119 : optwrapper_pattern("dict", "Push dict decompress down"),
120 : {.imp = NULL}
121 : };
122 :
123 : #include "mal_import.h"
124 : #ifdef _MSC_VER
125 : #undef read
126 : #pragma section(".CRT$XCU",read)
127 : #endif
128 345 : LIB_STARTUP_FUNC(init_optimizer_mal)
129 : {
130 345 : mal_module2("optimizer", NULL, optimizer_init_funcs, NULL, NULL);
131 345 : }
|