Search in sources :

Example 11 with AggregatorFactory

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

the class SegmentMetadataQueryTest method testSegmentMetadataQueryWithAggregatorsMerge.

@Test
public void testSegmentMetadataQueryWithAggregatorsMerge() {
    final Map<String, AggregatorFactory> expectedAggregators = new HashMap<>();
    for (AggregatorFactory agg : TestIndex.METRIC_AGGS) {
        expectedAggregators.put(agg.getName(), agg.getCombiningFactory());
    }
    SegmentAnalysis mergedSegmentAnalysis = new SegmentAnalysis(differentIds ? "merged" : SegmentId.dummy("testSegment").toString(), null, ImmutableMap.of("placement", new ColumnAnalysis(ColumnType.STRING, ValueType.STRING.toString(), false, false, 0, 0, null, null, null)), 0, expectedSegmentAnalysis1.getNumRows() + expectedSegmentAnalysis2.getNumRows(), expectedAggregators, null, null, null);
    QueryToolChest toolChest = FACTORY.getToolchest();
    ExecutorService exec = Executors.newCachedThreadPool();
    QueryRunner myRunner = new FinalizeResultsQueryRunner<>(toolChest.mergeResults(FACTORY.mergeRunners(Execs.directExecutor(), Lists.newArrayList(toolChest.preMergeQueryDecoration(runner1), toolChest.preMergeQueryDecoration(runner2)))), toolChest);
    SegmentMetadataQuery query = Druids.newSegmentMetadataQueryBuilder().dataSource("testing").intervals("2013/2014").toInclude(new ListColumnIncluderator(Collections.singletonList("placement"))).analysisTypes(SegmentMetadataQuery.AnalysisType.AGGREGATORS).merge(true).build();
    TestHelper.assertExpectedObjects(ImmutableList.of(mergedSegmentAnalysis), myRunner.run(QueryPlus.wrap(query)), "failed SegmentMetadata merging query");
    exec.shutdownNow();
}
Also used : FinalizeResultsQueryRunner(org.apache.druid.query.FinalizeResultsQueryRunner) HashMap(java.util.HashMap) SegmentMetadataQuery(org.apache.druid.query.metadata.metadata.SegmentMetadataQuery) ListColumnIncluderator(org.apache.druid.query.metadata.metadata.ListColumnIncluderator) ColumnAnalysis(org.apache.druid.query.metadata.metadata.ColumnAnalysis) ExecutorService(java.util.concurrent.ExecutorService) SegmentAnalysis(org.apache.druid.query.metadata.metadata.SegmentAnalysis) QueryToolChest(org.apache.druid.query.QueryToolChest) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) QueryRunner(org.apache.druid.query.QueryRunner) FinalizeResultsQueryRunner(org.apache.druid.query.FinalizeResultsQueryRunner) Test(org.junit.Test)

Example 12 with AggregatorFactory

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

the class VectorGroupByEngineIteratorTest method testCreateOneGrouperAndCloseItWhenClose.

@Test
public void testCreateOneGrouperAndCloseItWhenClose() throws IOException {
    final Interval interval = TestIndex.DATA_INTERVAL;
    final AggregatorFactory factory = new DoubleSumAggregatorFactory("index", "index");
    final GroupByQuery query = GroupByQuery.builder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setGranularity(QueryRunnerTestHelper.DAY_GRAN).setInterval(interval).setDimensions(new DefaultDimensionSpec("market", null, null)).setAggregatorSpecs(factory).build();
    final StorageAdapter storageAdapter = new QueryableIndexStorageAdapter(TestIndex.getMMappedTestIndex());
    final ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[4096]);
    final VectorCursor cursor = storageAdapter.makeVectorCursor(Filters.toFilter(query.getDimFilter()), interval, query.getVirtualColumns(), false, QueryContexts.getVectorSize(query), null);
    final List<GroupByVectorColumnSelector> dimensions = query.getDimensions().stream().map(dimensionSpec -> ColumnProcessors.makeVectorProcessor(dimensionSpec, GroupByVectorColumnProcessorFactory.instance(), cursor.getColumnSelectorFactory())).collect(Collectors.toList());
    final MutableObject<VectorGrouper> grouperCaptor = new MutableObject<>();
    final VectorGroupByEngineIterator iterator = new VectorGroupByEngineIterator(query, new GroupByQueryConfig(), storageAdapter, cursor, interval, dimensions, byteBuffer, null) {

        @Override
        VectorGrouper makeGrouper() {
            grouperCaptor.setValue(Mockito.spy(super.makeGrouper()));
            return grouperCaptor.getValue();
        }
    };
    iterator.close();
    Mockito.verify(grouperCaptor.getValue()).close();
}
Also used : VectorGroupByEngineIterator(org.apache.druid.query.groupby.epinephelinae.vector.VectorGroupByEngine.VectorGroupByEngineIterator) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) GroupByQueryConfig(org.apache.druid.query.groupby.GroupByQueryConfig) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test) IOException(java.io.IOException) StorageAdapter(org.apache.druid.segment.StorageAdapter) Collectors(java.util.stream.Collectors) QueryContexts(org.apache.druid.query.QueryContexts) ColumnProcessors(org.apache.druid.segment.ColumnProcessors) ByteBuffer(java.nio.ByteBuffer) TestIndex(org.apache.druid.segment.TestIndex) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) VectorCursor(org.apache.druid.segment.vector.VectorCursor) Mockito(org.mockito.Mockito) Interval(org.joda.time.Interval) List(java.util.List) QueryRunnerTestHelper(org.apache.druid.query.QueryRunnerTestHelper) VectorGrouper(org.apache.druid.query.groupby.epinephelinae.VectorGrouper) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Filters(org.apache.druid.segment.filter.Filters) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) GroupByQueryConfig(org.apache.druid.query.groupby.GroupByQueryConfig) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) StorageAdapter(org.apache.druid.segment.StorageAdapter) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) ByteBuffer(java.nio.ByteBuffer) VectorCursor(org.apache.druid.segment.vector.VectorCursor) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) VectorGrouper(org.apache.druid.query.groupby.epinephelinae.VectorGrouper) VectorGroupByEngineIterator(org.apache.druid.query.groupby.epinephelinae.vector.VectorGroupByEngine.VectorGroupByEngineIterator) Interval(org.joda.time.Interval) MutableObject(org.apache.commons.lang3.mutable.MutableObject) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 13 with AggregatorFactory

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

the class FloatLastAggregationTest method testSerde.

@Test
public void testSerde() throws Exception {
    DefaultObjectMapper mapper = new DefaultObjectMapper();
    String floatSpecJson = "{\"type\":\"floatLast\",\"name\":\"billy\",\"fieldName\":\"nilly\"}";
    AggregatorFactory deserialized = mapper.readValue(floatSpecJson, AggregatorFactory.class);
    Assert.assertEquals(floatLastAggregatorFactory, deserialized);
    Assert.assertArrayEquals(floatLastAggregatorFactory.getCacheKey(), deserialized.getCacheKey());
}
Also used : DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 14 with AggregatorFactory

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

the class FinalizingFieldAccessPostAggregatorTest method testComparatorsWithFinalizing.

@Test
public void testComparatorsWithFinalizing() {
    String aggName = "billy";
    AggregatorFactory aggFactory = EasyMock.createMock(AggregatorFactory.class);
    EasyMock.expect(aggFactory.finalizeComputation("test_val1")).andReturn(new Long(10L)).times(1);
    EasyMock.expect(aggFactory.finalizeComputation("test_val2")).andReturn(new Long(21)).times(1);
    EasyMock.expect(aggFactory.finalizeComputation("test_val3")).andReturn(new Long(3)).times(1);
    EasyMock.expect(aggFactory.finalizeComputation("test_val4")).andReturn(null).times(1);
    EasyMock.expect(aggFactory.getComparator()).andReturn(Ordering.natural().<Long>nullsLast()).times(1);
    EasyMock.expect(aggFactory.getResultType()).andReturn(ColumnType.LONG).once();
    EasyMock.replay(aggFactory);
    FinalizingFieldAccessPostAggregator postAgg = buildDecorated("final_billy", aggName, ImmutableMap.of(aggName, aggFactory));
    List<Object> computedValues = new ArrayList<>();
    computedValues.add(postAgg.compute(ImmutableMap.of(aggName, "test_val1")));
    computedValues.add(postAgg.compute(ImmutableMap.of(aggName, "test_val2")));
    computedValues.add(postAgg.compute(ImmutableMap.of(aggName, "test_val3")));
    computedValues.add(postAgg.compute(ImmutableMap.of(aggName, "test_val4")));
    Collections.sort(computedValues, postAgg.getComparator());
    Assert.assertArrayEquals(new Object[] { 3L, 10L, 21L, null }, computedValues.toArray(new Object[0]));
    EasyMock.verify();
}
Also used : ArrayList(java.util.ArrayList) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) StringFirstAggregatorFactory(org.apache.druid.query.aggregation.first.StringFirstAggregatorFactory) GroupByQueryRunnerTest(org.apache.druid.query.groupby.GroupByQueryRunnerTest) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 15 with AggregatorFactory

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

the class FinalizingFieldAccessPostAggregatorTest method testComparatorsWithFinalizingAndComparatorNull.

@Test
public void testComparatorsWithFinalizingAndComparatorNull() {
    String aggName = "billy";
    AggregatorFactory aggFactory = EasyMock.createMock(AggregatorFactory.class);
    EasyMock.expect(aggFactory.getComparator()).andReturn(null).times(1);
    EasyMock.replay(aggFactory);
    FinalizingFieldAccessPostAggregator postAgg = buildDecorated("final_billy", "joe", ImmutableMap.of(aggName, aggFactory));
    List<Object> computedValues = new ArrayList<>();
    Map<String, Object> forNull = new HashMap<>();
    // guava does not allow the value to be null.
    forNull.put("joe", null);
    computedValues.add(postAgg.compute(ImmutableMap.of("joe", "test_val1")));
    computedValues.add(postAgg.compute(ImmutableMap.of("joe", "test_val2")));
    computedValues.add(postAgg.compute(forNull));
    computedValues.add(postAgg.compute(ImmutableMap.of("joe", "test_val4")));
    Collections.sort(computedValues, postAgg.getComparator());
    Assert.assertArrayEquals(new Object[] { null, "test_val1", "test_val2", "test_val4" }, computedValues.toArray(new Object[0]));
    EasyMock.verify();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) StringFirstAggregatorFactory(org.apache.druid.query.aggregation.first.StringFirstAggregatorFactory) GroupByQueryRunnerTest(org.apache.druid.query.groupby.GroupByQueryRunnerTest) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Aggregations

AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)163 Test (org.junit.Test)85 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)56 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)48 DimensionsSpec (org.apache.druid.data.input.impl.DimensionsSpec)42 ArrayList (java.util.ArrayList)33 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)33 TimestampSpec (org.apache.druid.data.input.impl.TimestampSpec)28 DataSchema (org.apache.druid.segment.indexing.DataSchema)25 Nullable (javax.annotation.Nullable)22 DoubleSumAggregatorFactory (org.apache.druid.query.aggregation.DoubleSumAggregatorFactory)22 PostAggregator (org.apache.druid.query.aggregation.PostAggregator)22 UniformGranularitySpec (org.apache.druid.segment.indexing.granularity.UniformGranularitySpec)22 HashMap (java.util.HashMap)20 List (java.util.List)20 DoubleMaxAggregatorFactory (org.apache.druid.query.aggregation.DoubleMaxAggregatorFactory)18 LongMaxAggregatorFactory (org.apache.druid.query.aggregation.LongMaxAggregatorFactory)18 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)18 Map (java.util.Map)17 FloatFirstAggregatorFactory (org.apache.druid.query.aggregation.first.FloatFirstAggregatorFactory)15