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
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
import org.elasticsearch.compute.data.Block;
import org.elasticsearch.compute.data.BlockFactory;
import org.elasticsearch.compute.data.BytesRefBlock;
import org.elasticsearch.compute.data.BytesRefVector;
import org.elasticsearch.compute.data.ElementType;
import org.elasticsearch.compute.data.IntBlock;
import org.elasticsearch.compute.data.IntVector;
import org.elasticsearch.compute.data.LongBlock;
import org.elasticsearch.compute.data.LongVector;
import org.elasticsearch.compute.data.OrdinalBytesRefVector;
import org.elasticsearch.compute.data.Page;
import org.elasticsearch.compute.operator.AggregationOperator;
import org.elasticsearch.compute.operator.DriverContext;
Expand Down Expand Up @@ -282,11 +285,18 @@ private static Block dataBlock(int groups, String dataType) {
int blockLength = blockLength(groups);
return switch (dataType) {
case BYTES_REF -> {
try (BytesRefBlock.Builder builder = blockFactory.newBytesRefBlockBuilder(blockLength)) {
try (
BytesRefVector.Builder dict = blockFactory.newBytesRefVectorBuilder(blockLength);
IntVector.Builder ords = blockFactory.newIntVectorBuilder(blockLength)
) {
final int dictLength = Math.min(blockLength, KEYWORDS.length);
for (int i = 0; i < dictLength; i++) {
dict.appendBytesRef(KEYWORDS[i]);
}
for (int i = 0; i < blockLength; i++) {
builder.appendBytesRef(KEYWORDS[i % KEYWORDS.length]);
ords.appendInt(i % dictLength);
}
yield builder.build();
yield new OrdinalBytesRefVector(ords.build(), dict.build()).asBlock();
}
}
case INT -> {
Expand Down
5 changes: 5 additions & 0 deletions docs/changelog/127849.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 127849
summary: Optimize ordinal inputs in Values aggregation
area: "ES|QL, TSDB"
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import static java.util.stream.Collectors.joining;
import static org.elasticsearch.compute.gen.AggregatorImplementer.capitalize;
import static org.elasticsearch.compute.gen.Methods.optionalStaticMethod;
import static org.elasticsearch.compute.gen.Methods.requireAnyArgs;
import static org.elasticsearch.compute.gen.Methods.requireAnyType;
import static org.elasticsearch.compute.gen.Methods.requireArgs;
Expand Down Expand Up @@ -336,10 +337,32 @@ private MethodSpec prepareProcessPage() {
builder.beginControlFlow("if (valuesBlock.mayHaveNulls())");
builder.addStatement("state.enableGroupIdTracking(seenGroupIds)");
builder.endControlFlow();
builder.addStatement("return $L", addInput(b -> b.addStatement("addRawInput(positionOffset, groupIds, valuesBlock$L)", extra)));
if (shouldWrapAddInput(blockType(aggParam.type()))) {
builder.addStatement(
"var addInput = $L",
addInput(b -> b.addStatement("addRawInput(positionOffset, groupIds, valuesBlock$L)", extra))
);
builder.addStatement("return $T.wrapAddInput(addInput, state, valuesBlock)", declarationType);
} else {
builder.addStatement(
"return $L",
addInput(b -> b.addStatement("addRawInput(positionOffset, groupIds, valuesBlock$L)", extra))
);
}
}
builder.endControlFlow();
builder.addStatement("return $L", addInput(b -> b.addStatement("addRawInput(positionOffset, groupIds, valuesVector$L)", extra)));
if (shouldWrapAddInput(vectorType(aggParam.type()))) {
builder.addStatement(
"var addInput = $L",
addInput(b -> b.addStatement("addRawInput(positionOffset, groupIds, valuesVector$L)", extra))
);
builder.addStatement("return $T.wrapAddInput(addInput, state, valuesVector)", declarationType);
} else {
builder.addStatement(
"return $L",
addInput(b -> b.addStatement("addRawInput(positionOffset, groupIds, valuesVector$L)", extra))
);
}
return builder.build();
}

Expand Down Expand Up @@ -526,6 +549,15 @@ private void combineRawInputForArray(MethodSpec.Builder builder, String arrayVar
warningsBlock(builder, () -> builder.addStatement("$T.combine(state, groupId, $L)", declarationType, arrayVariable));
}

private boolean shouldWrapAddInput(TypeName valuesType) {
return optionalStaticMethod(
declarationType,
requireType(GROUPING_AGGREGATOR_FUNCTION_ADD_INPUT),
requireName("wrapAddInput"),
requireArgs(requireType(GROUPING_AGGREGATOR_FUNCTION_ADD_INPUT), requireType(aggState.declaredType()), requireType(valuesType))
) != null;
}

private void warningsBlock(MethodSpec.Builder builder, Runnable block) {
if (warnExceptions.isEmpty() == false) {
builder.beginControlFlow("try");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,31 @@ static ExecutableElement requireStaticMethod(
TypeMatcher returnTypeMatcher,
NameMatcher nameMatcher,
ArgumentMatcher argumentMatcher
) {
ExecutableElement method = optionalStaticMethod(declarationType, returnTypeMatcher, nameMatcher, argumentMatcher);
if (method == null) {
var message = nameMatcher.names.size() == 1 ? "Requires method: " : "Requires one of methods: ";
var signatures = nameMatcher.names.stream()
.map(name -> "public static " + returnTypeMatcher + " " + declarationType + "#" + name + "(" + argumentMatcher + ")")
.collect(joining(" or "));
throw new IllegalArgumentException(message + signatures);
}
return method;
}

static ExecutableElement optionalStaticMethod(
TypeElement declarationType,
TypeMatcher returnTypeMatcher,
NameMatcher nameMatcher,
ArgumentMatcher argumentMatcher
) {
return typeAndSuperType(declarationType).flatMap(type -> ElementFilter.methodsIn(type.getEnclosedElements()).stream())
.filter(method -> method.getModifiers().contains(Modifier.STATIC))
.filter(method -> nameMatcher.test(method.getSimpleName().toString()))
.filter(method -> returnTypeMatcher.test(TypeName.get(method.getReturnType())))
.filter(method -> argumentMatcher.test(method.getParameters().stream().map(it -> TypeName.get(it.asType())).toList()))
.findFirst()
.orElseThrow(() -> {
var message = nameMatcher.names.size() == 1 ? "Requires method: " : "Requires one of methods: ";
var signatures = nameMatcher.names.stream()
.map(name -> "public static " + returnTypeMatcher + " " + declarationType + "#" + name + "(" + argumentMatcher + ")")
.collect(joining(" or "));
return new IllegalArgumentException(message + signatures);
});
.orElse(null);
}

static NameMatcher requireName(String... names) {
Expand Down

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.

Loading
Loading