prestodb/presto · error · PrestoException
INVALID_CAST_ARGUMENT
INVALID_CAST_ARGUMENT
Error message
Invalid UUID string length:
What it means
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.
Source
Thrown at presto-main-base/src/main/java/com/facebook/presto/type/UuidOperators.java:139
@ScalarOperator(XX_HASH_64)
@SqlType(StandardTypes.BIGINT)
public static long xxHash64(@SqlType(StandardTypes.UUID) Slice value)
{
return XxHash64.hash(value);
}
@LiteralParameters("x")
@ScalarOperator(CAST)
@SqlType(StandardTypes.UUID)
public static Slice castFromVarcharToUuid(@SqlType("varchar(x)") Slice slice)
{
try {
java.util.UUID uuid = java.util.UUID.fromString(slice.toStringUtf8());
if (slice.length() == 36) {
return javaUuidToPrestoUuid(uuid);
}
throw new PrestoException(INVALID_CAST_ARGUMENT, "Invalid UUID string length: " + slice.length());
}
catch (IllegalArgumentException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast value to UUID: " + slice.toStringUtf8());
}
}
@ScalarOperator(CAST)
@SqlType(StandardTypes.VARCHAR)
public static Slice castFromUuidToVarchar(@SqlType(StandardTypes.UUID) Slice slice)
{
return utf8Slice(prestoUuidToJavaUuid(slice).toString());
}
@ScalarOperator(CAST)
@SqlType(StandardTypes.UUID)
public static Slice castFromVarbinaryToUuid(@SqlType("varbinary") Slice slice)
{
if (slice.length() == 16) {View on GitHub (pinned to 55bb57d202)
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
Example fix
// before
SELECT CAST(id_str AS UUID) FROM t;
// after
SELECT CAST(regexp_replace(trim(id_str), '[{}]|[uU][rR][nN]:[uU][uU][iI][dD]:', '') AS UUID) FROM t; Defensive patterns
Strategy: validation
Validate before calling
-- Presto SQL: ensure canonical 36-char UUID text before casting
SELECT * FROM t
WHERE length(trim(id_str)) = 36
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}$'); Type guard
// JS-side guard for canonical UUID strings
function isCanonicalUuid(s) {
return typeof s === 'string' && s.length === 36 &&
/^[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);
} Try / catch
SELECT try_cast(id_str AS UUID) AS id FROM t; -- NULL instead of failure
Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- INVALID_CAST_ARGUMENT
- INVALID_CAST_ARGUMENT
- INVALID_CAST_ARGUMENT
- INVALID_CAST_ARGUMENT
- INVALID_CAST_ARGUMENT
AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04).
Data as JSON: /api/errors/0183dec8d259b2e6.
Report an issue: GitHub.