Commit 3d9cd36f authored by unknown's avatar unknown

limit clause fixed

parent 807b5085
...@@ -36,4 +36,8 @@ a b ...@@ -36,4 +36,8 @@ a b
1 7 1 7
2 7 2 7
3 8 3 8
select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
(select a from t3 where a<t2.a*4 order by 1 desc limit 1) a
3 1
7 2
drop table t1,t2,t3,t4; drop table t1,t2,t3,t4;
...@@ -14,7 +14,10 @@ select (select a from t1), a from t2; ...@@ -14,7 +14,10 @@ select (select a from t1), a from t2;
select (select a from t3), a from t2; select (select a from t3), a from t2;
select * from t2 where t2.a=(select a from t1); select * from t2 where t2.a=(select a from t1);
insert into t3 values (6),(7),(3); insert into t3 values (6),(7),(3);
select * from t2 where t2.b=(select a from t3 order by 1 limit 1); select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1);
select * from t2 where t2.b=(select a from t3 order by 1 limit 1) select * from t2 where t2.b=(select a from t3 order by 1 desc limit 1)
union (select * from t4 order by a limit 2) limit 3; union (select * from t4 order by a limit 2) limit 3;
select (select a from t3 where a<t2.a*4 order by 1 desc limit 1), a from t2;
select (select t3.a from t3 where a<8 order by 1 desc limit 1), a from
(select * from t2 where a>1) as tt;
drop table t1,t2,t3,t4; drop table t1,t2,t3,t4;
...@@ -24,7 +24,6 @@ SUBSELECT TODO: ...@@ -24,7 +24,6 @@ SUBSELECT TODO:
(sql_select.h/sql_select.cc) (sql_select.h/sql_select.cc)
- add subselect union select (sql_union.cc) - add subselect union select (sql_union.cc)
- depended from outer select subselects
*/ */
...@@ -41,6 +40,14 @@ Item_subselect::Item_subselect(THD *thd, st_select_lex *select_lex): ...@@ -41,6 +40,14 @@ Item_subselect::Item_subselect(THD *thd, st_select_lex *select_lex):
DBUG_ENTER("Item_subselect::Item_subselect"); DBUG_ENTER("Item_subselect::Item_subselect");
DBUG_PRINT("subs", ("select_lex 0x%xl", (long) select_lex)); DBUG_PRINT("subs", ("select_lex 0x%xl", (long) select_lex));
result= new select_subselect(this); result= new select_subselect(this);
SELECT_LEX_UNIT *unit= select_lex->master_unit();
unit->offset_limit_cnt= unit->global_parameters->offset_limit;
unit->select_limit_cnt= unit->global_parameters->select_limit+
select_lex->offset_limit;
if (unit->select_limit_cnt < unit->global_parameters->select_limit)
unit->select_limit_cnt= HA_POS_ERROR; // no limit
if (unit->select_limit_cnt == HA_POS_ERROR)
select_lex->options&= ~OPTION_FOUND_ROWS;
join= new JOIN(thd, select_lex->item_list, select_lex->options, result); join= new JOIN(thd, select_lex->item_list, select_lex->options, result);
this->select_lex= select_lex; this->select_lex= select_lex;
maybe_null= 1; maybe_null= 1;
...@@ -141,9 +148,9 @@ int Item_subselect::exec() ...@@ -141,9 +148,9 @@ int Item_subselect::exec()
join->thd->lex.select= select_lex; join->thd->lex.select= select_lex;
join->exec(); join->exec();
join->thd->lex.select= save_select; join->thd->lex.select= save_select;
if (!executed) //if (!executed)
//No rows returned => value is null (returned as inited) //No rows returned => value is null (returned as inited)
executed= 1; // executed= 1;
return join->error; return join->error;
} }
return 0; return 0;
......
...@@ -784,9 +784,15 @@ select_subselect::select_subselect(Item_subselect *item) ...@@ -784,9 +784,15 @@ select_subselect::select_subselect(Item_subselect *item)
bool select_subselect::send_data(List<Item> &items) bool select_subselect::send_data(List<Item> &items)
{ {
DBUG_ENTER("select_subselect::send_data");
if (item->executed){ if (item->executed){
my_printf_error(ER_SUBSELECT_NO_1_ROW, ER(ER_SUBSELECT_NO_1_ROW), MYF(0)); my_printf_error(ER_SUBSELECT_NO_1_ROW, ER(ER_SUBSELECT_NO_1_ROW), MYF(0));
return 1; DBUG_RETURN(1);
}
if (unit->offset_limit_cnt)
{ // using limit offset,count
unit->offset_limit_cnt--;
DBUG_RETURN(0);
} }
Item *val_item= (Item *)item->select_lex->item_list.head(); Item *val_item= (Item *)item->select_lex->item_list.head();
if ((item->null_value= val_item->is_null())) if ((item->null_value= val_item->is_null()))
...@@ -801,5 +807,6 @@ bool select_subselect::send_data(List<Item> &items) ...@@ -801,5 +807,6 @@ bool select_subselect::send_data(List<Item> &items)
item->real_value= val_item->val(); item->real_value= val_item->val();
item->res_type= val_item->result_type(); item->res_type= val_item->result_type();
} }
return 0; item->executed= 1;
DBUG_RETURN(0);
} }
...@@ -1275,9 +1275,10 @@ mysql_execute_command(void) ...@@ -1275,9 +1275,10 @@ mysql_execute_command(void)
break; // Error message is given break; // Error message is given
} }
unit->offset_limit_cnt =select_lex->offset_limit; unit->offset_limit_cnt= unit->global_parameters->offset_limit;
unit->select_limit_cnt =select_lex->select_limit+select_lex->offset_limit; unit->select_limit_cnt= unit->global_parameters->select_limit+
if (unit->select_limit_cnt < select_lex->select_limit) unit->global_parameters->offset_limit;
if (unit->select_limit_cnt < unit->global_parameters->select_limit)
unit->select_limit_cnt= HA_POS_ERROR; // no limit unit->select_limit_cnt= HA_POS_ERROR; // no limit
if (unit->select_limit_cnt == HA_POS_ERROR) if (unit->select_limit_cnt == HA_POS_ERROR)
select_lex->options&= ~OPTION_FOUND_ROWS; select_lex->options&= ~OPTION_FOUND_ROWS;
...@@ -2672,7 +2673,7 @@ mysql_init_query(THD *thd) ...@@ -2672,7 +2673,7 @@ mysql_init_query(THD *thd)
thd->lex.unit.init_select(); thd->lex.unit.init_select();
thd->lex.select_lex.init_query(); thd->lex.select_lex.init_query();
thd->lex.unit.slave= &thd->lex.select_lex; thd->lex.unit.slave= &thd->lex.select_lex;
thd->lex.unit.select_limit= thd->default_select_limit; //Global limit thd->lex.unit.global_parameters= &thd->lex.select_lex; //Global limit & order
thd->lex.select_lex.master= &thd->lex.unit; thd->lex.select_lex.master= &thd->lex.unit;
thd->lex.select_lex.prev= &thd->lex.unit.slave; thd->lex.select_lex.prev= &thd->lex.unit.slave;
thd->lex.value_list.empty(); thd->lex.value_list.empty();
......
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