{"record":{"id":"a775eb0be90e0217","repo":"prestodb/presto","slug":"invalid-cast-argument-a775eb","errorCode":"INVALID_CAST_ARGUMENT","errorMessage":"Cannot cast '%s' to BOOLEAN","messagePattern":"Cannot cast '(.+?)' to BOOLEAN","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/type/VarcharOperators.java","lineNumber":137,"sourceCode":"                return false;\n            }\n        }\n        if ((value.length() == 4) &&\n                (toUpperCase(value.getByte(0)) == 'T') &&\n                (toUpperCase(value.getByte(1)) == 'R') &&\n                (toUpperCase(value.getByte(2)) == 'U') &&\n                (toUpperCase(value.getByte(3)) == 'E')) {\n            return true;\n        }\n        if ((value.length() == 5) &&\n                (toUpperCase(value.getByte(0)) == 'F') &&\n                (toUpperCase(value.getByte(1)) == 'A') &&\n                (toUpperCase(value.getByte(2)) == 'L') &&\n                (toUpperCase(value.getByte(3)) == 'S') &&\n                (toUpperCase(value.getByte(4)) == 'E')) {\n            return false;\n        }\n        throw new PrestoException(INVALID_CAST_ARGUMENT, format(\"Cannot cast '%s' to BOOLEAN\", value.toStringUtf8()));\n    }\n\n    private static byte toUpperCase(byte b)\n    {\n        return isLowerCase(b) ? ((byte) (b - 32)) : b;\n    }\n\n    private static boolean isLowerCase(byte b)\n    {\n        return (b >= 'a') && (b <= 'z');\n    }\n\n    @LiteralParameters(\"x\")\n    @ScalarOperator(CAST)\n    @SqlType(StandardTypes.DOUBLE)\n    public static double castToDouble(@SqlType(\"varchar(x)\") Slice slice)\n    {\n        try {","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/type/VarcharOperators.java#L119-L155","documentation":"Thrown when casting a VARCHAR to BOOLEAN and the string is not one of the accepted literals ('true'/'t'/'yes' variants or 'false'/'f'/'no'-style values matched case-insensitively). Presto raises INVALID_CAST_ARGUMENT with the offending value. This backs CAST(varchar AS boolean).","triggerScenarios":"CAST(varchar_col AS BOOLEAN) with values like '1', '0', 'Y', 'on', 'TRUE ' with whitespace, or any free text not matching the accepted true/false spellings.","commonSituations":"Boolean-like data encoded as 1/0 or Y/N imported from MySQL or Excel; trailing spaces or hidden characters from CSV parsing; columns mixing real booleans with sentinel text like 'N/A'.","solutions":["Map the values explicitly before casting: CASE WHEN col IN ('1','Y','yes','true') THEN true WHEN col IN ('0','N','no','false') THEN false ... END","Apply trim() and/or lower() to the column before casting to remove whitespace/case issues","Clean the sentinel/garbage values upstream or with regexp_replace","Use try_cast(col AS BOOLEAN) to surface NULL for unconvertible rows instead of failing"],"exampleFix":"// before\nSELECT CAST(flag AS BOOLEAN) FROM t; -- flag = 'Y'\n// after\nSELECT CAST(trim(lower(flag)) AS BOOLEAN) FROM t; -- or a CASE mapping 'y'/'1' to true","handlingStrategy":"validation","validationCode":"-- Presto SQL: pre-check accepted boolean spellings\nSELECT * FROM t\nWHERE lower(trim(flag)) NOT IN ('true','t','yes','false','f','no');","typeGuard":"// JS-side guard\nfunction isBooleanLiteral(s) {\n  return ['true','t','yes','false','f','no'].includes(String(s).trim().toLowerCase());\n}","tryCatchPattern":"SELECT try_cast(trim(flag) AS BOOLEAN) AS b FROM t; -- NULL for unconvertible values","preventionTips":["Normalize boolean-like columns (trim + lower) before casting","Map 1/0 and Y/N encodings to true/false with an explicit CASE upstream","Ban sentinel text ('N/A') in boolean columns at ingestion","Use try_cast in ETL to route unconvertible rows to a quarantine table"],"tags":["presto","boolean","cast","varchar"],"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-12T02:17:10.037Z"}