|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.optimizer; |
| 19 | + |
| 20 | +import com.google.common.collect.Lists; |
| 21 | +import org.apache.doris.analysis.ArithmeticExpr; |
| 22 | +import org.apache.doris.analysis.BinaryPredicate; |
| 23 | +import org.apache.doris.analysis.CaseExpr; |
| 24 | +import org.apache.doris.analysis.CaseWhenClause; |
| 25 | +import org.apache.doris.analysis.CastExpr; |
| 26 | +import org.apache.doris.analysis.CompoundPredicate; |
| 27 | +import org.apache.doris.analysis.Expr; |
| 28 | +import org.apache.doris.analysis.FunctionCallExpr; |
| 29 | +import org.apache.doris.analysis.InPredicate; |
| 30 | +import org.apache.doris.analysis.IsNullPredicate; |
| 31 | +import org.apache.doris.analysis.LikePredicate; |
| 32 | +import org.apache.doris.analysis.SlotDescriptor; |
| 33 | +import org.apache.doris.analysis.SlotRef; |
| 34 | +import org.apache.doris.common.UserException; |
| 35 | +import org.apache.doris.optimizer.operator.OptItemArithmetic; |
| 36 | +import org.apache.doris.optimizer.operator.OptItemBinaryPredicate; |
| 37 | +import org.apache.doris.optimizer.operator.OptItemCase; |
| 38 | +import org.apache.doris.optimizer.operator.OptItemCast; |
| 39 | +import org.apache.doris.optimizer.operator.OptItemColumnRef; |
| 40 | +import org.apache.doris.optimizer.operator.OptItemCompoundPredicate; |
| 41 | +import org.apache.doris.optimizer.operator.OptItemConst; |
| 42 | +import org.apache.doris.optimizer.operator.OptItemFunctionCall; |
| 43 | +import org.apache.doris.optimizer.operator.OptItemInPredicate; |
| 44 | +import org.apache.doris.optimizer.operator.OptItemIsNullPredicate; |
| 45 | +import org.apache.doris.optimizer.operator.OptItemLikePredicate; |
| 46 | +import org.apache.doris.optimizer.operator.OptOperator; |
| 47 | +import org.apache.doris.optimizer.operator.OptPhysicalMysqlScan; |
| 48 | +import org.apache.doris.planner.MysqlScanNode; |
| 49 | +import org.apache.doris.planner.PlanNode; |
| 50 | +import org.apache.logging.log4j.LogManager; |
| 51 | +import org.apache.logging.log4j.Logger; |
| 52 | + |
| 53 | +import java.util.List; |
| 54 | + |
| 55 | +// Convert expression to statement, used to convert optimized expression |
| 56 | +// to a plan tree |
| 57 | +public class ExpressionToStmtConverter { |
| 58 | + private static final Logger LOG = LogManager.getLogger(ExpressionToStmtConverter.class); |
| 59 | + |
| 60 | + private OptConverterCtx ctx; |
| 61 | + |
| 62 | + public ExpressionToStmtConverter(OptConverterCtx ctx) { |
| 63 | + this.ctx = ctx; |
| 64 | + } |
| 65 | + |
| 66 | + public PlanNode convertPhysical(OptExpression expr) { |
| 67 | + OptOperator op = expr.getOp(); |
| 68 | + switch (op.getType()) { |
| 69 | + case OP_PHYSICAL_MYSQL_SCAN: |
| 70 | + return convertMysqlScan(expr) |
| 71 | + } |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + public MysqlScanNode convertMysqlScan(OptExpression expr) { |
| 76 | + OptPhysicalMysqlScan scanOp = (OptPhysicalMysqlScan) expr.getOp(); |
| 77 | + MysqlScanNode node = new MysqlScanNode() |
| 78 | + return null; |
| 79 | + } |
| 80 | + |
| 81 | + public Expr convertItem(OptExpression expr) throws UserException { |
| 82 | + OptOperator op = expr.getOp(); |
| 83 | + switch (op.getType()) { |
| 84 | + case OP_ITEM_BINARY_PREDICATE: |
| 85 | + return convertBinaryPred(expr); |
| 86 | + case OP_ITEM_COMPOUND_PREDICATE: |
| 87 | + return convertCompoundPred(expr); |
| 88 | + case OP_ITEM_CONST: |
| 89 | + return convertConst(expr); |
| 90 | + case OP_ITEM_ARITHMETIC: |
| 91 | + return convertArithmetic(expr); |
| 92 | + case OP_ITEM_CASE: |
| 93 | + return convertCase(expr); |
| 94 | + case OP_ITEM_CAST: |
| 95 | + return convertCast(expr); |
| 96 | + case OP_ITEM_FUNCTION_CALL: |
| 97 | + return convertFunctionCall(expr); |
| 98 | + case OP_ITEM_AGG_FUNC: |
| 99 | + return convertAggregateFunction(expr); |
| 100 | + case OP_ITEM_COLUMN_REF: |
| 101 | + return convertColumnRef(expr); |
| 102 | + case OP_ITEM_IN_PREDICATE: |
| 103 | + return convertInPred(expr); |
| 104 | + case OP_ITEM_LIKE_PREDICATE: |
| 105 | + return convertLikePred(expr); |
| 106 | + case OP_ITEM_IS_NULL_PREDICATE: |
| 107 | + return convertIsNullPred(expr); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private Expr convertConst(OptExpression expr) { |
| 112 | + return ((OptItemConst) expr.getOp()).getLiteral(); |
| 113 | + } |
| 114 | + |
| 115 | + private Expr convertIsNullPred(OptExpression expr) throws UserException { |
| 116 | + Expr child = convertItem(expr.getInput(0)); |
| 117 | + OptItemIsNullPredicate pred = (OptItemIsNullPredicate) expr.getOp(); |
| 118 | + return new IsNullPredicate(child, pred.isNotNull()); |
| 119 | + } |
| 120 | + |
| 121 | + private Expr convertLikePred(OptExpression expr) throws UserException { |
| 122 | + Expr leftChild = convertItem(expr.getInput(0)); |
| 123 | + Expr rightChild = convertItem(expr.getInput(0)); |
| 124 | + OptItemLikePredicate pred = (OptItemLikePredicate) expr.getOp(); |
| 125 | + return new LikePredicate(pred.getOp(), leftChild, rightChild); |
| 126 | + } |
| 127 | + |
| 128 | + private Expr convertInPred(OptExpression expr) throws UserException { |
| 129 | + Expr compareChild = convertItem(expr.getInput(0)); |
| 130 | + |
| 131 | + List<Expr> children = Lists.newArrayList(); |
| 132 | + for (int i = 1; i < expr.getInputs().size(); ++i) { |
| 133 | + children.add(convertItem(expr.getInput(i))); |
| 134 | + } |
| 135 | + |
| 136 | + OptItemInPredicate pred = (OptItemInPredicate) expr.getOp(); |
| 137 | + return new InPredicate(compareChild, children, pred.isNotIn()); |
| 138 | + } |
| 139 | + |
| 140 | + private Expr convertColumnRef(OptExpression expr) throws UserException { |
| 141 | + OptItemColumnRef columnRef = (OptItemColumnRef) expr.getOp(); |
| 142 | + SlotDescriptor slotDesc = ctx.getSlotRef(columnRef.getRef().getId()); |
| 143 | + if (slotDesc == null) { |
| 144 | + LOG.warn("fail to get SlotRef with ColumnRef, columnRef={}", columnRef.getRef()); |
| 145 | + throw new UserException("Unknown column reference"); |
| 146 | + } |
| 147 | + return new SlotRef(slotDesc); |
| 148 | + } |
| 149 | + |
| 150 | + private Expr convertAggregateFunction(OptExpression expr) { |
| 151 | + return null; |
| 152 | + } |
| 153 | + |
| 154 | + private Expr convertCast(OptExpression expr) throws UserException { |
| 155 | + Expr child = convertItem(expr.getInput(0)); |
| 156 | + OptItemCast op = (OptItemCast) expr.getOp(); |
| 157 | + return new CastExpr(op.getDestType(), child); |
| 158 | + } |
| 159 | + |
| 160 | + private Expr convertCase(OptExpression expr) throws UserException { |
| 161 | + OptItemCase caseOp = (OptItemCase) expr.getOp(); |
| 162 | + Expr caseExpr = null; |
| 163 | + int idx = 0; |
| 164 | + if (caseOp.hasCase()) { |
| 165 | + caseExpr = convertItem(expr.getInput(idx)); |
| 166 | + idx++; |
| 167 | + } |
| 168 | + int endIdx = expr.getInputs().size(); |
| 169 | + Expr elseExpr = null; |
| 170 | + if (caseOp.hasElse()) { |
| 171 | + endIdx--; |
| 172 | + elseExpr = convertItem(expr.getInput(endIdx)); |
| 173 | + } |
| 174 | + if ((endIdx - idx) % 2 != 0) { |
| 175 | + throw new UserException(""); |
| 176 | + } |
| 177 | + List<CaseWhenClause> whenThenList = Lists.newArrayList(); |
| 178 | + for (; idx < endIdx; idx += 2) { |
| 179 | + Expr whenExpr = convertItem(expr.getInput(idx)); |
| 180 | + Expr thenExpr = convertItem(expr.getInput(idx + 1)); |
| 181 | + whenThenList.add(new CaseWhenClause(whenExpr, thenExpr)); |
| 182 | + } |
| 183 | + |
| 184 | + return new CaseExpr(caseExpr, whenThenList, elseExpr); |
| 185 | + } |
| 186 | + |
| 187 | + private Expr convertArithmetic(OptExpression expr) throws UserException { |
| 188 | + Expr leftChild = convertItem(expr.getInput(0)); |
| 189 | + Expr rightChild = null; |
| 190 | + if (expr.getInputs().size() == 2) { |
| 191 | + rightChild = convertItem(expr.getInput(1)); |
| 192 | + } |
| 193 | + OptItemArithmetic item = (OptItemArithmetic) expr.getOp(); |
| 194 | + return new ArithmeticExpr(item.getOp(), leftChild, rightChild); |
| 195 | + } |
| 196 | + |
| 197 | + private Expr convertBinaryPred(OptExpression expr) throws UserException { |
| 198 | + Expr leftChild = convertItem(expr.getInput(0)); |
| 199 | + Expr rightChild = convertItem(expr.getInput(1)); |
| 200 | + |
| 201 | + OptItemBinaryPredicate pred = (OptItemBinaryPredicate) expr.getOp(); |
| 202 | + return new BinaryPredicate(pred.getOp(), leftChild, rightChild); |
| 203 | + } |
| 204 | + |
| 205 | + private Expr convertCompoundPred(OptExpression expr) throws UserException { |
| 206 | + OptItemCompoundPredicate pred = (OptItemCompoundPredicate) expr.getOp(); |
| 207 | + Expr leftChild = convertItem(expr.getInput(0)); |
| 208 | + Expr rightChild = null; |
| 209 | + if (pred.getOp() != CompoundPredicate.Operator.NOT) { |
| 210 | + rightChild = convertItem(expr.getInput(1)); |
| 211 | + } |
| 212 | + return new CompoundPredicate(pred.getOp(), leftChild, rightChild); |
| 213 | + } |
| 214 | + |
| 215 | + private Expr convertFunctionCall(OptExpression expr) throws UserException { |
| 216 | + OptItemFunctionCall funcOp = (OptItemFunctionCall) expr.getOp(); |
| 217 | + |
| 218 | + List<Expr> children = Lists.newArrayList(); |
| 219 | + for (OptExpression input : expr.getInputs()) { |
| 220 | + children.add(convertItem(input)); |
| 221 | + } |
| 222 | + |
| 223 | + return new FunctionCallExpr(funcOp.getName(), children); |
| 224 | + } |
| 225 | +} |
0 commit comments