Skip to content

Commit 5925ad4

Browse files
committed
[SPARK-9949] [SQL] Fix TakeOrderedAndProject's output.
https://issues.apache.org/jira/browse/SPARK-9949 Author: Yin Huai <[email protected]> Closes #8179 from yhuai/SPARK-9949. (cherry picked from commit 932b24f) Signed-off-by: Reynold Xin <[email protected]> Conflicts: sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala
1 parent c5070b1 commit 5925ad4

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

sql/core/src/main/scala/org/apache/spark/sql/execution/basicOperators.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ case class TakeOrderedAndProject(
157157
projectList: Option[Seq[NamedExpression]],
158158
child: SparkPlan) extends UnaryNode {
159159

160-
override def output: Seq[Attribute] = child.output
160+
override def output: Seq[Attribute] = {
161+
val projectOutput = projectList.map(_.map(_.toAttribute))
162+
projectOutput.getOrElse(child.output)
163+
}
161164

162165
override def outputPartitioning: Partitioning = SinglePartition
163166

@@ -181,6 +184,13 @@ case class TakeOrderedAndProject(
181184
protected override def doExecute(): RDD[Row] = sparkContext.makeRDD(collectData(), 1)
182185

183186
override def outputOrdering: Seq[SortOrder] = sortOrder
187+
188+
override def simpleString: String = {
189+
val orderByString = sortOrder.mkString("[", ",", "]")
190+
val outputString = output.mkString("[", ",", "]")
191+
192+
s"TakeOrderedAndProject(limit=$limit, orderBy=$orderByString, output=$outputString)"
193+
}
184194
}
185195

186196
/**

sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,22 @@ class PlannerSuite extends SparkFunSuite {
144144
}
145145

146146
test("efficient limit -> project -> sort") {
147-
val query = testData.sort('key).select('value).limit(2).logicalPlan
148-
val planned = planner.TakeOrderedAndProject(query)
149-
assert(planned.head.isInstanceOf[execution.TakeOrderedAndProject])
147+
{
148+
val query =
149+
testData.select('key, 'value).sort('key).limit(2).logicalPlan
150+
val planned = planner.TakeOrderedAndProject(query)
151+
assert(planned.head.isInstanceOf[execution.TakeOrderedAndProject])
152+
assert(planned.head.output === testData.select('key, 'value).logicalPlan.output)
153+
}
154+
155+
{
156+
// We need to make sure TakeOrderedAndProject's output is correct when we push a project
157+
// into it.
158+
val query =
159+
testData.select('key, 'value).sort('key).select('value, 'key).limit(2).logicalPlan
160+
val planned = planner.TakeOrderedAndProject(query)
161+
assert(planned.head.isInstanceOf[execution.TakeOrderedAndProject])
162+
assert(planned.head.output === testData.select('value, 'key).logicalPlan.output)
163+
}
150164
}
151165
}

0 commit comments

Comments
 (0)