Skip to content

Commit 080b6c3

Browse files
committed
handle methods in grouping aggregator
1 parent 2bfc745 commit 080b6c3

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

x-pack/plugin/esql/compute/gen/src/main/java/org/elasticsearch/compute/gen/GroupingAggregatorImplementer.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
import static org.elasticsearch.compute.gen.AggregatorImplementer.capitalize;
3737
import static org.elasticsearch.compute.gen.Methods.findMethod;
3838
import static org.elasticsearch.compute.gen.Methods.findRequiredMethod;
39+
import static org.elasticsearch.compute.gen.Methods.requireAnyArgs;
40+
import static org.elasticsearch.compute.gen.Methods.requireName;
41+
import static org.elasticsearch.compute.gen.Methods.requirePrimitiveOrImplements;
42+
import static org.elasticsearch.compute.gen.Methods.requireStaticMethod;
3943
import static org.elasticsearch.compute.gen.Methods.vectorAccessorName;
4044
import static org.elasticsearch.compute.gen.Types.BIG_ARRAYS;
4145
import static org.elasticsearch.compute.gen.Types.BLOCK_ARRAY;
@@ -73,7 +77,6 @@ public class GroupingAggregatorImplementer {
7377
private final ExecutableElement combine;
7478
private final ExecutableElement combineStates;
7579
private final ExecutableElement evaluateFinal;
76-
private final ExecutableElement combineIntermediate;
7780
private final List<Parameter> createParameters;
7881
private final ClassName implementation;
7982
private final List<AggregatorImplementer.IntermediateStateDesc> intermediateState;
@@ -92,9 +95,16 @@ public GroupingAggregatorImplementer(
9295
this.declarationType = declarationType;
9396
this.warnExceptions = warnExceptions;
9497

95-
this.init = findRequiredMethod(declarationType, new String[] { "init", "initGrouping" }, e -> true);
98+
this.init = requireStaticMethod(
99+
declarationType,
100+
// This should be more restrictive and require org.elasticsearch.compute.aggregation.AggregatorState
101+
requirePrimitiveOrImplements(elements, Types.RELEASABLE),
102+
requireName("init", "initGrouping"),
103+
requireAnyArgs("<arbitrary init arguments>")
104+
);
96105
this.aggState = AggregationState.create(elements, init.getReturnType(), warnExceptions.isEmpty() == false, true);
97106

107+
// TODO optional timestamp
98108
this.combine = findRequiredMethod(declarationType, new String[] { "combine" }, e -> {
99109
if (e.getParameters().size() == 0) {
100110
return false;
@@ -106,7 +116,6 @@ public GroupingAggregatorImplementer(
106116
this.aggParam = AggregationParameter.create(combine.getParameters().get(combine.getParameters().size() - 1).asType());
107117

108118
this.combineStates = findMethod(declarationType, "combineStates");
109-
this.combineIntermediate = findMethod(declarationType, "combineIntermediate");
110119
this.evaluateFinal = findMethod(declarationType, "evaluateFinal");
111120
this.createParameters = init.getParameters()
112121
.stream()
@@ -557,6 +566,7 @@ private MethodSpec addIntermediateInput() {
557566
});
558567
builder.endControlFlow();
559568
} else {
569+
// TODO combineIntermediate with optional block parameter
560570
builder.addStatement("$T.combineIntermediate(state, groupId, " + intermediateStateRowAccess() + ")", declarationType);
561571
}
562572
builder.endControlFlow();

x-pack/plugin/esql/compute/gen/src/main/java/org/elasticsearch/compute/gen/Methods.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.lang.model.element.Modifier;
2323
import javax.lang.model.element.TypeElement;
2424
import javax.lang.model.type.DeclaredType;
25+
import javax.lang.model.type.TypeKind;
2526
import javax.lang.model.type.TypeMirror;
2627
import javax.lang.model.util.ElementFilter;
2728
import javax.lang.model.util.Elements;
@@ -149,11 +150,13 @@ private static boolean isImplementing(Elements elements, TypeName type, TypeName
149150
}
150151

151152
private static Stream<TypeName> allInterfacesOf(Elements elements, TypeName type) {
152-
return elements.getTypeElement(type.toString())
153-
.getInterfaces()
154-
.stream()
155-
.map(TypeName::get)
156-
.flatMap(anInterface -> Stream.concat(Stream.of(anInterface), allInterfacesOf(elements, anInterface)));
153+
var typeElement = elements.getTypeElement(type.toString());
154+
var superType = Stream.of(typeElement.getSuperclass()).filter(sType -> sType.getKind() != TypeKind.NONE).map(TypeName::get);
155+
var interfaces = typeElement.getInterfaces().stream().map(TypeName::get);
156+
return Stream.concat(
157+
superType.flatMap(sType -> allInterfacesOf(elements, sType)),
158+
interfaces.flatMap(anInterface -> Stream.concat(Stream.of(anInterface), allInterfacesOf(elements, anInterface)))
159+
);
157160
}
158161

159162
private static Stream<TypeElement> typeAndSuperType(TypeElement declarationType) {

0 commit comments

Comments
 (0)