[ 
https://issues.apache.org/jira/browse/DRILL-6381?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16643358#comment-16643358
 ] 

ASF GitHub Bot commented on DRILL-6381:
---------------------------------------

vdiravka commented on a change in pull request #1466: DRILL-6381: Add support 
for index based planning and execution
URL: https://github.com/apache/drill/pull/1466#discussion_r223671338
 
 

 ##########
 File path: 
exec/java-exec/src/main/java/org/apache/drill/exec/planner/index/IndexSelector.java
 ##########
 @@ -0,0 +1,766 @@
+/*
+ * 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.drill.exec.planner.index;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.calcite.plan.RelOptCost;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelCollationTraitDef;
+import org.apache.calcite.rel.metadata.RelMdUtil;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.drill.common.expression.LogicalExpression;
+import org.apache.drill.exec.physical.base.DbGroupScan;
+import org.apache.drill.exec.planner.common.DrillJoinRelBase;
+import org.apache.drill.exec.planner.cost.DrillCostBase;
+import org.apache.drill.exec.planner.cost.PluginCost;
+import org.apache.drill.exec.planner.physical.PlannerSettings;
+import org.apache.drill.exec.planner.physical.PrelUtil;
+import org.apache.drill.exec.planner.physical.ScanPrel;
+import org.apache.drill.exec.planner.common.DrillScanRelBase;
+
+import org.apache.drill.shaded.guava.com.google.common.base.Preconditions;
+import org.apache.drill.shaded.guava.com.google.common.collect.ImmutableList;
+import org.apache.drill.shaded.guava.com.google.common.collect.Lists;
+import org.apache.drill.shaded.guava.com.google.common.collect.Maps;
+
+public class IndexSelector  {
+  static final org.slf4j.Logger logger = 
org.slf4j.LoggerFactory.getLogger(IndexSelector.class);
+  private static final double COVERING_TO_NONCOVERING_FACTOR = 100.0;
+  private RexNode indexCondition;   // filter condition on indexed columns
+  private RexNode otherRemainderCondition;  // remainder condition on all 
other columns
+  private double totalRows;
+  private Statistics stats;         // a Statistics instance that will be used 
to get estimated rowcount for filter conditions
+  private IndexConditionInfo.Builder builder;
+  private List<IndexProperties> indexPropList;
+  private DrillScanRelBase primaryTableScan;
+  private IndexCallContext indexContext;
+  private RexBuilder rexBuilder;
+
+  public IndexSelector(RexNode indexCondition,
+      RexNode otherRemainderCondition,
+      IndexCallContext indexContext,
+      IndexCollection collection,
+      RexBuilder rexBuilder,
+      double totalRows) {
+    this.indexCondition = indexCondition;
+    this.otherRemainderCondition = otherRemainderCondition;
+    this.indexContext = indexContext;
+    this.totalRows = totalRows;
+    this.stats = indexContext.getGroupScan().getStatistics();
+    this.rexBuilder = rexBuilder;
+    this.builder =
+        IndexConditionInfo.newBuilder(indexCondition, collection, rexBuilder, 
indexContext.getScan());
+    this.primaryTableScan = indexContext.getScan();
+    this.indexPropList = Lists.newArrayList();
+  }
+
+  /**
+   * This constructor is to build selector for no index condition case (no 
filter)
+   * @param indexContext
+   */
+  public IndexSelector(IndexCallContext indexContext) {
+    this.indexCondition = null;
+    this.otherRemainderCondition = null;
+    this.indexContext = indexContext;
+    this.totalRows = Statistics.ROWCOUNT_UNKNOWN;
+    this.stats = indexContext.getGroupScan().getStatistics();
+    this.rexBuilder = indexContext.getScan().getCluster().getRexBuilder();
+    this.builder = null;
+    this.primaryTableScan = indexContext.getScan();
+    this.indexPropList = Lists.newArrayList();
+  }
+
+  public void addIndex(IndexDescriptor indexDesc, boolean isCovering, int 
numProjectedFields) {
+    IndexProperties indexProps = new DrillIndexProperties(indexDesc, 
isCovering, otherRemainderCondition, rexBuilder,
+        numProjectedFields, totalRows, primaryTableScan);
+    indexPropList.add(indexProps);
+  }
+
+  /**
+   * This method analyzes an index's columns and starting from the first 
column, checks
+   * which part of the filter condition matches that column.  This process 
continues with
+   * subsequent columns.  The goal is to identify the portion of the filter 
condition that
+   * match the prefix columns.  If there are additional conditions that don't 
match prefix
+   * columns, that condition is set as a remainder condition.
+   * @param indexProps
+   */
+  public void analyzePrefixMatches(IndexProperties indexProps) {
+    RexNode initCondition = indexCondition.isAlwaysTrue() ? null : 
indexCondition;
+    Map<LogicalExpression, RexNode> leadingPrefixMap = Maps.newHashMap();
+    List<LogicalExpression> indexCols = 
indexProps.getIndexDesc().getIndexColumns();
+    boolean satisfiesCollation = false;
+
+    if (indexCols.size() > 0) {
+      if (initCondition != null) { // check filter condition
+        initCondition = IndexPlanUtils.getLeadingPrefixMap(leadingPrefixMap, 
indexCols, builder, indexCondition);
+      }
+      if (requiredCollation()) {
+        satisfiesCollation = buildAndCheckCollation(indexProps);
+      }
+    }
+
+    indexProps.setProperties(leadingPrefixMap, satisfiesCollation,
+        initCondition /* the remainder condition for indexed columns */, 
stats);
+  }
+
+  private boolean requiredCollation() {
+    if (indexContext.getCollationList() != null && 
indexContext.getCollationList().size() > 0) {
+      return true;
+    }
+    return false;
+  }
+
+  private boolean buildAndCheckCollation(IndexProperties indexProps) {
+    IndexDescriptor indexDesc = indexProps.getIndexDesc();
+    FunctionalIndexInfo functionInfo = indexDesc.getFunctionalInfo();
+
+    RelCollation inputCollation;
+    // for the purpose of collation we can assume that a covering index scan 
would provide
+    // the collation property that would be relevant for non-covering as well
+    ScanPrel indexScanPrel =
+        IndexPlanUtils.buildCoveringIndexScan(indexContext.getScan(), 
indexDesc.getIndexGroupScan(), indexContext, indexDesc);
+    inputCollation = 
indexScanPrel.getTraitSet().getTrait(RelCollationTraitDef.INSTANCE);
+
+    // we don't create collation for Filter because it will inherit the 
child's collation
+
+    if (indexContext.hasLowerProject()) {
+      inputCollation =
+          
IndexPlanUtils.buildCollationProject(indexContext.getLowerProject().getProjects(),
 null,
+              indexContext.getScan(), functionInfo,indexContext);
+    }
+
+    if (indexContext.hasUpperProject()) {
+      inputCollation =
+          
IndexPlanUtils.buildCollationProject(indexContext.getUpperProject().getProjects(),
 indexContext.getLowerProject(),
+              indexContext.getScan(), functionInfo, indexContext);
+    }
+
+    if ( (inputCollation != null) &&
 
 Review comment:
   formatting

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Add capability to do index based planning and execution
> -------------------------------------------------------
>
>                 Key: DRILL-6381
>                 URL: https://issues.apache.org/jira/browse/DRILL-6381
>             Project: Apache Drill
>          Issue Type: New Feature
>          Components: Execution - Relational Operators, Query Planning &amp; 
> Optimization
>            Reporter: Aman Sinha
>            Assignee: Aman Sinha
>            Priority: Major
>             Fix For: 1.15.0
>
>
> If the underlying data source supports indexes (primary and secondary 
> indexes), Drill should leverage those during planning and execution in order 
> to improve query performance.  
> On the planning side, Drill planner should be enhanced to provide an 
> abstraction layer which express the index metadata and statistics.  Further, 
> a cost-based index selection is needed to decide which index(es) are 
> suitable.  
> On the execution side, appropriate operator enhancements would be needed to 
> handle different categories of indexes such as covering, non-covering 
> indexes, taking into consideration the index data may not be co-located with 
> the primary table, i.e a global index.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to