Protryon / klickhouse

Rust crate for accessing Clickhouse
Apache License 2.0
88 stars 22 forks source link

[BUG?] Weird (conversion?) issue for a simple query #68

Open keltia opened 1 week ago

keltia commented 1 week ago

Given the following code:

#[derive(Copy, Clone, Debug)]
struct Point {
    latitude: f64,
    longitude: f64,
}

async fn ch_distance(point1: Point, point2: Point) -> eyre::Result<f64> {
    let url = std::env::var("KLICKHOUSE_URL")?;
    let db = std::env::var("CLICKHOUSE_DB")?;
    let user = std::env::var("CLICKHOUSE_USER")?;
    let pwd = std::env::var("CLICKHOUSE_PASSWD")?;

    let client = klickhouse::Client::connect(
        url,
        ClientOptions {
            username: user,
            password: pwd,
            default_database: db,
        },
    )
        .await?;

    #[derive(Debug, Row)]
    struct Ans {
        dist: f64,
    }
    let q = QueryBuilder::new("SELECT geoDistance($1,$2,$3,$4) AS dist");
        .arg(point1.longitude).arg(point1.latitude).arg(point2.longitude).arg(point2.latitude);
    let val = client.query_one::<Ans>(q).await?;
    dbg!(&val);
    Ok(val.dist.into())
}

The answer from the query is 4907813.76 which is utterly wrong, it should be 23439.96. using the query manually inside clickhouse-client works. Using the real value in the query instead of $1..$4 works so I guess something is weirtd with .arg() (.args([]) same issue). Maybe I missed something.

EDIT: it happens on macOS but NOT on Ubuntu 22.04!!

Any idea? Using 0.13.

Protryon commented 1 week ago

The fact that it is OS dependent is absolutely whack, I have no idea what would cause that.

Ignoring that, you can print out the direct query that Klickhouse is sending upstream via QueryBuilder::finalize to see what the query looks like

EDIT: It might be related to running in ARM instead of x86? They have the same floating point representation though?