diff --git a/core/incremental/cursor.rs b/core/incremental/cursor.rs index d2a4667bd..c228d9003 100644 --- a/core/incremental/cursor.rs +++ b/core/incremental/cursor.rs @@ -25,6 +25,14 @@ enum SeekState { target: i64, }, + /// Btree seek returned TryAdvance, now advancing with next()/prev() + Advancing { + /// The row we are trying to find + target: i64, + /// The seek operation (determines direction of advance) + op: SeekOp, + }, + /// Seek completed successfully Done, } @@ -155,6 +163,67 @@ impl MaterializedViewCursor { )])) } + /// Process btree changes: merge with uncommitted, build zset, and determine result. + /// Returns the next state action: either Done with a result, or updates seek_state for another iteration. + fn process_btree_changes( + &mut self, + target: i64, + target_rowid: i64, + op: SeekOp, + changes: Vec<(HashableRow, isize)>, + ) -> Result> { + let mut btree_entries = Delta { changes }; + let changes = self.uncommitted.seek(target, op); + + let uncommitted_entries = Delta { changes }; + btree_entries.merge(&uncommitted_entries); + + // if empty pre-zset, means nothing was found. Empty post-zset can mean that + // we just canceled weights. + if btree_entries.is_empty() { + self.seek_state = SeekState::Done; + return Ok(IOResult::Done(())); + } + + let min_seen = btree_entries + .changes + .first() + .expect("cannot be empty, we just tested for it") + .0 + .rowid; + let max_seen = btree_entries + .changes + .last() + .expect("cannot be empty, we just tested for it") + .0 + .rowid; + + let zset = RowKeyZSet::from_delta(&btree_entries); + let ret = zset.seek(target_rowid, op); + + if !ret.is_empty() { + let (row, _) = &ret[0]; + self.current_row = Some((row.rowid, row.values.clone())); + self.seek_state = SeekState::Done; + return Ok(IOResult::Done(())); + } + + let new_target = match op { + SeekOp::GT => Some(max_seen), + SeekOp::GE { eq_only: false } => Some(max_seen + 1), + SeekOp::LT => Some(min_seen), + SeekOp::LE { eq_only: false } => Some(min_seen - 1), + SeekOp::LE { eq_only: true } | SeekOp::GE { eq_only: true } => None, + }; + + if let Some(target) = new_target { + self.seek_state = SeekState::Seek { target }; + } else { + self.seek_state = SeekState::Done; + } + Ok(IOResult::Done(())) + } + /// Internal seek implementation that doesn't check preconditions fn do_seek(&mut self, target_rowid: i64, op: SeekOp) -> Result> { loop { @@ -171,62 +240,60 @@ impl MaterializedViewCursor { let btree_result = return_if_io!(self.btree_cursor.seek(SeekKey::TableRowId(target), op)); - let changes = if btree_result == SeekResult::Found { - return_if_io!(self.read_btree_delta_entry()) - } else { - Vec::new() + let changes = match btree_result { + SeekResult::Found => return_if_io!(self.read_btree_delta_entry()), + SeekResult::TryAdvance => { + // Transition to Advancing state before calling next/prev. + // This ensures that if next/prev returns IO, we resume in + // Advancing state and don't redundantly call seek again. + self.seek_state = SeekState::Advancing { target, op }; + continue; + } + SeekResult::NotFound => Vec::new(), }; - let mut btree_entries = Delta { changes }; - let changes = self.uncommitted.seek(target, op); + return_if_io!(self.process_btree_changes(target, target_rowid, op, changes)); - let uncommitted_entries = Delta { changes }; - btree_entries.merge(&uncommitted_entries); - - // if empty pre-zset, means nothing was found. Empty post-zset can mean that - // we just canceled weights. - if btree_entries.is_empty() { - self.seek_state = SeekState::Done; - return Ok(IOResult::Done(SeekResult::NotFound)); + // Check if we're done or need to continue seeking + if matches!(self.seek_state, SeekState::Done) { + let result = if self.current_row.is_some() { + SeekResult::Found + } else { + SeekResult::NotFound + }; + return Ok(IOResult::Done(result)); } + // Otherwise state is Seek with new target, loop continues + } + SeekState::Advancing { target, op } => { + let target = *target; + let op = *op; - let min_seen = btree_entries - .changes - .first() - .expect("cannot be empty, we just tested for it") - .0 - .rowid; - let max_seen = btree_entries - .changes - .last() - .expect("cannot be empty, we just tested for it") - .0 - .rowid; - - let zset = RowKeyZSet::from_delta(&btree_entries); - let ret = zset.seek(target_rowid, op); - - if !ret.is_empty() { - let (row, _) = &ret[0]; - self.current_row = Some((row.rowid, row.values.clone())); - self.seek_state = SeekState::Done; - return Ok(IOResult::Done(SeekResult::Found)); - } - - let new_target = match op { - SeekOp::GT => Some(max_seen), - SeekOp::GE { eq_only: false } => Some(max_seen + 1), - SeekOp::LT => Some(min_seen), - SeekOp::LE { eq_only: false } => Some(min_seen - 1), - SeekOp::LE { eq_only: true } | SeekOp::GE { eq_only: true } => None, + // Cursor is positioned at the leaf but current entry doesn't match. + // Advance in the appropriate direction to find the next matching entry. + match op { + SeekOp::GT | SeekOp::GE { .. } => { + return_if_io!(self.btree_cursor.next()) + } + SeekOp::LT | SeekOp::LE { .. } => { + return_if_io!(self.btree_cursor.prev()) + } }; + // read_btree_delta_entry handles the case where cursor is at end + let changes = return_if_io!(self.read_btree_delta_entry()); - if let Some(target) = new_target { - self.seek_state = SeekState::Seek { target }; - } else { - self.seek_state = SeekState::Done; - return Ok(IOResult::Done(SeekResult::NotFound)); + return_if_io!(self.process_btree_changes(target, target_rowid, op, changes)); + + // Check if we're done or need to continue seeking + if matches!(self.seek_state, SeekState::Done) { + let result = if self.current_row.is_some() { + SeekResult::Found + } else { + SeekResult::NotFound + }; + return Ok(IOResult::Done(result)); } + // Otherwise state is Seek with new target, loop continues } SeekState::Done => { // We always return before setting the state to done. Meaning if we got here, @@ -254,6 +321,18 @@ impl MaterializedViewCursor { } pub fn next(&mut self) -> Result> { + // If there's a pending seek operation (due to IO), complete it first. + // SeekState::Seek or SeekState::Advancing means IO was interrupted mid-seek and we need to resume. + // SeekState::Init means cursor was never positioned - don't resume, fall through to check current_row. + if matches!( + self.seek_state, + SeekState::Seek { .. } | SeekState::Advancing { .. } + ) { + // target is ignored when resuming + let result = return_if_io!(self.do_seek(0, SeekOp::GT)); + return Ok(IOResult::Done(result == SeekResult::Found)); + } + // If cursor is not positioned (no current_row), return false // This matches BTreeCursor behavior when valid_state == Invalid let Some((current_rowid, _)) = &self.current_row else { @@ -1616,4 +1695,289 @@ mod tests { Ok(()) } + + // ===== IO RESUMPTION TEST SUITE ===== + // These tests verify correct behavior when btree operations return IO (pending) + + mod io_resumption_tests { + use super::*; + use crate::io::Completion; + use crate::storage::btree::{BTreeKey, CursorTrait}; + use crate::types::{IOCompletions, ImmutableRecord, IndexInfo}; + use crate::Register; + use std::sync::atomic::{AtomicUsize, Ordering}; + + /// Mock btree cursor that tracks calls and can simulate IO pending states. + /// Used to verify that seek operations aren't redundantly repeated after IO resumption. + struct MockBTreeCursor { + /// Number of times seek() was called + seek_count: AtomicUsize, + /// Number of times next() was called + next_count: AtomicUsize, + /// Number of times prev() was called + prev_count: AtomicUsize, + /// Current rowid to return + current_rowid: Option, + /// Record to return (needs to live for the cursor lifetime) + record: ImmutableRecord, + /// Index info + index_info: Arc, + } + + impl MockBTreeCursor { + fn new() -> Self { + // Create a minimal record with rowid=1, value=10, weight=1 + let record = Self::create_test_record(1, 10, 1); + Self { + seek_count: AtomicUsize::new(0), + next_count: AtomicUsize::new(0), + prev_count: AtomicUsize::new(0), + current_rowid: Some(1), + record, + index_info: Arc::new(IndexInfo::default()), + } + } + + fn create_test_record(rowid: i64, value: i64, weight: i64) -> ImmutableRecord { + // Build a binary record with format: [header_size, type1, type2, type3, rowid, value, weight] + // For integers, type code is 1 for 1-byte int, 2 for 2-byte, etc. + // Using type 6 (8-byte integer) for all values + // Header: 4 bytes (header size byte + 3 type bytes) + let mut payload = vec![ + 4u8, // header size + 6u8, // type for rowid (8-byte int) + 6u8, // type for value (8-byte int) + 6u8, // type for weight (8-byte int) + ]; + + // Data: 3 x 8-byte integers + payload.extend_from_slice(&rowid.to_be_bytes()); + payload.extend_from_slice(&value.to_be_bytes()); + payload.extend_from_slice(&weight.to_be_bytes()); + + ImmutableRecord::from_bin_record(payload) + } + + fn get_seek_count(&self) -> usize { + self.seek_count.load(Ordering::SeqCst) + } + + fn get_prev_count(&self) -> usize { + self.prev_count.load(Ordering::SeqCst) + } + } + + impl CursorTrait for MockBTreeCursor { + fn seek(&mut self, _key: SeekKey<'_>, _op: SeekOp) -> Result> { + let count = self.seek_count.fetch_add(1, Ordering::SeqCst); + if count == 0 { + // First seek returns TryAdvance + Ok(IOResult::Done(SeekResult::TryAdvance)) + } else { + // Subsequent seeks return Found to avoid infinite loop + // (The bug is that this second seek happens at all) + Ok(IOResult::Done(SeekResult::Found)) + } + } + + fn seek_unpacked( + &mut self, + _registers: &[Register], + _op: SeekOp, + ) -> Result> { + // Not used in these tests + Ok(IOResult::Done(SeekResult::NotFound)) + } + + fn next(&mut self) -> Result> { + let count = self.next_count.fetch_add(1, Ordering::SeqCst); + if count == 0 { + // First call returns IO (pending) + let completion = Completion::new_yield(); + Ok(IOResult::IO(IOCompletions::Single(completion))) + } else { + // Subsequent calls return Done + Ok(IOResult::Done(())) + } + } + + fn prev(&mut self) -> Result> { + let count = self.prev_count.fetch_add(1, Ordering::SeqCst); + if count == 0 { + // First call returns IO (pending) + let completion = Completion::new_yield(); + Ok(IOResult::IO(IOCompletions::Single(completion))) + } else { + // Subsequent calls return Done + Ok(IOResult::Done(())) + } + } + + fn rowid(&mut self) -> Result>> { + Ok(IOResult::Done(self.current_rowid)) + } + + fn record(&mut self) -> Result>> { + Ok(IOResult::Done(Some(&self.record))) + } + + fn last(&mut self) -> Result> { + Ok(IOResult::Done(())) + } + + fn insert(&mut self, _key: &BTreeKey) -> Result> { + Ok(IOResult::Done(())) + } + + fn delete(&mut self) -> Result> { + Ok(IOResult::Done(())) + } + + fn set_null_flag(&mut self, _flag: bool) {} + + fn get_null_flag(&self) -> bool { + false + } + + fn exists(&mut self, _key: &Value) -> Result> { + Ok(IOResult::Done(false)) + } + + fn clear_btree(&mut self) -> Result>> { + Ok(IOResult::Done(None)) + } + + fn btree_destroy(&mut self) -> Result>> { + Ok(IOResult::Done(None)) + } + + fn count(&mut self) -> Result> { + Ok(IOResult::Done(0)) + } + + fn is_empty(&self) -> bool { + false + } + + fn root_page(&self) -> i64 { + 1 + } + + fn rewind(&mut self) -> Result> { + Ok(IOResult::Done(())) + } + + fn has_record(&self) -> bool { + true + } + + fn set_has_record(&mut self, _has_record: bool) {} + + fn get_index_info(&self) -> &Arc { + &self.index_info + } + + fn seek_end(&mut self) -> Result> { + Ok(IOResult::Done(())) + } + + fn seek_to_last(&mut self, _always_seek: bool) -> Result> { + Ok(IOResult::Done(())) + } + + fn invalidate_record(&mut self) {} + + fn has_rowid(&self) -> bool { + true + } + + fn get_pager(&self) -> Arc { + panic!("MockBTreeCursor::get_pager should not be called") + } + + fn get_skip_advance(&self) -> bool { + false + } + } + + /// Test that verifies the bug: when btree.next() returns IO after TryAdvance, + /// resuming should NOT call btree.seek() again. + /// + /// Current behavior (BUG): seek is called twice + /// Expected behavior: seek should only be called once + #[test] + fn test_seek_not_repeated_after_io_during_try_advance() -> Result<()> { + let conn = create_test_connection()?; + + // Get the view for creating a cursor + let view_mutex = conn + .schema + .read() + .get_materialized_view("test_view") + .ok_or_else(|| crate::LimboError::InternalError("View not found".to_string()))?; + + let pager = conn.get_pager(); + let tx_state = conn.view_transaction_states.get_or_create("test_view"); + + // Create mock cursor that returns TryAdvance from seek and IO from next + let mock_cursor = MockBTreeCursor::new(); + let mock_cursor_box: Box = Box::new(mock_cursor); + + // Get a reference to the mock to check counts later + // We need to use Any::downcast to access the mock's methods + let mock_ptr = mock_cursor_box.as_ref() as *const dyn CursorTrait; + + let mut cursor = + MaterializedViewCursor::new(mock_cursor_box, view_mutex, pager, tx_state)?; + + // Use LE so that rowid=1 satisfies the condition (1 <= 5) + let seek_op = SeekOp::LE { eq_only: false }; + + // First call to do_seek - should call btree.seek() which returns TryAdvance, + // then btree.prev() which returns IO + let result = cursor.do_seek(5, seek_op); + + // Should return IO (pending) + assert!( + matches!(result, Ok(IOResult::IO(_))), + "Expected IO result, got {result:?}" + ); + + // Check seek was called once + let mock_ref: &MockBTreeCursor = unsafe { &*(mock_ptr as *const MockBTreeCursor) }; + assert_eq!( + mock_ref.get_seek_count(), + 1, + "seek should be called exactly once before IO" + ); + // For LE, we call prev() not next() + assert_eq!( + mock_ref.get_prev_count(), + 1, + "prev should be called once (returned IO)" + ); + + // Second call to do_seek (simulating resumption after IO completes) + // BUG: This will call btree.seek() again, which is wasteful + let result = cursor.do_seek(5, seek_op); + + // The result might be Found or some other result + assert!( + matches!(result, Ok(IOResult::Done(_))), + "Expected Done result on resume, got {result:?}" + ); + + // Check seek count - seek should only be called once + // If this fails with seek_count=2, it means the bug exists: + // seek is being redundantly called again after IO resumption + let final_seek_count = mock_ref.get_seek_count(); + + assert_eq!( + final_seek_count, 1, + "seek should only be called once, but was called {final_seek_count} times (redundant seek after IO during TryAdvance)" + ); + + Ok(()) + } + } } diff --git a/macros/src/test.rs b/macros/src/test.rs index 6c472e3de..dfcf53999 100644 --- a/macros/src/test.rs +++ b/macros/src/test.rs @@ -36,6 +36,7 @@ impl ToTokens for SpannedType { struct Args { path: Option>, mvcc: Option>, + views: Option>, init_sql: Option, } @@ -78,6 +79,14 @@ impl Args { }; } + // Enable views if requested + if self.views.is_some() { + builder = quote! { + #builder + .with_views(true) + }; + } + // Add init_sql if provided if let Some(user_sql) = &self.init_sql { builder = quote! { @@ -99,6 +108,7 @@ impl Parse for Args { let mut path = None; let mut mvcc = None; + let mut views = None; let mut init_sql = None; let errors = args @@ -144,6 +154,9 @@ impl Parse for Args { if p.is_ident("mvcc") { mvcc = Some(SpannedType((), p.span())); seen_args.insert(ident.unwrap().clone()); + } else if p.is_ident("views") { + views = Some(SpannedType((), p.span())); + seen_args.insert(ident.unwrap().clone()); } else { return Some(syn::Error::new_spanned(p, "unexpected flag")); } @@ -166,6 +179,7 @@ impl Parse for Args { Ok(Args { path, mvcc, + views, init_sql, }) } diff --git a/tests/integration/common.rs b/tests/integration/common.rs index d789b5ea1..a8143c3f6 100644 --- a/tests/integration/common.rs +++ b/tests/integration/common.rs @@ -30,6 +30,7 @@ pub struct TempDatabaseBuilder { init_sql: Option, enable_mvcc: bool, io_uring: bool, + enable_views: bool, } struct TestIo { @@ -105,6 +106,7 @@ impl TempDatabaseBuilder { init_sql: None, enable_mvcc: false, io_uring: false, + enable_views: false, } } @@ -156,11 +158,20 @@ impl TempDatabaseBuilder { self } + pub fn with_views(mut self, enable: bool) -> Self { + self.enable_views = enable; + self + } + pub fn build(self) -> TempDatabase { - let opts = self + let mut opts = self .opts .unwrap_or_else(|| turso_core::DatabaseOpts::new().with_encryption(true)); + if self.enable_views { + opts = opts.with_views(true); + } + let flags = self.flags.unwrap_or_default(); let db_path = match self.db_path { diff --git a/tests/integration/query_processing/test_write_path.rs b/tests/integration/query_processing/test_write_path.rs index b278a5b01..f5eb0daa7 100644 --- a/tests/integration/query_processing/test_write_path.rs +++ b/tests/integration/query_processing/test_write_path.rs @@ -1598,3 +1598,83 @@ pub fn test_mvcc_stale_snapshot_after_schema_updated() { conn1.execute("COMMIT").unwrap(); } + +/// Test materialized view population with enough rows to trigger btree page splits. +/// +/// This test exposes a bug where the matview code doesn't properly handle +/// btree operations during page splits, causing rows to be silently lost. +/// The bug triggers around row 193 with 500-byte content - the matview stops +/// accepting new rows and freezes at 192. +/// +/// Key conditions: +/// - TEXT PRIMARY KEY (not INTEGER) +/// - 500+ byte content column +/// - Each INSERT in a separate connection (simulating CLI usage) +#[turso_macros::test(views)] +fn test_matview_row_loss_during_btree_split(tmp_db: TempDatabase) -> anyhow::Result<()> { + let conn = tmp_db.connect_limbo(); + + // Create table with TEXT PRIMARY KEY and content column + common::run_query( + &tmp_db, + &conn, + "CREATE TABLE t (id TEXT PRIMARY KEY, title TEXT, content TEXT)", + )?; + common::run_query( + &tmp_db, + &conn, + "CREATE MATERIALIZED VIEW v AS SELECT id, title FROM t", + )?; + + // Insert 250 rows with 500-byte content + // Each insert uses a fresh connection to simulate CLI behavior + let content = "x".repeat(500); + for i in 1..=250 { + let insert_conn = tmp_db.connect_limbo(); + let insert_sql = format!("INSERT INTO t VALUES ('id{i}', 'title {i}', '{content}')"); + common::run_query(&tmp_db, &insert_conn, &insert_sql)?; + } + + // Check counts with fresh connection + let check_conn = tmp_db.connect_limbo(); + + let mut table_count = 0i64; + match check_conn.query("SELECT COUNT(*) FROM t") { + Ok(Some(ref mut rows)) => loop { + match rows.step()? { + StepResult::Row => { + table_count = rows.row().unwrap().get::(0).unwrap(); + } + StepResult::IO => rows._io().step()?, + StepResult::Done => break, + _ => {} + } + }, + Ok(None) => {} + Err(e) => return Err(e.into()), + } + + let mut view_count = 0i64; + match check_conn.query("SELECT COUNT(*) FROM v") { + Ok(Some(ref mut rows)) => loop { + match rows.step()? { + StepResult::Row => { + view_count = rows.row().unwrap().get::(0).unwrap(); + } + StepResult::IO => rows._io().step()?, + StepResult::Done => break, + _ => {} + } + }, + Ok(None) => {} + Err(e) => return Err(e.into()), + } + + assert_eq!( + table_count, view_count, + "Materialized view row count ({view_count}) doesn't match table row count ({table_count}). \ + Rows were lost during btree page splits.", + ); + + Ok(()) +}