{"record":{"id":"4c7aa9003a7102c7","repo":"apache/cassandra","slug":"descending-ann-ordering-is-not-supported","errorCode":null,"errorMessage":"Descending ANN ordering is not supported","messagePattern":"Descending ANN ordering is not supported","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/restrictions/StatementRestrictions.java","lineNumber":556,"sourceCode":"     * Indexes are selected based on RowFilter only, so we need to turn orderings into restrictions\n     * so they end up in the row filter.\n     *\n     * @param orderings orderings from the select statement\n     * @return the {@link RestrictionSet} with the added orderings\n     */\n    private RestrictionSet addOrderingRestrictions(List<Ordering> orderings, RestrictionSet restrictionSet)\n    {\n        List<Ordering> annOrderings = orderings.stream().filter(o -> o.expression.hasNonClusteredOrdering()).collect(Collectors.toList());\n\n        if (annOrderings.size() > 1)\n            throw new InvalidRequestException(\"Cannot specify more than one ANN ordering\");\n        else if (annOrderings.size() == 1)\n        {\n            if (orderings.size() > 1)\n                throw new InvalidRequestException(\"ANN ordering does not support any other ordering\");\n            Ordering annOrdering = annOrderings.get(0);\n            if (annOrdering.direction != Ordering.Direction.ASC)\n                throw new InvalidRequestException(\"Descending ANN ordering is not supported\");\n            SingleRestriction restriction = annOrdering.expression.toRestriction();\n            return restrictionSet.addRestriction(restriction);\n        }\n        return restrictionSet;\n    }\n\n    private void processPartitionKeyRestrictions(ClientState state, boolean hasQueriableIndex, boolean allowFiltering, boolean forView)\n    {\n        if (!type.allowPartitionKeyRanges())\n        {\n            checkFalse(partitionKeyRestrictions.isOnToken(),\n                       \"The token function cannot be used in WHERE clauses for %s statements\", type);\n\n            if (partitionKeyRestrictions.hasUnrestrictedPartitionKeyComponents())\n                throw invalidRequest(\"Some partition key parts are missing: %s\",\n                                     Joiner.on(\", \").join(getPartitionKeyUnrestrictedComponents()));\n\n            // slice query","sourceCodeStart":538,"sourceCodeEnd":574,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/restrictions/StatementRestrictions.java#L538-L574","documentation":"InvalidRequestException from addOrderingRestrictions: an ANN (approximate nearest neighbor / vector similarity) ordering was specified as DESC. Vector similarity search ordering is inherently ascending-only, so a descending ANN ORDER BY is rejected while translating orderings into restrictions for the row filter.","triggerScenarios":"A SELECT with ORDER BY ann_of(vector_column) : [...] DESC on a vector-enabled table.","commonSituations":"Developers assume DESC gives 'most distant first' or copy DESC from other ORDER BY clauses; ANN always returns nearest neighbors, so only ASC is accepted.","solutions":["Drop the DESC keyword; ANN ordering must be ASC (or defaulted).","If farthest-first ordering is needed, fetch ANN results and reverse/filter them client-side.","Use a different similarity metric/vector encoding if the intended semantics differ."],"exampleFix":"// before\nSELECT * FROM items ORDER BY ann_of(vec) : [0.1,0.2] DESC;\n// after\nSELECT * FROM items ORDER BY ann_of(vec) : [0.1,0.2];","handlingStrategy":"validation","validationCode":"if (annOrdering != null && annOrdering.direction == Direction.DESC)\n    throw new IllegalArgumentException(\"ANN ordering must be ASC\");","typeGuard":null,"tryCatchPattern":"try { session.execute(stmt); }\ncatch (InvalidRequestException e) {\n    if (e.getMessage().equals(\"Descending ANN ordering is not supported\")) { /* retry with ASC */ }\n    else throw e;\n}","preventionTips":["Never emit DESC for vector similarity orderings.","Treat ANN results as nearest-first by definition.","Reverse results client-side if descending order is wanted."],"tags":["cassandra","cql","ann","vector-search","ordering"],"backgroundTag":"invalid-enum-value","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"}