julianhyde commented on a change in pull request #2124:
URL: https://github.com/apache/calcite/pull/2124#discussion_r480772730



##########
File path: core/src/main/java/org/apache/calcite/util/RangeSets.java
##########
@@ -43,4 +49,384 @@ private RangeSets() {}
   public static <C extends Comparable<C>> RangeSet<C> rangeSetAll() {
     return (RangeSet) ALL;
   }
+
+  /** Compares two range sets. */
+  public static <C extends Comparable<C>> int compare(RangeSet<C> s0,
+      RangeSet<C> s1) {
+    final Iterator<Range<C>> i0 = s0.asRanges().iterator();
+    final Iterator<Range<C>> i1 = s1.asRanges().iterator();
+    for (;;) {
+      final boolean h0 = i0.hasNext();
+      final boolean h1 = i1.hasNext();
+      if (!h0 || !h1) {
+        return Boolean.compare(h0, h1);
+      }
+      final Range<C> r0 = i0.next();
+      final Range<C> r1 = i1.next();
+      int c = compare(r0, r1);
+      if (c != 0) {
+        return c;
+      }
+    }
+  }
+
+  /** Compares two ranges. */
+  public static <C extends Comparable<C>> int compare(Range<C> r0,
+      Range<C> r1) {
+    int c = Boolean.compare(r0.hasLowerBound(), r1.hasLowerBound());
+    if (c != 0) {
+      return c;
+    }
+    if (r0.hasLowerBound()) {
+      c = r0.lowerEndpoint().compareTo(r1.lowerEndpoint());

Review comment:
       This code is not terribly important. We need a `Sarg` to be `Comparable` 
only because we have an existing rule that all values inside literals must be 
`Comparable`.
   
   Also, this code is just re-implementing the comparison that Guava uses for 
`Range`. In Guava, Ranges are not themselves discrete, and therefore `(5, 10)` 
is a different range to `[6, 9]` even though they contain the same values over 
`int`; in Guava you have to canonize for a particular `DiscreteDomain`.
   
   Still, thanks for bringing it up. I'll keep your example in mind as we go 
forward. Especially if we start to canonize `Sarg`s or lean more heavily on 
`Sarg`s being comparable.




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to