jasonstack commented on a change in pull request #735:
URL: https://github.com/apache/cassandra/pull/735#discussion_r682276829



##########
File path: src/java/org/apache/cassandra/index/SecondaryIndexManager.java
##########
@@ -1088,46 +1123,63 @@ public Index getBestIndexFor(RowFilter rowFilter)
                 RowFilter.CustomExpression customExpression = 
(RowFilter.CustomExpression) expression;
                 logger.trace("Command contains a custom index expression, 
using target index {}", customExpression.getTargetIndex().name);
                 Tracing.trace("Command contains a custom index expression, 
using target index {}", customExpression.getTargetIndex().name);
-                return indexes.get(customExpression.getTargetIndex().name);
-            }
-            else if (!expression.isUserDefined())
-            {
-                indexes.values().stream()
-                       .filter(index -> 
index.supportsExpression(expression.column(), expression.operator()))
-                       .forEach(searchableIndexes::add);
+                Index.Group group = 
getIndexGroup(customExpression.getTargetIndex());
+                return group == null ? null : group.queryPlanFor(rowFilter);
             }
         }
 
-        if (searchableIndexes.isEmpty())
+        Set<Index.QueryPlan> queryPlans = indexGroups.values()
+                                                     .stream()
+                                                     .map(g -> 
g.queryPlanFor(rowFilter))
+                                                     .filter(Objects::nonNull)
+                                                     
.collect(Collectors.toSet());
+
+        if (queryPlans.isEmpty())
         {
             logger.trace("No applicable indexes found");
             Tracing.trace("No applicable indexes found");
             return null;
         }
 
-        Index selected = searchableIndexes.size() == 1
-                         ? Iterables.getOnlyElement(searchableIndexes)
-                         : searchableIndexes.stream()
-                                            .min((a, b) -> 
Longs.compare(a.getEstimatedResultRows(),
-                                                                         
b.getEstimatedResultRows()))
-                                            .orElseThrow(() -> new 
AssertionError("Could not select most selective index"));
+        // find the best plan
+        Index.QueryPlan selected = queryPlans.size() == 1
+                                   ? Iterables.getOnlyElement(queryPlans)
+                                   : queryPlans.stream()
+                                               .min(Comparator.naturalOrder())
+                                               .orElseThrow(() -> new 
AssertionError("Could not select most selective index"));
 
         // pay for an additional threadlocal get() rather than build the 
strings unnecessarily
         if (Tracing.isTracing())
         {
             Tracing.trace("Index mean cardinalities are {}. Scanning with {}.",
-                          searchableIndexes.stream().map(i -> 
i.getIndexMetadata().name + ':' + i.getEstimatedResultRows())
-                                           .collect(Collectors.joining(",")),
-                          selected.getIndexMetadata().name);
+                          queryPlans.stream()
+                                    .map(p -> commaSeparated(p.getIndexes()) + 
':' + p.getEstimatedResultRows())
+                                    .collect(Collectors.joining(",")),
+                          commaSeparated(selected.getIndexes()));
         }
         return selected;
     }
 
+    private static String commaSeparated(Collection<Index> indexes)
+    {
+        return indexes.stream().map(i -> 
i.getIndexMetadata().name).collect(Collectors.joining(","));
+    }
+
+
     public Optional<Index> getBestIndexFor(RowFilter.Expression expression)
     {
         return indexes.values().stream().filter((i) -> 
i.supportsExpression(expression.column(), expression.operator())).findFirst();
     }
 
+    public <T extends Index> Optional<T> getBestIndexFor(RowFilter.Expression 
expression, Class<T> indexType)

Review comment:
       removed




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