Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LUCENE-10087: Validate number of dimensions and bytes per dimension for numeric SortFields. #283

Merged
merged 1 commit into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lucene/core/src/java/org/apache/lucene/search/SortField.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import java.io.IOException;
import java.util.Comparator;
import java.util.Objects;
import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.document.FloatPoint;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.IndexSorter;
import org.apache.lucene.index.SortFieldProvider;
Expand Down Expand Up @@ -68,25 +72,25 @@ public enum Type {

/**
* Sort using term values as encoded Integers. Sort values are Integer and lower values are at
* the front.
* the front. Fields must either be not indexed, or indexed with {@link IntPoint}.
*/
INT,

/**
* Sort using term values as encoded Floats. Sort values are Float and lower values are at the
* front.
* front. Fields must either be not indexed, or indexed with {@link FloatPoint}.
*/
FLOAT,

/**
* Sort using term values as encoded Longs. Sort values are Long and lower values are at the
* front.
* front. Fields must either be not indexed, or indexed with {@link LongPoint}.
*/
LONG,

/**
* Sort using term values as encoded Doubles. Sort values are Double and lower values are at the
* front.
* front. Fields must either be not indexed, or indexed with {@link DoublePoint}.
*/
DOUBLE,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.util.Arrays;
import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.PointValues;
Expand Down Expand Up @@ -90,6 +91,26 @@ public NumericLeafComparator(LeafReaderContext context) throws IOException {
this.docValues = getNumericDocValues(context, field);
this.pointValues = canSkipDocuments ? context.reader().getPointValues(field) : null;
if (pointValues != null) {
FieldInfo info = context.reader().getFieldInfos().fieldInfo(field);
if (info == null || info.getPointDimensionCount() == 0) {
throw new IllegalStateException(
"Field "
+ field
+ " doesn't index points according to FieldInfos yet returns non-null PointValues");
} else if (info.getPointDimensionCount() > 1) {
throw new IllegalArgumentException(
"Field " + field + " is indexed with multiple dimensions, sorting is not supported");
} else if (info.getPointNumBytes() != bytesCount) {
throw new IllegalArgumentException(
"Field "
+ field
+ " is indexed with "
+ info.getPointNumBytes()
+ " bytes per dimension, but "
+ NumericComparator.this
+ " expected "
+ bytesCount);
}
this.enableSkipping = true; // skipping is enabled when points are available
this.maxDoc = context.reader().maxDoc();
this.maxValueAsBytes =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.lucene.document.FloatDocValuesField;
import org.apache.lucene.document.FloatPoint;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.IntRange;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.StoredField;
Expand All @@ -33,6 +34,7 @@
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
Expand Down Expand Up @@ -583,4 +585,49 @@ public void testDocSort() throws IOException {
reader.close();
dir.close();
}

public void testPointValidation() throws IOException {
final Directory dir = newDirectory();
final RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
Document doc = new Document();

doc.add(new IntPoint("intField", 4));
doc.add(new NumericDocValuesField("intField", 4));

doc.add(new LongPoint("longField", 42));
doc.add(new NumericDocValuesField("longField", 42));

doc.add(new IntRange("intRange", new int[] {1}, new int[] {10}));
doc.add(new NumericDocValuesField("intRange", 4));

writer.addDocument(doc);
IndexReader reader = writer.getReader();
writer.close();

IndexSearcher searcher = newSearcher(reader);
assertThrows(
IllegalArgumentException.class,
() ->
searcher.search(
new MatchAllDocsQuery(),
1,
new Sort(new SortField("intField", SortField.Type.LONG))));
assertThrows(
IllegalArgumentException.class,
() ->
searcher.search(
new MatchAllDocsQuery(),
1,
new Sort(new SortField("longField", SortField.Type.INT))));
assertThrows(
IllegalArgumentException.class,
() ->
searcher.search(
new MatchAllDocsQuery(),
1,
new Sort(new SortField("intRange", SortField.Type.INT))));

reader.close();
dir.close();
}
}