{"record":{"id":"c194161e34811035","repo":"apache/cassandra","slug":"invalid-element-selection-s-is-of-type-s-is-not","errorCode":null,"errorMessage":"Invalid element selection: %s is of type %s is not a collection","messagePattern":"Invalid element selection: (.+?) is of type (.+?) is not a collection","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/selection/Selectable.java","lineNumber":1479,"sourceCode":"\n        @Override\n        public String toString()\n        {\n            return String.format(\"%s[%s]\", selected, element);\n        }\n\n        public Selector.Factory newSelectorFactory(TableMetadata cfm, AbstractType<?> expectedType, List<ColumnMetadata> defs, VariableSpecifications boundNames)\n        {\n            Selector.Factory factory = selected.newSelectorFactory(cfm, null, defs, boundNames);\n            ColumnSpecification receiver = factory.getColumnSpecification(cfm);\n\n            AbstractType<?> type = receiver.type;\n            if (receiver.isReversedType())\n            {\n                type = ((ReversedType<?>) type).baseType;\n            }\n            if (!(type instanceof CollectionType))\n                throw new InvalidRequestException(String.format(\"Invalid element selection: %s is of type %s is not a collection\", selected, type.asCQL3Type()));\n\n            ColumnSpecification boundSpec = specForElementOrSlice(selected, receiver, ((CollectionType) type).kind, \"Element\");\n\n            Term elt = element.prepare(cfm.keyspace, boundSpec);\n            elt.collectMarkerSpecification(boundNames, cfm);\n            return ElementsSelector.newElementFactory(toString(), factory, (CollectionType)type, elt);\n        }\n\n        public AbstractType<?> getExactTypeIfKnown(String keyspace)\n        {\n            AbstractType<?> selectedType = selected.getExactTypeIfKnown(keyspace);\n            if (selectedType == null || !(selectedType instanceof CollectionType))\n                return null;\n\n            return ElementsSelector.valueType((CollectionType) selectedType);\n        }\n\n        @Override","sourceCodeStart":1461,"sourceCodeEnd":1497,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/selection/Selectable.java#L1461-L1497","documentation":"Thrown when preparing an element selection (collection[key] or collection[idx] in a SELECT) if the type of the selected column, after unwrapping ReversedType, is not a CollectionType. Element access is only defined for lists, sets and maps, so non-collection columns cannot be indexed with [].","triggerScenarios":"SELECT mycol[k] or mycol[idx] where mycol is not a list/set/map (e.g. a text, int, frozen non-collection, or a reversed wrapper around a non-collection).","commonSituations":"Copy-pasting element-access syntax onto a scalar column; assuming a frozen column is still a collection for element selection; schema changes that changed a column's type after queries were written.","solutions":["Verify the column type with DESC[RIBE] TABLE or system_schema.columns; ensure it is list/set/map","Remove the [element] accessor and select the whole column instead","If a reversed type was intended to wrap a collection, ensure the underlying base type is actually a collection"],"exampleFix":"// before\nSELECT plain_text_col['key'] FROM ks.tbl;\n// after\nSELECT map_col['key'] FROM ks.tbl;  -- map_col must be a map/list/set","handlingStrategy":"validation","validationCode":"// verify column is a collection before element access\nString type = getColumnType(keyspace, table, column); // from system_schema.columns\nif (!type.matches(\"(list|set|map).*\")) throw new IllegalArgumentException(column + \" is not a collection; cannot use [element]\");","typeGuard":null,"tryCatchPattern":"try { session.execute(String.format(\"SELECT %s[?] FROM %s\", col, table), key); } catch (InvalidRequestException e) { if (e.getMessage().contains(\"is not a collection\")) { /* select whole column instead */ } else throw e; }","preventionTips":["Confirm column types in system_schema.columns before using [] access","Remember reversed/frozen wrapping does not make a scalar a collection","Regenerate queries after schema type changes"],"tags":["cql","collections","invalid-argument"],"backgroundTag":"invalid-argument-value","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"}