|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * 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, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.gluten.extension |
| 18 | + |
| 19 | +import org.apache.gluten.execution.{FilterExecTransformer, ProjectExecTransformer} |
| 20 | +import org.apache.gluten.expression.{CHCollapsedExpression, ExpressionMappings} |
| 21 | + |
| 22 | +import org.apache.spark.sql.SparkSession |
| 23 | +import org.apache.spark.sql.catalyst.expressions._ |
| 24 | +import org.apache.spark.sql.catalyst.rules.Rule |
| 25 | +import org.apache.spark.sql.execution.SparkPlan |
| 26 | +import org.apache.spark.sql.types.DataType |
| 27 | + |
| 28 | +/** |
| 29 | + * Collapse nested expressions for optimization, to reduce expression calls. Now support `and`, |
| 30 | + * `or`. e.g. select ... and(and(a=1, b=2), c=3) => select ... and(a=1, b=2, c=3). |
| 31 | + */ |
| 32 | +case class CollapseNestedExpressions(spark: SparkSession) extends Rule[SparkPlan] { |
| 33 | + |
| 34 | + override def apply(plan: SparkPlan): SparkPlan = { |
| 35 | + if (canBeOptimized(plan)) { |
| 36 | + visitPlan(plan) |
| 37 | + } else { |
| 38 | + plan |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private def canBeOptimized(plan: SparkPlan): Boolean = plan match { |
| 43 | + case p: ProjectExecTransformer => |
| 44 | + var res = p.projectList.exists(c => c.isInstanceOf[And] || c.isInstanceOf[Or]) |
| 45 | + if (res) { |
| 46 | + return false |
| 47 | + } |
| 48 | + res = p.projectList.exists(c => canBeOptimized(c)) |
| 49 | + if (!res) { |
| 50 | + res = p.children.exists(c => canBeOptimized(c)) |
| 51 | + } |
| 52 | + res |
| 53 | + case f: FilterExecTransformer => |
| 54 | + var res = canBeOptimized(f.condition) |
| 55 | + if (!res) { |
| 56 | + res = canBeOptimized(f.child) |
| 57 | + } |
| 58 | + res |
| 59 | + case _ => plan.children.exists(c => canBeOptimized(c)) |
| 60 | + } |
| 61 | + |
| 62 | + private def canBeOptimized(expr: Expression): Boolean = { |
| 63 | + var exprCall = expr |
| 64 | + expr match { |
| 65 | + case a: Alias => exprCall = a.child |
| 66 | + case _ => |
| 67 | + } |
| 68 | + val exprName = getExpressionName(exprCall) |
| 69 | + exprName match { |
| 70 | + case None => |
| 71 | + exprCall match { |
| 72 | + case _: LeafExpression => false |
| 73 | + case _ => exprCall.children.exists(c => canBeOptimized(c)) |
| 74 | + } |
| 75 | + case Some(f) => |
| 76 | + CHCollapsedExpression.supported(f) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private def getExpressionName(expr: Expression): Option[String] = expr match { |
| 81 | + case _: And => ExpressionMappings.expressionsMap.get(classOf[And]) |
| 82 | + case _: Or => ExpressionMappings.expressionsMap.get(classOf[Or]) |
| 83 | + case _ => Option.empty[String] |
| 84 | + } |
| 85 | + |
| 86 | + private def visitPlan(plan: SparkPlan): SparkPlan = plan match { |
| 87 | + case p: ProjectExecTransformer => |
| 88 | + var newProjectList = Seq.empty[NamedExpression] |
| 89 | + p.projectList.foreach { |
| 90 | + case a: Alias => |
| 91 | + val newAlias = Alias(optimize(a.child), a.name)(a.exprId) |
| 92 | + newProjectList :+= newAlias |
| 93 | + case p => |
| 94 | + newProjectList :+= p |
| 95 | + } |
| 96 | + val newChild = visitPlan(p.child) |
| 97 | + ProjectExecTransformer(newProjectList, newChild) |
| 98 | + case f: FilterExecTransformer => |
| 99 | + val newCondition = optimize(f.condition) |
| 100 | + val newChild = visitPlan(f.child) |
| 101 | + FilterExecTransformer(newCondition, newChild) |
| 102 | + case _ => |
| 103 | + val newChildren = plan.children.map(p => visitPlan(p)) |
| 104 | + plan.withNewChildren(newChildren) |
| 105 | + } |
| 106 | + |
| 107 | + private def optimize(expr: Expression): Expression = { |
| 108 | + var resultExpr = expr |
| 109 | + var name = getExpressionName(expr) |
| 110 | + var children = Seq.empty[Expression] |
| 111 | + var dataType = null.asInstanceOf[DataType] |
| 112 | + |
| 113 | + def f(e: Expression, parent: Option[Expression] = Option.empty[Expression]): Unit = { |
| 114 | + parent match { |
| 115 | + case None => |
| 116 | + name = getExpressionName(e) |
| 117 | + dataType = e.dataType |
| 118 | + case _ => |
| 119 | + } |
| 120 | + e match { |
| 121 | + case a: And if canBeOptimized(a) => |
| 122 | + parent match { |
| 123 | + case Some(_: And) | None => |
| 124 | + f(a.left, Option.apply(a)) |
| 125 | + f(a.right, Option.apply(a)) |
| 126 | + case _ => |
| 127 | + children +:= optimize(a) |
| 128 | + } |
| 129 | + case o: Or if canBeOptimized(o) => |
| 130 | + parent match { |
| 131 | + case Some(_: Or) | None => |
| 132 | + f(o.left, parent = Option.apply(o)) |
| 133 | + f(o.right, parent = Option.apply(o)) |
| 134 | + case _ => |
| 135 | + children +:= optimize(o) |
| 136 | + } |
| 137 | + case _ => |
| 138 | + if (parent.nonEmpty) { |
| 139 | + children +:= optimize(e) |
| 140 | + } else { |
| 141 | + children = Seq.empty[Expression] |
| 142 | + val exprNewChildren = e.children.map(p => optimize(p)) |
| 143 | + resultExpr = e.withNewChildren(exprNewChildren) |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + f(expr) |
| 148 | + if (name.isDefined || collapsedExpressionExists(children)) { |
| 149 | + CHCollapsedExpression(dataType, children, name.getOrElse(""), expr.nullable, expr) |
| 150 | + } else { |
| 151 | + resultExpr |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private def collapsedExpressionExists(children: Seq[Expression]): Boolean = { |
| 156 | + var res = false |
| 157 | + children.foreach { |
| 158 | + case _: CHCollapsedExpression if !res => res = true |
| 159 | + case c if !res => res = collapsedExpressionExists(c.children) |
| 160 | + case _ => |
| 161 | + } |
| 162 | + res |
| 163 | + } |
| 164 | +} |
0 commit comments