diff --git a/mysql-test/main/limit_rows_examined.result b/mysql-test/main/limit_rows_examined.result index 99c48dba454..b60bbddc083 100644 --- a/mysql-test/main/limit_rows_examined.result +++ b/mysql-test/main/limit_rows_examined.result @@ -938,3 +938,14 @@ Warnings: Warning 1931 Query execution was interrupted. The query exceeded LIMIT ROWS EXAMINED 100. The query result may be incomplete DROP TABLE t1, t2; # End of 10.5 tests +# +# MDEV-22241: Assertion `0' failed in Protocol::end_statement after query with LIMIT ROWS EXAMINED +# +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (1); +SELECT 1 FROM t1 +WHERE (1 IN (SELECT 8 UNION SELECT 5)) OR t1.a = 2 +LIMIT ROWS EXAMINED 1; +1 +DROP TABLE t1; +# End of 10.11 tests diff --git a/mysql-test/main/limit_rows_examined.test b/mysql-test/main/limit_rows_examined.test index 12c75121dfb..839148c943c 100644 --- a/mysql-test/main/limit_rows_examined.test +++ b/mysql-test/main/limit_rows_examined.test @@ -658,3 +658,18 @@ SELECT COUNT(*) FROM t1 JOIN t2 ON (b = a) UNION DISTINCT SELECT COUNT(*) FROM t DROP TABLE t1, t2; --echo # End of 10.5 tests + +--echo # +--echo # MDEV-22241: Assertion `0' failed in Protocol::end_statement after query with LIMIT ROWS EXAMINED +--echo # + +CREATE TABLE t1 (a int); +INSERT INTO t1 VALUES (1); + +SELECT 1 FROM t1 +WHERE (1 IN (SELECT 8 UNION SELECT 5)) OR t1.a = 2 +LIMIT ROWS EXAMINED 1; + +DROP TABLE t1; + +--echo # End of 10.11 tests diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 20a43bf0365..62067969f66 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -3604,8 +3604,6 @@ public: { if (limit_rows_examined) limit_rows_examined_cnt= limit_rows_examined->val_uint(); - else - limit_rows_examined_cnt= ULONGLONG_MAX; // Unreachable value } /** @@ -3617,7 +3615,7 @@ public: */ bool deactivate_limit_rows_examined() { - bool was_activated= (limit_rows_examined_cnt == ULONGLONG_MAX); + bool was_activated= (limit_rows_examined_cnt != ULONGLONG_MAX); limit_rows_examined_cnt= ULONGLONG_MAX; // Unreachable value return was_activated; } diff --git a/sql/sql_union.cc b/sql/sql_union.cc index b03ff1e4c85..f0cea5ef141 100644 --- a/sql/sql_union.cc +++ b/sql/sql_union.cc @@ -2340,14 +2340,15 @@ bool st_select_lex_unit::exec() DBUG_EXECUTE_IF("show_explain_probe_union_read", dbug_serve_apcs(thd, 1);); + bool limit_rows_was_activated; { List empty_list; empty_list.empty(); /* - Deactivate LIMIT ROWS EXAMINED in order to produce the possibly incomplete - result of the UNION without interruption due to exceeding the limit. + Deactivate LIMIT ROWS EXAMINED to avoid producing potentially incomplete + result of the UNION due to exceeding of the limit. */ - thd->lex->deactivate_limit_rows_examined(); + limit_rows_was_activated= thd->lex->deactivate_limit_rows_examined(); // Check if EOM if (fake_select_lex != NULL && likely(!thd->is_fatal_error)) @@ -2447,7 +2448,8 @@ bool st_select_lex_unit::exec() } thd->lex->current_select= lex_select_save; err: - thd->lex->activate_limit_rows_examined(); + if (limit_rows_was_activated) + thd->lex->activate_limit_rows_examined(); if (likely(!saved_error)) thd->inc_examined_row_count(examined_rows); DBUG_RETURN(saved_error); @@ -2588,7 +2590,6 @@ bool st_select_lex_unit::exec_recursive() thd->lex->current_select= lex_select_save; err: - thd->lex->activate_limit_rows_examined(); DBUG_RETURN(saved_error); }