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


##########
src/java/org/apache/cassandra/index/Index.java:
##########
@@ -440,6 +445,19 @@ default boolean filtersMultipleContains()
      */
     public RowFilter getPostIndexQueryFilter(RowFilter filter);
 
+    /**
+     * Return a comparator that reorders query result before sending to client
+     *
+     * @param restriction restriction that requires current index
+     * @param columnIndex idx of the indexed column in returned row
+     * @param options query options
+     * @return comparator that for post-query ordering; or null if not 
supported
+     */
+    default Comparator<List<ByteBuffer>> getPostQueryOrdering(Restriction 
restriction, int columnIndex, QueryOptions options)

Review Comment:
   Ah, it only takes a restriction, so it's fixed to a single-column ordering. 
In that case, I think we could omit the column index arg and make it return a 
column value comparator, instead of a row comparator:
   ```suggestion
       default Comparator<ByteBuffer> getPostQueryOrdering(Restriction 
restriction, QueryOptions options)
   ```
   So the CQL layer could take care of dealing with the column index in the 
result set. For example, in `SelectStatement.IndexColumnComparator#prepareFor`:
   ```java
   Comparator<ByteBuffer> comparator = index.getPostQueryOrdering(restriction, 
options);
   return (a, b) -> compare(comparator, a.get(columnIndex), b.get(columnIndex));
   ```
   And the SAI implementation would be:
   ```java
   @Override
   public Comparator<ByteBuffer> getPostQueryOrdering(Restriction restriction, 
QueryOptions options)
   {
       // For now, only support ANN
       assert restriction instanceof SingleColumnRestriction.AnnRestriction;
   
       Preconditions.checkState(indexContext.isVector());
   
       SingleColumnRestriction.AnnRestriction annRestriction = 
(SingleColumnRestriction.AnnRestriction) restriction;
       VectorSimilarityFunction function = 
indexContext.getIndexWriterConfig().getSimilarityFunction();
   
       float[] target = TypeUtil.decomposeVector(indexContext, 
annRestriction.value(options).duplicate());
   
       return (leftBuf, rightBuf) -> {
           float[] left = TypeUtil.decomposeVector(indexContext, 
leftBuf.duplicate());
           double scoreLeft = function.compare(left, target);
   
           float[] right = TypeUtil.decomposeVector(indexContext, 
rightBuf.duplicate());
           double scoreRight = function.compare(right, target);
           return Double.compare(scoreRight, scoreLeft); // descending 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