Skip to content

Commit 2f9555c

Browse files
committed
Removed the parameter from st_select_lex_unit::exec_recursive.
Moved checking whether the limit set for the number of iterations when executing a recursive query has been reached from st_select_lex_unit::exec_recursive to TABLE_LIST::fill_recursive. Changed the name of the system variable max_recursion_level for max_recursive_iterations. Adjusted test cases.
1 parent e20e28b commit 2f9555c

File tree

10 files changed

+40
-34
lines changed

10 files changed

+40
-34
lines changed

mysql-test/r/cte_recursive.result

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ generation name
10861086
1 Mom
10871087
2 Grandpa Bill
10881088
2 Grandma Ann
1089-
set statement max_recursion_level=2 for
1089+
set statement max_recursive_iterations=1 for
10901090
with recursive
10911091
ancestor_ids (id, generation)
10921092
as
@@ -1463,8 +1463,8 @@ drop table folks;
14631463
create table t1(a int);
14641464
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
14651465
explain format=json
1466-
with recursive T as (select a from t1 union select a+10 from T where a < 1000)
1467-
select * from T;
1466+
with recursive t as (select a from t1 union select a+10 from t where a < 1000)
1467+
select * from t;
14681468
EXPLAIN
14691469
{
14701470
"query_block": {
@@ -1499,7 +1499,7 @@ EXPLAIN
14991499
"access_type": "ALL",
15001500
"rows": 10,
15011501
"filtered": 100,
1502-
"attached_condition": "(T.a < 1000)"
1502+
"attached_condition": "(t.a < 1000)"
15031503
}
15041504
}
15051505
}

mysql-test/r/mysqld--help.result

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ The following options may be given as the first argument:
450450
max_allowed_packet instead.
451451
--max-prepared-stmt-count=#
452452
Maximum number of prepared statements in the server
453-
--max-recursion-level[=#]
453+
--max-recursive-iterations[=#]
454454
Maximum number of iterations when executing recursive
455455
queries
456456
--max-relay-log-size=#
@@ -1273,7 +1273,7 @@ max-join-size 18446744073709551615
12731273
max-length-for-sort-data 1024
12741274
max-long-data-size 4194304
12751275
max-prepared-stmt-count 16382
1276-
max-recursion-level 18446744073709551615
1276+
max-recursive-iterations 18446744073709551615
12771277
max-relay-log-size 1073741824
12781278
max-seeks-for-key 18446744073709551615
12791279
max-sort-length 1024

mysql-test/suite/sys_vars/r/sysvars_server_notembedded.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2165,7 +2165,7 @@ NUMERIC_BLOCK_SIZE 1
21652165
ENUM_VALUE_LIST NULL
21662166
READ_ONLY NO
21672167
COMMAND_LINE_ARGUMENT REQUIRED
2168-
VARIABLE_NAME MAX_RECURSION_LEVEL
2168+
VARIABLE_NAME MAX_RECURSIVE_ITERATIONS
21692169
SESSION_VALUE 4294967295
21702170
GLOBAL_VALUE 4294967295
21712171
GLOBAL_VALUE_ORIGIN COMPILE-TIME

mysql-test/t/cte_recursive.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ as
914914
)
915915
select * from ancestors;
916916

917-
set statement max_recursion_level=2 for
917+
set statement max_recursive_iterations=1 for
918918
with recursive
919919
ancestor_ids (id, generation)
920920
as
@@ -1074,8 +1074,8 @@ create table t1(a int);
10741074
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
10751075

10761076
explain format=json
1077-
with recursive T as (select a from t1 union select a+10 from T where a < 1000)
1078-
select * from T;
1077+
with recursive t as (select a from t1 union select a+10 from t where a < 1000)
1078+
select * from t;
10791079

10801080
drop table t1;
10811081

sql/sql_class.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ typedef struct system_variables
558558
ulong max_allowed_packet;
559559
ulong max_error_count;
560560
ulong max_length_for_sort_data;
561-
ulong max_recursion_level;
561+
ulong max_recursive_iterations;
562562
ulong max_sort_length;
563563
ulong max_tmp_tables;
564564
ulong max_insert_delayed_threads;

sql/sql_cte.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define SQL_CTE_INCLUDED
33
#include "sql_list.h"
44
#include "sql_lex.h"
5+
#include "sql_select.h"
56

67
class select_union;
78
struct st_unit_ctxt_elem;
@@ -186,6 +187,8 @@ class With_element : public Sql_alloc
186187

187188
bool instantiate_tmp_tables();
188189

190+
void prepare_for_next_iteration();
191+
189192
friend class With_clause;
190193
};
191194

@@ -356,6 +359,19 @@ bool With_element::all_are_stabilized()
356359
}
357360

358361

362+
inline
363+
void With_element::prepare_for_next_iteration()
364+
{
365+
With_element *with_elem= this;
366+
while ((with_elem= with_elem->get_next_mutually_recursive()) != this)
367+
{
368+
TABLE *rec_table= with_elem->first_rec_table_to_update;
369+
if (rec_table)
370+
rec_table->reginfo.join_tab->preread_init_done= false;
371+
}
372+
}
373+
374+
359375
inline
360376
void st_select_lex_unit::set_with_clause(With_clause *with_cl)
361377
{

sql/sql_derived.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -927,13 +927,18 @@ bool TABLE_LIST::fill_recursive(THD *thd)
927927
bool rc= false;
928928
st_select_lex_unit *unit= get_unit();
929929
if (is_with_table_recursive_reference())
930-
rc= unit->exec_recursive(false);
930+
{
931+
rc= unit->exec_recursive();
932+
}
931933
else
932934
{
933935
rc= with->instantiate_tmp_tables();
934-
while(!rc && !with->all_are_stabilized())
936+
while (!rc && !with->all_are_stabilized())
935937
{
936-
rc= unit->exec_recursive(true);
938+
if (with->level > thd->variables.max_recursive_iterations)
939+
break;
940+
with->prepare_for_next_iteration();
941+
rc= unit->exec_recursive();
937942
}
938943
if (!rc)
939944
{

sql/sql_lex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ class st_select_lex_unit: public st_select_lex_node {
706706
bool prepare(THD *thd, select_result *result, ulong additional_options);
707707
bool optimize();
708708
bool exec();
709-
bool exec_recursive(bool is_driving_recursive);
709+
bool exec_recursive();
710710
bool cleanup();
711711
inline void unclean() { cleaned= 0; }
712712
void reinit_exec_mechanism();

sql/sql_union.cc

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ bool st_select_lex_unit::exec()
11671167

11681168

11691169
// One step of recursive execution
1170-
bool st_select_lex_unit::exec_recursive(bool is_driving_recursive)
1170+
bool st_select_lex_unit::exec_recursive()
11711171
{
11721172
st_select_lex *lex_select_save= thd->lex->current_select;
11731173
st_select_lex *start= with_element->first_recursive;
@@ -1189,18 +1189,6 @@ bool st_select_lex_unit::exec_recursive(bool is_driving_recursive)
11891189
if ((saved_error= incr_table->file->ha_delete_all_rows()))
11901190
goto err;
11911191

1192-
if (is_driving_recursive)
1193-
{
1194-
With_element *with_elem= with_element;
1195-
while ((with_elem= with_elem->get_next_mutually_recursive()) !=
1196-
with_element)
1197-
{
1198-
rec_table= with_elem->first_rec_table_to_update;
1199-
if (rec_table)
1200-
rec_table->reginfo.join_tab->preread_init_done= false;
1201-
}
1202-
}
1203-
12041192
if (with_element->level == 0)
12051193
{
12061194
start= first_select();
@@ -1248,9 +1236,6 @@ bool st_select_lex_unit::exec_recursive(bool is_driving_recursive)
12481236
if (with_element->level == 1)
12491237
rec_table->reginfo.join_tab->preread_init_done= true;
12501238
}
1251-
1252-
if (with_element->level == thd->variables.max_recursion_level)
1253-
with_element->set_as_stabilized();
12541239

12551240
thd->lex->current_select= lex_select_save;
12561241
err:

sql/sys_vars.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,10 +2145,10 @@ static Sys_var_ulong Sys_max_prepared_stmt_count(
21452145
VALID_RANGE(0, 1024*1024), DEFAULT(16382), BLOCK_SIZE(1),
21462146
&PLock_prepared_stmt_count);
21472147

2148-
static Sys_var_ulong Sys_max_recursion_level(
2149-
"max_recursion_level",
2148+
static Sys_var_ulong Sys_max_recursive_iterations(
2149+
"max_recursive_iterations",
21502150
"Maximum number of iterations when executing recursive queries",
2151-
SESSION_VAR(max_recursion_level), CMD_LINE(OPT_ARG),
2151+
SESSION_VAR(max_recursive_iterations), CMD_LINE(OPT_ARG),
21522152
VALID_RANGE(0, UINT_MAX), DEFAULT(UINT_MAX), BLOCK_SIZE(1));
21532153

21542154
static Sys_var_ulong Sys_max_sort_length(

0 commit comments

Comments
 (0)