{"record":{"id":"a102e7ffdc88ae66","repo":"apache/cassandra","slug":"invalid-unset-value-for-element-selection-on","errorCode":null,"errorMessage":"Invalid unset value for element selection on ","messagePattern":"Invalid unset value for element selection on ","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/selection/ElementsSelector.java","lineNumber":156,"sourceCode":"     * @param key the element within the value represented by {@code factory} that is selected.\n     * @return the created factory.\n     */\n    public static Factory newElementFactory(String name, Selector.Factory factory, CollectionType<?> type, final Term key)\n    {\n        return new AbstractFactory(name, factory, type)\n        {\n            protected AbstractType<?> getReturnType()\n            {\n                return valueType(type);\n            }\n\n            public Selector newInstance(QueryOptions options) throws InvalidRequestException\n            {\n                ByteBuffer keyValue = key.bindAndGet(options);\n                if (keyValue == null)\n                    throw new InvalidRequestException(\"Invalid null value for element selection on \" + factory.getColumnName());\n                if (keyValue == ByteBufferUtil.UNSET_BYTE_BUFFER)\n                    throw new InvalidRequestException(\"Invalid unset value for element selection on \" + factory.getColumnName());\n                return new ElementSelector(factory.newInstance(options), keyValue);\n            }\n\n            public boolean areAllFetchedColumnsKnown()\n            {\n                // If we known all the fetched columns, it means that we don't have to wait execution to create\n                // the ColumnFilter (through addFetchedColumns below).\n                // That's the case if either there is no particular subselection\n                // to add, or if there is one but the selected key is terminal. In other words,\n                // we known all the fetched columns if all the feched columns of the factory are known and either:\n                //  1) the type is frozen (in which case there isn't subselection to do).\n                //  2) the factory (the left-hand-side) isn't a simple column selection (here again, no\n                //     subselection we can do).\n                //  3) the element selected is terminal.\n                return factory.areAllFetchedColumnsKnown()\n                        && (!type.isMultiCell() || !factory.isSimpleSelectorFactory() || key.isTerminal());\n            }\n","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/selection/ElementsSelector.java#L138-L174","documentation":"Element selection (e.g. SELECT m['key'] FROM ...) requires the map/set element key term to be bound to a concrete value. bindAndGet returns the UNSET sentinel when a bind marker was supplied but never assigned a value, so the selector cannot build an ElementSelector and Cassandra rejects the query.","triggerScenarios":"SELECT collection['key'] where the key is a bind marker '?' and the corresponding query parameter is not provided (unset variable) via the driver/BoundStatement.","commonSituations":"Driver code builds a BoundStatement from a prepared SELECT with an element-selection bind marker but forgets to set that variable, or an older driver serializes unset variables as UNSET instead of skipping the column.","solutions":["Set a value for the element-key bind parameter before executing the statement","Check statement.variables()/BoundStatement for unset values prior to execute","If the key is known at query-construction time, inline the literal instead of using a bind marker"],"exampleFix":"// before\nPreparedStatement ps = session.prepare(\"SELECT m['?'] FROM t WHERE id=?\"); // key left unset\n// after\nBoundStatement bs = ps.bind();\nbs.setString(0, \"myKey\"); // bind the element key\nbs.setInt(1, id);","handlingStrategy":"validation","validationCode":"for (int i = 0; i < stmt.variables().size(); i++)\n    if (!bs.isSet(i)) throw new IllegalStateException(\"unset variable at index \" + i);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always bind every variable of a prepared statement before execute","Enable driver checks that warn on unset bound variables","Prefer inline literals for keys known at build time"],"tags":["cql","bind-marker","selection"],"backgroundTag":"missing-required-argument","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"}