1 /* Copyright (C) 2014 InfiniDB, Inc.
2    Copyright (C) 2016 MariaDB Corporation
3 
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License
6    as published by the Free Software Foundation; version 2 of
7    the License.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17    MA 02110-1301, USA. */
18 
19 /***********************************************************************
20 *   $Id: ha_in_sub.cpp 6407 2010-03-26 19:36:56Z zzhu $
21 *
22 *
23 ***********************************************************************/
24 /** @file */
25 /** class InSub definition */
26 
27 #include <my_config.h>
28 #include <stdint.h>
29 //#define NDEBUG
30 #include <cassert>
31 #include <vector>
32 using namespace std;
33 
34 #include "idb_mysql.h"
35 
36 #include "parsetree.h"
37 #include "logicoperator.h"
38 #include "existsfilter.h"
39 #include "simplescalarfilter.h"
40 #include "selectfilter.h"
41 #include "simplefilter.h"
42 #include "predicateoperator.h"
43 #include "rowcolumn.h"
44 using namespace execplan;
45 
46 #include "errorids.h"
47 using namespace logging;
48 
49 #include "ha_subquery.h"
50 
51 namespace cal_impl_if
52 {
53 extern void parse_item (Item* item, vector<Item_field*>& field_vec, bool& hasNonSupportItem, uint16& parseInfo);
54 
makeAntiJoin(const ParseTree * n)55 void makeAntiJoin(const ParseTree* n)
56 {
57     TreeNode* tn = n->data();
58     SimpleFilter* sf = dynamic_cast<SimpleFilter*>(tn);
59 
60     if (!sf)
61         return;
62 
63     uint64_t lJoinInfo = sf->lhs()->joinInfo();
64 
65     if (lJoinInfo & JOIN_SEMI)
66     {
67         lJoinInfo &= ~JOIN_SEMI;
68         lJoinInfo |= JOIN_ANTI;
69 
70         if (lJoinInfo & JOIN_NULLMATCH_CANDIDATE)
71             lJoinInfo |= JOIN_NULL_MATCH;
72 
73         sf->lhs()->joinInfo(lJoinInfo);
74     }
75 
76     uint64_t rJoinInfo = sf->rhs()->joinInfo();
77 
78     if (rJoinInfo & JOIN_SEMI)
79     {
80         rJoinInfo &= ~JOIN_SEMI;
81         rJoinInfo |= JOIN_ANTI;
82 
83         if (rJoinInfo & JOIN_NULLMATCH_CANDIDATE)
84             rJoinInfo |= JOIN_NULL_MATCH;
85 
86         sf->rhs()->joinInfo(rJoinInfo);
87     }
88 }
89 
InSub(gp_walk_info & gwip)90 InSub::InSub(gp_walk_info& gwip) : WhereSubQuery(gwip)
91 {}
92 
InSub(gp_walk_info & gwip,Item_func * func)93 InSub::InSub(gp_walk_info& gwip, Item_func* func) :
94     WhereSubQuery(gwip, func)
95 {}
96 
InSub(const InSub & rhs)97 InSub::InSub(const InSub& rhs) :
98     WhereSubQuery(rhs.gwip(), rhs.fColumn, rhs.fSub, rhs.fFunc)
99 {}
100 
~InSub()101 InSub::~InSub()
102 {}
103 
104 /** MySQL transform (NOT) IN subquery to (NOT) EXIST
105  *
106  */
transform()107 execplan::ParseTree* InSub::transform()
108 {
109     if (!fFunc)
110         return NULL;
111 
112     // @todo need to handle scalar IN and BETWEEN specially
113     // this blocks handles only one subselect scalar
114     // arg[0]: column | arg[1]: subselect
115     //assert (fFunc->argument_count() == 2 && fGwip.rcWorkStack.size() >= 2);
116     if (fFunc->argument_count() != 2 || fGwip.rcWorkStack.size() < 2)
117     {
118         fGwip.fatalParseError = true;
119         fGwip.parseErrorText = "Unsupported item in IN subquery";
120         return NULL;
121     }
122 
123     ReturnedColumn* rhs = fGwip.rcWorkStack.top();
124     fGwip.rcWorkStack.pop();
125     delete rhs;
126     ReturnedColumn* lhs = fGwip.rcWorkStack.top();
127     fGwip.rcWorkStack.pop();
128     delete lhs;
129 
130     fSub = (Item_subselect*)(fFunc->arguments()[1]);
131     idbassert(fSub && fFunc);
132 
133     SCSEP csep (new CalpontSelectExecutionPlan());
134     csep->sessionID(fGwip.sessionid);
135     csep->location(CalpontSelectExecutionPlan::WHERE);
136     csep->subType (CalpontSelectExecutionPlan::IN_SUBS);
137 
138     // gwi for the sub query
139     gp_walk_info gwi;
140     gwi.thd = fGwip.thd;
141     gwi.subQuery = this;
142 
143     // @4827 merge table list to gwi in case there is FROM sub to be referenced
144     // in the FROM sub
145     gwi.derivedTbCnt = fGwip.derivedTbList.size();
146     uint32_t tbCnt = fGwip.tbList.size();
147 
148     gwi.tbList.insert(gwi.tbList.begin(), fGwip.tbList.begin(), fGwip.tbList.end());
149     gwi.derivedTbList.insert(gwi.derivedTbList.begin(), fGwip.derivedTbList.begin(), fGwip.derivedTbList.end());
150 
151     if (getSelectPlan(gwi, *(fSub->get_select_lex()), csep, false) != 0)
152     {
153         fGwip.fatalParseError = true;
154 
155         if (gwi.fatalParseError && !gwi.parseErrorText.empty())
156             fGwip.parseErrorText = gwi.parseErrorText;
157         else
158             fGwip.parseErrorText = "Error occured in InSub::transform()";
159 
160         return NULL;
161     }
162 
163     // remove outer query tables
164     CalpontSelectExecutionPlan::TableList tblist;
165 
166     if (csep->tableList().size() >= tbCnt)
167         tblist.insert(tblist.begin(), csep->tableList().begin() + tbCnt, csep->tableList().end());
168 
169     CalpontSelectExecutionPlan::SelectList derivedTbList;
170 
171     if (csep->derivedTableList().size() >= gwi.derivedTbCnt)
172         derivedTbList.insert(derivedTbList.begin(),
173                              csep->derivedTableList().begin() + gwi.derivedTbCnt,
174                              csep->derivedTableList().end());
175 
176     csep->tableList(tblist);
177     csep->derivedTableList(derivedTbList);
178 
179     ExistsFilter* subFilter = new ExistsFilter();
180     subFilter->sub(csep);
181 
182     if (gwi.subQuery->correlated())
183         subFilter->correlated(true);
184     else
185         subFilter->correlated(false);
186 
187     if (fGwip.clauseType == HAVING && subFilter->correlated())
188     {
189         fGwip.fatalParseError = true;
190         fGwip.parseErrorText = logging::IDBErrorInfo::instance()->errorMsg(logging::ERR_NON_SUPPORT_HAVING);
191     }
192 
193     fGwip.subselectList.push_back(csep);
194     return new ParseTree(subFilter);
195 }
196 
197 /**
198  * Handle MySQL's plugin functions
199  * This is mostly for handling the null related functions that MySQL adds to the execution plan
200  */
handleFunc(gp_walk_info * gwip,Item_func * func)201 void InSub::handleFunc(gp_walk_info* gwip, Item_func* func)
202 {
203     if (func->functype() == Item_func::TRIG_COND_FUNC || func->functype() == Item_func::COND_OR_FUNC)
204     {
205         // purpose: remove the isnull() function from the parsetree in ptWorkStack.
206         // IDB handles the null semantics in the join operation
207         // trigcond(or_cond) is the only form we recognize for now
208         if (func->argument_count() > 2)
209         {
210             fGwip.fatalParseError = true;
211             fGwip.parseErrorText = "Unsupported item in IN subquery";
212             return;
213         }
214 
215         Item_cond* cond;
216 
217         if (func->functype() == Item_func::TRIG_COND_FUNC)
218         {
219             Item* item;
220 
221             if (func->arguments()[0]->type() == Item::REF_ITEM)
222                 item = (Item_ref*)(func->arguments()[0])->real_item();
223             else
224                 item = func->arguments()[0];
225 
226             cond = (Item_cond*)(item);
227         }
228         else
229         {
230             cond = (Item_cond*)(func);
231         }
232 
233         if (cond->functype() == Item_func::COND_OR_FUNC)
234         {
235             // (cache=item) case. do nothing. ignore trigcond()?
236             if (cond->argument_list()->elements == 1)
237                 return;
238 
239             if (cond->argument_list()->elements == 2)
240             {
241                 // don't know how to deal with this. don't think it's a fatal error either.
242                 if (gwip->ptWorkStack.empty())
243                     return;
244 
245                 ParseTree* pt = gwip->ptWorkStack.top();
246 
247                 if (!pt->left() || !pt->right())
248                     return;
249 
250                 SimpleFilter* lsf = dynamic_cast<SimpleFilter*>(pt->left()->data());
251                 SimpleFilter* rsf = dynamic_cast<SimpleFilter*>(pt->right()->data());
252                 SimpleFilter* sf = nullptr;
253 
254                 if (!lsf || !rsf)
255                     return;
256 
257                 if ((lsf->op()->op() == execplan::OP_ISNULL ||
258                      lsf->op()->op() == execplan::OP_ISNOTNULL) &&
259                     rsf->op()->op() == execplan::OP_EQ)
260                 {
261                     // (cache=item or isnull(item)) case. remove "or isnull()"
262                     if ((rsf->lhs()->joinInfo() & JOIN_CORRELATED ||
263                          rsf->rhs()->joinInfo() & JOIN_CORRELATED) &&
264                         lsf->op()->op() == execplan::OP_ISNULL)
265                     {
266                         delete lsf;
267                         sf = rsf;
268                         pt = pt->right();
269                     }
270                     else
271                     {
272                         ParseTree* temp = pt->left();
273                         pt->left(pt->right());
274                         pt->right(temp);
275                         return;
276                     }
277                 }
278                 else
279                 {
280                     return;
281                 }
282 
283                 // set NULLMATCH for both operand. It's really a setting for the join.
284                 // should only set NULLMATCH when the subtype is NOT_IN. for some IN subquery
285                 // with aggregation column, MySQL inefficiently convert to:
286                 // (cache=item or item is null) and item is not null, which is equivalent to
287                 // cache = item. Do not set NULLMATCH for this case.
288                 // Because we don't know IN or NOTIN yet, set candidate bit and switch to NULLMATCH
289                 // later in handleNot function.
290                 if (sf->lhs()->joinInfo() & JOIN_CORRELATED)
291                 {
292                     sf->lhs()->joinInfo(sf->lhs()->joinInfo() | JOIN_NULLMATCH_CANDIDATE);
293                 }
294 
295                 if (sf->rhs()->joinInfo() & JOIN_CORRELATED)
296                 {
297                     sf->rhs()->joinInfo(sf->rhs()->joinInfo() | JOIN_NULLMATCH_CANDIDATE);
298                 }
299 
300                 gwip->ptWorkStack.pop();
301                 gwip->ptWorkStack.push(pt);
302             }
303         }
304         else if (cond->functype() == Item_func::EQ_FUNC)
305         {
306             // not in (select const ...)
307             if (gwip->ptWorkStack.empty())
308                 return;
309 
310             ParseTree* pt = gwip->ptWorkStack.top();
311             SimpleFilter* sf = dynamic_cast<SimpleFilter*>(pt->data());
312 
313             if (!sf || sf->op()->op() != execplan::OP_EQ)
314                 return;
315 
316             if (sf->lhs()->joinInfo() & JOIN_CORRELATED)
317                 sf->lhs()->joinInfo(sf->lhs()->joinInfo() | JOIN_NULLMATCH_CANDIDATE);
318 
319             if (sf->rhs()->joinInfo() & JOIN_CORRELATED)
320                 sf->rhs()->joinInfo(sf->rhs()->joinInfo() | JOIN_NULLMATCH_CANDIDATE);
321         }
322     }
323 }
324 
325 /**
326  * This is invoked when a NOT function is got. It's usually the case NOT<IN optimizer>
327  * This function will simple turn the semi join to anti join
328  *
329  */
handleNot()330 void InSub::handleNot()
331 {
332     ParseTree* pt = fGwip.ptWorkStack.top();
333     ExistsFilter* subFilter = dynamic_cast<ExistsFilter*>(pt->data());
334     idbassert(subFilter);
335     subFilter->notExists(true);
336     SCSEP csep = subFilter->sub();
337     const ParseTree* ptsub = csep->filters();
338 
339     if (ptsub)
340         ptsub->walk(makeAntiJoin);
341 
342     ptsub = csep->having();
343 
344     if (ptsub)
345         ptsub->walk(makeAntiJoin);
346 }
347 
348 }
349