prestodb/presto · error · PrestoException

INVALID_CAST_ARGUMENT

INVALID_CAST_ARGUMENT

Error message

Cannot cast '%s' to DOUBLE

What it means

Thrown by CharOperators.castToDouble when a CHAR(x) value cannot be parsed as a double by Double.parseDouble (empty strings, letters, nulls-in-text, malformed numbers). Presto maps any parse failure to INVALID_CAST_ARGUMENT with the offending text in the message.

Source

Thrown at presto-main-base/src/main/java/com/facebook/presto/type/CharOperators.java:184

    @LiteralParameters("x")
    @ScalarOperator(INDETERMINATE)
    @SqlType(StandardTypes.BOOLEAN)
    public static boolean indeterminate(@SqlType("char(x)") Slice value, @IsNull boolean isNull)
    {
        return isNull;
    }

    @LiteralParameters("x")
    @ScalarOperator(CAST)
    @SqlType(StandardTypes.DOUBLE)
    public static double castToDouble(@SqlType("char(x)") Slice slice)
    {
        try {
            return Double.parseDouble(slice.toStringUtf8());
        }
        catch (Exception e) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to DOUBLE", slice.toStringUtf8()));
        }
    }

    @LiteralParameters("x")
    @ScalarOperator(CAST)
    @SqlType(StandardTypes.REAL)
    public static long castToFloat(@SqlType("char(x)") Slice slice)
    {
        try {
            return Float.floatToIntBits(Float.parseFloat(slice.toStringUtf8()));
        }
        catch (Exception e) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to REAL", slice.toStringUtf8()));
        }
    }

    @LiteralParameters("x")
    @ScalarOperator(CAST)

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Clean the text first with trim/replace: CAST(replace(trim(char_col), ',', '') AS DOUBLE).
  2. Use TRY_CAST(char_col AS DOUBLE) to get NULL for unparsable rows.
  3. Filter invalid rows: WHERE trim(char_col) <> '' AND char_col NOT LIKE '%[^0-9.eE+-]%'-style checks.
  4. Fix the upstream writer so numeric values are stored in typed columns, not CHAR.

Example fix

// before
SELECT CAST(value AS DOUBLE) FROM t; -- value = '1,234.5'

// after
SELECT TRY_CAST(replace(trim(value), ',', '') AS DOUBLE) FROM t;
Defensive patterns

Strategy: try-catch

Validate before calling

-- validate the text parses as a double before casting
SELECT * FROM t
WHERE regexp_like(trim(char_col), '^[+-]?([0-9]+(\\.[0-9]*)?|\\.[0-9]+)([eE][+-]?[0-9]+)?$');

Type guard

-- SQL predicate: char value is double-safe
regexp_like(trim(char_col), '^[+-]?([0-9]+(\\.[0-9]*)?|\\.[0-9]+)([eE][+-]?[0-9]+)?$')

Try / catch

SELECT TRY_CAST(char_col AS DOUBLE) FROM t; -- NULL instead of INVALID_CAST_ARGUMENT

Prevention

When it happens

Trigger: Executing CAST(char_col AS DOUBLE) or char_col::DOUBLE where the char text is not a valid Java double literal, via the @ScalarOperator(CAST) char-to-double operator.

Common situations: Parsing numeric columns stored as text that contain whitespace-only chars, commas as thousand separators, currency symbols, locale-formatted decimals, or empty strings.

Related errors


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