sql_buffer_result=1 gives wrong result for GROUP BY with a constant expression

Bug #780425 reported by Michael Widenius
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
MariaDB
Fix Released
Medium
Michael Widenius

Bug Description

This fails in MySQL 5.1, MariaDB 5.1 and above:

drop table if exists t1;
CREATE TABLE t1(f1 DECIMAL(5,1) UNSIGNED);
INSERT INTO t1 VALUES (0.2),(0.1);
set sql_buffer_result=0;
SELECT 1 as 'one' FROM t1 GROUP BY @a:= (SELECT ROUND(f1) FROM t1 WHERE f1 = 0);
set sql_buffer_result=1;
SELECT 1 as 'one' FROM t1 GROUP BY @a:= (SELECT ROUND(f1) FROM t1 WHERE f1 = 0);

The second query return two rows, the first one only one row (which is correct).

Tags: by group subquery
Michael Widenius (monty)
Changed in maria:
assignee: nobody → Oleksandr "Sanja" Byelkin (sanja-byelkin)
importance: Undecided → Medium
milestone: none → 5.1
Changed in maria:
status: New → In Progress
Revision history for this message
Oleksandr "Sanja" Byelkin (sanja-byelkin) wrote :

A bit simplified test suite:

CREATE TABLE t1(f1 int UNSIGNED);
INSERT INTO t1 VALUES (2),(1);
set sql_buffer_result=0;
SELECT 1 as 'one' FROM t1 GROUP BY (SELECT f1 FROM t1 WHERE f1 = 0);
set sql_buffer_result=1;
SELECT 1 as 'one' FROM t1 GROUP BY (SELECT f1 FROM t1 WHERE f1 = 0);
drop table t1;

Revision history for this message
Oleksandr "Sanja" Byelkin (sanja-byelkin) wrote :

Number of rows in the secind case depends in number of rows in the table:
CREATE TABLE t1(f1 int UNSIGNED);
INSERT INTO t1 VALUES (3),(2),(1);
set sql_buffer_result=0;
SELECT 1 as 'one' FROM t1 GROUP BY (SELECT f1 FROM t1 WHERE f1 = 0);
one
1
set sql_buffer_result=1;
SELECT 1 as 'one' FROM t1 GROUP BY (SELECT f1 FROM t1 WHERE f1 = 0);
one
1
1
1

Revision history for this message
Oleksandr "Sanja" Byelkin (sanja-byelkin) wrote :

end_write set instead of end_write_group.

Revision history for this message
Oleksandr "Sanja" Byelkin (sanja-byelkin) wrote :

The problem has nothing related to subquery, just constant groupping:

SELECT 1 as 'one' FROM t1 GROUP BY '123' = 'abc';
one
1
1
1

summary: - sql_buffer_result=1 gives wrong result for GROUP BY with subquery
+ sql_buffer_result=1 gives wrong result for GROUP BY with a constant
+ expression
Revision history for this message
Oleksandr "Sanja" Byelkin (sanja-byelkin) wrote :

Current minimal test suite is:

CREATE TABLE t1(f1 int UNSIGNED);
INSERT INTO t1 VALUES (3),(2),(1);
set sql_buffer_result=0;
SELECT 1 as 'one' FROM t1 GROUP BY '123' = 'abc';
set sql_buffer_result=1;
SELECT 1 as 'one' FROM t1 GROUP BY '123' = 'abc';
drop table t1;

Revision history for this message
Oleksandr "Sanja" Byelkin (sanja-byelkin) wrote :

join->sort_and_group set to false (which is incorrect)

Revision history for this message
Oleksandr "Sanja" Byelkin (sanja-byelkin) wrote :

in JOIN::exec in if(need_tmp) branch make_simple_join() drops JOIN::group flag (wich was the only trace (after reducing group list by optimizer) that we need grouping).

For query with no sql_buffer_result set need_tmp is also unset so we have no such problem.

Changed in maria:
assignee: Oleksandr "Sanja" Byelkin (sanja-byelkin) → Michael Widenius (monty)
Revision history for this message
Michael Widenius (monty) wrote :

Fix pushed into MariaDB 5.2
(Very old MySQL bug, should not be critical for MariaDB 5.1)

Changed in maria:
milestone: 5.1 → 5.2
status: In Progress → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.