Skip to content

Commit

Permalink
[Scripting] Use Number as a return value for BucketAggregationScript (#…
Browse files Browse the repository at this point in the history
…35653)

This change fixes #35351. Users were no longer able to return types of numbers other than doubles for bucket aggregation scripts. This change reverts to the previous behavior of being able to return any type of number and having it converted to a double outside of the script.
  • Loading branch information
jdconrad committed Nov 16, 2018
1 parent 93c7cf5 commit d47ccac
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
BucketAggregationSelectorScript.Factory wrappedFactory = parameters -> new BucketAggregationSelectorScript(parameters) {
@Override
public boolean execute() {
return factory.newInstance(getParams()).execute() == 1.0;
return factory.newInstance(getParams()).execute().doubleValue() == 1.0;
}
};
return context.factoryClazz.cast(wrappedFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Map<String, Object> getParams() {
return params;
}

public abstract Double execute();
public abstract Number execute();

public interface Factory {
BucketAggregationScript newInstance(Map<String, Object> params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext
if (skipBucket) {
newBuckets.add(bucket);
} else {
Double returned = factory.newInstance(vars).execute();
Number returned = factory.newInstance(vars).execute();
if (returned == null) {
newBuckets.add(bucket);
} else {
final List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map(
(p) -> (InternalAggregation) p).collect(Collectors.toList());
aggs.add(new InternalSimpleValue(name(), returned, formatter, new ArrayList<>(), metaData()));
aggs.add(new InternalSimpleValue(name(), returned.doubleValue(), formatter, new ArrayList<>(), metaData()));
InternalMultiBucketAggregation.InternalBucket newBucket = originalAgg.createBucket(new InternalAggregations(aggs),
bucket);
newBuckets.add(newBucket);
Expand Down

0 comments on commit d47ccac

Please sign in to comment.