Skip to content

ANSI cast of double/float to int/long falsely reports CAST_OVERFLOW for exactly-representable boundary values (2147483647.0, -2147483648.0, ±2^63) #5673

Description

@peterxcli

Describe the bug

In ANSI mode, cast_float_to_int32_up (native/spark-expr/src/conversion_funcs/numeric.rs:395-396, same shape in cast_float_to_int16_down at :333) detects overflow as

let is_overflow = value.is_nan() || value.abs() as $rust_dest_type == $max_dest_val;

i.e. "the saturating conversion of |value| landed on MAX". That flags every double in [2147483647.0, 2147483648.0) and (-2147483649.0, -2147483647.0] as overflow, including INT_MAX and INT_MIN themselves, which are exactly representable doubles and valid ints. Spark's DoubleExactNumeric.toInt / toLong accept any x with Math.floor(x) <= MaxValue && Math.ceil(x) >= MinValue and return x.toInt / x.toLong; for BIGINT Long.MaxValue.toDouble == 2^63, so Spark accepts ±2^63 and saturates.

Steps to reproduce

SET spark.sql.ansi.enabled = true;
CREATE TABLE t USING parquet AS SELECT * FROM VALUES (2147483647.0D), (-2147483648.0D), (2147483647.5D) AS t(c);
SELECT CAST(c AS INT) FROM t;
SELECT CAST(9223372036854775808.0D AS BIGINT), CAST(-9223372036854775808.0D AS BIGINT);

Spark: 2147483647, -2147483648, 2147483647; 9223372036854775807, -9223372036854775808. Comet: CAST_OVERFLOW error for every one of them. Same for FLOAT sources.

Expected behavior

Match Spark's bound check: only values outside [MinValue, MaxValue] after floor/ceil overflow.

Proposed solution

let is_overflow = value.is_nan()
    || !(value.floor() <= $max_dest_val as $float_ty && value.ceil() >= $min_dest_val as $float_ty);
// then `value as $rust_dest_type` — Rust `as` saturates exactly like JVM d2i/d2l

Apply to both macros and add the boundary values to CometNativeCastSuite in ANSI mode.

Additional context

Default-on: CometCast.canCastFromDouble / canCastFromFloat mark Int/Long as Compatible in all eval modes. The check dates from #350; #4941 and the #5128 plan intend to keep "the saturation-based overflow detection exactly as written", so a performance rewrite would carry the bug forward.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions