adelapena commented on code in PR #2673:
URL: https://github.com/apache/cassandra/pull/2673#discussion_r1360986023


##########
src/java/org/apache/cassandra/cql3/statements/SelectStatement.java:
##########
@@ -1080,19 +1139,26 @@ public void processPartition(RowIterator partition, 
QueryOptions options, Result
 
     private boolean needsPostQueryOrdering()
     {
-        // We need post-query ordering only for queries with IN on the 
partition key and an ORDER BY.
-        return restrictions.keyIsInRelation() && 
!parameters.orderings.isEmpty();
+        // We need post-query ordering only for queries with IN on the 
partition key and an ORDER BY or index restriction reordering
+        return restrictions.keyIsInRelation() && 
!parameters.orderings.isEmpty() || needIndexOrdering();
+    }
+
+    private boolean needIndexOrdering()
+    {
+        return orderingComparator != null && 
orderingComparator.indexOrdering();
     }
 
     /**
      * Orders results when multiple keys are selected (using IN)
      */
-    private void orderResults(ResultSet cqlRows)
+    private void orderResults(ResultSet cqlRows, QueryOptions options)
     {
         if (cqlRows.size() == 0 || !needsPostQueryOrdering())
             return;
 
-        Collections.sort(cqlRows.rows, orderingComparator);
+        Comparator<List<ByteBuffer>> comparator = 
orderingComparator.prepareFor(table, getRowFilter(options), options);
+        if (comparator != null)
+            Collections.sort(cqlRows.rows, comparator);

Review Comment:
   Nit: this can be slightly simplified:
   ```suggestion
               cqlRows.rows.sort(comparator);
   ```
   Also, it might be worth adding a comment saying that this orders first by 
the indexed column, then by primary key. This guarantee will probably be useful 
if we support paging in the future. 
   
   The order is that way even when we only consider the indexed column when 
sorting here. That's because the replica results have been returned ordered by 
primary key. The stability of the used sorting algorithm (merge sort) ensures 
that rows with the same value-based index score preserve the original primary 
key order.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to