{"record":{"id":"33746e00e065e60c","repo":"prestodb/presto","slug":"invalid-cast-argument-33746e","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"Unable to cast %s to bigint","messagePattern":"Unable to cast (.+?) to bigint","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/type/RealOperators.java","lineNumber":127,"sourceCode":"    }\n\n    @ScalarOperator(CAST)\n    @LiteralParameters(\"x\")\n    @SqlType(\"varchar(x)\")\n    public static Slice castToVarchar(@SqlType(StandardTypes.REAL) long value)\n    {\n        return utf8Slice(String.valueOf(intBitsToFloat((int) value)));\n    }\n\n    @ScalarOperator(CAST)\n    @SqlType(StandardTypes.BIGINT)\n    public static long castToLong(@SqlType(StandardTypes.REAL) long value)\n    {\n        try {\n            return DoubleMath.roundToLong(intBitsToFloat((int) value), HALF_UP);\n        }\n        catch (ArithmeticException e) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, format(\"Unable to cast %s to bigint\", intBitsToFloat((int) value)), e);\n        }\n    }\n\n    @ScalarOperator(CAST)\n    @SqlType(StandardTypes.INTEGER)\n    public static long castToInteger(@SqlType(StandardTypes.REAL) long value)\n    {\n        try {\n            return DoubleMath.roundToInt(intBitsToFloat((int) value), HALF_UP);\n        }\n        catch (ArithmeticException e) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, format(\"Unable to cast %s to integer\", intBitsToFloat((int) value)), e);\n        }\n    }\n\n    @ScalarOperator(CAST)\n    @SqlType(StandardTypes.SMALLINT)\n    public static long castToSmallint(@SqlType(StandardTypes.REAL) long value)","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/type/RealOperators.java#L109-L145","documentation":"castToLong implements CAST(REAL AS BIGINT) using Guava's DoubleMath.roundToLong with HALF_UP rounding. roundToLong throws ArithmeticException when the float is NaN or its magnitude exceeds the BIGINT range; the operator rethrows it as INVALID_CAST_ARGUMENT including the offending float value.","triggerScenarios":"CAST(real_col AS BIGINT) where the value is NaN, +Infinity/-Infinity, or outside [-2^63, 2^63) after rounding (e.g., 3.5e38 or 9.3e18-ish overflow).","commonSituations":"Aggregations (SUM of large reals) overflowing bigint range; imported sensor/measurement data containing NaN/Infinity; scientific data that was always out of bigint range.","solutions":["Filter out non-finite and out-of-range rows first: WHERE is_finite(x) AND x BETWEEN -9223372036854775808 AND 9223372036854775807.","Use COALESCE/NULLIF to map NaN/Inf to NULL before casting, e.g. CAST(NULLIF(x, infinity()) AS BIGINT).","If the data is genuinely huge, keep it as REAL/DOUBLE instead of casting to BIGINT."],"exampleFix":"// before\nSELECT CAST(metric AS BIGINT) FROM readings; -- NaN rows fail\n// after\nSELECT CAST(NULLIF(metric, infinity()) AS BIGINT) FROM readings WHERE is_finite(metric);","handlingStrategy":"validation","validationCode":"-- guard before casting REAL to BIGINT\nSELECT CAST(x AS BIGINT)\nFROM t\nWHERE is_finite(x) AND x BETWEEN -9223372036854775808 AND 9223372036854775807;","typeGuard":"boolean isCastableToBigint(double v) {\n    return !Double.isNaN(v) && !Double.isInfinite(v) && v >= -9.223372036854776E18 && v < 9.223372036854776E18;\n}","tryCatchPattern":"try {\n    return castToLong(value);\n} catch (PrestoException e) {\n    if (INVALID_CAST_ARGUMENT.equals(e.getErrorCode()) && e.getMessage().startsWith(\"Unable to cast\")) {\n        return null; // or a configured sentinel\n    }\n    throw e;\n}","preventionTips":["Always filter or NULLIF NaN/Infinity before casting REAL to an integer type.","Range-check against the target integer type's bounds; prefer BIGINT for large magnitudes.","Sanitize upstream data pipelines so NaN/Inf never reach cast expressions."],"tags":["presto","cast","real","bigint","overflow"],"backgroundTag":"numeric-cast-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"}