{"record":{"id":"1781bed3ea34a45e","repo":"apache/cassandra","slug":"invalid-tuple-literal-for-s-too-many-elements-t","errorCode":null,"errorMessage":"Invalid tuple literal for %s: too many elements. Type %s expects %d but got %d","messagePattern":"Invalid tuple literal for (.+?): too many elements\\. Type (.+?) expects (.+?) but got (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/terms/Tuples.java","lineNumber":189,"sourceCode":"    /**\n     * Checks if the tuple with the specified elements can be assigned to the specified column.\n     *\n     * @param receiver the receiving column\n     * @param elements the tuple elements\n     * @throws InvalidRequestException if the tuple cannot be assigned to the specified column.\n     */\n    public static void validateTupleAssignableTo(ColumnSpecification receiver,\n                                                 List<? extends AssignmentTestable> elements)\n    {\n        if (!checkIfTupleType(receiver.type))\n            throw invalidRequest(\"Invalid tuple type literal for %s of type %s\", receiver.name, receiver.type.asCQL3Type());\n\n        TupleType tt = getTupleType(receiver.type);\n        for (int i = 0; i < elements.size(); i++)\n        {\n            if (i >= tt.size())\n            {\n                throw invalidRequest(\"Invalid tuple literal for %s: too many elements. Type %s expects %d but got %d\",\n                                     receiver.name, tt.asCQL3Type(), tt.size(), elements.size());\n            }\n\n            AssignmentTestable value = elements.get(i);\n            ColumnSpecification spec = componentSpecOf(receiver, i);\n            if (!value.testAssignment(receiver.ksName, spec).isAssignable())\n                throw invalidRequest(\"Invalid tuple literal for %s: component %d is not of type %s\",\n                                     receiver.name, i, spec.type.asCQL3Type());\n        }\n    }\n\n    /**\n     * Tests that the tuple with the specified elements can be assigned to the specified column.\n     *\n     * @param receiver the receiving column\n     * @param elements the tuple elements\n     */\n    public static AssignmentTestable.TestResult testTupleAssignment(ColumnSpecification receiver,","sourceCodeStart":171,"sourceCodeEnd":207,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/terms/Tuples.java#L171-L207","documentation":"Cassandra throws this when a tuple literal in a CQL statement contains more values than the target column's tuple type defines components for. Tuples in Cassandra have a fixed arity: a column declared, say, `frozen<tuple<int, text>>` accepts exactly 2 elements and no more. During statement validation (validateTupleAssignableTo, invoked via testTupleAssignment), the element list is compared element-by-element against the receiver's TupleType, and any excess element triggers this InvalidRequestException before execution.","triggerScenarios":"Executing INSERT/UPDATE/DELETE with a tuple literal like `c = (1, 'a', true)` against a column of type `tuple<int, text>`; calling `Tuples.validateTupleAssignableTo(receiver, elements)` (or `testTupleAssignment`) with `elements.size() > tupleType.size()`; binding a user-supplied list into a tuple column without checking arity; preparing statements where a schema change shortened the tuple type while cached literals still supply the old element count.","commonSituations":"Application code building tuple values dynamically from a list of unknown length; a schema migration that reduced the number of tuple components while old application versions still send the larger literal; typos in hand-written CQL where an extra value is pasted into the tuple; ORM/driver mapping layers that serialize the whole row object into a tuple field.","solutions":["Count the elements in the tuple literal and compare with the column definition (`DESCRIBE TABLE` or system_schema.columns shows the tuple type); remove extra elements so it matches the declared arity.","If the extra element should be stored, ALTER the column to a wider tuple type (note: Cassandra only supports adding components at the end, e.g. ALTER TABLE ... ALTER c TYPE tuple<int, text, boolean> in older versions or recreate the column), then update the statement.","If element count is dynamic in application code, validate `values.size() <= declaredTupleSize` before building the CQL literal, or use a separate collection column (list/set/map) instead of a tuple.","After any tuple type change, restart/refresh prepared statement caches so stale literals against the old type are revalidated."],"exampleFix":"// before: column is frozen<tuple<int, text>> (2 components)\nINSERT INTO t (k, c) VALUES (1, (1, 'a', true));\n\n// after: literal arity matches the declared tuple type\nINSERT INTO t (k, c) VALUES (1, (1, 'a'));","handlingStrategy":"validation","validationCode":"TupleType tt = (TupleType) column.getType();\nif (values.size() > tt.size())\n    throw new IllegalArgumentException(\n        String.format(\"tuple for column %s expects at most %d elements, got %d\",\n                      column.getName(), tt.size(), values.size()));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Generate INSERT/UPDATE statements from the live table metadata (driver SchemaMetadata) rather than hardcoding tuple arity.","Use the driver's typed TupleValue API so arity and per-component types are enforced at bind time.","Re-prepare statements after any ALTER that changes a tuple column type.","Prefer dedicated collection columns (list/map) over tuples when element count can vary."],"tags":["cql","tuple","validation","invalid-request"],"backgroundTag":"index-out-of-range","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}