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_r223677718
########## File path: exec/java-exec/src/main/java/org/apache/drill/exec/planner/index/SimpleRexRemap.java ########## @@ -0,0 +1,300 @@ +/* + * 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 org.apache.drill.shaded.guava.com.google.common.collect.ImmutableMap; +import org.apache.drill.shaded.guava.com.google.common.collect.Maps; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeField; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexCall; +import org.apache.calcite.rex.RexCorrelVariable; +import org.apache.calcite.rex.RexDynamicParam; +import org.apache.calcite.rex.RexFieldAccess; + +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexLiteral; +import org.apache.calcite.rex.RexLocalRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexOver; +import org.apache.calcite.rex.RexRangeRef; +import org.apache.calcite.rex.RexShuttle; +import org.apache.calcite.rex.RexVisitorImpl; + +import org.apache.calcite.sql.fun.SqlStdOperatorTable; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.calcite.util.NlsString; +import org.apache.drill.common.expression.LogicalExpression; +import org.apache.drill.common.expression.PathSegment; + +import java.util.List; +import java.util.Map; + +/** + * Rewrite RexNode with these policies: + * 1) field renamed. The input field was named differently in index table, + * 2) field is in different position of underlying rowtype + * + * TODO: 3) certain operator needs rewriting. e.g. CAST function + * This class for now applies to only filter on scan, for filter-on-project-on-scan. A stack of + * rowType is required. + */ +public class SimpleRexRemap { + final RelNode origRel; + final RelDataType origRowType; + final RelDataType newRowType; + + private RexBuilder builder; + private Map<LogicalExpression, LogicalExpression> destExprMap; + + public SimpleRexRemap(RelNode origRel, + RelDataType newRowType, RexBuilder builder) { + super(); + this.origRel = origRel; + this.origRowType = origRel.getRowType(); + this.newRowType = newRowType; + this.builder = builder; + this.destExprMap = Maps.newHashMap(); + } + + /** + * Set the map of src expression to target expression, expressions not in the map do not have assigned destinations + * @param exprMap + * @return + */ + public SimpleRexRemap setExpressionMap(Map<LogicalExpression, LogicalExpression> exprMap) { + destExprMap.putAll(exprMap); + return this; + } + + public RexNode rewriteEqualOnCharToLike(RexNode expr, + Map<RexNode, LogicalExpression> equalOnCastCharExprs) { + Map<RexNode, RexNode> srcToReplace = Maps.newIdentityHashMap(); + for(Map.Entry<RexNode, LogicalExpression> entry: equalOnCastCharExprs.entrySet()) { + RexNode equalOp = entry.getKey(); + LogicalExpression opInput = entry.getValue(); + + final List<RexNode> operands = ((RexCall)equalOp).getOperands(); + RexLiteral newLiteral = null; + RexNode input = null; + if(operands.size() == 2 ) { + RexLiteral oplit = null; + if (operands.get(0) instanceof RexLiteral) { + oplit = (RexLiteral) operands.get(0); + if(oplit.getTypeName() == SqlTypeName.CHAR) { + newLiteral = builder.makeLiteral(((NlsString) oplit.getValue()).getValue() + "%"); + input = operands.get(1); + } + } + else if (operands.get(1) instanceof RexLiteral) { + oplit = (RexLiteral) operands.get(1); + if(oplit.getTypeName() == SqlTypeName.CHAR) { Review comment: space ---------------------------------------------------------------- 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 With regards, Apache Git Services