|
| 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 | + |
| 18 | +package org.apache.spark.sql.catalyst.optimizer |
| 19 | + |
| 20 | +import org.apache.spark.api.java.function.FilterFunction |
| 21 | +import org.apache.spark.sql.catalyst.expressions._ |
| 22 | +import org.apache.spark.sql.catalyst.plans.logical._ |
| 23 | +import org.apache.spark.sql.catalyst.rules._ |
| 24 | + |
| 25 | +/* |
| 26 | + * This file defines optimization rules related to object manipulation (for the Dataset API). |
| 27 | + */ |
| 28 | + |
| 29 | +/** |
| 30 | + * Removes cases where we are unnecessarily going between the object and serialized (InternalRow) |
| 31 | + * representation of data item. For example back to back map operations. |
| 32 | + */ |
| 33 | +object EliminateSerialization extends Rule[LogicalPlan] { |
| 34 | + def apply(plan: LogicalPlan): LogicalPlan = plan transform { |
| 35 | + case d @ DeserializeToObject(_, _, s: SerializeFromObject) |
| 36 | + if d.outputObjAttr.dataType == s.inputObjAttr.dataType => |
| 37 | + // Adds an extra Project here, to preserve the output expr id of `DeserializeToObject`. |
| 38 | + // We will remove it later in RemoveAliasOnlyProject rule. |
| 39 | + val objAttr = Alias(s.inputObjAttr, s.inputObjAttr.name)(exprId = d.outputObjAttr.exprId) |
| 40 | + Project(objAttr :: Nil, s.child) |
| 41 | + |
| 42 | + case a @ AppendColumns(_, _, _, _, _, s: SerializeFromObject) |
| 43 | + if a.deserializer.dataType == s.inputObjAttr.dataType => |
| 44 | + AppendColumnsWithObject(a.func, s.serializer, a.serializer, s.child) |
| 45 | + |
| 46 | + // If there is a `SerializeFromObject` under typed filter and its input object type is same with |
| 47 | + // the typed filter's deserializer, we can convert typed filter to normal filter without |
| 48 | + // deserialization in condition, and push it down through `SerializeFromObject`. |
| 49 | + // e.g. `ds.map(...).filter(...)` can be optimized by this rule to save extra deserialization, |
| 50 | + // but `ds.map(...).as[AnotherType].filter(...)` can not be optimized. |
| 51 | + case f @ TypedFilter(_, _, _, _, s: SerializeFromObject) |
| 52 | + if f.deserializer.dataType == s.inputObjAttr.dataType => |
| 53 | + s.copy(child = f.withObjectProducerChild(s.child)) |
| 54 | + |
| 55 | + // If there is a `DeserializeToObject` upon typed filter and its output object type is same with |
| 56 | + // the typed filter's deserializer, we can convert typed filter to normal filter without |
| 57 | + // deserialization in condition, and pull it up through `DeserializeToObject`. |
| 58 | + // e.g. `ds.filter(...).map(...)` can be optimized by this rule to save extra deserialization, |
| 59 | + // but `ds.filter(...).as[AnotherType].map(...)` can not be optimized. |
| 60 | + case d @ DeserializeToObject(_, _, f: TypedFilter) |
| 61 | + if d.outputObjAttr.dataType == f.deserializer.dataType => |
| 62 | + f.withObjectProducerChild(d.copy(child = f.child)) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Combines two adjacent [[TypedFilter]]s, which operate on same type object in condition, into one, |
| 68 | + * mering the filter functions into one conjunctive function. |
| 69 | + */ |
| 70 | +object CombineTypedFilters extends Rule[LogicalPlan] { |
| 71 | + def apply(plan: LogicalPlan): LogicalPlan = plan transform { |
| 72 | + case t1 @ TypedFilter(_, _, _, _, t2 @ TypedFilter(_, _, _, _, child)) |
| 73 | + if t1.deserializer.dataType == t2.deserializer.dataType => |
| 74 | + TypedFilter( |
| 75 | + combineFilterFunction(t2.func, t1.func), |
| 76 | + t1.argumentClass, |
| 77 | + t1.argumentSchema, |
| 78 | + t1.deserializer, |
| 79 | + child) |
| 80 | + } |
| 81 | + |
| 82 | + private def combineFilterFunction(func1: AnyRef, func2: AnyRef): Any => Boolean = { |
| 83 | + (func1, func2) match { |
| 84 | + case (f1: FilterFunction[_], f2: FilterFunction[_]) => |
| 85 | + input => f1.asInstanceOf[FilterFunction[Any]].call(input) && |
| 86 | + f2.asInstanceOf[FilterFunction[Any]].call(input) |
| 87 | + case (f1: FilterFunction[_], f2) => |
| 88 | + input => f1.asInstanceOf[FilterFunction[Any]].call(input) && |
| 89 | + f2.asInstanceOf[Any => Boolean](input) |
| 90 | + case (f1, f2: FilterFunction[_]) => |
| 91 | + input => f1.asInstanceOf[Any => Boolean].apply(input) && |
| 92 | + f2.asInstanceOf[FilterFunction[Any]].call(input) |
| 93 | + case (f1, f2) => |
| 94 | + input => f1.asInstanceOf[Any => Boolean].apply(input) && |
| 95 | + f2.asInstanceOf[Any => Boolean].apply(input) |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments