|
28 | 28 | import static java.util.Collections.emptyList; |
29 | 29 | import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.DEFAULT; |
30 | 30 | import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isType; |
| 31 | +import static org.elasticsearch.xpack.esql.core.type.DataType.AGGREGATE_METRIC_DOUBLE; |
31 | 32 |
|
32 | 33 | public class Avg extends AggregateFunction implements SurrogateExpression { |
33 | 34 | public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "Avg", Avg::new); |
@@ -65,7 +66,7 @@ public Avg(Source source, Expression field, Expression filter) { |
65 | 66 | protected Expression.TypeResolution resolveType() { |
66 | 67 | return isType( |
67 | 68 | field(), |
68 | | - dt -> dt.isNumeric() && dt != DataType.UNSIGNED_LONG || dt == DataType.AGGREGATE_METRIC_DOUBLE, |
| 69 | + dt -> dt.isNumeric() && dt != DataType.UNSIGNED_LONG || dt == AGGREGATE_METRIC_DOUBLE, |
69 | 70 | sourceText(), |
70 | 71 | DEFAULT, |
71 | 72 | "aggregate_metric_double or numeric except unsigned_long or counter types" |
@@ -108,12 +109,9 @@ public Expression surrogate() { |
108 | 109 | if (field.foldable()) { |
109 | 110 | return new MvAvg(s, field); |
110 | 111 | } |
111 | | - Expression sum = new Sum(s, field, filter()).surrogate() == null |
112 | | - ? new Sum(s, field, filter()) |
113 | | - : new Sum(s, field, filter()).surrogate(); |
114 | | - Expression count = new Count(s, field, filter()).surrogate() == null |
115 | | - ? new Count(s, field, filter()) |
116 | | - : new Count(s, field, filter()).surrogate(); |
117 | | - return new Div(s, sum, count, dataType()); |
| 112 | + if (field.dataType() == AGGREGATE_METRIC_DOUBLE) { |
| 113 | + return new Div(s, new Sum(s, field, filter()).surrogate(), new Count(s, field, filter()).surrogate()); |
| 114 | + } |
| 115 | + return new Div(s, new Sum(s, field, filter()), new Count(s, field, filter()), dataType()); |
118 | 116 | } |
119 | 117 | } |
0 commit comments