{"record":{"id":"403b2c28e886892a","repo":"apache/cassandra","slug":"no-order-by-clause-allowed-within-a-transaction","errorCode":null,"errorMessage":"No ORDER BY clause allowed within a transaction; %s statement %s","messagePattern":"No ORDER BY clause allowed within a transaction; (.+?) statement (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/TransactionStatement.java","lineNumber":698,"sourceCode":"    }\n\n    @Override\n    public AuditLogContext getAuditLogContext()\n    {\n        return new AuditLogContext(AuditLogEntryType.TRANSACTION);\n    }\n\n    @Override\n    public boolean eligibleAsPreparedStatement()\n    {\n        // false is the default, but still best to be explicit.\n        return false;\n    }\n\n    private static void validate(SelectStatement.RawStatement select)\n    {\n        if (select.parameters.orderings != null && !select.parameters.orderings.isEmpty())\n            throw invalidRequest(NO_ORDER_BY_IN_TXNS_MESSAGE, \"SELECT\", select.source);\n        if (select.parameters.groups != null && !select.parameters.groups.isEmpty())\n            throw invalidRequest(NO_GROUP_BY_IN_TXNS_MESSAGE, \"SELECT\", select.source);\n    }\n\n    private static void validate(SelectStatement prepared)\n    {\n        if (!prepared.table.isAccordEnabled())\n            throw invalidRequest(TRANSACTIONS_DISABLED_ON_TABLE_MESSAGE, \"SELECT\", prepared.source);\n        if (prepared.table.params.pendingDrop)\n            throw invalidRequest(TRANSACTIONS_DISABLED_ON_TABLE_BEING_DROPPED_MESSAGE, \"SELECT\", prepared.source);\n        if (prepared.table.isCounter())\n            throw invalidRequest(NO_COUNTERS_IN_TXNS_MESSAGE, \"SELECT\", prepared.source);\n        if (prepared.hasAggregation())\n            throw invalidRequest(NO_AGGREGATION_IN_TXNS_MESSAGE, \"SELECT\", prepared.source);\n\n        // when \"LIMIT ?\" this check can't be performed, so need to do again once the options are known\n        if (prepared.getRestrictions().keyIsInRelation())\n            checkTrue(prepared.isLimitMarker() || prepared.getLimit(null) == DataLimits.NO_LIMIT, NO_PARTITION_IN_CLAUSE_WITH_LIMIT, \"SELECT\", prepared.source);","sourceCodeStart":680,"sourceCodeEnd":716,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/TransactionStatement.java#L680-L716","documentation":"Cassandra rejects SELECT statements that carry an ORDER BY clause when executed inside an Accord transaction block. Ordering is not supported by the transactional read path, so any SELECT with orderings is rejected at prepare/validate time with an InvalidRequestException. The message includes the statement type and its source text.","triggerScenarios":"Running a transaction (BEGIN TRANSACTION ... COMIT) whose SELECT over an Accord-enabled table includes `ORDER BY <column>` in its ordering clause; caught in TransactionStatement.validate(SelectStatement.RawStatement) when select.parameters.orderings is non-empty.","commonSituations":"Porting existing interactive CQL SELECTs into a transaction unchanged; copying application queries that relied on per-partition ORDER BY into a txn block.","solutions":["Remove the ORDER BY clause from the SELECT inside the transaction","Sort results client-side after the transaction returns","Move the ordering query outside the transaction if atomicity is not required"],"exampleFix":"// before\nBEGIN TRANSACTION\n  SELECT ... FROM t WHERE pk = 1 ORDER BY ck DESC;\nCOMMIT TRANSACTION;\n// after\nBEGIN TRANSACTION\n  SELECT ... FROM t WHERE pk = 1;\nCOMMIT TRANSACTION;\n// then sort client-side","handlingStrategy":"validation","validationCode":"if (query.toUpperCase().matches(\"(?s).*ORDER\\\\s+BY.*\") && inTransaction) throw new IllegalArgumentException(\"ORDER BY not allowed in transaction\");","typeGuard":null,"tryCatchPattern":"try { session.execute(txnCql); } catch (InvalidRequestException e) { if (e.getMessage().contains(\"No ORDER BY clause allowed within a transaction\")) { /* strip ORDER BY and retry */ } else throw e; }","preventionTips":["Keep transaction bodies to plain point reads/writes","Lint CQL for ORDER BY/GROUP BY inside BEGIN TRANSACTION blocks","Sort results in application code"],"tags":["cql","transactions","accord","validation"],"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"}