apache/cassandra · error · InvalidRequestException

Duplicate column ' ' in PRIMARY KEY clause for materialized…

Error message

Duplicate column '%s' in PRIMARY KEY clause for materialized view '%s'

What it means

Parse/validation guard while processing the PRIMARY KEY clause of CREATE MATERIALIZED VIEW: the same column identifier appears more than once across the concatenated partition-key and clustering-column lists. A view primary key requires distinct columns, so the duplicate is detected via a HashSet (usedNames-style guard) and the statement fails with InvalidRequestException naming the column and view.

Solutions

  1. Remove the duplicated column from the PRIMARY KEY clause so each partition-key and clustering column appears exactly once; re-check the column list for accidental repeats, especially when listing both PARTITION KEY and CLUSTERING COLUMN explicitly.

Example fix

// before
CREATE MATERIALIZED VIEW mv AS SELECT pk, ck, v FROM base WHERE ... PRIMARY KEY ((pk, pk), ck);
// after
CREATE MATERIALIZED VIEW mv AS SELECT pk, ck, v FROM base WHERE ... PRIMARY KEY ((pk), ck);
Defensive patterns

Strategy: validation

Validate before calling

Set<String> seen = new HashSet<>(); for (String c : primaryKeyCols) if (!seen.add(c)) throw new IllegalArgumentException("Duplicate PRIMARY KEY column: " + c);

Try / catch

try { session.execute(createMvStmt); } catch (InvalidQueryException e) { if (e.getMessage().contains("Duplicate column")) { /* dedupe PRIMARY KEY clause */ } }

Prevention

When it happens

Trigger: CREATE MATERIALIZED VIEW ... PRIMARY KEY ((pk, pk), ck) or listing one column both as partition and clustering key.

Common situations: Copy-paste editing of long PRIMARY KEY clauses; generated DDL accidentally duplicating a key column when programmatically composing the statement.

Related errors


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

Appendix: source

Thrown at src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java:249

                       .ifPresent(c -> { throw ire("Cannot include static column '%s' in materialized view '%s'", c, viewName); });

        /*
         * Process PRIMARY KEY columns and CLUSTERING ORDER BY clause
         */

        if (partitionKeyColumns.isEmpty())
            throw ire("Must provide at least one partition key column for materialized view '%s'", viewName);

        HashSet<ColumnIdentifier> primaryKeyColumns = new HashSet<>();

        concat(partitionKeyColumns, clusteringColumns).forEach(name ->
        {
            ColumnMetadata column = table.getColumn(name);
            if (null == column || !selectedColumns.contains(name))
                throw ire("Unknown column '%s' referenced in PRIMARY KEY for materialized view '%s'", name, viewName);

            if (!primaryKeyColumns.add(name))
                throw ire("Duplicate column '%s' in PRIMARY KEY clause for materialized view '%s'", name, viewName);

            AbstractType<?> type = column.type;

            if (type.isMultiCell())
            {
                if (type.isCollection())
                    throw ire("Invalid non-frozen collection type '%s' for PRIMARY KEY column '%s'", type, name);
                else
                    throw ire("Invalid non-frozen user-defined type '%s' for PRIMARY KEY column '%s'", type, name);
            }

            if (type.isCounter())
                throw ire("counter type is not supported for PRIMARY KEY column '%s'", name);

            if (type.referencesDuration())
                throw ire("duration type is not supported for PRIMARY KEY column '%s'", name);
        });

View on GitHub (pinned to 88fd0f6a0e)