apache/cassandra · error · InvalidTypeException

Cannot parse 16-bits int value from

Error message

Cannot parse 16-bits int value from "%s"

What it means

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.

Solutions

  1. Range-check the parsed integer against -32768..32767 before parsing as short.
  2. Upgrade the column to int/bigint if values legitimately exceed short range, and use the corresponding codec.
  3. Parse with Integer.parseInt first, validate, then hand the canonical string to ShortCodec.parse.
  4. Catch InvalidTypeException around parse and produce a clear validation error naming the offending input.

Example fix

// before
short s = new ShortCodec().parse(userInput);
// after
int n = Integer.parseInt(userInput.trim());
if (n < Short.MIN_VALUE || n > Short.MAX_VALUE)
    throw new IllegalArgumentException("smallint out of range: " + n);
short s = (short) n;
Defensive patterns

Strategy: validation

Validate before calling

boolean isValidSmallint(String s) {
    if (s == null || s.trim().isEmpty() || s.equalsIgnoreCase("NULL")) return true;
    try { int n = Integer.parseInt(s.trim()); return n >= Short.MIN_VALUE && n <= Short.MAX_VALUE; }
    catch (NumberFormatException e) { return false; }
}

Try / catch

try { Short s = codec.parse(value); }
catch (InvalidTypeException e) {
    throw new IllegalArgumentException("smallint must be integer in [-32768,32767]: " + value, e);
}

Prevention

When it happens

Trigger: 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.

Common situations: Counter or metric values stored as smallint but exceeding 32767, CSV/JSON imports feeding unvalidated numbers into smallint columns, or localized decimal formats.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/5c370fda7563e322. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java:1620

        private static final SmallIntCodec instance = new SmallIntCodec();

        private SmallIntCodec()
        {
            super(smallint());
        }

        @Override
        public Short parse(String value)
        {
            try
            {
                return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")
                       ? null
                       : Short.parseShort(value);
            }
            catch (NumberFormatException e)
            {
                throw new InvalidTypeException(
                String.format("Cannot parse 16-bits int value from \"%s\"", value));
            }
        }

        @Override
        public String format(Short value)
        {
            if (value == null) return "NULL";
            return Short.toString(value);
        }

        @Override
        public ByteBuffer serializeNoBoxing(short value, ProtocolVersion protocolVersion)
        {
            ByteBuffer bb = ByteBuffer.allocate(2);
            bb.putShort(0, value);
            return bb;
        }

View on GitHub (pinned to 88fd0f6a0e)