1 /*
2    Copyright (c) 2018, 2019 MariaDB
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */
16 
17 #include "mariadb.h"
18 #include "sql_priv.h"
19 #include "sql_select.h"
20 #include "derived_handler.h"
21 
22 
23 /**
24   The methods of the Pushdown_derived class.
25 
26   The objects of this class are used for pushdown of the derived tables
27   into engines. The main  method of the class is Pushdown_derived::execute()
28   that initiates execution of the query specifying a derived by a foreign
29   engine, receives the rows of the result set and put them in a temporary
30   table on the server side.
31 
32   The method uses only the functions of the derived_handle interface to do
33   this. The constructor of the class gets this interface as a parameter.
34 
35   Currently a derived tables pushed into an engine is always materialized.
36   It could be changed if the cases when the tables is used as driving table.
37 */
38 
39 
Pushdown_derived(TABLE_LIST * tbl,derived_handler * h)40 Pushdown_derived::Pushdown_derived(TABLE_LIST *tbl, derived_handler *h)
41  : derived(tbl), handler(h)
42 {
43   is_analyze= handler->thd->lex->analyze_stmt;
44 }
45 
46 
~Pushdown_derived()47 Pushdown_derived::~Pushdown_derived()
48 {
49   delete handler;
50 }
51 
52 
execute()53 int Pushdown_derived::execute()
54 {
55   int err;
56   THD *thd= handler->thd;
57   TABLE *table= handler->table;
58   TMP_TABLE_PARAM *tmp_table_param= handler->tmp_table_param;
59 
60   DBUG_ENTER("Pushdown_query::execute");
61 
62   if ((err= handler->init_scan()))
63     goto error;
64 
65   if (is_analyze)
66   {
67     handler->end_scan();
68     DBUG_RETURN(0);
69   }
70 
71   while (!(err= handler->next_row()))
72   {
73     if (unlikely(thd->check_killed()))
74     {
75       handler->end_scan();
76       DBUG_RETURN(-1);
77     }
78 
79     if ((err= table->file->ha_write_tmp_row(table->record[0])))
80     {
81       bool is_duplicate;
82       if (likely(!table->file->is_fatal_error(err, HA_CHECK_DUP)))
83         continue;                           // Distinct elimination
84 
85       if (create_internal_tmp_table_from_heap(thd, table,
86                                               tmp_table_param->start_recinfo,
87                                               &tmp_table_param->recinfo,
88                                               err, 1, &is_duplicate))
89         DBUG_RETURN(1);
90       if (is_duplicate)
91         continue;
92     }
93   }
94 
95   if (err != 0 && err != HA_ERR_END_OF_FILE)
96     goto error;
97 
98   if ((err= handler->end_scan()))
99     goto error_2;
100 
101   DBUG_RETURN(0);
102 
103 error:
104   handler->end_scan();
105 error_2:
106   handler->print_error(err, MYF(0));
107   DBUG_RETURN(-1);                              // Error not sent to client
108 }
109 
110 
print_error(int error,myf errflag)111 void derived_handler::print_error(int error, myf errflag)
112 {
113   my_error(ER_GET_ERRNO, MYF(0), error, hton_name(ht)->str);
114 }
115 
116 
set_derived(TABLE_LIST * tbl)117 void derived_handler::set_derived(TABLE_LIST *tbl)
118 {
119   derived= tbl;
120   table= tbl->table;
121   unit= tbl->derived;
122   select= unit->first_select();
123   tmp_table_param= ((select_unit *)(unit->result))->get_tmp_table_param();
124 }
125 
126