You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "trsigg (via GitHub)" <gi...@apache.org> on 2025/06/03 08:31:38 UTC
[PR] [WIP][SPARK-47404][SQL] Add configurable size limits for ANTLR DFA cache [spark]
trsigg opened a new pull request, #51069:
URL: https://github.com/apache/spark/pull/51069
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
Add hooks to release the ANTLR DFA cache after parsing SQL
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
ANTLR builds a DFA cache while parsing to speed up parsing of similar future inputs. However, this cache is never cleared and can only grow. Extremely large SQL inputs can lead to very large DFA caches (>20GiB in one extreme case I've seen).
Spark’s ANTLR SQL parser is derived from the Presto ANTLR SQL Parser (see [SPARK-13713](https://issues.apache.org/jira/browse/SPARK-13713) and https://github.com/apache/spark/pull/11557), and Presto has added hooks to be able to clear this DFA cache (https://github.com/trinodb/trino/pull/3186). I think Spark should have similar hooks.
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
New SQLConf to control the behaviour (spark.sql.parser.releaseAntlrCacheAfterParse)
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
New unit test
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
No
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-47404][SQL] Add configurable size limits for ANTLR DFA cache [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #51069:
URL: https://github.com/apache/spark/pull/51069#discussion_r2199275333
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -1145,6 +1145,63 @@ object SQLConf {
.booleanConf
.createWithDefault(false)
+ val PARSER_DFA_CACHE_FLUSH_RATIO =
+ buildConf("spark.sql.parser.parserDfaCacheFlushRatio")
+ .internal()
+ .doc(
+ """Like `spark.sql.parser.parserDfaCacheFlushThreshold`, but uses a threshold that is a
+ |linear function of the memory allocated to the driver process. Represents the percentage
+ |of the driver memory that the DFA cache can consume before it is flushed.
+ |
+ |Estimates the memory used by the DFA cache, assuming each state consumes
+ |`AbstractParser.BYTES_PER_DFA_STATE` bytes. If this value exceeds the product of the
+ |driver memory with the config value (interpreted as a percentage), the cache is flushed.
+ |
+ |Active values should be in the range 0-100, and a negative value disables the feature.
+ |If both this config and `spark.sql.parser.parserDfaCacheFlushThreshold` are set, the
+ |cache is flushed if either condition is met.
+ |Requires `spark.sql.parser.manageParserCachesKillSwitch` to be true to take effect.
+ |""".stripMargin)
+ .version("4.0.0")
+ .doubleConf
+ .checkValue(_ <= 100.0, "The ratio must be less than 100%")
+ .createWithDefault(-1.0)
+
+ val PARSER_DFA_CACHE_FLUSH_THRESHOLD =
+ buildConf("spark.sql.parser.parserDfaCacheFlushThreshold")
+ .internal()
+ .doc(
+ """When positive, release ANTLR caches after parsing a SQL query when the number of states
+ |in the DFA cache exceeds the value of the config. DFA states empirically consume about
+ |`AbstractParser.BYTES_PER_DFA_STATE` bytes of memory each.
+ |
+ |ANTLR parsers retain a DFA cache designed to speed up parsing future input. However,
+ |there is no limit to how large this cache can become. Parsing large SQL statements can
+ |lead to an accumulation of objects in the cache that are unlikely to be reused, causing
+ |high GC overhead and eventually OOMs.
+ |
+ |If this config is set to a negative value, it is ignored.
+ |If both this config and `spark.sql.parser.parserDfaCacheFlushRatio` are set, the
+ |cache is flushed if either condition is met.
+ |Requires `spark.sql.parser.manageParserCachesKillSwitch` to be true to take effect.
+ |
+ |Can significantly slow down parsing in exchange for better memory stability.
+ |""".stripMargin)
+ .version("4.0.0")
+ .intConf
+ .createWithDefault(-1)
+
+ val MANAGE_PARSER_CACHES =
+ buildConf("spark.sql.parser.manageParserCachesKillSwitch")
Review Comment:
```suggestion
buildConf("spark.sql.parser.manageParserCaches")
```
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-47404][SQL] Add configurable size limits for ANTLR DFA cache [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #51069:
URL: https://github.com/apache/spark/pull/51069#discussion_r2199274712
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -1145,6 +1145,63 @@ object SQLConf {
.booleanConf
.createWithDefault(false)
+ val PARSER_DFA_CACHE_FLUSH_RATIO =
+ buildConf("spark.sql.parser.parserDfaCacheFlushRatio")
+ .internal()
+ .doc(
+ """Like `spark.sql.parser.parserDfaCacheFlushThreshold`, but uses a threshold that is a
+ |linear function of the memory allocated to the driver process. Represents the percentage
+ |of the driver memory that the DFA cache can consume before it is flushed.
+ |
+ |Estimates the memory used by the DFA cache, assuming each state consumes
+ |`AbstractParser.BYTES_PER_DFA_STATE` bytes. If this value exceeds the product of the
+ |driver memory with the config value (interpreted as a percentage), the cache is flushed.
+ |
+ |Active values should be in the range 0-100, and a negative value disables the feature.
+ |If both this config and `spark.sql.parser.parserDfaCacheFlushThreshold` are set, the
+ |cache is flushed if either condition is met.
+ |Requires `spark.sql.parser.manageParserCachesKillSwitch` to be true to take effect.
+ |""".stripMargin)
+ .version("4.0.0")
Review Comment:
4.1.0
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-47404][SQL] Add configurable size limits for ANTLR DFA cache [spark]
Posted by "trsigg (via GitHub)" <gi...@apache.org>.
trsigg commented on code in PR #51069:
URL: https://github.com/apache/spark/pull/51069#discussion_r2199325678
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisSuite.scala:
##########
@@ -1034,51 +1034,53 @@ class AnalysisSuite extends AnalysisTest with Matchers {
test("SPARK-30886 Deprecate two-parameter TRIM/LTRIM/RTRIM") {
Seq("trim", "ltrim", "rtrim").foreach { f =>
- val logAppender = new LogAppender("deprecated two-parameter TRIM/LTRIM/RTRIM functions")
- def check(count: Int): Unit = {
- val message = "Two-parameter TRIM/LTRIM/RTRIM function signatures are deprecated."
- assert(logAppender.loggingEvents.size == count)
- assert(logAppender.loggingEvents.exists(
- e => e.getLevel == Level.WARN &&
- e.getMessage.getFormattedMessage.contains(message)))
- }
+ withSQLConf(SQLConf.MANAGE_PARSER_CACHES.key -> "false") {
Review Comment:
It adds additional logging that interferes with the logging count checks. If I comment out [these](https://github.com/apache/spark/pull/51069/files#diff-d13c96aa540aa8c6fcc48f31a03f4be71af2f63e795cae0c25a40eca8039788eR542-R547) log lines, the test passes even with this feature enabled. Added a comment here for clarity.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-47404][SQL] Add configurable size limits for ANTLR DFA cache [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #51069:
URL: https://github.com/apache/spark/pull/51069#discussion_r2199278482
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/AnalysisSuite.scala:
##########
@@ -1034,51 +1034,53 @@ class AnalysisSuite extends AnalysisTest with Matchers {
test("SPARK-30886 Deprecate two-parameter TRIM/LTRIM/RTRIM") {
Seq("trim", "ltrim", "rtrim").foreach { f =>
- val logAppender = new LogAppender("deprecated two-parameter TRIM/LTRIM/RTRIM functions")
- def check(count: Int): Unit = {
- val message = "Two-parameter TRIM/LTRIM/RTRIM function signatures are deprecated."
- assert(logAppender.loggingEvents.size == count)
- assert(logAppender.loggingEvents.exists(
- e => e.getLevel == Level.WARN &&
- e.getMessage.getFormattedMessage.contains(message)))
- }
+ withSQLConf(SQLConf.MANAGE_PARSER_CACHES.key -> "false") {
Review Comment:
how does this feature affect this test case?
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-47404][SQL] Add configurable size limits for ANTLR DFA cache [spark]
Posted by "trsigg (via GitHub)" <gi...@apache.org>.
trsigg commented on code in PR #51069:
URL: https://github.com/apache/spark/pull/51069#discussion_r2199301163
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -1145,6 +1145,63 @@ object SQLConf {
.booleanConf
.createWithDefault(false)
+ val PARSER_DFA_CACHE_FLUSH_RATIO =
+ buildConf("spark.sql.parser.parserDfaCacheFlushRatio")
+ .internal()
+ .doc(
+ """Like `spark.sql.parser.parserDfaCacheFlushThreshold`, but uses a threshold that is a
+ |linear function of the memory allocated to the driver process. Represents the percentage
+ |of the driver memory that the DFA cache can consume before it is flushed.
+ |
+ |Estimates the memory used by the DFA cache, assuming each state consumes
+ |`AbstractParser.BYTES_PER_DFA_STATE` bytes. If this value exceeds the product of the
+ |driver memory with the config value (interpreted as a percentage), the cache is flushed.
+ |
+ |Active values should be in the range 0-100, and a negative value disables the feature.
+ |If both this config and `spark.sql.parser.parserDfaCacheFlushThreshold` are set, the
+ |cache is flushed if either condition is met.
+ |Requires `spark.sql.parser.manageParserCachesKillSwitch` to be true to take effect.
+ |""".stripMargin)
+ .version("4.0.0")
+ .doubleConf
+ .checkValue(_ <= 100.0, "The ratio must be less than 100%")
+ .createWithDefault(-1.0)
+
+ val PARSER_DFA_CACHE_FLUSH_THRESHOLD =
+ buildConf("spark.sql.parser.parserDfaCacheFlushThreshold")
+ .internal()
+ .doc(
+ """When positive, release ANTLR caches after parsing a SQL query when the number of states
+ |in the DFA cache exceeds the value of the config. DFA states empirically consume about
+ |`AbstractParser.BYTES_PER_DFA_STATE` bytes of memory each.
+ |
+ |ANTLR parsers retain a DFA cache designed to speed up parsing future input. However,
+ |there is no limit to how large this cache can become. Parsing large SQL statements can
+ |lead to an accumulation of objects in the cache that are unlikely to be reused, causing
+ |high GC overhead and eventually OOMs.
+ |
+ |If this config is set to a negative value, it is ignored.
+ |If both this config and `spark.sql.parser.parserDfaCacheFlushRatio` are set, the
+ |cache is flushed if either condition is met.
+ |Requires `spark.sql.parser.manageParserCachesKillSwitch` to be true to take effect.
+ |
+ |Can significantly slow down parsing in exchange for better memory stability.
+ |""".stripMargin)
+ .version("4.0.0")
+ .intConf
+ .createWithDefault(-1)
+
+ val MANAGE_PARSER_CACHES =
+ buildConf("spark.sql.parser.manageParserCachesKillSwitch")
+ .internal()
+ .doc(
+ """When true, we install our own ANTLR caches to manage memory usage. When false, we use the
+ |default ANTLR caches. Dependency for
+ |`spark.sql.parser.parserDfaCacheFlushThreshold`.""".stripMargin)
+ .version("4.0.0")
+ .booleanConf
+ .createWithDefault(true)
Review Comment:
`true` for testing, but plan to ship default-off.
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-47404][SQL] Add configurable size limits for ANTLR DFA cache [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #51069:
URL: https://github.com/apache/spark/pull/51069#issuecomment-3073583298
The AQE and pyspark test failures are unrelated, thanks, merging to master!
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-47404][SQL] Add configurable size limits for ANTLR DFA cache [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan closed pull request #51069: [SPARK-47404][SQL] Add configurable size limits for ANTLR DFA cache
URL: https://github.com/apache/spark/pull/51069
--
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: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org