Skip to content

[WIP][SPARK-42746][SQL] Fix optimizer failure for SortOrder in the LISTAGG function #51105

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -258,12 +258,18 @@ object RewriteDistinctAggregates extends Rule[LogicalPlan] {
def patchAggregateFunctionChildren(
af: AggregateFunction)(
attrs: Expression => Option[Expression]): AggregateFunction = {
val newChildren = af.children.map(c => attrs(c).getOrElse(c))
val newChildren = af.children.map {
case so: SortOrder =>
so.copy(child = attrs(so.child).getOrElse(so.child))
case c =>
attrs(c).getOrElse(c)
}
af.withNewChildren(newChildren).asInstanceOf[AggregateFunction]
}

// Setup unique distinct aggregate children.
val distinctAggChildren = distinctAggGroups.keySet.flatten.toSeq.distinct
val distinctAggChildren = distinctAggGroups.keySet.flatten.toSeq
.filter(_.isInstanceOf[AttributeReference]).distinct
val distinctAggChildAttrMap = distinctAggChildren.map { e =>
e.canonicalized -> AttributeReference(e.sql, e.dataType, nullable = true)()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,17 @@ class DataFrameAggregateSuite extends QueryTest
)
}

test("ListAgg should be able to handle multiple distinct aggregations") {
withTable("tbl") {
sql("CREATE TABLE tbl (col1 STRING, col2 STRING) USING parquet")
sql("INSERT INTO tbl VALUES ('A', 'x'), ('A', 'y'), ('B', 'y')")
val lag1 = "LISTAGG(DISTINCT col1) WITHIN GROUP (ORDER BY col1)"
val lag2 = "LISTAGG(DISTINCT col2) WITHIN GROUP (ORDER BY col2)"
val query = sql(s"SELECT $lag1, $lag2 FROM tbl")
checkAnswer(query, Seq(Row("AB", "xy")))
}
}

test("SPARK-31500: collect_set() of BinaryType returns duplicate elements") {
val bytesTest1 = "test1".getBytes
val bytesTest2 = "test2".getBytes
Expand Down