Commit 10fedf67 authored by Sergei Golubchik's avatar Sergei Golubchik

change test_if_equality_guarantees_uniqueness()

from an ad hoc set of limitations
to a correct rule
parent fdfeb4be
......@@ -804,6 +804,32 @@ select distinct a from t1 where a = DATE('2010-10-10');
a
2010-10-10
20101010
explain select distinct a from t1 where a = DATE('2010-10-10');
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using where; Using temporary
drop table t1;
# date = string
create table t1 (a date);
insert t1 values ('2010-10-10'), ('20101010');
explain select distinct a from t1 where a = '2010-10-10';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using where
drop table t1;
# double = string
create table t1 (a double);
insert t1 values (2), (2);
explain select distinct a from t1 where a = '2';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using where
# double = int
explain select distinct a from t1 where a = 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using where
# string = double
alter table t1 modify a varchar(100);
explain select distinct a from t1 where a = 2e0;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using where; Using temporary
drop table t1;
create table t1 (f1 varchar(40));
insert into t1 values ('2010-10-10 00:00:00.0001'),('2010-10-10 00:00:00.0002'),('2010-10-10 00:00:00.0003');
......
......@@ -622,6 +622,25 @@ create table t1 (a varchar(100));
insert t1 values ('2010-10-10'), ('20101010');
select * from t1 where a = DATE('2010-10-10');
select distinct a from t1 where a = DATE('2010-10-10');
explain select distinct a from t1 where a = DATE('2010-10-10');
drop table t1;
#
# test_if_equality_guarantees_uniqueness() and different type combinations
#
--echo # date = string
create table t1 (a date);
insert t1 values ('2010-10-10'), ('20101010');
explain select distinct a from t1 where a = '2010-10-10';
drop table t1;
--echo # double = string
create table t1 (a double);
insert t1 values (2), (2);
explain select distinct a from t1 where a = '2';
--echo # double = int
explain select distinct a from t1 where a = 2;
--echo # string = double
alter table t1 modify a varchar(100);
explain select distinct a from t1 where a = 2e0;
drop table t1;
#
......
......@@ -12171,15 +12171,15 @@ remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value)
Checks if an equality predicate can be used to take away
DISTINCT/GROUP BY because it is known to be true for exactly one
distinct value (e.g. <expr> == <const>).
Arguments must be of the same type because e.g.
<string_field> = <int_const> may match more than 1 distinct value from
the column.
Additionally, strings must have the same collation.
Or the *field* must be a datetime - if the constant is a datetime
and a field is not - this is not enough, consider:
create table t1 (a varchar(100));
insert t1 values ('2010-01-02'), ('2010-1-2'), ('20100102');
select distinct t1 from t1 where a=date('2010-01-02');
Arguments must be compared in the native type of the left argument
and (for strings) in the native collation of the left argument.
Otherwise, for example,
<string_field> = <int_const> may match more than 1 distinct value or
the <string_field>.
@note We don't need to aggregate l and r collations here, because r -
the constant item - has already been converted to a proper collation
for comparison. We only need to compare this collation with field's collation.
@retval true can be used
@retval false cannot be used
......@@ -12188,13 +12188,9 @@ static bool
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
{
return r->const_item() &&
/* the field is a date (the const will be converted to a date) */
(l->cmp_type() == TIME_RESULT ||
/* or arguments are of the same result type */
(r->result_type() == l->result_type() &&
/* and must have the same collation if compared as strings */
(l->result_type() != STRING_RESULT ||
l->collation.collation == r->collation.collation)));
item_cmp_type(l->cmp_type(), r->cmp_type()) == l->cmp_type() &&
(l->cmp_type() != STRING_RESULT ||
l->collation.collation == r->collation.collation);
}
/**
......
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