Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial SQL support for non-expression sketch postaggs (#8487)
* Add initial SQL support for non-expression sketch postaggs * Checkstyle, spotbugs * checkstyle * imports * Update SQL docs * Checkstyle * Fix theta sketch operator docs * PR comments * Checkstyle fixes * Add missing entries for HLL sketch module * PR comments, add round param to HLL estimate operator, fix optional HLL param
- Loading branch information
Showing
39 changed files
with
3,038 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
...rg/apache/druid/query/aggregation/datasketches/hll/HllSketchToEstimatePostAggregator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* 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.hll; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.yahoo.sketches.hll.HllSketch; | ||
import org.apache.druid.query.aggregation.AggregatorFactory; | ||
import org.apache.druid.query.aggregation.PostAggregator; | ||
import org.apache.druid.query.aggregation.post.ArithmeticPostAggregator; | ||
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 distinct count estimate a from a given {@link HllSketch}. | ||
* The result will be a double value. | ||
*/ | ||
public class HllSketchToEstimatePostAggregator implements PostAggregator | ||
{ | ||
private final String name; | ||
private final PostAggregator field; | ||
private final boolean round; | ||
|
||
@JsonCreator | ||
public HllSketchToEstimatePostAggregator( | ||
@JsonProperty("name") final String name, | ||
@JsonProperty("field") final PostAggregator field, | ||
@JsonProperty("round") boolean round | ||
) | ||
{ | ||
this.name = name; | ||
this.field = field; | ||
this.round = round; | ||
} | ||
|
||
@Override | ||
@JsonProperty | ||
public String getName() | ||
{ | ||
return name; | ||
} | ||
|
||
@JsonProperty | ||
public PostAggregator getField() | ||
{ | ||
return field; | ||
} | ||
|
||
@JsonProperty | ||
public boolean isRound() | ||
{ | ||
return round; | ||
} | ||
|
||
@Override | ||
public Set<String> getDependentFields() | ||
{ | ||
return field.getDependentFields(); | ||
} | ||
|
||
@Override | ||
public Comparator<Double> getComparator() | ||
{ | ||
return ArithmeticPostAggregator.DEFAULT_COMPARATOR; | ||
} | ||
|
||
@Override | ||
public Object compute(final Map<String, Object> combinedAggregators) | ||
{ | ||
final HllSketch sketch = (HllSketch) field.compute(combinedAggregators); | ||
return round ? Math.round(sketch.getEstimate()) : sketch.getEstimate(); | ||
} | ||
|
||
@Override | ||
public PostAggregator decorate(final Map<String, AggregatorFactory> aggregators) | ||
{ | ||
return this; | ||
} | ||
|
||
@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 HllSketchToEstimatePostAggregator)) { | ||
return false; | ||
} | ||
|
||
final HllSketchToEstimatePostAggregator that = (HllSketchToEstimatePostAggregator) 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.HLL_SKETCH_TO_ESTIMATE_CACHE_TYPE_ID) | ||
.appendCacheable(field) | ||
.build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.