Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/130421.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 130421
summary: Support avg on aggregate metric double
area: ES|QL
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,12 @@ public enum Cap {
/**
* (Re)Added EXPLAIN command
*/
EXPLAIN(Build.current().isSnapshot());
EXPLAIN(Build.current().isSnapshot()),

/**
* Support avg with aggregate metric doubles
*/
AGGREGATE_METRIC_DOUBLE_AVG(AGGREGATE_METRIC_DOUBLE_FEATURE_FLAG);

private final boolean enabled;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Avg(
Source source,
@Param(
name = "number",
type = { "double", "integer", "long" },
type = { "aggregate_metric_double", "double", "integer", "long" },
description = "Expression that outputs values to average."
) Expression field
) {
Expand All @@ -65,10 +65,10 @@ public Avg(Source source, Expression field, Expression filter) {
protected Expression.TypeResolution resolveType() {
return isType(
field(),
dt -> dt.isNumeric() && dt != DataType.UNSIGNED_LONG,
dt -> dt.isNumeric() && dt != DataType.UNSIGNED_LONG || dt == DataType.AGGREGATE_METRIC_DOUBLE,
sourceText(),
DEFAULT,
"numeric except unsigned_long or counter types"
"aggregate_metric_double or numeric except unsigned_long or counter types"
);
}

Expand Down Expand Up @@ -105,9 +105,15 @@ public Avg withFilter(Expression filter) {
public Expression surrogate() {
var s = source();
var field = field();

return field().foldable()
? new MvAvg(s, field)
: new Div(s, new Sum(s, field, filter()), new Count(s, field, filter()), dataType());
if (field.foldable()) {
return new MvAvg(s, field);
}
Expression sum = new Sum(s, field, filter()).surrogate() == null
? new Sum(s, field, filter())
: new Sum(s, field, filter()).surrogate();
Expression count = new Count(s, field, filter()).surrogate() == null
? new Count(s, field, filter())
: new Count(s, field, filter()).surrogate();
return new Div(s, sum, count, dataType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2006,7 +2006,7 @@ public void testUnsupportedTypesInStats() {
| stats avg(x), count_distinct(x), max(x), median(x), median_absolute_deviation(x), min(x), percentile(x, 10), sum(x)
""", """
Found 8 problems
line 2:12: argument of [avg(x)] must be [numeric except unsigned_long or counter types],\
line 2:12: argument of [avg(x)] must be [aggregate_metric_double or numeric except unsigned_long or counter types],\
found value [x] type [unsigned_long]
line 2:20: argument of [count_distinct(x)] must be [any exact type except unsigned_long, _source, or counter types],\
found value [x] type [unsigned_long]
Expand All @@ -2028,7 +2028,7 @@ public void testUnsupportedTypesInStats() {
| stats avg(x), median(x), median_absolute_deviation(x), percentile(x, 10), sum(x)
""", """
Found 5 problems
line 2:10: argument of [avg(x)] must be [numeric except unsigned_long or counter types],\
line 2:10: argument of [avg(x)] must be [aggregate_metric_double or numeric except unsigned_long or counter types],\
found value [x] type [version]
line 2:18: argument of [median(x)] must be [numeric except unsigned_long or counter types],\
found value [x] type [version]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ public void testAggsExpressionsInStatsAggs() {
error("from test | stats max(max(salary)) by first_name")
);
assertEquals(
"1:25: argument of [avg(first_name)] must be [numeric except unsigned_long or counter types],"
"1:25: argument of [avg(first_name)] must be [aggregate_metric_double or numeric except unsigned_long or counter types],"
+ " found value [first_name] type [keyword]",
error("from test | stats count(avg(first_name)) by first_name")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ protected Expression build(Source source, List<Expression> args) {

@Override
protected Matcher<String> expectedTypeErrorMatcher(List<Set<DataType>> validPerPosition, List<DataType> signature) {
return equalTo(typeErrorMessage(false, validPerPosition, signature, (v, p) -> "numeric except unsigned_long or counter types"));
return equalTo(
typeErrorMessage(
false,
validPerPosition,
signature,
(v, p) -> "aggregate_metric_double or numeric except unsigned_long or counter types"
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -707,3 +707,27 @@ to_aggregate_metric_double with multi_values:
- match: {values.0.1: '{"min":102.44400024414062,"max":195.10000610351562,"sum":297.54400634765625,"value_count":2}'}
- match: {values.0.2: '{"min":64.0,"max":1456.0,"sum":2139.0,"value_count":4}'}
- match: {values.0.3: '{"min":123.0,"max":1.9418924E7,"sum":1.9433032E7,"value_count":3}'}

---
avg of aggregate_metric_double:
- requires:
test_runner_features: [capabilities]
capabilities:
- method: POST
path: /_query
parameters: []
capabilities: [aggregate_metric_double_avg]
reason: "support avg aggregations with aggregate metric double"

- do:
allowed_warnings_regex:
- "No limit defined, adding default limit of \\[.*\\]"
esql.query:
body:
query: 'FROM test2 | STATS avg = avg(agg_metric) | KEEP avg'

- length: {values: 1}
- length: {values.0: 1}
- match: {columns.0.name: "avg"}
- match: {columns.0.type: "double"}
- match: {values.0.0: 4.904761904761905}
Loading