Apache Lucene open-source search software
Make SimpleCodec return docvalues singletons (#16714)
Dawid Weisse357029271b3
diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesReader.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesReader.javaindex 4941bc25211..692a37cb6bc 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesReader.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextDocValuesReader.java@@ -54,6 +54,7 @@ import java.util.function.IntFunction;import org.apache.lucene.codecs.DocValuesProducer;import org.apache.lucene.index.BinaryDocValues;import org.apache.lucene.index.CorruptIndexException;+import org.apache.lucene.index.DocValues;import org.apache.lucene.index.DocValuesSkipper;import org.apache.lucene.index.DocValuesType;import org.apache.lucene.index.FieldInfo;@@ -65,6 +66,9 @@ import org.apache.lucene.index.SortedDocValues;import org.apache.lucene.index.SortedNumericDocValues;import org.apache.lucene.index.SortedSetDocValues;import org.apache.lucene.search.DocIdSetIterator;+import org.apache.lucene.search.SortField;+import org.apache.lucene.search.SortedNumericSelector;+import org.apache.lucene.search.SortedSetSelector;import org.apache.lucene.store.BufferedChecksumIndexInput;import org.apache.lucene.store.ChecksumIndexInput;import org.apache.lucene.store.IndexInput;@@ -669,7 +673,30 @@ class SimpleTextDocValuesReader extends DocValuesProducer {}@Override- public SortedNumericDocValues getSortedNumeric(FieldInfo field) throws IOException {+ public SortedNumericDocValues getSortedNumeric(FieldInfo fieldInfo) throws IOException {+ final OneField field = fields.get(fieldInfo.name);+ assert field != null;++ final SortedNumericDocValues multi = getMultiValuedSortedNumeric(fieldInfo);+ if (isSingleValued(field) == false) {+ return multi;+ }++ // Every document has at most one value: expose the column as a singleton, like the default+ // codec does, so that consumers relying on DocValues.unwrapSingleton() see the same shape.+ return DocValues.singleton(+ SortedNumericSelector.wrap(multi, SortedNumericSelector.Type.MIN, SortField.Type.LONG));+ }++ /**+ * Returns true if every document of the field has at most one value (according to the max value+ * count recorded by the writer), in which case the field is exposed as a singleton.+ */+ private static boolean isSingleValued(OneField field) {+ return field.maxValueCount >= 0 && field.maxValueCount <= 1;+ }++ private SortedNumericDocValues getMultiValuedSortedNumeric(FieldInfo field) throws IOException {final BinaryDocValues binary = getBinary(field);return new SortedNumericDocValues() {@@ -746,6 +773,17 @@ class SimpleTextDocValuesReader extends DocValuesProducer {// valid:assert field != null;+ final SortedSetDocValues multi = getMultiValuedSortedSet(field);+ if (isSingleValued(field) == false) {+ return multi;+ }++ // Every document has at most one ordinal: expose the column as a singleton, like the default+ // codec does, so that consumers relying on DocValues.unwrapSingleton() see the same shape.+ return DocValues.singleton(SortedSetSelector.wrap(multi, SortedSetSelector.Type.MIN));+ }++ private SortedSetDocValues getMultiValuedSortedSet(OneField field) throws IOException {final IndexInput in = data.clone();final BytesRefBuilder scratch = new BytesRefBuilder();final DecimalFormat decoder =diff --git a/lucene/core/src/java/org/apache/lucene/search/SortedSetSelector.java b/lucene/core/src/java/org/apache/lucene/search/SortedSetSelector.javaindex a7fbad70b44..927fe466f1b 100644--- a/lucene/core/src/java/org/apache/lucene/search/SortedSetSelector.java+++ b/lucene/core/src/java/org/apache/lucene/search/SortedSetSelector.java@@ -134,6 +134,7 @@ public class SortedSetSelector {@Overridepublic void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {in.intoBitSet(upTo, bitSet, offset);+ setOrd();}@Override@@ -213,6 +214,7 @@ public class SortedSetSelector {@Overridepublic void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {in.intoBitSet(upTo, bitSet, offset);+ setOrd();}@Override@@ -296,6 +298,7 @@ public class SortedSetSelector {@Overridepublic void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {in.intoBitSet(upTo, bitSet, offset);+ setOrd();}@Override@@ -380,6 +383,7 @@ public class SortedSetSelector {@Overridepublic void intoBitSet(int upTo, FixedBitSet bitSet, int offset) throws IOException {in.intoBitSet(upTo, bitSet, offset);+ setOrd();}@Override