Skip to content

Commit 7b1bef7

Browse files
committed
add test
1 parent daf206d commit 7b1bef7

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,22 @@ class Analyzer(
968968
* }}}
969969
*/
970970
object RemoveEvaluationFromSort extends Rule[LogicalPlan] {
971+
private def hasAlias(expr: Expression) = {
972+
expr.find {
973+
case a: Alias => true
974+
case _ => false
975+
}.isDefined
976+
}
977+
971978
override def apply(plan: LogicalPlan): LogicalPlan = plan transform {
979+
// The ordering expressions have no effect to the output schema of `Sort`,
980+
// so `Alias`s in ordering expressions are unnecessary and we should remove them.
981+
case s @ Sort(ordering, _, _) if ordering.exists(hasAlias) =>
982+
val newOrdering = ordering.map(_.transformUp {
983+
case Alias(child, _) => child
984+
}.asInstanceOf[SortOrder])
985+
s.copy(order = newOrdering)
986+
972987
case s @ Sort(ordering, global, child)
973988
if s.expressions.forall(_.resolved) && s.childrenResolved && !s.hasNoEvaluation =>
974989

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisSuite.scala

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,39 @@ class AnalysisSuite extends AnalysisTest {
165165

166166
test("pull out nondeterministic expressions from Sort") {
167167
val plan = Sort(Seq(SortOrder(Rand(33), Ascending)), false, testRelation)
168-
val projected = Alias(Rand(33), "_nondeterministic")()
168+
val analyzed = caseSensitiveAnalyzer.execute(plan)
169+
analyzed.transform {
170+
case s: Sort if s.expressions.exists(!_.deterministic) =>
171+
fail("nondeterministic expressions are not allowed in Sort")
172+
}
173+
}
174+
175+
test("remove still-need-evaluate ordering expressions from sort") {
176+
val a = testRelation2.output(0)
177+
val b = testRelation2.output(1)
178+
179+
def makeOrder(e: Expression): SortOrder = SortOrder(e, Ascending)
180+
181+
val noEvalOrdering = makeOrder(a)
182+
val noEvalOrderingWithAlias = makeOrder(Alias(Alias(b, "name1")(), "name2")())
183+
184+
val needEvalExpr = Coalesce(Seq(a, Literal("1")))
185+
val needEvalExpr2 = Coalesce(Seq(a, b))
186+
val needEvalOrdering = makeOrder(needEvalExpr)
187+
val needEvalOrdering2 = makeOrder(needEvalExpr2)
188+
189+
val plan = Sort(
190+
Seq(noEvalOrdering, noEvalOrderingWithAlias, needEvalOrdering, needEvalOrdering2),
191+
false, testRelation2)
192+
193+
val evaluatedOrdering = makeOrder(AttributeReference("_sortCondition", StringType)())
194+
val materializedExprs = Seq(needEvalExpr, needEvalExpr2).map(e => Alias(e, "_sortCondition")())
195+
169196
val expected =
170-
Project(testRelation.output,
171-
Sort(Seq(SortOrder(projected.toAttribute, Ascending)), false,
172-
Project(testRelation.output :+ projected, testRelation)))
197+
Project(testRelation2.output,
198+
Sort(Seq(makeOrder(a), makeOrder(b), evaluatedOrdering, evaluatedOrdering), false,
199+
Project(testRelation2.output ++ materializedExprs, testRelation2)))
200+
173201
checkAnalysis(plan, expected)
174202
}
175203
}

sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,6 @@ class DataFrameSuite extends QueryTest with SQLTestUtils {
841841
}
842842
}
843843

844-
<<<<<<< HEAD
845844
test("SPARK-8608: call `show` on local DataFrame with random columns should return same value") {
846845
// Make sure we can pass this test for both codegen mode and interpreted mode.
847846
withSQLConf(SQLConf.CODEGEN_ENABLED.key -> "true") {

0 commit comments

Comments
 (0)