apache/cassandra · error · InvalidRequestException
Cannot mix counter and non counter columns in the same table
Error message
Cannot mix counter and non counter columns in the same table
What it means
Counter columns cannot coexist with non-counter columns in the same table (except within the primary key). The builder checks whether any column uses the counter type, and if so verifies every non-primary-key column is also a counter; otherwise it throws this InvalidRequest.
Source
Thrown at src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java:381
}
else
{
// Static columns only make sense if we have at least one clustering column. Otherwise everything is static anyway
if (clusteringColumns.isEmpty() && !staticColumns.isEmpty())
throw ire("Static columns are only useful (and thus allowed) if the table has at least one clustering column");
}
/*
* Counter table validation
*/
boolean hasCounters = rawColumns.values().stream().anyMatch(c -> c.rawType.isCounter());
if (hasCounters)
{
// We've handled anything that is not a PRIMARY KEY so columns only contains NON-PK columns. So
// if it's a counter table, make sure we don't have non-counter types
if (columns.values().stream().anyMatch(t -> !t.type.isCounter()))
throw ire("Cannot mix counter and non counter columns in the same table");
if (params.defaultTimeToLive > 0)
throw ire("Cannot set %s on a table with counters", TableParams.Option.DEFAULT_TIME_TO_LIVE);
}
/*
* Create the builder
*/
TableMetadata.Builder builder = TableMetadata.builder(keyspaceName, tableName);
if (attrs.hasProperty(TableAttributes.ID))
builder.id(attrs.getId());
builder.isCounter(hasCounters)
.params(params);
for (int i = 0; i < partitionKeyColumns.size(); i++)View on GitHub (pinned to 88fd0f6a0e)
Solutions
- Split into two tables: one for counters and one for non-counter data.
- Change regular columns to counter type if only counts are needed (with default 0).
- Remove the counter column if regular columns are what you need.
Example fix
// before CREATE TABLE stats (pk int, hits counter, label text, PRIMARY KEY (pk)); // after CREATE TABLE stats (pk int, hits counter, PRIMARY KEY (pk)); CREATE TABLE meta (pk int, label text, PRIMARY KEY (pk));
Defensive patterns
Strategy: validation
Validate before calling
// Ensure a table with a counter column has only counter columns outside the PK:
boolean hasCounter = true; // any column of type counter
List<String> nonPkTypes = List.of("counter"); // types of non-PK columns
if (hasCounter && nonPkTypes.stream().anyMatch(t -> !t.equals("counter"))) throw new IllegalArgumentException("Cannot mix counter and non-counter columns"); Try / catch
try { session.execute(ddl); } catch (com.datastax.driver.core.exceptions.InvalidQueryException e) { if (e.getMessage().contains("Cannot mix counter")) { /* split tables */ } else throw e; } Prevention
- Dedicate tables to counters; keep descriptive data in a companion table.
- Check counter usage before ALTERing a table to add regular columns.
- Document counter-table restrictions in schema design guides.
When it happens
Trigger: CREATE TABLE t (pk int, c counter, v text, PRIMARY KEY (pk)) — mixing a counter column with a regular text column.
Common situations: Adding a counter column to an existing normal table's schema or vice versa; modeling statistics (counters) alongside descriptive fields in one table.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- Cannot set %s on a table with counters
- Only clustering key columns can be defined in CLUSTERING ORD
- The order of columns in the CLUSTERING ORDER directive must
- Missing CLUSTERING ORDER for column %s
- Static columns are only useful (and thus allowed) if the tab
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/a0c80d90864e30a2.
Report an issue: GitHub.