43
43
import org .apache .cassandra .db .PartitionPosition ;
44
44
import org .apache .cassandra .db .ReadCommand ;
45
45
import org .apache .cassandra .db .ReadExecutionController ;
46
- import org .apache .cassandra .db .filter .ColumnFilter ;
47
46
import org .apache .cassandra .db .marshal .FloatType ;
48
47
import org .apache .cassandra .db .partitions .UnfilteredPartitionIterator ;
49
48
import org .apache .cassandra .db .rows .AbstractUnfilteredRowIterator ;
@@ -124,7 +123,8 @@ public UnfilteredPartitionIterator search(ReadExecutionController executionContr
124
123
assert !(keysIterator instanceof KeyRangeIterator );
125
124
var scoredKeysIterator = (CloseableIterator <PrimaryKeyWithSortKey >) keysIterator ;
126
125
var result = new ScoreOrderedResultRetriever (scoredKeysIterator , filterTree , controller ,
127
- executionController , queryContext , command .limits ().count ());
126
+ executionController , queryContext , command .limits ().count (),
127
+ ordering .context .getDefinition ());
128
128
return new TopKProcessor (command ).filter (result );
129
129
}
130
130
catch (QueryView .Builder .MissingIndexException e )
@@ -479,6 +479,10 @@ public static class ScoreOrderedResultRetriever extends AbstractIterator<Unfilte
479
479
private final HashSet <PrimaryKey > processedKeys ;
480
480
private final Queue <UnfilteredRowIterator > pendingRows ;
481
481
482
+ // Null indicates we are not sending the synthetic score column to the coordinator
483
+ @ Nullable
484
+ private final ColumnMetadata syntheticScoreColumn ;
485
+
482
486
// The limit requested by the query. We cannot load more than softLimit rows in bulk because we only want
483
487
// to fetch the topk rows where k is the limit. However, we allow the iterator to fetch more rows than the
484
488
// soft limit to avoid confusing behavior. When the softLimit is reached, the iterator will fetch one row
@@ -491,7 +495,8 @@ private ScoreOrderedResultRetriever(CloseableIterator<PrimaryKeyWithSortKey> sco
491
495
QueryController controller ,
492
496
ReadExecutionController executionController ,
493
497
QueryContext queryContext ,
494
- int limit )
498
+ int limit ,
499
+ ColumnMetadata orderedColumn )
495
500
{
496
501
IndexContext context = controller .getOrderer ().context ;
497
502
this .view = controller .getQueryView (context ).view ;
@@ -507,6 +512,13 @@ private ScoreOrderedResultRetriever(CloseableIterator<PrimaryKeyWithSortKey> sco
507
512
this .processedKeys = new HashSet <>(limit );
508
513
this .pendingRows = new ArrayDeque <>(limit );
509
514
this .softLimit = limit ;
515
+
516
+ // When +score is added on the coordinator side, it's represented as a PrecomputedColumnFilter
517
+ // even in a 'SELECT *' because WCF is not capable of representing synthetic columns.
518
+ // This can be simplified when we remove ANN_USE_SYNTHETIC_SCORE
519
+ var tempColumn = ColumnMetadata .syntheticScoreColumn (orderedColumn , FloatType .instance );
520
+ var isScoreFetched = controller .command ().columnFilter ().fetchesExplicitly (tempColumn );
521
+ this .syntheticScoreColumn = isScoreFetched ? tempColumn : null ;
510
522
}
511
523
512
524
@ Override
@@ -663,7 +675,8 @@ public UnfilteredRowIterator readAndValidatePartition(PrimaryKey pk, List<Primar
663
675
}
664
676
}
665
677
}
666
- return isRowValid ? new PrimaryKeyIterator (partition , staticRow , row , sourceKeys , controller .command ()) : null ;
678
+ return isRowValid ? new PrimaryKeyIterator (partition , staticRow , row , sourceKeys , syntheticScoreColumn )
679
+ : null ;
667
680
}
668
681
}
669
682
@@ -684,7 +697,7 @@ public static class PrimaryKeyIterator extends AbstractUnfilteredRowIterator
684
697
private boolean consumed = false ;
685
698
private final Unfiltered row ;
686
699
687
- public PrimaryKeyIterator (UnfilteredRowIterator partition , Row staticRow , Unfiltered content , List <PrimaryKeyWithSortKey > primaryKeysWithScore , ReadCommand command )
700
+ public PrimaryKeyIterator (UnfilteredRowIterator partition , Row staticRow , Unfiltered content , List <PrimaryKeyWithSortKey > primaryKeysWithScore , ColumnMetadata syntheticScoreColumn )
688
701
{
689
702
super (partition .metadata (),
690
703
partition .partitionKey (),
@@ -702,16 +715,8 @@ public PrimaryKeyIterator(UnfilteredRowIterator partition, Row staticRow, Unfilt
702
715
return ;
703
716
}
704
717
705
- // When +score is added on the coordinator side, it's represented as a PrecomputedColumnFilter
706
- // even in a 'SELECT *' because WCF is not capable of representing synthetic columns.
707
- // This can be simplified when we remove ANN_USE_SYNTHETIC_SCORE
708
- var tm = metadata ();
709
- var scoreColumn = ColumnMetadata .syntheticColumn (tm .keyspace ,
710
- tm .name ,
711
- ColumnMetadata .SYNTHETIC_SCORE_ID ,
712
- FloatType .instance );
713
- var isScoreFetched = command .columnFilter ().fetchesExplicitly (scoreColumn );
714
- if (!isScoreFetched )
718
+
719
+ if (syntheticScoreColumn == null )
715
720
{
716
721
this .row = content ;
717
722
return ;
@@ -724,7 +729,7 @@ public PrimaryKeyIterator(UnfilteredRowIterator partition, Row staticRow, Unfilt
724
729
725
730
// inject +score as a new column
726
731
var pkWithScore = (PrimaryKeyWithScore ) primaryKeysWithScore .get (0 );
727
- columnData .add (BufferCell .live (scoreColumn ,
732
+ columnData .add (BufferCell .live (syntheticScoreColumn ,
728
733
FBUtilities .nowInSeconds (),
729
734
FloatType .instance .decompose (pkWithScore .indexScore )));
730
735
0 commit comments