Search in sources :

Example 11 with ExpressionLambdaAggregatorFactory

use of org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory in project druid by druid-io.

the class GroupByQueryRunnerTest method testGroupByExpressionAggregatorArrayMultiValue.

@Test
public void testGroupByExpressionAggregatorArrayMultiValue() {
    // expression agg not yet vectorized
    cannotVectorize();
    // array types don't work with group by v1
    if (config.getDefaultStrategy().equals(GroupByStrategySelector.STRATEGY_V1)) {
        expectedException.expect(IllegalStateException.class);
        expectedException.expectMessage("Unable to handle type[ARRAY<STRING>] for AggregatorFactory[class org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory]");
    }
    GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("quality", "alias")).setAggregatorSpecs(new ExpressionLambdaAggregatorFactory("array_agg_distinct", ImmutableSet.of(QueryRunnerTestHelper.PLACEMENTISH_DIMENSION), "acc", "[]", null, null, true, false, "array_set_add(acc, placementish)", "array_set_add_all(acc, array_agg_distinct)", null, null, null, TestExprMacroTable.INSTANCE)).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
    List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "alias", "automotive", "array_agg_distinct", new String[] { "a", "preferred" }), makeRow(query, "2011-04-01", "alias", "business", "array_agg_distinct", new String[] { "b", "preferred" }), makeRow(query, "2011-04-01", "alias", "entertainment", "array_agg_distinct", new String[] { "e", "preferred" }), makeRow(query, "2011-04-01", "alias", "health", "array_agg_distinct", new String[] { "h", "preferred" }), makeRow(query, "2011-04-01", "alias", "mezzanine", "array_agg_distinct", new String[] { "m", "preferred" }), makeRow(query, "2011-04-01", "alias", "news", "array_agg_distinct", new String[] { "n", "preferred" }), makeRow(query, "2011-04-01", "alias", "premium", "array_agg_distinct", new String[] { "p", "preferred" }), makeRow(query, "2011-04-01", "alias", "technology", "array_agg_distinct", new String[] { "preferred", "t" }), makeRow(query, "2011-04-01", "alias", "travel", "array_agg_distinct", new String[] { "preferred", "t" }), makeRow(query, "2011-04-02", "alias", "automotive", "array_agg_distinct", new String[] { "a", "preferred" }), makeRow(query, "2011-04-02", "alias", "business", "array_agg_distinct", new String[] { "b", "preferred" }), makeRow(query, "2011-04-02", "alias", "entertainment", "array_agg_distinct", new String[] { "e", "preferred" }), makeRow(query, "2011-04-02", "alias", "health", "array_agg_distinct", new String[] { "h", "preferred" }), makeRow(query, "2011-04-02", "alias", "mezzanine", "array_agg_distinct", new String[] { "m", "preferred" }), makeRow(query, "2011-04-02", "alias", "news", "array_agg_distinct", new String[] { "n", "preferred" }), makeRow(query, "2011-04-02", "alias", "premium", "array_agg_distinct", new String[] { "p", "preferred" }), makeRow(query, "2011-04-02", "alias", "technology", "array_agg_distinct", new String[] { "preferred", "t" }), makeRow(query, "2011-04-02", "alias", "travel", "array_agg_distinct", new String[] { "preferred", "t" }));
    Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "groupBy");
}
Also used : ExpressionLambdaAggregatorFactory(org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 12 with ExpressionLambdaAggregatorFactory

use of org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory in project druid by druid-io.

the class ArrayConcatSqlAggregator method toDruidAggregation.

@Nullable
@Override
public Aggregation toDruidAggregation(PlannerContext plannerContext, RowSignature rowSignature, VirtualColumnRegistry virtualColumnRegistry, RexBuilder rexBuilder, String name, AggregateCall aggregateCall, Project project, List<Aggregation> existingAggregations, boolean finalizeAggregations) {
    final List<RexNode> arguments = aggregateCall.getArgList().stream().map(i -> Expressions.fromFieldAccess(rowSignature, project, i)).collect(Collectors.toList());
    Integer maxSizeBytes = null;
    if (arguments.size() > 1) {
        RexNode maxBytes = arguments.get(1);
        if (!maxBytes.isA(SqlKind.LITERAL)) {
            // maxBytes must be a literal
            return null;
        }
        maxSizeBytes = ((Number) RexLiteral.value(maxBytes)).intValue();
    }
    final DruidExpression arg = Expressions.toDruidExpression(plannerContext, rowSignature, arguments.get(0));
    final ExprMacroTable macroTable = plannerContext.getExprMacroTable();
    final String fieldName;
    final ColumnType druidType = Calcites.getValueTypeForRelDataTypeFull(aggregateCall.getType());
    if (druidType == null || !druidType.isArray()) {
        // must be an array
        return null;
    }
    final String initialvalue = ExpressionType.fromColumnTypeStrict(druidType).asTypeString() + "[]";
    if (arg.isDirectColumnAccess()) {
        fieldName = arg.getDirectColumn();
    } else {
        VirtualColumn vc = virtualColumnRegistry.getOrCreateVirtualColumnForExpression(plannerContext, arg, druidType);
        fieldName = vc.getOutputName();
    }
    if (aggregateCall.isDistinct()) {
        return Aggregation.create(new ExpressionLambdaAggregatorFactory(name, ImmutableSet.of(fieldName), null, initialvalue, null, true, false, false, StringUtils.format("array_set_add_all(\"__acc\", \"%s\")", fieldName), StringUtils.format("array_set_add_all(\"__acc\", \"%s\")", name), null, null, maxSizeBytes != null ? new HumanReadableBytes(maxSizeBytes) : null, macroTable));
    } else {
        return Aggregation.create(new ExpressionLambdaAggregatorFactory(name, ImmutableSet.of(fieldName), null, initialvalue, null, true, false, false, StringUtils.format("array_concat(\"__acc\", \"%s\")", fieldName), StringUtils.format("array_concat(\"__acc\", \"%s\")", name), null, null, maxSizeBytes != null ? new HumanReadableBytes(maxSizeBytes) : null, macroTable));
    }
}
Also used : Project(org.apache.calcite.rel.core.Project) SqlAggregator(org.apache.druid.sql.calcite.aggregation.SqlAggregator) ReturnTypes(org.apache.calcite.sql.type.ReturnTypes) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) HumanReadableBytes(org.apache.druid.java.util.common.HumanReadableBytes) Optionality(org.apache.calcite.util.Optionality) RexNode(org.apache.calcite.rex.RexNode) ExpressionType(org.apache.druid.math.expr.ExpressionType) VirtualColumnRegistry(org.apache.druid.sql.calcite.rel.VirtualColumnRegistry) PlannerContext(org.apache.druid.sql.calcite.planner.PlannerContext) Nullable(javax.annotation.Nullable) ImmutableSet(com.google.common.collect.ImmutableSet) SqlKind(org.apache.calcite.sql.SqlKind) ExpressionLambdaAggregatorFactory(org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory) InferTypes(org.apache.calcite.sql.type.InferTypes) RexBuilder(org.apache.calcite.rex.RexBuilder) RexLiteral(org.apache.calcite.rex.RexLiteral) VirtualColumn(org.apache.druid.segment.VirtualColumn) SqlFunctionCategory(org.apache.calcite.sql.SqlFunctionCategory) StringUtils(org.apache.druid.java.util.common.StringUtils) Aggregation(org.apache.druid.sql.calcite.aggregation.Aggregation) Collectors(java.util.stream.Collectors) ExprMacroTable(org.apache.druid.math.expr.ExprMacroTable) List(java.util.List) RowSignature(org.apache.druid.segment.column.RowSignature) OperandTypes(org.apache.calcite.sql.type.OperandTypes) ColumnType(org.apache.druid.segment.column.ColumnType) AggregateCall(org.apache.calcite.rel.core.AggregateCall) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) Calcites(org.apache.druid.sql.calcite.planner.Calcites) Expressions(org.apache.druid.sql.calcite.expression.Expressions) ColumnType(org.apache.druid.segment.column.ColumnType) ExpressionLambdaAggregatorFactory(org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory) DruidExpression(org.apache.druid.sql.calcite.expression.DruidExpression) VirtualColumn(org.apache.druid.segment.VirtualColumn) HumanReadableBytes(org.apache.druid.java.util.common.HumanReadableBytes) ExprMacroTable(org.apache.druid.math.expr.ExprMacroTable) RexNode(org.apache.calcite.rex.RexNode) Nullable(javax.annotation.Nullable)

Example 13 with ExpressionLambdaAggregatorFactory

use of org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory in project druid by druid-io.

the class GroupByQueryRunnerTest method testGroupByWithExpressionAggregatorWithComplex.

@Test
public void testGroupByWithExpressionAggregatorWithComplex() {
    cannotVectorize();
    final GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FULL_ON_INTERVAL_SPEC).setDimensions(Collections.emptyList()).setAggregatorSpecs(new CardinalityAggregatorFactory("car", ImmutableList.of(new DefaultDimensionSpec("quality", "quality")), false), new ExpressionLambdaAggregatorFactory("carExpr", ImmutableSet.of("quality"), null, "hyper_unique()", null, null, false, false, "hyper_unique_add(quality, __acc)", "hyper_unique_add(carExpr, __acc)", null, "hyper_unique_estimate(o)", null, TestExprMacroTable.INSTANCE)).setGranularity(QueryRunnerTestHelper.ALL_GRAN).build();
    List<ResultRow> expectedResults = Collections.singletonList(makeRow(query, "1970-01-01", "car", QueryRunnerTestHelper.UNIQUES_9, "carExpr", QueryRunnerTestHelper.UNIQUES_9));
    Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "subquery-cardinality");
}
Also used : ExpressionLambdaAggregatorFactory(org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory) CardinalityAggregatorFactory(org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 14 with ExpressionLambdaAggregatorFactory

use of org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory in project druid by druid-io.

the class GroupByQueryRunnerTest method testGroupByWithExpressionAggregatorWithArrays.

@Test
public void testGroupByWithExpressionAggregatorWithArrays() {
    // expression agg not yet vectorized
    cannotVectorize();
    // array types don't work with group by v1
    if (config.getDefaultStrategy().equals(GroupByStrategySelector.STRATEGY_V1)) {
        expectedException.expect(IllegalStateException.class);
        expectedException.expectMessage("Unable to handle type[ARRAY<STRING>] for AggregatorFactory[class org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory]");
    }
    GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("quality", "alias")).setAggregatorSpecs(new ExpressionLambdaAggregatorFactory("rows", Collections.emptySet(), null, "0", null, false, false, false, "__acc + 1", "__acc + rows", null, null, null, TestExprMacroTable.INSTANCE), new ExpressionLambdaAggregatorFactory("idx", ImmutableSet.of("index"), null, "0.0", null, true, false, false, "__acc + index", null, null, null, null, TestExprMacroTable.INSTANCE), new ExpressionLambdaAggregatorFactory("array_agg_distinct", ImmutableSet.of(QueryRunnerTestHelper.MARKET_DIMENSION), "acc", "[]", null, null, true, false, "array_set_add(acc, market)", "array_set_add_all(acc, array_agg_distinct)", null, null, null, TestExprMacroTable.INSTANCE)).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
    List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "alias", "automotive", "rows", 1L, "idx", 135.88510131835938d, "array_agg_distinct", new String[] { "spot" }), makeRow(query, "2011-04-01", "alias", "business", "rows", 1L, "idx", 118.57034, "array_agg_distinct", new String[] { "spot" }), makeRow(query, "2011-04-01", "alias", "entertainment", "rows", 1L, "idx", 158.747224, "array_agg_distinct", new String[] { "spot" }), makeRow(query, "2011-04-01", "alias", "health", "rows", 1L, "idx", 120.134704, "array_agg_distinct", new String[] { "spot" }), makeRow(query, "2011-04-01", "alias", "mezzanine", "rows", 3L, "idx", 2871.8866900000003d, "array_agg_distinct", new String[] { "spot", "total_market", "upfront" }), makeRow(query, "2011-04-01", "alias", "news", "rows", 1L, "idx", 121.58358d, "array_agg_distinct", new String[] { "spot" }), makeRow(query, "2011-04-01", "alias", "premium", "rows", 3L, "idx", 2900.798647d, "array_agg_distinct", new String[] { "spot", "total_market", "upfront" }), makeRow(query, "2011-04-01", "alias", "technology", "rows", 1L, "idx", 78.622547d, "array_agg_distinct", new String[] { "spot" }), makeRow(query, "2011-04-01", "alias", "travel", "rows", 1L, "idx", 119.922742d, "array_agg_distinct", new String[] { "spot" }), makeRow(query, "2011-04-02", "alias", "automotive", "rows", 1L, "idx", 147.42593d, "array_agg_distinct", new String[] { "spot" }), makeRow(query, "2011-04-02", "alias", "business", "rows", 1L, "idx", 112.987027d, "array_agg_distinct", new String[] { "spot" }), makeRow(query, "2011-04-02", "alias", "entertainment", "rows", 1L, "idx", 166.016049d, "array_agg_distinct", new String[] { "spot" }), makeRow(query, "2011-04-02", "alias", "health", "rows", 1L, "idx", 113.446008d, "array_agg_distinct", new String[] { "spot" }), makeRow(query, "2011-04-02", "alias", "mezzanine", "rows", 3L, "idx", 2448.830613d, "array_agg_distinct", new String[] { "spot", "total_market", "upfront" }), makeRow(query, "2011-04-02", "alias", "news", "rows", 1L, "idx", 114.290141d, "array_agg_distinct", new String[] { "spot" }), makeRow(query, "2011-04-02", "alias", "premium", "rows", 3L, "idx", 2506.415148d, "array_agg_distinct", new String[] { "spot", "total_market", "upfront" }), makeRow(query, "2011-04-02", "alias", "technology", "rows", 1L, "idx", 97.387433d, "array_agg_distinct", new String[] { "spot" }), makeRow(query, "2011-04-02", "alias", "travel", "rows", 1L, "idx", 126.411364d, "array_agg_distinct", new String[] { "spot" }));
    Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "groupBy");
}
Also used : ExpressionLambdaAggregatorFactory(org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 15 with ExpressionLambdaAggregatorFactory

use of org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory in project druid by druid-io.

the class GroupByQueryRunnerTest method testGroupByWithExpressionAggregatorWithComplexOnSubquery.

@Test
public void testGroupByWithExpressionAggregatorWithComplexOnSubquery() {
    final GroupByQuery subquery = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FULL_ON_INTERVAL_SPEC).setDimensions(new DefaultDimensionSpec("market", "market"), new DefaultDimensionSpec("quality", "quality")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("index", "index")).setGranularity(QueryRunnerTestHelper.ALL_GRAN).build();
    final GroupByQuery query = makeQueryBuilder().setDataSource(subquery).setQuerySegmentSpec(QueryRunnerTestHelper.FULL_ON_INTERVAL_SPEC).setDimensions(Collections.emptyList()).setAggregatorSpecs(new CardinalityAggregatorFactory("car", ImmutableList.of(new DefaultDimensionSpec("quality", "quality")), false), new ExpressionLambdaAggregatorFactory("carExpr", ImmutableSet.of("quality"), null, "hyper_unique()", null, null, false, false, "hyper_unique_add(quality, __acc)", null, null, "hyper_unique_estimate(o)", null, TestExprMacroTable.INSTANCE)).setGranularity(QueryRunnerTestHelper.ALL_GRAN).build();
    List<ResultRow> expectedResults = Collections.singletonList(makeRow(query, "1970-01-01", "car", QueryRunnerTestHelper.UNIQUES_9, "carExpr", QueryRunnerTestHelper.UNIQUES_9));
    Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "subquery-cardinality");
}
Also used : ExpressionLambdaAggregatorFactory(org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) CardinalityAggregatorFactory(org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Aggregations

ExpressionLambdaAggregatorFactory (org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory)30 Test (org.junit.Test)25 FilteredAggregatorFactory (org.apache.druid.query.aggregation.FilteredAggregatorFactory)12 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)11 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)8 HumanReadableBytes (org.apache.druid.java.util.common.HumanReadableBytes)6 ImmutableSet (com.google.common.collect.ImmutableSet)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 Nullable (javax.annotation.Nullable)4 AggregateCall (org.apache.calcite.rel.core.AggregateCall)4 Project (org.apache.calcite.rel.core.Project)4 RexBuilder (org.apache.calcite.rex.RexBuilder)4 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)4 SqlFunctionCategory (org.apache.calcite.sql.SqlFunctionCategory)4 SqlKind (org.apache.calcite.sql.SqlKind)4 InferTypes (org.apache.calcite.sql.type.InferTypes)4 OperandTypes (org.apache.calcite.sql.type.OperandTypes)4 Optionality (org.apache.calcite.util.Optionality)4 StringUtils (org.apache.druid.java.util.common.StringUtils)4