You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "xiaoh1024 (via GitHub)" <gi...@apache.org> on 2026/09/21 09:57:54 UTC
[PR] [SPARK-31760][SQL] Simplify contained integral range predicates [spark]
xiaoh1024 opened a new pull request, #58940:
URL: https://github.com/apache/spark/pull/58940
### What changes were proposed in this pull request?
Extend `BooleanSimplification` to remove redundant integral range comparisons:
```sql
a > 5 AND a > 0 --> a > 5
a > 1 OR a > 2 --> a > 1
a > 1 OR (a > 2 AND a < 4) --> a > 1
```
Support `<`, `<=`, `>`, and `>=` between direct Byte/Short/Int/Long attributes and non-null literals of the same type, including reversed operands and one level of nested absorption. Bounds are compared without arithmetic to avoid overflow. Casts and arbitrary expressions are excluded.
### Why are the changes needed?
The existing rule retains these redundant comparisons. Removing them reduces per-row evaluation. This implements the integral range subset of [SPARK-31760](https://issues.apache.org/jira/browse/SPARK-31760).
### Does this PR introduce _any_ user-facing change?
Yes: eligible queries have simpler optimized plans and may execute faster. Query results, including SQL NULL semantics, are preserved. No public API or configuration changes.
### How was this patch tested?
Added Catalyst and SQL tests covering operand order, inclusive/exclusive bounds, nested absorption, NULLs, integer extremes, unsupported expressions, and generated evaluation. The new contained-range regression test fails with the original rule.
On Spark 5.0.0-SNAPSHOT with Scala 2.13.18 and JDK 17.0.18:
- 37 Catalyst tests passed across the new suite and the existing BooleanSimplification and BinaryComparisonSimplification suites.
- The SQL suite passed all 16 filter/projection and AQE on/off combinations.
- Catalyst source/test and SQL test scalastyle checks passed.
Local synthetic-data benchmarks used 30 million cached rows and `local[2]`, covering filter/projection queries with 0% and 10% nulls. AND placed the wider condition first, OR placed the narrower condition first, and nested absorption placed the complex branch first. Four JVMs ran in baseline/candidate/candidate/baseline order. Median query time decreased by 3–5% for AND, 30–34% for OR, and 35–43% for nested absorption. A separate rule microbenchmark measured up to 3.2 microseconds additional time per 32-expression projection (about 8%).
### Was this patch authored or co-authored using generative AI tooling?
Yes.
--
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-31760][SQL] Simplify contained integral range predicates [spark]
Posted by "uros-b (via GitHub)" <gi...@apache.org>.
uros-b commented on PR #58940:
URL: https://github.com/apache/spark/pull/58940#issuecomment-5783114251
@xiaoh1024 please let me know your JIRA username, so that I can attribute the ticket to you: https://issues.apache.org/jira/browse/SPARK-31760
--
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-31760][SQL] Simplify contained integral range predicates [spark]
Posted by "stevomitric (via GitHub)" <gi...@apache.org>.
stevomitric commented on code in PR #58940:
URL: https://github.com/apache/spark/pull/58940#discussion_r4071528835
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -564,6 +570,70 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
case _ => not
}
+
+ private case class IntegralRange(
+ attribute: AttributeReference,
+ bound: Long,
+ isLowerBound: Boolean,
+ inclusive: Boolean)
+
+ private object IntegralComparison {
+ def unapply(expression: Expression): Option[IntegralRange] = {
+ def range(
+ attribute: AttributeReference,
+ literal: Literal,
+ isLowerBound: Boolean,
+ inclusive: Boolean): Option[IntegralRange] = {
+ if (attribute.dataType.isInstanceOf[IntegralType] &&
+ literal.dataType == attribute.dataType && literal.value != null) {
+ Some(IntegralRange(attribute, literal.value.asInstanceOf[Number].longValue(),
+ isLowerBound, inclusive))
+ } else {
+ None
+ }
+ }
+
+ expression match {
+ case GreaterThan(a: AttributeReference, l: Literal) => range(a, l, true, false)
Review Comment:
Does constant folding run before this rule? Asking because matching against literal might be too restrictive, i.e. if you have an expression "2+2" which would be Add(Literal, Literal).
--
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-31760][SQL] Simplify contained integral range predicates [spark]
Posted by "uros-b (via GitHub)" <gi...@apache.org>.
uros-b commented on PR #58940:
URL: https://github.com/apache/spark/pull/58940#issuecomment-5783091119
**Merge Summary:**
- merged into master https://github.com/apache/spark/commit/9d84a6c1185d6957d9e7ccb99ce25a92862cd436
- merged into branch-4.x https://github.com/apache/spark/commit/5944b823b85544648fdd20b4848c5505b93bbb07
*Posted by `merge_spark_pr.py`*
--
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-31760][SQL] Simplify contained integral range predicates [spark]
Posted by "uros-b (via GitHub)" <gi...@apache.org>.
uros-b closed pull request #58940: [SPARK-31760][SQL] Simplify contained integral range predicates
URL: https://github.com/apache/spark/pull/58940
--
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-31760][SQL] Simplify contained integral range predicates [spark]
Posted by "xiaoh1024 (via GitHub)" <gi...@apache.org>.
xiaoh1024 commented on PR #58940:
URL: https://github.com/apache/spark/pull/58940#issuecomment-5788262572
> @xiaoh1024 please let me know your JIRA username, so that I can attribute the ticket to you: https://issues.apache.org/jira/browse/SPARK-31760
My JIRA username is `xiaoh0425`. Thank you!
--
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-31760][SQL] Simplify contained integral range predicates [spark]
Posted by "uros-b (via GitHub)" <gi...@apache.org>.
uros-b commented on PR #58940:
URL: https://github.com/apache/spark/pull/58940#issuecomment-5801847289
Thank you @xiaoh1024, assigned the ticket to you!
--
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-31760][SQL] Simplify contained integral range predicates [spark]
Posted by "xiaoh1024 (via GitHub)" <gi...@apache.org>.
xiaoh1024 commented on code in PR #58940:
URL: https://github.com/apache/spark/pull/58940#discussion_r4071783806
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/expressions.scala:
##########
@@ -564,6 +570,70 @@ object BooleanSimplification extends Rule[LogicalPlan] with PredicateHelper {
case _ => not
}
+
+ private case class IntegralRange(
+ attribute: AttributeReference,
+ bound: Long,
+ isLowerBound: Boolean,
+ inclusive: Boolean)
+
+ private object IntegralComparison {
+ def unapply(expression: Expression): Option[IntegralRange] = {
+ def range(
+ attribute: AttributeReference,
+ literal: Literal,
+ isLowerBound: Boolean,
+ inclusive: Boolean): Option[IntegralRange] = {
+ if (attribute.dataType.isInstanceOf[IntegralType] &&
+ literal.dataType == attribute.dataType && literal.value != null) {
+ Some(IntegralRange(attribute, literal.value.asInstanceOf[Number].longValue(),
+ isLowerBound, inclusive))
+ } else {
+ None
+ }
+ }
+
+ expression match {
+ case GreaterThan(a: AttributeReference, l: Literal) => range(a, l, true, false)
Review Comment:
Yes. [ConstantFolding runs before BooleanSimplification](https://github.com/apache/spark/blob/ac4006aed0fde4060461f42888adcfbe03eb2321/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala#L139-L143) in the operator optimization batches, so `2 + 2` is folded to `Literal(4)` before reaching this matcher.
I have added relevant tests.
--
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-31760][SQL] Simplify contained integral range predicates [spark]
Posted by "uros-b (via GitHub)" <gi...@apache.org>.
uros-b commented on PR #58940:
URL: https://github.com/apache/spark/pull/58940#issuecomment-5783107997
Thank you @xiaoh1024 and @stevomitric!
--
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