From cbb56a182e73904b6e72dfae695ef12dfc2d01a0 Mon Sep 17 00:00:00 2001 From: Jussi Saurio Date: Fri, 23 May 2025 14:23:18 +0300 Subject: [PATCH 1/3] Fix bug: backwards iteration of table btree hangs --- core/storage/btree.rs | 2 ++ testing/orderby.test | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/core/storage/btree.rs b/core/storage/btree.rs index 763b9340b..febb08ade 100644 --- a/core/storage/btree.rs +++ b/core/storage/btree.rs @@ -576,6 +576,7 @@ impl BTreeCursor { } else { cell_idx }; + let cell = contents.cell_get( cell_idx, payload_overflow_threshold_max(contents.page_type(), self.usable_space() as u16), @@ -589,6 +590,7 @@ impl BTreeCursor { _rowid, }) => { let mem_page = self.read_page(_left_child_page as usize)?; + self.stack.retreat(); self.stack.push_backwards(mem_page); continue; } diff --git a/testing/orderby.test b/testing/orderby.test index b5b56cdd4..0a12e84d7 100755 --- a/testing/orderby.test +++ b/testing/orderby.test @@ -199,4 +199,9 @@ do_execsql_test orderby_desc_with_filter_id_le { 6665 6664 6663 -6662} \ No newline at end of file +6662} + +# regression test where backwards iteration used to hang indefinitely +do_execsql_test orderby_desc_subquery_count_regression { + select count(1) from (select * from users where id < 100 order by id desc) +} {99} \ No newline at end of file From cbb3efab82c420ded289e1980376d17fa9d10673 Mon Sep 17 00:00:00 2001 From: Jussi Saurio Date: Fri, 23 May 2025 14:28:25 +0300 Subject: [PATCH 2/3] Fuzz: modify rowid_seek_fuzz so that it catches this regression in the future --- tests/integration/fuzz/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/integration/fuzz/mod.rs b/tests/integration/fuzz/mod.rs index a981ce4a1..f7c3860d7 100644 --- a/tests/integration/fuzz/mod.rs +++ b/tests/integration/fuzz/mod.rs @@ -51,7 +51,7 @@ mod tests { let insert = format!( "INSERT INTO t VALUES {}", - (1..10000) + (1..2000) .map(|x| format!("({})", x)) .collect::>() .join(", ") @@ -71,9 +71,9 @@ mod tests { for comp in COMPARISONS.iter() { for order_by in ORDER_BY.iter() { - for max in 0..=10000 { + for max in 0..=2000 { let query = format!( - "SELECT * FROM t WHERE x {} {} {} LIMIT 3", + "SELECT * FROM t WHERE x {} {} {}", comp, max, order_by.unwrap_or("") From 517c795f151787732e6af55d2197fe6d5cae9202 Mon Sep 17 00:00:00 2001 From: Jussi Saurio Date: Fri, 23 May 2025 14:33:55 +0300 Subject: [PATCH 3/3] Add another test --- testing/orderby.test | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/testing/orderby.test b/testing/orderby.test index 0a12e84d7..d930bb388 100755 --- a/testing/orderby.test +++ b/testing/orderby.test @@ -202,6 +202,12 @@ do_execsql_test orderby_desc_with_filter_id_le { 6662} # regression test where backwards iteration used to hang indefinitely -do_execsql_test orderby_desc_subquery_count_regression { +do_execsql_test orderby_desc_regression { select count(1) from (select * from users where id < 100 order by id desc) -} {99} \ No newline at end of file +} {99} + +do_execsql_test orderby_desc_regression_verify_order { + select id from users where id < 100 order by id desc limit 3; +} {99 +98 +97} \ No newline at end of file