Search in sources :

Example 6 with NumericTopNMetricSpec

use of org.apache.druid.query.topn.NumericTopNMetricSpec in project druid by druid-io.

the class CalciteQueryTest method testOrderByEarliestFloat.

@Test
public void testOrderByEarliestFloat() throws Exception {
    // Cannot vectorize EARLIEST aggregator.
    skipVectorize();
    List<Object[]> expected;
    if (NullHandling.replaceWithDefault()) {
        expected = ImmutableList.of(new Object[] { "1", 0.0f }, new Object[] { "2", 0.0f }, new Object[] { "abc", 0.0f }, new Object[] { "def", 0.0f }, new Object[] { "10.1", 0.1f }, new Object[] { "", 1.0f });
    } else {
        expected = ImmutableList.of(new Object[] { "1", null }, new Object[] { "abc", null }, new Object[] { "def", null }, new Object[] { "2", 0.0f }, new Object[] { "10.1", 0.1f }, new Object[] { "", 1.0f });
    }
    testQuery("SELECT dim1, EARLIEST(f1) FROM druid.numfoo GROUP BY 1 ORDER BY 2 LIMIT 10", ImmutableList.of(new TopNQueryBuilder().dataSource(CalciteTests.DATASOURCE3).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).dimension(new DefaultDimensionSpec("dim1", "_d0")).aggregators(aggregators(new FloatFirstAggregatorFactory("a0", "f1", null))).metric(new InvertedTopNMetricSpec(new NumericTopNMetricSpec("a0"))).threshold(10).context(QUERY_CONTEXT_DEFAULT).build()), expected);
}
Also used : TopNQueryBuilder(org.apache.druid.query.topn.TopNQueryBuilder) FloatFirstAggregatorFactory(org.apache.druid.query.aggregation.first.FloatFirstAggregatorFactory) InvertedTopNMetricSpec(org.apache.druid.query.topn.InvertedTopNMetricSpec) NumericTopNMetricSpec(org.apache.druid.query.topn.NumericTopNMetricSpec) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 7 with NumericTopNMetricSpec

use of org.apache.druid.query.topn.NumericTopNMetricSpec in project druid by druid-io.

the class DruidQuery method toTopNQuery.

/**
 * Return this query as a TopN query, or null if this query is not compatible with TopN.
 *
 * @return query or null
 */
@Nullable
private TopNQuery toTopNQuery(final QueryFeatureInspector queryFeatureInspector) {
    // Must be allowed by the QueryMaker.
    if (!queryFeatureInspector.feature(QueryFeature.CAN_RUN_TOPN)) {
        return null;
    }
    // Must have GROUP BY one column, no GROUPING SETS, ORDER BY ≤ 1 column, LIMIT > 0 and ≤ maxTopNLimit,
    // no OFFSET, no HAVING.
    final boolean topNOk = grouping != null && grouping.getDimensions().size() == 1 && !grouping.getSubtotals().hasEffect(grouping.getDimensionSpecs()) && sorting != null && (sorting.getOrderBys().size() <= 1 && sorting.getOffsetLimit().hasLimit() && sorting.getOffsetLimit().getLimit() > 0 && sorting.getOffsetLimit().getLimit() <= plannerContext.getPlannerConfig().getMaxTopNLimit() && !sorting.getOffsetLimit().hasOffset()) && grouping.getHavingFilter() == null;
    if (!topNOk) {
        return null;
    }
    final DimensionSpec dimensionSpec = Iterables.getOnlyElement(grouping.getDimensions()).toDimensionSpec();
    // grouping col cannot be type array
    if (dimensionSpec.getOutputType().isArray()) {
        return null;
    }
    final OrderByColumnSpec limitColumn;
    if (sorting.getOrderBys().isEmpty()) {
        limitColumn = new OrderByColumnSpec(dimensionSpec.getOutputName(), OrderByColumnSpec.Direction.ASCENDING, Calcites.getStringComparatorForValueType(dimensionSpec.getOutputType()));
    } else {
        limitColumn = Iterables.getOnlyElement(sorting.getOrderBys());
    }
    final TopNMetricSpec topNMetricSpec;
    if (limitColumn.getDimension().equals(dimensionSpec.getOutputName())) {
        // DimensionTopNMetricSpec is exact; always return it even if allowApproximate is false.
        final DimensionTopNMetricSpec baseMetricSpec = new DimensionTopNMetricSpec(null, limitColumn.getDimensionComparator());
        topNMetricSpec = limitColumn.getDirection() == OrderByColumnSpec.Direction.ASCENDING ? baseMetricSpec : new InvertedTopNMetricSpec(baseMetricSpec);
    } else if (plannerContext.getPlannerConfig().isUseApproximateTopN()) {
        // ORDER BY metric
        final NumericTopNMetricSpec baseMetricSpec = new NumericTopNMetricSpec(limitColumn.getDimension());
        topNMetricSpec = limitColumn.getDirection() == OrderByColumnSpec.Direction.ASCENDING ? new InvertedTopNMetricSpec(baseMetricSpec) : baseMetricSpec;
    } else {
        return null;
    }
    final Pair<DataSource, Filtration> dataSourceFiltrationPair = getFiltration(dataSource, filter, virtualColumnRegistry);
    final DataSource newDataSource = dataSourceFiltrationPair.lhs;
    final Filtration filtration = dataSourceFiltrationPair.rhs;
    final List<PostAggregator> postAggregators = new ArrayList<>(grouping.getPostAggregators());
    if (sorting.getProjection() != null) {
        postAggregators.addAll(sorting.getProjection().getPostAggregators());
    }
    return new TopNQuery(newDataSource, getVirtualColumns(true), dimensionSpec, topNMetricSpec, Ints.checkedCast(sorting.getOffsetLimit().getLimit()), filtration.getQuerySegmentSpec(), filtration.getDimFilter(), Granularities.ALL, grouping.getAggregatorFactories(), postAggregators, ImmutableSortedMap.copyOf(plannerContext.getQueryContext()));
}
Also used : DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) Filtration(org.apache.druid.sql.calcite.filtration.Filtration) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) ArrayList(java.util.ArrayList) InvertedTopNMetricSpec(org.apache.druid.query.topn.InvertedTopNMetricSpec) TopNMetricSpec(org.apache.druid.query.topn.TopNMetricSpec) NumericTopNMetricSpec(org.apache.druid.query.topn.NumericTopNMetricSpec) DimensionTopNMetricSpec(org.apache.druid.query.topn.DimensionTopNMetricSpec) DataSource(org.apache.druid.query.DataSource) QueryDataSource(org.apache.druid.query.QueryDataSource) JoinDataSource(org.apache.druid.query.JoinDataSource) OrderByColumnSpec(org.apache.druid.query.groupby.orderby.OrderByColumnSpec) DimensionTopNMetricSpec(org.apache.druid.query.topn.DimensionTopNMetricSpec) InvertedTopNMetricSpec(org.apache.druid.query.topn.InvertedTopNMetricSpec) NumericTopNMetricSpec(org.apache.druid.query.topn.NumericTopNMetricSpec) TopNQuery(org.apache.druid.query.topn.TopNQuery) Nullable(javax.annotation.Nullable)

Example 8 with NumericTopNMetricSpec

use of org.apache.druid.query.topn.NumericTopNMetricSpec in project druid by druid-io.

the class MapVirtualColumnTopNTest method testWithMapColumn.

@Test
public void testWithMapColumn() {
    final TopNQuery query = new TopNQuery(new TableDataSource(QueryRunnerTestHelper.DATA_SOURCE), VirtualColumns.create(ImmutableList.of(new MapVirtualColumn("keys", "values", "params"))), // params is the map type
    new DefaultDimensionSpec("params", "params"), new NumericTopNMetricSpec("count"), 1, new MultipleIntervalSegmentSpec(ImmutableList.of(Intervals.of("2011/2012"))), null, Granularities.ALL, ImmutableList.of(new CountAggregatorFactory("count")), null, null);
    expectedException.expect(UnsupportedOperationException.class);
    expectedException.expectMessage("Map column doesn't support getRow()");
    runner.run(QueryPlus.wrap(query)).toList();
}
Also used : TableDataSource(org.apache.druid.query.TableDataSource) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) TopNQuery(org.apache.druid.query.topn.TopNQuery) NumericTopNMetricSpec(org.apache.druid.query.topn.NumericTopNMetricSpec) MultipleIntervalSegmentSpec(org.apache.druid.query.spec.MultipleIntervalSegmentSpec) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 9 with NumericTopNMetricSpec

use of org.apache.druid.query.topn.NumericTopNMetricSpec in project druid by druid-io.

the class MapVirtualColumnTopNTest method testWithSubColumn.

@Test
public void testWithSubColumn() {
    final TopNQuery query = new TopNQuery(new TableDataSource(QueryRunnerTestHelper.DATA_SOURCE), VirtualColumns.create(ImmutableList.of(new MapVirtualColumn("keys", "values", "params"))), // params.key3 is string
    new DefaultDimensionSpec("params.key3", "params.key3"), new NumericTopNMetricSpec("count"), 2, new MultipleIntervalSegmentSpec(ImmutableList.of(Intervals.of("2011/2012"))), null, Granularities.ALL, ImmutableList.of(new CountAggregatorFactory("count")), null, null);
    final List<Result<TopNResultValue>> result = runner.run(QueryPlus.wrap(query)).toList();
    final List<Result<TopNResultValue>> expected = Collections.singletonList(new Result<>(DateTimes.of("2011-01-12T00:00:00.000Z"), new TopNResultValue(ImmutableList.of(new DimensionAndMetricValueExtractor(MapVirtualColumnTestBase.mapOf("count", 2L, "params.key3", null)), new DimensionAndMetricValueExtractor(MapVirtualColumnTestBase.mapOf("count", 1L, "params.key3", "value3"))))));
    Assert.assertEquals(expected, result);
}
Also used : TopNResultValue(org.apache.druid.query.topn.TopNResultValue) TableDataSource(org.apache.druid.query.TableDataSource) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) TopNQuery(org.apache.druid.query.topn.TopNQuery) NumericTopNMetricSpec(org.apache.druid.query.topn.NumericTopNMetricSpec) MultipleIntervalSegmentSpec(org.apache.druid.query.spec.MultipleIntervalSegmentSpec) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) DimensionAndMetricValueExtractor(org.apache.druid.query.topn.DimensionAndMetricValueExtractor) Result(org.apache.druid.query.Result) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 10 with NumericTopNMetricSpec

use of org.apache.druid.query.topn.NumericTopNMetricSpec in project druid by druid-io.

the class CalciteQueryTest method testOrderByAnyFloat.

@Test
public void testOrderByAnyFloat() throws Exception {
    List<Object[]> expected;
    if (NullHandling.replaceWithDefault()) {
        expected = ImmutableList.of(new Object[] { "1", 0.0f }, new Object[] { "2", 0.0f }, new Object[] { "abc", 0.0f }, new Object[] { "def", 0.0f }, new Object[] { "10.1", 0.1f }, new Object[] { "", 1.0f });
    } else {
        expected = ImmutableList.of(new Object[] { "2", 0.0f }, new Object[] { "10.1", 0.1f }, new Object[] { "", 1.0f }, // reversed by TopNNumericResultBuilder.build()
        new Object[] { "1", null }, new Object[] { "abc", null }, new Object[] { "def", null });
    }
    testQuery("SELECT dim1, ANY_VALUE(f1) FROM druid.numfoo GROUP BY 1 ORDER BY 2 LIMIT 10", ImmutableList.of(new TopNQueryBuilder().dataSource(CalciteTests.DATASOURCE3).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).dimension(new DefaultDimensionSpec("dim1", "_d0")).aggregators(aggregators(new FloatAnyAggregatorFactory("a0", "f1"))).metric(new InvertedTopNMetricSpec(new NumericTopNMetricSpec("a0"))).threshold(10).context(QUERY_CONTEXT_DEFAULT).build()), expected);
}
Also used : TopNQueryBuilder(org.apache.druid.query.topn.TopNQueryBuilder) InvertedTopNMetricSpec(org.apache.druid.query.topn.InvertedTopNMetricSpec) FloatAnyAggregatorFactory(org.apache.druid.query.aggregation.any.FloatAnyAggregatorFactory) NumericTopNMetricSpec(org.apache.druid.query.topn.NumericTopNMetricSpec) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Aggregations

NumericTopNMetricSpec (org.apache.druid.query.topn.NumericTopNMetricSpec)12 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)11 Test (org.junit.Test)11 InvertedTopNMetricSpec (org.apache.druid.query.topn.InvertedTopNMetricSpec)10 TopNQueryBuilder (org.apache.druid.query.topn.TopNQueryBuilder)9 TopNQuery (org.apache.druid.query.topn.TopNQuery)3 TableDataSource (org.apache.druid.query.TableDataSource)2 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)2 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)2 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)2 IntArrayList (it.unimi.dsi.fastutil.ints.IntArrayList)1 ArrayList (java.util.ArrayList)1 Nullable (javax.annotation.Nullable)1 DataSource (org.apache.druid.query.DataSource)1 JoinDataSource (org.apache.druid.query.JoinDataSource)1 QueryDataSource (org.apache.druid.query.QueryDataSource)1 Result (org.apache.druid.query.Result)1 PostAggregator (org.apache.druid.query.aggregation.PostAggregator)1 DoubleAnyAggregatorFactory (org.apache.druid.query.aggregation.any.DoubleAnyAggregatorFactory)1 FloatAnyAggregatorFactory (org.apache.druid.query.aggregation.any.FloatAnyAggregatorFactory)1