Skip to content

Commit

Permalink
Merge pull request #51 from zapr-oss/develop
Browse files Browse the repository at this point in the history
Release of 2.5
  • Loading branch information
abhi-zapr authored Oct 4, 2018
2 parents c59aec5 + 2a8c331 commit e915191
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ Aggregation Queries
* LongMax
* LongMin
* LongSum
* ThetaSketch

#### Filters

Expand All @@ -216,6 +217,8 @@ Aggregation Queries
* FieldAccess
* HyperUniqueCardinality
* Javascript
* ThetaSketchEstimate
* ThetaSketchSetOp

#### Granularity
* Duration
Expand Down
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>in.zapr.druid</groupId>
<artifactId>druidry</artifactId>
<version>2.4</version>
<version>2.5</version>

<name>Druidry - Druid Java Client</name>
<description>Druidry is an open-source Java based utility library which supports creating
Expand All @@ -24,6 +24,7 @@
<jsonassert.version>1.4.0</jsonassert.version>
<commons.version>3.5</commons.version>
<jersey.version>2.26</jersey.version>
<guava.version>23.0</guava.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -108,6 +109,13 @@
<version>${jersey.version}</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>


</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package in.zapr.druid.druidry.extensions.datasketches.aggregator;

import com.google.common.base.Preconditions;
import com.google.common.math.LongMath;

import com.fasterxml.jackson.annotation.JsonInclude;

import in.zapr.druid.druidry.aggregator.DruidAggregator;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;

@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ThetaSketchAggregator extends DruidAggregator {

private static final String THETA_SKETCH_TYPE_AGGREGATOR = "thetaSketch";
private String fieldName;
private Boolean isInputThetaSketch;
private Long size;

@Builder
public ThetaSketchAggregator(
@NonNull String name,
@NonNull String fieldName,
Boolean isInputThetaSketch,
Long size) {
this.type = THETA_SKETCH_TYPE_AGGREGATOR;
this.name = name;
this.fieldName = fieldName;
this.isInputThetaSketch = isInputThetaSketch;
this.size = size;

if (size != null) {
Preconditions.checkArgument(LongMath.isPowerOfTwo(size), "size must be a power of 2");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package in.zapr.druid.druidry.extensions.datasketches.postAggregator;

import in.zapr.druid.druidry.postAggregator.DruidPostAggregator;
import lombok.Getter;
import lombok.NonNull;

@Getter
public class ThetaSketchEstimatePostAggregator extends DruidPostAggregator {

private static final String THETA_SKETCH_ESTIMATE_POST_AGGREGATOR_TYPE = "thetaSketchEstimate";

private DruidPostAggregator field;

public ThetaSketchEstimatePostAggregator(@NonNull String name,
@NonNull DruidPostAggregator field) {
this.type = THETA_SKETCH_ESTIMATE_POST_AGGREGATOR_TYPE;
this.name = name;
this.field = field;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package in.zapr.druid.druidry.extensions.datasketches.postAggregator;

import com.fasterxml.jackson.annotation.JsonValue;

public enum ThetaSketchFunction {

INTERSECT("INTERSECT"),
UNION("UNION"),
NOT("NOT");

private String value;

ThetaSketchFunction(String value) {
this.value = value;
}

@JsonValue
public String getValue() {
return value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package in.zapr.druid.druidry.extensions.datasketches.postAggregator;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;

import in.zapr.druid.druidry.postAggregator.DruidPostAggregator;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;

@Getter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ThetaSketchSetOpPostAggregator extends DruidPostAggregator {

private static final String THETA_SKETCH_SET_OP_POST_AGGREGATOR_TYPE = "thetaSketchSetOp";

@JsonProperty("func")
private ThetaSketchFunction function;
private List<DruidPostAggregator> fields;
private Long size;

@Builder
private ThetaSketchSetOpPostAggregator(@NonNull String name,
@NonNull ThetaSketchFunction function,
@NonNull List<DruidPostAggregator> fields,
Long size) {
this.type = THETA_SKETCH_SET_OP_POST_AGGREGATOR_TYPE;
this.name = name;
this.function = function;
this.fields = fields;
this.size = size;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package in.zapr.druid.druidry.extensions.datasketches.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.annotations.BeforeClass;
import org.testng.annotations.Test;

public class ThetaSketchAggregatorTest {

private static ObjectMapper objectMapper;

@BeforeClass
public void init() {
objectMapper = new ObjectMapper();
}

private JSONObject getThetaSketchAggregatorJSON() throws JSONException {
JSONObject jsonObject = new JSONObject();
jsonObject.put("type", "thetaSketch");
jsonObject.put("name", "estimated_stars");
jsonObject.put("fieldName", "stars");

return jsonObject;
}

@Test
public void testAllFields() throws JsonProcessingException, JSONException {

ThetaSketchAggregator thetaSketchAggregator = ThetaSketchAggregator.builder()
.name("estimated_stars")
.fieldName("stars")
.isInputThetaSketch(true)
.size(1024L)
.build();

JSONObject jsonObject = getThetaSketchAggregatorJSON();
jsonObject.put("isInputThetaSketch", true);
jsonObject.put("size", 1024L);

String actualJSON = objectMapper.writeValueAsString(thetaSketchAggregator);
String expectedJSON = jsonObject.toString();
JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE);
}

@Test
public void testRequiredFields() throws JsonProcessingException, JSONException {

ThetaSketchAggregator thetaSketchAggregator = ThetaSketchAggregator.builder()
.name("estimated_stars")
.fieldName("stars")
.build();

JSONObject jsonObject = getThetaSketchAggregatorJSON();

String actualJSON = objectMapper.writeValueAsString(thetaSketchAggregator);
String expectedJSON = jsonObject.toString();
JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE);
}

@Test(expectedExceptions = IllegalArgumentException.class)
public void preconditionCheck() {

ThetaSketchAggregator thetaSketchAggregator = ThetaSketchAggregator.builder()
.name("estimated_stars")
.fieldName("stars")
.size(420L)
.build();
}

@Test(expectedExceptions = NullPointerException.class)
public void testNullName() {

ThetaSketchAggregator thetaSketchAggregator = ThetaSketchAggregator.builder()
.fieldName("stars")
.build();
}

@Test(expectedExceptions = NullPointerException.class)
public void testNullFieldName() {

ThetaSketchAggregator thetaSketchAggregator = ThetaSketchAggregator.builder()
.name("estimated_stars")
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package in.zapr.druid.druidry.extensions.datasketches.postAggregator;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.Collections;

import in.zapr.druid.druidry.postAggregator.FieldAccessPostAggregator;

public class ThetaSketchEstimatePostAggregatorTest {

private static ObjectMapper objectMapper;

@BeforeClass
public void init() {
objectMapper = new ObjectMapper();
}

private JSONObject getFieldAccessJSON() throws JSONException {
JSONObject fieldAccess = new JSONObject();
fieldAccess.put("type", "fieldAccess");
fieldAccess.put("fieldName", "stars");

return fieldAccess;
}

@Test
public void testAllFieldsWithFieldAccess() throws JsonProcessingException, JSONException {

FieldAccessPostAggregator fieldAccessPostAggregator =
new FieldAccessPostAggregator("stars");

ThetaSketchEstimatePostAggregator thetaSketchEstimatePostAggregator =
new ThetaSketchEstimatePostAggregator("estimate_stars", fieldAccessPostAggregator);


JSONObject fieldAccess = getFieldAccessJSON();

JSONObject jsonObject = new JSONObject();
jsonObject.put("type", "thetaSketchEstimate");
jsonObject.put("name", "estimate_stars");
jsonObject.put("field", fieldAccess);

String actualJSON = objectMapper.writeValueAsString(thetaSketchEstimatePostAggregator);
String expectedJSON = jsonObject.toString();
JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE);

}

@Test
public void testAllFieldsWithThetaSketchSetOp() throws JsonProcessingException, JSONException {

FieldAccessPostAggregator fieldAccessPostAggregator =
new FieldAccessPostAggregator("stars");

ThetaSketchSetOpPostAggregator thetaSketchSetOpPostAggregator = ThetaSketchSetOpPostAggregator.builder()
.name("name")
.function(ThetaSketchFunction.INTERSECT)
.fields(Collections.singletonList(fieldAccessPostAggregator))
.build();

ThetaSketchEstimatePostAggregator thetaSketchEstimatePostAggregator =
new ThetaSketchEstimatePostAggregator("estimate_stars", thetaSketchSetOpPostAggregator);


JSONObject fieldAccess = getFieldAccessJSON();

JSONArray fields = new JSONArray(Collections.singletonList(getFieldAccessJSON()));

JSONObject thetaSketchSetOp = new JSONObject();
thetaSketchSetOp.put("type", "thetaSketchSetOp");
thetaSketchSetOp.put("name", "name");
thetaSketchSetOp.put("func", "INTERSECT");
thetaSketchSetOp.put("fields", fields);

JSONObject jsonObject = new JSONObject();
jsonObject.put("type", "thetaSketchEstimate");
jsonObject.put("name", "estimate_stars");
jsonObject.put("field", thetaSketchSetOp);

String actualJSON = objectMapper.writeValueAsString(thetaSketchEstimatePostAggregator);
String expectedJSON = jsonObject.toString();
JSONAssert.assertEquals(expectedJSON, actualJSON, JSONCompareMode.NON_EXTENSIBLE);
}

@Test(expectedExceptions = NullPointerException.class)
public void testNullName() {

ThetaSketchEstimatePostAggregator thetaSketchEstimatePostAggregator =
new ThetaSketchEstimatePostAggregator(null, new FieldAccessPostAggregator("stars"));
}

@Test(expectedExceptions = NullPointerException.class)
public void testNullField() {

ThetaSketchEstimatePostAggregator thetaSketchEstimatePostAggregator =
new ThetaSketchEstimatePostAggregator("estimate_stars", null);
}

}
Loading

0 comments on commit e915191

Please sign in to comment.