Skip to content
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

[SPARK-49722][CONNECT] Fix Column ordering of dropDuplicates and dropDuplicatesWithinWatermarks #48173

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -1194,6 +1194,23 @@ class SparkConnectPlanner(
}
}

private def groupColsFromDropDuplicates(
colNames: Seq[String], allColumns: Seq[Attribute]): Seq[Attribute] = {
val resolver = session.sessionState.analyzer.resolver
// [SPARK-31990][SPARK-49722]: We must keep `toSet.toSeq` here because of the backward
// compatibility issue (the Streaming's state store depends on the `groupCols` order).
// If you modify this function, please also modify the same function in `DataSet.scala` in SQL.
colNames.toSet.toSeq.flatMap { (colName: String) =>
// It is possibly there are more than one columns with the same name,
// so we call filter instead of find.
val cols = allColumns.filter(col => resolver(col.name, colName))
if (cols.isEmpty) {
throw InvalidPlanInput(s"Invalid deduplicate column ${colName}")
}
cols
}
}

private def transformDeduplicate(rel: proto.Deduplicate): LogicalPlan = {
if (!rel.hasInput) {
throw InvalidPlanInput("Deduplicate needs a plan input")
Expand All @@ -1209,19 +1226,15 @@ class SparkConnectPlanner(
val resolver = session.sessionState.analyzer.resolver
val allColumns = queryExecution.analyzed.output
if (rel.getAllColumnsAsKeys) {
if (rel.getWithinWatermark) DeduplicateWithinWatermark(allColumns, queryExecution.analyzed)
else Deduplicate(allColumns, queryExecution.analyzed)
val groupCals = groupColsFromDropDuplicates(allColumns.map(_.name), allColumns)
WweiL marked this conversation as resolved.
Show resolved Hide resolved
if (rel.getWithinWatermark) {
DeduplicateWithinWatermark(groupCals, queryExecution.analyzed)
} else {
Deduplicate(groupCals, queryExecution.analyzed)
}
} else {
val toGroupColumnNames = rel.getColumnNamesList.asScala.toSeq
val groupCols = toGroupColumnNames.flatMap { (colName: String) =>
// It is possibly there are more than one columns with the same name,
// so we call filter instead of find.
val cols = allColumns.filter(col => resolver(col.name, colName))
if (cols.isEmpty) {
throw InvalidPlanInput(s"Invalid deduplicate column ${colName}")
}
cols
}
val groupCols = groupColsFromDropDuplicates(toGroupColumnNames, allColumns)
if (rel.getWithinWatermark) DeduplicateWithinWatermark(groupCols, queryExecution.analyzed)
else Deduplicate(groupCols, queryExecution.analyzed)
}
Expand Down
2 changes: 2 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,8 @@ class Dataset[T] private[sql](
val allColumns = queryExecution.analyzed.output
// SPARK-31990: We must keep `toSet.toSeq` here because of the backward compatibility issue
// (the Streaming's state store depends on the `groupCols` order).
// SPARK-49722: If you modify this function, please please also modify the same function in
// `SparkConnectPlanner.scala` in connect.
colNames.toSet.toSeq.flatMap { (colName: String) =>
// It is possibly there are more than one columns with the same name,
// so we call filter instead of find.
Expand Down