3030import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
3131import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
3232import org.elasticsearch.xpack.esql.core.expression.FieldAttribute.FieldName;
33+ import org.elasticsearch.xpack.esql.core.util.Holder;
3334
3435import java.io.IOException;
3536import java.util.LinkedHashMap;
@@ -205,17 +206,19 @@ public Object min(FieldName field) {
205206 }
206207 if (stat.min == null) {
207208 var min = new long[] { Long.MAX_VALUE };
209+ Holder<Boolean> foundMinValue = new Holder<>(false);
208210 doWithContexts(r -> {
209211 byte[] minPackedValue = PointValues.getMinPackedValue(r, field.string());
210212 if (minPackedValue != null) {
211213 long minValue = NumericUtils.sortableBytesToLong(minPackedValue, 0);
212- if (minValue < min[0]) {
214+ if (minValue <= min[0]) {
213215 min[0] = minValue;
216+ foundMinValue.set(true);
214217 }
215218 }
216219 return true;
217220 }, true);
218- stat.min = min[0];
221+ stat.min = foundMinValue.get() ? min[0] : null ;
219222 }
220223 return stat.min;
221224 }
@@ -230,17 +233,19 @@ public Object max(FieldName field) {
230233 }
231234 if (stat.max == null) {
232235 var max = new long[] { Long.MIN_VALUE };
236+ Holder<Boolean> foundMaxValue = new Holder<>(false);
233237 doWithContexts(r -> {
234238 byte[] maxPackedValue = PointValues.getMaxPackedValue(r, field.string());
235239 if (maxPackedValue != null) {
236240 long maxValue = NumericUtils.sortableBytesToLong(maxPackedValue, 0);
237- if (maxValue > max[0]) {
241+ if (maxValue >= max[0]) {
238242 max[0] = maxValue;
243+ foundMaxValue.set(true);
239244 }
240245 }
241246 return true;
242247 }, true);
243- stat.max = max[0];
248+ stat.max = foundMaxValue.get() ? max[0] : null ;
244249 }
245250 return stat.max;
246251 }
0 commit comments