Skip to content

SQL INSERT casts to Int64 when handling UInt64 values #14208

@nuno-faria

Description

@nuno-faria

Describe the bug

When performing a SQL INSERT to a BIGINT UNSIGNED column, the wrong type is assumed when mixing UInt64 data with values that can be represented in Int64. However, it works when the inserted values must all be represented in UInt64.

To Reproduce

use datafusion::common::Result;
use datafusion::prelude::SessionContext;

#[tokio::main]
async fn main() -> Result<()> {
    let ctx = SessionContext::new();
    // column is declared as bigint unsigned
    ctx.sql("create table t (v bigint unsigned)").await?;
    let value: u64 = 10000000000000000000;

    // works: all values are u64
    ctx.sql(format!("insert into t values ({value}), ({value})").as_str()).await?.collect().await?;
    ctx.sql("select * from t").await?.show().await?;

    // does not work: one of the values forces the type to be Int64, invalidating {value}
    // Error: ArrowError(CastError("Can't cast value 10000000000000000000 to type Int64"), None)
    ctx.sql(format!("insert into t values ({value}), (1)").as_str()).await?.collect().await?;
    ctx.sql("select * from t").await?.show().await?;

    Ok(())
}

Expected behavior

When at least one of the values must be encoded with UInt64, the assumed type must be UInt64.

Additional context

I can take a look at this issue, but wanted to first make sure this is a bug and not expected behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions