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 @@ -630,7 +630,8 @@ class Analyzer(

// Try resolving the ordering as though it is in the aggregate clause.
try {
val aliasedOrdering = sortOrder.map(o => Alias(o.child, "aggOrder")())
val unresolvedSortOrders = sortOrder.filter(s => !s.resolved || containsAggregate(s))
val aliasedOrdering = unresolvedSortOrders.map(o => Alias(o.child, "aggOrder")())
val aggregatedOrdering = aggregate.copy(aggregateExpressions = aliasedOrdering)
val resolvedAggregate: Aggregate = execute(aggregatedOrdering).asInstanceOf[Aggregate]
val resolvedAliasedOrdering: Seq[Alias] =
Expand Down Expand Up @@ -663,13 +664,19 @@ class Analyzer(
}
}

val sortOrdersMap = unresolvedSortOrders
.map(new TreeNodeRef(_))
.zip(evaluatedOrderings)
.toMap
val finalSortOrders = sortOrder.map(s => sortOrdersMap.getOrElse(new TreeNodeRef(s), s))

// Since we don't rely on sort.resolved as the stop condition for this rule,
// we need to check this and prevent applying this rule multiple times
if (sortOrder == evaluatedOrderings) {
if (sortOrder == finalSortOrders) {
sort
} else {
Project(aggregate.output,
Sort(evaluatedOrderings, global,
Sort(finalSortOrders, global,
aggregate.copy(aggregateExpressions = originalAggExprs ++ needsPushDown)))
}
} catch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,24 @@ class AnalysisSuite extends AnalysisTest {
// checkUDF(udf4, expected4)
}

test("SPARK-11863 mixture of aliases and real columns in order by clause - tpcds 19,55,71") {
val a = testRelation2.output(0)
val c = testRelation2.output(2)
val alias1 = a.as("a1")
val alias2 = c.as("a2")
val alias3 = count(a).as("a3")

val plan = testRelation2
.groupBy('a, 'c)('a.as("a1"), 'c.as("a2"), count('a).as("a3"))
.orderBy('a1.asc, 'c.asc)

val expected = testRelation2
.groupBy(a, c)(alias1, alias2, alias3)
.orderBy(alias1.toAttribute.asc, alias2.toAttribute.asc)
.select(alias1.toAttribute, alias2.toAttribute, alias3.toAttribute)
checkAnalysis(plan, expected)
}

test("analyzer should replace current_timestamp with literals") {
val in = Project(Seq(Alias(CurrentTimestamp(), "a")(), Alias(CurrentTimestamp(), "b")()),
LocalRelation())
Expand Down