{"record":{"id":"eaa808a830cbb4f5","repo":"prestodb/presto","slug":"invalid-cast-argument-eaa808","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"Value %s cannot be represented as varchar(%s)","messagePattern":"Value (.+?) cannot be represented as varchar\\((.+?)\\)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/type/BigintOperators.java","lineNumber":284,"sourceCode":"    @SqlType(StandardTypes.REAL)\n    public static long castToReal(@SqlType(StandardTypes.BIGINT) long value)\n    {\n        return (long) floatToRawIntBits((float) value);\n    }\n\n    @ScalarOperator(CAST)\n    @LiteralParameters(\"x\")\n    @SqlType(\"varchar(x)\")\n    public static Slice castToVarchar(@LiteralParameter(\"x\") long x, @SqlType(StandardTypes.BIGINT) long value)\n    {\n        // todo optimize me\n        String stringValue = String.valueOf(value);\n        // String is all-ASCII, so String.length() here returns actual code points count\n        if (stringValue.length() <= x) {\n            return utf8Slice(stringValue);\n        }\n\n        throw new PrestoException(INVALID_CAST_ARGUMENT, format(\"Value %s cannot be represented as varchar(%s)\", value, x));\n    }\n\n    @ScalarOperator(HASH_CODE)\n    @SqlType(StandardTypes.BIGINT)\n    public static long hashCode(@SqlType(StandardTypes.BIGINT) long value)\n    {\n        return AbstractLongType.hash(value);\n    }\n\n    @ScalarOperator(IS_DISTINCT_FROM)\n    public static class BigintDistinctFromOperator\n    {\n        @SqlType(StandardTypes.BOOLEAN)\n        public static boolean isDistinctFrom(\n                @SqlType(StandardTypes.BIGINT) long left,\n                @IsNull boolean leftNull,\n                @SqlType(StandardTypes.BIGINT) long right,\n                @IsNull boolean rightNull)","sourceCodeStart":266,"sourceCodeEnd":302,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/type/BigintOperators.java#L266-L302","documentation":"Thrown by BigintOperators.castToVarchar when casting a BIGINT to a fixed-length VARCHAR(x) whose length is shorter than the decimal string representation of the value. Since the string is ASCII, String.length() equals the code point count; if it exceeds x, Presto throws INVALID_CAST_ARGUMENT rather than truncating.","triggerScenarios":"Executing CAST(col AS VARCHAR(x)) on a BIGINT whose decimal digit count (plus possible '-' sign) exceeds x, e.g. CAST(123456 AS VARCHAR(3)), via the @ScalarOperator(CAST) bigint-to-varchar operator.","commonSituations":"Formatting IDs/codes into fixed-width char columns in ETL, migrating to schemas with varchar(n) too small, casting values that can be negative where the '-' sign pushes length over the limit.","solutions":["Cast to an unbounded VARCHAR first: CAST(CAST(col AS VARCHAR) AS VARCHAR(x)) only after ensuring it fits, or enlarge the target length.","Choose a sufficient width: VARCHAR(20) is always enough for signed 64-bit values.","Filter or NULL out rows that don't fit: WHERE length(CAST(col AS VARCHAR)) <= x.","Use TRY(CAST(col AS VARCHAR(x))) to map offenders to NULL."],"exampleFix":"// before\nSELECT CAST(id AS VARCHAR(5)) FROM t;\n\n// after\nSELECT TRY(CAST(id AS VARCHAR(20))) FROM t; -- width big enough for any bigint","handlingStrategy":"validation","validationCode":"-- string form must fit the target varchar length\nSELECT * FROM t WHERE length(CAST(col AS VARCHAR)) <= x; -- e.g. x = 20 covers all bigints","typeGuard":"-- SQL predicate: value fits varchar(n)\nlength(CAST(col AS VARCHAR)) <= n","tryCatchPattern":"SELECT TRY(CAST(col AS VARCHAR(n))) FROM t; -- NULL when too long","preventionTips":["Use VARCHAR(20) for any signed 64-bit value (19 digits + sign).","Cast through plain VARCHAR when a fixed length isn't required.","Account for the '-' sign on negatives when picking n.","Use TRY_CAST in ETL to avoid failing whole queries on width mismatches."],"tags":["presto","sql","cast","varchar","invalid-argument"],"backgroundTag":"varchar-length-overflow","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"}