-
Notifications
You must be signed in to change notification settings - Fork 25.5k
ESQL: Standardize block args #135160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
ESQL: Standardize block args #135160
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
x-pack/plugin/esql/compute/ann/src/main/java/org/elasticsearch/compute/ann/Position.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.compute.ann; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Used on parameters on methods annotated with {@link Evaluator} or in | ||
* {@link Aggregator} or {@link GroupingAggregator} to indicate an argument | ||
* that is the position in a block. | ||
*/ | ||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.SOURCE) | ||
public @interface Position { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,8 @@ | |
import org.elasticsearch.compute.gen.AggregatorImplementer.AggregationParameter; | ||
import org.elasticsearch.compute.gen.AggregatorImplementer.AggregationState; | ||
import org.elasticsearch.compute.gen.argument.Argument; | ||
import org.elasticsearch.compute.gen.argument.ArrayArgument; | ||
import org.elasticsearch.compute.gen.argument.BlockArgument; | ||
import org.elasticsearch.compute.gen.argument.PositionArgument; | ||
import org.elasticsearch.compute.gen.argument.StandardArgument; | ||
|
||
import java.util.ArrayList; | ||
|
@@ -37,7 +38,6 @@ | |
import javax.lang.model.util.Elements; | ||
|
||
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; | ||
|
@@ -118,12 +118,13 @@ public GroupingAggregatorImplementer( | |
requireName("combine"), | ||
combineArgs(aggState) | ||
); | ||
this.aggParams = combine.getParameters().stream().skip(aggState.declaredType().isPrimitive() ? 1 : 2).map(v -> { | ||
this.aggParams = combine.getParameters().stream().skip(aggState.declaredType().isPrimitive() ? 1 : 2).flatMap(v -> { | ||
Argument a = Argument.fromParameter(types, v); | ||
return switch (a) { | ||
case StandardArgument sa -> new AggregationParameter(sa.name(), sa.type(), false); | ||
case ArrayArgument aa -> new AggregationParameter(aa.name(), aa.componentType(), true); | ||
default -> throw new IllegalArgumentException("unsupported argument [" + a + "]"); | ||
case StandardArgument sa -> Stream.of(new AggregationParameter(sa.name(), sa.type(), false)); | ||
case BlockArgument ba -> Stream.of(new AggregationParameter(ba.name(), Types.elementType(ba.type()), true)); | ||
case PositionArgument pa -> Stream.of(); | ||
default -> throw new IllegalArgumentException("unsupported argument [" + declarationType + "][" + a + "]"); | ||
}; | ||
}).toList(); | ||
|
||
|
@@ -476,21 +477,14 @@ private MethodSpec addRawInputLoop(TypeName groupsType, boolean valuesAreVector) | |
if (aggParams.size() > 1) { | ||
throw new IllegalArgumentException("array mode not supported for multiple args"); | ||
} | ||
String arrayType = aggParams.getFirst().type().toString().replace("[]", ""); | ||
builder.addStatement("int valuesStart = $L.getFirstValueIndex(valuesPosition)", aggParams.getFirst().blockName()); | ||
builder.addStatement( | ||
"int valuesEnd = valuesStart + $L.getValueCount(valuesPosition)", | ||
aggParams.getFirst().blockName() | ||
); | ||
builder.addStatement("$L[] valuesArray = new $L[valuesEnd - valuesStart]", arrayType, arrayType); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! |
||
builder.beginControlFlow("for (int v = valuesStart; v < valuesEnd; v++)"); | ||
builder.addStatement( | ||
"valuesArray[v-valuesStart] = $L.get$L(v)", | ||
aggParams.getFirst().blockName(), | ||
capitalize(aggParams.getFirst().arrayType()) | ||
warningsBlock( | ||
builder, | ||
() -> builder.addStatement( | ||
"$T.combine(state, groupId, valuesPosition, $L)", | ||
declarationType, | ||
aggParams.getFirst().blockName() | ||
) | ||
); | ||
builder.endControlFlow(); | ||
combineRawInputForArray(builder, "valuesArray"); | ||
} else { | ||
for (AggregationParameter p : aggParams) { | ||
builder.addStatement("int $L = $L.getFirstValueIndex(valuesPosition)", p.startName(), p.blockName()); | ||
|
@@ -536,6 +530,9 @@ private void invokeCombineRawInput(TypeName returnType, MethodSpec.Builder build | |
pattern.append("$T.combine(state, groupId"); | ||
params.add(declarationType); | ||
} | ||
if (aggParams.getFirst().isArray()) { | ||
pattern.append(", p"); | ||
} | ||
for (AggregationParameter p : aggParams) { | ||
pattern.append(", $L"); | ||
params.add(p.valueName()); | ||
|
@@ -547,10 +544,6 @@ private void invokeCombineRawInput(TypeName returnType, MethodSpec.Builder build | |
builder.addStatement(pattern.toString(), params.toArray()); | ||
} | ||
|
||
private void combineRawInputForArray(MethodSpec.Builder builder, String arrayVariable) { | ||
warningsBlock(builder, () -> builder.addStatement("$T.combine(state, groupId, $L)", declarationType, arrayVariable)); | ||
} | ||
|
||
private boolean shouldWrapAddInput(boolean valuesAreVector) { | ||
return optionalStaticMethod( | ||
declarationType, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...ql/compute/gen/src/main/java/org/elasticsearch/compute/gen/argument/PositionArgument.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.compute.gen.argument; | ||
|
||
import com.squareup.javapoet.MethodSpec; | ||
import com.squareup.javapoet.TypeName; | ||
import com.squareup.javapoet.TypeSpec; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* The position in a block. | ||
*/ | ||
public record PositionArgument() implements Argument { | ||
@Override | ||
public TypeName dataType(boolean blockStyle) { | ||
return TypeName.INT; | ||
} | ||
|
||
@Override | ||
public String paramName(boolean blockStyle) { | ||
// No need to pass it | ||
return null; | ||
} | ||
|
||
@Override | ||
public void declareField(TypeSpec.Builder builder) { | ||
// Nothing to do | ||
} | ||
|
||
@Override | ||
public void declareFactoryField(TypeSpec.Builder builder) { | ||
// Nothing to do | ||
} | ||
|
||
@Override | ||
public void implementCtor(MethodSpec.Builder builder) { | ||
// Nothing to do | ||
} | ||
|
||
@Override | ||
public void implementFactoryCtor(MethodSpec.Builder builder) { | ||
// Nothing to do | ||
} | ||
|
||
@Override | ||
public String factoryInvocation(MethodSpec.Builder factoryMethodBuilder) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void evalToBlock(MethodSpec.Builder builder) { | ||
// nothing to do | ||
} | ||
|
||
@Override | ||
public void closeEvalToBlock(MethodSpec.Builder builder) { | ||
// nothing to do | ||
} | ||
|
||
@Override | ||
public void resolveVectors(MethodSpec.Builder builder, String invokeBlockEval) { | ||
// nothing to do | ||
} | ||
|
||
@Override | ||
public void createScratch(MethodSpec.Builder builder) { | ||
// nothing to do | ||
} | ||
|
||
@Override | ||
public void skipNull(MethodSpec.Builder builder) { | ||
// nothing to do | ||
} | ||
|
||
@Override | ||
public void allBlocksAreNull(MethodSpec.Builder builder) { | ||
// nothing to do | ||
} | ||
|
||
@Override | ||
public void read(MethodSpec.Builder builder, boolean blockStyle) { | ||
// nothing to do | ||
} | ||
|
||
@Override | ||
public void buildInvocation(StringBuilder pattern, List<Object> args, boolean blockStyle) { | ||
pattern.append("p"); | ||
} | ||
|
||
@Override | ||
public void buildToStringInvocation(StringBuilder pattern, List<Object> args, String prefix) { | ||
// nothing to do | ||
} | ||
|
||
@Override | ||
public String closeInvocation() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void sumBaseRamBytesUsed(MethodSpec.Builder builder) {} | ||
} |
16 changes: 2 additions & 14 deletions
16
...h/compute/aggregation/spatial/SpatialExtentCartesianShapeDocValuesAggregatorFunction.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the next PR I should be ale to remove this
instanceof
and just us theArgument
directly. It'll need a few more methods, but should be fine.