apache/cassandra · error · InvalidRequestException
Cannot use selection function %s on PRIMARY KEY part %s
Error message
Cannot use selection function %s on PRIMARY KEY part %s
What it means
Selection functions of certain kinds (e.g. writetime, ttl, ttl-like wrappers) cannot be applied to columns that are part of the primary key, because primary key columns have no per-row writetime/ttl cells to read. The factory refuses to build the selector.
Source
Thrown at src/java/org/apache/cassandra/cql3/selection/Selectable.java:321
{
this.column = column;
this.selectable = selectable;
this.kind = kind;
}
@Override
public String toString()
{
return kind.name + "(" + selectable + ")";
}
public Selector.Factory newSelectorFactory(TableMetadata table,
AbstractType<?> expectedType,
List<ColumnMetadata> defs,
VariableSpecifications boundNames)
{
if (column.isPrimaryKeyColumn())
throw new InvalidRequestException(
String.format("Cannot use selection function %s on PRIMARY KEY part %s",
kind.name,
column.name));
Selector.Factory factory = selectable.newSelectorFactory(table, expectedType, defs, boundNames);
boolean isMultiCell = factory.getColumnSpecification(table).type.isMultiCell();
return WritetimeOrTTLSelector.newFactory(factory, addAndGetIndex(column, defs), kind, isMultiCell);
}
@Override
public AbstractType<?> getExactTypeIfKnown(String keyspace)
{
AbstractType<?> type = kind.returnType;
return column.type.isMultiCell() && !kind.aggregatesMultiCell() ? ListType.getInstance(type, false) : type;
}
@OverrideView on GitHub (pinned to 88fd0f6a0e)
Solutions
- Remove the selection function from the primary key column
- Apply the function to a regular (non-key) column
- If you need key write history, model it in a separate table with the key as a value column
Example fix
// before SELECT writetime(pk) FROM t; // after SELECT writetime(value) FROM t;
Defensive patterns
Strategy: validation
Validate before calling
Set<String> pkCols = table.primaryKeyColumns().stream().map(ColumnMetadata::getName).collect(toSet());
if (pkCols.contains(targetColumn)) throw new IllegalArgumentException("writetime/ttl not allowed on PK column"); Prevention
- Never wrap primary key columns in writetime()/ttl()
- Check ColumnMetadata.isPrimaryKeyColumn() when generating queries dynamically
- Keep key-vs-value column awareness in query builders
When it happens
Trigger: SELECT writetime(pk_col) FROM t or TTL(clustering_col) where the wrapped column belongs to the PRIMARY KEY.
Common situations: Users probe write timestamps of partition/clustering keys; Cassandra never stores writetime/ttl for primary key columns, so the query is rejected at prepare time.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- %s constraint can not be specified on a %s key column '%s'
- PRIMARY KEY column "%s" cannot be restricted (preceding colu
- Non PRIMARY KEY columns found in where clause: %s
- Cannot drop PRIMARY KEY column %s
- Unknown column '%s' referenced in PRIMARY KEY for table '%s'
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/73788973a19574ac.
Report an issue: GitHub.