Commit cbb67428 authored by Aleksey Midenkov's avatar Aleksey Midenkov

SQL: moved VTQ items to separate file

parent afe64a45
......@@ -143,6 +143,7 @@ SET (SQL_SOURCE
rpl_gtid.cc rpl_parallel.cc
sql_type.cc sql_type.h
item_windowfunc.cc sql_window.cc
item_vers.cc
sql_cte.cc sql_cte.h
sql_sequence.cc sql_sequence.h ha_sequence.h
${WSREP_SOURCES}
......
......@@ -4937,6 +4937,7 @@ class Item_int_with_ref :public Item_int
#include "item_xmlfunc.h"
#include "item_jsonfunc.h"
#include "item_create.h"
#include "item_vers.h"
#endif
/**
......
......@@ -3273,260 +3273,3 @@ bool Item_func_last_day::get_date(MYSQL_TIME *ltime, ulonglong fuzzy_date)
ltime->time_type= MYSQL_TIMESTAMP_DATE;
return (null_value= 0);
}
Item_func_vtq_ts::Item_func_vtq_ts(
THD *thd,
Item* a,
vtq_field_t _vtq_field,
handlerton* hton) :
VTQ_common<Item_datetimefunc>(thd, a, hton),
vtq_field(_vtq_field)
{
decimals= 6;
null_value= true;
DBUG_ASSERT(arg_count == 1 && args[0]);
}
Item_func_vtq_ts::Item_func_vtq_ts(
THD *thd,
Item* a,
vtq_field_t _vtq_field) :
VTQ_common<Item_datetimefunc>(thd, a),
vtq_field(_vtq_field)
{
decimals= 6;
null_value= true;
DBUG_ASSERT(arg_count == 1 && args[0]);
}
template <class Item_func_X>
void
VTQ_common<Item_func_X>::init_hton()
{
if (!hton)
{
if (Item_func_X::args[0]->type() == Item::FIELD_ITEM)
{
Item_field *f=
static_cast<Item_field *>(Item_func_X::args[0]);
DBUG_ASSERT(
f->field &&
f->field->table &&
f->field->table->s &&
f->field->table->s->db_plugin);
hton= f->field->table->file->partition_ht();
DBUG_ASSERT(hton);
}
else if (innodb_plugin)
{
hton= plugin_hton(plugin_int_to_ref(innodb_plugin));
DBUG_ASSERT(hton);
}
if (hton && !(hton->flags & HTON_SUPPORTS_SYS_VERSIONING))
{
my_error(ER_VERS_ENGINE_UNSUPPORTED, MYF(0), Item::name ? Item::name : this->func_name());
hton= NULL;
}
}
}
bool
Item_func_vtq_ts::get_date(MYSQL_TIME *res, ulonglong fuzzy_date)
{
THD *thd= current_thd; // can it differ from constructor's?
DBUG_ASSERT(thd);
ulonglong trx_id= args[0]->val_uint();
if (trx_id == ULONGLONG_MAX)
{
null_value= false;
thd->variables.time_zone->gmt_sec_to_TIME(res, TIMESTAMP_MAX_VALUE);
return false;
}
init_hton();
if (!hton)
return true;
DBUG_ASSERT(hton->vers_query_trx_id);
null_value= !hton->vers_query_trx_id(thd, res, trx_id, vtq_field);
return false;
}
Item_func_vtq_id::Item_func_vtq_id(
THD *thd,
Item* a,
vtq_field_t _vtq_field,
bool _backwards) :
VTQ_common<Item_int_func>(thd, a),
vtq_field(_vtq_field),
backwards(_backwards)
{
memset(&cached_result, 0, sizeof(cached_result));
decimals= 0;
unsigned_flag= 1;
null_value= true;
DBUG_ASSERT(arg_count == 1 && args[0]);
}
Item_func_vtq_id::Item_func_vtq_id(
THD *thd,
Item* a,
Item* b,
vtq_field_t _vtq_field) :
VTQ_common<Item_int_func>(thd, a, b),
vtq_field(_vtq_field),
backwards(false)
{
memset(&cached_result, 0, sizeof(cached_result));
decimals= 0;
unsigned_flag= 1;
null_value= true;
DBUG_ASSERT(arg_count == 2 && args[0] && args[1]);
}
longlong
Item_func_vtq_id::get_by_trx_id(ulonglong trx_id)
{
ulonglong res;
THD *thd= current_thd; // can it differ from constructor's?
DBUG_ASSERT(thd);
if (trx_id == ULONGLONG_MAX)
{
null_value= true;
return 0;
}
DBUG_ASSERT(hton->vers_query_trx_id);
null_value= !hton->vers_query_trx_id(thd, &res, trx_id, vtq_field);
return res;
}
longlong
Item_func_vtq_id::get_by_commit_ts(MYSQL_TIME &commit_ts, bool backwards)
{
THD *thd= current_thd; // can it differ from constructor's?
DBUG_ASSERT(thd);
DBUG_ASSERT(hton->vers_query_commit_ts);
null_value= !hton->vers_query_commit_ts(thd, &cached_result, commit_ts, VTQ_ALL, backwards);
if (null_value)
{
return 0;
}
switch (vtq_field)
{
case VTQ_COMMIT_ID:
return cached_result.commit_id;
case VTQ_ISO_LEVEL:
return cached_result.iso_level;
case VTQ_TRX_ID:
return cached_result.trx_id;
default:
DBUG_ASSERT(0);
null_value= true;
}
return 0;
}
longlong
Item_func_vtq_id::val_int()
{
init_hton();
if (!hton)
{
null_value= true;
return 0;
}
if (args[0]->is_null())
{
if (arg_count < 2 || vtq_field == VTQ_TRX_ID)
{
null_value= true;
return 0;
}
return get_by_trx_id(args[1]->val_uint());
}
else
{
MYSQL_TIME commit_ts;
if (args[0]->get_date(&commit_ts, 0))
{
null_value= true;
return 0;
}
if (arg_count > 1)
{
backwards= args[1]->val_bool();
DBUG_ASSERT(arg_count == 2);
}
return get_by_commit_ts(commit_ts, backwards);
}
}
Item_func_vtq_trx_sees::Item_func_vtq_trx_sees(
THD *thd,
Item* a,
Item* b) :
VTQ_common<Item_bool_func>(thd, a, b),
accept_eq(false)
{
null_value= true;
DBUG_ASSERT(arg_count == 2 && args[0] && args[1]);
}
longlong
Item_func_vtq_trx_sees::val_int()
{
THD *thd= current_thd;
DBUG_ASSERT(thd);
init_hton();
if (!hton)
{
null_value= true;
return 0;
}
ulonglong trx_id1, trx_id0;
ulonglong commit_id1= 0;
ulonglong commit_id0= 0;
uchar iso_level1= 0;
DBUG_ASSERT(arg_count > 1);
trx_id1= args[0]->val_uint();
trx_id0= args[1]->val_uint();
vtq_record_t *cached= args[0]->vtq_cached_result();
if (cached && cached->commit_id)
{
commit_id1= cached->commit_id;
iso_level1= cached->iso_level;
}
cached= args[1]->vtq_cached_result();
if (cached && cached->commit_id)
{
commit_id0= cached->commit_id;
}
if (accept_eq && trx_id1 && trx_id1 == trx_id0)
{
null_value= false;
return true;
}
DBUG_ASSERT(hton->vers_trx_sees);
bool result= false;
null_value= !hton->vers_trx_sees(thd, result, trx_id1, trx_id0, commit_id1, iso_level1, commit_id0);
return result;
}
......@@ -1286,119 +1286,4 @@ class Item_func_last_day :public Item_datefunc
{ return get_item_copy<Item_func_last_day>(thd, mem_root, this); }
};
#include "vtq.h"
template <class Item_func_X>
class VTQ_common : public Item_func_X
{
protected:
handlerton *hton;
void init_hton();
public:
VTQ_common(THD *thd, Item* a) :
Item_func_X(thd, a),
hton(NULL) {}
VTQ_common(THD *thd, Item* a, Item* b) :
Item_func_X(thd, a, b),
hton(NULL) {}
VTQ_common(THD *thd, Item* a, handlerton* _hton) :
Item_func_X(thd, a),
hton(_hton) {}
};
class Item_func_vtq_ts :
public VTQ_common<Item_datetimefunc>
{
vtq_field_t vtq_field;
public:
Item_func_vtq_ts(THD *thd, Item* a, vtq_field_t _vtq_field, handlerton *hton);
Item_func_vtq_ts(THD *thd, Item* a, vtq_field_t _vtq_field);
const char *func_name() const
{
if (vtq_field == VTQ_BEGIN_TS)
{
return "vtq_begin_ts";
}
return "vtq_commit_ts";
}
bool get_date(MYSQL_TIME *res, ulonglong fuzzy_date);
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_func_vtq_ts>(thd, mem_root, this); }
};
class Item_func_vtq_id :
public VTQ_common<Item_int_func>
{
vtq_field_t vtq_field;
vtq_record_t cached_result;
bool backwards;
longlong get_by_trx_id(ulonglong trx_id);
longlong get_by_commit_ts(MYSQL_TIME &commit_ts, bool backwards);
public:
Item_func_vtq_id(THD *thd, Item* a, vtq_field_t _vtq_field, bool _backwards= false);
Item_func_vtq_id(THD *thd, Item* a, Item* b, vtq_field_t _vtq_field);
vtq_record_t *vtq_cached_result() { return &cached_result; }
const char *func_name() const
{
switch (vtq_field)
{
case VTQ_TRX_ID:
return "vtq_trx_id";
case VTQ_COMMIT_ID:
return "vtq_commit_id";
case VTQ_ISO_LEVEL:
return "vtq_iso_level";
default:
DBUG_ASSERT(0);
}
return NULL;
}
void fix_length_and_dec()
{
Item_int_func::fix_length_and_dec();
max_length= 20;
}
longlong val_int();
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_func_vtq_id>(thd, mem_root, this); }
};
class Item_func_vtq_trx_sees :
public VTQ_common<Item_bool_func>
{
protected:
bool accept_eq;
public:
Item_func_vtq_trx_sees(THD *thd, Item* a, Item* b);
const char *func_name() const
{
return "vtq_trx_sees";
}
longlong val_int();
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_func_vtq_trx_sees>(thd, mem_root, this); }
};
class Item_func_vtq_trx_sees_eq :
public Item_func_vtq_trx_sees
{
public:
Item_func_vtq_trx_sees_eq(THD *thd, Item* a, Item* b) :
Item_func_vtq_trx_sees(thd, a, b)
{
accept_eq= true;
}
const char *func_name() const
{
return "vtq_trx_sees_eq";
}
};
#endif /* ITEM_TIMEFUNC_INCLUDED */
/* Copyright (c) 2017, MariaDB Corporation.
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 02110-1301, USA */
/**
@brief
System Versioning items
*/
#include "item.h"
#include "sql_class.h"
#include "tztime.h"
Item_func_vtq_ts::Item_func_vtq_ts(
THD *thd,
Item* a,
vtq_field_t _vtq_field,
handlerton* hton) :
VTQ_common<Item_datetimefunc>(thd, a, hton),
vtq_field(_vtq_field)
{
decimals= 6;
null_value= true;
DBUG_ASSERT(arg_count == 1 && args[0]);
}
Item_func_vtq_ts::Item_func_vtq_ts(
THD *thd,
Item* a,
vtq_field_t _vtq_field) :
VTQ_common<Item_datetimefunc>(thd, a),
vtq_field(_vtq_field)
{
decimals= 6;
null_value= true;
DBUG_ASSERT(arg_count == 1 && args[0]);
}
template <class Item_func_X>
void
VTQ_common<Item_func_X>::init_hton()
{
if (!hton)
{
if (Item_func_X::args[0]->type() == Item::FIELD_ITEM)
{
Item_field *f=
static_cast<Item_field *>(Item_func_X::args[0]);
DBUG_ASSERT(
f->field &&
f->field->table &&
f->field->table->s &&
f->field->table->s->db_plugin);
hton= f->field->table->file->partition_ht();
DBUG_ASSERT(hton);
}
else if (innodb_plugin)
{
hton= plugin_hton(plugin_int_to_ref(innodb_plugin));
DBUG_ASSERT(hton);
}
if (hton && !(hton->flags & HTON_SUPPORTS_SYS_VERSIONING))
{
my_error(ER_VERS_ENGINE_UNSUPPORTED, MYF(0), Item::name ? Item::name : this->func_name());
hton= NULL;
}
}
}
bool
Item_func_vtq_ts::get_date(MYSQL_TIME *res, ulonglong fuzzy_date)
{
THD *thd= current_thd; // can it differ from constructor's?
DBUG_ASSERT(thd);
ulonglong trx_id= args[0]->val_uint();
if (trx_id == ULONGLONG_MAX)
{
null_value= false;
thd->variables.time_zone->gmt_sec_to_TIME(res, TIMESTAMP_MAX_VALUE);
return false;
}
init_hton();
if (!hton)
return true;
DBUG_ASSERT(hton->vers_query_trx_id);
null_value= !hton->vers_query_trx_id(thd, res, trx_id, vtq_field);
return false;
}
Item_func_vtq_id::Item_func_vtq_id(
THD *thd,
Item* a,
vtq_field_t _vtq_field,
bool _backwards) :
VTQ_common<Item_int_func>(thd, a),
vtq_field(_vtq_field),
backwards(_backwards)
{
memset(&cached_result, 0, sizeof(cached_result));
decimals= 0;
unsigned_flag= 1;
null_value= true;
DBUG_ASSERT(arg_count == 1 && args[0]);
}
Item_func_vtq_id::Item_func_vtq_id(
THD *thd,
Item* a,
Item* b,
vtq_field_t _vtq_field) :
VTQ_common<Item_int_func>(thd, a, b),
vtq_field(_vtq_field),
backwards(false)
{
memset(&cached_result, 0, sizeof(cached_result));
decimals= 0;
unsigned_flag= 1;
null_value= true;
DBUG_ASSERT(arg_count == 2 && args[0] && args[1]);
}
longlong
Item_func_vtq_id::get_by_trx_id(ulonglong trx_id)
{
ulonglong res;
THD *thd= current_thd; // can it differ from constructor's?
DBUG_ASSERT(thd);
if (trx_id == ULONGLONG_MAX)
{
null_value= true;
return 0;
}
DBUG_ASSERT(hton->vers_query_trx_id);
null_value= !hton->vers_query_trx_id(thd, &res, trx_id, vtq_field);
return res;
}
longlong
Item_func_vtq_id::get_by_commit_ts(MYSQL_TIME &commit_ts, bool backwards)
{
THD *thd= current_thd; // can it differ from constructor's?
DBUG_ASSERT(thd);
DBUG_ASSERT(hton->vers_query_commit_ts);
null_value= !hton->vers_query_commit_ts(thd, &cached_result, commit_ts, VTQ_ALL, backwards);
if (null_value)
{
return 0;
}
switch (vtq_field)
{
case VTQ_COMMIT_ID:
return cached_result.commit_id;
case VTQ_ISO_LEVEL:
return cached_result.iso_level;
case VTQ_TRX_ID:
return cached_result.trx_id;
default:
DBUG_ASSERT(0);
null_value= true;
}
return 0;
}
longlong
Item_func_vtq_id::val_int()
{
init_hton();
if (!hton)
{
null_value= true;
return 0;
}
if (args[0]->is_null())
{
if (arg_count < 2 || vtq_field == VTQ_TRX_ID)
{
null_value= true;
return 0;
}
return get_by_trx_id(args[1]->val_uint());
}
else
{
MYSQL_TIME commit_ts;
if (args[0]->get_date(&commit_ts, 0))
{
null_value= true;
return 0;
}
if (arg_count > 1)
{
backwards= args[1]->val_bool();
DBUG_ASSERT(arg_count == 2);
}
return get_by_commit_ts(commit_ts, backwards);
}
}
Item_func_vtq_trx_sees::Item_func_vtq_trx_sees(
THD *thd,
Item* a,
Item* b) :
VTQ_common<Item_bool_func>(thd, a, b),
accept_eq(false)
{
null_value= true;
DBUG_ASSERT(arg_count == 2 && args[0] && args[1]);
}
longlong
Item_func_vtq_trx_sees::val_int()
{
THD *thd= current_thd;
DBUG_ASSERT(thd);
init_hton();
if (!hton)
{
null_value= true;
return 0;
}
ulonglong trx_id1, trx_id0;
ulonglong commit_id1= 0;
ulonglong commit_id0= 0;
uchar iso_level1= 0;
DBUG_ASSERT(arg_count > 1);
trx_id1= args[0]->val_uint();
trx_id0= args[1]->val_uint();
vtq_record_t *cached= args[0]->vtq_cached_result();
if (cached && cached->commit_id)
{
commit_id1= cached->commit_id;
iso_level1= cached->iso_level;
}
cached= args[1]->vtq_cached_result();
if (cached && cached->commit_id)
{
commit_id0= cached->commit_id;
}
if (accept_eq && trx_id1 && trx_id1 == trx_id0)
{
null_value= false;
return true;
}
DBUG_ASSERT(hton->vers_trx_sees);
bool result= false;
null_value= !hton->vers_trx_sees(thd, result, trx_id1, trx_id0, commit_id1, iso_level1, commit_id0);
return result;
}
#ifndef ITEM_VERS_INCLUDED
#define ITEM_VERS_INCLUDED
/* Copyright (c) 2017, MariaDB Corporation.
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 02110-1301, USA */
/* System Versioning items */
#include "vtq.h"
#ifdef USE_PRAGMA_INTERFACE
#pragma interface /* gcc class implementation */
#endif
template <class Item_func_X>
class VTQ_common : public Item_func_X
{
protected:
handlerton *hton;
void init_hton();
public:
VTQ_common(THD *thd, Item* a) :
Item_func_X(thd, a),
hton(NULL) {}
VTQ_common(THD *thd, Item* a, Item* b) :
Item_func_X(thd, a, b),
hton(NULL) {}
VTQ_common(THD *thd, Item* a, handlerton* _hton) :
Item_func_X(thd, a),
hton(_hton) {}
};
class Item_func_vtq_ts :
public VTQ_common<Item_datetimefunc>
{
vtq_field_t vtq_field;
public:
Item_func_vtq_ts(THD *thd, Item* a, vtq_field_t _vtq_field, handlerton *hton);
Item_func_vtq_ts(THD *thd, Item* a, vtq_field_t _vtq_field);
const char *func_name() const
{
if (vtq_field == VTQ_BEGIN_TS)
{
return "vtq_begin_ts";
}
return "vtq_commit_ts";
}
bool get_date(MYSQL_TIME *res, ulonglong fuzzy_date);
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_func_vtq_ts>(thd, mem_root, this); }
};
class Item_func_vtq_id :
public VTQ_common<Item_int_func>
{
vtq_field_t vtq_field;
vtq_record_t cached_result;
bool backwards;
longlong get_by_trx_id(ulonglong trx_id);
longlong get_by_commit_ts(MYSQL_TIME &commit_ts, bool backwards);
public:
Item_func_vtq_id(THD *thd, Item* a, vtq_field_t _vtq_field, bool _backwards= false);
Item_func_vtq_id(THD *thd, Item* a, Item* b, vtq_field_t _vtq_field);
vtq_record_t *vtq_cached_result() { return &cached_result; }
const char *func_name() const
{
switch (vtq_field)
{
case VTQ_TRX_ID:
return "vtq_trx_id";
case VTQ_COMMIT_ID:
return "vtq_commit_id";
case VTQ_ISO_LEVEL:
return "vtq_iso_level";
default:
DBUG_ASSERT(0);
}
return NULL;
}
void fix_length_and_dec()
{
Item_int_func::fix_length_and_dec();
max_length= 20;
}
longlong val_int();
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_func_vtq_id>(thd, mem_root, this); }
};
class Item_func_vtq_trx_sees :
public VTQ_common<Item_bool_func>
{
protected:
bool accept_eq;
public:
Item_func_vtq_trx_sees(THD *thd, Item* a, Item* b);
const char *func_name() const
{
return "vtq_trx_sees";
}
longlong val_int();
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_func_vtq_trx_sees>(thd, mem_root, this); }
};
class Item_func_vtq_trx_sees_eq :
public Item_func_vtq_trx_sees
{
public:
Item_func_vtq_trx_sees_eq(THD *thd, Item* a, Item* b) :
Item_func_vtq_trx_sees(thd, a, b)
{
accept_eq= true;
}
const char *func_name() const
{
return "vtq_trx_sees_eq";
}
};
#endif /* ITEM_VERS_INCLUDED */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment