xiangfu0 commented on code in PR #19894:
URL: https://github.com/apache/pinot/pull/19894#discussion_r4623086097


##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/readers/vector/LuceneHnswRuntimeControlUtils.java:
##########
@@ -111,65 +174,48 @@ private static final class 
RuntimeControlledKnnCollectorManager implements KnnCo
     }
 
     @Override
-    public KnnCollector newCollector(int visitLimit, LeafReaderContext 
context) {
-      int effectiveVisitLimit = _efSearch > 0 ? Math.min(visitLimit, 
_efSearch) : visitLimit;
-      if (_useBoundedQueue) {
-        AbstractKnnCollector leafCollector = new TopKnnCollector(_k, 
effectiveVisitLimit);
-        KnnCollector collector = _globalScoreQueue != null
-            ? new MultiLeafKnnCollector(_k, _globalScoreQueue, leafCollector)
-            : leafCollector;
-        return _useRelativeDistance ? collector : new 
RelativeDistanceDisabledKnnCollector(collector);
-      }
-      return new UnboundedKnnCollector(_k, effectiveVisitLimit, 
_useRelativeDistance);
-    }
-  }
-
-  private static final class RelativeDistanceDisabledKnnCollector implements 
KnnCollector {
-    private final KnnCollector _delegate;
-
-    RelativeDistanceDisabledKnnCollector(KnnCollector delegate) {
-      _delegate = delegate;
-    }
-
-    @Override
-    public boolean earlyTerminated() {
-      return _delegate.earlyTerminated();
-    }
-
-    @Override
-    public void incVisitedCount(int count) {
-      _delegate.incVisitedCount(count);
+    public KnnCollector newCollector(int visitLimit, KnnSearchStrategy 
searchStrategy, LeafReaderContext context) {
+      // A reentrant pass is seeded with the first pass's candidates. Retain 
that leaf quota when it exceeds k.
+      int leafK = searchStrategy instanceof KnnSearchStrategy.Seeded seeded
+          ? Math.max(_k, seeded.numberOfEntryPoints()) : _k;
+      return newCollector(visitLimit, searchStrategy, leafK);
     }
 
     @Override
-    public long visitedCount() {
-      return _delegate.visitedCount();
+    public KnnCollector newOptimisticCollector(int visitLimit, 
KnnSearchStrategy searchStrategy,
+        LeafReaderContext context, int leafK) {
+      return newCollector(visitLimit, searchStrategy, leafK);
     }
 
     @Override
-    public long visitLimit() {
-      return _delegate.visitLimit();
+    public boolean isOptimistic() {
+      // Lucene checks its leaf quota before accepting filtered ANN results. A 
seeded second pass can
+      // score all entry points before checking its visit limit, so keep it 
disabled for explicit budgets.
+      return _hasFilter && _efSearch <= 0;

Review Comment:
   Fixed in 
[5eced15247](https://github.com/apache/pinot/pull/19894/commits/5eced15247424b2414914641f532eb8aa00a074f).
 A filtered query with explicit `efSearch` now uses at least Lucene 10.6's 
per-leaf candidate quota on its single ANN pass. The regression verifies that a 
high positive budget completes once per leaf with zero exact scans and that an 
exhausted budget still falls back to exact search.



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/utils/LuceneRegexpAutomaton.java:
##########
@@ -0,0 +1,126 @@
+/**
+ * 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.pinot.segment.local.utils;
+
+import org.apache.lucene.util.automaton.Automaton;
+import org.apache.lucene.util.automaton.Operations;
+import org.apache.lucene.util.automaton.RegExp;
+
+
+/// Bounds Lucene regular expression construction and compiles deterministic 
automata for the FST matchers.
+/// All compilation state is local to the caller.
+public final class LuceneRegexpAutomaton {
+  private static final int MAX_CONSTRUCTION_WORK = 
Operations.DEFAULT_DETERMINIZE_WORK_LIMIT;
+
+  private LuceneRegexpAutomaton() {
+  }
+
+  @SuppressWarnings("deprecation") // Preserve the complement syntax supported 
by existing FST queries.
+  public static Automaton compile(String expression) {
+    RegExp regex = new RegExp(expression, RegExp.ALL | 
RegExp.DEPRECATED_COMPLEMENT);
+    validateConstructionBudget(regex);
+    return Operations.determinize(regex.toAutomaton(), 
Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
+  }
+
+  /// Checks a pattern using Lucene text query syntax before building its 
automaton.
+  public static void validateConstructionBudget(String expression) {
+    validateConstructionBudget(new RegExp(expression, RegExp.ALL));
+  }
+
+  private static void validateConstructionBudget(RegExp regex) {
+    // Lucene expands counted repetitions before determinizing. Estimate that 
work on the parsed tree first.
+    estimate(regex);
+  }
+
+  private static ConstructionCost estimate(RegExp regex) {
+    return switch (regex.kind) {
+      case REGEXP_REPEAT_MIN, REGEXP_REPEAT_MINMAX -> 
estimateRepetition(regex);
+      case REGEXP_CONCATENATION, REGEXP_UNION, REGEXP_INTERSECTION -> {
+        ConstructionCost left = estimate(regex.exp1);
+        ConstructionCost right = estimate(regex.exp2);
+        long work = regex.kind == RegExp.Kind.REGEXP_INTERSECTION
+            ? left.work() * right.work() : left.work() + right.work();
+        boolean acceptsEmpty = regex.kind == RegExp.Kind.REGEXP_UNION
+            ? left.acceptsEmpty() || right.acceptsEmpty()
+            : left.acceptsEmpty() && right.acceptsEmpty();
+        yield new ConstructionCost(work, acceptsEmpty);
+      }
+      case REGEXP_OPTIONAL, REGEXP_REPEAT -> new 
ConstructionCost(estimate(regex.exp1).work() + 1, true);

Review Comment:
   Fixed in 
[5eced15247](https://github.com/apache/pinot/pull/19894/commits/5eced15247424b2414914641f532eb8aa00a074f).
 The construction budget now charges for copying the operand's initial 
transitions to its nonterminal accepting states before Lucene constructs the 
outer repetition. That includes the implicit star for `+` and open-ended 
counts. A small regression with a reduced work limit verifies that the guard 
rejects the expansion.



-- 
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: [email protected]

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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to