{"record":{"id":"0dfe98db22301faa","repo":"prestodb/presto","slug":"invalid-cast-argument-0dfe98","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"Value cannot be cast to time: ","messagePattern":"Value cannot be cast to time: ","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/type/TimeOperators.java","lineNumber":182,"sourceCode":"            return utf8Slice(printTimeWithoutTimeZone(value));\n        }\n    }\n\n    @ScalarOperator(CAST)\n    @LiteralParameters(\"x\")\n    @SqlType(StandardTypes.TIME)\n    public static long castFromSlice(SqlFunctionProperties properties, @SqlType(\"varchar(x)\") Slice value)\n    {\n        try {\n            if (properties.isLegacyTimestamp()) {\n                return parseTimeWithoutTimeZone(properties.getTimeZoneKey(), value.toStringUtf8());\n            }\n            else {\n                return parseTimeWithoutTimeZone(value.toStringUtf8());\n            }\n        }\n        catch (IllegalArgumentException e) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, \"Value cannot be cast to time: \" + value.toStringUtf8(), e);\n        }\n    }\n\n    @ScalarOperator(HASH_CODE)\n    @SqlType(StandardTypes.BIGINT)\n    public static long hashCode(@SqlType(StandardTypes.TIME) long value)\n    {\n        return AbstractLongType.hash(value);\n    }\n\n    @ScalarOperator(XX_HASH_64)\n    @SqlType(StandardTypes.BIGINT)\n    public static long xxHash64(@SqlType(StandardTypes.TIME) long value)\n    {\n        return XxHash64.hash(value);\n    }\n\n    @ScalarOperator(IS_DISTINCT_FROM)","sourceCodeStart":164,"sourceCodeEnd":200,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/type/TimeOperators.java#L164-L200","documentation":"Thrown by the VARCHAR->TIME cast implementation (castFromSlice) when the string cannot be parsed as a time-without-time-zone value. parseTimeWithoutTimeZone throws IllegalArgumentException and Presto wraps it as INVALID_CAST_ARGUMENT, meaning the literal is not a valid 'HH:mm:ss[.fff]' style time.","triggerScenarios":"CAST(varchar_col AS TIME) or CREATE TABLE ... AS with string literals where the text is not a parseable time, e.g. '25:00', '12:60:00', 'morning', or includes an unexpected timezone/date part.","commonSituations":"Loading CSV/object-store data where time strings vary in format ('9:30 AM' vs '09:30:00'); upstream systems exporting localized times; values with trailing whitespace or stray characters.","solutions":["Normalize the string to ISO 'HH:mm:ss' format before casting (date_parse/regexp operations).","Use try_cast(col AS TIME) and coalesce to NULL or a default for unparseable rows.","Inspect the offending value in the message and fix the source data / parsing expression.","If values carry a timezone or date portion, cast to TIMESTAMP or TIME WITH TIME ZONE instead."],"exampleFix":"// before\nSELECT CAST(event_time AS TIME) FROM events; -- '9:30 AM'\n// after\nSELECT CAST(date_format(date_parse(event_time, '%h:%i %p'), '%H:%i:%s') AS TIME) FROM events;","handlingStrategy":"try-catch","validationCode":"-- Guard: regex check for strict HH:mm:ss format before casting\nSELECT * FROM t WHERE regexp_like(time_str, '^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\\.[0-9]{1,9})?$');","typeGuard":"SELECT CASE WHEN regexp_like(time_str, '^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\\.[0-9]+)?$')\n       THEN CAST(time_str AS TIME) END AS safe_time FROM t;","tryCatchPattern":"// JDBC\ntry { rs = stmt.executeQuery(timeCastSql); }\ncatch (SQLException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"Value cannot be cast to time\")) {\n    // log offending value from message; rerun with try_cast\n  } else throw e;\n}","preventionTips":["Normalize all time strings to ISO HH:mm:ss at ingestion.","Use try_cast for user-sourced string columns.","Reject 12-hour formats ('9:30 AM') or convert them explicitly.","Profile distinct formats with regexp before batch casts."],"tags":["presto","cast","time","parse","invalid-format"],"backgroundTag":"date-parse-failed","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"}