{"record":{"id":"31ab2d2f863ffb28","repo":"apache/cassandra","slug":"text-or-varchar-values-must-be-enclosed-by-single","errorCode":null,"errorMessage":"text or varchar values must be enclosed by single quotes","messagePattern":"text or varchar values must be enclosed by single quotes","errorType":"validation","errorClass":"InvalidTypeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java","lineNumber":972,"sourceCode":"     * DataType#text()} or {@link DataType#ascii()}.\n     */\n    private abstract static class StringCodec extends TypeCodec<String>\n    {\n\n        private final Charset charset;\n\n        private StringCodec(DataType cqlType, Charset charset)\n        {\n            super(cqlType, String.class);\n            this.charset = charset;\n        }\n\n        @Override\n        public String parse(String value)\n        {\n            if (value == null || value.isEmpty() || value.equalsIgnoreCase(\"NULL\")) return null;\n            if (!ParseUtils.isQuoted(value))\n                throw new InvalidTypeException(\"text or varchar values must be enclosed by single quotes\");\n\n            return ParseUtils.unquote(value);\n        }\n\n        @Override\n        public String format(String value)\n        {\n            if (value == null) return \"NULL\";\n            return ParseUtils.quote(value);\n        }\n\n        @Override\n        public ByteBuffer serialize(String value, ProtocolVersion protocolVersion)\n        {\n            return value == null ? null : ByteBuffer.wrap(value.getBytes(charset));\n        }\n\n        /**","sourceCodeStart":954,"sourceCodeEnd":990,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java#L954-L990","documentation":"The text/varchar codec's parse() only accepts CQL literal syntax, and CQL string literals must be wrapped in single quotes. If the passed string is non-empty, not NULL, and not already quoted (ParseUtils.isQuoted), the codec refuses to interpret it. This guards against silently parsing a bare token as a string literal.","triggerScenarios":"Calling TypeCodec.parse() (or a higher-level parsing API such as Metadata/Row parsing of literals) on a value like `hello` instead of `'hello'` for a text/varchar column.","commonSituations":"Building CQL literals from user input without quoting; copy-pasting CQL values out of docs or config files without the quotes; writing custom tooling that serializes values into CQL statements; string values that were already unquoted once before reaching the codec.","solutions":["Wrap the value in single quotes before passing it to parse: parse(\"'\" + value.replace(\"'\", \"''\") + \"'\")","Better, avoid manual quoting entirely — use codec.format(value) or bind parameters via BoundStatement instead of composing literals.","If the input may already be quoted, check ParseUtils.isQuoted(value) and only quote when needed."],"exampleFix":"// before\ncodec.parse(name); // InvalidTypeException if name = Alice\n// after\nString literal = \"'\" + name.replace(\"'\", \"''\") + \"'\";\ncodec.parse(literal); // or use codec.format(name)","handlingStrategy":"validation","validationCode":"static String toCqlStringLiteral(String v) {\n    if (v == null || v.isEmpty() || v.equalsIgnoreCase(\"NULL\")) return null;\n    if (ParseUtils.isQuoted(v)) return v;\n    return \"'\" + v.replace(\"'\", \"''\") + \"'\";\n}","typeGuard":"boolean isCqlQuotedString(String v) {\n    return v != null && ParseUtils.isQuoted(v);\n}","tryCatchPattern":"try {\n    String s = codec.parse(raw);\n} catch (InvalidTypeException e) {\n    // fall back to quote-then-reparse or surface a user-facing validation error\n    String s = codec.parse(\"'\" + raw.replace(\"'\", \"''\") + \"'\");\n}","preventionTips":["Prefer bound statements / codec.format() over composing CQL string literals by hand.","Always escape embedded single quotes by doubling them.","Centralize literal building in one quoting helper rather than sprinkling quotes at call sites."],"tags":["cql","parsing","codec","string-literal"],"backgroundTag":"invalid-argument-format","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}