russcam opened a new issue, #17042:
URL: https://github.com/apache/lucene/issues/17042
### Description
Since 10.6.0, `NumericDocValuesField.newSlowRangeQuery` and
`SortedNumericDocValuesField.newSlowRangeQuery` miss matching documents on a
field with a skip index (`indexedField`) whose values span 2^63 or more, for
example a field that holds both `Long.MIN_VALUE` and values near
`Long.MAX_VALUE`. 10.5.2 returns the right count.
The cause is the bound transform of #16160 and #16285 in
`Lucene90DocValuesProducer`.
`rangeGcdDeltaIntoBitSet` maps the query bounds into the domain of the
packed values (`stored = raw * mul + delta`) and runs the range scan on the
packed values directly. `transformGcdBounds` clamps the lower bound with
`encodedMin = Math.max(0, encodedMin)`, which assumes that packed values are
non-negative. When the values span 2^63 or more, `max - min` overflows, the
writer stores `value - min` with wrapping in 64 bits per value, and every
packed value from 2^63 onwards reads back as a negative `long`. The scan on the
encoded domain then skips those documents.
The skip index is where this occurs: `DocValuesRangeIterator` calls
`rangeIntoBitSet` for its MAYBE blocks.
Without a skip index the same queries count correctly, because each document
is checked through `LongValues.get`.
### Reproduction
Here's a reproduction, run against `lucene-core` on JDK 25:
```java
import org.apache.lucene.document.Document;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.ByteBuffersDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Version;
/**
* Doc values range queries drop matches when a field's values span 2^63 or
more.
*
* <p>Even docs hold Long.MIN_VALUE and odd docs Long.MAX_VALUE - doc, so
max - min overflows and
* the field is delta-encoded with 64 bits per value. The range
[Long.MIN_VALUE, Long.MAX_VALUE -
* 100] matches every even doc (5,000) and the odd docs from 101 on (4,950):
9,950 in all.
*/
public class Repro {
public static void main(String[] args) throws Exception {
int numDocs = 10_000;
try (Directory dir = new ByteBuffersDirectory()) {
try (IndexWriter w = new IndexWriter(dir, new IndexWriterConfig())) {
for (int d = 0; d < numDocs; d++) {
long v = d % 2 == 0 ? Long.MIN_VALUE : Long.MAX_VALUE - d;
Document doc = new Document();
doc.add(new NumericDocValuesField("n", v));
doc.add(NumericDocValuesField.indexedField("ni", v));
doc.add(new SortedNumericDocValuesField("sn", v));
doc.add(SortedNumericDocValuesField.indexedField("sni", v));
w.addDocument(doc);
}
w.forceMerge(1);
}
long lower = Long.MIN_VALUE;
long upper = Long.MAX_VALUE - 100;
long expected = 0;
for (int d = 0; d < numDocs; d++) {
long v = d % 2 == 0 ? Long.MIN_VALUE : Long.MAX_VALUE - d;
if (v >= lower && v <= upper) expected++;
}
try (DirectoryReader reader = DirectoryReader.open(dir)) {
IndexSearcher searcher = new IndexSearcher(reader);
System.out.println("Lucene " + Version.LATEST + ", expected " +
expected);
for (String f : new String[] {"n", "ni"}) {
System.out.println(
" NumericDocValuesField.newSlowRangeQuery(\"" + f + "\"): "
+
searcher.count(NumericDocValuesField.newSlowRangeQuery(f, lower, upper)));
}
for (String f : new String[] {"sn", "sni"}) {
System.out.println(
" SortedNumericDocValuesField.newSlowRangeQuery(\"" + f +
"\"): "
+
searcher.count(SortedNumericDocValuesField.newSlowRangeQuery(f, lower, upper)));
}
}
}
}
}
```
1. The index has 10,000 documents in one segment.
2. Even documents hold `Long.MIN_VALUE` and odd documents `Long.MAX_VALUE -
doc`, in a field without a skip index (`n`, `sn`) and one with (`ni`, `sni`).
3. The range `[Long.MIN_VALUE, Long.MAX_VALUE - 100]` matches all 5,000 even
documents and the 4,950 odd documents from 101 on.
Called directly, `rangeIntoBitSet` of such a field finds 5,000 of the 9,950
documents, both for the numeric and for the sorted numeric doc values.
The query counts are higher only because the skip index answers some blocks
as YES without the scan.
### Suggested fix
Keep the encoded-domain scan only when the packed values cannot be negative,
that is when `bitsPerValue < 64`.
With 64 bits per value, evaluate the range on the decoded values, as 10.5.2
does.
The same holds for a GCD that is not positive, which only a corrupt index
has: `transformGcdBounds` asserts `mul > 0`, and without assertions it divides
by it.
An alternative is to compare the packed values as unsigned, but the SIMD
range kernels compare signed values.
### Version and environment details
- Lucene 10.6.0, `lucene-core`; 10.5.2 is not affected.
- Temurin 25.0.5+8, Windows 11, x86_64 (the result does not depend on the
platform or the vector API).
- Introduced by #16160 (numeric) and #16285 (sorted numeric).
--
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]