From 20e058b2bd0ae452966afa7f548bd305e2f742c4 Mon Sep 17 00:00:00 2001 From: Sylvain Lebresne Date: Wed, 21 May 2014 11:29:42 +0200 Subject: [PATCH] Fix 2ndary index queries with DESC clustering order patch by slebresne; reviewed by thobbs for CASSANDRA-6950 --- CHANGES.txt | 1 + .../cql3/statements/SelectStatement.java | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index df3741e760..bd0031e927 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -46,6 +46,7 @@ * reduce garbage on codec flag deserialization (CASSANDRA-7244) * Proper null handle for IF with map element access (CASSANDRA-7155) * Improve compaction visibility (CASSANDRA-7242) + * Fix 2ndary index queries with DESC clustering order (CASSANDRA-6950) Merged from 1.2: * Add Cloudstack snitch (CASSANDRA-7147) * Update system.peers correctly when relocating tokens (CASSANDRA-7126) diff --git a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java index 2468eb9361..6b4309f08c 100644 --- a/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java +++ b/src/java/org/apache/cassandra/cql3/statements/SelectStatement.java @@ -927,7 +927,14 @@ public class SelectStatement implements CQLStatement, MeasurableForPreparedCache throw new InvalidRequestException(String.format("Unsupported null value for indexed column %s", name)); if (value.remaining() > 0xFFFF) throw new InvalidRequestException("Index expression values may not be larger than 64K"); - expressions.add(new IndexExpression(name.name.key, slice.getIndexOperator(b), value)); + + IndexOperator op = slice.getIndexOperator(b); + // If the underlying comparator for name is reversed, we need to reverse the IndexOperator: user operation + // always refer to the "forward" sorting even if the clustering order is reversed, but the 2ndary code does + // use the underlying comparator as is. + if (name.type instanceof ReversedType) + op = reverse(op); + expressions.add(new IndexExpression(name.name.key, op, value)); } } } @@ -949,6 +956,18 @@ public class SelectStatement implements CQLStatement, MeasurableForPreparedCache return expressions; } + private static IndexOperator reverse(IndexOperator op) + { + switch (op) + { + case LT: return IndexOperator.GT; + case LTE: return IndexOperator.GTE; + case GT: return IndexOperator.LT; + case GTE: return IndexOperator.LTE; + default: return op; + } + } + private ResultSet process(List rows, List variables, int limit, long now) throws InvalidRequestException { Selection.ResultSetBuilder result = selection.resultSetBuilder(now);