summaryrefslogtreecommitdiff
path: root/sql-common
diff options
context:
space:
mode:
authorMichael Widenius <monty@askmonty.org>2011-05-28 05:11:32 +0300
committerMichael Widenius <monty@askmonty.org>2011-05-28 05:11:32 +0300
commitf197991f4102ed8ac66b7b57071f24f1d3b86aea (patch)
treeb4590b80e7d50b664d8e6ff6a62978cb2402f0a6 /sql-common
parentde44b51e151a00a00d0e396dc57ced3682d24d78 (diff)
parent306ed65302e14f303fdc33cfa9d19016fb319440 (diff)
downloadmariadb-git-f197991f4102ed8ac66b7b57071f24f1d3b86aea.tar.gz
Merge with 5.1-microseconds
A lot of small fixes and new test cases. client/mysqlbinlog.cc: Cast removed client/mysqltest.cc: Added missing DBUG_RETURN include/my_pthread.h: set_timespec_time_nsec() now only takes one argument mysql-test/t/date_formats.test: Remove --disable_ps_protocl as now also ps supports microseconds mysys/my_uuid.c: Changed to use my_interval_timer() instead of my_getsystime() mysys/waiting_threads.c: Changed to use my_hrtime() sql/field.h: Added bool special_const_compare() for fields that may convert values before compare (like year) sql/field_conv.cc: Added test to get optimal copying of identical temporal values. sql/item.cc: Return that item_int is equal if it's positive, even if unsigned flag is different. Fixed Item_cache_str::save_in_field() to have identical null check as other similar functions Added proper NULL check to Item_cache_int::save_in_field() sql/item_cmpfunc.cc: Don't call convert_constant_item() if there is nothing that is worth converting. Simplified test when years should be converted sql/item_sum.cc: Mark cache values in Item_sum_hybrid as not constants to ensure they are not replaced by other cache values in compare_datetime() sql/item_timefunc.cc: Changed sec_to_time() to take a my_decimal argument to ensure we don't loose any sub seconds. Added Item_temporal_func::get_time() (This simplifies some things) sql/mysql_priv.h: Added Lazy_string_decimal() sql/mysqld.cc: Added my_decimal constants max_seconds_for_time_type, time_second_part_factor sql/table.cc: Changed expr_arena to be of type CONVENTIONAL_EXECUTION to ensure that we don't loose any items that are created by fix_fields() sql/tztime.cc: TIME_to_gmt_sec() now sets *in_dst_time_gap in case of errors This is needed to be able to detect if timestamp is 0 storage/maria/lockman.c: Changed from my_getsystime() to set_timespec_time_nsec() storage/maria/ma_loghandler.c: Changed from my_getsystime() to my_hrtime() storage/maria/ma_recovery.c: Changed from my_getsystime() to mmicrosecond_interval_timer() storage/maria/unittest/trnman-t.c: Changed from my_getsystime() to mmicrosecond_interval_timer() storage/xtradb/handler/ha_innodb.cc: Added support for new time,datetime and timestamp unittest/mysys/thr_template.c: my_getsystime() -> my_interval_timer() unittest/mysys/waiting_threads-t.c: my_getsystime() -> my_interval_timer()
Diffstat (limited to 'sql-common')
-rw-r--r--sql-common/my_time.c206
1 files changed, 158 insertions, 48 deletions
diff --git a/sql-common/my_time.c b/sql-common/my_time.c
index b72e76a5ed8..f692bd51185 100644
--- a/sql-common/my_time.c
+++ b/sql-common/my_time.c
@@ -81,7 +81,7 @@ my_bool check_date(const MYSQL_TIME *ltime, my_bool not_zero_date,
if (not_zero_date)
{
if ((((flags & TIME_NO_ZERO_IN_DATE) || !(flags & TIME_FUZZY_DATE)) &&
- (ltime->month == 0 || ltime->day == 0)) ||
+ (ltime->month == 0 || ltime->day == 0)) || ltime->neg ||
(!(flags & TIME_INVALID_DATES) &&
ltime->month && ltime->day > days_in_month[ltime->month-1] &&
(ltime->month != 2 || calc_days_in_year(ltime->year) != 366 ||
@@ -158,7 +158,7 @@ my_bool check_date(const MYSQL_TIME *ltime, my_bool not_zero_date,
enum enum_mysql_timestamp_type
str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time,
- uint flags, int *was_cut)
+ ulong flags, int *was_cut)
{
uint field_length, UNINIT_VAR(year_length), digits, i, number_of_fields;
uint date[MAX_DATE_PARTS], date_len[MAX_DATE_PARTS];
@@ -175,6 +175,13 @@ str_to_datetime(const char *str, uint length, MYSQL_TIME *l_time,
LINT_INIT(field_length);
+ if (flags & TIME_TIME_ONLY)
+ {
+ enum enum_mysql_timestamp_type ret;
+ ret= str_to_time(str, length, l_time, flags, was_cut);
+ DBUG_RETURN(ret);
+ }
+
*was_cut= 0;
/* Skip space at start */
@@ -477,12 +484,13 @@ err:
work with times where the time arguments are in the above order.
RETURN
- 0 ok
- 1 error
+ MYSQL_TIMESTAMP_TIME
+ MYSQL_TIMESTAMP_ERROR
*/
-my_bool str_to_time(const char *str, uint length, MYSQL_TIME *l_time,
- ulong fuzzydate, int *warning)
+enum enum_mysql_timestamp_type
+str_to_time(const char *str, uint length, MYSQL_TIME *l_time,
+ ulong fuzzydate, int *warning)
{
ulong date[5];
ulonglong value;
@@ -501,7 +509,7 @@ my_bool str_to_time(const char *str, uint length, MYSQL_TIME *l_time,
length--;
}
if (str == end)
- return 1;
+ return MYSQL_TIMESTAMP_ERROR;
/* Check first if this is a full TIMESTAMP */
if (length >= 12)
@@ -509,12 +517,13 @@ my_bool str_to_time(const char *str, uint length, MYSQL_TIME *l_time,
int was_cut;
enum enum_mysql_timestamp_type
res= str_to_datetime(str, length, l_time,
- fuzzydate | TIME_DATETIME_ONLY, &was_cut);
+ (fuzzydate & ~TIME_TIME_ONLY) | TIME_DATETIME_ONLY,
+ &was_cut);
if ((int) res >= (int) MYSQL_TIMESTAMP_ERROR)
{
if (was_cut)
*warning|= MYSQL_TIME_WARN_TRUNCATED;
- return res == MYSQL_TIMESTAMP_ERROR;
+ return res;
}
}
@@ -609,7 +618,7 @@ fractional:
((str[1] == '-' || str[1] == '+') &&
(end - str) > 2 &&
my_isdigit(&my_charset_latin1, str[2]))))
- return 1;
+ return MYSQL_TIMESTAMP_ERROR;
if (internal_format_positions[7] != 255)
{
@@ -632,7 +641,7 @@ fractional:
if (date[0] > UINT_MAX || date[1] > UINT_MAX ||
date[2] > UINT_MAX || date[3] > UINT_MAX ||
date[4] > UINT_MAX)
- return 1;
+ return MYSQL_TIMESTAMP_ERROR;
l_time->year= 0; /* For protocol::store_time */
l_time->month= 0;
@@ -644,8 +653,8 @@ fractional:
l_time->time_type= MYSQL_TIMESTAMP_TIME;
/* Check if the value is valid and fits into MYSQL_TIME range */
- if (check_time_range(l_time, warning))
- return 1;
+ if (check_time_range(l_time, 6, warning))
+ return MYSQL_TIMESTAMP_ERROR;
/* Check if there is garbage at end of the MYSQL_TIME specification */
if (str != end)
@@ -659,7 +668,7 @@ fractional:
}
} while (++str != end);
}
- return 0;
+ return MYSQL_TIMESTAMP_TIME;
}
@@ -669,6 +678,7 @@ fractional:
SYNOPSIS:
check_time_range()
time pointer to MYSQL_TIME value
+ uint dec
warning set MYSQL_TIME_WARN_OUT_OF_RANGE flag if the value is out of range
DESCRIPTION
@@ -681,24 +691,31 @@ fractional:
1 time value is invalid
*/
-int check_time_range(struct st_mysql_time *my_time, int *warning)
+int check_time_range(struct st_mysql_time *my_time, uint dec, int *warning)
{
longlong hour;
+ static ulong max_sec_part[TIME_SECOND_PART_DIGITS+1]= {000000, 900000, 990000,
+ 999000, 999900, 999990, 999999};
if (my_time->minute >= 60 || my_time->second >= 60)
return 1;
hour= my_time->hour + (24*my_time->day);
+
+ if (dec == AUTO_SEC_PART_DIGITS)
+ dec= TIME_SECOND_PART_DIGITS;
+
if (hour <= TIME_MAX_HOUR &&
(hour != TIME_MAX_HOUR || my_time->minute != TIME_MAX_MINUTE ||
- my_time->second != TIME_MAX_SECOND || !my_time->second_part))
+ my_time->second != TIME_MAX_SECOND ||
+ my_time->second_part <= max_sec_part[dec]))
return 0;
my_time->day= 0;
my_time->hour= TIME_MAX_HOUR;
my_time->minute= TIME_MAX_MINUTE;
my_time->second= TIME_MAX_SECOND;
- my_time->second_part= 0;
+ my_time->second_part= max_sec_part[dec];
*warning|= MYSQL_TIME_WARN_OUT_OF_RANGE;
return 0;
}
@@ -826,6 +843,8 @@ my_system_gmt_sec(const MYSQL_TIME *t_src, long *my_timezone,
struct tm *l_time,tm_tmp;
long diff, current_timezone;
+ *in_dst_time_gap= 0; /* Reset */
+
/*
Use temp variable to avoid trashing input data, which could happen in
case of shift required for boundary dates processing.
@@ -833,7 +852,10 @@ my_system_gmt_sec(const MYSQL_TIME *t_src, long *my_timezone,
memcpy(&tmp_time, t_src, sizeof(MYSQL_TIME));
if (!validate_timestamp_range(t))
+ {
+ *in_dst_time_gap= 1;
return 0;
+ }
/*
Calculate the gmt time based on current time and timezone
@@ -998,7 +1020,10 @@ my_system_gmt_sec(const MYSQL_TIME *t_src, long *my_timezone,
larger then TIMESTAMP_MAX_VALUE, so another check will work.
*/
if (!IS_TIME_T_VALID_FOR_TIMESTAMP(tmp))
+ {
tmp= 0;
+ *in_dst_time_gap= 1;
+ }
return (my_time_t) tmp;
} /* my_system_gmt_sec */
@@ -1018,26 +1043,27 @@ void set_zero_time(MYSQL_TIME *tm, enum enum_mysql_timestamp_type time_type)
using default format.
This functions don't check that given MYSQL_TIME structure members are
in valid range. If they are not, return value won't reflect any
- valid date either. Additionally, make_time doesn't take into
- account time->day member: it's assumed that days have been converted
- to hours already.
+ valid date either.
RETURN
number of characters written to 'to'
*/
-int my_time_to_str(const MYSQL_TIME *l_time, char *to)
+int my_time_to_str(const MYSQL_TIME *l_time, char *to, uint digits)
{
- uint extra_hours= 0;
- return my_sprintf(to, (to,
- (l_time->second_part ?
- "%s%02u:%02u:%02u.%06ld" :
- "%s%02u:%02u:%02u"),
- (l_time->neg ? "-" : ""),
- extra_hours+ l_time->hour,
- l_time->minute,
- l_time->second,
- l_time->second_part));
+ ulong day= (l_time->year || l_time->month) ? 0 : l_time->day;
+
+ if (digits == AUTO_SEC_PART_DIGITS)
+ digits= l_time->second_part ? TIME_SECOND_PART_DIGITS : 0;
+
+ DBUG_ASSERT(digits <= TIME_SECOND_PART_DIGITS);
+
+ return sprintf(to,
+ digits ? "%s%02lu:%02u:%02u.%0*lu"
+ : "%s%02lu:%02u:%02u",
+ (l_time->neg ? "-" : ""),
+ day * 24L + l_time->hour, l_time->minute, l_time->second,
+ digits, (ulong)sec_part_shift(l_time->second_part, digits));
}
int my_date_to_str(const MYSQL_TIME *l_time, char *to)
@@ -1048,19 +1074,19 @@ int my_date_to_str(const MYSQL_TIME *l_time, char *to)
l_time->day));
}
-int my_datetime_to_str(const MYSQL_TIME *l_time, char *to)
+int my_datetime_to_str(const MYSQL_TIME *l_time, char *to, uint digits)
{
- return my_sprintf(to, (to,
- (l_time->second_part ?
- "%04u-%02u-%02u %02u:%02u:%02u.%06ld" :
- "%04u-%02u-%02u %02u:%02u:%02u"),
- l_time->year,
- l_time->month,
- l_time->day,
- l_time->hour,
- l_time->minute,
- l_time->second,
- l_time->second_part));
+ if (digits == AUTO_SEC_PART_DIGITS)
+ digits= l_time->second_part ? TIME_SECOND_PART_DIGITS : 0;
+
+ DBUG_ASSERT(digits <= TIME_SECOND_PART_DIGITS);
+
+ return sprintf(to,
+ digits ? "%04u-%02u-%02u %02u:%02u:%02u.%0*lu"
+ : "%04u-%02u-%02u %02u:%02u:%02u",
+ l_time->year, l_time->month, l_time->day,
+ l_time->hour, l_time->minute, l_time->second,
+ digits, (ulong)sec_part_shift(l_time->second_part, digits));
}
@@ -1078,15 +1104,15 @@ int my_datetime_to_str(const MYSQL_TIME *l_time, char *to)
The string must have at least MAX_DATE_STRING_REP_LENGTH bytes reserved.
*/
-int my_TIME_to_str(const MYSQL_TIME *l_time, char *to)
+int my_TIME_to_str(const MYSQL_TIME *l_time, char *to, uint digits)
{
switch (l_time->time_type) {
case MYSQL_TIMESTAMP_DATETIME:
- return my_datetime_to_str(l_time, to);
+ return my_datetime_to_str(l_time, to, digits);
case MYSQL_TIMESTAMP_DATE:
return my_date_to_str(l_time, to);
case MYSQL_TIMESTAMP_TIME:
- return my_time_to_str(l_time, to);
+ return my_time_to_str(l_time, to, digits);
case MYSQL_TIMESTAMP_NONE:
case MYSQL_TIMESTAMP_ERROR:
to[0]='\0';
@@ -1207,6 +1233,52 @@ longlong number_to_datetime(longlong nr, MYSQL_TIME *time_res,
return LL(-1);
}
+/*
+ Convert a double to a MYSQL_TIME struct.
+
+ @param[in] nr a number to convert
+ @param[out] ltime Date to check.
+ @param[out] was_cut MYSQL_TIME_WARN_OUT_OF_RANGE if the value was
+ modified to fit in the valid range. Otherwise 0.
+
+ @details
+ Takes a number in the [-]HHHMMSS.uuuuuu format.
+
+ number_to_datetime() cannot take a double, because double precision is not
+ enough for YYYYMMDDHHMMSS.uuuuuu
+
+ @return
+ 0 time value is valid, but was possibly truncated
+ 1 time value is invalid
+*/
+int number_to_time(double nr, MYSQL_TIME *ltime, int *was_cut)
+{
+ ulong tmp;
+ *was_cut= 0;
+ ltime->year= ltime->month= ltime->day= 0;
+ ltime->time_type= MYSQL_TIMESTAMP_TIME;
+
+ if ((ltime->neg= nr < 0))
+ nr= -nr;
+
+ if (nr > TIME_MAX_VALUE)
+ {
+ nr= TIME_MAX_VALUE;
+ *was_cut= MYSQL_TIME_WARN_OUT_OF_RANGE;
+ }
+ tmp=(ulong)floor(nr);
+ ltime->hour = tmp/100/100;
+ ltime->minute= tmp/100%100;
+ ltime->second= tmp%100;
+ ltime->second_part= (ulong)((nr-tmp)*TIME_SECOND_PART_FACTOR);
+
+ if (ltime->minute < 60 && ltime->second < 60)
+ return 0;
+
+ *was_cut= MYSQL_TIME_WARN_TRUNCATED;
+ return 1;
+}
+
/*
Convert a double to a date/datetime
@@ -1239,7 +1311,7 @@ my_bool double_to_datetime(double value, MYSQL_TIME *ltime,
{
/* Add sub seconds to results */
ltime->second_part= (ulong) (floor((value - datepart_value) *
- TIME_SUBSECOND_RANGE));
+ TIME_SECOND_PART_FACTOR));
}
return 0;
}
@@ -1292,7 +1364,7 @@ ulonglong TIME_to_ulonglong_time(const MYSQL_TIME *my_time)
DESCRIPTION
The function is used when we need to convert value of time item
to a number if it's used in numeric context, i. e.:
- SELECT NOW()+1, CURDATE()+0, CURTIMIE()+0;
+ SELECT NOW()+1, CURDATE()+0, CURTIME()+0;
SELECT ?+1;
NOTE
@@ -1319,3 +1391,41 @@ ulonglong TIME_to_ulonglong(const MYSQL_TIME *my_time)
return 0;
}
+double TIME_to_double(const MYSQL_TIME *my_time)
+{
+ double d= (double)TIME_to_ulonglong(my_time);
+
+ if (my_time->time_type == MYSQL_TIMESTAMP_DATE)
+ return d;
+
+ d+= my_time->second_part/(double)TIME_SECOND_PART_FACTOR;
+ return my_time->neg ? -d : d;
+}
+
+longlong pack_time(MYSQL_TIME *my_time)
+{
+ return ((((((my_time->year * 13ULL +
+ my_time->month) * 32ULL +
+ my_time->day) * 24ULL +
+ my_time->hour) * 60ULL +
+ my_time->minute) * 60ULL +
+ my_time->second) * 1000000ULL +
+ my_time->second_part) * (my_time->neg ? -1 : 1);
+}
+
+#define get_one(WHERE, FACTOR) WHERE= (ulong)(packed % FACTOR); packed/= FACTOR
+
+MYSQL_TIME *unpack_time(longlong packed, MYSQL_TIME *my_time)
+{
+ if ((my_time->neg= packed < 0))
+ packed= -packed;
+ get_one(my_time->second_part, 1000000ULL);
+ get_one(my_time->second, 60ULL);
+ get_one(my_time->minute, 60ULL);
+ get_one(my_time->hour, 24ULL);
+ get_one(my_time->day, 32ULL);
+ get_one(my_time->month, 13ULL);
+ my_time->year= (uint)packed;
+ my_time->time_type= MYSQL_TIMESTAMP_DATETIME;
+ return my_time;
+}