{"record":{"id":"706276bd5a45daa0","repo":"apache/cassandra","slug":"duplicate-column-s-in-primary-key-clause-for-ma","errorCode":null,"errorMessage":"Duplicate column '%s' in PRIMARY KEY clause for materialized view '%s'","messagePattern":"Duplicate column '(.+?)' in PRIMARY KEY clause for materialized view '(.+?)'","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java","lineNumber":249,"sourceCode":"                       .ifPresent(c -> { throw ire(\"Cannot include static column '%s' in materialized view '%s'\", c, viewName); });\n\n        /*\n         * Process PRIMARY KEY columns and CLUSTERING ORDER BY clause\n         */\n\n        if (partitionKeyColumns.isEmpty())\n            throw ire(\"Must provide at least one partition key column for materialized view '%s'\", viewName);\n\n        HashSet<ColumnIdentifier> primaryKeyColumns = new HashSet<>();\n\n        concat(partitionKeyColumns, clusteringColumns).forEach(name ->\n        {\n            ColumnMetadata column = table.getColumn(name);\n            if (null == column || !selectedColumns.contains(name))\n                throw ire(\"Unknown column '%s' referenced in PRIMARY KEY for materialized view '%s'\", name, viewName);\n\n            if (!primaryKeyColumns.add(name))\n                throw ire(\"Duplicate column '%s' in PRIMARY KEY clause for materialized view '%s'\", name, viewName);\n\n            AbstractType<?> type = column.type;\n\n            if (type.isMultiCell())\n            {\n                if (type.isCollection())\n                    throw ire(\"Invalid non-frozen collection type '%s' for PRIMARY KEY column '%s'\", type, name);\n                else\n                    throw ire(\"Invalid non-frozen user-defined type '%s' for PRIMARY KEY column '%s'\", type, name);\n            }\n\n            if (type.isCounter())\n                throw ire(\"counter type is not supported for PRIMARY KEY column '%s'\", name);\n\n            if (type.referencesDuration())\n                throw ire(\"duration type is not supported for PRIMARY KEY column '%s'\", name);\n        });\n","sourceCodeStart":231,"sourceCodeEnd":267,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateViewStatement.java#L231-L267","documentation":"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.","triggerScenarios":"CREATE MATERIALIZED VIEW ... PRIMARY KEY ((pk, pk), ck) or listing one column both as partition and clustering key.","commonSituations":"Copy-paste editing of long PRIMARY KEY clauses; generated DDL accidentally duplicating a key column when programmatically composing the statement.","solutions":["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."],"exampleFix":"// before\nCREATE MATERIALIZED VIEW mv AS SELECT pk, ck, v FROM base WHERE ... PRIMARY KEY ((pk, pk), ck);\n// after\nCREATE MATERIALIZED VIEW mv AS SELECT pk, ck, v FROM base WHERE ... PRIMARY KEY ((pk), ck);","handlingStrategy":"validation","validationCode":"Set<String> seen = new HashSet<>(); for (String c : primaryKeyCols) if (!seen.add(c)) throw new IllegalArgumentException(\"Duplicate PRIMARY KEY column: \" + c);","typeGuard":null,"tryCatchPattern":"try { session.execute(createMvStmt); } catch (InvalidQueryException e) { if (e.getMessage().contains(\"Duplicate column\")) { /* dedupe PRIMARY KEY clause */ } }","preventionTips":["Deduplicate PRIMARY KEY columns when generating DDL programmatically","Review hand-edited PRIMARY KEY clauses for copy-paste duplicates"],"tags":["cql","materialized-view","primary-key","duplicate-column"],"backgroundTag":"duplicate-key","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}