Skip to content
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-45584][SQL] Fix subquery execution failure with TakeOrderedAndProjectExec #43419

Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -283,6 +283,8 @@ case class TakeOrderedAndProjectExec(
}

override def executeCollect(): Array[InternalRow] = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
override def executeCollect(): Array[InternalRow] = {
override def executeCollect(): Array[InternalRow] = executeQuery {

// SPARK-45584: Wait for subquery execution to finish before executing collect.
prepareAndWaitForSubqueries()
val orderingSatisfies = SortOrder.orderingSatisfies(child.outputOrdering, sortOrder)
val ord = new LazilyGeneratedOrdering(sortOrder, child.output)
val limited = if (orderingSatisfies) {
Expand All @@ -299,6 +301,11 @@ case class TakeOrderedAndProjectExec(
}
}

private def prepareAndWaitForSubqueries(): Unit = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of adding this method, shall we just call executeQuery in executeCollect?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sg. Just want to make sure RDDOperationScope.withScope does not any side effect right?

Copy link
Contributor

@cloud-fan cloud-fan Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same with this one, plus some tracking logic

prepare()
waitForSubqueries()
}

private val serializer: Serializer = new UnsafeRowSerializer(child.output.size)

private lazy val writeMetrics =
Expand Down
24 changes: 24 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 @@ -2711,4 +2711,28 @@ class SubquerySuite extends QueryTest
checkAnswer(df, Row(1, "foo", 1, "foo"))
}
}

test("SPARK-45584: subquery execution should not fail with ORDER BY and LIMIT") {
withTable("t1") {
sql(
"""
|CREATE TABLE t1 USING PARQUET
|AS SELECT * FROM VALUES
|(1, "a"),
|(2, "a"),
|(3, "a") t(id, value)
|""".stripMargin)
val df = sql(
"""
|WITH t2 AS (
| SELECT * FROM t1 ORDER BY id
|)
|SELECT *, (SELECT COUNT(*) FROM t2) FROM t2 LIMIT 10
|""".stripMargin)
// This should not fail with IllegalArgumentException.
checkAnswer(
df,
Row(1, "a", 3) :: Row(2, "a", 3) :: Row(3, "a", 3) :: Nil)
}
}
}