amansinha100 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_r228349995
########## File path: contrib/format-maprdb/src/main/java/org/apache/drill/exec/store/mapr/db/MapRDBPushProjectIntoScan.java ########## @@ -0,0 +1,145 @@ +/* + * 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.store.mapr.db; + +import com.google.common.collect.Lists; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.RelOptRuleOperand; +import org.apache.calcite.plan.RelTrait; +import org.apache.calcite.plan.RelTraitSet; +import org.apache.calcite.rel.RelCollation; +import org.apache.calcite.rel.rules.ProjectRemoveRule; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeField; +import org.apache.calcite.rex.RexNode; +import org.apache.drill.common.exceptions.DrillRuntimeException; +import org.apache.drill.exec.planner.common.DrillRelOptUtil; +import org.apache.drill.exec.planner.logical.RelOptHelper; +import org.apache.drill.exec.planner.common.DrillRelOptUtil; +import org.apache.drill.exec.planner.common.DrillRelOptUtil.ProjectPushInfo; +import org.apache.drill.exec.planner.physical.Prel; +import org.apache.drill.exec.planner.physical.ProjectPrel; +import org.apache.drill.exec.planner.physical.ScanPrel; +import org.apache.drill.exec.store.StoragePluginOptimizerRule; +import org.apache.drill.exec.store.mapr.db.binary.BinaryTableGroupScan; +import org.apache.drill.exec.store.mapr.db.json.JsonTableGroupScan; +import org.apache.drill.exec.util.Utilities; + +import java.util.List; + +public abstract class MapRDBPushProjectIntoScan extends StoragePluginOptimizerRule { + static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(MapRDBPushProjectIntoScan.class); + + private MapRDBPushProjectIntoScan(RelOptRuleOperand operand, String description) { + super(operand, description); + } + + public static final StoragePluginOptimizerRule PROJECT_ON_SCAN = new MapRDBPushProjectIntoScan( + RelOptHelper.some(ProjectPrel.class, RelOptHelper.any(ScanPrel.class)), "MapRDBPushProjIntoScan:Proj_On_Scan") { + @Override + public void onMatch(RelOptRuleCall call) { + final ScanPrel scan = (ScanPrel) call.rel(1); + final ProjectPrel project = (ProjectPrel) call.rel(0); + if (!(scan.getGroupScan() instanceof MapRDBGroupScan)) { + return; + } + doPushProjectIntoGroupScan(call, project, scan, (MapRDBGroupScan) scan.getGroupScan()); + if (scan.getGroupScan() instanceof BinaryTableGroupScan) { + BinaryTableGroupScan groupScan = (BinaryTableGroupScan) scan.getGroupScan(); + + } else { + assert (scan.getGroupScan() instanceof JsonTableGroupScan); + JsonTableGroupScan groupScan = (JsonTableGroupScan) scan.getGroupScan(); + + doPushProjectIntoGroupScan(call, project, scan, groupScan); + } + } + + @Override + public boolean matches(RelOptRuleCall call) { + final ScanPrel scan = (ScanPrel) call.rel(1); + if (scan.getGroupScan() instanceof BinaryTableGroupScan || + scan.getGroupScan() instanceof JsonTableGroupScan) { + return super.matches(call); + } + return false; + } + }; + + protected void doPushProjectIntoGroupScan(RelOptRuleCall call, + ProjectPrel project, ScanPrel scan, MapRDBGroupScan groupScan) { + try { + + DrillRelOptUtil.ProjectPushInfo columnInfo = + DrillRelOptUtil.getFieldsInformation(scan.getRowType(), project.getProjects()); + if (columnInfo == null || Utilities.isStarQuery(columnInfo.getFields()) // + || !groupScan.canPushdownProjects(columnInfo.getFields())) { + return; + } + RelTraitSet newTraits = call.getPlanner().emptyTraitSet(); + // Clear out collation trait + for (RelTrait trait : scan.getTraitSet()) { + if (!(trait instanceof RelCollation)) { + newTraits.plus(trait); + } + } + final ScanPrel newScan = new ScanPrel(scan.getCluster(), newTraits.plus(Prel.DRILL_PHYSICAL), + groupScan.clone(columnInfo.getFields()), + columnInfo.createNewRowType(project.getInput().getCluster().getTypeFactory()), scan.getTable()); + + List<RexNode> newProjects = Lists.newArrayList(); + for (RexNode n : project.getChildExps()) { + newProjects.add(n.accept(columnInfo.getInputReWriter())); + } + + final ProjectPrel newProj = + new ProjectPrel(project.getCluster(), + project.getTraitSet().plus(Prel.DRILL_PHYSICAL), + newScan, + newProjects, + project.getRowType()); + + if (ProjectRemoveRule.isTrivial(newProj) && + // the old project did not involve any column renaming + sameRowTypeProjectionsFields(project.getRowType(), newScan.getRowType())) { Review comment: This may have to do with the fact that `MapRDBPushProjectIntoScan` is applied during physical planning unlike the `DrillPushProjectIntoScanRule` so there may be some differences in how the pushdown occurs. We would have added the extra check `sameRowTypeProjectionsFields` to prevent an incorrect pushdown, although I don't recall exact details now. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
