use of org.apache.druid.query.aggregation.PostAggregator in project druid by druid-io.
the class QueriesTest method testVerifyAggregationsMultiLevelMissingVal.
@Test
public void testVerifyAggregationsMultiLevelMissingVal() {
List<AggregatorFactory> aggFactories = Arrays.asList(new CountAggregatorFactory("count"), new DoubleSumAggregatorFactory("idx", "index"), new DoubleSumAggregatorFactory("rev", "revenue"));
List<PostAggregator> postAggs = Arrays.asList(new ArithmeticPostAggregator("divideStuff", "/", Arrays.asList(new ArithmeticPostAggregator("addStuff", "+", Arrays.asList(new FieldAccessPostAggregator("idx", "idx"), new ConstantPostAggregator("const", 1))), new ArithmeticPostAggregator("subtractStuff", "-", Arrays.asList(new FieldAccessPostAggregator("rev", "rev2"), new ConstantPostAggregator("const", 1))))), new ArithmeticPostAggregator("addStuff", "+", Arrays.asList(new FieldAccessPostAggregator("divideStuff", "divideStuff"), new FieldAccessPostAggregator("count", "count"))));
boolean exceptionOccured = false;
try {
Queries.prepareAggregations(ImmutableList.of(), aggFactories, postAggs);
} catch (IllegalArgumentException e) {
exceptionOccured = true;
}
Assert.assertTrue(exceptionOccured);
}
use of org.apache.druid.query.aggregation.PostAggregator in project druid by druid-io.
the class NumericTopNMetricSpec method verifyPreconditions.
@Override
public void verifyPreconditions(List<AggregatorFactory> aggregatorSpecs, List<PostAggregator> postAggregatorSpecs) {
Preconditions.checkNotNull(metric, "metric can't be null");
Preconditions.checkNotNull(aggregatorSpecs, "aggregations cannot be null");
Preconditions.checkArgument(aggregatorSpecs.size() > 0 || postAggregatorSpecs.size() > 0, "Must have at least one AggregatorFactory or PostAggregator");
final AggregatorFactory aggregator = Iterables.tryFind(aggregatorSpecs, new Predicate<AggregatorFactory>() {
@Override
public boolean apply(AggregatorFactory input) {
return input.getName().equals(metric);
}
}).orNull();
final PostAggregator postAggregator = Iterables.tryFind(postAggregatorSpecs, new Predicate<PostAggregator>() {
@Override
public boolean apply(PostAggregator input) {
return input.getName().equals(metric);
}
}).orNull();
Preconditions.checkArgument(aggregator != null || postAggregator != null, "Must have an AggregatorFactory or PostAggregator for metric[%s], gave[%s] and [%s]", metric, aggregatorSpecs, postAggregatorSpecs);
}
use of org.apache.druid.query.aggregation.PostAggregator in project druid by druid-io.
the class Expressions method rexCallToDruidExpression.
private static DruidExpression rexCallToDruidExpression(final PlannerContext plannerContext, final RowSignature rowSignature, final RexNode rexNode, final PostAggregatorVisitor postAggregatorVisitor) {
final SqlOperator operator = ((RexCall) rexNode).getOperator();
final SqlOperatorConversion conversion = plannerContext.getOperatorTable().lookupOperatorConversion(operator);
if (conversion == null) {
plannerContext.setPlanningError("SQL query requires '%s' operator that is not supported.", operator.getName());
return null;
} else {
if (postAggregatorVisitor != null) {
// try making postagg first
PostAggregator postAggregator = conversion.toPostAggregator(plannerContext, rowSignature, rexNode, postAggregatorVisitor);
if (postAggregator != null) {
postAggregatorVisitor.addPostAgg(postAggregator);
String exprName = postAggregator.getName();
return DruidExpression.ofColumn(postAggregator.getType(rowSignature), exprName);
}
}
DruidExpression expression = conversion.toDruidExpressionWithPostAggOperands(plannerContext, rowSignature, rexNode, postAggregatorVisitor);
return expression;
}
}
use of org.apache.druid.query.aggregation.PostAggregator in project druid by druid-io.
the class ResultRow method fromLegacyRow.
/**
* Create a row based on a legacy {@link Row} that was generated by a given {@link GroupByQuery}. This is useful
* for deserializing rows that have come off the wire in the older format. (In the past, GroupBy query results
* were sequences of {@link Row}, not ResultRow.)
*
* @param row legacy row
* @param query query corresponding to the output ResultRow
*/
public static ResultRow fromLegacyRow(Row row, final GroupByQuery query) {
// Can't be sure if we'll get result rows with or without postaggregations, so be safe.
final ResultRow resultRow = ResultRow.create(query.getResultRowSizeWithPostAggregators());
int i = 0;
if (query.getResultRowHasTimestamp()) {
resultRow.set(i++, row.getTimestamp().getMillis());
}
for (DimensionSpec dimensionSpec : query.getDimensions()) {
resultRow.set(i++, row.getRaw(dimensionSpec.getOutputName()));
}
for (AggregatorFactory aggregatorFactory : query.getAggregatorSpecs()) {
resultRow.set(i++, row.getRaw(aggregatorFactory.getName()));
}
for (PostAggregator postAggregator : query.getPostAggregatorSpecs()) {
resultRow.set(i++, row.getRaw(postAggregator.getName()));
}
return resultRow;
}
use of org.apache.druid.query.aggregation.PostAggregator in project druid by druid-io.
the class GroupByQueryHelper method toResultRow.
public static ResultRow toResultRow(final GroupByQuery query, final Row row) {
final ResultRow resultRow = ResultRow.create(query.getResultRowSizeWithPostAggregators());
int i = 0;
if (query.getResultRowHasTimestamp()) {
resultRow.set(i++, row.getTimestampFromEpoch());
}
for (DimensionSpec dimensionSpec : query.getDimensions()) {
resultRow.set(i++, row.getRaw(dimensionSpec.getOutputName()));
}
for (AggregatorFactory aggregatorFactory : query.getAggregatorSpecs()) {
resultRow.set(i++, row.getRaw(aggregatorFactory.getName()));
}
for (PostAggregator postAggregator : query.getPostAggregatorSpecs()) {
resultRow.set(i++, row.getRaw(postAggregator.getName()));
}
return resultRow;
}
Aggregations