summaryrefslogtreecommitdiff
path: root/sql/derived_handler.cc
blob: f48b95cbf7686fedff344139023d8306d81d9667 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
   Copyright (c) 2018, 2019 MariaDB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */

#include "mariadb.h"
#include "sql_priv.h"
#include "sql_select.h"
#include "derived_handler.h"


/**
  The methods of the Pushdown_derived class.

  The objects of this class are used for pushdown of the derived tables
  into engines. The main  method of the class is Pushdown_derived::execute()
  that initiates execution of the query specifying a derived by a foreign
  engine, receives the rows of the result set and put them in a temporary
  table on the server side.

  The method uses only the functions of the derived_handle interface to do
  this. The constructor of the class gets this interface as a parameter.

  Currently a derived tables pushed into an engine is always materialized.
  It could be changed if the cases when the tables is used as driving table.
*/


Pushdown_derived::Pushdown_derived(TABLE_LIST *tbl, derived_handler *h)
 : derived(tbl), handler(h)
{
  is_analyze= handler->thd->lex->analyze_stmt;
}


Pushdown_derived::~Pushdown_derived()
{
  delete handler;
}


int Pushdown_derived::execute()
{
  int err;
  THD *thd= handler->thd;
  TABLE *table= handler->table;
  TMP_TABLE_PARAM *tmp_table_param= handler->tmp_table_param;

  DBUG_ENTER("Pushdown_query::execute");

  if ((err= handler->init_scan()))
    goto error;

  if (is_analyze)
  {
    handler->end_scan();
    DBUG_RETURN(0);
  }

  while (!(err= handler->next_row()))
  {
    if (unlikely(thd->check_killed()))
    {
      handler->end_scan();
      DBUG_RETURN(-1);
    }

    if ((err= table->file->ha_write_tmp_row(table->record[0])))
    {
      bool is_duplicate;
      if (likely(!table->file->is_fatal_error(err, HA_CHECK_DUP)))
        continue;                           // Distinct elimination

      if (create_internal_tmp_table_from_heap(thd, table,
                                              tmp_table_param->start_recinfo,
                                              &tmp_table_param->recinfo,
                                              err, 1, &is_duplicate))
        DBUG_RETURN(1);
      if (is_duplicate)
        continue;
    }
  }

  if (err != 0 && err != HA_ERR_END_OF_FILE)
    goto error;

  if ((err= handler->end_scan()))
    goto error_2;

  DBUG_RETURN(0);

error:
  handler->end_scan();
error_2:
  handler->print_error(err, MYF(0));
  DBUG_RETURN(-1);                              // Error not sent to client
}


void derived_handler::print_error(int error, myf errflag)
{
  my_error(ER_GET_ERRNO, MYF(0), error, hton_name(ht)->str);
}


void derived_handler::set_derived(TABLE_LIST *tbl)
{
  derived= tbl;
  table= tbl->table;
  unit= tbl->derived;
  select= unit->first_select();
  tmp_table_param= ((select_unit *)(unit->result))->get_tmp_table_param();
}