From f3fab2c5c3e1135887aee2014fd70335fb10e2da Mon Sep 17 00:00:00 2001 From: barrybecker4 Date: Tue, 16 Mar 2021 13:41:33 -0700 Subject: [PATCH] Fix for #160 - additional aggregators Add - DoubleMeanAggregator - StdDevPostAggregator - VarianceAggregator --- .../aggregator/DoubleMaxAggregator.java | 2 +- .../aggregator/DoubleMeanAggregator.java | 52 ++++++ .../aggregator/DoubleMinAggregator.java | 2 +- .../aggregator/VarianceAggregator.java | 58 ++++++ .../postAggregator/StdDevPostAggregator.java | 43 +++++ .../aggregator/DoubleMeanAggregatorTest.java | 159 +++++++++++++++++ .../aggregator/VarianceAggregatorTest.java | 167 ++++++++++++++++++ .../StdDevPostAggregatorTest.java | 98 ++++++++++ 8 files changed, 579 insertions(+), 2 deletions(-) create mode 100644 src/main/java/in/zapr/druid/druidry/aggregator/DoubleMeanAggregator.java create mode 100644 src/main/java/in/zapr/druid/druidry/aggregator/VarianceAggregator.java create mode 100644 src/main/java/in/zapr/druid/druidry/postAggregator/StdDevPostAggregator.java create mode 100644 src/test/java/in/zapr/druid/druidry/aggregator/DoubleMeanAggregatorTest.java create mode 100644 src/test/java/in/zapr/druid/druidry/aggregator/VarianceAggregatorTest.java create mode 100644 src/test/java/in/zapr/druid/druidry/postAggregator/StdDevPostAggregatorTest.java diff --git a/src/main/java/in/zapr/druid/druidry/aggregator/DoubleMaxAggregator.java b/src/main/java/in/zapr/druid/druidry/aggregator/DoubleMaxAggregator.java index cf9532aa..12b59228 100644 --- a/src/main/java/in/zapr/druid/druidry/aggregator/DoubleMaxAggregator.java +++ b/src/main/java/in/zapr/druid/druidry/aggregator/DoubleMaxAggregator.java @@ -32,7 +32,7 @@ public class DoubleMaxAggregator extends DruidAggregator { private static final String DOUBLE_MAX_TYPE_AGGREGATOR = "doubleMax"; - private String fieldName; + private final String fieldName; private String expression; @Deprecated diff --git a/src/main/java/in/zapr/druid/druidry/aggregator/DoubleMeanAggregator.java b/src/main/java/in/zapr/druid/druidry/aggregator/DoubleMeanAggregator.java new file mode 100644 index 00000000..3d04524f --- /dev/null +++ b/src/main/java/in/zapr/druid/druidry/aggregator/DoubleMeanAggregator.java @@ -0,0 +1,52 @@ +/* + * Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package in.zapr.druid.druidry.aggregator; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.google.common.base.Preconditions; + +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NonNull; + + + +@Getter +@EqualsAndHashCode(callSuper = true) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class DoubleMeanAggregator extends DruidAggregator { + + private static final String DOUBLE_MEAN_TYPE_AGGREGATOR = "doubleMean"; + + private final String fieldName; + private final String expression; + + @Builder + private DoubleMeanAggregator(@NonNull String name, String fieldName, String expression) { + this.type = DOUBLE_MEAN_TYPE_AGGREGATOR; + this.name = name; + this.fieldName = fieldName; + this.expression = expression; + + Preconditions.checkArgument( + fieldName == null ^ expression == null, + "Must have a valid, non-null fieldName or expression" + ); + } +} + diff --git a/src/main/java/in/zapr/druid/druidry/aggregator/DoubleMinAggregator.java b/src/main/java/in/zapr/druid/druidry/aggregator/DoubleMinAggregator.java index 11f0c82e..a6bf736d 100644 --- a/src/main/java/in/zapr/druid/druidry/aggregator/DoubleMinAggregator.java +++ b/src/main/java/in/zapr/druid/druidry/aggregator/DoubleMinAggregator.java @@ -32,7 +32,7 @@ public class DoubleMinAggregator extends DruidAggregator { private static final String DOUBLE_MIN_TYPE_AGGREGATOR = "doubleMin"; - private String fieldName; + private final String fieldName; private String expression; @Deprecated diff --git a/src/main/java/in/zapr/druid/druidry/aggregator/VarianceAggregator.java b/src/main/java/in/zapr/druid/druidry/aggregator/VarianceAggregator.java new file mode 100644 index 00000000..b88b2694 --- /dev/null +++ b/src/main/java/in/zapr/druid/druidry/aggregator/VarianceAggregator.java @@ -0,0 +1,58 @@ +/* + * Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package in.zapr.druid.druidry.aggregator; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.google.common.base.Preconditions; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NonNull; +import lombok.Builder; + + + +@Getter +@EqualsAndHashCode(callSuper = true) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class VarianceAggregator extends DruidAggregator { + private static final String VARIANCE_TYPE_AGGREGATOR = "variance"; + + private final String fieldName; + private final String expression; + + // One of "float", "double", "long", "variance"; defaults to "float". + private final String inputType; + + // Use "population" to get variance_pop rather than variance_sample, which is default. + private final String estimator; + + @Builder + private VarianceAggregator(@NonNull String name, String fieldName, String expression, String inputType, String estimator) { + this.type = VARIANCE_TYPE_AGGREGATOR; + this.name = name; + this.fieldName = fieldName; + this.expression = expression; + this.inputType = inputType; + this.estimator = estimator; + + Preconditions.checkArgument( + fieldName == null ^ expression == null, + "Must have a valid, non-null fieldName or expression" + ); + } +} diff --git a/src/main/java/in/zapr/druid/druidry/postAggregator/StdDevPostAggregator.java b/src/main/java/in/zapr/druid/druidry/postAggregator/StdDevPostAggregator.java new file mode 100644 index 00000000..1f094ea9 --- /dev/null +++ b/src/main/java/in/zapr/druid/druidry/postAggregator/StdDevPostAggregator.java @@ -0,0 +1,43 @@ +/* + * Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package in.zapr.druid.druidry.postAggregator; + +import com.fasterxml.jackson.annotation.JsonInclude; + +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.NonNull; + + +@Getter +@JsonInclude(JsonInclude.Include.NON_NULL) +@EqualsAndHashCode(callSuper = true) +public class StdDevPostAggregator extends DruidPostAggregator { + + private static final String STDDEV_POST_AGGREGATOR_TYPE = "stddev"; + private final String fieldName; + + @Builder + StdDevPostAggregator(@NonNull String name, + @NonNull String fieldName) { + this.type = STDDEV_POST_AGGREGATOR_TYPE; + this.name = name; + this.fieldName = fieldName; + } + +} diff --git a/src/test/java/in/zapr/druid/druidry/aggregator/DoubleMeanAggregatorTest.java b/src/test/java/in/zapr/druid/druidry/aggregator/DoubleMeanAggregatorTest.java new file mode 100644 index 00000000..724b7229 --- /dev/null +++ b/src/test/java/in/zapr/druid/druidry/aggregator/DoubleMeanAggregatorTest.java @@ -0,0 +1,159 @@ +/* + * Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package in.zapr.druid.druidry.aggregator; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.json.JSONException; +import org.json.JSONObject; +import org.skyscreamer.jsonassert.JSONAssert; +import org.skyscreamer.jsonassert.JSONCompareMode; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class DoubleMeanAggregatorTest { + + private static ObjectMapper objectMapper; + + @BeforeClass + public void init() { + objectMapper = new ObjectMapper(); + } + + @Test + public void testAllFields() throws JsonProcessingException, JSONException { + + DoubleMeanAggregator doubleMeanAggregator = + DoubleMeanAggregator.builder().name("CarpeDiem").fieldName("someField").build(); + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("type", "doubleMean"); + jsonObject.put("name", "CarpeDiem"); + jsonObject.put("fieldName", "someField"); + + String actualJSON = objectMapper.writeValueAsString(doubleMeanAggregator); + String expectedJSON = jsonObject.toString(); + JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE); + } + + @Test + public void testAllButFieldName() throws JSONException, JsonProcessingException { + + DoubleMeanAggregator doubleMeanAggregator = + DoubleMeanAggregator.builder() + .name("CarpeDiem") + .expression("(\"foo\" / \"bar\")") + .build(); + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("type", "doubleMean"); + jsonObject.put("name", "CarpeDiem"); + jsonObject.put("expression", "(\"foo\" / \"bar\")"); + + String actualJSON = objectMapper.writeValueAsString(doubleMeanAggregator); + String expectedJSON = jsonObject.toString(); + JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE); + } + + @Test(expectedExceptions = NullPointerException.class) + public void testNullName() throws JsonProcessingException, JSONException { + + DoubleMeanAggregator doubleMeanAggregator = + DoubleMeanAggregator.builder() + .fieldName("anotherField") + .build(); + } + + @Test + public void testEqualsPositive() { + DoubleMeanAggregator aggregator1 = + DoubleMeanAggregator.builder() + .name("name") + .fieldName("field") + .build(); + + DoubleMeanAggregator aggregator2 = + DoubleMeanAggregator.builder() + .name("name") + .fieldName("field") + .build(); + + DoubleMeanAggregator aggregator3 = + DoubleMeanAggregator.builder() + .name("name") + .expression("(\"foo\" / \"bar\")") + .build(); + + DoubleMeanAggregator aggregator4 = + DoubleMeanAggregator.builder() + .name("name") + .expression("(\"foo\" / \"bar\")") + .build(); + + Assert.assertEquals(aggregator1, aggregator2); + Assert.assertEquals(aggregator3, aggregator4); + } + + @Test + public void testEqualsNegative() { + DoubleMeanAggregator aggregator1 = + DoubleMeanAggregator.builder() + .name("name") + .fieldName("field") + .build(); + + DoubleMeanAggregator aggregator2 = + DoubleMeanAggregator.builder() + .name("name1") + .fieldName("field1") + .build(); + + DoubleMeanAggregator aggregator3 = + DoubleMeanAggregator.builder() + .name("name") + .expression("(\"foo\" / \"bar\")") + .build(); + + DoubleMeanAggregator aggregator4 = + DoubleMeanAggregator.builder() + .name("name") + .expression("(\"foo\" / \"baz\")") + .build(); + + Assert.assertNotEquals(aggregator1, aggregator2); + Assert.assertNotEquals(aggregator3, aggregator4); + } + + @Test + public void testEqualsWithAnotherSubClass() { + DoubleMeanAggregator aggregator1 = + DoubleMeanAggregator.builder() + .name("name") + .fieldName("field") + .build(); + + CountAggregator aggregator2 = new CountAggregator("countAgg1"); + + Assert.assertNotEquals(aggregator1, aggregator2); + } + +} diff --git a/src/test/java/in/zapr/druid/druidry/aggregator/VarianceAggregatorTest.java b/src/test/java/in/zapr/druid/druidry/aggregator/VarianceAggregatorTest.java new file mode 100644 index 00000000..732568d4 --- /dev/null +++ b/src/test/java/in/zapr/druid/druidry/aggregator/VarianceAggregatorTest.java @@ -0,0 +1,167 @@ +/* + * Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package in.zapr.druid.druidry.aggregator; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.json.JSONException; +import org.json.JSONObject; +import org.skyscreamer.jsonassert.JSONAssert; +import org.skyscreamer.jsonassert.JSONCompareMode; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import lombok.extern.slf4j.Slf4j; + + + +@Slf4j +public class VarianceAggregatorTest { + + private static ObjectMapper objectMapper; + + @BeforeClass + public void init() { + objectMapper = new ObjectMapper(); + } + + @Test + public void testAllFields() throws JsonProcessingException, JSONException { + + VarianceAggregator varianceAggregator = VarianceAggregator.builder() + .name("CarpeDiem") + .fieldName("someField") + .inputType("float") + .estimator("population") + .build(); + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("type", "variance"); + jsonObject.put("name", "CarpeDiem"); + jsonObject.put("fieldName", "someField"); + jsonObject.put("inputType", "float"); + jsonObject.put("estimator", "population"); + + String actualJSON = objectMapper.writeValueAsString(varianceAggregator); + String expectedJSON = jsonObject.toString(); + JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE); + } + + @Test + public void testAllButFieldName() throws JSONException, JsonProcessingException { + + VarianceAggregator varianceAggregator = + VarianceAggregator.builder() + .name("CarpeDiem") + .expression("(\"foo\" / \"bar\")") + .build(); + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("type", "variance"); + jsonObject.put("name", "CarpeDiem"); + jsonObject.put("expression", "(\"foo\" / \"bar\")"); + + String actualJSON = objectMapper.writeValueAsString(varianceAggregator); + String expectedJSON = jsonObject.toString(); + JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE); + } + + @Test(expectedExceptions = NullPointerException.class) + public void testNullName() throws JsonProcessingException, JSONException { + + VarianceAggregator varianceAggregator = + VarianceAggregator.builder() + .fieldName("anotherField") + .build(); + } + + @Test + public void testEqualsPositive() { + VarianceAggregator aggregator1 = + VarianceAggregator.builder() + .name("name") + .fieldName("field") + .build(); + + VarianceAggregator aggregator2 = + VarianceAggregator.builder() + .name("name") + .fieldName("field") + .build(); + + VarianceAggregator aggregator3 = + VarianceAggregator.builder() + .name("name") + .expression("(\"foo\" / \"bar\")") + .build(); + + VarianceAggregator aggregator4 = + VarianceAggregator.builder() + .name("name") + .expression("(\"foo\" / \"bar\")") + .build(); + + Assert.assertEquals(aggregator1, aggregator2); + Assert.assertEquals(aggregator3, aggregator4); + } + + @Test + public void testEqualsNegative() { + VarianceAggregator aggregator1 = + VarianceAggregator.builder() + .name("name") + .fieldName("field") + .build(); + + VarianceAggregator aggregator2 = + VarianceAggregator.builder() + .name("name1") + .fieldName("field1") + .build(); + + VarianceAggregator aggregator3 = + VarianceAggregator.builder() + .name("name") + .expression("(\"foo\" / \"bar\")") + .build(); + + VarianceAggregator aggregator4 = + VarianceAggregator.builder() + .name("name") + .expression("(\"foo\" / \"baz\")") + .build(); + + Assert.assertNotEquals(aggregator1, aggregator2); + Assert.assertNotEquals(aggregator3, aggregator4); + } + + @Test + public void testEqualsWithAnotherSubClass() { + VarianceAggregator aggregator1 = + VarianceAggregator.builder() + .name("name") + .fieldName("field") + .build(); + + CountAggregator aggregator2 = new CountAggregator("countAgg1"); + + Assert.assertNotEquals(aggregator1, aggregator2); + } + +} diff --git a/src/test/java/in/zapr/druid/druidry/postAggregator/StdDevPostAggregatorTest.java b/src/test/java/in/zapr/druid/druidry/postAggregator/StdDevPostAggregatorTest.java new file mode 100644 index 00000000..d88e94cb --- /dev/null +++ b/src/test/java/in/zapr/druid/druidry/postAggregator/StdDevPostAggregatorTest.java @@ -0,0 +1,98 @@ +/* + * Copyright 2018-present Red Brick Lane Marketing Solutions Pvt. Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package in.zapr.druid.druidry.postAggregator; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.json.JSONException; +import org.json.JSONObject; +import org.skyscreamer.jsonassert.JSONAssert; +import org.skyscreamer.jsonassert.JSONCompareMode; +import org.testng.Assert; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +public class StdDevPostAggregatorTest { + + private static ObjectMapper objectMapper; + + @BeforeClass + public void init() { + objectMapper = new ObjectMapper(); + } + + @Test + public void testStdDevPostAggregatorAllFields() throws JsonProcessingException, JSONException { + + StdDevPostAggregator stdDevPostAggregator + = new StdDevPostAggregator("Hello", "someField"); + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("type", "stddev"); + jsonObject.put("name", "Hello"); + jsonObject.put("fieldName", "someField"); + + String actualJSON = objectMapper.writeValueAsString(stdDevPostAggregator); + String expectedJSON = jsonObject.toString(); + JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE); + } + + @Test(expectedExceptions = NullPointerException.class) + public void testNullName() { + + StdDevPostAggregator stdDevPostAggregator = + new StdDevPostAggregator(null, "someField"); + } + + @Test(expectedExceptions = NullPointerException.class) + public void testNullValue() { + + StdDevPostAggregator stdDevPostAggregator = + new StdDevPostAggregator("Name", null); + } + + @Test + public void testEqualsPositive() { + StdDevPostAggregator aggregator1 + = new StdDevPostAggregator("Hello", "someField"); + StdDevPostAggregator aggregator2 + = new StdDevPostAggregator("Hello", "someField"); + + Assert.assertEquals(aggregator1, aggregator2); + } + + @Test + public void testEqualsNegative() { + StdDevPostAggregator aggregator1 + = new StdDevPostAggregator("Hello", "someField"); + StdDevPostAggregator aggregator2 + = new StdDevPostAggregator("Pi", "anotherField"); + + Assert.assertNotEquals(aggregator1, aggregator2); + } + + @Test + public void testEqualsWithAnotherSubClass() { + StdDevPostAggregator aggregator1 + = new StdDevPostAggregator("Hello", "someField"); + FieldAccessPostAggregator aggregator2 + = new FieldAccessPostAggregator("Hello", "blah"); + + Assert.assertNotEquals(aggregator1, aggregator2); + } +}