gengliangwang opened a new pull request, #60681: URL: https://github.com/apache/spark/pull/60681
### What changes were proposed in this pull request? This PR adds `spark.sql.execution.maxScanBytes`, off by default: the most bytes a query may read from its data sources. A query that reads more is cancelled and fails with the new error condition `SCAN_BYTES_LIMIT_EXCEEDED`, for example: ``` [SCAN_BYTES_LIMIT_EXCEEDED] The query was cancelled because it read 101.0 GiB from its data sources, more than the limit of 100.0 GiB set by "spark.sql.execution.maxScanBytes". Make the query read less data, for example by filtering on a partition column, or increase "spark.sql.execution.maxScanBytes". SQLSTATE: 53400 ``` How it works: - `SQLExecution.withNewExecutionId` registers a root execution that has a limit with a new `ScanLimitListener`, and unregisters it when the execution ends. The listener is created, and added to the listener bus, only when the first query with a limit runs, so there is no cost when the config is not set. - The listener adds up the task input metric `bytesRead` of the stages of the execution. The metric arrives with the executor heartbeats while a task runs, and with the task end event. A stage is matched to its execution by the root execution ID in the job properties, so nested executions (e.g. the query that `CACHE TABLE` runs) count towards the same limit. - Once the total is over the limit, the listener cancels the jobs of the execution by the job tag of the root execution. It cancels again when the execution submits another stage, because a cancellation only reaches the jobs that are active at that time. - `withNewExecutionId` then fails the query with `SCAN_BYTES_LIMIT_EXCEEDED`. The error of the cancelled job stays as the cause. - A task retry, a speculative task and a new attempt of a stage read the data of a partition again. The listener keeps one value per partition of a stage, the largest that any attempt reported, so this does not count against the limit. The limit is a best-effort guard rather than a hard cap, which the config documents: - A query can read more than the limit before it is cancelled, by about what it reads in one heartbeat interval (`spark.executor.heartbeatInterval`, 10s by default). A query whose last job ends before the listener sees that it is over the limit does not fail. - Only data sources that report the bytes they read are limited. File-based sources report the bytes they read from storage, while sources such as JDBC report none. - Reading cached data counts the size of the cached blocks. - Streaming queries are not limited. ### Why are the changes needed? A query with a missing filter, which is common in AI-generated and ad hoc SQL, can read terabytes of data before anyone notices. A timeout does not help: it fires only after the compute has been spent, and the query returns nothing. `LIMIT` is not always wanted, and it does not always bound what a query reads. Other engines have a similar setting: `maximum_bytes_billed` in BigQuery and `query_max_scan_physical_bytes` in Trino. ### Does this PR introduce _any_ user-facing change? Yes. It adds the config `spark.sql.execution.maxScanBytes` (-1 by default, which means no limit) and the error condition `SCAN_BYTES_LIMIT_EXCEEDED`. Nothing changes unless the config is set. ### How was this patch tested? New tests: - `ScanLimitListenerSuite` drives the listener with scheduler events: it cancels an execution once its tasks have read more than the limit, counts a partition once over the attempts of its task and of its stage, cancels the jobs that an execution submits after it went over the limit, and ignores executions that are not registered. - `ScanLimitSuite` runs queries end to end with a short heartbeat interval: a query over the limit is cancelled while its task is still running, a query under the limit runs, the limit covers a nested execution, and a streaming query is not limited. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code This pull request and its description were written by Isaac. -- 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]
