{"record":{"id":"3c6911b58db3070c","repo":"prestodb/presto","slug":"invalid-cast-argument-3c6911","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"Unable to cast %s to integer","messagePattern":"Unable to cast (.+?) to integer","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/type/DoubleOperators.java","lineNumber":129,"sourceCode":"        return -value;\n    }\n\n    @ScalarOperator(CAST)\n    @SqlType(StandardTypes.BOOLEAN)\n    public static boolean castToBoolean(@SqlType(StandardTypes.DOUBLE) double value)\n    {\n        return value != 0;\n    }\n\n    @ScalarOperator(CAST)\n    @SqlType(StandardTypes.INTEGER)\n    public static long castToInteger(@SqlType(StandardTypes.DOUBLE) double value)\n    {\n        try {\n            return DoubleMath.roundToInt(value, HALF_UP);\n        }\n        catch (ArithmeticException e) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, format(\"Unable to cast %s to integer\", value), e);\n        }\n    }\n\n    @ScalarOperator(CAST)\n    @SqlType(StandardTypes.SMALLINT)\n    public static long castToSmallint(@SqlType(StandardTypes.DOUBLE) double value)\n    {\n        try {\n            return Shorts.checkedCast(DoubleMath.roundToInt(value, HALF_UP));\n        }\n        catch (ArithmeticException | IllegalArgumentException e) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, format(\"Unable to cast %s to smallint\", value), e);\n        }\n    }\n\n    @ScalarOperator(CAST)\n    @SqlType(StandardTypes.TINYINT)\n    public static long castToTinyint(@SqlType(StandardTypes.DOUBLE) double value)","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/type/DoubleOperators.java#L111-L147","documentation":"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.","triggerScenarios":"CAST(double_col AS INTEGER) where the double is NaN, +/-Infinity, or outside [-2147483648, 2147483647] after rounding (castToInteger in DoubleOperators).","commonSituations":"Divisions that produced Infinity (pre-strict engines), NaN from sqrt(-1)-style math, loading float data that exceeds INT range into an INT column.","solutions":["Clamp or validate the double before casting (LEAST/GREATEST against INT bounds)","Filter out NaN/Infinity rows: WHERE is_finite(x)","Cast to BIGINT instead if values legitimately exceed int range","Fix upstream data producing NaN/Infinity"],"exampleFix":"// before (SQL)\nSELECT CAST(x AS INTEGER) FROM t\n// after\nSELECT CAST(LEAST(GREATEST(x, -2147483648), 2147483647) AS INTEGER) FROM t WHERE is_finite(x)","handlingStrategy":"validation","validationCode":"SELECT * FROM t WHERE NOT is_finite(x) OR x > 2147483647 OR x < -2147483648; -- find bad rows first","typeGuard":null,"tryCatchPattern":"SELECT try(CAST(x AS INTEGER)) FROM t -- NULL for out-of-range","preventionTips":["Check MIN/MAX of the double column before casting to INTEGER","Handle NaN/Infinity explicitly before casting","Clamp with LEAST/GREATEST when truncation is acceptable"],"tags":["cast","overflow","double","integer","presto"],"backgroundTag":"numeric-value-out-of-range","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"}