-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-20254][SQL] Remove unnecessary data conversion for Dataset with primitive array #17568
Changes from 15 commits
1ece803
c042ff2
264a18b
82a1f2b
cc3c7bd
7d43d27
3bcee3f
1b24be0
5174b5e
e608c45
2ee4030
1d6ab36
0fd8c25
791aad9
f695e50
ce6927d
a6bedee
c0dca2b
8de6915
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ import org.apache.spark.sql.catalyst.analysis._ | |
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.catalyst.expressions.aggregate._ | ||
import org.apache.spark.sql.catalyst.expressions.Literal.{FalseLiteral, TrueLiteral} | ||
import org.apache.spark.sql.catalyst.expressions.objects.AssertNotNull | ||
import org.apache.spark.sql.catalyst.plans._ | ||
import org.apache.spark.sql.catalyst.plans.logical._ | ||
import org.apache.spark.sql.catalyst.rules._ | ||
|
@@ -368,6 +369,8 @@ case class NullPropagation(conf: SQLConf) extends Rule[LogicalPlan] { | |
case EqualNullSafe(Literal(null, _), r) => IsNull(r) | ||
case EqualNullSafe(l, Literal(null, _)) => IsNull(l) | ||
|
||
case AssertNotNull(c, _) if !c.nullable => c | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this safe to do? According to the description of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that this is what @cloud-fan suggested in his comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if @cloud-fan's no-op There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah good catch! sorry it was my mistake, but then seems we can not remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually, I checked all the usage of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with @cloud-fan. I have also checked the usages of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the purpose of Maybe for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as long as we trust the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. Sounds reasonable to me. |
||
|
||
// For Coalesce, remove null literals. | ||
case e @ Coalesce(children) => | ||
val newChildren = children.filterNot(isNullLiteral) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,10 @@ package org.apache.spark.sql.catalyst.optimizer | |
|
||
import org.apache.spark.api.java.function.FilterFunction | ||
import org.apache.spark.sql.catalyst.expressions._ | ||
import org.apache.spark.sql.catalyst.expressions.objects._ | ||
import org.apache.spark.sql.catalyst.plans.logical._ | ||
import org.apache.spark.sql.catalyst.rules._ | ||
import org.apache.spark.sql.types._ | ||
|
||
/* | ||
* This file defines optimization rules related to object manipulation (for the Dataset API). | ||
|
@@ -96,3 +98,22 @@ object CombineTypedFilters extends Rule[LogicalPlan] { | |
} | ||
} | ||
} | ||
|
||
/** | ||
* Removes MapObjects when the following conditions are satisfied | ||
* 1. Mapobject(e) where e is lambdavariable(), which means types for input output | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is obscure. Can we improve it a bit? For example, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, done |
||
* are primitive types | ||
* 2. no custom collection class specified | ||
* representation of data item. For example back to back map operations. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we rephrase this comment too? It looks weird. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, deleted |
||
*/ | ||
object EliminateMapObjects extends Rule[LogicalPlan] { | ||
def apply(plan: LogicalPlan): LogicalPlan = plan transform { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, it works. done |
||
case DeserializeToObject(Invoke( | ||
MapObjects(_, _, _, _ : LambdaVariable, inputData, None), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we just replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cloud-fan, I misunderstood. Both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cloud-fan Unfortunately, this change caused two test failures. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When we run the following code, the following exception occurs. This is because
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The solution is to use the following matching:
Previous one avoided the case such as |
||
funcName, returnType: ObjectType, arguments, propagateNull, returnNullable), | ||
outputObjAttr, child) => | ||
DeserializeToObject(Invoke( | ||
inputData, funcName, returnType, arguments, propagateNull, returnNullable), | ||
outputObjAttr, child) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* 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.spark.sql.catalyst.optimizer | ||
|
||
import org.apache.spark.sql.catalyst.dsl.expressions._ | ||
import org.apache.spark.sql.catalyst.dsl.plans._ | ||
import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder | ||
import org.apache.spark.sql.catalyst.expressions.AttributeReference | ||
import org.apache.spark.sql.catalyst.expressions.objects.Invoke | ||
import org.apache.spark.sql.catalyst.plans.PlanTest | ||
import org.apache.spark.sql.catalyst.plans.logical.{DeserializeToObject, LocalRelation, LogicalPlan} | ||
import org.apache.spark.sql.catalyst.rules.RuleExecutor | ||
import org.apache.spark.sql.types._ | ||
|
||
class EliminateMapObjectsSuite extends PlanTest { | ||
object Optimize extends RuleExecutor[LogicalPlan] { | ||
val batches = { | ||
Batch("EliminateMapObjects", FixedPoint(50), | ||
NullPropagation(conf), | ||
SimplifyCasts, | ||
EliminateMapObjects) :: Nil | ||
} | ||
} | ||
|
||
implicit private def intArrayEncoder = ExpressionEncoder[Array[Int]]() | ||
implicit private def doubleArrayEncoder = ExpressionEncoder[Array[Double]]() | ||
|
||
test("SPARK-20254: Remove unnecessary data conversion for primitive array") { | ||
val intObjType = ObjectType(classOf[Array[Int]]) | ||
val intInput = LocalRelation('a.array(ArrayType(IntegerType, false))) | ||
val intQuery = intInput.deserialize[Array[Int]].analyze | ||
val intOptimized = Optimize.execute(intQuery) | ||
val intExpected = DeserializeToObject( | ||
Invoke(intInput.output(0), "toIntArray", intObjType, Nil, true, false), | ||
AttributeReference("obj", intObjType, true)(), intInput) | ||
comparePlans(intOptimized, intExpected) | ||
|
||
val doubleObjType = ObjectType(classOf[Array[Double]]) | ||
val doubleInput = LocalRelation('a.array(ArrayType(DoubleType, false))) | ||
val doubleQuery = doubleInput.deserialize[Array[Double]].analyze | ||
val doubleOptimized = Optimize.execute(doubleQuery) | ||
val doubleExpected = DeserializeToObject( | ||
Invoke(doubleInput.output(0), "toDoubleArray", doubleObjType, Nil, true, false), | ||
AttributeReference("obj", doubleObjType, true)(), doubleInput) | ||
comparePlans(doubleOptimized, doubleExpected) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
@param
?