Commit ea187cfd authored by bell@sanja.is.com.ua's avatar bell@sanja.is.com.ua

fixed proccesing global LIMIT in last SELECT of UNION

parent 6d62d310
...@@ -1046,8 +1046,9 @@ t1 CREATE TABLE `t1` ( ...@@ -1046,8 +1046,9 @@ t1 CREATE TABLE `t1` (
) TYPE=MyISAM CHARSET=latin1 ) TYPE=MyISAM CHARSET=latin1
drop table t1; drop table t1;
CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a; CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
Warnings: select * from t1;
Warning 1262 Data truncated, NULL supplied to NOT NULL column 'a' at row 1 a
2
SHOW CREATE TABLE t1; SHOW CREATE TABLE t1;
Table Create Table Table Create Table
t1 CREATE TABLE `t1` ( t1 CREATE TABLE `t1` (
......
...@@ -409,7 +409,7 @@ found_rows() ...@@ -409,7 +409,7 @@ found_rows()
SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 2,2; SELECT SQL_CALC_FOUND_ROWS * FROM t1 UNION SELECT * FROM t2 LIMIT 2,2;
a a
3 3
5 4
select found_rows(); select found_rows();
found_rows() found_rows()
6 6
...@@ -423,7 +423,7 @@ found_rows() ...@@ -423,7 +423,7 @@ found_rows()
5 5
SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY a desc LIMIT 1; SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY a desc LIMIT 1;
a a
3 5
(SELECT * FROM t1 ORDER by a) UNION ALL (SELECT * FROM t2 ORDER BY a) ORDER BY A desc LIMIT 4; (SELECT * FROM t1 ORDER by a) UNION ALL (SELECT * FROM t2 ORDER BY a) ORDER BY A desc LIMIT 4;
a a
5 5
......
...@@ -606,6 +606,7 @@ CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a; ...@@ -606,6 +606,7 @@ CREATE TABLE t1 SELECT * FROM (SELECT 1 as a,(SELECT a+0)) a;
SHOW CREATE TABLE t1; SHOW CREATE TABLE t1;
drop table t1; drop table t1;
CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a; CREATE TABLE t1 SELECT (SELECT 1 as a UNION SELECT 1+1 limit 1,1) as a;
select * from t1;
SHOW CREATE TABLE t1; SHOW CREATE TABLE t1;
drop table t1; drop table t1;
......
...@@ -1723,6 +1723,10 @@ mysql_execute_command(THD *thd) ...@@ -1723,6 +1723,10 @@ mysql_execute_command(THD *thd)
break; // Error message is given break; // Error message is given
} }
/*
In case of single SELECT unit->global_parameters points on first SELECT
TODO: move counters to SELECT_LEX
*/
unit->offset_limit_cnt= (ha_rows) unit->global_parameters->offset_limit; unit->offset_limit_cnt= (ha_rows) unit->global_parameters->offset_limit;
unit->select_limit_cnt= (ha_rows) (unit->global_parameters->select_limit+ unit->select_limit_cnt= (ha_rows) (unit->global_parameters->select_limit+
unit->global_parameters->offset_limit); unit->global_parameters->offset_limit);
......
...@@ -261,9 +261,24 @@ int st_select_lex_unit::exec() ...@@ -261,9 +261,24 @@ int st_select_lex_unit::exec()
if (optimized) if (optimized)
res= sl->join->reinit(); res= sl->join->reinit();
else else
{
if (sl != global_parameters)
{ {
offset_limit_cnt= sl->offset_limit; offset_limit_cnt= sl->offset_limit;
select_limit_cnt= sl->select_limit+sl->offset_limit; select_limit_cnt= sl->select_limit+sl->offset_limit;
}
else
{
offset_limit_cnt= 0;
/*
We can't use LIMIT at this stage if we are using ORDER BY for the
whole query
*/
if (sl->order_list.first)
select_limit_cnt= HA_POS_ERROR;
else
select_limit_cnt= sl->select_limit+sl->offset_limit;
}
if (select_limit_cnt < sl->select_limit) if (select_limit_cnt < sl->select_limit)
select_limit_cnt= HA_POS_ERROR; // no limit select_limit_cnt= HA_POS_ERROR; // no limit
...@@ -352,14 +367,11 @@ int st_select_lex_unit::exec() ...@@ -352,14 +367,11 @@ int st_select_lex_unit::exec()
{ {
ulong options= thd->options; ulong options= thd->options;
thd->lex.current_select= fake_select_lex; thd->lex.current_select= fake_select_lex;
if (select_cursor->braces)
{
offset_limit_cnt= global_parameters->offset_limit; offset_limit_cnt= global_parameters->offset_limit;
select_limit_cnt= global_parameters->select_limit + select_limit_cnt= global_parameters->select_limit +
global_parameters->offset_limit; global_parameters->offset_limit;
if (select_limit_cnt < global_parameters->select_limit) if (select_limit_cnt < global_parameters->select_limit)
select_limit_cnt= HA_POS_ERROR; // no limit select_limit_cnt= HA_POS_ERROR; // no limit
}
if (select_limit_cnt == HA_POS_ERROR) if (select_limit_cnt == HA_POS_ERROR)
options&= ~OPTION_FOUND_ROWS; options&= ~OPTION_FOUND_ROWS;
else if (found_rows_for_union && !describe) else if (found_rows_for_union && !describe)
......
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