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");
}
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));
}
}
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");
}
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");
}
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");
}
Aggregations