Skip to content
Closed
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 @@ -17,6 +17,7 @@

package org.apache.spark.sql.execution.exchange

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

import org.apache.spark.sql.catalyst.expressions._
Expand Down Expand Up @@ -227,9 +228,16 @@ case class EnsureRequirements(conf: SQLConf) extends Rule[SparkPlan] {
currentOrderOfKeys: Seq[Expression]): (Seq[Expression], Seq[Expression]) = {
val leftKeysBuffer = ArrayBuffer[Expression]()
val rightKeysBuffer = ArrayBuffer[Expression]()
val alreadyUsedIndexes = mutable.Set[Int]()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe pickedIndexes?

val keysAndIndexes = currentOrderOfKeys.zipWithIndex

expectedOrderOfKeys.foreach(expression => {
val index = currentOrderOfKeys.indexWhere(e => e.semanticEquals(expression))
val index = keysAndIndexes.find { case (e, idx) =>
// As we may have the same key used many times, we need to filter out its occurrence we
// have already used.
e.semanticEquals(expression) && !alreadyUsedIndexes.contains(idx)
}.map(_._2).get
alreadyUsedIndexes += index
leftKeysBuffer.append(leftKeys(index))
rightKeysBuffer.append(rightKeys(index))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,17 @@ class PlannerSuite extends SharedSQLContext {
}
assert(rangeExecInZeroPartition.head.outputPartitioning == UnknownPartitioning(0))
}

test("SPARK-24495: EnsureRequirements can return wrong plan when reusing the same key in join") {
withSQLConf(("spark.sql.shuffle.partitions", "1"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not use hard coded config name, use SQLConf.SHUFFLE_PARTITIONS instead.

a nit: we usually use a -> b, c -> d, ... to specify config pairs.

("spark.sql.constraintPropagation.enabled", "false"),
("spark.sql.autoBroadcastJoinThreshold", "-1")) {
val df1 = spark.range(100)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should make sure range has more than one partitions, otherwise we can't reproduce the bug.

val df2 = spark.range(100).select(($"id" * 2).as("b1"), (- $"id").as("b2"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

($"id" * 2).as("b1") -> $"id".as("b1"), to minimize the test.

val res = df1.join(df2, $"id" === $"b1" && $"id" === $"b2")
assert(res.collect().sameElements(Array(Row(0, 0, 0))))
}
}
}

// Used for unit-testing EnsureRequirements
Expand Down