{"record":{"id":"8a4a0b4bd426463f","repo":"apache/cassandra","slug":"invalid-tuple-literal-for-s-component-d-is-not","errorCode":null,"errorMessage":"Invalid tuple literal for %s: component %d is not of type %s","messagePattern":"Invalid tuple literal for (.+?): component (.+?) is not of type (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/terms/Tuples.java","lineNumber":196,"sourceCode":"    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,\n                                                                    List<? extends AssignmentTestable> elements)\n    {\n        try\n        {\n            validateTupleAssignableTo(receiver, elements);\n            return AssignmentTestable.TestResult.WEAKLY_ASSIGNABLE;\n        }","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/terms/Tuples.java#L178-L214","documentation":"Cassandra throws this when an individual component of a tuple literal cannot be assigned to the declared type of the corresponding tuple component. During validation, each element's type is checked via AssignmentTestable.testAssignment against the component spec (componentSpecOf(receiver, i)); if the value's type is incompatible with the tuple's i-th component type, an InvalidRequestException naming the zero-based component index and expected type is thrown.","triggerScenarios":"Inserting a tuple literal whose i-th value has a mismatched type, e.g. `c = ('a', 2)` for `tuple<int, text>` (component 0 is text, expected int); using a string literal where a numeric/timestamp/boolean component is required; relying on implicit conversions Cassandra does not perform; binding parameters whose Java types the driver maps to a non-assignable CQL type.","commonSituations":"Swapping the order of tuple components in application code; passing a stringly-typed value ('2024-01-01') into a timestamp or int component without an explicit cast; driver version changes that alter how a Java type binds to a CQL type; constructing tuples programmatically from heterogeneous config values.","solutions":["Read the expected component type from the error message (e.g. component 2 must be int) and fix the literal or binding at that position to match.","Check the column's declared tuple type with `DESCRIBE TABLE` and reorder values so each matches its positional component type.","Add explicit literals/casts in CQL (e.g. use 2 instead of '2', toTimestamp(now()) for timestamps) since Cassandra does not implicitly coerce between types.","In application code, validate each value's type against the tuple component types before building the statement, or use the driver's typed tuple type (e.g. TupleType.newValue with correctly typed fields)."],"exampleFix":"// before: component 0 of tuple<int, text> gets a string\nINSERT INTO t (k, c) VALUES (1, ('42', 'a'));\n\n// after: component types match the declaration\nINSERT INTO t (k, c) VALUES (1, (42, 'a'));","handlingStrategy":"validation","validationCode":"TupleType tt = (TupleType) column.getType();\nfor (int i = 0; i < values.size(); i++) {\n    DataType expected = tt.getComponentTypes().get(i);\n    if (!expected.accepts(values.get(i)))\n        throw new IllegalArgumentException(\n            String.format(\"component %d of %s must be %s\", i, column.getName(), expected));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the driver's TupleValue with typed setters (setInt/setString/...) so the compiler/driver enforces component types.","Do not rely on implicit string-to-number coercion; convert explicitly in application code before binding.","Keep tuple construction co-located with the schema definition to avoid positional swaps.","Unit-test statement building against the schema metadata to catch component order/type drift."],"tags":["cql","tuple","type-mismatch","validation"],"backgroundTag":"type-mismatch","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"}