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, 2025 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : /* The SQL code generator can not always look ahead to avoid
14 : * generation of intermediates.
15 : * Some of these patterns are captured in a postfix optimalisation.
16 : */
17 : #include "monetdb_config.h"
18 : #include "mal_instruction.h"
19 : #include "opt_postfix.h"
20 :
21 : #define isCandidateList(M,P,I) ((M)->var[getArg(P,I)].id[0]== 'C')
22 : str
23 539441 : OPTpostfixImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk,
24 : InstrPtr pci)
25 : {
26 539441 : int i, slimit, actions = 0;
27 539441 : str msg = MAL_SUCCEED;
28 539441 : InstrPtr p;
29 :
30 539441 : (void) cntxt;
31 539441 : (void) stk;
32 :
33 539441 : slimit = mb->stop;
34 539441 : setVariableScope(mb);
35 : /* Remove the result from any join/group instruction when it is not used later on */
36 33048141 : for (i = 0; i < slimit; i++) {
37 : /* POSTFIX ACTION FOR THE JOIN CASE */
38 31969194 : p = getInstrPtr(mb, i);
39 31969194 : if (getModuleId(p) == algebraRef && p->retc == 2) {
40 536012 : if (getFunctionId(p) == leftjoinRef || /*getFunctionId(p) == outerjoinRef || */
41 : getFunctionId(p) == bandjoinRef
42 535364 : || getFunctionId(p) == rangejoinRef
43 535214 : || getFunctionId(p) == likejoinRef) {
44 851 : if (getVarEolife(mb, getArg(p, p->retc - 1)) == i) {
45 56 : delArgument(p, p->retc - 1);
46 56 : typeChecker(cntxt->usermodule, mb, p, i, TRUE);
47 56 : actions++;
48 56 : continue;
49 : }
50 535161 : } else if (getFunctionId(p) == semijoinRef
51 534888 : || getFunctionId(p) == joinRef
52 118478 : || getFunctionId(p) == thetajoinRef
53 67517 : || /*getFunctionId(p) == outerjoinRef || */
54 : getFunctionId(p) == crossRef) {
55 514057 : int is_first_ret_not_used = getVarEolife(mb, getArg(p, p->retc - 2)) == i;
56 514057 : int is_second_ret_not_used = getVarEolife(mb, getArg(p, p->retc - 1)) == i;
57 :
58 514057 : if (getFunctionId(p) == semijoinRef
59 273 : &&
60 : ((is_first_ret_not_used
61 58 : && getVarConstant(mb, getArg(p, 7)).val.btval != 1 /*not single */ )
62 273 : || is_second_ret_not_used)) {
63 : /* Can't swap arguments on single semijoins */
64 91 : if (is_first_ret_not_used) {
65 : /* semijoin with just the right output is a join */
66 0 : getArg(p, 2) ^= getArg(p, 3); /* swap join inputs */
67 0 : getArg(p, 3) ^= getArg(p, 2);
68 0 : getArg(p, 2) ^= getArg(p, 3);
69 :
70 0 : getArg(p, 4) ^= getArg(p, 5); /* swap candidate lists */
71 0 : getArg(p, 5) ^= getArg(p, 4);
72 0 : getArg(p, 4) ^= getArg(p, 5);
73 0 : setFunctionId(p, joinRef);
74 0 : delArgument(p, 7); /* delete 'max_one' argument */
75 : } else {
76 : /* semijoin with just the left output is an intersection */
77 91 : setFunctionId(p, intersectRef);
78 : }
79 :
80 182 : delArgument(p,
81 91 : is_second_ret_not_used ? p->retc - 1 : p->retc -
82 : 2);
83 91 : typeChecker(cntxt->usermodule, mb, p, i, TRUE);
84 91 : actions++;
85 91 : continue;
86 513966 : } else if (is_second_ret_not_used) {
87 62494 : delArgument(p, p->retc - 1);
88 62494 : typeChecker(cntxt->usermodule, mb, p, i, TRUE);
89 62494 : actions++;
90 62494 : continue;
91 451472 : } else if (is_first_ret_not_used &&
92 : (getFunctionId(p) == joinRef
93 13769 : || (getFunctionId(p) == thetajoinRef
94 13365 : && isVarConstant(mb, getArg(p, 6)))
95 404 : || (getFunctionId(p) == crossRef
96 346 : && getVarConstant(mb, getArg(p, 4)).val.btval != 1 /*not single */ ))) {
97 : /* Can't swap arguments on single cross products */
98 : /* swap join inputs */
99 25731 : getArg(p, 2) ^= getArg(p, 3);
100 25731 : getArg(p, 3) ^= getArg(p, 2);
101 25731 : getArg(p, 2) ^= getArg(p, 3);
102 :
103 25731 : if (getFunctionId(p) != crossRef) { /* swap candidate lists */
104 25390 : getArg(p, 4) ^= getArg(p, 5);
105 25390 : getArg(p, 5) ^= getArg(p, 4);
106 25390 : getArg(p, 4) ^= getArg(p, 5);
107 25390 : if (getFunctionId(p) == thetajoinRef) { /* swap the comparison */
108 13365 : ValRecord *x = &getVarConstant(mb, getArg(p, 6)),
109 13365 : cst = {
110 : .vtype = TYPE_int
111 : };
112 13365 : switch (x->val.ival) {
113 13293 : case JOIN_LT:
114 13293 : cst.val.ival = JOIN_GT;
115 13293 : break;
116 17 : case JOIN_LE:
117 17 : cst.val.ival = JOIN_GE;
118 17 : break;
119 47 : case JOIN_GT:
120 47 : cst.val.ival = JOIN_LT;
121 47 : break;
122 8 : case JOIN_GE:
123 8 : cst.val.ival = JOIN_LE;
124 8 : break;
125 0 : default:
126 0 : cst.val.ival = x->val.ival;
127 : }
128 13365 : setArg(p, 6, defConstant(mb, TYPE_int, &cst));
129 : }
130 : }
131 25731 : delArgument(p, p->retc - 2);
132 25731 : typeChecker(cntxt->usermodule, mb, p, i, TRUE);
133 25731 : actions++;
134 25731 : continue;
135 : }
136 : }
137 : }
138 : /* POSTFIX ACTION FOR THE EXTENT CASE */
139 31880822 : if (getModuleId(p) == groupRef && getFunctionId(p) == groupRef
140 20952 : && getVarEolife(mb, getArg(p, p->retc - 1)) == i) {
141 20910 : delArgument(p, p->retc - 1);
142 20910 : typeChecker(cntxt->usermodule, mb, p, i, TRUE);
143 20910 : actions++;
144 20910 : continue;
145 : }
146 31859912 : if (getModuleId(p) == groupRef && getFunctionId(p) == subgroupRef
147 5424 : && getVarEolife(mb, getArg(p, p->retc - 1)) == i) {
148 5424 : delArgument(p, p->retc - 1);
149 5424 : typeChecker(cntxt->usermodule, mb, p, i, TRUE);
150 5424 : actions++;
151 5424 : continue;
152 : }
153 31854488 : if (getModuleId(p) == groupRef && getFunctionId(p) == subgroupdoneRef
154 21129 : && getVarEolife(mb, getArg(p, p->retc - 1)) == i) {
155 20816 : delArgument(p, p->retc - 1);
156 20816 : typeChecker(cntxt->usermodule, mb, p, i, TRUE);
157 20816 : actions++;
158 20816 : continue;
159 : }
160 31833672 : if (getModuleId(p) == groupRef && getFunctionId(p) == groupdoneRef
161 42697 : && getVarEolife(mb, getArg(p, p->retc - 1)) == i) {
162 33165 : delArgument(p, p->retc - 1);
163 33165 : typeChecker(cntxt->usermodule, mb, p, i, TRUE);
164 33165 : actions++;
165 33165 : continue;
166 : }
167 : /* POSTFIX ACTION FOR SORT, could be dropping the last two */
168 31800507 : if (getModuleId(p) == algebraRef && getFunctionId(p) == sortRef
169 26406 : && getVarEolife(mb, getArg(p, p->retc - 1)) == i) {
170 12721 : delArgument(p, p->retc - 1);
171 12721 : typeChecker(cntxt->usermodule, mb, p, i, TRUE);
172 12721 : actions++;
173 12721 : if (getModuleId(p) == algebraRef && getFunctionId(p) == sortRef
174 12721 : && getVarEolife(mb, getArg(p, p->retc - 1)) == i) {
175 3563 : delArgument(p, p->retc - 1);
176 3563 : typeChecker(cntxt->usermodule, mb, p, i, TRUE);
177 3563 : actions++;
178 : }
179 12721 : continue;
180 : }
181 : }
182 : /* Defense line against incorrect plans */
183 539506 : if (actions) {
184 : // msg = chkTypes(cntxt->usermodule, mb, FALSE);
185 : // if (!msg)
186 : // msg = chkFlow(mb);
187 : // if (!msg)
188 : // msg = chkDeclarations(mb);
189 539506 : }
190 : /* keep actions taken as a fake argument */
191 539506 : (void) pushInt(mb, pci, actions);
192 539500 : return msg;
193 : }
|