{"record":{"id":"0183dec8d259b2e6","repo":"prestodb/presto","slug":"invalid-cast-argument-0183de","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"Invalid UUID string length: ","messagePattern":"Invalid UUID string length: ","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/type/UuidOperators.java","lineNumber":139,"sourceCode":"\n    @ScalarOperator(XX_HASH_64)\n    @SqlType(StandardTypes.BIGINT)\n    public static long xxHash64(@SqlType(StandardTypes.UUID) Slice value)\n    {\n        return XxHash64.hash(value);\n    }\n\n    @LiteralParameters(\"x\")\n    @ScalarOperator(CAST)\n    @SqlType(StandardTypes.UUID)\n    public static Slice castFromVarcharToUuid(@SqlType(\"varchar(x)\") Slice slice)\n    {\n        try {\n            java.util.UUID uuid = java.util.UUID.fromString(slice.toStringUtf8());\n            if (slice.length() == 36) {\n                return javaUuidToPrestoUuid(uuid);\n            }\n            throw new PrestoException(INVALID_CAST_ARGUMENT, \"Invalid UUID string length: \" + slice.length());\n        }\n        catch (IllegalArgumentException e) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, \"Cannot cast value to UUID: \" + slice.toStringUtf8());\n        }\n    }\n\n    @ScalarOperator(CAST)\n    @SqlType(StandardTypes.VARCHAR)\n    public static Slice castFromUuidToVarchar(@SqlType(StandardTypes.UUID) Slice slice)\n    {\n        return utf8Slice(prestoUuidToJavaUuid(slice).toString());\n    }\n\n    @ScalarOperator(CAST)\n    @SqlType(StandardTypes.UUID)\n    public static Slice castFromVarbinaryToUuid(@SqlType(\"varbinary\") Slice slice)\n    {\n        if (slice.length() == 16) {","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/type/UuidOperators.java#L121-L157","documentation":"Thrown when casting VARCHAR to UUID if the string parses as a UUID (via java.util.UUID.fromString) but is not exactly 36 characters long, which Presto requires. It is an INVALID_CAST_ARGUMENT error raised inside castFromVarcharToUuid.","triggerScenarios":"CAST(varchar_col AS UUID) where the string is a valid UUID text but with length != 36 (e.g. braced '{...}' form 38 chars, URN form, or trimmed/hyphen-less 32-char form); note a 32-char hyphen-less string usually fails fromString first and hits the companion 'Cannot cast value to UUID' error.","commonSituations":"UUIDs exported with braces or 'urn:uuid:' prefixes by other systems; whitespace or padding around the value inflating the length; joining UUID columns whose textual representations differ by format.","solutions":["Normalize the string before casting: trim and strip braces/urn prefix so it is exactly 36 chars","Use regexp_replace to remove surrounding characters: CAST(regexp_replace(col, '[{}]', '') AS UUID)","Fix the upstream producer to emit canonical 36-char lowercase-hyphen UUIDs","Use try_cast to identify offending rows without failing the query"],"exampleFix":"// before\nSELECT CAST(id_str AS UUID) FROM t;\n// after\nSELECT CAST(regexp_replace(trim(id_str), '[{}]|[uU][rR][nN]:[uU][uU][iI][dD]:', '') AS UUID) FROM t;","handlingStrategy":"validation","validationCode":"-- Presto SQL: ensure canonical 36-char UUID text before casting\nSELECT * FROM t\nWHERE length(trim(id_str)) = 36\n  AND regexp_like(trim(id_str), '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$');","typeGuard":"// JS-side guard for canonical UUID strings\nfunction isCanonicalUuid(s) {\n  return typeof s === 'string' && s.length === 36 &&\n    /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(s);\n}","tryCatchPattern":"SELECT try_cast(id_str AS UUID) AS id FROM t; -- NULL instead of failure","preventionTips":["Strip braces and 'urn:uuid:' prefixes before casting","Emit canonical 36-char hyphenated UUIDs from upstream systems","trim() varchar columns that come from files or external feeds","Use try_cast for exploratory queries on untrusted text"],"tags":["presto","uuid","cast","varchar"],"backgroundTag":"invalid-cast-argument","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"}