Loading mysql-test/r/distinct.result +25 −1 Original line number Diff line number Diff line Loading @@ -530,7 +530,8 @@ id select_type table type possible_keys key key_len ref rows Extra EXPLAIN SELECT DISTINCT a,b FROM t1 GROUP BY a,b; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 ALL NULL NULL NULL NULL 3 CREATE TABLE t2(a INT, b INT, c INT, d INT, PRIMARY KEY (a,b)); CREATE TABLE t2(a INT, b INT NOT NULL, c INT NOT NULL, d INT, PRIMARY KEY (a,b)); INSERT INTO t2 VALUES (1,1,1,50), (1,2,3,40), (2,1,3,4); EXPLAIN SELECT DISTINCT a FROM t2; id select_type table type possible_keys key key_len ref rows Extra Loading Loading @@ -644,3 +645,26 @@ SELECT COUNT(*) FROM COUNT(*) 2 DROP TABLE t1, t2; CREATE TABLE t1 (a INT, UNIQUE (a)); INSERT INTO t1 VALUES (4),(null),(2),(1),(null),(3); EXPLAIN SELECT DISTINCT a FROM t1; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 index NULL a 5 NULL 6 Using index SELECT DISTINCT a FROM t1; a NULL 1 2 3 4 EXPLAIN SELECT a FROM t1 GROUP BY a; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 index NULL a 5 NULL 6 Using index SELECT a FROM t1 GROUP BY a; a NULL 1 2 3 4 DROP TABLE t1; mysql-test/t/distinct.test +16 −1 Original line number Diff line number Diff line Loading @@ -364,7 +364,8 @@ EXPLAIN SELECT a FROM t1 GROUP BY a; EXPLAIN SELECT a,b FROM t1 GROUP BY a,b; EXPLAIN SELECT DISTINCT a,b FROM t1 GROUP BY a,b; CREATE TABLE t2(a INT, b INT, c INT, d INT, PRIMARY KEY (a,b)); CREATE TABLE t2(a INT, b INT NOT NULL, c INT NOT NULL, d INT, PRIMARY KEY (a,b)); INSERT INTO t2 VALUES (1,1,1,50), (1,2,3,40), (2,1,3,4); EXPLAIN SELECT DISTINCT a FROM t2; EXPLAIN SELECT DISTINCT a,a FROM t2; Loading Loading @@ -525,3 +526,17 @@ SELECT COUNT(*) FROM (SELECT DISTINCT a FROM t2 WHERE a='oe' COLLATE latin1_german2_ci) dt; DROP TABLE t1, t2; # # Bug #25551: inconsistent behaviour in grouping NULL, depending on index type # CREATE TABLE t1 (a INT, UNIQUE (a)); INSERT INTO t1 VALUES (4),(null),(2),(1),(null),(3); EXPLAIN SELECT DISTINCT a FROM t1; #result must have one row with NULL SELECT DISTINCT a FROM t1; EXPLAIN SELECT a FROM t1 GROUP BY a; #result must have one row with NULL SELECT a FROM t1 GROUP BY a; DROP TABLE t1; sql/sql_select.cc +11 −8 Original line number Diff line number Diff line Loading @@ -873,10 +873,11 @@ JOIN::optimize() } /* Check if we can optimize away GROUP BY/DISTINCT. We can do that if there are no aggregate functions and the We can do that if there are no aggregate functions, the fields in DISTINCT clause (if present) and/or columns in GROUP BY (if present) contain direct references to all key parts of an unique index (in whatever order). an unique index (in whatever order) and if the key parts of the unique index cannot contain NULLs. Note that the unique keys for DISTINCT and GROUP BY should not be the same (as long as they are unique). Loading Loading @@ -11918,7 +11919,8 @@ test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts, of the table are referenced by a list : either the select list through find_field_in_item_list or GROUP BY list through find_field_in_order_list. If the above holds then we can safely remove the GROUP BY/DISTINCT, If the above holds and the key parts cannot contain NULLs then we can safely remove the GROUP BY/DISTINCT, as no result set can be more distinct than an unique key. RETURN VALUE Loading @@ -11943,7 +11945,8 @@ list_contains_unique_index(TABLE *table, key_part < key_part_end; key_part++) { if (!find_func(key_part->field, data)) if (key_part->field->maybe_null() || !find_func(key_part->field, data)) break; } if (key_part == key_part_end) Loading Loading
mysql-test/r/distinct.result +25 −1 Original line number Diff line number Diff line Loading @@ -530,7 +530,8 @@ id select_type table type possible_keys key key_len ref rows Extra EXPLAIN SELECT DISTINCT a,b FROM t1 GROUP BY a,b; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 ALL NULL NULL NULL NULL 3 CREATE TABLE t2(a INT, b INT, c INT, d INT, PRIMARY KEY (a,b)); CREATE TABLE t2(a INT, b INT NOT NULL, c INT NOT NULL, d INT, PRIMARY KEY (a,b)); INSERT INTO t2 VALUES (1,1,1,50), (1,2,3,40), (2,1,3,4); EXPLAIN SELECT DISTINCT a FROM t2; id select_type table type possible_keys key key_len ref rows Extra Loading Loading @@ -644,3 +645,26 @@ SELECT COUNT(*) FROM COUNT(*) 2 DROP TABLE t1, t2; CREATE TABLE t1 (a INT, UNIQUE (a)); INSERT INTO t1 VALUES (4),(null),(2),(1),(null),(3); EXPLAIN SELECT DISTINCT a FROM t1; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 index NULL a 5 NULL 6 Using index SELECT DISTINCT a FROM t1; a NULL 1 2 3 4 EXPLAIN SELECT a FROM t1 GROUP BY a; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 index NULL a 5 NULL 6 Using index SELECT a FROM t1 GROUP BY a; a NULL 1 2 3 4 DROP TABLE t1;
mysql-test/t/distinct.test +16 −1 Original line number Diff line number Diff line Loading @@ -364,7 +364,8 @@ EXPLAIN SELECT a FROM t1 GROUP BY a; EXPLAIN SELECT a,b FROM t1 GROUP BY a,b; EXPLAIN SELECT DISTINCT a,b FROM t1 GROUP BY a,b; CREATE TABLE t2(a INT, b INT, c INT, d INT, PRIMARY KEY (a,b)); CREATE TABLE t2(a INT, b INT NOT NULL, c INT NOT NULL, d INT, PRIMARY KEY (a,b)); INSERT INTO t2 VALUES (1,1,1,50), (1,2,3,40), (2,1,3,4); EXPLAIN SELECT DISTINCT a FROM t2; EXPLAIN SELECT DISTINCT a,a FROM t2; Loading Loading @@ -525,3 +526,17 @@ SELECT COUNT(*) FROM (SELECT DISTINCT a FROM t2 WHERE a='oe' COLLATE latin1_german2_ci) dt; DROP TABLE t1, t2; # # Bug #25551: inconsistent behaviour in grouping NULL, depending on index type # CREATE TABLE t1 (a INT, UNIQUE (a)); INSERT INTO t1 VALUES (4),(null),(2),(1),(null),(3); EXPLAIN SELECT DISTINCT a FROM t1; #result must have one row with NULL SELECT DISTINCT a FROM t1; EXPLAIN SELECT a FROM t1 GROUP BY a; #result must have one row with NULL SELECT a FROM t1 GROUP BY a; DROP TABLE t1;
sql/sql_select.cc +11 −8 Original line number Diff line number Diff line Loading @@ -873,10 +873,11 @@ JOIN::optimize() } /* Check if we can optimize away GROUP BY/DISTINCT. We can do that if there are no aggregate functions and the We can do that if there are no aggregate functions, the fields in DISTINCT clause (if present) and/or columns in GROUP BY (if present) contain direct references to all key parts of an unique index (in whatever order). an unique index (in whatever order) and if the key parts of the unique index cannot contain NULLs. Note that the unique keys for DISTINCT and GROUP BY should not be the same (as long as they are unique). Loading Loading @@ -11918,7 +11919,8 @@ test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts, of the table are referenced by a list : either the select list through find_field_in_item_list or GROUP BY list through find_field_in_order_list. If the above holds then we can safely remove the GROUP BY/DISTINCT, If the above holds and the key parts cannot contain NULLs then we can safely remove the GROUP BY/DISTINCT, as no result set can be more distinct than an unique key. RETURN VALUE Loading @@ -11943,7 +11945,8 @@ list_contains_unique_index(TABLE *table, key_part < key_part_end; key_part++) { if (!find_func(key_part->field, data)) if (key_part->field->maybe_null() || !find_func(key_part->field, data)) break; } if (key_part == key_part_end) Loading