martijnvg commented on code in PR #14672: URL: https://github.com/apache/lucene/pull/14672#discussion_r2121019535
########## lucene/core/src/java/org/apache/lucene/search/comparators/NumericComparator.java: ########## @@ -328,120 +507,47 @@ private void updateSkipInterval(boolean success) { } } } + } - /** - * If {@link NumericComparator#pruning} equals {@link Pruning#GREATER_THAN_OR_EQUAL_TO}, we - * could better tune the {@link NumericLeafComparator#maxValueAsLong}/{@link - * NumericLeafComparator#minValueAsLong}. For instance, if the sort is ascending and bottom - * value is 5, we will use a range on [MIN_VALUE, 4]. - */ - private void encodeBottom() { - if (reverse == false) { - maxValueAsLong = bottomAsComparableLong(); - if (pruning == Pruning.GREATER_THAN_OR_EQUAL_TO && maxValueAsLong != Long.MIN_VALUE) { - maxValueAsLong--; - } - } else { - minValueAsLong = bottomAsComparableLong(); - if (pruning == Pruning.GREATER_THAN_OR_EQUAL_TO && minValueAsLong != Long.MAX_VALUE) { - minValueAsLong++; - } - } - } - - /** - * If {@link NumericComparator#pruning} equals {@link Pruning#GREATER_THAN_OR_EQUAL_TO}, we - * could better tune the {@link NumericLeafComparator#minValueAsLong}/{@link - * NumericLeafComparator#minValueAsLong}. For instance, if the sort is ascending and top value - * is 3, we will use a range on [4, MAX_VALUE]. - */ - private void encodeTop() { - if (reverse == false) { - minValueAsLong = topAsComparableLong(); - if (singleSort - && pruning == Pruning.GREATER_THAN_OR_EQUAL_TO - && queueFull - && minValueAsLong != Long.MAX_VALUE) { - minValueAsLong++; - } - } else { - maxValueAsLong = topAsComparableLong(); - if (singleSort - && pruning == Pruning.GREATER_THAN_OR_EQUAL_TO - && queueFull - && maxValueAsLong != Long.MIN_VALUE) { - maxValueAsLong--; - } - } - } + private class DVSkipperCompetitiveDISIBuilder extends CompetitiveDISIBuilder { - private boolean isMissingValueCompetitive() { - // if queue is full, compare with bottom first, - // if competitive, then check if we can compare with topValue - if (queueFull) { - int result = Long.compare(missingValueAsLong, bottomAsComparableLong()); - // in reverse (desc) sort missingValue is competitive when it's greater or equal to bottom, - // in asc sort missingValue is competitive when it's smaller or equal to bottom - final boolean competitive = - reverse - ? (pruning == Pruning.GREATER_THAN_OR_EQUAL_TO ? result > 0 : result >= 0) - : (pruning == Pruning.GREATER_THAN_OR_EQUAL_TO ? result < 0 : result <= 0); - if (competitive == false) { - return false; - } - } + private final DocValuesSkipper skipper; + private final TwoPhaseIterator innerTwoPhase; - if (leafTopSet) { - int result = Long.compare(missingValueAsLong, topAsComparableLong()); - // in reverse (desc) sort missingValue is competitive when it's smaller or equal to - // topValue, - // in asc sort missingValue is competitive when it's greater or equal to topValue - return reverse ? (result <= 0) : (result >= 0); - } + public DVSkipperCompetitiveDISIBuilder( + DocValuesSkipper skipper, NumericLeafComparator leafComparator) throws IOException { + super(leafComparator); + this.skipper = skipper; + NumericDocValues docValues = + leafComparator.getNumericDocValues(leafComparator.context, field); + innerTwoPhase = + new TwoPhaseIterator(docValues) { + @Override + public boolean matches() throws IOException { + final long value = docValues.longValue(); + return value >= minValueAsLong && value <= maxValueAsLong; + } - // by default competitive - return true; + @Override + public float matchCost() { + return 2; // 2 comparisons + } + }; } @Override - public DocIdSetIterator competitiveIterator() { - if (enableSkipping == false) return null; - return new AbstractDocIdSetIterator() { - - { - doc = competitiveIterator.docID(); - } - - @Override - public int nextDoc() throws IOException { - return advance(doc + 1); - } - - @Override - public long cost() { - return competitiveIterator.cost(); - } - - @Override - public int advance(int target) throws IOException { - return doc = competitiveIterator.advance(target); - } - - @Override - public void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException { - // The competitive iterator is usually a BitSetIterator, which has an optimized - // implementation of #intoBitSet. - if (competitiveIterator.docID() < doc) { - competitiveIterator.advance(doc); - } - competitiveIterator.intoBitSet(upTo, bitSet, offset); - doc = competitiveIterator.docID(); - } - }; + int docCount() { + return skipper.docCount(); } - protected abstract long bottomAsComparableLong(); - - protected abstract long topAsComparableLong(); + @Override + void doUpdateCompetitiveIterator() { + TwoPhaseIterator twoPhaseIterator = + new DocValuesRangeIterator(innerTwoPhase, skipper, minValueAsLong, maxValueAsLong, false); + // TODO this twoPhaseIterator is wrapped by #competitiveIterator() so caller can not use Review Comment: I don't think this can be addressed, as long as `LeafFieldComparator#competitiveIterator()` has `DocIdSetIterator` as return type? -- 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