tkhurana commented on code in PR #1701:
URL: https://github.com/apache/phoenix/pull/1701#discussion_r1349332165


##########
phoenix-core/src/main/java/org/apache/phoenix/optimize/QueryOptimizer.java:
##########
@@ -197,17 +199,41 @@ private List<QueryPlan> getApplicablePlans(QueryPlan 
dataPlan, PhoenixStatement
         return Collections.singletonList(compiler.compile());
     }
 
+    private static boolean isPartialIndexUsable(SelectStatement select, 
QueryPlan dataPlan,
+            PTable index) throws SQLException {
+
+        StatementContext context = new StatementContext(dataPlan.getContext());
+        context.setResolver(FromCompiler.getResolver(dataPlan.getTableRef()));
+        return WhereCompiler.contains(
+                
index.getIndexWhereExpression(dataPlan.getContext().getConnection()),
+                WhereCompiler.transformDNF(select.getWhere(), context));
+    }
+
     private List<QueryPlan> getApplicablePlansForSingleFlatQuery(QueryPlan 
dataPlan, PhoenixStatement statement, List<? extends PDatum> targetColumns, 
ParallelIteratorFactory parallelIteratorFactory, boolean stopAtBestPlan) throws 
SQLException {
         SelectStatement select = (SelectStatement)dataPlan.getStatement();
         // Exit early if we have a point lookup as we can't get better than 
that
         if (dataPlan.getContext().getScanRanges().isPointLookup() && 
stopAtBestPlan && dataPlan.isApplicable()) {
             return Collections.<QueryPlan> singletonList(dataPlan);
         }
-
-        List<PTable>indexes = 
Lists.newArrayList(dataPlan.getTableRef().getTable().getIndexes());
-        if (dataPlan.isApplicable() && (indexes.isEmpty() || 
dataPlan.isDegenerate() || dataPlan.getTableRef().hasDynamicCols() || 
select.getHint().hasHint(Hint.NO_INDEX))) {
+        List<PTable> indexList =  
dataPlan.getTableRef().getTable().getIndexes();
+        if (dataPlan.isApplicable() &&
+                (indexList.isEmpty() ||
+                        dataPlan.isDegenerate() ||
+                        dataPlan.getTableRef().hasDynamicCols() ||
+                        select.getHint().hasHint(Hint.NO_INDEX))) {
             return Collections.<QueryPlan> singletonList(dataPlan);
         }
+        // Include full indexes, and usable partial indexes
+        List<PTable> indexes = new ArrayList<>(indexList.size());
+        for (PTable index : indexList) {
+            if (index.getIndexWhere() != null) {
+                if (isPartialIndexUsable(select, dataPlan, index)) {

Review Comment:
   I think it will be better if we delay doing this check till we have 
determined that the partial index is usable based on other conditions. It could 
be potentially expensive to do all the DNF transformations only to later throw 
that work if that index doesn't qualify based on the search criteria. Once we 
have generated the list of applicable plans we can then do this check and if 
the partial index is not usable remove its plan.



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

Reply via email to