{"record":{"id":"92c904bc92e55d41","repo":"tursodatabase/turso","slug":"next-on-non-btree-materialized-view-cursor","errorCode":null,"errorMessage":"Next on non-btree/materialized-view cursor","messagePattern":"Next on non-btree/materialized-view cursor","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/vdbe/execute.rs","lineNumber":3438,"sourceCode":"fn blob_reg_usize(state: &ProgramState, reg: usize, opcode: &str, what: &str) -> Result<usize> {\n    match state.registers[reg].get_value() {\n        Value::Numeric(Numeric::Integer(n)) if *n >= 0 => Ok(*n as usize),\n        other => Err(LimboError::InternalError(format!(\n            \"{opcode}: {what} register must hold a non-negative integer, got {other:?}\"\n        ))),\n    }\n}\n\n/// Resolve `cursor` to the b-tree cursor the blob opcodes require, or error. Only\n/// table-row cursors expose byte-level column access, so this is the single gate all\n/// three opcodes (BlobRead/BlobWrite/BlobLen) share.\nfn blob_btree_cursor<'a>(\n    state: &'a mut ProgramState,\n    cursor: usize,\n    opcode: &str,\n) -> Result<&'a mut dyn crate::storage::btree::CursorTrait> {\n    match state.get_cursor(cursor) {\n        Cursor::BTree(btree) => Ok(btree.as_mut()),\n        Cursor::Dyn(btree) => Ok(btree.as_mut()),\n        _ => Err(LimboError::InternalError(format!(\n            \"{opcode} requires a b-tree cursor\"\n        ))),\n    }\n}\n\n/// Signal incremental-blob expiry to `Blob::read`/`write`: store NULL in `dest` and\n/// advance. Expiry must fail the operation without aborting the program — the paused\n/// blob program owns the handle's transaction, and writes made before the expiry\n/// must still commit at close (sqlite3_blob semantics). Shared by BlobRead/BlobWrite.\nfn blob_expired_ack(state: &mut ProgramState, dest: usize) -> InsnFunctionStepResult {\n    state.registers[dest].set_value(Value::Null);\n    state.pc += 1;\n    InsnFunctionStepResult::Step\n}\n\npub fn op_blob_read(","sourceCodeStart":3420,"sourceCodeEnd":3456,"githubUrl":"https://github.com/tursodatabase/turso/blob/8d4a589f8d13ac184700d2a8f724f27e1995be3b/core/vdbe/execute.rs#L3420-L3456","documentation":"Insn::Next (advance a scan cursor to the following row) executed on a cursor outside the handled set — the match covers BTree, MaterializedView, and IndexMethod; sorter, pseudo, and virtual table cursors are not (sorters advance via SorterNext, vtabs via VNext). The program advanced a cursor with the wrong opcode — a codegen inconsistency caught in op_next.","triggerScenarios":"Loop emission picking the generic Next for a cursor allocated as a sorter (ORDER BY/GROUP BY pipeline), pseudo, or virtual table — e.g. after flattening rules changed or DISTINCT handling was reworked, so the loop opcode no longer matches the cursor kind.","commonSituations":"ORDER BY / GROUP BY / DISTINCT queries mixed with joins and subqueries; engine upgrades altering loop emission.","solutions":["Report the SQL — the loop's advance opcode must match the cursor kind","Test removing ORDER BY / GROUP BY / DISTINCT and vtab references to isolate which cursor is mis-targeted","Restructure so sorting happens in an outer query level","Upgrade"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Next must only advance cursors that support it\nfor insn in &program.insns {\n    if let Insn::Next { cursor_id, .. } = insn {\n        match program.cursor_ref.get(*cursor_id).map(|(_, t)| t) {\n            Some(CursorType::BTreeTable(_)) | Some(CursorType::BTreeIndex(_))\n            | Some(CursorType::MaterializedView(..)) | Some(CursorType::IndexMethod(..)) => {}\n            other => crate::bail_parse_error!(\"Next on unsupported cursor ({other:?})\"),\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Cover ORDER BY/GROUP BY/DISTINCT with joins and subqueries in CI","Debug-build audits matching loop opcodes to cursor kinds","Boundary catch_unwind for engine panics"],"tags":["vdbe","executor","cursor-type","iteration","panic","runtime"],"backgroundTag":"bytecode-cursor-type-mismatch","analyzedSha":"8d4a589f8d13ac184700d2a8f724f27e1995be3b","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}