Skip to content

[SPARK-33400][SQL] Normalize sameOrderExpressions in SortOrder to avoid unnecessary sort operations #30302

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

Closed
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 @@ -65,7 +65,11 @@ trait AliasAwareOutputOrdering extends AliasAwareOutputExpression {

final override def outputOrdering: Seq[SortOrder] = {
if (hasAlias) {
orderingExpressions.map(normalizeExpression(_).asInstanceOf[SortOrder])
orderingExpressions.map { sortOrder =>
val newSortOrder = normalizeExpression(sortOrder).asInstanceOf[SortOrder]
val newSameOrderExpressions = newSortOrder.sameOrderExpressions.map(normalizeExpression)
newSortOrder.copy(sameOrderExpressions = newSameOrderExpressions)
}
} else {
orderingExpressions
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,37 @@ class PlannerSuite extends SharedSparkSession with AdaptiveSparkPlanHelper {
}
}

test("SPARK-33400: Normalization of sortOrder should take care of sameOrderExprs") {
withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
withTempView("t1", "t2", "t3") {
spark.range(10).repartition($"id").createTempView("t1")
spark.range(20).repartition($"id").createTempView("t2")
spark.range(30).repartition($"id").createTempView("t3")
val planned = sql(
"""
|SELECT t2id, t3.id as t3id
|FROM (
| SELECT t1.id as t1id, t2.id as t2id
| FROM t1, t2
| WHERE t1.id = t2.id
|) t12, t3
|WHERE t2id = t3.id
""".stripMargin).queryExecution.executedPlan

val sortNodes = planned.collect { case s: SortExec => s }
assert(sortNodes.size == 3)

val projects = planned.collect { case p: ProjectExec => p }
assert(projects.exists(_.outputOrdering match {
case Seq(SortOrder(_, Ascending, NullsFirst, sameOrderExprs)) =>
sameOrderExprs.size == 1 && sameOrderExprs.head.isInstanceOf[AttributeReference] &&
sameOrderExprs.head.asInstanceOf[AttributeReference].name == "t2id"
case _ => false
}))
}
}
}

test("aliases to expressions should not be replaced") {
withSQLConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
withTempView("df1", "df2") {
Expand Down