{"record":{"id":"481b6e2994d578fd","repo":"apache/cassandra","slug":"only-clustering-key-columns-can-be-defined-in-clus","errorCode":null,"errorMessage":"Only clustering key columns can be defined in CLUSTERING ORDER directive: %s are not clustering columns","messagePattern":"Only clustering key columns can be defined in CLUSTERING ORDER directive: (.+?) are not clustering columns","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java","lineNumber":342,"sourceCode":"        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);\n            clusteringColumnProperties.add(reverse ? columnProperties.withReversedType() : columnProperties);\n        });\n\n        List<ColumnIdentifier> nonClusterColumn = clusteringOrder.keySet().stream()\n                                                                 .filter((id) -> !clusteringColumns.contains(id))\n                                                                 .collect(Collectors.toList());\n        if (!nonClusterColumn.isEmpty())\n        {\n            throw ire(\"Only clustering key columns can be defined in CLUSTERING ORDER directive: \" + nonClusterColumn + \" are not clustering columns\");\n        }\n\n        int n = 0;\n        for (ColumnIdentifier id : clusteringOrder.keySet())\n        {\n            ColumnIdentifier c = clusteringColumns.get(n);\n            if (!id.equals(c))\n            {\n                if (clusteringOrder.containsKey(c))\n                    throw ire(\"The order of columns in the CLUSTERING ORDER directive must match that of the clustering columns (%s must appear before %s)\", c, id);\n                else\n                    throw ire(\"Missing CLUSTERING ORDER for column %s\", c);\n            }\n            ++n;\n        }\n\n        // For COMPACT STORAGE, we reject any \"feature\" that we wouldn't be able to translate back to thrift.\n        if (useCompactStorage)","sourceCodeStart":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java#L324-L360","documentation":"During CREATE TABLE, Cassandra validates that every column named in the CLUSTERING ORDER BY directive is actually one of the table's clustering columns. The builder collects identifiers in clusteringOrder that are absent from clusteringColumns and throws this InvalidRequest if any remain. This prevents a clustering order definition referencing columns that have no clustering semantics.","triggerScenarios":"CREATE TABLE with WITH CLUSTERING ORDER BY (some_col ASC/DESC) where some_col is a regular, static, or partition-key column rather than a clustering column.","commonSituations":"Typos in the column name inside CLUSTERING ORDER BY; moving a column from clustering to partition key in a refactor; assuming a PRIMARY KEY column listed first can be ordered.","solutions":["Move the named column into the clustering portion of the PRIMARY KEY (after the partition key).","Remove the column from the CLUSTERING ORDER BY clause so it only lists clustering columns.","Fix the column name typo in CLUSTERING ORDER BY so it matches an actual clustering column."],"exampleFix":"// before\nCREATE TABLE t (pk int, c1 int, v text, PRIMARY KEY (pk)) WITH CLUSTERING ORDER BY (c1 DESC);\n// after\nCREATE TABLE t (pk int, c1 int, v text, PRIMARY KEY (pk, c1)) WITH CLUSTERING ORDER BY (c1 DESC);","handlingStrategy":"validation","validationCode":"// Parse the PRIMARY KEY clustering columns and the CLUSTERING ORDER BY list; before executing:\nSet<String> clustering = Set.of(\"c1\"); // from PRIMARY KEY (pk, c1)\nList<String> orderCols = List.of(\"c1\"); // parsed from CLUSTERING ORDER BY\nif (!clustering.containsAll(orderCols)) throw new IllegalArgumentException(\"CLUSTERING ORDER BY names non-clustering columns\");","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); } catch (com.datastax.driver.core.exceptions.InvalidQueryException e) { if (e.getMessage().contains(\"CLUSTERING ORDER\")) { /* fix schema DDL */ } else throw e; }","preventionTips":["Keep CLUSTERING ORDER BY limited to columns appearing after the partition key in PRIMARY KEY.","Generate CLUSTERING ORDER BY from the same list used to build the PRIMARY KEY.","Lint DDL scripts to cross-check ORDER BY columns against clustering columns."],"tags":["cql","schema","create-table","clustering-order"],"backgroundTag":"schema-validation-failed","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}