From e299a0e77e554ecac033fec4b42ecaa162917898 Mon Sep 17 00:00:00 2001 From: Jussi Saurio Date: Tue, 15 Apr 2025 11:19:50 +0300 Subject: [PATCH] vdbe: add Insn::IdxRowId --- core/vdbe/execute.rs | 22 ++++++++++++++++++++++ core/vdbe/explain.rs | 16 ++++++++++++++++ core/vdbe/insn.rs | 6 ++++++ 3 files changed, 44 insertions(+) diff --git a/core/vdbe/execute.rs b/core/vdbe/execute.rs index 2eafed217..0325ffae8 100644 --- a/core/vdbe/execute.rs +++ b/core/vdbe/execute.rs @@ -1836,6 +1836,28 @@ pub fn op_row_id( Ok(InsnFunctionStepResult::Step) } +pub fn op_idx_row_id( + program: &Program, + state: &mut ProgramState, + insn: &Insn, + pager: &Rc, + mv_store: Option<&Rc>, +) -> Result { + let Insn::IdxRowId { cursor_id, dest } = insn else { + unreachable!("unexpected Insn {:?}", insn) + }; + let mut cursors = state.cursors.borrow_mut(); + let cursor = cursors.get_mut(*cursor_id).unwrap().as_mut().unwrap(); + let cursor = cursor.as_btree_mut(); + let rowid = cursor.rowid()?; + state.registers[*dest] = match rowid { + Some(rowid) => Register::OwnedValue(OwnedValue::Integer(rowid as i64)), + None => Register::OwnedValue(OwnedValue::Null), + }; + state.pc += 1; + Ok(InsnFunctionStepResult::Step) +} + pub fn op_seek_rowid( program: &Program, state: &mut ProgramState, diff --git a/core/vdbe/explain.rs b/core/vdbe/explain.rs index f2230d2eb..f6e1073c3 100644 --- a/core/vdbe/explain.rs +++ b/core/vdbe/explain.rs @@ -684,6 +684,22 @@ pub fn insn_to_str( .unwrap_or(&format!("cursor {}", cursor_id)) ), ), + Insn::IdxRowId { cursor_id, dest } => ( + "IdxRowId", + *cursor_id as i32, + *dest as i32, + 0, + OwnedValue::build_text(""), + 0, + format!( + "r[{}]={}.rowid", + dest, + &program.cursor_ref[*cursor_id] + .0 + .as_ref() + .unwrap_or(&format!("cursor {}", cursor_id)) + ), + ), Insn::SeekRowid { cursor_id, src_reg, diff --git a/core/vdbe/insn.rs b/core/vdbe/insn.rs index 7b30396c0..87a615aee 100644 --- a/core/vdbe/insn.rs +++ b/core/vdbe/insn.rs @@ -438,6 +438,11 @@ pub enum Insn { cursor_id: CursorID, dest: usize, }, + /// Read the rowid of the current row from an index cursor. + IdxRowId { + cursor_id: CursorID, + dest: usize, + }, /// Seek to a rowid in the cursor. If not found, jump to the given PC. Otherwise, continue to the next instruction. SeekRowid { @@ -856,6 +861,7 @@ impl Insn { Insn::String8 { .. } => execute::op_string8, Insn::Blob { .. } => execute::op_blob, Insn::RowId { .. } => execute::op_row_id, + Insn::IdxRowId { .. } => execute::op_idx_row_id, Insn::SeekRowid { .. } => execute::op_seek_rowid, Insn::DeferredSeek { .. } => execute::op_deferred_seek, Insn::SeekGE { .. } => execute::op_seek,