prestodb/presto · error · PrestoException

INVALID_CAST_ARGUMENT

INVALID_CAST_ARGUMENT

Error message

Unable to cast %s to integer

What it means

Casting a DOUBLE to INTEGER fails when the value is NaN, infinite, or outside the 32-bit int range. DoubleMath.roundToInt(value, HALF_UP) throws ArithmeticException, which is wrapped as INVALID_CAST_ARGUMENT.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/type/DoubleOperators.java:129

        return -value;
    }

    @ScalarOperator(CAST)
    @SqlType(StandardTypes.BOOLEAN)
    public static boolean castToBoolean(@SqlType(StandardTypes.DOUBLE) double value)
    {
        return value != 0;
    }

    @ScalarOperator(CAST)
    @SqlType(StandardTypes.INTEGER)
    public static long castToInteger(@SqlType(StandardTypes.DOUBLE) double value)
    {
        try {
            return DoubleMath.roundToInt(value, HALF_UP);
        }
        catch (ArithmeticException e) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, format("Unable to cast %s to integer", value), e);
        }
    }

    @ScalarOperator(CAST)
    @SqlType(StandardTypes.SMALLINT)
    public static long castToSmallint(@SqlType(StandardTypes.DOUBLE) double value)
    {
        try {
            return Shorts.checkedCast(DoubleMath.roundToInt(value, HALF_UP));
        }
        catch (ArithmeticException | IllegalArgumentException e) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, format("Unable to cast %s to smallint", value), e);
        }
    }

    @ScalarOperator(CAST)
    @SqlType(StandardTypes.TINYINT)
    public static long castToTinyint(@SqlType(StandardTypes.DOUBLE) double value)

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Clamp or validate the double before casting (LEAST/GREATEST against INT bounds)
  2. Filter out NaN/Infinity rows: WHERE is_finite(x)
  3. Cast to BIGINT instead if values legitimately exceed int range
  4. Fix upstream data producing NaN/Infinity

Example fix

// before (SQL)
SELECT CAST(x AS INTEGER) FROM t
// after
SELECT CAST(LEAST(GREATEST(x, -2147483648), 2147483647) AS INTEGER) FROM t WHERE is_finite(x)
Defensive patterns

Strategy: validation

Validate before calling

SELECT * FROM t WHERE NOT is_finite(x) OR x > 2147483647 OR x < -2147483648; -- find bad rows first

Try / catch

SELECT try(CAST(x AS INTEGER)) FROM t -- NULL for out-of-range

Prevention

When it happens

Trigger: CAST(double_col AS INTEGER) where the double is NaN, +/-Infinity, or outside [-2147483648, 2147483647] after rounding (castToInteger in DoubleOperators).

Common situations: Divisions that produced Infinity (pre-strict engines), NaN from sqrt(-1)-style math, loading float data that exceeds INT range into an INT column.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/3c6911b58db3070c. Report an issue: GitHub.