{"record":{"id":"10b1f6c8dad35299","repo":"prestodb/presto","slug":"invalid-cast-argument-10b1f6","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"Value cannot be cast to timestamp: ","messagePattern":"Value cannot be cast to timestamp: ","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/type/TimestampOperators.java","lineNumber":212,"sourceCode":"            return utf8Slice(printTimestampWithoutTimeZone(properties.getTimeZoneKey(), value));\n        }\n        else {\n            return utf8Slice(printTimestampWithoutTimeZone(value));\n        }\n    }\n\n    @ScalarOperator(CAST)\n    @LiteralParameters(\"x\")\n    @SqlType(StandardTypes.TIMESTAMP)\n    public static long castFromSlice(SqlFunctionProperties properties, @SqlType(\"varchar(x)\") Slice value)\n    {\n        // This accepts value with or without time zone\n        if (properties.isLegacyTimestamp()) {\n            try {\n                return parseTimestampWithoutTimeZone(properties.getTimeZoneKey(), trim(value).toStringUtf8());\n            }\n            catch (IllegalArgumentException e) {\n                throw new PrestoException(INVALID_CAST_ARGUMENT, \"Value cannot be cast to timestamp: \" + value.toStringUtf8(), e);\n            }\n        }\n        else {\n            try {\n                return parseTimestampWithoutTimeZone(trim(value).toStringUtf8());\n            }\n            catch (IllegalArgumentException e) {\n                throw new PrestoException(INVALID_CAST_ARGUMENT, \"Value cannot be cast to timestamp: \" + value.toStringUtf8(), e);\n            }\n        }\n    }\n\n    @ScalarOperator(HASH_CODE)\n    @SqlType(StandardTypes.BIGINT)\n    public static long hashCode(@SqlType(StandardTypes.TIMESTAMP) long value)\n    {\n        return AbstractLongType.hash(value);\n    }","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/type/TimestampOperators.java#L194-L230","documentation":"Thrown by the VARCHAR->TIMESTAMP cast when legacy timestamp semantics are enabled: the string (after trimming) cannot be parsed by parseTimestampWithoutTimeZone with the session time zone, so IllegalArgumentException is wrapped as INVALID_CAST_ARGUMENT. The literal must be a valid timestamp without (or in legacy mode, with) a time zone.","triggerScenarios":"CAST(varchar_col AS TIMESTAMP) under legacy_timestamp=true where the string is malformed ('2024-13-01', 'now', '2024/01/02 10:00'), or includes timezone info the legacy parser rejects.","commonSituations":"Clusters still on legacy timestamp behavior parsing mixed-format log dates; ingestion pipelines feeding 'MM/DD/YYYY' style strings; strings with 'Z' or offsets under legacy parsing.","solutions":["Reformat the string to ISO 8601 'YYYY-MM-DD HH:mm:ss[.SSS]' before casting.","Use try_cast(col AS TIMESTAMP) to skip/NULL bad rows.","If strings carry offsets, cast to TIMESTAMP WITH TIME ZONE or strip the offset.","Check the session/catalog legacy timestamp property and align parsing accordingly (consider migrating off legacy_timestamp)."],"exampleFix":"// before\nSELECT CAST(ts_text AS TIMESTAMP) FROM logs; -- '01/02/2024 10:00'\n// after\nSELECT CAST(date_parse(ts_text, '%m/%d/%Y %H:%i') AS TIMESTAMP) FROM logs;","handlingStrategy":"try-catch","validationCode":"-- Guard: ISO-like timestamp shape check before casting\nSELECT * FROM t WHERE regexp_like(ts_str,\n  '^\\d{4}-\\d{2}-\\d{2}[ T]\\d{2}:\\d{2}:\\d{2}(\\.\\d{1,9})?([+-]\\d{2}:?\\d{2}|Z)?$');","typeGuard":"SELECT CASE WHEN regexp_like(ts_str, '^\\d{4}-\\d{2}-\\d{2}[ T]\\d{2}:\\d{2}:\\d{2}.*$')\n       THEN CAST(ts_str AS TIMESTAMP) END AS safe_ts FROM t;","tryCatchPattern":"// JDBC\ntry { rs = stmt.executeQuery(tsCastSql); }\ncatch (SQLException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"Value cannot be cast to timestamp\")) {\n    // inspect offending literal; rerun with try_cast or date_parse\n  } else throw e;\n}","preventionTips":["Check the cluster's legacy timestamp setting; format strings accordingly.","Standardize on ISO 8601 in upstream systems.","Use try_cast when data quality is uncertain.","Convert 'MM/DD/YYYY' inputs with date_parse before casting."],"tags":["presto","cast","timestamp","parse","legacy"],"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"}