You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "joooger (via GitHub)" <gi...@apache.org> on 2026/10/19 11:29:30 UTC

[PR] [CALCITE-7867] RexSimplify takes cubic time on long IN and NOT IN lists [calcite]

joooger opened a new pull request, #5343:
URL: https://github.com/apache/calcite/pull/5343

   ## Jira Link
   
   [CALCITE-7867](https://issues.apache.org/jira/browse/CALCITE-7867)
   
   ## Changes Proposed
   
   `RexSimplify.residue` applied each `<>` predicate with `RangeSets.minus`, which copies the whole range set. `simplifyOrTerms` and `simplifyAndTerms` pass one predicate per preceding term, so simplifying `x = c1 OR ... OR x = cN` on a NOT NULL `x`, or `x <> c1 AND ... AND x <> cN` as a filter, took O(N^3) time: about 425 s and 475 s for N = 2,000.
   
   `residue` now narrows one mutable `TreeRangeSet` in place and returns an immutable copy, which makes both O(N^2 log N): about 1.4 s and 1.2 s for N = 2,000. The returned range set is the same as before.
   
   Tests: `RexProgramTest.testSimplifyLargeOrOfEqualitiesOnNotNullOperand` and `testSimplifyLargeAndOfNotEqualsAsFilter`. Without the fix each one times out after 5 minutes.
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7867] RexSimplify takes cubic time on long IN and NOT IN lists [calcite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #5343:
URL: https://github.com/apache/calcite/pull/5343#issuecomment-6268744468

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=5343) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=5343&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=5343&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=5343&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=5343&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=5343&metric=new_duplicated_lines_density&view=list)  
     
   <!-- sqra-placement-anchor -->
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=5343)
   
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7867] RexSimplify takes cubic time on long IN and NOT IN lists [calcite]

Posted by "asolimando (via GitHub)" <gi...@apache.org>.
asolimando commented on PR #5343:
URL: https://github.com/apache/calcite/pull/5343#issuecomment-6336237984

   Discussion in Jira is over and everyone agrees in getting this contribution in, merging now!


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7867] RexSimplify takes cubic time on long IN and NOT IN lists [calcite]

Posted by "joooger (via GitHub)" <gi...@apache.org>.
joooger commented on code in PR #5343:
URL: https://github.com/apache/calcite/pull/5343#discussion_r4316985606


##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -2330,20 +2333,20 @@ private static <C extends Comparable<C>> RangeSet<C> residue(RexNode ref,
             final RangeSet<C> notEqualsRangeSet =
                 ImmutableRangeSet.of(pointRange).complement();
             if (result.enclosesAll(notEqualsRangeSet)) {
-              result = RangeSets.rangeSetAll();
+              result.add(Range.all());
               continue;
             }
-            result = RangeSets.minus(result, pointRange);
+            result.remove(pointRange);
             break;
           default:
             final Range<C> r1 = range(comparison.kind, c1);
             if (result.encloses(r1)) {
               // Given these predicates, term is always satisfied.
               // e.g. r0 is "$0 < 10", r1 is "$0 < 5"
-              result = RangeSets.rangeSetAll();
+              result.add(Range.all());
               continue;
             }
-            result = result.subRangeSet(r1);
+            result.removeAll(ImmutableRangeSet.of(r1).complement());
           }
           if (result.isEmpty()) {
             break; // short-cut

Review Comment:
   Good catch, the `break` only left the switch. Done in e2da6d205: it now returns an empty range set, which no later predicate can change.



##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -2330,20 +2333,20 @@ private static <C extends Comparable<C>> RangeSet<C> residue(RexNode ref,
             final RangeSet<C> notEqualsRangeSet =
                 ImmutableRangeSet.of(pointRange).complement();
             if (result.enclosesAll(notEqualsRangeSet)) {
-              result = RangeSets.rangeSetAll();
+              result.add(Range.all());

Review Comment:
   Done in e2da6d205. Once the result is all values, every later predicate is enclosed by it, so returning immediately gives the same result.



##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -2330,20 +2333,20 @@ private static <C extends Comparable<C>> RangeSet<C> residue(RexNode ref,
             final RangeSet<C> notEqualsRangeSet =
                 ImmutableRangeSet.of(pointRange).complement();
             if (result.enclosesAll(notEqualsRangeSet)) {
-              result = RangeSets.rangeSetAll();
+              result.add(Range.all());
               continue;
             }
-            result = RangeSets.minus(result, pointRange);
+            result.remove(pointRange);
             break;
           default:
             final Range<C> r1 = range(comparison.kind, c1);
             if (result.encloses(r1)) {
               // Given these predicates, term is always satisfied.
               // e.g. r0 is "$0 < 10", r1 is "$0 < 5"
-              result = RangeSets.rangeSetAll();
+              result.add(Range.all());

Review Comment:
   Done in e2da6d205.



##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -2305,7 +2305,10 @@ private <C extends Comparable<C>> RexNode simplifyUsingPredicates(RexNode e,
    */
   private static <C extends Comparable<C>> RangeSet<C> residue(RexNode ref,
       RangeSet<C> r0, List<RexNode> predicates, Class<C> clazz) {
-    RangeSet<C> result = r0;
+    // Narrow a single mutable range set in place. Copying the range set for

Review Comment:
   Agreed, reworded in e2da6d205. The comment now also mentions simplifyAndTerms, since NOT IN in a filter goes through the AND path.



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7867] RexSimplify takes cubic time on long IN and NOT IN lists [calcite]

Posted by "asolimando (via GitHub)" <gi...@apache.org>.
asolimando commented on code in PR #5343:
URL: https://github.com/apache/calcite/pull/5343#discussion_r4314534849


##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -2330,20 +2333,20 @@ private static <C extends Comparable<C>> RangeSet<C> residue(RexNode ref,
             final RangeSet<C> notEqualsRangeSet =
                 ImmutableRangeSet.of(pointRange).complement();
             if (result.enclosesAll(notEqualsRangeSet)) {
-              result = RangeSets.rangeSetAll();
+              result.add(Range.all());
               continue;
             }
-            result = RangeSets.minus(result, pointRange);
+            result.remove(pointRange);
             break;
           default:
             final Range<C> r1 = range(comparison.kind, c1);
             if (result.encloses(r1)) {
               // Given these predicates, term is always satisfied.
               // e.g. r0 is "$0 < 10", r1 is "$0 < 5"
-              result = RangeSets.rangeSetAll();
+              result.add(Range.all());
               continue;
             }
-            result = result.subRangeSet(r1);
+            result.removeAll(ImmutableRangeSet.of(r1).complement());
           }
           if (result.isEmpty()) {
             break; // short-cut

Review Comment:
   I know you haven't touched this line, but I think we can now short-circuit to `return ImmutableRangeSet.of(); // short-cut`. I'd do it here as the `break` exists the switch statement, not the for, so this is/was broken and it's a simple fix



##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -2330,20 +2333,20 @@ private static <C extends Comparable<C>> RangeSet<C> residue(RexNode ref,
             final RangeSet<C> notEqualsRangeSet =
                 ImmutableRangeSet.of(pointRange).complement();
             if (result.enclosesAll(notEqualsRangeSet)) {
-              result = RangeSets.rangeSetAll();
+              result.add(Range.all());

Review Comment:
   Here we can do `return RangeSets.rangeSetAll();`, as all additions are no-op and removals aren't reachable anymore



##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -2305,7 +2305,10 @@ private <C extends Comparable<C>> RexNode simplifyUsingPredicates(RexNode e,
    */
   private static <C extends Comparable<C>> RangeSet<C> residue(RexNode ref,
       RangeSet<C> r0, List<RexNode> predicates, Class<C> clazz) {
-    RangeSet<C> result = r0;
+    // Narrow a single mutable range set in place. Copying the range set for

Review Comment:
   Nit: "quadratic" here is per residue call, the overall cost is cubic (the JIRA title) because simplifyAndTerms/simplifyOrTerms call it once per term with a growing predicate list.
   
   Maybe: “Copying the range set for every predicate is quadratic per call, cubic over the whole AND/OR”.
   
   WDYT?



##########
core/src/main/java/org/apache/calcite/rex/RexSimplify.java:
##########
@@ -2330,20 +2333,20 @@ private static <C extends Comparable<C>> RangeSet<C> residue(RexNode ref,
             final RangeSet<C> notEqualsRangeSet =
                 ImmutableRangeSet.of(pointRange).complement();
             if (result.enclosesAll(notEqualsRangeSet)) {
-              result = RangeSets.rangeSetAll();
+              result.add(Range.all());
               continue;
             }
-            result = RangeSets.minus(result, pointRange);
+            result.remove(pointRange);
             break;
           default:
             final Range<C> r1 = range(comparison.kind, c1);
             if (result.encloses(r1)) {
               // Given these predicates, term is always satisfied.
               // e.g. r0 is "$0 < 10", r1 is "$0 < 5"
-              result = RangeSets.rangeSetAll();
+              result.add(Range.all());

Review Comment:
   Same here we can `return RangeSets.rangeSetAll();`



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7867] RexSimplify takes cubic time on long IN and NOT IN lists [calcite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #5343:
URL: https://github.com/apache/calcite/pull/5343#issuecomment-6273947143

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=5343) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=5343&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=5343&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=5343&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=5343&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=5343&metric=new_duplicated_lines_density&view=list)  
     
   <!-- sqra-placement-anchor -->
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=5343)
   
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7867] RexSimplify takes cubic time on long IN and NOT IN lists [calcite]

Posted by "sonarqubecloud[bot] (via GitHub)" <gi...@apache.org>.
sonarqubecloud[bot] commented on PR #5343:
URL: https://github.com/apache/calcite/pull/5343#issuecomment-6290550503

   ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=5343) **Quality Gate passed**  
   Issues  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=5343&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=apache_calcite&pullRequest=5343&issueStatuses=ACCEPTED)
   
   Measures  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=apache_calcite&pullRequest=5343&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=5343&metric=new_coverage&view=list)  
   ![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=apache_calcite&pullRequest=5343&metric=new_duplicated_lines_density&view=list)  
     
   <!-- sqra-placement-anchor -->
   [See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=apache_calcite&pullRequest=5343)
   
   


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [CALCITE-7867] RexSimplify takes cubic time on long IN and NOT IN lists [calcite]

Posted by "asolimando (via GitHub)" <gi...@apache.org>.
asolimando merged PR #5343:
URL: https://github.com/apache/calcite/pull/5343


-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org