{"record":{"id":"27ba607ff2a10755","repo":"prestodb/presto","slug":"invalid-cast-argument-27ba60","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"Cannot cast '%s' to %s","messagePattern":"Cannot cast '(.+?)' to (.+?)","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/operator/scalar/JsonOperators.java","lineNumber":94,"sourceCode":"\n    private JsonOperators()\n    {\n    }\n\n    @ScalarOperator(CAST)\n    @SqlNullable\n    @LiteralParameters(\"x\")\n    @SqlType(\"varchar(x)\")\n    public static Slice castToVarchar(@SqlType(JSON) Slice json)\n    {\n        try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {\n            parser.nextToken();\n            Slice result = currentTokenAsVarchar(parser);\n            checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, \"Cannot cast input json to VARCHAR\"); // check no trailing token\n            return result;\n        }\n        catch (IOException | JsonCastException e) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, format(\"Cannot cast '%s' to %s\", json.toStringUtf8(), VARCHAR), e);\n        }\n    }\n\n    @ScalarOperator(CAST)\n    @SqlNullable\n    @SqlType(BIGINT)\n    public static Long castToBigint(@SqlType(JSON) Slice json)\n    {\n        try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {\n            parser.nextToken();\n            Long result = currentTokenAsBigint(parser);\n            checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, \"Cannot cast input json to BIGINT\"); // check no trailing token\n            return result;\n        }\n        catch (IOException | JsonCastException e) {\n            throw new PrestoException(INVALID_CAST_ARGUMENT, format(\"Cannot cast '%s' to %s\", json.toStringUtf8(), BIGINT), e);\n        }\n    }","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/JsonOperators.java#L76-L112","documentation":"CAST(json AS VARCHAR) in JsonOperators.castToVarchar rethrows any IOException or JsonCastException as INVALID_CAST_ARGUMENT, echoing the raw JSON bytes and the target type VARCHAR. It fires when the json value cannot be parsed as a single JSON value or a trailing token exists after it.","triggerScenarios":"Casting a corrupted/invalid json-typed value to VARCHAR, e.g. CAST(malformed AS VARCHAR) or json values containing trailing garbage produced by a faulty pipeline; note the earlier checkCondition already rejects trailing tokens and this catch converts parser failures.","commonSituations":"Json values produced by non-conforming producers (multiple concatenated documents, truncated writes); round-tripping through systems that mangle JSON; debugging columns typed JSON that actually hold raw text.","solutions":["Fix the producer so the JSON column contains exactly one valid document.","Re-parse the raw source text with try(json_parse(...)) to sanitize, then cast.","Use try(CAST(j AS VARCHAR)) to get NULL for corrupt rows instead of failing the query."],"exampleFix":"-- before\nSELECT CAST(raw_json AS VARCHAR) FROM t; -- raw_json malformed\n-- after\nSELECT CAST(try(json_parse(raw_text)) AS VARCHAR) FROM t; -- sanitized, NULL on invalid","handlingStrategy":"validation","validationCode":"SELECT try(CAST(j AS VARCHAR)) FROM t; -- NULL instead of query failure on corrupt json values","typeGuard":null,"tryCatchPattern":"try { String s = (String) session.execute(\"SELECT CAST(j AS VARCHAR)\"); } catch (PrestoException e) { if (e.getErrorCode().getName().equals(\"INVALID_CAST_ARGUMENT\")) return null; throw e; }","preventionTips":["Ensure JSON producers write exactly one complete document per value.","Sanitize with try(json_parse(raw_text)) before casting.","Monitor for INVALID_CAST_ARGUMENT to catch producer regressions early."],"tags":["presto","json","cast"],"backgroundTag":"invalid-json-input","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"}