rich7420 commented on code in PR #6785:
URL: https://github.com/apache/datafusion-comet/pull/6785#discussion_r4706593646
##########
spark/src/test/scala/org/apache/comet/exec/CometJoinSuite.scala:
##########
@@ -2999,6 +3614,125 @@ class CometJoinSuite extends CometTestBase {
}
}
+ test("an inner hash join over a checkpointed streamed side declares no
order") {
+ // Comet cannot check the checkpoint's order, so it declares none, and the
inner join keeps its
+ // probe rows in place without one, so it stays native.
+ withOrderedCheckpoint { streamed =>
+ val build = spark.range(10).selectExpr("id * 10 AS k", "id AS w")
+ val order =
+ (0 to 1).map(i => SortOrder(BoundReference(i, LongType, nullable =
true), Ascending))
+ for (hint <- Seq("shuffle_hash", "broadcast")) {
+ def df: DataFrame = streamed
+ .join(build.hint(hint), streamed("k") === build("k"))
+ .select(streamed("k"), streamed("v"), build("w"))
+ .sortWithinPartitions("k", "v")
+ val (_, plan) = checkSparkAnswer(df)
+ val declared = collect(plan) {
+ case j: CometHashJoinExec =>
j.nativeOp.getHashJoin.getStreamedSortOrdersCount
+ case j: CometBroadcastHashJoinExec =>
j.nativeOp.getHashJoin.getStreamedSortOrdersCount
+ }
+ assert(declared == Seq(0), s"$hint\n$plan")
+ assert(
+ collect(plan) { case s @ (_: CometSortExec | _: SortExec) => s
}.isEmpty,
+ s"$hint\n$plan")
+ assert(partitionsInOrder(df, order: _*), s"$hint\n$plan")
+ }
+ }
+ }
+
+ test("a native window above an inner hash join over a checkpointed streamed
side runs") {
+ // Spark drops the window's sort for the checkpoint's order, which the
join reports, but Comet
+ // cannot check that order, so the join declares none and the native
window sorts its input.
+ withOrderedCheckpoint { streamed =>
+ for {
+ (build, force) <- checkpointJoinBuildSides
+ window <- Seq(
+ "dense_rank() OVER (PARTITION BY k ORDER BY v)",
+ "sum(v) OVER (PARTITION BY k)")
+ } {
+ withSQLConf(CometConf.COMET_FORCE_SHJ.key -> force.toString) {
+ def df: DataFrame = streamed
+ .join(build, $"k" === $"id")
+ .selectExpr("k", "v", s"$window AS r")
+ val plan = df.queryExecution.executedPlan
+ val context = s"force=$force $window\n$plan"
+ val joins = collect(plan) {
+ case j: CometHashJoinExec => j
+ case j: CometBroadcastHashJoinExec => j
+ }
+ assert(joins.size == 1, context)
+ val windows = collect(plan) { case w: CometWindowExec => w }
+ assert(
+
windows.map(_.nativeOp.getWindow.getUnverifiedInputSortOrdersCount) == Seq(2),
+ context)
+ assert(
+ collect(plan) { case s @ (_: CometSortExec | _: SortExec) => s
}.isEmpty,
+ context)
+ sameRowsAsSpark(df)
+ }
+ }
+ }
+ }
+
+ test("a native window group limit above an inner hash join over a checkpoint
ranks it") {
+ // The native limit declares the order Spark requires of its input without
checking it, which
+ // holds for the checkpoint's rows.
+ withOrderedCheckpoint { streamed =>
+ for ((build, force) <- checkpointJoinBuildSides) {
+ withSQLConf(CometConf.COMET_FORCE_SHJ.key -> force.toString) {
+ def df: DataFrame = streamed
+ .join(build, $"k" === $"id")
+ .selectExpr("k", "v", "rank() OVER (PARTITION BY k ORDER BY v) AS
rk")
+ .where("rk <= 2")
+ val plan = df.queryExecution.executedPlan
+ val context = s"force=$force\n$plan"
+ assert(collect(plan) { case l: CometWindowGroupLimitExec => l
}.nonEmpty, context)
+ assert(collect(plan) { case w: CometWindowExec => w }.size == 1,
context)
+ assert(
+ collect(plan) { case s @ (_: CometSortExec | _: SortExec) => s
}.isEmpty,
+ context)
+ sameRowsAsSpark(df)
+ }
+ }
+ }
+ }
+
+ test("a native window over an outer hash join that may move rows restores
the whole order") {
Review Comment:
This passes for me with the native window's sort restoration disabled, since
it's all one native plan. Could the name say no sort is needed here?
##########
spark/src/test/scala/org/apache/comet/exec/CometJoinSuite.scala:
##########
@@ -1404,19 +1422,491 @@ class CometJoinSuite extends CometTestBase {
// output ordering satisfies it, so the window needs that ordering restored
too.
for (adaptive <- Seq(false, true)) {
test(s"forceShuffledHashJoin restores ordering for a window over the join,
AQE=$adaptive") {
+ withForcedShjTables(adaptive) {
+ val (_, cometPlan) = checkSparkAnswer(
+ "SELECT big._1, big._2, small._2, count(*) OVER (PARTITION BY
big._1) " +
+ "FROM big JOIN small ON big._1 = small._1")
+ assert(collect(cometPlan) { case j: CometHashJoinExec => j }.nonEmpty,
cometPlan)
+ assert(collect(cometPlan) { case w: CometWindowExec => w }.nonEmpty,
cometPlan)
+ }
+ }
+ }
+
+ test("forceShuffledHashJoin chain keeps early join dynamic filtering") {
+ withForcedShjTables(adaptive = false) {
+ val query = "SELECT big._1, big._2, mid._2, small._2 FROM big " +
+ "JOIN mid ON big._1 = mid._1 JOIN small ON big._1 = small._1"
+ for (enabled <- Seq(false, true)) {
+ withSQLConf(CometConf.COMET_EXEC_JOIN_DYNAMIC_FILTER_ENABLED.key ->
enabled.toString) {
+ val (_, plan) = checkSparkAnswerAndOperator(sql(query))
+ val joins = collect(plan) { case join: CometHashJoinExec => join }
+ assert(joins.map(_.buildSide) == Seq(BuildRight, BuildRight), plan)
+ // big has 100 rows per key, mid 30, and small one row for keys 0 to
90 by 10.
+ val outputs = joins.map(_.metrics("output_rows").value).sorted
+ assert(outputs == (if (enabled) Seq(30000L, 30000L) else Seq(30000L,
300000L)))
+ if (enabled) {
+ assert(
+
joins.map(_.metrics("dynamic_filter_early_rows_evaluated").value).sum == 10000L)
+
assert(joins.map(_.metrics("dynamic_filter_early_rows_pruned").value).sum ==
9000L)
+ } else {
+
assert(joins.forall(!_.metrics.contains("dynamic_filter_early_rows_pruned")))
+ }
+ }
+ }
+ }
+ }
+
+ private def checkPartitionsSortedOverHashJoin(df: => DataFrame): Unit = {
Review Comment:
Could this reuse `sameRowsAsSpark` and `partitionsInOrder`? When it failed
for me the message was only `was false false, false`, with no plan.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]