Copilot commented on code in PR #14404:
URL: https://github.com/apache/lucene/pull/14404#discussion_r2046173651


##########
lucene/sandbox/src/java/org/apache/lucene/sandbox/search/SortedNumericDocValuesMultiRangeQuery.java:
##########
@@ -0,0 +1,274 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.sandbox.search;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NavigableSet;
+import java.util.Objects;
+import java.util.TreeSet;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.index.DocValuesSkipper;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.SortedNumericDocValues;
+import org.apache.lucene.search.ConstantScoreScorerSupplier;
+import org.apache.lucene.search.ConstantScoreWeight;
+import org.apache.lucene.search.DocValuesRangeIterator;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.QueryVisitor;
+import org.apache.lucene.search.ScoreMode;
+import org.apache.lucene.search.ScorerSupplier;
+import org.apache.lucene.search.TwoPhaseIterator;
+import org.apache.lucene.search.Weight;
+import org.apache.lucene.util.PriorityQueue;
+
+/**
+ * A union multiple ranges over SortedNumericDocValuesField
+ *
+ * @lucene.experimental
+ */
+public class SortedNumericDocValuesMultiRangeQuery extends Query {
+
+  protected final String fieldName;
+  protected final NavigableSet<DocValuesMultiRangeQuery.LongRange> 
sortedClauses;
+
+  protected SortedNumericDocValuesMultiRangeQuery(
+      String fieldName, List<DocValuesMultiRangeQuery.LongRange> clauses) {
+    this.fieldName = fieldName;
+    sortedClauses = resolveOverlaps(clauses);
+  }
+
+  private static final class Edge {
+    private final DocValuesMultiRangeQuery.LongRange range;
+    private final boolean point;
+    private final boolean upper;
+
+    private static Edge createPoint(DocValuesMultiRangeQuery.LongRange r) {
+      return new Edge(r);
+    }
+
+    long getValue() {
+      return upper ? range.upper : range.lower;
+    }
+
+    private Edge(DocValuesMultiRangeQuery.LongRange range, boolean upper) {
+      this.range = range;
+      this.upper = upper;
+      this.point = false;
+    }
+
+    /** expecting Arrays.equals(lower.bytes,upper.bytes) i.e. point */
+    private Edge(DocValuesMultiRangeQuery.LongRange range) {
+      this.range = range;
+      this.upper = false;
+      this.point = true;
+    }
+  }
+
+  /** Merges overlapping ranges. map.floor() doesn't work with overlaps */
+  private static NavigableSet<DocValuesMultiRangeQuery.LongRange> 
resolveOverlaps(
+      Collection<DocValuesMultiRangeQuery.LongRange> clauses) {
+    NavigableSet<DocValuesMultiRangeQuery.LongRange> sortedClauses =
+        new TreeSet<>(
+            Comparator.comparing(r -> r.lower)
+            // .thenComparing(r -> r.upper)// have to ignore upper boundary 
for .floor() lookups
+            );
+    PriorityQueue<Edge> heap =
+        new PriorityQueue<>(clauses.size() * 2) {
+          @Override
+          protected boolean lessThan(Edge a, Edge b) {
+            return a.getValue() - b.getValue() < 0;

Review Comment:
   Using subtraction to compare long values can lead to overflow issues; 
consider using 'Long.compare(a.getValue(), b.getValue()) < 0' for clarity and 
safety.
   ```suggestion
               return Long.compare(a.getValue(), b.getValue()) < 0;
   ```



##########
lucene/sandbox/src/java/org/apache/lucene/sandbox/search/DocValuesMultiRangeQuery.java:
##########
@@ -114,4 +151,39 @@ protected Query createSortedSetDocValuesMultiRangeQuery() {
       return new SortedSetDocValuesMultiRangeQuery(fieldName, clauses);
     }
   }
+
+  /**
+   * Builder for creating a multi-range query for stabbing by SortedNumerics 
or Numerics field
+   * values. For the single range it behaves like {@link
+   * SortedNumericDocValuesField#newSlowRangeQuery(String, long, long)}
+   */
+  public static class SortedNumericStabbingBuilder {
+    protected final String fieldName;
+    protected final List<LongRange> clauses = new ArrayList<>();
+
+    public SortedNumericStabbingBuilder(String fieldName) {
+      this.fieldName = Objects.requireNonNull(fieldName);
+    }
+
+    public SortedNumericStabbingBuilder add(long lowerValue, long upperValue) {
+      clauses.add(new LongRange(lowerValue, upperValue));
+      return this;
+    }
+
+    public Query build() {
+      if (clauses.isEmpty()) {
+        return new MatchNoDocsQuery();
+      }
+      if (clauses.size() == 1) {
+        LongRange theOnlyOne = clauses.getFirst();

Review Comment:
   The method 'getFirst()' is not available on a List (e.g. ArrayList). Replace 
it with 'clauses.get(0)'.
   ```suggestion
           LongRange theOnlyOne = clauses.get(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: issues-unsubscr...@lucene.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to