Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

result stream stops after the first decoding error #1884

Closed
lovasoa opened this issue May 31, 2022 · 1 comment · Fixed by #1887
Closed

result stream stops after the first decoding error #1884

lovasoa opened this issue May 31, 2022 · 1 comment · Fixed by #1887

Comments

@lovasoa
Copy link
Contributor

lovasoa commented May 31, 2022

QueryAs::fetch returns a stream of Result<MyType, Error>. So, I would expect that a single error for a single row in the results would result in a stream that contains many Ok(MyType) and a single Err. However, it does not seem to be the case. The resulting stream contains Ok(MyType) for all the rows before the error, then an Err, and then it stops, without returning the next results.

@lovasoa
Copy link
Contributor Author

lovasoa commented May 31, 2022

Here is a small reproduction with sqlite :

use futures::stream::StreamExt;
use sqlx::{sqlite::SqliteConnection, Connection};

#[derive(Debug, sqlx::FromRow, PartialEq)]
struct MyType(u32);

#[tokio::main]
async fn main() {
    let mut conn = SqliteConnection::connect("sqlite::memory:").await.unwrap();
    let rows: Vec<Option<MyType>> =
        sqlx::query_as("SELECT 1 UNION ALL SELECT 'x' UNION  ALL SELECT 2")
            .fetch(&mut conn)
            .map(|x| x.ok())
            .collect()
            .await;
    assert_eq!(rows, vec![Some(MyType(1)), None, Some(MyType(2))]);
}

The result is

assertion failed: (left == right)
left: [Some(MyType(1)), None],
right: [Some(MyType(1)), None, Some(MyType(2))]

lovasoa added a commit to sqlpage/sqlx-oldapi that referenced this issue May 31, 2022
Fixes launchbadge#1884

When a single row cannot be converted to the target type of query_as,
it should not prevent the library user from accessing the other rows

Otherwise, the user cannot access all query results in query_as.
abonander pushed a commit that referenced this issue Jun 2, 2022
* query_as: don't stop stream after decoding error

Fixes #1884

When a single row cannot be converted to the target type of query_as,
it should not prevent the library user from accessing the other rows

Otherwise, the user cannot access all query results in query_as.

* use union in tests to maximize db compatibility
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant