yadavay-amzn commented on code in PR #56101: URL: https://github.com/apache/spark/pull/56101#discussion_r3338556532
########## sql/core/src/test/scala/org/apache/spark/sql/execution/joins/NearestByJoinBenchmark.scala: ########## @@ -0,0 +1,203 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.execution.joins + +import org.apache.spark.sql.QueryTest +import org.apache.spark.sql.functions._ +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.test.SharedSparkSession + +class NearestByJoinBenchmark extends QueryTest with SharedSparkSession { + + private val leftSize = 30000 + private val rightSize = 30000 + private val k = 5 + + override def sparkConf = super.sparkConf + .set("spark.driver.memory", "4g") + .set("spark.executor.memory", "4g") + + test("correctness: streaming heap produces same results as rewrite") { + // Use data with no ties to avoid tie-breaking differences + val left = spark.range(0, 20).toDF("id") + .withColumn("x", col("id").cast("double") * 7.3) + val right = spark.range(0, 15).toDF("rid") + .withColumn("y", col("rid").cast("double") * 11.1 + 0.5) + + // Get results with current rewrite + val rewriteResult = withSQLConf( + SQLConf.NEAREST_BY_BROADCAST_ENABLED.key -> "false", + SQLConf.CROSS_JOINS_ENABLED.key -> "true") { + left.nearestByJoin( + right, + abs(col("x") - col("y")), + numResults = 3, + mode = "exact", + direction = "distance") + .orderBy("id", "rid") + .collect() + } + + // Get results with streaming heap + val heapResult = withSQLConf( + SQLConf.NEAREST_BY_BROADCAST_ENABLED.key -> "true", + SQLConf.CROSS_JOINS_ENABLED.key -> "true") { + left.nearestByJoin( + right, + abs(col("x") - col("y")), + numResults = 3, + mode = "exact", + direction = "distance") + .orderBy("id", "rid") + .collect() + } + + assert(rewriteResult.length == heapResult.length, + s"Row count mismatch: rewrite=${rewriteResult.length}, heap=${heapResult.length}") + rewriteResult.zip(heapResult).zipWithIndex.foreach { case ((r, h), i) => + assert(r == h, s"Row $i mismatch: rewrite=$r, heap=$h") + } + // scalastyle:off println + println(s"Correctness check passed: ${rewriteResult.length} rows match") + // scalastyle:on println + } + + private def measureMemoryMB(block: => Long): (Long, Double, Double) = { + val runtime = Runtime.getRuntime + runtime.gc() + Thread.sleep(100) + val before = runtime.totalMemory() - runtime.freeMemory() + val result = block + val after = runtime.totalMemory() - runtime.freeMemory() + val deltaMB = Math.max(0, after - before) / (1024.0 * 1024.0) + val peakMB = after / (1024.0 * 1024.0) + (result, deltaMB, peakMB) + } + + test("benchmark: streaming heap at 200K x 200K") { + val size = 200000 + val left = spark.range(0, size).toDF("id").withColumn("x", rand(42) * 1000.0) + val right = spark.range(0, size).toDF("rid").withColumn("y", rand(43) * 1000.0) + left.cache().count() + right.cache().count() + + val start = System.nanoTime() + val count = withSQLConf( + SQLConf.NEAREST_BY_BROADCAST_ENABLED.key -> "true", + SQLConf.CROSS_JOINS_ENABLED.key -> "true") { + left.nearestByJoin(right, abs(col("x") - col("y")), + numResults = k, mode = "exact", direction = "distance").count() + } + val secs = (System.nanoTime() - start) / 1e9 + // scalastyle:off println + println("=" * 70) + println(s"200K x 200K streaming heap: ${secs}s, rows=$count") + println("=" * 70) + // scalastyle:on println + assert(count == size.toLong * k) + } + + test("memory benchmark: streaming heap vs cross-product at 30K x 30K") { + val left = spark.range(0, leftSize).toDF("id") + .withColumn("x", rand(42) * 1000.0) + val right = spark.range(0, rightSize).toDF("rid") + .withColumn("y", rand(43) * 1000.0) + + left.cache().count() + right.cache().count() + + // Streaming heap: should complete with minimal memory (M rows + k heap) + val heapStart = System.nanoTime() + val (heapCount, heapDeltaMB, heapPeakMB) = measureMemoryMB { + withSQLConf( + SQLConf.NEAREST_BY_BROADCAST_ENABLED.key -> "true", + SQLConf.CROSS_JOINS_ENABLED.key -> "true") { + left.nearestByJoin(right, abs(col("x") - col("y")), + numResults = k, mode = "exact", direction = "distance").count() + } + } + val heapMs = (System.nanoTime() - heapStart) / 1e6 + + // Cross-product: tries to materialize N*M = 900M rows → likely OOM at 1GB Review Comment: Fixed (old benchmark file deleted entirely, new SqlBasedBenchmark uses ASCII only). -- 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]
