apache/cassandra · error · InvalidRequestException
Invalid unset value for element selection on
Error message
Invalid unset value for element selection on
What it means
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.
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
Example fix
// before
PreparedStatement ps = session.prepare("SELECT m['?'] FROM t WHERE id=?"); // key left unset
// after
BoundStatement bs = ps.bind();
bs.setString(0, "myKey"); // bind the element key
bs.setInt(1, id); Defensive patterns
Strategy: validation
Validate before calling
for (int i = 0; i < stmt.variables().size(); i++)
if (!bs.isSet(i)) throw new IllegalStateException("unset variable at index " + i); Prevention
- 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
When it happens
Trigger: SELECT collection['key'] where the key is a bind marker '?' and the corresponding query parameter is not provided (unset variable) via the driver/BoundStatement.
Common situations: 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.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- Cannot assign value to of type
- Cannot infer type for term
- Cannot infer type for term
- Invalid map literal for
- Invalid null value for slice selection on
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/a102e7ffdc88ae66.
Report an issue: GitHub.
Appendix: source
Thrown at src/java/org/apache/cassandra/cql3/selection/ElementsSelector.java:156
* @param key the element within the value represented by {@code factory} that is selected.
* @return the created factory.
*/
public static Factory newElementFactory(String name, Selector.Factory factory, CollectionType<?> type, final Term key)
{
return new AbstractFactory(name, factory, type)
{
protected AbstractType<?> getReturnType()
{
return valueType(type);
}
public Selector newInstance(QueryOptions options) throws InvalidRequestException
{
ByteBuffer keyValue = key.bindAndGet(options);
if (keyValue == null)
throw new InvalidRequestException("Invalid null value for element selection on " + factory.getColumnName());
if (keyValue == ByteBufferUtil.UNSET_BYTE_BUFFER)
throw new InvalidRequestException("Invalid unset value for element selection on " + factory.getColumnName());
return new ElementSelector(factory.newInstance(options), keyValue);
}
public boolean areAllFetchedColumnsKnown()
{
// If we known all the fetched columns, it means that we don't have to wait execution to create
// the ColumnFilter (through addFetchedColumns below).
// That's the case if either there is no particular subselection
// to add, or if there is one but the selected key is terminal. In other words,
// we known all the fetched columns if all the feched columns of the factory are known and either:
// 1) the type is frozen (in which case there isn't subselection to do).
// 2) the factory (the left-hand-side) isn't a simple column selection (here again, no
// subselection we can do).
// 3) the element selected is terminal.
return factory.areAllFetchedColumnsKnown()
&& (!type.isMultiCell() || !factory.isSimpleSelectorFactory() || key.isTerminal());
}
View on GitHub (pinned to 88fd0f6a0e)