Skip to content

Conversation

arvindksi274-ksolves
Copy link

@arvindksi274-ksolves arvindksi274-ksolves commented Oct 6, 2025

Fix CASSANDRA-20154: BETWEEN operator returns wrong results when start bound is greater than end bound.

This PR fixes a bug where queries like SELECT * FROM table WHERE column BETWEEN 5 AND 3
incorrectly return rows instead of an empty result set. The issue was caused by Cassandra
automatically sorting BETWEEN bounds, which violates SQL standards.

Changes made:

  • Modified SimpleRestriction.java to preserve user-specified bound order for BETWEEN queries
  • Updated ClusteringColumnRestrictions.java to properly handle BETWEEN on first clustering column
  • Added tests to verify inverted BETWEEN bounds now return empty results

Before: WHERE pk BETWEEN 5 AND 3 would return rows between 3 and 5
After: WHERE pk BETWEEN 5 AND 3 correctly returns no rows (as per SQL standard)

By Arvind Kandpal for CASSANDRA-20154

@arvindksi274-ksolves
Copy link
Author

Hi team!

This PR has been open for 4 days and I'd appreciate some feedback on my fix for CASSANDRA-20154.

Quick Summary:

  • Fixes BETWEEN operator to handle inverted bounds correctly per SQL standards
  • WHERE pk BETWEEN 5 AND 3 now returns empty results (was incorrectly returning rows)
  • Changes in SimpleRestriction.java and ClusteringColumnRestrictions.java
  • Added comprehensive tests to verify the fix

Technical Details:
The core issue was Cassandra automatically sorting BETWEEN bounds, causing inverted bounds like BETWEEN 5 AND 3 to return rows instead of empty results. This PR preserves user-specified bound order to match SQL standard behavior.

Testing:

  • Added tests for inverted BETWEEN bounds
  • Verified existing functionality remains intact
  • Covers both partition key and clustering column scenarios

Would really appreciate review from the community! Particularly looking for feedback on:

  1. Any edge cases I might have missed
  2. Performance implications
  3. Additional test scenarios to consider

@dineshjoshi @maedhroz @smiklosovic @jmckenzie-dev @ifesdjeen - would any of you have time to take a look when convenient?

Thanks for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant