summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
authorAlexander Barkov <bar@mariadb.com>2018-07-31 16:58:02 +0400
committerAlexander Barkov <bar@mariadb.com>2018-07-31 16:58:02 +0400
commitb1ae4e7e154b9b1ffb87918a8646173e1f8063fe (patch)
treeb40a3a79675dd7dab40c40b800f37a2be0099954 /sql
parent2bbee0e1ecc1d71cc7ca890d70e101a69955e5d9 (diff)
downloadmariadb-git-b1ae4e7e154b9b1ffb87918a8646173e1f8063fe.tar.gz
MDEV-16864 Implement class Item_func_timestamp
Diffstat (limited to 'sql')
-rw-r--r--sql/item_create.cc4
-rw-r--r--sql/item_timefunc.cc24
-rw-r--r--sql/item_timefunc.h46
-rw-r--r--sql/sql_yacc.yy2
-rw-r--r--sql/sql_yacc_ora.yy2
5 files changed, 44 insertions, 34 deletions
diff --git a/sql/item_create.cc b/sql/item_create.cc
index 80cb830c584..d9b007d4728 100644
--- a/sql/item_create.cc
+++ b/sql/item_create.cc
@@ -3636,7 +3636,7 @@ Create_func_addtime Create_func_addtime::s_singleton;
Item*
Create_func_addtime::create_2_arg(THD *thd, Item *arg1, Item *arg2)
{
- return new (thd->mem_root) Item_func_add_time(thd, arg1, arg2, 0, 0);
+ return new (thd->mem_root) Item_func_add_time(thd, arg1, arg2, false);
}
@@ -6657,7 +6657,7 @@ Create_func_subtime Create_func_subtime::s_singleton;
Item*
Create_func_subtime::create_2_arg(THD *thd, Item *arg1, Item *arg2)
{
- return new (thd->mem_root) Item_func_add_time(thd, arg1, arg2, 0, 1);
+ return new (thd->mem_root) Item_func_add_time(thd, arg1, arg2, true);
}
diff --git a/sql/item_timefunc.cc b/sql/item_timefunc.cc
index f040e8a4a41..789297f0c7f 100644
--- a/sql/item_timefunc.cc
+++ b/sql/item_timefunc.cc
@@ -2698,8 +2698,7 @@ bool Item_func_add_time::fix_length_and_dec()
arg0_field_type= args[0]->field_type();
if (arg0_field_type == MYSQL_TYPE_DATE ||
arg0_field_type == MYSQL_TYPE_DATETIME ||
- arg0_field_type == MYSQL_TYPE_TIMESTAMP ||
- is_date)
+ arg0_field_type == MYSQL_TYPE_TIMESTAMP)
{
uint dec= MY_MAX(args[0]->datetime_precision(), args[1]->time_precision());
set_handler(&type_handler_datetime2);
@@ -2767,27 +2766,6 @@ bool Item_func_add_time::get_date(MYSQL_TIME *ltime, ulonglong fuzzy_date)
}
-void Item_func_add_time::print(String *str, enum_query_type query_type)
-{
- if (is_date)
- {
- DBUG_ASSERT(sign > 0);
- str->append(STRING_WITH_LEN("timestamp("));
- }
- else
- {
- if (sign > 0)
- str->append(STRING_WITH_LEN("addtime("));
- else
- str->append(STRING_WITH_LEN("subtime("));
- }
- args[0]->print(str, query_type);
- str->append(',');
- args[1]->print(str, query_type);
- str->append(')');
-}
-
-
/**
TIMEDIFF(t,s) is a time function that calculates the
time value between a start and end time.
diff --git a/sql/item_timefunc.h b/sql/item_timefunc.h
index 58392dacceb..a7bdbc8a76b 100644
--- a/sql/item_timefunc.h
+++ b/sql/item_timefunc.h
@@ -667,6 +667,8 @@ class Item_datetimefunc :public Item_temporal_func
public:
Item_datetimefunc(THD *thd): Item_temporal_func(thd) {}
Item_datetimefunc(THD *thd, Item *a): Item_temporal_func(thd, a) {}
+ Item_datetimefunc(THD *thd, Item *a, Item *b):
+ Item_temporal_func(thd, a, b) {}
Item_datetimefunc(THD *thd, Item *a, Item *b, Item *c):
Item_temporal_func(thd, a, b ,c) {}
const Type_handler *type_handler() const { return &type_handler_datetime2; }
@@ -1229,19 +1231,49 @@ public:
};
+class Item_func_timestamp :public Item_datetimefunc
+{
+ bool check_arguments() const
+ {
+ return args[0]->check_type_can_return_date(func_name()) ||
+ args[1]->check_type_can_return_time(func_name());
+ }
+public:
+ Item_func_timestamp(THD *thd, Item *a, Item *b)
+ :Item_datetimefunc(thd, a, b)
+ { }
+ const char *func_name() const { return "timestamp"; }
+ bool fix_length_and_dec()
+ {
+ uint dec= MY_MAX(args[0]->datetime_precision(), args[1]->time_precision());
+ fix_attributes_datetime(dec);
+ maybe_null= true;
+ return false;
+ }
+ bool get_date(MYSQL_TIME *ltime, ulonglong fuzzy_date)
+ {
+ Datetime dt(current_thd, args[0], 0);
+ MYSQL_TIME ltime2;
+ return (null_value= (!dt.is_valid_datetime() ||
+ args[1]->get_time(&ltime2) ||
+ Sec6_add(dt.get_mysql_time(), &ltime2, 1).
+ to_datetime(ltime)));
+ }
+ Item *get_copy(THD *thd)
+ { return get_item_copy<Item_func_timestamp>(thd, this); }
+};
+
+
class Item_func_add_time :public Item_temporal_hybrid_func
{
- const bool is_date;
int sign;
-
public:
- Item_func_add_time(THD *thd, Item *a, Item *b, bool type_arg, bool neg_arg):
- Item_temporal_hybrid_func(thd, a, b), is_date(type_arg)
- { sign= neg_arg ? -1 : 1; }
+ Item_func_add_time(THD *thd, Item *a, Item *b, bool neg_arg)
+ :Item_temporal_hybrid_func(thd, a, b), sign(neg_arg ? -1 : 1)
+ { }
bool fix_length_and_dec();
bool get_date(MYSQL_TIME *ltime, ulonglong fuzzy_date);
- void print(String *str, enum_query_type query_type);
- const char *func_name() const { return "add_time"; }
+ const char *func_name() const { return sign > 0 ? "addtime" : "subtime"; }
Item *get_copy(THD *thd)
{ return get_item_copy<Item_func_add_time>(thd, this); }
};
diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
index c484af0fb78..cf894325ba5 100644
--- a/sql/sql_yacc.yy
+++ b/sql/sql_yacc.yy
@@ -10216,7 +10216,7 @@ function_call_keyword_timestamp:
}
| TIMESTAMP '(' expr ',' expr ')'
{
- $$= new (thd->mem_root) Item_func_add_time(thd, $3, $5, 1, 0);
+ $$= new (thd->mem_root) Item_func_timestamp(thd, $3, $5);
if (unlikely($$ == NULL))
MYSQL_YYABORT;
}
diff --git a/sql/sql_yacc_ora.yy b/sql/sql_yacc_ora.yy
index 6397654bc7d..ef12b9c0c53 100644
--- a/sql/sql_yacc_ora.yy
+++ b/sql/sql_yacc_ora.yy
@@ -10413,7 +10413,7 @@ function_call_keyword_timestamp:
}
| TIMESTAMP '(' expr ',' expr ')'
{
- $$= new (thd->mem_root) Item_func_add_time(thd, $3, $5, 1, 0);
+ $$= new (thd->mem_root) Item_func_timestamp(thd, $3, $5);
if (unlikely($$ == NULL))
MYSQL_YYABORT;
}