Skip to content

Commit 84e4fc7

Browse files
authored
[ES|QL] Fuse From_Aggregate_Metric_Double to BlockLoader (elastic#138392)
This commit fuses the FROM_AGGREGATE_METRIC_DOUBLE function into the blockloaders for the aggregate_metric_double field type. In particular, it works by directly loading the individual sub-blocks, rather than the entire AggregateMetricDoubleBlock, when called in a function that uses one of the sub-blocks via FROM_AGGREGATE_METRIC_DOUBLE
1 parent 8fd9b86 commit 84e4fc7

File tree

6 files changed

+295
-2
lines changed

6 files changed

+295
-2
lines changed

muted-tests.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ tests:
436436
- class: org.elasticsearch.xpack.unsignedlong.UnsignedLongSyntheticSourceNativeArrayIntegrationTests
437437
method: testSynthesizeArrayRandom
438438
issue: https://github.com/elastic/elasticsearch/issues/138684
439+
- class: org.elasticsearch.xpack.esql.optimizer.LocalLogicalPlanOptimizerTests
440+
method: testAggregateMetricDoubleInlineStats
441+
issue: https://github.com/elastic/elasticsearch/issues/138620
442+
- class: org.elasticsearch.xpack.esql.optimizer.rules.logical.local.ReplaceDateTruncBucketWithRoundToTests
443+
method: testAggregateMetricDoubleInlineStats
444+
issue: https://github.com/elastic/elasticsearch/issues/138620
439445
- class: org.elasticsearch.index.mapper.HalfFloatSyntheticSourceNativeArrayIntegrationTests
440446
method: testSynthesizeArrayRandom
441447
issue: https://github.com/elastic/elasticsearch/issues/138730

server/src/main/java/org/elasticsearch/index/mapper/blockloader/BlockLoaderFunctionConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public int hashCode() {
4545
}
4646

4747
enum Function {
48+
AMD_COUNT,
49+
AMD_MAX,
50+
AMD_MIN,
51+
AMD_SUM,
4852
MV_MAX,
4953
MV_MIN,
5054
LENGTH,

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/AggregateMetricDoubleBlockBuilder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,16 @@ public int getIndex() {
203203
public String getLabel() {
204204
return label;
205205
}
206+
207+
public static Metric indexToMetric(int i) {
208+
return switch (i) {
209+
case 0 -> MIN;
210+
case 1 -> MAX;
211+
case 2 -> SUM;
212+
case 3 -> COUNT;
213+
default -> null;
214+
};
215+
}
206216
}
207217

208218
/**

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/convert/FromAggregateMetricDouble.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,22 @@
1919
import org.elasticsearch.compute.operator.DriverContext;
2020
import org.elasticsearch.compute.operator.EvalOperator;
2121
import org.elasticsearch.core.Releasables;
22+
import org.elasticsearch.index.mapper.blockloader.BlockLoaderFunctionConfig;
2223
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
2324
import org.elasticsearch.xpack.esql.core.expression.Expression;
2425
import org.elasticsearch.xpack.esql.core.expression.Expressions;
26+
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
2527
import org.elasticsearch.xpack.esql.core.expression.FoldContext;
2628
import org.elasticsearch.xpack.esql.core.expression.Literal;
2729
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
2830
import org.elasticsearch.xpack.esql.core.tree.Source;
2931
import org.elasticsearch.xpack.esql.core.type.DataType;
3032
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
3133
import org.elasticsearch.xpack.esql.expression.function.Param;
34+
import org.elasticsearch.xpack.esql.expression.function.blockloader.BlockLoaderExpression;
3235
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction;
3336
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
37+
import org.elasticsearch.xpack.esql.stats.SearchStats;
3438

3539
import java.io.IOException;
3640
import java.util.List;
@@ -43,7 +47,7 @@
4347
import static org.elasticsearch.xpack.esql.core.type.DataType.INTEGER;
4448
import static org.elasticsearch.xpack.esql.core.type.DataType.NULL;
4549

46-
public class FromAggregateMetricDouble extends EsqlScalarFunction implements ConvertFunction {
50+
public class FromAggregateMetricDouble extends EsqlScalarFunction implements ConvertFunction, BlockLoaderExpression {
4751
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
4852
Expression.class,
4953
"FromAggregateMetricDouble",
@@ -119,7 +123,7 @@ protected TypeResolution resolveType() {
119123
if (childrenResolved() == false) {
120124
return new TypeResolution("Unresolved children");
121125
}
122-
return isType(field, dt -> dt == DataType.AGGREGATE_METRIC_DOUBLE, sourceText(), DEFAULT, "aggregate_metric_double only");
126+
return isType(field, dt -> dt == AGGREGATE_METRIC_DOUBLE, sourceText(), DEFAULT, "aggregate_metric_double only");
123127
}
124128

125129
@Override
@@ -192,4 +196,24 @@ public Expression field() {
192196
public Set<DataType> supportedTypes() {
193197
return Set.of(AGGREGATE_METRIC_DOUBLE);
194198
}
199+
200+
@Override
201+
public PushedBlockLoaderExpression tryPushToFieldLoading(SearchStats stats) {
202+
if (field() instanceof FieldAttribute f && f.dataType() == AGGREGATE_METRIC_DOUBLE) {
203+
var folded = subfieldIndex.fold(FoldContext.small());
204+
if (folded == null) {
205+
throw new IllegalArgumentException("Subfield Index was null");
206+
}
207+
var subfield = ((Number) folded).intValue();
208+
var functionConfig = switch (AggregateMetricDoubleBlockBuilder.Metric.indexToMetric(subfield)) {
209+
case MIN -> BlockLoaderFunctionConfig.Function.AMD_MIN;
210+
case MAX -> BlockLoaderFunctionConfig.Function.AMD_MAX;
211+
case SUM -> BlockLoaderFunctionConfig.Function.AMD_SUM;
212+
case COUNT -> BlockLoaderFunctionConfig.Function.AMD_COUNT;
213+
case null -> throw new IllegalArgumentException("Received invalid subfield index [" + subfield + "].");
214+
};
215+
return new PushedBlockLoaderExpression(f, new BlockLoaderFunctionConfig.JustFunction(functionConfig));
216+
}
217+
return null;
218+
}
195219
}

0 commit comments

Comments
 (0)