Skip to content

try_cast_literal_to_type doesn't respect timestamp precision #5507

Description

@vrongmeal

Describe the bug

In the function try_cast_literal_to_type: https://github.com/apache/arrow-datafusion/blob/928662bb12d915aef83abba1312392d25770f68f/datafusion/optimizer/src/unwrap_cast_in_comparison.rs#L295

Converting from Timestamp(Nanosecond, _) to Timestamp(Microsecond, _) doesn't actually return the correct value. It simply interchanges the inner integer.

To Reproduce

Running the following:

#[test]
fn test_repro() {
    let new_scalar = try_cast_literal_to_type(
        &ScalarValue::TimestampNanosecond(Some(123456), None),
        &DataType::Timestamp(TimeUnit::Microsecond, None)
    ).unwrap().unwrap();

    // This passes which is wrong
    assert_eq!(new_scalar, ScalarValue::TimestampMicrosecond(Some(123456), None));

    // This fails!
    // assert_eq!(new_scalar, ScalarValue::TimestampMicrosecond(Some(123), None));
}

Expected behavior

// This should be the correct result
assert_eq!(new_scalar, ScalarValue::TimestampMicrosecond(Some(123), None));

Additional context

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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