apache/cassandra · error · MarshalException

Invalid tuple literal: too many elements. Type

Error message

Invalid tuple literal: too many elements. Type %s expects %d but got %d

What it means

TupleType.fromString parses a tuple literal string (split on non-escaped ':'). If the string contains more colon-separated elements than the tuple type declares, it throws this MarshalException. The CQL literal form must not exceed the tuple's arity.

Solutions

  1. Reduce the literal to at most size() elements
  2. Escape literal ':' characters inside field data with a backslash ('\\:')
  3. Use the JSON/parameterized insert form instead of string literals for data containing colons
  4. Verify the target tuple type's arity before constructing the literal

Example fix

// before: too many elements for tuple<int,int>
TupleType t = TupleType.getInstance(Int32Type.instance, Int32Type.instance);
t.fromString("1:2:3"); // throws
// after
t.fromString("1:2");
Defensive patterns

Strategy: validation

Validate before calling

int count = AbstractCompositeType.split(source).size();
if (count > tupleType.size()) throw new IllegalArgumentException("literal has " + count + " elements, max " + tupleType.size());

Try / catch

try {
    tupleType.fromString(literal);
} catch (MarshalException e) {
    throw new IllegalArgumentException("bad tuple literal: " + e.getMessage());
}

Prevention

When it happens

Trigger: Passing a string literal like "1:2:3" to a tuple<int,int> type's fromString; using the type in a context where a raw string is parsed into a Term with more fields than declared.

Common situations: Typing tuple literals in cqlsh or application-generated CQL with the wrong element count; forgetting that fields are ':' separated and embedding unescaped colons in data (e.g. timestamps or URLs).

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/java/org/apache/cassandra/db/marshal/TupleType.java:465

            }

            V field = accessor.slice(input, offset, size);
            offset += size;
            // We use ':' as delimiter, and @ to represent null, so escape them in the generated string
            String fld = COLON_PAT.matcher(type.getString(field, accessor)).replaceAll(ESCAPED_COLON);
            fld = AT_PAT.matcher(fld).replaceAll(ESCAPED_AT);
            sb.append(fld);
        }
        return sb.toString();
    }

    public ByteBuffer fromString(String source)
    {
        // Split the input on non-escaped ':' characters
        List<String> fieldStrings = AbstractCompositeType.split(source);

        if (fieldStrings.size() > size())
            throw new MarshalException(String.format("Invalid tuple literal: too many elements. Type %s expects %d but got %d",
                                                     asCQL3Type(), size(), fieldStrings.size()));

        List<ByteBuffer> fields = new ArrayList<>(fieldStrings.size());
        for (int i = 0; i < fieldStrings.size(); i++)
        {
            String fieldString = fieldStrings.get(i);
            // We use @ to represent nulls
            if (fieldString.equals("@"))
            {
                fields.add(null);
            }
            else
            {
                AbstractType<?> type = type(i);
                fieldString = ESCAPED_COLON_PAT.matcher(fieldString).replaceAll(COLON);
                fieldString = ESCAPED_AT_PAT.matcher(fieldString).replaceAll(AT);
                fields.add(type.fromString(fieldString));
            }

View on GitHub (pinned to 88fd0f6a0e)