@@ -54,12 +54,8 @@ abstract static class TsdbDocValuesProducer extends EmptyDocValuesProducer {
5454
5555 record MergeStats (boolean supported , long sumNumValues , int sumNumDocsWithField ) {}
5656
57- static MergeStats compatibleWithOptimizedMerge (
58- boolean optimizedMergeEnabled ,
59- MergeState mergeState ,
60- FieldInfo fieldInfo ,
61- DocValuesType docValuesType
62- ) throws IOException {
57+ static MergeStats compatibleWithOptimizedMerge (boolean optimizedMergeEnabled , MergeState mergeState , FieldInfo fieldInfo )
58+ throws IOException {
6359 if (optimizedMergeEnabled == false || mergeState .needsIndexSort == false ) {
6460 return UNSUPPORTED ;
6561 }
@@ -77,24 +73,15 @@ static MergeStats compatibleWithOptimizedMerge(
7773 // TODO bring back codec version check? (per field doc values producer sits between ES87TSDBDocValuesConsumer)
7874 for (int i = 0 ; i < mergeState .docValuesProducers .length ; i ++) {
7975 DocValuesProducer docValuesProducer = mergeState .docValuesProducers [i ];
80- switch (docValuesType ) {
76+ switch (fieldInfo . getDocValuesType () ) {
8177 case NUMERIC -> {
8278 var numeric = docValuesProducer .getNumeric (fieldInfo );
83- if (numeric instanceof ES87TSDBDocValuesProducer .BaseNumericDocValues baseNumericDocValues ) {
84- var entry = baseNumericDocValues .entry ;
79+ if (numeric instanceof ES87TSDBDocValuesProducer .BaseNumericDocValues baseNumeric ) {
80+ var entry = baseNumeric .entry ;
8581 sumNumValues += entry .numValues ;
86- // In this case the numDocsWithField doesn't get recorded in meta:
87- int numDocsWithField ;
88- if (entry .docsWithFieldOffset == -2 ) {
89- numDocsWithField = 0 ;
90- } else if (entry .docsWithFieldOffset == -1 ) {
91- numDocsWithField = mergeState .maxDocs [i ];
92- } else {
93- // Value doesn't matter in this case:
94- numDocsWithField = mergeState .maxDocs [i ] - 1 ;
95- }
82+ int numDocsWithField = getNumDocsWithField (entry , mergeState .maxDocs [i ]);
9683 sumNumDocsWithField += numDocsWithField ;
97- } else {
84+ } else if ( numeric != null ) {
9885 return UNSUPPORTED ;
9986 }
10087 }
@@ -105,26 +92,27 @@ static MergeStats compatibleWithOptimizedMerge(
10592 sumNumValues += entry .numValues ;
10693 sumNumDocsWithField += entry .numDocsWithField ;
10794 } else {
108- return UNSUPPORTED ;
95+ var singleton = DocValues .unwrapSingleton (sortedNumeric );
96+ if (singleton instanceof ES87TSDBDocValuesProducer .BaseNumericDocValues baseNumeric ) {
97+ var entry = baseNumeric .entry ;
98+ sumNumValues += entry .numValues ;
99+ // In this case the numDocsWithField doesn't get recorded in meta:
100+ int numDocsWithField = getNumDocsWithField (entry , mergeState .maxDocs [i ]);
101+ sumNumDocsWithField += numDocsWithField ;
102+ } else if (sortedNumeric != null ) {
103+ return UNSUPPORTED ;
104+ }
109105 }
110106 }
111107 case SORTED -> {
112108 var sorted = docValuesProducer .getSorted (fieldInfo );
113109 if (sorted instanceof ES87TSDBDocValuesProducer .BaseSortedDocValues baseSortedDocValues ) {
114110 var entry = baseSortedDocValues .entry ;
115111 sumNumValues += entry .ordsEntry .numValues ;
116- // In this case the numDocsWithField doesn't get recorded in meta:
117- int numDocsWithField ;
118- if (entry .ordsEntry .docsWithFieldOffset == -2 ) {
119- numDocsWithField = 0 ;
120- } else if (entry .ordsEntry .docsWithFieldOffset == -1 ) {
121- numDocsWithField = mergeState .maxDocs [i ];
122- } else {
123- // Value doesn't matter in this case:
124- numDocsWithField = mergeState .maxDocs [i ] - 1 ;
125- }
112+ // In this case the numDocsWithField doesn't get recorded in meta:v
113+ int numDocsWithField = getNumDocsWithField (entry .ordsEntry , mergeState .maxDocs [i ]);
126114 sumNumDocsWithField += numDocsWithField ;
127- } else {
115+ } else if ( sorted != null ) {
128116 return UNSUPPORTED ;
129117 }
130118 }
@@ -135,16 +123,37 @@ static MergeStats compatibleWithOptimizedMerge(
135123 sumNumValues += entry .ordsEntry .numValues ;
136124 sumNumDocsWithField += entry .ordsEntry .numDocsWithField ;
137125 } else {
138- return UNSUPPORTED ;
126+ var singleton = DocValues .unwrapSingleton (sortedSet );
127+ if (singleton instanceof ES87TSDBDocValuesProducer .BaseSortedDocValues baseSorted ) {
128+ var entry = baseSorted .entry ;
129+ sumNumValues += entry .ordsEntry .numValues ;
130+ // In this case the numDocsWithField doesn't get recorded in meta:
131+ int numDocsWithField = getNumDocsWithField (entry .ordsEntry , mergeState .maxDocs [i ]);
132+ sumNumDocsWithField += numDocsWithField ;
133+ } else if (sortedSet != null ) {
134+ return UNSUPPORTED ;
135+ }
139136 }
140137 }
141- default -> throw new IllegalStateException ("unexpected doc values producer type: " + docValuesType );
138+ default -> throw new IllegalStateException ("unexpected doc values producer type: " + fieldInfo . getDocValuesType () );
142139 }
143140 }
144141
145142 return new MergeStats (true , sumNumValues , sumNumDocsWithField );
146143 }
147144
145+ private static int getNumDocsWithField (ES87TSDBDocValuesProducer .NumericEntry entry , int maxDoc ) {
146+ // In this case the numDocsWithField doesn't get recorded in meta:
147+ if (entry .docsWithFieldOffset == -2 ) {
148+ return 0 ;
149+ } else if (entry .docsWithFieldOffset == -1 ) {
150+ return maxDoc ;
151+ } else {
152+ // numDocsWithField doesn't matter in this case:
153+ return 1 ;
154+ }
155+ }
156+
148157 static DocValuesProducer mergeNumericProducer (MergeStats mergeStats , FieldInfo mergeFieldInfo , MergeState mergeState ) {
149158 return new TsdbDocValuesProducer (mergeStats ) {
150159
0 commit comments