apache/cassandra · error · InvalidRequestException

Reversed queries are not supported.

Error message

Reversed queries are not supported.

What it means

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.

Source

Thrown at src/java/org/apache/cassandra/db/virtual/PartitionKeyStatsTable.java:154

        this.metadata = TableMetadata.builder(keyspace, NAME)
                                     .kind(TableMetadata.Kind.VIRTUAL)
                                     .partitioner(new LocalPartitioner(CompositeType.getInstance(UTF8Type.instance, UTF8Type.instance)))
                                     .addPartitionKeyColumn(COLUMN_KEYSPACE_NAME, UTF8Type.instance)
                                     .addPartitionKeyColumn(COLUMN_TABLE_NAME, UTF8Type.instance)
                                     .addClusteringColumn(COLUMN_TOKEN_VALUE, IntegerType.instance)
                                     .addClusteringColumn(COLUMN_KEY, UTF8Type.instance)
                                     .addRegularColumn(COLUMN_SIZE_ESTIMATE, CounterColumnType.instance)
                                     .addRegularColumn(COLUMN_SSTABLES, CounterColumnType.instance)
                                     .build();
        sizeEstimateColumn = metadata.regularColumns().getSimple(0);
        sstablesColumn = metadata.regularColumns().getSimple(1);
    }

    @Override
    public UnfilteredPartitionIterator select(DecoratedKey partitionKey, ClusteringIndexFilter clusteringIndexFilter, ColumnFilter columnFilter, RowFilter rowFilter, DataLimits limits)
    {
        if (clusteringIndexFilter.isReversed())
            throw new InvalidRequestException(REVERSED_QUERY_ERROR);

        ByteBuffer[] key = ((CompositeType) this.metadata.partitionKeyType).split(partitionKey.getKey());
        String keyspace = UTF8Type.instance.getString(key[0]);
        String table = UTF8Type.instance.getString(key[1]);

        KeyspaceMetadata ksm = Schema.instance.getKeyspaceMetadata(keyspace);
        if (ksm == null)
            throw invalidRequest(KEYSPACE_NOT_EXIST_ERROR, keyspace);

        TableMetadata metadata = ksm.getTableOrViewNullable(table);
        if (metadata == null)
            throw invalidRequest(TABLE_NOT_EXIST_ERROR, table, keyspace);

        if (!metadata.partitioner.supportsSplitting())
            throw invalidRequest(PARTITIONER_NOT_SUPPORTED, metadata.partitioner.getClass().getName(), table, keyspace);

        AbstractBounds<PartitionPosition> range = getBounds(metadata, clusteringIndexFilter, rowFilter);
        return new SingletonUnfilteredPartitionIterator(select(partitionKey, metadata, clusteringIndexFilter, range));

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Re-run the query without reversed ordering (drop the DESC/ORDER BY on the virtual table).
  2. Retrieve rows in default order and reverse them client-side.
  3. If you need newest-first data from a real table, query the actual table, not the virtual stats table.

Example fix

// before
SELECT * FROM system_views.partitions WHERE keyspace_name='ks' AND table_name='t' ORDER BY partition_index DESC;
// after
SELECT * FROM system_views.partitions WHERE keyspace_name='ks' AND table_name='t';
// reverse client-side if needed
Defensive patterns

Strategy: validation

Validate before calling

// Issue plain (non-reversed) queries on system_views.partitions
String cql = "SELECT * FROM system_views.partitions WHERE keyspace_name=? AND table_name=?"; // no ORDER BY ... DESC

Try / catch

try { rs = session.execute(query); }
catch (com.datastax.oss.driver.api.core.servererrors.InvalidQueryException e) {
    if (e.getMessage().contains("Reversed queries are not supported")) { /* rewrite query without reversal and retry once */ }
    else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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