Skip to content

Commit 4f67d1a

Browse files
ivanceaalbertzaharovits
authored andcommitted
ESQL: Fix Max doubles bug with negatives and add tests for Max and Min (#110586)
`MAX()` currently doesn't work with doubles smaller than `Double.MIN_VALUE` (Note that `Double.MIN_VALUE` returns the smallest non-zero positive, not the smallest double). This PR adds tests for Max and Min, and fixes the bug (Detected by the tests). Also, as the tests now generate the docs, replaced the old docs with the generated ones, and updated the Max&Min examples.
1 parent 62873d1 commit 4f67d1a

File tree

25 files changed

+609
-87
lines changed

25 files changed

+609
-87
lines changed

docs/changelog/110586.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 110586
2+
summary: "ESQL: Fix Max doubles bug with negatives and add tests for Max and Min"
3+
area: ES|QL
4+
type: bug
5+
issues: []

docs/reference/esql/functions/aggregation-functions.asciidoc

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ The <<esql-stats-by>> command supports these aggregate functions:
1111
* <<esql-avg>>
1212
* <<esql-agg-count>>
1313
* <<esql-agg-count-distinct>>
14-
* <<esql-agg-max>>
14+
* <<esql-max>>
1515
* <<esql-agg-median>>
1616
* <<esql-agg-median-absolute-deviation>>
17-
* <<esql-agg-min>>
17+
* <<esql-min>>
1818
* <<esql-agg-percentile>>
1919
* experimental:[] <<esql-agg-st-centroid>>
2020
* <<esql-agg-sum>>
@@ -25,14 +25,14 @@ The <<esql-stats-by>> command supports these aggregate functions:
2525

2626
include::count.asciidoc[]
2727
include::count-distinct.asciidoc[]
28-
include::max.asciidoc[]
2928
include::median.asciidoc[]
3029
include::median-absolute-deviation.asciidoc[]
31-
include::min.asciidoc[]
3230
include::percentile.asciidoc[]
3331
include::st_centroid_agg.asciidoc[]
3432
include::sum.asciidoc[]
3533
include::layout/avg.asciidoc[]
34+
include::layout/max.asciidoc[]
35+
include::layout/min.asciidoc[]
3636
include::layout/top.asciidoc[]
3737
include::values.asciidoc[]
3838
include::weighted-avg.asciidoc[]

docs/reference/esql/functions/description/max.asciidoc

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/description/min.asciidoc

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/max.asciidoc renamed to docs/reference/esql/functions/examples/max.asciidoc

+5-26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/min.asciidoc renamed to docs/reference/esql/functions/examples/min.asciidoc

+4-25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/kibana/definition/max.json

+60
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/kibana/definition/min.json

+60
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/kibana/docs/max.md

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/kibana/docs/min.md

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/layout/max.asciidoc

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/layout/min.asciidoc

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/parameters/max.asciidoc

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/parameters/min.asciidoc

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/signature/max.svg

+1
Loading

docs/reference/esql/functions/signature/min.svg

+1
Loading

docs/reference/esql/functions/types/max.asciidoc

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/reference/esql/functions/types/min.asciidoc

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/MaxDoubleAggregator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class MaxDoubleAggregator {
1717

1818
public static double init() {
19-
return Double.MIN_VALUE;
19+
return -Double.MAX_VALUE;
2020
}
2121

2222
public static double combine(double current, double v) {

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/aggregate/Max.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.elasticsearch.xpack.esql.core.tree.Source;
1919
import org.elasticsearch.xpack.esql.core.type.DataType;
2020
import org.elasticsearch.xpack.esql.expression.SurrogateExpression;
21+
import org.elasticsearch.xpack.esql.expression.function.Example;
2122
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
2223
import org.elasticsearch.xpack.esql.expression.function.Param;
2324
import org.elasticsearch.xpack.esql.expression.function.scalar.multivalue.MvMax;
@@ -31,7 +32,16 @@ public class Max extends NumericAggregate implements SurrogateExpression {
3132
@FunctionInfo(
3233
returnType = { "double", "integer", "long", "date" },
3334
description = "The maximum value of a numeric field.",
34-
isAggregation = true
35+
isAggregation = true,
36+
examples = {
37+
@Example(file = "stats", tag = "max"),
38+
@Example(
39+
description = "The expression can use inline functions. For example, to calculate the maximum "
40+
+ "over an average of a multivalued column, use `MV_AVG` to first average the "
41+
+ "multiple values per row, and use the result with the `MAX` function",
42+
file = "stats",
43+
tag = "docsStatsMaxNestedExpression"
44+
) }
3545
)
3646
public Max(Source source, @Param(name = "number", type = { "double", "integer", "long", "date" }) Expression field) {
3747
super(source, field);

0 commit comments

Comments
 (0)