Skip to content

Commit

Permalink
ESQL: Fix Max doubles bug with negatives and add tests for Max and Min (
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
ivancea authored Jul 9, 2024
1 parent 38cd0b3 commit 5d3512f
Show file tree
Hide file tree
Showing 25 changed files with 609 additions and 87 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/110586.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 110586
summary: "ESQL: Fix Max doubles bug with negatives and add tests for Max and Min"
area: ES|QL
type: bug
issues: []
8 changes: 4 additions & 4 deletions docs/reference/esql/functions/aggregation-functions.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ The <<esql-stats-by>> command supports these aggregate functions:
* <<esql-avg>>
* <<esql-agg-count>>
* <<esql-agg-count-distinct>>
* <<esql-agg-max>>
* <<esql-max>>
* <<esql-agg-median>>
* <<esql-agg-median-absolute-deviation>>
* <<esql-agg-min>>
* <<esql-min>>
* <<esql-agg-percentile>>
* experimental:[] <<esql-agg-st-centroid>>
* <<esql-agg-sum>>
Expand All @@ -25,14 +25,14 @@ The <<esql-stats-by>> command supports these aggregate functions:

include::count.asciidoc[]
include::count-distinct.asciidoc[]
include::max.asciidoc[]
include::median.asciidoc[]
include::median-absolute-deviation.asciidoc[]
include::min.asciidoc[]
include::percentile.asciidoc[]
include::st_centroid_agg.asciidoc[]
include::sum.asciidoc[]
include::layout/avg.asciidoc[]
include::layout/max.asciidoc[]
include::layout/min.asciidoc[]
include::layout/top.asciidoc[]
include::values.asciidoc[]
include::weighted-avg.asciidoc[]
5 changes: 5 additions & 0 deletions docs/reference/esql/functions/description/max.asciidoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions docs/reference/esql/functions/description/min.asciidoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions docs/reference/esql/functions/kibana/definition/max.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions docs/reference/esql/functions/kibana/definition/min.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions docs/reference/esql/functions/kibana/docs/max.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions docs/reference/esql/functions/kibana/docs/min.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions docs/reference/esql/functions/layout/max.asciidoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions docs/reference/esql/functions/layout/min.asciidoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions docs/reference/esql/functions/parameters/max.asciidoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions docs/reference/esql/functions/parameters/min.asciidoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/reference/esql/functions/signature/max.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/reference/esql/functions/signature/min.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions docs/reference/esql/functions/types/max.asciidoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions docs/reference/esql/functions/types/min.asciidoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class MaxDoubleAggregator {

public static double init() {
return Double.MIN_VALUE;
return -Double.MAX_VALUE;
}

public static double combine(double current, double v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.elasticsearch.xpack.esql.core.tree.Source;
import org.elasticsearch.xpack.esql.core.type.DataType;
import org.elasticsearch.xpack.esql.expression.SurrogateExpression;
import org.elasticsearch.xpack.esql.expression.function.Example;
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
import org.elasticsearch.xpack.esql.expression.function.Param;
import org.elasticsearch.xpack.esql.expression.function.scalar.multivalue.MvMax;
Expand All @@ -31,7 +32,16 @@ public class Max extends NumericAggregate implements SurrogateExpression {
@FunctionInfo(
returnType = { "double", "integer", "long", "date" },
description = "The maximum value of a numeric field.",
isAggregation = true
isAggregation = true,
examples = {
@Example(file = "stats", tag = "max"),
@Example(
description = "The expression can use inline functions. For example, to calculate the maximum "
+ "over an average of a multivalued column, use `MV_AVG` to first average the "
+ "multiple values per row, and use the result with the `MAX` function",
file = "stats",
tag = "docsStatsMaxNestedExpression"
) }
)
public Max(Source source, @Param(name = "number", type = { "double", "integer", "long", "date" }) Expression field) {
super(source, field);
Expand Down
Loading

0 comments on commit 5d3512f

Please sign in to comment.