SAPDB date/time finctions

parent e6eef748
...@@ -44,10 +44,10 @@ t1 CREATE TABLE `t1` ( ...@@ -44,10 +44,10 @@ t1 CREATE TABLE `t1` (
drop table t1; drop table t1;
select cast("2001-1-1" as date) = "2001-01-01"; select cast("2001-1-1" as date) = "2001-01-01";
cast("2001-1-1" as date) = "2001-01-01" cast("2001-1-1" as date) = "2001-01-01"
0 1
select cast("2001-1-1" as datetime) = "2001-01-01 00:00:00"; select cast("2001-1-1" as datetime) = "2001-01-01 00:00:00";
cast("2001-1-1" as datetime) = "2001-01-01 00:00:00" cast("2001-1-1" as datetime) = "2001-01-01 00:00:00"
0 1
select cast("1:2:3" as TIME) = "1:02:03"; select cast("1:2:3" as TIME) = "1:02:03";
cast("1:2:3" as TIME) = "1:02:03" cast("1:2:3" as TIME) = "1:02:03"
0 0
......
...@@ -101,8 +101,8 @@ timediff("1997-12-30 23:59:59.000001","1997-12-31 23:59:59.000002") ...@@ -101,8 +101,8 @@ timediff("1997-12-30 23:59:59.000001","1997-12-31 23:59:59.000002")
select timediff("1997-12-31 23:59:59.000001","23:59:59.000001"); select timediff("1997-12-31 23:59:59.000001","23:59:59.000001");
timediff("1997-12-31 23:59:59.000001","23:59:59.000001") timediff("1997-12-31 23:59:59.000001","23:59:59.000001")
NULL NULL
select timediff("2000:01:01 00:00:00", "2000:01:01 00:00:00.1"); select timediff("2000:01:01 00:00:00", "2000:01:01 00:00:00.000001");
timediff("2000:01:01 00:00:00", "2000:01:01 00:00:00.1") timediff("2000:01:01 00:00:00", "2000:01:01 00:00:00.000001")
-00:00:00.000001 -00:00:00.000001
select maketime(10,11,12); select maketime(10,11,12);
maketime(10,11,12) maketime(10,11,12)
...@@ -122,6 +122,12 @@ NULL ...@@ -122,6 +122,12 @@ NULL
select timestamp("2001-12-01", "25:01:01"); select timestamp("2001-12-01", "25:01:01");
timestamp("2001-12-01", "25:01:01") timestamp("2001-12-01", "25:01:01")
2001-12-02 01:01:01 2001-12-02 01:01:01
select timestamp("2001-12-01 01:01:01.000100");
timestamp("2001-12-01 01:01:01.000100")
2001-12-01 01:01:01.000100
select timestamp("2001-12-01");
timestamp("2001-12-01")
2001-12-01 00:00:00
select day("1997-12-31 23:59:59.000001"); select day("1997-12-31 23:59:59.000001");
day("1997-12-31 23:59:59.000001") day("1997-12-31 23:59:59.000001")
31 31
...@@ -147,7 +153,7 @@ addtime(cast("23:59:59.999999" as time) , "1 1:1:1.000002") as f3, ...@@ -147,7 +153,7 @@ addtime(cast("23:59:59.999999" as time) , "1 1:1:1.000002") as f3,
timediff("1997-12-31 23:59:59.000001","1997-12-30 01:01:01.000002") as f4, timediff("1997-12-31 23:59:59.000001","1997-12-30 01:01:01.000002") as f4,
timediff("1997-12-30 23:59:59.000001","1997-12-31 23:59:59.000002") as f5, timediff("1997-12-30 23:59:59.000001","1997-12-31 23:59:59.000002") as f5,
maketime(10,11,12) as f6, maketime(10,11,12) as f6,
timestamp("2001-12-01", "01:01:01") as f7, timestamp(cast("2001-12-01" as date), "01:01:01") as f7,
date("1997-12-31 23:59:59.000001") as f8, date("1997-12-31 23:59:59.000001") as f8,
time("1997-12-31 23:59:59.000001") as f9; time("1997-12-31 23:59:59.000001") as f9;
describe t1; describe t1;
......
...@@ -51,7 +51,7 @@ select timediff("1997-01-01 23:59:59.000001","1995-12-31 23:59:59.000002"); ...@@ -51,7 +51,7 @@ select timediff("1997-01-01 23:59:59.000001","1995-12-31 23:59:59.000002");
select timediff("1997-12-31 23:59:59.000001","1997-12-30 01:01:01.000002"); select timediff("1997-12-31 23:59:59.000001","1997-12-30 01:01:01.000002");
select timediff("1997-12-30 23:59:59.000001","1997-12-31 23:59:59.000002"); select timediff("1997-12-30 23:59:59.000001","1997-12-31 23:59:59.000002");
select timediff("1997-12-31 23:59:59.000001","23:59:59.000001"); select timediff("1997-12-31 23:59:59.000001","23:59:59.000001");
select timediff("2000:01:01 00:00:00", "2000:01:01 00:00:00.1"); select timediff("2000:01:01 00:00:00", "2000:01:01 00:00:00.000001");
select maketime(10,11,12); select maketime(10,11,12);
select maketime(25,11,12); select maketime(25,11,12);
...@@ -61,6 +61,8 @@ select maketime(-25,11,12); ...@@ -61,6 +61,8 @@ select maketime(-25,11,12);
select timestamp("2001-12-01", "01:01:01.999999"); select timestamp("2001-12-01", "01:01:01.999999");
select timestamp("2001-13-01", "01:01:01.000001"); select timestamp("2001-13-01", "01:01:01.000001");
select timestamp("2001-12-01", "25:01:01"); select timestamp("2001-12-01", "25:01:01");
select timestamp("2001-12-01 01:01:01.000100");
select timestamp("2001-12-01");
select day("1997-12-31 23:59:59.000001"); select day("1997-12-31 23:59:59.000001");
select date("1997-12-31 23:59:59.000001"); select date("1997-12-31 23:59:59.000001");
select date("1997-13-31 23:59:59.000001"); select date("1997-13-31 23:59:59.000001");
...@@ -75,7 +77,7 @@ select makedate(1997,1) as f1, ...@@ -75,7 +77,7 @@ select makedate(1997,1) as f1,
timediff("1997-12-31 23:59:59.000001","1997-12-30 01:01:01.000002") as f4, timediff("1997-12-31 23:59:59.000001","1997-12-30 01:01:01.000002") as f4,
timediff("1997-12-30 23:59:59.000001","1997-12-31 23:59:59.000002") as f5, timediff("1997-12-30 23:59:59.000001","1997-12-31 23:59:59.000002") as f5,
maketime(10,11,12) as f6, maketime(10,11,12) as f6,
timestamp("2001-12-01", "01:01:01") as f7, timestamp(cast("2001-12-01" as date), "01:01:01") as f7,
date("1997-12-31 23:59:59.000001") as f8, date("1997-12-31 23:59:59.000001") as f8,
time("1997-12-31 23:59:59.000001") as f9; time("1997-12-31 23:59:59.000001") as f9;
describe t1; describe t1;
......
...@@ -709,12 +709,12 @@ Item *create_func_makedate(Item* a,Item* b) ...@@ -709,12 +709,12 @@ Item *create_func_makedate(Item* a,Item* b)
Item *create_func_addtime(Item* a,Item* b) Item *create_func_addtime(Item* a,Item* b)
{ {
return new Item_func_add_time(a, b, 0); return new Item_func_add_time(a, b, 0, 0);
} }
Item *create_func_subtime(Item* a,Item* b) Item *create_func_subtime(Item* a,Item* b)
{ {
return new Item_func_add_time(a, b, 1); return new Item_func_add_time(a, b, 0, 1);
} }
Item *create_func_timediff(Item* a,Item* b) Item *create_func_timediff(Item* a,Item* b)
......
This diff is collapsed.
...@@ -557,6 +557,8 @@ class Item_date_typecast :public Item_typecast ...@@ -557,6 +557,8 @@ class Item_date_typecast :public Item_typecast
{ {
public: public:
Item_date_typecast(Item *a) :Item_typecast(a) {} Item_date_typecast(Item *a) :Item_typecast(a) {}
String *val_str(String *str);
bool get_date(TIME *ltime, bool fuzzy_date);
const char *func_name() const { return "date"; } const char *func_name() const { return "date"; }
enum_field_types field_type() const { return MYSQL_TYPE_DATE; } enum_field_types field_type() const { return MYSQL_TYPE_DATE; }
Field *tmp_table_field() { return result_field; } Field *tmp_table_field() { return result_field; }
...@@ -571,6 +573,8 @@ class Item_time_typecast :public Item_typecast ...@@ -571,6 +573,8 @@ class Item_time_typecast :public Item_typecast
{ {
public: public:
Item_time_typecast(Item *a) :Item_typecast(a) {} Item_time_typecast(Item *a) :Item_typecast(a) {}
String *val_str(String *str);
bool get_time(TIME *ltime);
const char *func_name() const { return "time"; } const char *func_name() const { return "time"; }
enum_field_types field_type() const { return MYSQL_TYPE_TIME; } enum_field_types field_type() const { return MYSQL_TYPE_TIME; }
Field *tmp_table_field() { return result_field; } Field *tmp_table_field() { return result_field; }
...@@ -585,6 +589,7 @@ class Item_datetime_typecast :public Item_typecast ...@@ -585,6 +589,7 @@ class Item_datetime_typecast :public Item_typecast
{ {
public: public:
Item_datetime_typecast(Item *a) :Item_typecast(a) {} Item_datetime_typecast(Item *a) :Item_typecast(a) {}
String *val_str(String *str);
const char *func_name() const { return "datetime"; } const char *func_name() const { return "datetime"; }
enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; } enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; }
Field *tmp_table_field() { return result_field; } Field *tmp_table_field() { return result_field; }
...@@ -604,7 +609,7 @@ class Item_func_makedate :public Item_str_func ...@@ -604,7 +609,7 @@ class Item_func_makedate :public Item_str_func
void fix_length_and_dec() void fix_length_and_dec()
{ {
decimals=0; decimals=0;
max_length=8*my_charset_bin.mbmaxlen; max_length=8*MY_CHARSET_BIN_MB_MAXLEN;
} }
Field *tmp_table_field() { return result_field; } Field *tmp_table_field() { return result_field; }
Field *tmp_table_field(TABLE *t_arg) Field *tmp_table_field(TABLE *t_arg)
...@@ -613,18 +618,26 @@ class Item_func_makedate :public Item_str_func ...@@ -613,18 +618,26 @@ class Item_func_makedate :public Item_str_func
} }
}; };
class Item_func_add_time :public Item_str_func class Item_func_add_time :public Item_str_func
{ {
const bool is_date;
int sign; int sign;
enum_field_types cached_field_type; enum_field_types cached_field_type;
public: public:
Item_func_add_time(Item *a, Item *b, bool neg_arg) Item_func_add_time(Item *a, Item *b, bool type_arg, bool neg_arg)
:Item_str_func(a, b) { sign= neg_arg ? -1 : 1; } :Item_str_func(a, b), is_date(type_arg) { sign= neg_arg ? -1 : 1; }
String *val_str(String *str); String *val_str(String *str);
const char *func_name() const { return "addtime"; } const char *func_name() const { return "addtime"; }
enum_field_types field_type() const { return cached_field_type; } enum_field_types field_type() const { return cached_field_type; }
void fix_length_and_dec(); void fix_length_and_dec();
/*
TODO:
Change this when we support
microseconds in TIME/DATETIME
*/
Field *tmp_table_field() { return result_field; } Field *tmp_table_field() { return result_field; }
Field *tmp_table_field(TABLE *t_arg) Field *tmp_table_field(TABLE *t_arg)
{ {
...@@ -647,7 +660,7 @@ class Item_func_timediff :public Item_str_func ...@@ -647,7 +660,7 @@ class Item_func_timediff :public Item_str_func
void fix_length_and_dec() void fix_length_and_dec()
{ {
decimals=0; decimals=0;
max_length=17*my_charset_bin.mbmaxlen; max_length=17*MY_CHARSET_BIN_MB_MAXLEN;
} }
Field *tmp_table_field() { return result_field; } Field *tmp_table_field() { return result_field; }
Field *tmp_table_field(TABLE *t_arg) Field *tmp_table_field(TABLE *t_arg)
...@@ -667,66 +680,7 @@ class Item_func_maketime :public Item_str_func ...@@ -667,66 +680,7 @@ class Item_func_maketime :public Item_str_func
void fix_length_and_dec() void fix_length_and_dec()
{ {
decimals=0; decimals=0;
max_length=8*my_charset_bin.mbmaxlen; max_length=8*MY_CHARSET_BIN_MB_MAXLEN;
}
Field *tmp_table_field() { return result_field; }
Field *tmp_table_field(TABLE *t_arg)
{
return (new Field_time(maybe_null, name, t_arg, &my_charset_bin));
}
};
class Item_func_timestamp :public Item_str_func
{
public:
Item_func_timestamp(Item *a, Item *b) :Item_str_func(a, b) {}
String *val_str(String *str);
const char *func_name() const { return "timestamp"; }
enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; }
void fix_length_and_dec()
{
decimals=0;
max_length=26*my_charset_bin.mbmaxlen;
}
Field *tmp_table_field() { return result_field; }
Field *tmp_table_field(TABLE *t_arg)
{
return (new Field_datetime(maybe_null, name, t_arg, &my_charset_bin));
}
};
class Item_func_date :public Item_str_func
{
public:
Item_func_date(Item *a)
:Item_str_func(a) {}
String *val_str(String *str);
const char *func_name() const { return "date"; }
enum_field_types field_type() const { return MYSQL_TYPE_DATE; }
void fix_length_and_dec()
{
decimals=0;
max_length=10*my_charset_bin.mbmaxlen;
}
Field *tmp_table_field() { return result_field; }
Field *tmp_table_field(TABLE *t_arg)
{
return (new Field_date(maybe_null, name, t_arg, &my_charset_bin));
}
};
class Item_func_time :public Item_str_func
{
public:
Item_func_time(Item *a)
:Item_str_func(a) {}
String *val_str(String *str);
const char *func_name() const { return "time"; }
enum_field_types field_type() const { return MYSQL_TYPE_TIME; }
void fix_length_and_dec()
{
decimals=0;
max_length=15*my_charset_bin.mbmaxlen;
} }
Field *tmp_table_field() { return result_field; } Field *tmp_table_field() { return result_field; }
Field *tmp_table_field(TABLE *t_arg) Field *tmp_table_field(TABLE *t_arg)
......
...@@ -224,6 +224,8 @@ extern CHARSET_INFO *national_charset_info, *table_alias_charset; ...@@ -224,6 +224,8 @@ extern CHARSET_INFO *national_charset_info, *table_alias_charset;
#define RAID_BLOCK_SIZE 1024 #define RAID_BLOCK_SIZE 1024
#define MY_CHARSET_BIN_MB_MAXLEN 1
#ifdef EXTRA_DEBUG #ifdef EXTRA_DEBUG
/* /*
Sync points allow us to force the server to reach a certain line of code Sync points allow us to force the server to reach a certain line of code
......
...@@ -823,6 +823,13 @@ bool Protocol_simple::store(Field *field) ...@@ -823,6 +823,13 @@ bool Protocol_simple::store(Field *field)
} }
/*
TODO:
Second_part format ("%06") needs to change when
we support 0-6 decimals for time.
*/
bool Protocol_simple::store(TIME *tm) bool Protocol_simple::store(TIME *tm)
{ {
#ifndef DEBUG_OFF #ifndef DEBUG_OFF
...@@ -863,6 +870,12 @@ bool Protocol_simple::store_date(TIME *tm) ...@@ -863,6 +870,12 @@ bool Protocol_simple::store_date(TIME *tm)
} }
/*
TODO:
Second_part format ("%06") needs to change when
we support 0-6 decimals for time.
*/
bool Protocol_simple::store_time(TIME *tm) bool Protocol_simple::store_time(TIME *tm)
{ {
#ifndef DEBUG_OFF #ifndef DEBUG_OFF
......
...@@ -390,6 +390,23 @@ bool String::append(IO_CACHE* file, uint32 arg_length) ...@@ -390,6 +390,23 @@ bool String::append(IO_CACHE* file, uint32 arg_length)
return FALSE; return FALSE;
} }
bool String::append_with_prefill(const char *s,uint32 arg_length,
uint32 full_length, char fill_char)
{
int t_length= arg_length > full_length ? arg_length : full_length;
if (realloc(str_length + t_length))
return TRUE;
t_length= full_length - arg_length;
if (t_length > 0)
{
bfill(Ptr+str_length, t_length, fill_char);
str_length=str_length + t_length;
}
append(s, arg_length);
return FALSE;
}
uint32 String::numchars() uint32 String::numchars()
{ {
return str_charset->cset->numchars(str_charset, Ptr, Ptr+str_length); return str_charset->cset->numchars(str_charset, Ptr, Ptr+str_length);
......
...@@ -189,6 +189,8 @@ class String ...@@ -189,6 +189,8 @@ class String
bool append(const char *s,uint32 arg_length=0); bool append(const char *s,uint32 arg_length=0);
bool append(const char *s,uint32 arg_length, CHARSET_INFO *cs); bool append(const char *s,uint32 arg_length, CHARSET_INFO *cs);
bool append(IO_CACHE* file, uint32 arg_length); bool append(IO_CACHE* file, uint32 arg_length);
bool append_with_prefill(const char *s, uint32 arg_length,
uint32 full_length, char fill_char);
int strstr(const String &search,uint32 offset=0); // Returns offset to substring or -1 int strstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
int strstr_case(const String &s,uint32 offset=0); int strstr_case(const String &s,uint32 offset=0);
int strrstr(const String &search,uint32 offset=0); // Returns offset to substring or -1 int strrstr(const String &search,uint32 offset=0); // Returns offset to substring or -1
......
...@@ -2381,7 +2381,7 @@ simple_expr: ...@@ -2381,7 +2381,7 @@ simple_expr:
Lex->safe_to_cache_query=0; Lex->safe_to_cache_query=0;
} }
| DATE_SYM '(' expr ')' | DATE_SYM '(' expr ')'
{ $$= new Item_func_date($3); } { $$= new Item_date_typecast($3); }
| DAY_SYM '(' expr ')' | DAY_SYM '(' expr ')'
{ $$= new Item_func_dayofmonth($3); } { $$= new Item_func_dayofmonth($3); }
| ELT_FUNC '(' expr ',' expr_list ')' | ELT_FUNC '(' expr ',' expr_list ')'
...@@ -2580,9 +2580,11 @@ simple_expr: ...@@ -2580,9 +2580,11 @@ simple_expr:
| SUBSTRING_INDEX '(' expr ',' expr ',' expr ')' | SUBSTRING_INDEX '(' expr ',' expr ',' expr ')'
{ $$= new Item_func_substr_index($3,$5,$7); } { $$= new Item_func_substr_index($3,$5,$7); }
| TIME_SYM '(' expr ')' | TIME_SYM '(' expr ')'
{ $$= new Item_func_time($3); } { $$= new Item_time_typecast($3); }
| TIMESTAMP '(' expr ')'
{ $$= new Item_datetime_typecast($3); }
| TIMESTAMP '(' expr ',' expr ')' | TIMESTAMP '(' expr ',' expr ')'
{ $$= new Item_func_timestamp($3, $5); } { $$= new Item_func_add_time($3, $5, 1, 0); }
| TRIM '(' expr ')' | TRIM '(' expr ')'
{ $$= new Item_func_trim($3); } { $$= new Item_func_trim($3); }
| TRIM '(' LEADING expr FROM expr ')' | TRIM '(' LEADING expr FROM expr ')'
...@@ -4427,6 +4429,7 @@ keyword: ...@@ -4427,6 +4429,7 @@ keyword:
| MEDIUM_SYM {} | MEDIUM_SYM {}
| MERGE_SYM {} | MERGE_SYM {}
| MEMORY_SYM {} | MEMORY_SYM {}
| MICROSECOND_SYM {}
| MINUTE_SYM {} | MINUTE_SYM {}
| MIN_ROWS {} | MIN_ROWS {}
| MODIFY_SYM {} | MODIFY_SYM {}
......
...@@ -432,6 +432,7 @@ str_to_TIME(const char *str, uint length, TIME *l_time,bool fuzzy_date) ...@@ -432,6 +432,7 @@ str_to_TIME(const char *str, uint length, TIME *l_time,bool fuzzy_date)
l_time->minute=date[4]; l_time->minute=date[4];
l_time->second=date[5]; l_time->second=date[5];
l_time->second_part=date[6]; l_time->second_part=date[6];
l_time->neg= 0;
DBUG_RETURN(l_time->time_type= DBUG_RETURN(l_time->time_type=
(number_of_fields <= 3 ? TIMESTAMP_DATE : TIMESTAMP_FULL)); (number_of_fields <= 3 ? TIMESTAMP_DATE : TIMESTAMP_FULL));
} }
......
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