Support postaggregation function as in Math.pow() (#13703) (#13704)

Support postaggregation function as in Math.pow()
This commit is contained in:
Tijo Thomas 2023-01-31 22:55:04 +05:30 committed by GitHub
parent 51dfde0284
commit 1beef30bb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 1 deletions

View File

@ -36,7 +36,7 @@ There are several post-aggregators available.
The arithmetic post-aggregator applies the provided function to the given
fields from left to right. The fields can be aggregators or other post aggregators.
Supported functions are `+`, `-`, `*`, `/`, and `quotient`.
Supported functions are `+`, `-`, `*`, `/`, `pow` and `quotient`.
**Note**:

View File

@ -204,6 +204,7 @@ public class ArithmeticPostAggregator implements PostAggregator
case MINUS:
case DIV:
case QUOTIENT:
case POW:
return true;
default:
throw new IAE(op.fn);
@ -246,6 +247,14 @@ public class ArithmeticPostAggregator implements PostAggregator
{
return lhs / rhs;
}
},
POW("pow") {
@Override
public double compute(double lhs, double rhs)
{
return Math.pow(lhs, rhs);
}
};
private static final Map<String, Ops> LOOKUP_MAP = new HashMap<>();

View File

@ -183,7 +183,36 @@ public class ArithmeticPostAggregatorTest extends InitializedNullHandlingTest
Assert.assertEquals(Double.POSITIVE_INFINITY, agg.compute(ImmutableMap.of("value", 1)));
Assert.assertEquals(Double.NEGATIVE_INFINITY, agg.compute(ImmutableMap.of("value", -1)));
}
@Test
public void testPow()
{
ArithmeticPostAggregator agg = new ArithmeticPostAggregator(
null,
"pow",
ImmutableList.of(
new ConstantPostAggregator("value", 4),
new ConstantPostAggregator("power", .5)
),
"numericFirst"
);
Assert.assertEquals(2.0, agg.compute(ImmutableMap.of("value", 0)));
agg = new ArithmeticPostAggregator(
null,
"pow",
ImmutableList.of(
new FieldAccessPostAggregator("base", "value"),
new ConstantPostAggregator("zero", 0)
),
"numericFirst"
);
Assert.assertEquals(1.0, agg.compute(ImmutableMap.of("value", 0)));
Assert.assertEquals(1.0, agg.compute(ImmutableMap.of("value", Double.NaN)));
Assert.assertEquals(1.0, agg.compute(ImmutableMap.of("value", 1)));
Assert.assertEquals(1.0, agg.compute(ImmutableMap.of("value", -1)));
Assert.assertEquals(1.0, agg.compute(ImmutableMap.of("value", .5)));
}
@Test
public void testDiv()
{