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 @@ -76,6 +76,8 @@ class DetectAmbiguousSelfJoin(conf: SQLConf) extends Rule[LogicalPlan] {
// We always remove the special metadata from `AttributeReference` at the end of this rule, so
// Dataset column reference only exists in the root node via Dataset transformations like
// `Dataset#select`.
if (plan.find(_.isInstanceOf[Join]).isEmpty) return stripColumnReferenceMetadataInPlan(plan)

val colRefAttrs = plan.expressions.flatMap(_.collect {
case a: AttributeReference if isColumnReference(a) => a
})
Expand Down Expand Up @@ -153,6 +155,10 @@ class DetectAmbiguousSelfJoin(conf: SQLConf) extends Rule[LogicalPlan] {
}
}

stripColumnReferenceMetadataInPlan(plan)
}

private def stripColumnReferenceMetadataInPlan(plan: LogicalPlan): LogicalPlan = {
plan.transformExpressions {
case a: AttributeReference if isColumnReference(a) =>
// Remove the special metadata from this `AttributeReference`, as the detection is done.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.spark.sql

import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.functions.{count, sum}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.test.SharedSparkSession
Expand Down Expand Up @@ -202,4 +203,15 @@ class DataFrameSelfJoinSuite extends QueryTest with SharedSparkSession {
assertAmbiguousSelfJoin(df1.join(df4).join(df2).select(df2("id")))
}
}

test("SPARK-28344: don't fail as ambiguous self join when there is no join") {
withSQLConf(
SQLConf.FAIL_AMBIGUOUS_SELF_JOIN_ENABLED.key -> "true") {
val df = Seq(1, 1, 2, 2).toDF("a")
val w = Window.partitionBy(df("a"))
checkAnswer(
df.select(df("a").alias("x"), sum(df("a")).over(w)),
Seq((1, 2), (1, 2), (2, 4), (2, 4)).map(Row.fromTuple))
}
}
}