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


##########
src/java/org/apache/cassandra/index/sai/plan/vector/VectorQueryController.java:
##########
@@ -0,0 +1,247 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.index.sai.plan.vector;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.cassandra.db.ColumnFamilyStore;
+import org.apache.cassandra.db.ReadCommand;
+import org.apache.cassandra.db.filter.RowFilter;
+import org.apache.cassandra.db.memtable.Memtable;
+import org.apache.cassandra.exceptions.QueryCancelledException;
+import org.apache.cassandra.index.sai.QueryContext;
+import org.apache.cassandra.index.sai.disk.PrimaryKeyMap;
+import org.apache.cassandra.index.sai.disk.SSTableIndex;
+import org.apache.cassandra.index.sai.disk.SSTableRowIdKeyRangeIterator;
+import org.apache.cassandra.index.sai.disk.v1.postings.MergePostingList;
+import org.apache.cassandra.index.sai.disk.v1.vector.CheckpointingIterator;
+import org.apache.cassandra.index.sai.iterators.KeyRangeIntersectionIterator;
+import org.apache.cassandra.index.sai.iterators.KeyRangeIterator;
+import org.apache.cassandra.index.sai.iterators.KeyRangeUnionIterator;
+import org.apache.cassandra.index.sai.metrics.TableQueryMetrics;
+import org.apache.cassandra.index.sai.plan.Expression;
+import org.apache.cassandra.index.sai.plan.StorageAttachedIndexQueryController;
+import org.apache.cassandra.index.sai.postings.PeekablePostingList;
+import org.apache.cassandra.index.sai.postings.PostingList;
+import org.apache.cassandra.index.sai.postings.PostingListIntersection;
+import org.apache.cassandra.index.sai.postings.PostingListUnion;
+import org.apache.cassandra.io.sstable.format.SSTableReader;
+import org.apache.cassandra.utils.Throwables;
+
+public class VectorQueryController extends StorageAttachedIndexQueryController
+{
+    private static final Logger logger = 
LoggerFactory.getLogger(VectorQueryController.class);
+
+    public VectorQueryController(ColumnFamilyStore cfs,
+                                 ReadCommand command,
+                                 RowFilter filterOperation,
+                                 QueryContext queryContext,
+                                 TableQueryMetrics tableQueryMetrics)
+    {
+        super(cfs, command, filterOperation, queryContext, tableQueryMetrics);
+    }
+
+    /**
+     * Build a {@link KeyRangeIterator.Builder} from the given list of 
expressions by applying given operation (OR/AND).
+     * Building of such builder involves index search, results of which are 
persisted in the internal resources list
+     *
+     * @param expressions The expressions to build range iterator from 
(expressions with not results are ignored).
+     *
+     * @return range iterator builder based on given expressions and operation 
type.
+     */
+    public KeyRangeIterator.Builder 
getIndexQueryResults(Collection<Expression> expressions)
+    {
+        // FIXME having this at the expression level means that it only gets 
applied to Nodes at that level;
+        // moving it to ORDER BY will allow us to apply it correctly for other 
Node sub-trees
+        var annExpressionInHybridSearch = 
getAnnExpressionInHybridSearch(expressions);
+        boolean isAnnHybridSearch = annExpressionInHybridSearch != null;
+        if (isAnnHybridSearch)
+            expressions = expressions.stream().filter(e -> e != 
annExpressionInHybridSearch).collect(Collectors.toList());
+
+        // search memtable before referencing sstable indexes; otherwise we 
may miss newly flushed memtable index
+        var iteratorsByMemtable = expressions.stream()
+                                             .filter(expr -> 
!expr.context.isNotIndexed())
+                                             .flatMap(expr -> 
expr.context.getMemtableIndexManager().iteratorsForSearch(queryContext, expr, 
mergeRange).stream())
+                                             
.collect(Collectors.groupingBy(pair -> pair.left, Collectors.mapping(pair -> 
pair.right, Collectors.toList())));
+
+        VectorQueryViewBuilder.QueryView queryView = new 
VectorQueryViewBuilder(expressions, mergeRange).build();
+        // in case of ANN query in hybrid search, we have to reference ANN 
sstable indexes separately because queryView doesn't include ANN sstable indexes
+        VectorQueryViewBuilder.QueryView annQueryViewInHybridSearch = 
isAnnHybridSearch ? new 
VectorQueryViewBuilder(Collections.singleton(annExpressionInHybridSearch), 
mergeRange).build() : null;
+
+        try
+        {
+            List<KeyRangeIterator> sstableIntersections = new 
ArrayList<>(queryView.view.size());
+            for (Map.Entry<SSTableReader, 
List<VectorQueryViewBuilder.IndexExpression>> viewEntry : 
queryView.view.entrySet())
+            {
+                PostingList postings = 
createRowIdIterator(viewEntry.getValue(), isAnnHybridSearch);
+                if (isAnnHybridSearch)
+                    
sstableIntersections.add(reorderAndLimitBySSTableRowIds(postings, 
viewEntry.getKey(), annQueryViewInHybridSearch));
+                else
+                {
+                    PrimaryKeyMap.Factory pkFactory = 
viewEntry.getValue().iterator().next().index.getSSTableContext().primaryKeyMapFactory;
+                    
sstableIntersections.add(convertToPrimaryKeyIterator(pkFactory, postings));
+                }
+            }
+
+            List<KeyRangeIterator> memtableIntersections = 
iteratorsByMemtable.entrySet()
+                                                                              
.stream()
+                                                                              
.map(e -> {
+                                                                               
   // we need to do all the intersections at the index level, or ordering won't 
work
+                                                                               
   KeyRangeIterator it = buildIterator(e.getValue(), isAnnHybridSearch);
+                                                                               
   if (isAnnHybridSearch)
+                                                                               
       it = reorderAndLimitBy(it, e.getKey(), annExpressionInHybridSearch);
+                                                                               
   return it;
+                                                                              
})
+                                                                              
.collect(Collectors.toList());
+
+            Iterable<KeyRangeIterator> allIntersections = 
Iterables.concat(sstableIntersections, memtableIntersections);

Review Comment:
   I didn't do an exhaustive comparison but it looks like we're still missing 
the tests that 49a51690 added in ComplexQueryTest, VectorTypeTest, and 
VectorUpdateDeleteTest.



-- 
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