Search in sources :

Example 1 with ExpressionLambdaAggregatorFactory

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

the class CalciteQueryTest method testGroupByAggregatorDefaultValuesNonVectorized.

@Test
public void testGroupByAggregatorDefaultValuesNonVectorized() throws Exception {
    cannotVectorize();
    testQuery("SELECT\n" + " dim2,\n" + " ANY_VALUE(dim1, 1024) FILTER(WHERE dim1 = 'nonexistent'),\n" + " ANY_VALUE(l1) FILTER(WHERE dim1 = 'nonexistent'),\n" + " EARLIEST(dim1, 1024) FILTER(WHERE dim1 = 'nonexistent'),\n" + " EARLIEST(l1) FILTER(WHERE dim1 = 'nonexistent'),\n" + " LATEST(dim1, 1024) FILTER(WHERE dim1 = 'nonexistent'),\n" + " LATEST(l1) FILTER(WHERE dim1 = 'nonexistent'),\n" + " ARRAY_AGG(DISTINCT dim3) FILTER(WHERE dim1 = 'nonexistent'),\n" + " STRING_AGG(DISTINCT dim3, '|') FILTER(WHERE dim1 = 'nonexistent'),\n" + " BIT_AND(l1) FILTER(WHERE dim1 = 'nonexistent'),\n" + " BIT_OR(l1) FILTER(WHERE dim1 = 'nonexistent'),\n" + " BIT_XOR(l1) FILTER(WHERE dim1 = 'nonexistent')\n" + "FROM druid.numfoo WHERE dim2 = 'a' GROUP BY dim2", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE3).setInterval(querySegmentSpec(Filtration.eternity())).setDimFilter(selector("dim2", "a", null)).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "'a'", ColumnType.STRING)).setDimensions(new DefaultDimensionSpec("v0", "_d0", ColumnType.STRING)).setAggregatorSpecs(aggregators(new FilteredAggregatorFactory(new StringAnyAggregatorFactory("a0", "dim1", 1024), selector("dim1", "nonexistent", null)), new FilteredAggregatorFactory(new LongAnyAggregatorFactory("a1", "l1"), selector("dim1", "nonexistent", null)), new FilteredAggregatorFactory(new StringFirstAggregatorFactory("a2", "dim1", null, 1024), selector("dim1", "nonexistent", null)), new FilteredAggregatorFactory(new LongFirstAggregatorFactory("a3", "l1", null), selector("dim1", "nonexistent", null)), new FilteredAggregatorFactory(new StringLastAggregatorFactory("a4", "dim1", null, 1024), selector("dim1", "nonexistent", null)), new FilteredAggregatorFactory(new LongLastAggregatorFactory("a5", "l1", null), selector("dim1", "nonexistent", null)), new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a6", ImmutableSet.of("dim3"), "__acc", "ARRAY<STRING>[]", "ARRAY<STRING>[]", true, true, false, "array_set_add(\"__acc\", \"dim3\")", "array_set_add_all(\"__acc\", \"a6\")", null, null, ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), selector("dim1", "nonexistent", null)), new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a7", ImmutableSet.of("dim3"), "__acc", "[]", "[]", true, false, false, "array_set_add(\"__acc\", \"dim3\")", "array_set_add_all(\"__acc\", \"a7\")", null, "if(array_length(o) == 0, null, array_to_string(o, '|'))", ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), and(not(selector("dim3", null, null)), selector("dim1", "nonexistent", null))), new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a8", ImmutableSet.of("l1"), "__acc", "0", "0", NullHandling.sqlCompatible(), false, false, "bitwiseAnd(\"__acc\", \"l1\")", "bitwiseAnd(\"__acc\", \"a8\")", null, null, ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), and(not(selector("l1", null, null)), selector("dim1", "nonexistent", null))), new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a9", ImmutableSet.of("l1"), "__acc", "0", "0", NullHandling.sqlCompatible(), false, false, "bitwiseOr(\"__acc\", \"l1\")", "bitwiseOr(\"__acc\", \"a9\")", null, null, ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), and(not(selector("l1", null, null)), selector("dim1", "nonexistent", null))), new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a10", ImmutableSet.of("l1"), "__acc", "0", "0", NullHandling.sqlCompatible(), false, false, "bitwiseXor(\"__acc\", \"l1\")", "bitwiseXor(\"__acc\", \"a10\")", null, null, ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), and(not(selector("l1", null, null)), selector("dim1", "nonexistent", null))))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(useDefault ? new Object[] { "a", "", 0L, "", 0L, "", 0L, null, "", 0L, 0L, 0L } : new Object[] { "a", null, null, null, null, null, null, null, null, null, null, null }));
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) LongLastAggregatorFactory(org.apache.druid.query.aggregation.last.LongLastAggregatorFactory) ExpressionLambdaAggregatorFactory(org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory) LongAnyAggregatorFactory(org.apache.druid.query.aggregation.any.LongAnyAggregatorFactory) StringAnyAggregatorFactory(org.apache.druid.query.aggregation.any.StringAnyAggregatorFactory) StringLastAggregatorFactory(org.apache.druid.query.aggregation.last.StringLastAggregatorFactory) StringFirstAggregatorFactory(org.apache.druid.query.aggregation.first.StringFirstAggregatorFactory) LongFirstAggregatorFactory(org.apache.druid.query.aggregation.first.LongFirstAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 2 with ExpressionLambdaAggregatorFactory

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

the class CalciteQueryTest method testStringAggMaxBytes.

@Test
public void testStringAggMaxBytes() throws Exception {
    cannotVectorize();
    testQuery("SELECT STRING_AGG(l1, ',', 128), STRING_AGG(DISTINCT l1, ',', 128) FROM numfoo", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE3).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).aggregators(aggregators(new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a0", ImmutableSet.of("l1"), "__acc", "[]", "[]", true, false, false, "array_append(\"__acc\", \"l1\")", "array_concat(\"__acc\", \"a0\")", null, "if(array_length(o) == 0, null, array_to_string(o, ','))", new HumanReadableBytes(128), TestExprMacroTable.INSTANCE), not(selector("l1", null, null))), new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a1", ImmutableSet.of("l1"), "__acc", "[]", "[]", true, false, false, "array_set_add(\"__acc\", \"l1\")", "array_set_add_all(\"__acc\", \"a1\")", null, "if(array_length(o) == 0, null, array_to_string(o, ','))", new HumanReadableBytes(128), TestExprMacroTable.INSTANCE), not(selector("l1", null, null))))).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(useDefault ? new Object[] { "7,325323,0,0,0,0", "0,325323,7" } : new Object[] { "7,325323,0", "0,325323,7" }));
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) ExpressionLambdaAggregatorFactory(org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory) HumanReadableBytes(org.apache.druid.java.util.common.HumanReadableBytes) Test(org.junit.Test)

Example 3 with ExpressionLambdaAggregatorFactory

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

the class CalciteQueryTest method testTimeseriesEmptyResultsAggregatorDefaultValuesNonVectorized.

@Test
public void testTimeseriesEmptyResultsAggregatorDefaultValuesNonVectorized() throws Exception {
    cannotVectorize();
    // timeseries with all granularity have a single group, so should return default results for given aggregators
    testQuery("SELECT\n" + " ANY_VALUE(dim1, 1024),\n" + " ANY_VALUE(l1),\n" + " EARLIEST(dim1, 1024),\n" + " EARLIEST(l1),\n" + " LATEST(dim1, 1024),\n" + " LATEST(l1),\n" + " ARRAY_AGG(DISTINCT dim3),\n" + " STRING_AGG(DISTINCT dim3, '|'),\n" + " BIT_AND(l1),\n" + " BIT_OR(l1),\n" + " BIT_XOR(l1)\n" + "FROM druid.numfoo WHERE dim2 = 0", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE3).intervals(querySegmentSpec(Filtration.eternity())).filters(bound("dim2", "0", "0", false, false, null, StringComparators.NUMERIC)).granularity(Granularities.ALL).aggregators(aggregators(new StringAnyAggregatorFactory("a0", "dim1", 1024), new LongAnyAggregatorFactory("a1", "l1"), new StringFirstAggregatorFactory("a2", "dim1", null, 1024), new LongFirstAggregatorFactory("a3", "l1", null), new StringLastAggregatorFactory("a4", "dim1", null, 1024), new LongLastAggregatorFactory("a5", "l1", null), new ExpressionLambdaAggregatorFactory("a6", ImmutableSet.of("dim3"), "__acc", "ARRAY<STRING>[]", "ARRAY<STRING>[]", true, true, false, "array_set_add(\"__acc\", \"dim3\")", "array_set_add_all(\"__acc\", \"a6\")", null, null, ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a7", ImmutableSet.of("dim3"), "__acc", "[]", "[]", true, false, false, "array_set_add(\"__acc\", \"dim3\")", "array_set_add_all(\"__acc\", \"a7\")", null, "if(array_length(o) == 0, null, array_to_string(o, '|'))", ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), not(selector("dim3", null, null))), new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a8", ImmutableSet.of("l1"), "__acc", "0", "0", NullHandling.sqlCompatible(), false, false, "bitwiseAnd(\"__acc\", \"l1\")", "bitwiseAnd(\"__acc\", \"a8\")", null, null, ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), not(selector("l1", null, null))), new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a9", ImmutableSet.of("l1"), "__acc", "0", "0", NullHandling.sqlCompatible(), false, false, "bitwiseOr(\"__acc\", \"l1\")", "bitwiseOr(\"__acc\", \"a9\")", null, null, ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), not(selector("l1", null, null))), new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a10", ImmutableSet.of("l1"), "__acc", "0", "0", NullHandling.sqlCompatible(), false, false, "bitwiseXor(\"__acc\", \"l1\")", "bitwiseXor(\"__acc\", \"a10\")", null, null, ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), not(selector("l1", null, null))))).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(useDefault ? new Object[] { "", 0L, "", 0L, "", 0L, null, "", 0L, 0L, 0L } : new Object[] { null, null, null, null, null, null, null, null, null, null, null }));
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) LongLastAggregatorFactory(org.apache.druid.query.aggregation.last.LongLastAggregatorFactory) ExpressionLambdaAggregatorFactory(org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory) LongAnyAggregatorFactory(org.apache.druid.query.aggregation.any.LongAnyAggregatorFactory) StringAnyAggregatorFactory(org.apache.druid.query.aggregation.any.StringAnyAggregatorFactory) StringLastAggregatorFactory(org.apache.druid.query.aggregation.last.StringLastAggregatorFactory) StringFirstAggregatorFactory(org.apache.druid.query.aggregation.first.StringFirstAggregatorFactory) LongFirstAggregatorFactory(org.apache.druid.query.aggregation.first.LongFirstAggregatorFactory) Test(org.junit.Test)

Example 4 with ExpressionLambdaAggregatorFactory

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

the class CalciteQueryTest method testBitwiseAggregatorsTimeseries.

@Test
public void testBitwiseAggregatorsTimeseries() throws Exception {
    cannotVectorize();
    testQuery("SELECT\n" + " BIT_AND(l1),\n" + " BIT_OR(l1),\n" + " BIT_XOR(l1)\n" + "FROM druid.numfoo", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE3).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).aggregators(aggregators(new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a0", ImmutableSet.of("l1"), "__acc", "0", "0", NullHandling.sqlCompatible(), false, false, "bitwiseAnd(\"__acc\", \"l1\")", "bitwiseAnd(\"__acc\", \"a0\")", null, null, ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), not(selector("l1", null, null))), new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a1", ImmutableSet.of("l1"), "__acc", "0", "0", NullHandling.sqlCompatible(), false, false, "bitwiseOr(\"__acc\", \"l1\")", "bitwiseOr(\"__acc\", \"a1\")", null, null, ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), not(selector("l1", null, null))), new FilteredAggregatorFactory(new ExpressionLambdaAggregatorFactory("a2", ImmutableSet.of("l1"), "__acc", "0", "0", NullHandling.sqlCompatible(), false, false, "bitwiseXor(\"__acc\", \"l1\")", "bitwiseXor(\"__acc\", \"a2\")", null, null, ExpressionLambdaAggregatorFactory.DEFAULT_MAX_SIZE_BYTES, TestExprMacroTable.INSTANCE), not(selector("l1", null, null))))).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(useDefault ? new Object[] { 0L, 325327L, 325324L } : new Object[] { 0L, 325327L, 325324L }));
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) ExpressionLambdaAggregatorFactory(org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory) Test(org.junit.Test)

Example 5 with ExpressionLambdaAggregatorFactory

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

the class GroupByQueryRunnerTest method testGroupByWithExpressionAggregator.

@Test
public void testGroupByWithExpressionAggregator() {
    // expression agg not yet vectorized
    cannotVectorize();
    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, null, false, false, "__acc + index", null, 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), makeRow(query, "2011-04-01", "alias", "business", "rows", 1L, "idx", 118.57034), makeRow(query, "2011-04-01", "alias", "entertainment", "rows", 1L, "idx", 158.747224), makeRow(query, "2011-04-01", "alias", "health", "rows", 1L, "idx", 120.134704), makeRow(query, "2011-04-01", "alias", "mezzanine", "rows", 3L, "idx", 2871.8866900000003d), makeRow(query, "2011-04-01", "alias", "news", "rows", 1L, "idx", 121.58358d), makeRow(query, "2011-04-01", "alias", "premium", "rows", 3L, "idx", 2900.798647d), makeRow(query, "2011-04-01", "alias", "technology", "rows", 1L, "idx", 78.622547d), makeRow(query, "2011-04-01", "alias", "travel", "rows", 1L, "idx", 119.922742d), makeRow(query, "2011-04-02", "alias", "automotive", "rows", 1L, "idx", 147.42593d), makeRow(query, "2011-04-02", "alias", "business", "rows", 1L, "idx", 112.987027d), makeRow(query, "2011-04-02", "alias", "entertainment", "rows", 1L, "idx", 166.016049d), makeRow(query, "2011-04-02", "alias", "health", "rows", 1L, "idx", 113.446008d), makeRow(query, "2011-04-02", "alias", "mezzanine", "rows", 3L, "idx", 2448.830613d), makeRow(query, "2011-04-02", "alias", "news", "rows", 1L, "idx", 114.290141d), makeRow(query, "2011-04-02", "alias", "premium", "rows", 3L, "idx", 2506.415148d), makeRow(query, "2011-04-02", "alias", "technology", "rows", 1L, "idx", 97.387433d), makeRow(query, "2011-04-02", "alias", "travel", "rows", 1L, "idx", 126.411364d));
    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)

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