Skip to content
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

theta sketch to string post agg #7937

Merged
merged 1 commit into from Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/content/development/extensions-core/datasketches-theta.md
Expand Up @@ -78,6 +78,18 @@ druid.extensions.loadList=["druid-datasketches"]
}
```

#### Sketch Summary

This returns a summary of the sketch that can be used for debugging. This is the result of calling toString() method.

```json
{
"type" : "thetaSketchToString",
"name": <output name>,
"field" : <post aggregator that refers to a Theta sketch (fieldAccess or another post aggregator)>
}
```

### Examples

Assuming, you have a dataset containing (timestamp, product, user_id). You want to answer questions like
Expand Down
Expand Up @@ -41,8 +41,8 @@ public class SketchModule implements DruidModule

public static final String THETA_SKETCH_ESTIMATE_POST_AGG = "thetaSketchEstimate";
public static final String THETA_SKETCH_SET_OP_POST_AGG = "thetaSketchSetOp";

public static final String THETA_SKETCH_CONSTANT_POST_AGG = "thetaSketchConstant";
public static final String THETA_SKETCH_TO_STRING_POST_AGG = "thetaSketchToString";

@Override
public void configure(Binder binder)
Expand All @@ -60,7 +60,8 @@ public List<? extends Module> getJacksonModules()
new NamedType(SketchMergeAggregatorFactory.class, THETA_SKETCH),
new NamedType(SketchEstimatePostAggregator.class, THETA_SKETCH_ESTIMATE_POST_AGG),
new NamedType(SketchSetPostAggregator.class, THETA_SKETCH_SET_OP_POST_AGG),
new NamedType(SketchConstantPostAggregator.class, THETA_SKETCH_CONSTANT_POST_AGG)
new NamedType(SketchConstantPostAggregator.class, THETA_SKETCH_CONSTANT_POST_AGG),
new NamedType(SketchToStringPostAggregator.class, THETA_SKETCH_TO_STRING_POST_AGG)
)
.addSerializer(SketchHolder.class, new SketchHolderJsonSerializer())
);
Expand Down
@@ -0,0 +1,136 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.druid.query.aggregation.datasketches.theta;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.druid.query.aggregation.AggregatorFactory;
import org.apache.druid.query.aggregation.PostAggregator;
import org.apache.druid.query.aggregation.post.PostAggregatorIds;
import org.apache.druid.query.cache.CacheKeyBuilder;

import java.util.Comparator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
* Returns a human-readable summary of a given Theta sketch.
* This is a string returned by toString() method of the sketch.
* This can be useful for debugging.
*/
public class SketchToStringPostAggregator implements PostAggregator
{

private final String name;
private final PostAggregator field;

@JsonCreator
public SketchToStringPostAggregator(
@JsonProperty("name") final String name,
@JsonProperty("field") final PostAggregator field
)
{
this.name = name;
this.field = field;
}

@Override
public Set<String> getDependentFields()
{
return field.getDependentFields();
}

@Override
public Comparator<String> getComparator()
{
return Comparator.nullsFirst(Comparator.naturalOrder());
}

@Override
public Object compute(final Map<String, Object> combinedAggregators)
{
final SketchHolder holder = (SketchHolder) field.compute(combinedAggregators);
return holder.getSketch().toString();
}

@Override
@JsonProperty
public String getName()
{
return name;
}

@Override
public PostAggregator decorate(final Map<String, AggregatorFactory> aggregators)
{
return this;
}

@JsonProperty
public PostAggregator getField()
{
return field;
}

@Override
public String toString()
{
return getClass().getSimpleName() + "{" +
"name='" + name + '\'' +
", field=" + field +
"}";
}

@Override
public boolean equals(final Object o)
{
if (this == o) {
return true;
}
if (!(o instanceof SketchToStringPostAggregator)) {
return false;
}

final SketchToStringPostAggregator that = (SketchToStringPostAggregator) o;

if (!name.equals(that.name)) {
return false;
}

return field.equals(that.field);
}

@Override
public int hashCode()
{
return Objects.hash(name, field);
}

@Override
public byte[] getCacheKey()
{
return new CacheKeyBuilder(PostAggregatorIds.THETA_SKETCH_TO_STRING)
.appendString(name)
.appendCacheable(field)
.build();
}

}
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.druid.query.aggregation.datasketches.theta;

import org.apache.druid.query.aggregation.Aggregator;
import org.apache.druid.query.aggregation.PostAggregator;
import org.apache.druid.query.aggregation.TestObjectColumnSelector;
import org.apache.druid.query.aggregation.post.FieldAccessPostAggregator;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class SketchToStringPostAggregatorTest
{

@Test
public void test()
{
// not going to iterate over the selector since getting a summary of an empty sketch is sufficient
final TestObjectColumnSelector selector = new TestObjectColumnSelector(new Object[0]);
final Aggregator agg = new SketchAggregator(selector, 4096);

final Map<String, Object> fields = new HashMap<>();
fields.put("sketch", agg.get());

final PostAggregator postAgg = new SketchToStringPostAggregator(
"summary",
new FieldAccessPostAggregator("field", "sketch")
);

final String summary = (String) postAgg.compute(fields);
Assert.assertNotNull(summary);
Assert.assertTrue(summary.contains("SUMMARY"));
}

}
Expand Up @@ -48,8 +48,7 @@ public class PostAggregatorIds
public static final byte MOMENTS_SKETCH_TO_QUANTILES_CACHE_TYPE_ID = 24;
public static final byte MOMENTS_SKETCH_TO_MIN_CACHE_TYPE_ID = 25;
public static final byte MOMENTS_SKETCH_TO_MAX_CACHE_TYPE_ID = 26;

// Datasketches Quantiles sketch aggregator (part 2)
public static final byte QUANTILES_DOUBLES_SKETCH_TO_RANK_CACHE_TYPE_ID = 27;
public static final byte QUANTILES_DOUBLES_SKETCH_TO_CDF_CACHE_TYPE_ID = 28;
public static final byte THETA_SKETCH_TO_STRING = 29;
}