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 @@ -2400,16 +2400,22 @@ class Analyzer(override val catalogManager: CatalogManager)
// to push down this ordering expression and can reference the original aggregate
// expression instead.
val needsPushDown = ArrayBuffer.empty[NamedExpression]
val evaluatedOrderings = resolvedAliasedOrdering.zip(unresolvedSortOrders).map {
case (evaluated, order) =>
val orderToAlias = unresolvedSortOrders.zip(aliasedOrdering)
val evaluatedOrderings = resolvedAliasedOrdering.zip(orderToAlias).map {
case (evaluated, (order, aliasOrder)) =>
val index = originalAggExprs.indexWhere {
case Alias(child, _) => child semanticEquals evaluated.child
case other => other semanticEquals evaluated.child
}

if (index == -1) {
needsPushDown += evaluated
order.copy(child = evaluated.toAttribute)
if (CharVarcharUtils.getRawType(evaluated.metadata).nonEmpty) {
needsPushDown += aliasOrder
order.copy(child = aliasOrder)
} else {
needsPushDown += evaluated
order.copy(child = evaluated.toAttribute)
}
} else {
order.copy(child = originalAggExprs(index).toAttribute)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,14 @@ trait CharVarcharTestSuite extends QueryTest with SQLTestUtils {
Row("c"))
}
}

test("SPARK-34003: fix char/varchar fails w/ both group by and order by ") {
withTable("t") {
sql(s"CREATE TABLE t(v VARCHAR(3), i INT) USING $format")
sql("INSERT INTO t VALUES ('c', 1)")
checkAnswer(sql("SELECT v, sum(i) FROM t GROUP BY v ORDER BY v"), Row("c", 1))
}
}
}

// Some basic char/varchar tests which doesn't rely on table implementation.
Expand Down