{"record":{"id":"5c370fda7563e322","repo":"apache/cassandra","slug":"cannot-parse-16-bits-int-value-from-s","errorCode":null,"errorMessage":"Cannot parse 16-bits int value from \"%s\"","messagePattern":"Cannot parse 16-bits int value from \"(.+?)\"","errorType":"exception","errorClass":"InvalidTypeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java","lineNumber":1620,"sourceCode":"        private static final SmallIntCodec instance = new SmallIntCodec();\n\n        private SmallIntCodec()\n        {\n            super(smallint());\n        }\n\n        @Override\n        public Short parse(String value)\n        {\n            try\n            {\n                return value == null || value.isEmpty() || value.equalsIgnoreCase(\"NULL\")\n                       ? null\n                       : Short.parseShort(value);\n            }\n            catch (NumberFormatException e)\n            {\n                throw new InvalidTypeException(\n                String.format(\"Cannot parse 16-bits int value from \\\"%s\\\"\", value));\n            }\n        }\n\n        @Override\n        public String format(Short value)\n        {\n            if (value == null) return \"NULL\";\n            return Short.toString(value);\n        }\n\n        @Override\n        public ByteBuffer serializeNoBoxing(short value, ProtocolVersion protocolVersion)\n        {\n            ByteBuffer bb = ByteBuffer.allocate(2);\n            bb.putShort(0, value);\n            return bb;\n        }","sourceCodeStart":1602,"sourceCodeEnd":1638,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java#L1602-L1638","documentation":"InvalidTypeException from SmallIntCodec.parse: the string could not be parsed by Short.parseShort — outside the 16-bit signed range (-32768..32767) or not numeric. Null/empty/'NULL' inputs return null before parsing; the error is purely a client-supplied literal format/range problem.","triggerScenarios":"Calling ShortCodec.parse(String) with values like \"40000\", \"-40000\", \"12.7\", or \"abc\"; often when binding smallint values from string inputs or reading text-encoded results.","commonSituations":"Counter or metric values stored as smallint but exceeding 32767, CSV/JSON imports feeding unvalidated numbers into smallint columns, or localized decimal formats.","solutions":["Range-check the parsed integer against -32768..32767 before parsing as short.","Upgrade the column to int/bigint if values legitimately exceed short range, and use the corresponding codec.","Parse with Integer.parseInt first, validate, then hand the canonical string to ShortCodec.parse.","Catch InvalidTypeException around parse and produce a clear validation error naming the offending input."],"exampleFix":"// before\nshort s = new ShortCodec().parse(userInput);\n// after\nint n = Integer.parseInt(userInput.trim());\nif (n < Short.MIN_VALUE || n > Short.MAX_VALUE)\n    throw new IllegalArgumentException(\"smallint out of range: \" + n);\nshort s = (short) n;","handlingStrategy":"validation","validationCode":"boolean isValidSmallint(String s) {\n    if (s == null || s.trim().isEmpty() || s.equalsIgnoreCase(\"NULL\")) return true;\n    try { int n = Integer.parseInt(s.trim()); return n >= Short.MIN_VALUE && n <= Short.MAX_VALUE; }\n    catch (NumberFormatException e) { return false; }\n}","typeGuard":null,"tryCatchPattern":"try { Short s = codec.parse(value); }\ncatch (InvalidTypeException e) {\n    throw new IllegalArgumentException(\"smallint must be integer in [-32768,32767]: \" + value, e);\n}","preventionTips":["Range-check before narrowing to short.","Use int/bigint columns when values may exceed 16 bits.","Validate imported CSV/JSON numeric fields.","Catch NumberFormatException at the input layer."],"tags":["parsing","smallint","range","type-codec"],"backgroundTag":"value-out-of-range","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}