-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-9143] [SQL] Add planner rule for automatically inserting Unsafe <-> Safe row format converters #7482
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
Changes from all commits
9ba3038
b5df19b
d5f9005
0fef0f8
ae2195a
3b11ce3
cabb703
0e2d548
08ce199
6f79449
2bb8da8
5220cce
7450fa5
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 |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| /* | ||
| * 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.execution | ||
|
|
||
| import org.apache.spark.annotation.DeveloperApi | ||
| import org.apache.spark.rdd.RDD | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
|
|
||
| /** | ||
| * :: DeveloperApi :: | ||
| * Converts Java-object-based rows into [[UnsafeRow]]s. | ||
| */ | ||
| @DeveloperApi | ||
| case class ConvertToUnsafe(child: SparkPlan) extends UnaryNode { | ||
| override def output: Seq[Attribute] = child.output | ||
| override def outputsUnsafeRows: Boolean = true | ||
| override def canProcessUnsafeRows: Boolean = false | ||
| override def canProcessSafeRows: Boolean = true | ||
| override protected def doExecute(): RDD[InternalRow] = { | ||
| child.execute().mapPartitions { iter => | ||
| val convertToUnsafe = UnsafeProjection.create(child.schema) | ||
| iter.map(convertToUnsafe) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * :: DeveloperApi :: | ||
| * Converts [[UnsafeRow]]s back into Java-object-based rows. | ||
| */ | ||
| @DeveloperApi | ||
| case class ConvertToSafe(child: SparkPlan) extends UnaryNode { | ||
| override def output: Seq[Attribute] = child.output | ||
| override def outputsUnsafeRows: Boolean = false | ||
| override def canProcessUnsafeRows: Boolean = true | ||
| override def canProcessSafeRows: Boolean = false | ||
| override protected def doExecute(): RDD[InternalRow] = { | ||
| child.execute().mapPartitions { iter => | ||
| val convertToSafe = FromUnsafeProjection(child.output.map(_.dataType)) | ||
| iter.map(convertToSafe) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private[sql] object EnsureRowFormats extends Rule[SparkPlan] { | ||
|
|
||
| private def onlyHandlesSafeRows(operator: SparkPlan): Boolean = | ||
| operator.canProcessSafeRows && !operator.canProcessUnsafeRows | ||
|
|
||
| private def onlyHandlesUnsafeRows(operator: SparkPlan): Boolean = | ||
| operator.canProcessUnsafeRows && !operator.canProcessSafeRows | ||
|
|
||
| private def handlesBothSafeAndUnsafeRows(operator: SparkPlan): Boolean = | ||
| operator.canProcessSafeRows && operator.canProcessUnsafeRows | ||
|
|
||
| override def apply(operator: SparkPlan): SparkPlan = operator.transformUp { | ||
| case operator: SparkPlan if onlyHandlesSafeRows(operator) => | ||
| if (operator.children.exists(_.outputsUnsafeRows)) { | ||
| operator.withNewChildren { | ||
| operator.children.map { | ||
| c => if (c.outputsUnsafeRows) ConvertToSafe(c) else c | ||
| } | ||
| } | ||
| } else { | ||
| operator | ||
| } | ||
| case operator: SparkPlan if onlyHandlesUnsafeRows(operator) => | ||
| if (operator.children.exists(!_.outputsUnsafeRows)) { | ||
| operator.withNewChildren { | ||
| operator.children.map { | ||
| c => if (!c.outputsUnsafeRows) ConvertToUnsafe(c) else c | ||
| } | ||
| } | ||
| } else { | ||
| operator | ||
| } | ||
| case operator: SparkPlan if handlesBothSafeAndUnsafeRows(operator) => | ||
| if (operator.children.map(_.outputsUnsafeRows).toSet.size != 1) { | ||
| // If this operator's children produce both unsafe and safe rows, then convert everything | ||
| // to unsafe rows | ||
| operator.withNewChildren { | ||
|
Contributor
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. wouldn't it make more sense to convert to unsafe instead?
Contributor
Author
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. Yeah, I think so. I think that choosing to resolve this type of conflict in favor of UnsafeRow should be fine: if unsafe operators are disabled via a feature flag, then the plan shouldn't contain any operators which claim to output unsafe rows so this branch will never be triggered. I'll update this patch to change this logic. |
||
| operator.children.map { | ||
| c => if (!c.outputsUnsafeRows) ConvertToUnsafe(c) else c | ||
| } | ||
| } | ||
| } else { | ||
| operator | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * 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.execution | ||
|
|
||
| import org.apache.spark.sql.Row | ||
| import org.apache.spark.sql.catalyst.CatalystTypeConverters | ||
| import org.apache.spark.sql.catalyst.expressions.IsNull | ||
| import org.apache.spark.sql.test.TestSQLContext | ||
|
|
||
| class RowFormatConvertersSuite extends SparkPlanTest { | ||
|
|
||
| private def getConverters(plan: SparkPlan): Seq[SparkPlan] = plan.collect { | ||
| case c: ConvertToUnsafe => c | ||
| case c: ConvertToSafe => c | ||
| } | ||
|
|
||
| private val outputsSafe = ExternalSort(Nil, false, PhysicalRDD(Seq.empty, null)) | ||
| assert(!outputsSafe.outputsUnsafeRows) | ||
| private val outputsUnsafe = UnsafeExternalSort(Nil, false, PhysicalRDD(Seq.empty, null)) | ||
| assert(outputsUnsafe.outputsUnsafeRows) | ||
|
|
||
| test("planner should insert unsafe->safe conversions when required") { | ||
| val plan = Limit(10, outputsUnsafe) | ||
| val preparedPlan = TestSQLContext.prepareForExecution.execute(plan) | ||
| assert(preparedPlan.children.head.isInstanceOf[ConvertToSafe]) | ||
| } | ||
|
|
||
| test("filter can process unsafe rows") { | ||
| val plan = Filter(IsNull(null), outputsUnsafe) | ||
| val preparedPlan = TestSQLContext.prepareForExecution.execute(plan) | ||
| assert(getConverters(preparedPlan).isEmpty) | ||
| assert(preparedPlan.outputsUnsafeRows) | ||
| } | ||
|
|
||
| test("filter can process safe rows") { | ||
| val plan = Filter(IsNull(null), outputsSafe) | ||
| val preparedPlan = TestSQLContext.prepareForExecution.execute(plan) | ||
| assert(getConverters(preparedPlan).isEmpty) | ||
| assert(!preparedPlan.outputsUnsafeRows) | ||
| } | ||
|
|
||
| test("execute() fails an assertion if inputs rows are of different formats") { | ||
| val e = intercept[AssertionError] { | ||
| Union(Seq(outputsSafe, outputsUnsafe)).execute() | ||
| } | ||
| assert(e.getMessage.contains("format")) | ||
| } | ||
|
|
||
| test("union requires all of its input rows' formats to agree") { | ||
| val plan = Union(Seq(outputsSafe, outputsUnsafe)) | ||
| assert(plan.canProcessSafeRows && plan.canProcessUnsafeRows) | ||
| val preparedPlan = TestSQLContext.prepareForExecution.execute(plan) | ||
| assert(preparedPlan.outputsUnsafeRows) | ||
| } | ||
|
|
||
| test("union can process safe rows") { | ||
| val plan = Union(Seq(outputsSafe, outputsSafe)) | ||
| val preparedPlan = TestSQLContext.prepareForExecution.execute(plan) | ||
| assert(!preparedPlan.outputsUnsafeRows) | ||
| } | ||
|
|
||
| test("union can process unsafe rows") { | ||
| val plan = Union(Seq(outputsUnsafe, outputsUnsafe)) | ||
| val preparedPlan = TestSQLContext.prepareForExecution.execute(plan) | ||
| assert(preparedPlan.outputsUnsafeRows) | ||
| } | ||
|
|
||
| test("round trip with ConvertToUnsafe and ConvertToSafe") { | ||
| val input = Seq(("hello", 1), ("world", 2)) | ||
| checkAnswer( | ||
| TestSQLContext.createDataFrame(input), | ||
| plan => ConvertToSafe(ConvertToUnsafe(plan)), | ||
| input.map(Row.fromTuple) | ||
| ) | ||
| } | ||
| } |
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.
What about filter?
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.
Yep, meant to change this. Thanks for reminding me.
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.
Should we also add an assertion to our set operations that the inputs are the same type of row?
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.
or actually, maybe we can add a general assertion to the
executemethod ofSparkPlan?assert(children.map(_.outputsUnsafeRows).distinct <= 1)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.
Yeah, let's add it to
executeI think. I'll do this shortly.