summaryrefslogtreecommitdiffstats
path: root/storage/federatedx/federatedx_pushdown.cc
blob: e9a9791a859b65f2a50d59bf42622ee4a740c97b (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
   Copyright (c) 2019, 2020, 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 St, Fifth Floor, Boston, MA 02110-1301  USA */

/* !!! For inclusion into ha_federatedx.cc */


/*
  This is a quick a dirty implemention of the derived_handler and select_handler
  interfaces to be used to push select queries and the queries specifying
  derived tables into FEDERATEDX engine.
  The functions
    create_federatedx_derived_handler and
    create_federatedx_select_handler
  that return the corresponding interfaces for pushdown capabilities do
  not check a lot of things. In particular they do not check that the tables
  of the pushed queries belong to the same foreign server.

  The implementation is provided purely for testing purposes.
  The pushdown capabilities are enabled by turning on the plugin system
  variable federated_pushdown:
    set global federated_pushdown=1;
*/


/*
  Check if table and database names are equal on local and remote servers

  SYNOPSIS
    local_and_remote_names_match()
    tbl_share      Pointer to current table TABLE_SHARE structure
    fshare         Pointer to current table FEDERATEDX_SHARE structure

  DESCRIPTION
    FederatedX table on the local server may refer to a table having another
    name on the remote server. The remote table may even reside in a different
    database. For example:

    -- Remote server
    CREATE TABLE t1 (id int(32));

    -- Local server
    CREATE TABLE t2 ENGINE="FEDERATEDX"
    CONNECTION="mysql://joe:joespass@192.168.1.111:9308/federatedx/t1";

    It's not a problem while the federated_pushdown is disabled 'cause
    the CONNECTION strings are being parsed for every table during
    the execution, so the table names are translated from local to remote.
    But in case of the federated_pushdown the whole query is pushed down
    to the engine without any translation, so the remote server may try
    to select data from a nonexistent table (for example, query
    "SELECT * FROM t2" will try to retrieve data from nonexistent "t2").

    This function checks whether there is a mismatch between local and remote
    table/database names

  RETURN VALUE
    false           names are equal
    true            names are not equal

*/
bool local_and_remote_names_mismatch(const TABLE_SHARE *tbl_share,
                                     const FEDERATEDX_SHARE *fshare)
{

  if (lower_case_table_names)
  {
    if (strcasecmp(fshare->database, tbl_share->db.str) != 0)
      return true;
  }
  else
  {
    if (strncmp(fshare->database, tbl_share->db.str, tbl_share->db.length) != 0)
      return true;
  }

  return my_strnncoll(system_charset_info, (uchar *) fshare->table_name,
                      strlen(fshare->table_name),
                      (uchar *) tbl_share->table_name.str,
                      tbl_share->table_name.length) != 0;
}


static derived_handler*
create_federatedx_derived_handler(THD* thd, TABLE_LIST *derived)
{
  if (!use_pushdown)
    return 0;

  ha_federatedx_derived_handler* handler = NULL;

  SELECT_LEX_UNIT *unit= derived->derived;

  for (SELECT_LEX *sl= unit->first_select(); sl; sl= sl->next_select())
  {
    if (!(sl->join))
      return 0;
    for (TABLE_LIST *tbl= sl->join->tables_list; tbl; tbl= tbl->next_local)
    {
      if (!tbl->table)
	return 0;
      /*
        We intentionally don't support partitioned federatedx tables here, so
        use file->ht and not file->partition_ht().
      */
      if (tbl->table->file->ht != federatedx_hton)
        return 0;

      const FEDERATEDX_SHARE *fshare=
        ((ha_federatedx*)tbl->table->file)->get_federatedx_share();
      if (local_and_remote_names_mismatch(tbl->table->s, fshare))
        return 0;
    }
  }

  handler= new ha_federatedx_derived_handler(thd, derived);

  return handler;
}


/*
  Implementation class of the derived_handler interface for FEDERATEDX:
  class implementation
*/

ha_federatedx_derived_handler::ha_federatedx_derived_handler(THD *thd,
                                                             TABLE_LIST *dt)
  : derived_handler(thd, federatedx_hton),
    share(NULL), txn(NULL), iop(NULL), stored_result(NULL)
{
  derived= dt;
}

ha_federatedx_derived_handler::~ha_federatedx_derived_handler() = default;

int ha_federatedx_derived_handler::init_scan()
{
  THD *thd;
  int rc= 0;

  DBUG_ENTER("ha_federatedx_derived_handler::init_scan");

  TABLE *table= derived->get_first_table()->table;
  ha_federatedx *h= (ha_federatedx *) table->file;
  iop= &h->io;
  share= get_share(table->s->table_name.str, table);
  thd= table->in_use;
  txn= h->get_txn(thd);
  if ((rc= txn->acquire(share, thd, TRUE, iop)))
    DBUG_RETURN(rc);

  if ((*iop)->query(derived->derived_spec.str, derived->derived_spec.length))
    goto err;

  stored_result= (*iop)->store_result();
  if (!stored_result)
      goto err;

  DBUG_RETURN(0);

err:
  DBUG_RETURN(HA_FEDERATEDX_ERROR_WITH_REMOTE_SYSTEM);
}

int ha_federatedx_derived_handler::next_row()
{
  int rc;
  FEDERATEDX_IO_ROW *row;
  ulong *lengths;
  Field **field;
  int column= 0;
  Time_zone *saved_time_zone= table->in_use->variables.time_zone;
  DBUG_ENTER("ha_federatedx_derived_handler::next_row");

  if ((rc= txn->acquire(share, table->in_use, TRUE, iop)))
    DBUG_RETURN(rc);

  if (!(row= (*iop)->fetch_row(stored_result)))
    DBUG_RETURN(HA_ERR_END_OF_FILE);

  /* Convert row to internal format */
  table->in_use->variables.time_zone= UTC;
  lengths= (*iop)->fetch_lengths(stored_result);

  for (field= table->field; *field; field++, column++)
  {
    if ((*iop)->is_column_null(row, column))
       (*field)->set_null();
    else
    {
      (*field)->set_notnull();
      (*field)->store((*iop)->get_column_data(row, column),
                      lengths[column], &my_charset_bin);
    }
  }
  table->in_use->variables.time_zone= saved_time_zone;

  DBUG_RETURN(rc);
}

int ha_federatedx_derived_handler::end_scan()
{
  DBUG_ENTER("ha_federatedx_derived_handler::end_scan");

  (*iop)->free_result(stored_result);

  free_share(txn, share);

  DBUG_RETURN(0);
}

void ha_federatedx_derived_handler::print_error(int, unsigned long)
{
}


static select_handler*
create_federatedx_select_handler(THD* thd, SELECT_LEX *sel)
{
  if (!use_pushdown)
    return 0;

  ha_federatedx_select_handler* handler = NULL;

  for (TABLE_LIST *tbl= thd->lex->query_tables; tbl; tbl= tbl->next_global)
  {
    if (!tbl->table)
      return 0;
    /*
      We intentionally don't support partitioned federatedx tables here, so
      use file->ht and not file->partition_ht().
    */
    if (tbl->table->file->ht != federatedx_hton)
     return 0;

    const FEDERATEDX_SHARE *fshare=
      ((ha_federatedx*)tbl->table->file)->get_federatedx_share();

    if (local_and_remote_names_mismatch(tbl->table->s, fshare))
      return 0;
  }

  /*
    Currently, ha_federatedx_select_handler::init_scan just takes the
    thd->query and sends it to the backend.
    This obviously won't work if the SELECT uses an "INTO @var" or
    "INTO OUTFILE". It is also unlikely to work if the select has some
    other kind of side effect.
  */
  if (sel->uncacheable & UNCACHEABLE_SIDEEFFECT)
    return NULL;

  handler= new ha_federatedx_select_handler(thd, sel);

  return handler;
}

/*
  Implementation class of the select_handler interface for FEDERATEDX:
  class implementation
*/

ha_federatedx_select_handler::ha_federatedx_select_handler(THD *thd,
                                                           SELECT_LEX *sel)
  : select_handler(thd, federatedx_hton),
    share(NULL), txn(NULL), iop(NULL), stored_result(NULL)
{
  select= sel;
}

ha_federatedx_select_handler::~ha_federatedx_select_handler() = default;

int ha_federatedx_select_handler::init_scan()
{
  int rc= 0;

  DBUG_ENTER("ha_federatedx_select_handler::init_scan");

  TABLE *table= 0;
  for (TABLE_LIST *tbl= thd->lex->query_tables; tbl; tbl= tbl->next_global)
  {
    if (!tbl->table)
      continue;
    table= tbl->table;
    break;
  }
  ha_federatedx *h= (ha_federatedx *) table->file;
  iop= &h->io;
  share= get_share(table->s->table_name.str, table);
  txn= h->get_txn(thd);
  if ((rc= txn->acquire(share, thd, TRUE, iop)))
    DBUG_RETURN(rc);

  if ((*iop)->query(thd->query(), thd->query_length()))
    goto err;

  stored_result= (*iop)->store_result();
  if (!stored_result)
      goto err;

  DBUG_RETURN(0);

err:
  DBUG_RETURN(HA_FEDERATEDX_ERROR_WITH_REMOTE_SYSTEM);
}

int ha_federatedx_select_handler::next_row()
{
  int rc= 0;
  FEDERATEDX_IO_ROW *row;
  ulong *lengths;
  Field **field;
  int column= 0;
  Time_zone *saved_time_zone= table->in_use->variables.time_zone;
  DBUG_ENTER("ha_federatedx_select_handler::next_row");

  if ((rc= txn->acquire(share, table->in_use, TRUE, iop)))
    DBUG_RETURN(rc);

  if (!(row= (*iop)->fetch_row(stored_result)))
    DBUG_RETURN(HA_ERR_END_OF_FILE);

  /* Convert row to internal format */
  table->in_use->variables.time_zone= UTC;
  lengths= (*iop)->fetch_lengths(stored_result);

  for (field= table->field; *field; field++, column++)
  {
    if ((*iop)->is_column_null(row, column))
       (*field)->set_null();
    else
    {
      (*field)->set_notnull();
      (*field)->store((*iop)->get_column_data(row, column),
                      lengths[column], &my_charset_bin);
    }
  }
  table->in_use->variables.time_zone= saved_time_zone;

  DBUG_RETURN(rc);
}

int ha_federatedx_select_handler::end_scan()
{
  DBUG_ENTER("ha_federatedx_derived_handler::end_scan");

  free_tmp_table(thd, table);
  table= 0;

  (*iop)->free_result(stored_result);

  free_share(txn, share);

  DBUG_RETURN(0);
}

void ha_federatedx_select_handler::print_error(int error, myf error_flag)
{
  select_handler::print_error(error, error_flag);
}