{"record":{"id":"147954fdbe8bb913","repo":"apache/cassandra","slug":"duration-type-is-not-supported-for-primary-key-col","errorCode":null,"errorMessage":"duration type is not supported for PRIMARY KEY column '%s'","messagePattern":"duration type is not supported for PRIMARY KEY column '(.+?)'","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java","lineNumber":315,"sourceCode":"\n            if (!primaryKeyColumns.add(column))\n                throw ire(\"Duplicate column '%s' in PRIMARY KEY clause for table '%s'\", column, tableName);\n\n            AbstractType<?> type = properties.type;\n            if (type.isMultiCell())\n            {\n                CQL3Type cqlType = properties.cqlType;\n                if (type.isCollection())\n                    throw ire(\"Invalid non-frozen collection type %s for PRIMARY KEY column '%s'\", cqlType, column);\n                else\n                    throw ire(\"Invalid non-frozen user-defined type %s for PRIMARY KEY column '%s'\", cqlType, column);\n            }\n\n            if (type.isCounter())\n                throw ire(\"counter type is not supported for PRIMARY KEY column '%s'\", column);\n\n            if (type.referencesDuration())\n                throw ire(\"duration type is not supported for PRIMARY KEY column '%s'\", column);\n\n            if (staticColumns.contains(column))\n                throw ire(\"Static column '%s' cannot be part of the PRIMARY KEY\", column);\n        });\n\n        List<ColumnProperties> partitionKeyColumnProperties = new ArrayList<>();\n        List<ColumnProperties> clusteringColumnProperties = new ArrayList<>();\n\n        partitionKeyColumns.forEach(column ->\n        {\n            ColumnProperties columnProperties = columns.remove(column);\n            partitionKeyColumnProperties.add(columnProperties);\n        });\n\n        clusteringColumns.forEach(column ->\n        {\n            ColumnProperties columnProperties = columns.remove(column);\n            boolean reverse = !clusteringOrder.getOrDefault(column, true);","sourceCodeStart":297,"sourceCodeEnd":333,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java#L297-L333","documentation":"Thrown from CreateTableStatement's builder while validating PRIMARY KEY columns when a key column's type is a Duration. Durations cannot be serialized in a deterministic byte-comparable way for partitioning/clustering, so using them in a PRIMARY KEY is rejected.","triggerScenarios":"Declaring a partition or clustering column as `duration` or a type that references duration (e.g. a frozen collection/UDT containing duration) and using it in the PRIMARY KEY.","commonSituations":"Storing time intervals as key components; users assuming duration behaves like timestamp/uuid in keys.","solutions":["Use timestamp, time, or bigint (e.g. nanoseconds) as the key column instead of duration","Keep the duration column as a regular non-key column","Convert durations to a fixed-unit numeric representation before keying"],"exampleFix":"// before\nCREATE TABLE t (d duration, v int, PRIMARY KEY (d));\n// after\nCREATE TABLE t (d bigint, v int, PRIMARY KEY (d)); // duration stored as nanoseconds","handlingStrategy":"validation","validationCode":"if (keyColumnType.referencesDuration()) throw new IllegalArgumentException(\"duration cannot be used in PRIMARY KEY; use bigint/timestamp instead\");","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); } catch (InvalidRequestException e) { if (e.getMessage().contains(\"duration type is not supported for PRIMARY KEY\")) { /* substitute a numeric/timestamp key type */ } else throw e; }","preventionTips":["Avoid duration in key or clustering positions","Store durations as fixed-unit numbers when ordering is needed","Check nested types (frozen collections/UDTs) for duration references"],"tags":["cql","duration","primary-key","types"],"backgroundTag":"unsupported-operation","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"}