From d179dd6f74354938e3fee2f6fa26d5d1600864e5 Mon Sep 17 00:00:00 2001 From: Daniel Black Date: Tue, 26 May 2026 13:49:14 +1000 Subject: [PATCH] MDEV-31209 Queries with window functions do not obey KILL / max_statement_time Window functions run in a loop in Frame_cursor::compute_values_for_current_row which can include a large number of rows. Adjust this function to check for the current thd being killed. --- mysql-test/main/set_statement_notembedded.result | 9 +++++++++ mysql-test/main/set_statement_notembedded.test | 10 ++++++++++ sql/sql_window.cc | 4 +++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/mysql-test/main/set_statement_notembedded.result b/mysql-test/main/set_statement_notembedded.result index 67d2d8bc505cc..8c8608e992358 100644 --- a/mysql-test/main/set_statement_notembedded.result +++ b/mysql-test/main/set_statement_notembedded.result @@ -15,3 +15,12 @@ Max_statement_time_exceeded 1 SELECT @@MAX_STATEMENT_TIME; @@MAX_STATEMENT_TIME 0.000000 +# +# MDEV-31209 Queries with window functions do not obey KILL / max_statement_time +# +create table t (i int); +insert into t values(1),(2),(3); +set statement max_statement_time=0.03 for select max(sleep(0.1)) over( ) from t; +ERROR 70100: Query execution was interrupted (max_statement_time exceeded) +drop table t; +# End of 10.11 tests diff --git a/mysql-test/main/set_statement_notembedded.test b/mysql-test/main/set_statement_notembedded.test index 95d3efd40cacd..e71a0f5ee5c53 100644 --- a/mysql-test/main/set_statement_notembedded.test +++ b/mysql-test/main/set_statement_notembedded.test @@ -11,4 +11,14 @@ SET STATEMENT MAX_STATEMENT_TIME=1 FOR SELECT SLEEP(10); SHOW STATUS LIKE "max_statement_time_exceeded"; SELECT @@MAX_STATEMENT_TIME; +--echo # +--echo # MDEV-31209 Queries with window functions do not obey KILL / max_statement_time +--echo # +create table t (i int); +insert into t values(1),(2),(3); +--error ER_STATEMENT_TIMEOUT +set statement max_statement_time=0.03 for select max(sleep(0.1)) over( ) from t; +drop table t; + +--echo # End of 10.11 tests diff --git a/sql/sql_window.cc b/sql/sql_window.cc index 73db592fbc113..de4fb30258e58 100644 --- a/sql/sql_window.cc +++ b/sql/sql_window.cc @@ -2255,6 +2255,7 @@ class Frame_scan_cursor : public Frame_cursor between them, top bound row and bottom bound row inclusive. */ void compute_values_for_current_row() { + THD *thd= current_thd; if (top_bound.is_outside_computation_bounds() || bottom_bound.is_outside_computation_bounds()) return; @@ -2265,7 +2266,8 @@ class Frame_scan_cursor : public Frame_cursor cursor.move_to(start_rownum); - for (ha_rows idx= start_rownum; idx <= bottom_rownum; idx++) + for (ha_rows idx= start_rownum; idx <= bottom_rownum + && !thd->check_killed(true); idx++) { if (cursor.fetch()) //EOF break;