Bug #27659:

The optimizer transforms DISTINCT into a GROUP BY
when possible.
It does that by constructing the same structure
(a list of ORDER instances) the parser makes when
parsing GROUP BY.
While doing that it also eliminates duplicates.
But if a duplicate is found it doesn't advance the
pointer to ref_pointer array, so the next 
(and subsequent) ORDER structures point to the wrong
element in the SELECT list.
Fixed by advancing the pointer in ref_pointer_array
even in the case of a duplicate.
parent 204559cd
...@@ -668,3 +668,17 @@ NULL ...@@ -668,3 +668,17 @@ NULL
3 3
4 4
DROP TABLE t1; DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT);
INSERT INTO t1 VALUES(1,1),(1,2),(1,3);
SELECT DISTINCT a, b FROM t1;
a b
1 1
1 2
1 3
SELECT DISTINCT a, a, b FROM t1;
a a b
1 1 1
1 1 2
1 1 3
DROP TABLE t1;
End of 5.0 tests
...@@ -540,3 +540,16 @@ EXPLAIN SELECT a FROM t1 GROUP BY a; ...@@ -540,3 +540,16 @@ EXPLAIN SELECT a FROM t1 GROUP BY a;
SELECT a FROM t1 GROUP BY a; SELECT a FROM t1 GROUP BY a;
DROP TABLE t1; DROP TABLE t1;
#
#Bug #27659: SELECT DISTINCT returns incorrect result set when field is
#repeated
#
#
CREATE TABLE t1 (a INT, b INT);
INSERT INTO t1 VALUES(1,1),(1,2),(1,3);
SELECT DISTINCT a, b FROM t1;
SELECT DISTINCT a, a, b FROM t1;
DROP TABLE t1;
--echo End of 5.0 tests
...@@ -13539,9 +13539,7 @@ create_distinct_group(THD *thd, Item **ref_pointer_array, ...@@ -13539,9 +13539,7 @@ create_distinct_group(THD *thd, Item **ref_pointer_array,
ORDER *ord_iter; ORDER *ord_iter;
for (ord_iter= group; ord_iter; ord_iter= ord_iter->next) for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
if ((*ord_iter->item)->eq(item, 1)) if ((*ord_iter->item)->eq(item, 1))
break; goto next_item;
if (ord_iter)
continue;
ORDER *ord=(ORDER*) thd->calloc(sizeof(ORDER)); ORDER *ord=(ORDER*) thd->calloc(sizeof(ORDER));
if (!ord) if (!ord)
...@@ -13556,6 +13554,7 @@ create_distinct_group(THD *thd, Item **ref_pointer_array, ...@@ -13556,6 +13554,7 @@ create_distinct_group(THD *thd, Item **ref_pointer_array,
*prev=ord; *prev=ord;
prev= &ord->next; prev= &ord->next;
} }
next_item:
ref_pointer_array++; ref_pointer_array++;
} }
*prev=0; *prev=0;
......
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