tursodatabase / tursodatabase/libsql

[Rust] Calling previous `Row::get()`on a `Rows` that has been advanced returns `NullValue` causing runtime error

Open
#1,947 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
17.2k
Forks
531
Avg merge
1h 12m
Merged PRs (30d)
1

Description

Summary

When querying for data, fetching any previous Row values after advancing the Rows cursor will result in Row returning NullValue thus causing a runtime error.

The expected behavior that I'm looking for is that each Row value to be consistent regardless of where the current Rows cursor is pointing at; Row should always be returning the queried value independent of whether Rows::next() has been called.

Though, at the same time, I'm not sure if this is the intended behavior considering that Rust ownership and interior mutability model should've guaranteed that the Row value to stay the same. (I am still actively learning Rust & said behavior is not documented in the docs :/)

P.S, I should've probably opened a discussion thread first before opening this issue just to confirm, sorry!

Reproduction Code

use libsql::{Builder, params};

#[tokio::main]
async fn main() {
    let db = Builder::new_local(":memory:").build().await.unwrap();
    let conn = db.connect().unwrap();

    // Initialize db
    conn.execute("CREATE TABLE \"users\"(username text NOT NULL)", params![])
        .await
        .unwrap();

    // Seed data
    conn.execute("INSERT INTO \"users\"(username) VALUES (?)", params![
        "admin"
    ])
    .await
    .unwrap();

    // ------------

    let mut query = conn
        .query(
            "SELECT username FROM \"users\" WHERE username = ?",
            params!["admin"],
        )
        .await
        .unwrap();

    // Get the first user
    let user = query.next().await.unwrap().unwrap();
    let username_before = user.get_str(0).unwrap();
    println!("Before -- {username_before}");

    // Advance the cursor to the next row
    let _ = query.next().await.unwrap();

    // Get the first user again
    let username_after = user.get_str(0).unwrap(); // Runtime error here, looks like the row data is somehow dropped and returning NullValue?
    println!("After -- {username_after}");
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the reproduction against the Rust Row and Rows APIs, focusing on Rows::next() and Row::get_str(). Trace how the returned Row is represented after the cursor advances, then add or update coverage for repeated access; done means the documented behavior is consistent and the reproduction no longer produces NullValue unexpectedly.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sqlite
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.