gluesql / gluesql

GlueSQL is quite sticky. It attaches to anywhere.

Home Page:https://gluesql.org/docs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Decimal casted to I64 when using conditional statement

gitmalong opened this issue · comments

Reproducer

#[wasm_bindgen_test]
async fn test_decimal_error() {
    let mut glue = GlueSqlStorage::glue(Some("test_decimal_error".into())).await;
    let table_name = "temp";
    let expected_number = dec!(3.3333333);

    table(table_name)
        .create_table_if_not_exists()
        .add_column("number DECIMAL")
        .execute(&mut glue)
        .await
        .unwrap();

    table(table_name)
        .insert()
        .values(vec![vec!(null()), vec![num(0)], vec![num(expected_number.to_string())], vec![num(0)], vec!(null())])
        .execute(&mut glue)
        .await
        .unwrap();

    let res = table(table_name).select().project("SUM(CASE WHEN number IS NOT NULL and number > 0 THEN number ELSE 0 END)").execute(&mut glue).await.unwrap();
    match res {
        gluesql_core::prelude::Payload::Select { labels: _, rows } => {
            let row = rows.get(0).unwrap();
            let val = row.get(0).unwrap();
            let loaded_number: Decimal = val.try_into().unwrap();
            assert_eq!(expected_number, loaded_number);
        },
        _ => todo!(),
    }
}

fails with

assertion failed: `(left == right)`

Diff < left / right > :
<3.3333333
>3

Debug print of payload:

Select { labels: ["SUM(CASE WHEN number IS NOT NULL AND number > 0 THEN number ELSE 0 END)"], rows: [[I64(3)]] }

Using CAST(0 as DECIMAL) fixes the problem. So probably not a bug but it would be nice to get some clarification regarding which type is being used in case the return types within the conditional statement are different.