Search in sources :

Example 11 with PostAggregator

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);
}
Also used : ArithmeticPostAggregator(org.apache.druid.query.aggregation.post.ArithmeticPostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) ArithmeticPostAggregator(org.apache.druid.query.aggregation.post.ArithmeticPostAggregator) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) ConstantPostAggregator(org.apache.druid.query.aggregation.post.ConstantPostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) ConstantPostAggregator(org.apache.druid.query.aggregation.post.ConstantPostAggregator) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) Test(org.junit.Test)

Example 12 with PostAggregator

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);
}
Also used : PostAggregator(org.apache.druid.query.aggregation.PostAggregator) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) Predicate(com.google.common.base.Predicate)

Example 13 with PostAggregator

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;
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) SqlOperator(org.apache.calcite.sql.SqlOperator)

Example 14 with PostAggregator

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;
}
Also used : DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory)

Example 15 with PostAggregator

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;
}
Also used : DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory)

Aggregations

PostAggregator (org.apache.druid.query.aggregation.PostAggregator)135 Test (org.junit.Test)98 FieldAccessPostAggregator (org.apache.druid.query.aggregation.post.FieldAccessPostAggregator)48 ConstantPostAggregator (org.apache.druid.query.aggregation.post.ConstantPostAggregator)41 HashMap (java.util.HashMap)29 DefaultObjectMapper (org.apache.druid.jackson.DefaultObjectMapper)21 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)20 Nullable (javax.annotation.Nullable)16 Aggregator (org.apache.druid.query.aggregation.Aggregator)13 Comparator (java.util.Comparator)12 RexCall (org.apache.calcite.rex.RexCall)11 RexNode (org.apache.calcite.rex.RexNode)11 CountAggregator (org.apache.druid.query.aggregation.CountAggregator)10 ArrayList (java.util.ArrayList)9 TestDoubleColumnSelectorImpl (org.apache.druid.query.aggregation.TestDoubleColumnSelectorImpl)9 Map (java.util.Map)8 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)8 Function (com.google.common.base.Function)7 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)7 List (java.util.List)6