Skip to content

Commit 9630eda

Browse files
committed
MDEV-9390 Function found_rows() gives incorrect result where the previous SELECT contains ORDER BY clause
use the raw found_rows value only when SQL_CALC_FOUND_ROWS was specified
1 parent 38b89a6 commit 9630eda

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

mysql-test/r/select_found.result

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,18 @@ select found_rows();
348348
found_rows()
349349
75
350350
drop table t1;
351+
create table t1(c1 int);
352+
insert into t1 values(1),(2),(3),(4),(5);
353+
select * from t1 order by c1 limit 2,1;
354+
c1
355+
3
356+
select found_rows();
357+
found_rows()
358+
3
359+
select sql_calc_found_rows * from t1 order by c1 limit 2,1;
360+
c1
361+
3
362+
select found_rows();
363+
found_rows()
364+
5
365+
drop table t1;

mysql-test/t/select_found.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,13 @@ select sql_calc_found_rows * from t1 ignore index (i) where i = 0 order by v lim
277277
select found_rows();
278278
drop table t1;
279279

280+
#
281+
# MDEV-9390 Function found_rows() gives incorrect result where the previous SELECT contains ORDER BY clause
282+
#
283+
create table t1(c1 int);
284+
insert into t1 values(1),(2),(3),(4),(5);
285+
select * from t1 order by c1 limit 2,1;
286+
select found_rows();
287+
select sql_calc_found_rows * from t1 order by c1 limit 2,1;
288+
select found_rows();
289+
drop table t1;

sql/sql_select.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20813,7 +20813,7 @@ create_sort_index(THD *thd, JOIN *join, ORDER *order,
2081320813
select, filesort_limit, 0,
2081420814
&examined_rows, &found_rows);
2081520815
table->sort.found_records= filesort_retval;
20816-
tab->records= found_rows; // For SQL_CALC_ROWS
20816+
tab->records= join->select_options & OPTION_FOUND_ROWS ? found_rows : filesort_retval;
2081720817

2081820818
if (quick_created)
2081920819
{

0 commit comments

Comments
 (0)