Search in sources :

Example 1 with AllGranularity

use of org.apache.druid.java.util.common.granularity.AllGranularity in project druid by druid-io.

the class CalciteCorrelatedQueryTest method testCorrelatedSubqueryWithCorrelatedQueryFilter.

@Test
@Parameters(source = QueryContextForJoinProvider.class)
public void testCorrelatedSubqueryWithCorrelatedQueryFilter(Map<String, Object> queryContext) throws Exception {
    cannotVectorize();
    queryContext = withLeftDirectAccessEnabled(queryContext);
    testQuery("select country, ANY_VALUE(\n" + "        select max(\"users\") from (\n" + "            select floor(__time to day), count(user) \"users\" from visits f where f.country = visits.country and f.city = 'A' group by 1\n" + "        )\n" + "     ) as \"dailyVisits\"\n" + "from visits \n" + " where city = 'B'" + " group by 1", queryContext, ImmutableList.of(GroupByQuery.builder().setDataSource(join(new TableDataSource(CalciteTests.USERVISITDATASOURCE), new QueryDataSource(GroupByQuery.builder().setDataSource(GroupByQuery.builder().setDataSource(CalciteTests.USERVISITDATASOURCE).setQuerySegmentSpec(querySegmentSpec(Intervals.ETERNITY)).setVirtualColumns(new ExpressionVirtualColumn("v0", "timestamp_floor(\"__time\",'P1D',null,'UTC')", ColumnType.LONG, TestExprMacroTable.INSTANCE)).setDimensions(new DefaultDimensionSpec("v0", "d0", ColumnType.LONG), new DefaultDimensionSpec("country", "d1")).setAggregatorSpecs(new FilteredAggregatorFactory(new CountAggregatorFactory("a0"), not(selector("user", null, null)))).setDimFilter(and(selector("city", "A", null), not(selector("country", null, null)))).setContext(withTimestampResultContext(queryContext, "d0", Granularities.DAY)).setGranularity(new AllGranularity()).build()).setQuerySegmentSpec(querySegmentSpec(Intervals.ETERNITY)).setDimensions(new DefaultDimensionSpec("d1", "_d0")).setAggregatorSpecs(new LongMaxAggregatorFactory("_a0", "a0")).setGranularity(new AllGranularity()).setContext(queryContext).build()), "j0.", equalsCondition(makeColumnExpression("country"), makeColumnExpression("j0._d0")), JoinType.LEFT, selector("city", "B", null))).setQuerySegmentSpec(querySegmentSpec(Intervals.ETERNITY)).setDimensions(new DefaultDimensionSpec("country", "d0")).setAggregatorSpecs(new LongAnyAggregatorFactory("a0", "j0._a0")).setGranularity(new AllGranularity()).setContext(queryContext).build()), ImmutableList.of(new Object[] { "canada", 2L }));
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) TableDataSource(org.apache.druid.query.TableDataSource) QueryDataSource(org.apache.druid.query.QueryDataSource) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) LongAnyAggregatorFactory(org.apache.druid.query.aggregation.any.LongAnyAggregatorFactory) AllGranularity(org.apache.druid.java.util.common.granularity.AllGranularity) LongMaxAggregatorFactory(org.apache.druid.query.aggregation.LongMaxAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 2 with AllGranularity

use of org.apache.druid.java.util.common.granularity.AllGranularity in project druid by druid-io.

the class CalciteCorrelatedQueryTest method testCorrelatedSubquery.

@Test
@Parameters(source = QueryContextForJoinProvider.class)
public void testCorrelatedSubquery(Map<String, Object> queryContext) throws Exception {
    cannotVectorize();
    queryContext = withLeftDirectAccessEnabled(queryContext);
    testQuery("select country, ANY_VALUE(\n" + "        select avg(\"users\") from (\n" + "            select floor(__time to day), count(distinct user) \"users\" from visits f where f.country = visits.country group by 1\n" + "        )\n" + "     ) as \"DAU\"\n" + "from visits \n" + "group by 1", queryContext, ImmutableList.of(GroupByQuery.builder().setDataSource(join(new TableDataSource(CalciteTests.USERVISITDATASOURCE), new QueryDataSource(GroupByQuery.builder().setDataSource(GroupByQuery.builder().setDataSource(CalciteTests.USERVISITDATASOURCE).setQuerySegmentSpec(querySegmentSpec(Intervals.ETERNITY)).setVirtualColumns(new ExpressionVirtualColumn("v0", "timestamp_floor(\"__time\",'P1D',null,'UTC')", ColumnType.LONG, TestExprMacroTable.INSTANCE)).setDimFilter(not(selector("country", null, null))).setDimensions(new DefaultDimensionSpec("v0", "d0", ColumnType.LONG), new DefaultDimensionSpec("country", "d1")).setAggregatorSpecs(new CardinalityAggregatorFactory("a0:a", null, Collections.singletonList(new DefaultDimensionSpec("user", "user")), false, true)).setPostAggregatorSpecs(Collections.singletonList(new HyperUniqueFinalizingPostAggregator("a0", "a0:a"))).setContext(withTimestampResultContext(queryContext, "d0", Granularities.DAY)).setGranularity(new AllGranularity()).build()).setQuerySegmentSpec(querySegmentSpec(Intervals.ETERNITY)).setDimensions(new DefaultDimensionSpec("d1", "_d0")).setAggregatorSpecs(new LongSumAggregatorFactory("_a0:sum", "a0"), useDefault ? new CountAggregatorFactory("_a0:count") : new FilteredAggregatorFactory(new CountAggregatorFactory("_a0:count"), not(selector("a0", null, null)))).setPostAggregatorSpecs(Collections.singletonList(new ArithmeticPostAggregator("_a0", "quotient", Arrays.asList(new FieldAccessPostAggregator(null, "_a0:sum"), new FieldAccessPostAggregator(null, "_a0:count"))))).setGranularity(new AllGranularity()).setContext(queryContext).build()), "j0.", equalsCondition(makeColumnExpression("country"), makeColumnExpression("j0._d0")), JoinType.LEFT)).setQuerySegmentSpec(querySegmentSpec(Intervals.ETERNITY)).setDimensions(new DefaultDimensionSpec("country", "d0")).setAggregatorSpecs(new LongAnyAggregatorFactory("a0", "j0._a0")).setGranularity(new AllGranularity()).setContext(queryContext).build()), ImmutableList.of(new Object[] { "India", 2L }, new Object[] { "USA", 1L }, new Object[] { "canada", 3L }));
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) ArithmeticPostAggregator(org.apache.druid.query.aggregation.post.ArithmeticPostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) AllGranularity(org.apache.druid.java.util.common.granularity.AllGranularity) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) HyperUniqueFinalizingPostAggregator(org.apache.druid.query.aggregation.hyperloglog.HyperUniqueFinalizingPostAggregator) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) TableDataSource(org.apache.druid.query.TableDataSource) QueryDataSource(org.apache.druid.query.QueryDataSource) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) LongAnyAggregatorFactory(org.apache.druid.query.aggregation.any.LongAnyAggregatorFactory) CardinalityAggregatorFactory(org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 3 with AllGranularity

use of org.apache.druid.java.util.common.granularity.AllGranularity in project druid by druid-io.

the class TimeseriesBinaryFn method apply.

@Override
public Result<TimeseriesResultValue> apply(Result<TimeseriesResultValue> arg1, Result<TimeseriesResultValue> arg2) {
    if (arg1 == null) {
        return arg2;
    }
    if (arg2 == null) {
        return arg1;
    }
    TimeseriesResultValue arg1Val = arg1.getValue();
    TimeseriesResultValue arg2Val = arg2.getValue();
    Map<String, Object> retVal = new LinkedHashMap<String, Object>();
    for (AggregatorFactory factory : aggregations) {
        final String metricName = factory.getName();
        retVal.put(metricName, factory.combine(arg1Val.getMetric(metricName), arg2Val.getMetric(metricName)));
    }
    return (gran instanceof AllGranularity) ? new Result<TimeseriesResultValue>(arg1.getTimestamp(), new TimeseriesResultValue(retVal)) : new Result<TimeseriesResultValue>(gran.bucketStart(arg1.getTimestamp()), new TimeseriesResultValue(retVal));
}
Also used : AllGranularity(org.apache.druid.java.util.common.granularity.AllGranularity) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with AllGranularity

use of org.apache.druid.java.util.common.granularity.AllGranularity in project druid by druid-io.

the class SearchBinaryFn method apply.

@Override
public Result<SearchResultValue> apply(Result<SearchResultValue> arg1, Result<SearchResultValue> arg2) {
    if (arg1 == null) {
        return arg2;
    }
    if (arg2 == null) {
        return arg1;
    }
    final int limit = gran instanceof AllGranularity ? this.limit : -1;
    SearchResultValue arg1Vals = arg1.getValue();
    SearchResultValue arg2Vals = arg2.getValue();
    Iterable<SearchHit> merged = Iterables.mergeSorted(Arrays.asList(arg1Vals, arg2Vals), searchSortSpec.getComparator());
    int maxSize = arg1Vals.getValue().size() + arg2Vals.getValue().size();
    if (limit > 0) {
        maxSize = Math.min(limit, maxSize);
    }
    List<SearchHit> results = Lists.newArrayListWithExpectedSize(maxSize);
    SearchHit prev = null;
    for (SearchHit searchHit : merged) {
        if (prev == null) {
            prev = searchHit;
            continue;
        }
        if (prev.equals(searchHit)) {
            if (prev.getCount() != null && searchHit.getCount() != null) {
                prev = new SearchHit(prev.getDimension(), prev.getValue(), prev.getCount() + searchHit.getCount());
            } else {
                prev = new SearchHit(prev.getDimension(), prev.getValue());
            }
        } else {
            results.add(prev);
            prev = searchHit;
            if (limit > 0 && results.size() >= limit) {
                break;
            }
        }
    }
    if (prev != null && (limit < 0 || results.size() < limit)) {
        results.add(prev);
    }
    final DateTime timestamp = gran instanceof AllGranularity ? arg1.getTimestamp() : gran.bucketStart(arg1.getTimestamp());
    return new Result<>(timestamp, new SearchResultValue(results));
}
Also used : AllGranularity(org.apache.druid.java.util.common.granularity.AllGranularity) DateTime(org.joda.time.DateTime) Result(org.apache.druid.query.Result)

Example 5 with AllGranularity

use of org.apache.druid.java.util.common.granularity.AllGranularity in project druid by druid-io.

the class TopNBinaryFn method apply.

@Override
public Result<TopNResultValue> apply(Result<TopNResultValue> arg1, Result<TopNResultValue> arg2) {
    if (arg1 == null) {
        return arg2;
    }
    if (arg2 == null) {
        return arg1;
    }
    Map<Object, DimensionAndMetricValueExtractor> retVals = new LinkedHashMap<>();
    TopNResultValue arg1Vals = arg1.getValue();
    TopNResultValue arg2Vals = arg2.getValue();
    for (DimensionAndMetricValueExtractor arg1Val : arg1Vals) {
        retVals.put(arg1Val.getDimensionValue(dimension), arg1Val);
    }
    for (DimensionAndMetricValueExtractor arg2Val : arg2Vals) {
        final Object dimensionValue = arg2Val.getDimensionValue(dimension);
        DimensionAndMetricValueExtractor arg1Val = retVals.get(dimensionValue);
        if (arg1Val != null) {
            // size of map = aggregator + topNDim + postAgg (If sorting is done on post agg field)
            Map<String, Object> retVal = CollectionUtils.newLinkedHashMapWithExpectedSize(aggregations.size() + 2);
            retVal.put(dimension, dimensionValue);
            for (AggregatorFactory factory : aggregations) {
                final String metricName = factory.getName();
                retVal.put(metricName, factory.combine(arg1Val.getMetric(metricName), arg2Val.getMetric(metricName)));
            }
            for (PostAggregator pf : postAggregations) {
                retVal.put(pf.getName(), pf.compute(retVal));
            }
            retVals.put(dimensionValue, new DimensionAndMetricValueExtractor(retVal));
        } else {
            retVals.put(dimensionValue, arg2Val);
        }
    }
    final DateTime timestamp;
    if (gran instanceof AllGranularity) {
        timestamp = arg1.getTimestamp();
    } else {
        timestamp = gran.bucketStart(arg1.getTimestamp());
    }
    TopNResultBuilder bob = topNMetricSpec.getResultBuilder(timestamp, dimSpec, threshold, comparator, aggregations, postAggregations);
    for (DimensionAndMetricValueExtractor extractor : retVals.values()) {
        bob.addEntry(extractor);
    }
    return bob.build();
}
Also used : PostAggregator(org.apache.druid.query.aggregation.PostAggregator) AllGranularity(org.apache.druid.java.util.common.granularity.AllGranularity) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) DateTime(org.joda.time.DateTime) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

AllGranularity (org.apache.druid.java.util.common.granularity.AllGranularity)9 Test (org.junit.Test)6 Parameters (junitparams.Parameters)5 QueryDataSource (org.apache.druid.query.QueryDataSource)5 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)5 LongAnyAggregatorFactory (org.apache.druid.query.aggregation.any.LongAnyAggregatorFactory)5 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)5 ExpressionVirtualColumn (org.apache.druid.segment.virtual.ExpressionVirtualColumn)5 TableDataSource (org.apache.druid.query.TableDataSource)4 LongMaxAggregatorFactory (org.apache.druid.query.aggregation.LongMaxAggregatorFactory)4 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)3 FilteredAggregatorFactory (org.apache.druid.query.aggregation.FilteredAggregatorFactory)3 LinkedHashMap (java.util.LinkedHashMap)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeMap (java.util.TreeMap)1