ganeshashree commented on code in PR #60681:
URL: https://github.com/apache/spark/pull/60681#discussion_r4625011943


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/ScanLimitSuite.scala:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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
+
+import org.apache.spark.{SparkConf, SparkException}
+import org.apache.spark.internal.config.EXECUTOR_HEARTBEAT_INTERVAL
+import org.apache.spark.sql.QueryTest
+import org.apache.spark.sql.execution.streaming.runtime.StreamExecution
+import org.apache.spark.sql.functions.udf
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.test.SharedSparkSession
+
+class ScanLimitSuite extends QueryTest with SharedSparkSession {
+  import testImplicits._
+
+  // A running task reports the bytes it has read with the heartbeats of its 
executor.
+  override protected def sparkConf: SparkConf =
+    super.sparkConf.set(EXECUTOR_HEARTBEAT_INTERVAL.key, "100ms")
+
+  // Keeps a task running for much longer than it takes to cancel its query.
+  private val block = udf((id: Long) => { Thread.sleep(30000); id })
+
+  // Slow enough for several heartbeats to report what the task has read.
+  private val slow = udf((id: Long) => { Thread.sleep(200); id })
+
+  /** Runs `f` with the path of a Parquet file that has the IDs 0 to 3. */
+  private def withData(f: String => Unit): Unit = {
+    withTempPath { path =>
+      spark.range(0, 4, 1, 1).write.parquet(path.getCanonicalPath)
+      f(path.getCanonicalPath)
+    }
+  }
+
+  /** Checks that `query` is cancelled for reading more than a limit of 1 
byte. */
+  private def checkScanLimitExceeded(query: => Unit): Unit = {
+    withSQLConf(SQLConf.MAX_SCAN_BYTES.key -> "1") {
+      checkErrorMatchPVals(
+        exception = intercept[SparkException](query),
+        condition = "SCAN_BYTES_LIMIT_EXCEEDED",
+        parameters = Map(
+          "scannedBytes" -> ".+ B",
+          "limit" -> "1\\.0 B",
+          "config" -> "\"spark.sql.execution.maxScanBytes\""))
+    }
+  }
+
+  test("a query that reads more than the limit is cancelled") {
+    withData { path =>
+      checkScanLimitExceeded {
+        spark.read.parquet(path).select(block($"id")).collect()
+      }
+    }
+  }
+
+  test("a query that reads less than the limit runs") {
+    withData { path =>
+      withSQLConf(SQLConf.MAX_SCAN_BYTES.key -> "1g") {
+        checkAnswer(spark.read.parquet(path), spark.range(4).toDF())
+      }
+    }
+  }
+
+  test("a query that fails for another reason after it read more than the 
limit keeps its error") {
+    withData { path =>
+      withSQLConf(SQLConf.MAX_SCAN_BYTES.key -> "1") {
+        val df = spark.read.parquet(path)
+        val e = intercept[IllegalStateException] {
+          SQLExecution.withNewExecutionId(df.queryExecution) {
+            // A heartbeat may report what the task read before the task ends, 
and so cancel
+            // the job. Either way the query has read more than the limit once 
the job is over.
+            try {
+              df.queryExecution.executedPlan.executeCollect()
+            } catch {
+              case _: SparkException =>
+            }
+            spark.sparkContext.listenerBus.waitUntilEmpty()
+            val executionId =
+              
spark.sparkContext.getLocalProperty(SQLExecution.EXECUTION_ID_KEY).toLong
+            
assert(spark.sharedState.scanLimitListener.bytesReadOverLimit(executionId).isDefined)
+            throw new IllegalStateException("unrelated")
+          }
+        }
+        assert(e.getMessage == "unrelated")
+      }
+    }
+  }
+
+  test("the limit of a query covers its nested executions") {

Review Comment:
   **[P2]** The doc says the limit covers subqueries, but the only nested case 
tested is `CACHE TABLE`, which runs on the query thread. Could you add a case 
where the over-limit read happens in a scalar subquery or broadcast job, with 
AQE on and off? Those jobs run on other threads and rely on the captured local 
properties, so a test would catch a regression there.



-- 
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]

Reply via email to