{"record":{"id":"08a1bb2374011ba2","repo":"prestodb/presto","slug":"invalid-cast-argument-08a1bb","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"Cannot cast '%s' to DOUBLE","messagePattern":"Cannot cast '(.+?)' to DOUBLE","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/type/CharOperators.java","lineNumber":184,"sourceCode":"\n    @LiteralParameters(\"x\")\n    @ScalarOperator(INDETERMINATE)\n    @SqlType(StandardTypes.BOOLEAN)\n    public static boolean indeterminate(@SqlType(\"char(x)\") Slice value, @IsNull boolean isNull)\n    {\n        return isNull;\n    }\n\n    @LiteralParameters(\"x\")\n    @ScalarOperator(CAST)\n    @SqlType(StandardTypes.DOUBLE)\n    public static double castToDouble(@SqlType(\"char(x)\") Slice slice)\n    {\n        try {\n            return Double.parseDouble(slice.toStringUtf8());\n        }\n        catch (Exception e) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, format(\"Cannot cast '%s' to DOUBLE\", slice.toStringUtf8()));\n        }\n    }\n\n    @LiteralParameters(\"x\")\n    @ScalarOperator(CAST)\n    @SqlType(StandardTypes.REAL)\n    public static long castToFloat(@SqlType(\"char(x)\") Slice slice)\n    {\n        try {\n            return Float.floatToIntBits(Float.parseFloat(slice.toStringUtf8()));\n        }\n        catch (Exception e) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, format(\"Cannot cast '%s' to REAL\", slice.toStringUtf8()));\n        }\n    }\n\n    @LiteralParameters(\"x\")\n    @ScalarOperator(CAST)","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/type/CharOperators.java#L166-L202","documentation":"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.","triggerScenarios":"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.","commonSituations":"Parsing numeric columns stored as text that contain whitespace-only chars, commas as thousand separators, currency symbols, locale-formatted decimals, or empty strings.","solutions":["Clean the text first with trim/replace: CAST(replace(trim(char_col), ',', '') AS DOUBLE).","Use TRY_CAST(char_col AS DOUBLE) to get NULL for unparsable rows.","Filter invalid rows: WHERE trim(char_col) <> '' AND char_col NOT LIKE '%[^0-9.eE+-]%'-style checks.","Fix the upstream writer so numeric values are stored in typed columns, not CHAR."],"exampleFix":"// before\nSELECT CAST(value AS DOUBLE) FROM t; -- value = '1,234.5'\n\n// after\nSELECT TRY_CAST(replace(trim(value), ',', '') AS DOUBLE) FROM t;","handlingStrategy":"try-catch","validationCode":"-- validate the text parses as a double before casting\nSELECT * FROM t\nWHERE regexp_like(trim(char_col), '^[+-]?([0-9]+(\\\\.[0-9]*)?|\\\\.[0-9]+)([eE][+-]?[0-9]+)?$');","typeGuard":"-- SQL predicate: char value is double-safe\nregexp_like(trim(char_col), '^[+-]?([0-9]+(\\\\.[0-9]*)?|\\\\.[0-9]+)([eE][+-]?[0-9]+)?$')","tryCatchPattern":"SELECT TRY_CAST(char_col AS DOUBLE) FROM t; -- NULL instead of INVALID_CAST_ARGUMENT","preventionTips":["Always trim CHAR values; fixed-width padding can break parsing.","Strip thousands separators and currency symbols before casting.","Prefer typed numeric columns over storing numbers in CHAR.","Use TRY_CAST consistently in pipelines that ingest external text data."],"tags":["presto","sql","cast","char","parse-error"],"backgroundTag":"invalid-cast-to-number","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}