lazily create comparators for row columns when needed

This commit is contained in:
Himanshu Gupta 2016-02-02 23:36:25 -06:00
parent a737877680
commit 29e0d7f971
4 changed files with 55 additions and 11 deletions

View File

@ -628,9 +628,6 @@ public class GroupByQuery extends BaseQuery<Row>
if (limitSpec != null ? !limitSpec.equals(that.limitSpec) : that.limitSpec != null) {
return false;
}
if (limitFn != null ? !limitFn.equals(that.limitFn) : that.limitFn != null) {
return false;
}
if (postAggregatorSpecs != null
? !postAggregatorSpecs.equals(that.postAggregatorSpecs)
: that.postAggregatorSpecs != null) {

View File

@ -115,24 +115,32 @@ public class DefaultLimitSpec implements LimitSpec
}
};
Map<String, Ordering<Row>> possibleOrderings = Maps.newTreeMap(String.CASE_INSENSITIVE_ORDER);
Map<String, DimensionSpec> dimensionsMap = Maps.newHashMap();
for (DimensionSpec spec : dimensions) {
final String dimension = spec.getOutputName();
possibleOrderings.put(dimension, dimensionOrdering(dimension));
dimensionsMap.put(spec.getOutputName(), spec);
}
Map<String, AggregatorFactory> aggregatorsMap = Maps.newHashMap();
for (final AggregatorFactory agg : aggs) {
final String column = agg.getName();
possibleOrderings.put(column, metricOrdering(column, agg.getComparator()));
aggregatorsMap.put(agg.getName(), agg);
}
Map<String, PostAggregator> postAggregatorsMap = Maps.newHashMap();
for (PostAggregator postAgg : postAggs) {
final String column = postAgg.getName();
possibleOrderings.put(column, metricOrdering(column, postAgg.getComparator()));
postAggregatorsMap.put(postAgg.getName(), postAgg);
}
for (OrderByColumnSpec columnSpec : columns) {
Ordering<Row> nextOrdering = possibleOrderings.get(columnSpec.getDimension());
String columnName = columnSpec.getDimension();
Ordering<Row> nextOrdering = null;
if (postAggregatorsMap.containsKey(columnName)) {
nextOrdering = metricOrdering(columnName, postAggregatorsMap.get(columnName).getComparator());
} else if (aggregatorsMap.containsKey(columnName)) {
nextOrdering = metricOrdering(columnName, aggregatorsMap.get(columnName).getComparator());
} else if (dimensionsMap.containsKey(columnName)) {
nextOrdering = dimensionOrdering(columnName);
}
if (nextOrdering == null) {
throw new ISE("Unknown column in order clause[%s]", columnSpec);

View File

@ -172,6 +172,33 @@ public class OrderByColumnSpec
return direction;
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
OrderByColumnSpec that = (OrderByColumnSpec) o;
if (!dimension.equals(that.dimension)) {
return false;
}
return direction == that.direction;
}
@Override
public int hashCode()
{
int result = dimension.hashCode();
result = 31 * result + direction.hashCode();
return result;
}
@Override
public String toString()
{

View File

@ -20,14 +20,19 @@
package io.druid.query.groupby;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import io.druid.jackson.DefaultObjectMapper;
import io.druid.query.Query;
import io.druid.query.QueryRunnerTestHelper;
import io.druid.query.aggregation.AggregatorFactory;
import io.druid.query.aggregation.LongSumAggregatorFactory;
import io.druid.query.aggregation.PostAggregator;
import io.druid.query.aggregation.post.FieldAccessPostAggregator;
import io.druid.query.dimension.DefaultDimensionSpec;
import io.druid.query.dimension.DimensionSpec;
import io.druid.query.groupby.orderby.DefaultLimitSpec;
import io.druid.query.groupby.orderby.OrderByColumnSpec;
import org.junit.Assert;
import org.junit.Test;
@ -53,6 +58,13 @@ public class GroupByQueryTest
)
)
.setGranularity(QueryRunnerTestHelper.dayGran)
.setPostAggregatorSpecs(ImmutableList.<PostAggregator>of(new FieldAccessPostAggregator("x", "idx")))
.setLimitSpec(
new DefaultLimitSpec(
ImmutableList.of(new OrderByColumnSpec("alias", OrderByColumnSpec.Direction.ASCENDING)),
100
)
)
.build();
String json = jsonMapper.writeValueAsString(query);