Commit 316847ea authored by Alexander Barkov's avatar Alexander Barkov

MDEV-27101 Subquery using the ALL keyword on TIMESTAMP columns produces a wrong result

TIMESTAMP columns were compared as strings in ALL/ANY comparison,
which did not work well near DST time change.

Changing ALL/ANY comparison to use "Native" representation to compare
TIMESTAMP columns, like simple comparison does.
parent 36d173e5
...@@ -654,3 +654,25 @@ SET time_zone=DEFAULT; ...@@ -654,3 +654,25 @@ SET time_zone=DEFAULT;
# #
# End of 10.4 tests # End of 10.4 tests
# #
#
# MDEV-27101 Subquery using the ALL keyword on TIMESTAMP columns produces a wrong result
#
SET time_zone='Europe/Moscow';
CREATE TABLE t1 (a TIMESTAMP NULL);
SET timestamp=1288477526;
/* this is summer time, earlier */
INSERT INTO t1 VALUES (NOW());
SET timestamp=1288477526+3599;
/* this is winter time, later */
INSERT INTO t1 VALUES (NOW());
SELECT a, UNIX_TIMESTAMP(a) FROM t1 ORDER BY a;
a UNIX_TIMESTAMP(a)
2010-10-31 02:25:26 1288477526
2010-10-31 02:25:25 1288481125
SELECT a, UNIX_TIMESTAMP(a) FROM t1 WHERE a <= ALL (SELECT * FROM t1);
a UNIX_TIMESTAMP(a)
2010-10-31 02:25:26 1288477526
SELECT a, UNIX_TIMESTAMP(a) FROM t1 WHERE a >= ALL (SELECT * FROM t1);
a UNIX_TIMESTAMP(a)
2010-10-31 02:25:25 1288481125
DROP TABLE t1;
...@@ -598,3 +598,18 @@ SET time_zone=DEFAULT; ...@@ -598,3 +598,18 @@ SET time_zone=DEFAULT;
--echo # --echo #
--echo # End of 10.4 tests --echo # End of 10.4 tests
--echo # --echo #
--echo #
--echo # MDEV-27101 Subquery using the ALL keyword on TIMESTAMP columns produces a wrong result
--echo #
SET time_zone='Europe/Moscow';
CREATE TABLE t1 (a TIMESTAMP NULL);
SET timestamp=1288477526; /* this is summer time, earlier */
INSERT INTO t1 VALUES (NOW());
SET timestamp=1288477526+3599; /* this is winter time, later */
INSERT INTO t1 VALUES (NOW());
SELECT a, UNIX_TIMESTAMP(a) FROM t1 ORDER BY a;
SELECT a, UNIX_TIMESTAMP(a) FROM t1 WHERE a <= ALL (SELECT * FROM t1);
SELECT a, UNIX_TIMESTAMP(a) FROM t1 WHERE a >= ALL (SELECT * FROM t1);
DROP TABLE t1;
...@@ -3698,6 +3698,41 @@ void select_max_min_finder_subselect::cleanup() ...@@ -3698,6 +3698,41 @@ void select_max_min_finder_subselect::cleanup()
} }
void select_max_min_finder_subselect::set_op(const Type_handler *th)
{
if (th->is_val_native_ready())
{
op= &select_max_min_finder_subselect::cmp_native;
return;
}
switch (th->cmp_type()) {
case REAL_RESULT:
op= &select_max_min_finder_subselect::cmp_real;
break;
case INT_RESULT:
op= &select_max_min_finder_subselect::cmp_int;
break;
case STRING_RESULT:
op= &select_max_min_finder_subselect::cmp_str;
break;
case DECIMAL_RESULT:
op= &select_max_min_finder_subselect::cmp_decimal;
break;
case TIME_RESULT:
if (th->field_type() == MYSQL_TYPE_TIME)
op= &select_max_min_finder_subselect::cmp_time;
else
op= &select_max_min_finder_subselect::cmp_str;
break;
case ROW_RESULT:
// This case should never be chosen
DBUG_ASSERT(0);
op= 0;
}
}
int select_max_min_finder_subselect::send_data(List<Item> &items) int select_max_min_finder_subselect::send_data(List<Item> &items)
{ {
DBUG_ENTER("select_max_min_finder_subselect::send_data"); DBUG_ENTER("select_max_min_finder_subselect::send_data");
...@@ -3716,30 +3751,7 @@ int select_max_min_finder_subselect::send_data(List<Item> &items) ...@@ -3716,30 +3751,7 @@ int select_max_min_finder_subselect::send_data(List<Item> &items)
if (!cache) if (!cache)
{ {
cache= val_item->get_cache(thd); cache= val_item->get_cache(thd);
switch (val_item->cmp_type()) { set_op(val_item->type_handler());
case REAL_RESULT:
op= &select_max_min_finder_subselect::cmp_real;
break;
case INT_RESULT:
op= &select_max_min_finder_subselect::cmp_int;
break;
case STRING_RESULT:
op= &select_max_min_finder_subselect::cmp_str;
break;
case DECIMAL_RESULT:
op= &select_max_min_finder_subselect::cmp_decimal;
break;
case TIME_RESULT:
if (val_item->field_type() == MYSQL_TYPE_TIME)
op= &select_max_min_finder_subselect::cmp_time;
else
op= &select_max_min_finder_subselect::cmp_str;
break;
case ROW_RESULT:
// This case should never be choosen
DBUG_ASSERT(0);
op= 0;
}
} }
cache->store(val_item); cache->store(val_item);
it->store(0, cache); it->store(0, cache);
...@@ -3833,6 +3845,26 @@ bool select_max_min_finder_subselect::cmp_str() ...@@ -3833,6 +3845,26 @@ bool select_max_min_finder_subselect::cmp_str()
return (sortcmp(val1, val2, cache->collation.collation) < 0); return (sortcmp(val1, val2, cache->collation.collation) < 0);
} }
bool select_max_min_finder_subselect::cmp_native()
{
NativeBuffer<STRING_BUFFER_USUAL_SIZE> cvalue, mvalue;
Item *maxmin= ((Item_singlerow_subselect *)item)->element_index(0);
bool cvalue_is_null= cache->val_native(thd, &cvalue);
bool mvalue_is_null= maxmin->val_native(thd, &mvalue);
/* Ignore NULLs for ANY and keep them for ALL subqueries */
if (cvalue_is_null)
return (is_all && !mvalue_is_null) || (!is_all && mvalue_is_null);
if (mvalue_is_null)
return !is_all;
const Type_handler *th= cache->type_handler();
return fmax ? th->cmp_native(cvalue, mvalue) > 0 :
th->cmp_native(cvalue, mvalue) < 0;
}
int select_exists_subselect::send_data(List<Item> &items) int select_exists_subselect::send_data(List<Item> &items)
{ {
DBUG_ENTER("select_exists_subselect::send_data"); DBUG_ENTER("select_exists_subselect::send_data");
......
...@@ -6068,6 +6068,7 @@ class select_max_min_finder_subselect :public select_subselect ...@@ -6068,6 +6068,7 @@ class select_max_min_finder_subselect :public select_subselect
bool (select_max_min_finder_subselect::*op)(); bool (select_max_min_finder_subselect::*op)();
bool fmax; bool fmax;
bool is_all; bool is_all;
void set_op(const Type_handler *ha);
public: public:
select_max_min_finder_subselect(THD *thd_arg, Item_subselect *item_arg, select_max_min_finder_subselect(THD *thd_arg, Item_subselect *item_arg,
bool mx, bool all): bool mx, bool all):
...@@ -6080,6 +6081,7 @@ class select_max_min_finder_subselect :public select_subselect ...@@ -6080,6 +6081,7 @@ class select_max_min_finder_subselect :public select_subselect
bool cmp_decimal(); bool cmp_decimal();
bool cmp_str(); bool cmp_str();
bool cmp_time(); bool cmp_time();
bool cmp_native();
}; };
/* EXISTS subselect interface class */ /* EXISTS subselect interface class */
......
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