io.druid.query.aggregation.post.DoubleGreatestPostAggregator Maven / Gradle / Ivy
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets 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 io.druid.query.aggregation.post;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import com.google.common.collect.Sets;
import io.druid.query.Queries;
import io.druid.query.aggregation.AggregatorFactory;
import io.druid.query.aggregation.PostAggregator;
import io.druid.query.cache.CacheKeyBuilder;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DoubleGreatestPostAggregator implements PostAggregator
{
private static final Comparator COMPARATOR = new Comparator()
{
@Override
public int compare(Object o, Object o1)
{
return ((Double) o).compareTo((Double) o1);
}
};
private final String name;
private final List fields;
@JsonCreator
public DoubleGreatestPostAggregator(
@JsonProperty("name") String name,
@JsonProperty("fields") List fields
)
{
Preconditions.checkArgument(fields != null && fields.size() > 0, "Illegal number of fields[%s], must be > 0");
this.name = name;
this.fields = fields;
}
@Override
public Set getDependentFields()
{
Set dependentFields = Sets.newHashSet();
for (PostAggregator field : fields) {
dependentFields.addAll(field.getDependentFields());
}
return dependentFields;
}
@Override
public Comparator getComparator()
{
return COMPARATOR;
}
@Override
public Object compute(Map values)
{
Iterator fieldsIter = fields.iterator();
double retVal = Double.NEGATIVE_INFINITY;
if (fieldsIter.hasNext()) {
retVal = ((Number) fieldsIter.next().compute(values)).doubleValue();
while (fieldsIter.hasNext()) {
double other = ((Number) fieldsIter.next().compute(values)).doubleValue();
if (other > retVal) {
retVal = other;
}
}
}
return retVal;
}
@JsonProperty
@Override
public String getName()
{
return name;
}
@Override
public DoubleGreatestPostAggregator decorate(Map aggregators)
{
return new DoubleGreatestPostAggregator(name, Queries.decoratePostAggregators(fields, aggregators));
}
@JsonProperty
public List getFields()
{
return fields;
}
@Override
public String toString()
{
return "DoubleGreatestPostAggregator{" +
"name='" + name + '\'' +
", fields=" + fields +
"}";
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DoubleGreatestPostAggregator that = (DoubleGreatestPostAggregator) o;
if (!fields.equals(that.fields)) {
return false;
}
if (name != null ? !name.equals(that.name) : that.name != null) {
return false;
}
return true;
}
@Override
public int hashCode()
{
int result = name != null ? name.hashCode() : 0;
result = 31 * result + fields.hashCode();
return result;
}
@Override
public byte[] getCacheKey()
{
return new CacheKeyBuilder(PostAggregatorIds.DOUBLE_GREATEST)
.appendCacheablesIgnoringOrder(fields)
.build();
}
}