{"record":{"id":"d81410710979d846","repo":"prestodb/presto","slug":"invalid-cast-argument-d81410","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"Value cannot be cast to timestamp with time zone: ","messagePattern":"Value cannot be cast to timestamp with time zone: ","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/type/TimestampWithTimeZoneOperators.java","lineNumber":200,"sourceCode":"\n    @ScalarOperator(CAST)\n    @LiteralParameters(\"x\")\n    @SqlType(\"varchar(x)\")\n    public static Slice castToSlice(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long value)\n    {\n        return utf8Slice(printTimestampWithTimeZone(value));\n    }\n\n    @ScalarOperator(CAST)\n    @LiteralParameters(\"x\")\n    @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)\n    public static long castFromSlice(SqlFunctionProperties properties, @SqlType(\"varchar(x)\") Slice value)\n    {\n        try {\n            return parseTimestampWithTimeZone(properties.getTimeZoneKey(), trim(value).toStringUtf8());\n        }\n        catch (IllegalArgumentException e) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, \"Value cannot be cast to timestamp with time zone: \" + value.toStringUtf8(), e);\n        }\n    }\n\n    @ScalarOperator(HASH_CODE)\n    @SqlType(StandardTypes.BIGINT)\n    public static long hashCode(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long value)\n    {\n        return AbstractLongType.hash(unpackMillisUtc(value));\n    }\n\n    @ScalarOperator(XX_HASH_64)\n    @SqlType(StandardTypes.BIGINT)\n    public static long xxHash64(@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) long value)\n    {\n        return XxHash64.hash(unpackMillisUtc(value));\n    }\n\n    @ScalarOperator(IS_DISTINCT_FROM)","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/type/TimestampWithTimeZoneOperators.java#L182-L218","documentation":"This error is thrown when a VARCHAR value cannot be parsed into a TIMESTAMP WITH TIME ZONE. castFromSlice trims the input string and calls parseTimestampWithTimeZone; if the parser raises IllegalArgumentException (bad format, unparseable date/time, unknown zone), Presto wraps it in INVALID_CAST_ARGUMENT with the offending value in the message. It backs the SQL CAST(varchar AS timestamp with time zone) operator.","triggerScenarios":"CAST('not-a-date' AS timestamp with time zone); a varchar column holding malformed timestamps passed through the cast; strings using a date/time format the parser does not accept (e.g. wrong separator, missing time zone offset); empty or whitespace-only strings.","commonSituations":"ETL pipelines ingesting text files/CSVs with inconsistent timestamp formats; locale-specific date strings ('12/31/2021') that the ISO-style parser rejects; time zone names misspelled or not in the IANA database; upstream schema changes silently altering column formats.","solutions":["Inspect the failing value in the message and correct the source data or expression","Normalize the string first with date_format/date_parse or a regex to the expected 'YYYY-MM-DD HH:MM:SS ±zone' format","Use try_cast instead of cast to get NULL instead of a query failure for bad rows","Filter or route unparseable rows out with a WHERE ... IS NOT NULL after try_cast"],"exampleFix":"// before\nSELECT CAST(ts_str AS timestamp with time zone) FROM events;\n// after\nSELECT try_cast(ts_str AS timestamp with time zone) AS ts, ts_str FROM events WHERE try_cast(ts_str AS timestamp with time zone) IS NULL; -- inspect bad rows","handlingStrategy":"validation","validationCode":"// Presto SQL: pre-check before CAST\nSELECT * FROM t\nWHERE ts_str IS NOT NULL\n  AND regexp_like(trim(ts_str), '^\\\\d{4}-\\\\d{2}-\\\\d{2} [\\\\d:]+( [+-]\\\\d{2}:?\\\\d{2}| .+)?$');","typeGuard":null,"tryCatchPattern":"// Use try_cast to avoid hard failure\nSELECT try_cast(ts_str AS timestamp with time zone) AS ts FROM t; -- NULL for bad rows","preventionTips":["Standardize all timestamp strings to a canonical format at ingestion","Always prefer try_cast in exploratory/ETL queries over raw cast","Reject or quarantine unparseable rows with an explicit IS NULL filter","Validate time zone names against the IANA database upstream"],"tags":["presto","cast","timestamp","type-conversion"],"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"}