apache/cassandra · error · InvalidRequestException

Cannot set the value of counter column

Error message

Cannot set the value of counter column %s (counters can only be incremented/decremented, not set)

What it means

Operations like SetValue.prepare throw this InvalidRequestException when the target column's type is CounterColumnType. Counter columns in Cassandra cannot be assigned a value directly — they only support increment/decrement via CounterMutation — so any SET operation against a counter is rejected at statement preparation time.

Solutions

  1. Use UPDATE t SET counter_col = counter_col + 5 to increment (or - n to decrement)
  2. Counters cannot be set to an absolute value; remove direct assignment code paths
  3. If you need settable values, change the column type (requires a new table; ALTER to/from counter is unsupported)
  4. Fix ORM/code generators to detect CounterColumnType and emit increment syntax

Example fix

// before
UPDATE stats SET views = 10 WHERE id = 1;
// after
UPDATE stats SET views = views + 10 WHERE id = 1;
Defensive patterns

Strategy: validation

Validate before calling

void assertNotCounterAssignment(com.datastax.driver.core.ColumnMetadata col, String assignment) {
    if (col.getType().getName() == com.datastax.driver.core.DataType.Name.COUNTER && !assignment.contains("+") && !assignment.contains("-"))
        throw new IllegalArgumentException("Counter column " + col.getName() + " cannot be set directly; use counter_col = counter_col + n");
}

Try / catch

try { session.execute(update); } catch (InvalidRequestException e) { if (e.getMessage().startsWith("Cannot set the value of counter column")) { throw new IllegalArgumentException("Use increment/decrement syntax for counters", e); } throw e; }

Prevention

When it happens

Trigger: Executing UPDATE t SET counter_col = 5 or INSERT with a literal/term assigned to a counter column; also bound terms prepared against a counter receiver.

Common situations: Reusing an UPDATE/INSERT builder across schemas where a column changed from regular to counter; migrating SQL habits where assignment of counters is legal; ORMs generating SET clauses without knowing column types.

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


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/9cca01610e6fe548. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/cql3/Operation.java:200

         */
        public Operation prepare(String keyspace, ColumnMetadata receiver, TableMetadata metadata) throws InvalidRequestException;
    }

    public static class SetValue implements RawUpdate
    {
        private final Term.Raw value;

        public SetValue(Term.Raw value)
        {
            this.value = value;
        }

        public Operation prepare(TableMetadata metadata, ColumnMetadata receiver, boolean canReadExistingState) throws InvalidRequestException
        {
            Term v = value.prepare(metadata.keyspace, receiver);

            if (receiver.type instanceof CounterColumnType)
                throw new InvalidRequestException(String.format("Cannot set the value of counter column %s (counters can only be incremented/decremented, not set)", receiver.name));

            if (receiver.type.isCollection())
            {
                switch (((CollectionType<?>) receiver.type).kind)
                {
                    case LIST:
                        return new Lists.Setter(receiver, v);
                    case SET:
                        return new Sets.Setter(receiver, v);
                    case MAP:
                        return new Maps.Setter(receiver, v);
                    default:
                        throw new AssertionError();
                }
            }

            if (receiver.type.isUDT())
                return new UserTypes.Setter(receiver, v);

View on GitHub (pinned to 88fd0f6a0e)