LUCENE-9451 Sort.rewrite does not always return this when unchanged (#1731)

This commit is contained in:
Mike Drob 2020-09-04 09:46:03 -05:00 committed by GitHub
parent 65da5ed32c
commit 6c94ca9cb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 9 deletions

View File

@ -275,12 +275,12 @@ public abstract class DoubleValuesSource implements SegmentCacheable {
this.value = value; this.value = value;
this.doubleValues = new DoubleValues() { this.doubleValues = new DoubleValues() {
@Override @Override
public double doubleValue() throws IOException { public double doubleValue() {
return value; return value;
} }
@Override @Override
public boolean advanceExact(int doc) throws IOException { public boolean advanceExact(int doc) {
return true; return true;
} }
}; };
@ -456,13 +456,16 @@ public abstract class DoubleValuesSource implements SegmentCacheable {
@Override @Override
public SortField rewrite(IndexSearcher searcher) throws IOException { public SortField rewrite(IndexSearcher searcher) throws IOException {
DoubleValuesSortField rewritten = new DoubleValuesSortField(producer.rewrite(searcher), reverse); DoubleValuesSource rewrittenSource = producer.rewrite(searcher);
if (rewrittenSource == producer) {
return this;
}
DoubleValuesSortField rewritten = new DoubleValuesSortField(rewrittenSource, reverse);
if (missingValue != null) { if (missingValue != null) {
rewritten.setMissingValue(missingValue); rewritten.setMissingValue(missingValue);
} }
return rewritten; return rewritten;
} }
} }
private static class DoubleValuesHolder { private static class DoubleValuesHolder {

View File

@ -305,7 +305,11 @@ public abstract class LongValuesSource implements SegmentCacheable {
@Override @Override
public SortField rewrite(IndexSearcher searcher) throws IOException { public SortField rewrite(IndexSearcher searcher) throws IOException {
LongValuesSortField rewritten = new LongValuesSortField(producer.rewrite(searcher), reverse); LongValuesSource rewrittenSource = producer.rewrite(searcher);
if (producer == rewrittenSource) {
return this;
}
LongValuesSortField rewritten = new LongValuesSortField(rewrittenSource, reverse);
if (missingValue != null) { if (missingValue != null) {
rewritten.setMissingValue(missingValue); rewritten.setMissingValue(missingValue);
} }

View File

@ -182,7 +182,7 @@ public class Sort {
} }
} }
return (changed) ? new Sort(rewrittenSortFields) : this; return changed ? new Sort(rewrittenSortFields) : this;
} }
@Override @Override

View File

@ -284,6 +284,11 @@ public class TestDoubleValuesSource extends LuceneTestCase {
doTestQueryDoubleValuesSources(approximatingQuery); doTestQueryDoubleValuesSources(approximatingQuery);
} }
public void testRewriteSame() throws IOException {
SortField doubleField = DoubleValuesSource.constant(1.0).getSortField(false);
assertSame(doubleField, doubleField.rewrite(searcher));
}
private void doTestQueryDoubleValuesSources(Query q) throws Exception { private void doTestQueryDoubleValuesSources(Query q) throws Exception {
DoubleValuesSource vs = DoubleValuesSource.fromQuery(q).rewrite(searcher); DoubleValuesSource vs = DoubleValuesSource.fromQuery(q).rewrite(searcher);
searcher.search(q, new SimpleCollector() { searcher.search(q, new SimpleCollector() {

View File

@ -17,6 +17,7 @@
package org.apache.lucene.search; package org.apache.lucene.search;
import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -129,6 +130,11 @@ public class TestLongValuesSource extends LuceneTestCase {
} }
} }
public void testRewriteSame() throws IOException {
SortField longField = LongValuesSource.constant(1L).getSortField(false);
assertSame(longField, longField.rewrite(searcher));
}
Sort randomSort() throws Exception { Sort randomSort() throws Exception {
boolean reversed = random().nextBoolean(); boolean reversed = random().nextBoolean();
SortField fields[] = new SortField[] { SortField fields[] = new SortField[] {

View File

@ -56,9 +56,9 @@ public class TestSort extends LuceneTestCase {
} }
private void assertDifferent(Sort a, Sort b) { private void assertDifferent(Sort a, Sort b) {
assertFalse(a.equals(b)); assertNotEquals(a, b);
assertFalse(b.equals(a)); assertNotEquals(b, a);
assertFalse(a.hashCode() == b.hashCode()); assertNotEquals(a.hashCode(), b.hashCode());
} }
public void testEquals() { public void testEquals() {
@ -878,4 +878,22 @@ public class TestSort extends LuceneTestCase {
ir.close(); ir.close();
dir.close(); dir.close();
} }
public void testRewrite() throws IOException {
try (Directory dir = newDirectory()) {
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
IndexSearcher searcher = newSearcher(writer.getReader());
writer.close();
LongValuesSource longSource = LongValuesSource.constant(1L);
Sort sort = new Sort(longSource.getSortField(false));
assertSame(sort, sort.rewrite(searcher));
DoubleValuesSource doubleSource = DoubleValuesSource.constant(1.0);
sort = new Sort(doubleSource.getSortField(false));
assertSame(sort, sort.rewrite(searcher));
}
}
} }