Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1468,10 +1468,15 @@ object DecimalAggregates extends Rule[LogicalPlan] {
*/
object ConvertToLocalRelation extends Rule[LogicalPlan] {
def apply(plan: LogicalPlan): LogicalPlan = plan transform {
case Project(projectList, LocalRelation(output, data)) =>
case Project(projectList, LocalRelation(output, data))
if !projectList.exists(hasUnevaluableExpr) =>
val projection = new InterpretedProjection(projectList, output)
LocalRelation(projectList.map(_.toAttribute), data.map(projection))
}

private def hasUnevaluableExpr(expr: Expression): Boolean = {
expr.find(e => e.isInstanceOf[Unevaluable] && !e.isInstanceOf[AttributeReference]).isDefined
}
}

/**
Expand Down
27 changes: 27 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SubquerySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,33 @@ class SubquerySuite extends QueryTest with SharedSQLContext {
)
}

test("SPARK-15677: Queries against local relations with scalar subquery in Select list") {
withTempTable("t1", "t2") {
Seq((1, 1), (2, 2)).toDF("c1", "c2").createOrReplaceTempView("t1")
Seq((1, 1), (2, 2)).toDF("c1", "c2").createOrReplaceTempView("t2")

checkAnswer(
sql("SELECT (select 1 as col) from t1"),
Row(1) :: Row(1) :: Nil)

checkAnswer(
sql("SELECT (select max(c1) from t2) from t1"),
Row(2) :: Row(2) :: Nil)

checkAnswer(
sql("SELECT 1 + (select 1 as col) from t1"),
Row(2) :: Row(2) :: Nil)

checkAnswer(
sql("SELECT c1, (select max(c1) from t2) + c2 from t1"),
Row(1, 3) :: Row(2, 4) :: Nil)

checkAnswer(
sql("SELECT c1, (select max(c1) from t2 where t1.c2 = t2.c2) from t1"),
Row(1, 1) :: Row(2, 2) :: Nil)
}
}

test("SPARK-14791: scalar subquery inside broadcast join") {
val df = sql("select a, sum(b) as s from l group by a having a > (select avg(a) from l)")
val expected = Row(3, 2.0, 3, 3.0) :: Row(6, null, 6, null) :: Nil
Expand Down