{"record":{"id":"6cfcf306f24a9cdd","repo":"apache/cassandra","slug":"reversed-queries-are-not-supported","errorCode":null,"errorMessage":"Reversed queries are not supported.","messagePattern":"Reversed queries are not supported\\.","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/db/virtual/PartitionKeyStatsTable.java","lineNumber":154,"sourceCode":"        this.metadata = TableMetadata.builder(keyspace, NAME)\n                                     .kind(TableMetadata.Kind.VIRTUAL)\n                                     .partitioner(new LocalPartitioner(CompositeType.getInstance(UTF8Type.instance, UTF8Type.instance)))\n                                     .addPartitionKeyColumn(COLUMN_KEYSPACE_NAME, UTF8Type.instance)\n                                     .addPartitionKeyColumn(COLUMN_TABLE_NAME, UTF8Type.instance)\n                                     .addClusteringColumn(COLUMN_TOKEN_VALUE, IntegerType.instance)\n                                     .addClusteringColumn(COLUMN_KEY, UTF8Type.instance)\n                                     .addRegularColumn(COLUMN_SIZE_ESTIMATE, CounterColumnType.instance)\n                                     .addRegularColumn(COLUMN_SSTABLES, CounterColumnType.instance)\n                                     .build();\n        sizeEstimateColumn = metadata.regularColumns().getSimple(0);\n        sstablesColumn = metadata.regularColumns().getSimple(1);\n    }\n\n    @Override\n    public UnfilteredPartitionIterator select(DecoratedKey partitionKey, ClusteringIndexFilter clusteringIndexFilter, ColumnFilter columnFilter, RowFilter rowFilter, DataLimits limits)\n    {\n        if (clusteringIndexFilter.isReversed())\n            throw new InvalidRequestException(REVERSED_QUERY_ERROR);\n\n        ByteBuffer[] key = ((CompositeType) this.metadata.partitionKeyType).split(partitionKey.getKey());\n        String keyspace = UTF8Type.instance.getString(key[0]);\n        String table = UTF8Type.instance.getString(key[1]);\n\n        KeyspaceMetadata ksm = Schema.instance.getKeyspaceMetadata(keyspace);\n        if (ksm == null)\n            throw invalidRequest(KEYSPACE_NOT_EXIST_ERROR, keyspace);\n\n        TableMetadata metadata = ksm.getTableOrViewNullable(table);\n        if (metadata == null)\n            throw invalidRequest(TABLE_NOT_EXIST_ERROR, table, keyspace);\n\n        if (!metadata.partitioner.supportsSplitting())\n            throw invalidRequest(PARTITIONER_NOT_SUPPORTED, metadata.partitioner.getClass().getName(), table, keyspace);\n\n        AbstractBounds<PartitionPosition> range = getBounds(metadata, clusteringIndexFilter, rowFilter);\n        return new SingletonUnfilteredPartitionIterator(select(partitionKey, metadata, clusteringIndexFilter, range));","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/db/virtual/PartitionKeyStatsTable.java#L136-L172","documentation":"PartitionKeyStatsTable (system_views.partitions virtual table) does not support reversed clustering order. When the query's clustering index filter is reversed (e.g. ORDER BY ... DESC semantics or paging backwards), select() throws InvalidRequestException before reading stats.","triggerScenarios":"Querying system_views.partitions with a reversed clustering filter, e.g. `SELECT * FROM system_views.partitions ... ORDER BY partition_index DESC` or a driver paging request with reversed=true.","commonSituations":"Users trying to get 'last N partitions' by ordering DESC on the virtual table; drivers issuing reversed slices for pagination; copying query patterns from normal tables where reverse reads are allowed.","solutions":["Re-run the query without reversed ordering (drop the DESC/ORDER BY on the virtual table).","Retrieve rows in default order and reverse them client-side.","If you need newest-first data from a real table, query the actual table, not the virtual stats table."],"exampleFix":"// before\nSELECT * FROM system_views.partitions WHERE keyspace_name='ks' AND table_name='t' ORDER BY partition_index DESC;\n// after\nSELECT * FROM system_views.partitions WHERE keyspace_name='ks' AND table_name='t';\n// reverse client-side if needed","handlingStrategy":"validation","validationCode":"// Issue plain (non-reversed) queries on system_views.partitions\nString cql = \"SELECT * FROM system_views.partitions WHERE keyspace_name=? AND table_name=?\"; // no ORDER BY ... DESC","typeGuard":null,"tryCatchPattern":"try { rs = session.execute(query); }\ncatch (com.datastax.oss.driver.api.core.servererrors.InvalidQueryException e) {\n    if (e.getMessage().contains(\"Reversed queries are not supported\")) { /* rewrite query without reversal and retry once */ }\n    else throw e;\n}","preventionTips":["Never use ORDER BY DESC on the partitions virtual table.","Reverse results client-side instead of server-side reversal.","Do not enable reversed paging on virtual-table queries in drivers."],"tags":["cassandra","virtual-table","query","reversed"],"backgroundTag":"unsupported-operation","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"}