Skip to content

Commit cbb90f7

Browse files
committed
MDEV-18479 Complement
This patch complements the patch that fixes bug MDEV-18479. This patch takes care of possible overflow when calculating the estimated number of rows in a materialized derived table / view.
1 parent eb09580 commit cbb90f7

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed

include/my_base.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ typedef ulong ha_rows;
586586

587587
#define HA_POS_ERROR (~ (ha_rows) 0)
588588
#define HA_OFFSET_ERROR (~ (my_off_t) 0)
589+
#define HA_ROWS_MAX HA_POS_ERROR
589590

590591
#if SYSTEM_SIZEOF_OFF_T == 4
591592
#define MAX_FILE_SIZE INT_MAX32

mysql-test/r/derived_view.result

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,7 +2641,7 @@ DROP TABLE t1, t2;
26412641
set optimizer_switch=@exit_optimizer_switch;
26422642
set join_cache_level=@exit_join_cache_level;
26432643
#
2644-
# Bug mdev-12812: EXPLAIN for query with many expensive derived
2644+
# Bug mdev-18479: EXPLAIN for query with many expensive derived
26452645
#
26462646
CREATE TABLE t1
26472647
(id int auto_increment primary key,
@@ -2942,15 +2942,15 @@ id select_type table type possible_keys key key_len ref rows Extra
29422942
1 SIMPLE p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join)
29432943
1 SIMPLE <derived17> ALL NULL NULL NULL NULL 50328437500000 Using where; Using join buffer (incremental, BNL join)
29442944
1 SIMPLE <derived14> ALL NULL NULL NULL NULL 27680640625000000 Using where; Using join buffer (incremental, BNL join)
2945-
1 SIMPLE <derived7> ALL NULL NULL NULL NULL 7798774269472204288 Using where; Using join buffer (incremental, BNL join)
2946-
1 SIMPLE <derived8> ALL NULL NULL NULL NULL 7798774269472204288 Using where; Using join buffer (incremental, BNL join)
2947-
1 SIMPLE <derived9> ALL NULL NULL NULL NULL -3222391729959551616 Using where; Using join buffer (incremental, BNL join)
2948-
1 SIMPLE <derived10> ALL NULL NULL NULL NULL -3222391729959551616 Using where; Using join buffer (incremental, BNL join)
2949-
1 SIMPLE <derived11> ALL NULL NULL NULL NULL -3222391729959551616 Using where; Using join buffer (incremental, BNL join)
2950-
1 SIMPLE <derived12> ALL NULL NULL NULL NULL -3222391729959551616 Using where; Using join buffer (incremental, BNL join)
2951-
1 SIMPLE <derived13> ALL NULL NULL NULL NULL -3222391729959551616 Using where; Using join buffer (incremental, BNL join)
2952-
1 SIMPLE <derived15> ALL NULL NULL NULL NULL -3222391729959551616 Using where; Using join buffer (incremental, BNL join)
2953-
1 SIMPLE <derived16> ALL NULL NULL NULL NULL -3222391729959551616 Using where; Using join buffer (incremental, BNL join)
2945+
1 SIMPLE <derived9> ALL NULL NULL NULL NULL 9223372036854775807 Using where; Using join buffer (incremental, BNL join)
2946+
1 SIMPLE <derived10> ALL NULL NULL NULL NULL 9223372036854775807 Using where; Using join buffer (incremental, BNL join)
2947+
1 SIMPLE <derived11> ALL NULL NULL NULL NULL 9223372036854775807 Using where; Using join buffer (incremental, BNL join)
2948+
1 SIMPLE <derived12> ALL NULL NULL NULL NULL 9223372036854775807 Using where; Using join buffer (incremental, BNL join)
2949+
1 SIMPLE <derived13> ALL NULL NULL NULL NULL 9223372036854775807 Using where; Using join buffer (incremental, BNL join)
2950+
1 SIMPLE <derived15> ALL NULL NULL NULL NULL 9223372036854775807 Using where; Using join buffer (incremental, BNL join)
2951+
1 SIMPLE <derived16> ALL NULL NULL NULL NULL 9223372036854775807 Using where; Using join buffer (incremental, BNL join)
2952+
1 SIMPLE <derived7> ALL NULL NULL NULL NULL 9223372036854775807 Using where; Using join buffer (incremental, BNL join)
2953+
1 SIMPLE <derived8> ALL NULL NULL NULL NULL 9223372036854775807 Using where; Using join buffer (incremental, BNL join)
29542954
17 DERIVED t2 system NULL NULL NULL NULL 1
29552955
17 DERIVED p4 ALL NULL NULL NULL NULL 550 Using where
29562956
17 DERIVED p5 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (flat, BNL join)

mysql-test/t/derived_view.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1936,7 +1936,7 @@ set optimizer_switch=@exit_optimizer_switch;
19361936
set join_cache_level=@exit_join_cache_level;
19371937

19381938
--echo #
1939-
--echo # Bug mdev-12812: EXPLAIN for query with many expensive derived
1939+
--echo # Bug mdev-18479: EXPLAIN for query with many expensive derived
19401940
--echo #
19411941

19421942
CREATE TABLE t1

sql/sql_lex.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4100,7 +4100,10 @@ void SELECT_LEX::increase_derived_records(ha_rows records)
41004100
DBUG_ASSERT(unit->derived);
41014101

41024102
select_union *result= (select_union*)unit->result;
4103-
result->records+= records;
4103+
if (HA_ROWS_MAX - records > result->records)
4104+
result->records+= records;
4105+
else
4106+
result->records= HA_ROWS_MAX;
41044107
}
41054108

41064109

sql/sql_select.cc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3830,7 +3830,7 @@ make_join_statistics(JOIN *join, List<TABLE_LIST> &tables_list,
38303830
DBUG_RETURN(TRUE); /* purecov: inspected */
38313831

38323832
{
3833-
ha_rows records= 1;
3833+
double records= 1;
38343834
SELECT_LEX_UNIT *unit= join->select_lex->master_unit();
38353835

38363836
/* Find an optimal join order of the non-constant tables. */
@@ -3855,10 +3855,14 @@ make_join_statistics(JOIN *join, List<TABLE_LIST> &tables_list,
38553855
table/view.
38563856
*/
38573857
for (i= 0; i < join->table_count ; i++)
3858-
records*= join->best_positions[i].records_read ?
3859-
(ha_rows)join->best_positions[i].records_read : 1;
3860-
set_if_smaller(records, unit->select_limit_cnt);
3861-
join->select_lex->increase_derived_records(records);
3858+
{
3859+
records= COST_MULT(records,
3860+
join->best_positions[i].records_read ?
3861+
join->best_positions[i].records_read : 1);
3862+
}
3863+
ha_rows rows= records > HA_ROWS_MAX ? HA_ROWS_MAX : (ha_rows) records;
3864+
set_if_smaller(rows, unit->select_limit_cnt);
3865+
join->select_lex->increase_derived_records(rows);
38623866
}
38633867
}
38643868

@@ -10795,7 +10799,7 @@ ha_rows JOIN_TAB::get_examined_rows()
1079510799
}
1079610800
}
1079710801
else
10798-
examined_rows= (ha_rows) records_read;
10802+
examined_rows= (ha_rows) records_read;
1079910803

1080010804
return examined_rows;
1080110805
}
@@ -22924,8 +22928,9 @@ static void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
2292422928
else
2292522929
{
2292622930
ha_rows examined_rows= tab->get_examined_rows();
22927-
22928-
item_list.push_back(new Item_int((longlong) (ulonglong) examined_rows,
22931+
ha_rows displ_rows= examined_rows;
22932+
set_if_smaller(displ_rows, HA_ROWS_MAX/2);
22933+
item_list.push_back(new Item_int((longlong) (ulonglong) displ_rows,
2292922934
MY_INT64_NUM_DECIMAL_DIGITS));
2293022935

2293122936
/* Add "filtered" field to item_list. */

0 commit comments

Comments
 (0)