Commit ec7c4fb4 authored by Mattias Jonsson's avatar Mattias Jonsson

port of fixes for bug-20577 and 46362 for TO_SECONDS

parent 93e02adc
......@@ -656,6 +656,335 @@ EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a BETWEEN '0001-01-01' AND '1001-01-01
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p1001-01-01 ALL NULL NULL NULL NULL 7 Using where
DROP TABLE t1;
# TO_SECONDS, test of LIST and index
CREATE TABLE t1 (a DATE, KEY(a))
PARTITION BY LIST (TO_SECONDS(a))
(PARTITION `p0001-01-01` VALUES IN (TO_SECONDS('0001-01-01')),
PARTITION `p2001-01-01` VALUES IN (TO_SECONDS('2001-01-01')),
PARTITION `pNULL` VALUES IN (NULL),
PARTITION `p0000-01-02` VALUES IN (TO_SECONDS('0000-01-02')),
PARTITION `p1001-01-01` VALUES IN (TO_SECONDS('1001-01-01')));
INSERT INTO t1 VALUES ('0000-00-00'), ('0000-01-02'), ('0001-01-01'),
('1001-00-00'), ('1001-01-01'), ('1002-00-00'), ('2001-01-01');
SELECT * FROM t1 WHERE a < '1001-01-01';
a
0000-00-00
0000-01-02
0001-01-01
1001-00-00
SELECT * FROM t1 WHERE a <= '1001-01-01';
a
0000-00-00
0000-01-02
0001-01-01
1001-00-00
1001-01-01
SELECT * FROM t1 WHERE a >= '1001-01-01';
a
1001-01-01
1002-00-00
2001-01-01
SELECT * FROM t1 WHERE a > '1001-01-01';
a
1002-00-00
2001-01-01
SELECT * FROM t1 WHERE a = '1001-01-01';
a
1001-01-01
SELECT * FROM t1 WHERE a < '1001-00-00';
a
0000-00-00
0000-01-02
0001-01-01
SELECT * FROM t1 WHERE a <= '1001-00-00';
a
0000-00-00
0000-01-02
0001-01-01
1001-00-00
SELECT * FROM t1 WHERE a >= '1001-00-00';
a
1001-00-00
1001-01-01
1002-00-00
2001-01-01
SELECT * FROM t1 WHERE a > '1001-00-00';
a
1001-01-01
1002-00-00
2001-01-01
SELECT * FROM t1 WHERE a = '1001-00-00';
a
1001-00-00
# Disabling warnings for the invalid date
SELECT * FROM t1 WHERE a < '1999-02-31';
a
0000-00-00
0000-01-02
0001-01-01
1001-00-00
1001-01-01
1002-00-00
SELECT * FROM t1 WHERE a <= '1999-02-31';
a
0000-00-00
0000-01-02
0001-01-01
1001-00-00
1001-01-01
1002-00-00
SELECT * FROM t1 WHERE a >= '1999-02-31';
a
2001-01-01
SELECT * FROM t1 WHERE a > '1999-02-31';
a
2001-01-01
SELECT * FROM t1 WHERE a = '1999-02-31';
a
SELECT * FROM t1 WHERE a BETWEEN '0000-00-00' AND '1002-00-00';
a
0000-00-00
0000-01-02
0001-01-01
1001-00-00
1001-01-01
1002-00-00
SELECT * FROM t1 WHERE a BETWEEN '0000-00-00' AND '1001-01-01';
a
0000-00-00
0000-01-02
0001-01-01
1001-00-00
1001-01-01
SELECT * FROM t1 WHERE a BETWEEN '0001-01-02' AND '1002-00-00';
a
1001-00-00
1001-01-01
1002-00-00
SELECT * FROM t1 WHERE a BETWEEN '0001-01-01' AND '1001-01-01';
a
0001-01-01
1001-00-00
1001-01-01
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a < '1001-01-01';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02 range a a 4 NULL 3 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a <= '1001-01-01';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02,p1001-01-01 range a a 4 NULL 4 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a >= '1001-01-01';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p2001-01-01,pNULL,p1001-01-01 range a a 4 NULL 4 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a > '1001-01-01';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p2001-01-01,pNULL range a a 4 NULL 3 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a = '1001-01-01';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p1001-01-01 system a NULL NULL NULL 1
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a < '1001-00-00';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02 range a a 4 NULL 3 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a <= '1001-00-00';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02 range a a 4 NULL 3 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a >= '1001-00-00';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p2001-01-01,pNULL,p1001-01-01 range a a 4 NULL 4 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a > '1001-00-00';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p2001-01-01,pNULL,p1001-01-01 range a a 4 NULL 4 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a = '1001-00-00';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 pNULL ref a a 4 const 1 Using where; Using index
# Disabling warnings for the invalid date
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a < '1999-02-31';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02,p1001-01-01 range a a 4 NULL 5 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a <= '1999-02-31';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02,p1001-01-01 range a a 4 NULL 5 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a >= '1999-02-31';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p2001-01-01,pNULL range a a 4 NULL 2 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a > '1999-02-31';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p2001-01-01,pNULL range a a 4 NULL 2 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a = '1999-02-31';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 pNULL ref a a 4 const 1 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a BETWEEN '0000-00-00' AND '1002-00-00';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02,p1001-01-01 range a a 4 NULL 5 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a BETWEEN '0000-00-00' AND '1001-01-01';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02,p1001-01-01 range a a 4 NULL 4 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a BETWEEN '0001-01-02' AND '1002-00-00';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 pNULL,p1001-01-01 range a a 4 NULL 2 Using where; Using index
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a BETWEEN '0001-01-01' AND '1001-01-01';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p1001-01-01 range a a 4 NULL 3 Using where; Using index
# test without index
ALTER TABLE t1 DROP KEY a;
SELECT * FROM t1 WHERE a < '1001-01-01';
a
0000-00-00
0000-01-02
0001-01-01
1001-00-00
SELECT * FROM t1 WHERE a <= '1001-01-01';
a
0000-00-00
0000-01-02
0001-01-01
1001-00-00
1001-01-01
SELECT * FROM t1 WHERE a >= '1001-01-01';
a
1001-01-01
1002-00-00
2001-01-01
SELECT * FROM t1 WHERE a > '1001-01-01';
a
1002-00-00
2001-01-01
SELECT * FROM t1 WHERE a = '1001-01-01';
a
1001-01-01
SELECT * FROM t1 WHERE a < '1001-00-00';
a
0000-00-00
0000-01-02
0001-01-01
SELECT * FROM t1 WHERE a <= '1001-00-00';
a
0000-00-00
0000-01-02
0001-01-01
1001-00-00
SELECT * FROM t1 WHERE a >= '1001-00-00';
a
1001-00-00
1001-01-01
1002-00-00
2001-01-01
SELECT * FROM t1 WHERE a > '1001-00-00';
a
1001-01-01
1002-00-00
2001-01-01
SELECT * FROM t1 WHERE a = '1001-00-00';
a
1001-00-00
# Disabling warnings for the invalid date
SELECT * FROM t1 WHERE a < '1999-02-31';
a
0000-00-00
0000-01-02
0001-01-01
1001-00-00
1001-01-01
1002-00-00
SELECT * FROM t1 WHERE a <= '1999-02-31';
a
0000-00-00
0000-01-02
0001-01-01
1001-00-00
1001-01-01
1002-00-00
SELECT * FROM t1 WHERE a >= '1999-02-31';
a
2001-01-01
SELECT * FROM t1 WHERE a > '1999-02-31';
a
2001-01-01
SELECT * FROM t1 WHERE a = '1999-02-31';
a
SELECT * FROM t1 WHERE a BETWEEN '0000-00-00' AND '1002-00-00';
a
0000-00-00
0000-01-02
0001-01-01
1001-00-00
1001-01-01
1002-00-00
SELECT * FROM t1 WHERE a BETWEEN '0000-00-00' AND '1001-01-01';
a
0000-00-00
0000-01-02
0001-01-01
1001-00-00
1001-01-01
SELECT * FROM t1 WHERE a BETWEEN '0001-01-02' AND '1002-00-00';
a
1001-00-00
1001-01-01
1002-00-00
SELECT * FROM t1 WHERE a BETWEEN '0001-01-01' AND '1001-01-01';
a
0001-01-01
1001-00-00
1001-01-01
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a < '1001-01-01';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02 ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a <= '1001-01-01';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02,p1001-01-01 ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a >= '1001-01-01';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p2001-01-01,pNULL,p1001-01-01 ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a > '1001-01-01';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p2001-01-01,pNULL ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a = '1001-01-01';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p1001-01-01 system NULL NULL NULL NULL 1
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a < '1001-00-00';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02 ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a <= '1001-00-00';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02 ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a >= '1001-00-00';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p2001-01-01,pNULL,p1001-01-01 ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a > '1001-00-00';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p2001-01-01,pNULL,p1001-01-01 ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a = '1001-00-00';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 pNULL ALL NULL NULL NULL NULL 7 Using where
# Disabling warnings for the invalid date
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a < '1999-02-31';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02,p1001-01-01 ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a <= '1999-02-31';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02,p1001-01-01 ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a >= '1999-02-31';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p2001-01-01,pNULL ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a > '1999-02-31';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p2001-01-01,pNULL ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a = '1999-02-31';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 pNULL ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a BETWEEN '0000-00-00' AND '1002-00-00';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02,p1001-01-01 ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a BETWEEN '0000-00-00' AND '1001-01-01';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p0000-01-02,p1001-01-01 ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a BETWEEN '0001-01-02' AND '1002-00-00';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 pNULL,p1001-01-01 ALL NULL NULL NULL NULL 7 Using where
EXPLAIN PARTITIONS SELECT * FROM t1 WHERE a BETWEEN '0001-01-01' AND '1001-01-01';
id select_type table partitions type possible_keys key key_len ref rows Extra
1 SIMPLE t1 p0001-01-01,pNULL,p1001-01-01 ALL NULL NULL NULL NULL 7 Using where
DROP TABLE t1;
# Test with DATETIME column NOT NULL
CREATE TABLE t1 (
a int(10) unsigned NOT NULL,
......
......@@ -51,6 +51,26 @@ INSERT INTO t1 VALUES ('0000-00-00'), ('0000-01-02'), ('0001-01-01'),
ALTER TABLE t1 DROP KEY a;
--source include/partition_date_range.inc
DROP TABLE t1;
--echo # TO_SECONDS, test of LIST and index
CREATE TABLE t1 (a DATE, KEY(a))
PARTITION BY LIST (TO_SECONDS(a))
(PARTITION `p0001-01-01` VALUES IN (TO_SECONDS('0001-01-01')),
PARTITION `p2001-01-01` VALUES IN (TO_SECONDS('2001-01-01')),
PARTITION `pNULL` VALUES IN (NULL),
PARTITION `p0000-01-02` VALUES IN (TO_SECONDS('0000-01-02')),
PARTITION `p1001-01-01` VALUES IN (TO_SECONDS('1001-01-01')));
if ($verify_without_partitions)
{
ALTER TABLE t1 REMOVE PARTITIONING;
}
INSERT INTO t1 VALUES ('0000-00-00'), ('0000-01-02'), ('0001-01-01'),
('1001-00-00'), ('1001-01-01'), ('1002-00-00'), ('2001-01-01');
--source include/partition_date_range.inc
--echo # test without index
ALTER TABLE t1 DROP KEY a;
--source include/partition_date_range.inc
DROP TABLE t1;
#
# Bug#46362: Endpoint should be set to false for TO_DAYS(DATE)
......
......@@ -944,8 +944,29 @@ longlong Item_func_to_days::val_int()
longlong Item_func_to_seconds::val_int_endpoint(bool left_endp,
bool *incl_endp)
{
longlong res= val_int();
return null_value ? LONGLONG_MIN : res;
DBUG_ASSERT(fixed == 1);
MYSQL_TIME ltime;
longlong seconds;
longlong days;
int dummy; /* unused */
if (get_arg0_date(&ltime, TIME_FUZZY_DATE))
{
/* got NULL, leave the incl_endp intact */
return LONGLONG_MIN;
}
seconds= ltime.hour * 3600L + ltime.minute * 60 + ltime.second;
seconds= ltime.neg ? -seconds : seconds;
days= (longlong) calc_daynr(ltime.year, ltime.month, ltime.day);
seconds+= days * 24L * 3600L;
/* Set to NULL if invalid date, but keep the value */
null_value= check_date(&ltime,
(ltime.year || ltime.month || ltime.day),
(TIME_NO_ZERO_IN_DATE | TIME_NO_ZERO_DATE),
&dummy);
/*
Even if the evaluation return NULL, seconds is useful for pruning
*/
return seconds;
}
longlong Item_func_to_seconds::val_int()
......@@ -956,10 +977,10 @@ longlong Item_func_to_seconds::val_int()
longlong days;
if (get_arg0_date(&ltime, TIME_NO_ZERO_DATE))
return 0;
seconds=ltime.hour*3600L+ltime.minute*60+ltime.second;
seconds= ltime.hour * 3600L + ltime.minute * 60 + ltime.second;
seconds=ltime.neg ? -seconds : seconds;
days= (longlong) calc_daynr(ltime.year,ltime.month,ltime.day);
return (seconds + days * (24L * 3600L));
days= (longlong) calc_daynr(ltime.year, ltime.month, ltime.day);
return seconds + days * 24L * 3600L;
}
/*
......@@ -992,10 +1013,9 @@ enum_monotonicity_info Item_func_to_seconds::get_monotonicity_info() const
{
if (args[0]->type() == Item::FIELD_ITEM)
{
if (args[0]->field_type() == MYSQL_TYPE_DATE)
return MONOTONIC_STRICT_INCREASING;
if (args[0]->field_type() == MYSQL_TYPE_DATETIME)
return MONOTONIC_INCREASING;
if (args[0]->field_type() == MYSQL_TYPE_DATE ||
args[0]->field_type() == MYSQL_TYPE_DATETIME)
return MONOTONIC_STRICT_INCREASING_NOT_NULL;
}
return NON_MONOTONIC;
}
......
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