public TopNQueryBuilder metric(String s) { return metric(new NumericTopNMetricSpec(s)); }
@Deprecated public TopNQueryBuilder copy(TopNQueryBuilder builder) { return new TopNQueryBuilder() .dataSource(builder.dataSource) .virtualColumns(builder.virtualColumns) .dimension(builder.dimensionSpec) .metric(builder.topNMetricSpec) .threshold(builder.threshold) .intervals(builder.querySegmentSpec) .filters(builder.dimFilter) .granularity(builder.granularity) .aggregators(builder.aggregatorSpecs) .postAggregators(builder.postAggregatorSpecs) .context(builder.context); }
private TopNQuery makeTopNQuery() { return new TopNQueryBuilder() .dataSource(dataSource) .granularity(allGran) .dimension(marketDimension) .metric(indexMetric) .threshold(3) .intervals(fullOnInterval) .aggregators( Lists.newArrayList( Iterables.concat( commonAggregators, Lists.newArrayList( new DoubleMaxAggregatorFactory("maxIndex", "index"), new DoubleMinAggregatorFactory("minIndex", "index") ) ) ) ) .postAggregators(Collections.singletonList(addRowsIndexConstant)) .build(); }
@Test public void testQuerySerialization() throws IOException { Query query = new TopNQueryBuilder() .dataSource(dataSource) .granularity(allGran) .dimension(marketDimension) .metric(indexMetric) .threshold(4) .intervals(fullOnIntervalSpec) .aggregators( Lists.newArrayList( Iterables.concat( commonDoubleAggregators, Lists.newArrayList( new DoubleMaxAggregatorFactory("maxIndex", "index"), new DoubleMinAggregatorFactory("minIndex", "index") ) ) ) ) .postAggregators(Collections.singletonList(addRowsIndexConstant)) .build(); String json = jsonMapper.writeValueAsString(query); Query serdeQuery = jsonMapper.readValue(json, Query.class); Assert.assertEquals(query, serdeQuery); }
@Test public void testTopNOnMissingColumn() { TopNQuery query = new TopNQueryBuilder() .dataSource(QueryRunnerTestHelper.dataSource) .granularity(QueryRunnerTestHelper.allGran) .dimension(new DefaultDimensionSpec("nonexistentColumn", "alias")) .metric("rows") .threshold(4) .intervals(QueryRunnerTestHelper.fullOnIntervalSpec) .aggregators(Collections.singletonList(new CountAggregatorFactory("rows"))) .build(); final HashMap<String, Object> resultMap = new HashMap<>(); resultMap.put("alias", null); resultMap.put("rows", 1209L); List<Result<TopNResultValue>> expectedResults = Collections.singletonList( new Result<>( DateTimes.of("2011-01-12T00:00:00.000Z"), new TopNResultValue(Collections.<Map<String, Object>>singletonList(resultMap)) ) ); assertExpectedResults(expectedResults, query); }
@Test public void testTopNOverPartialNullDimensionWithFilterOnNullValue() { TopNQuery query = new TopNQueryBuilder() .dataSource(QueryRunnerTestHelper.dataSource) .granularity(Granularities.ALL) .dimension("partial_null_column") .metric(QueryRunnerTestHelper.uniqueMetric) .filters(new SelectorDimFilter("partial_null_column", null, null)) .threshold(1000) .intervals(QueryRunnerTestHelper.firstToThird) .aggregators(commonAggregators) .build(); Map<String, Object> map = new HashMap<>(); map.put("partial_null_column", null); map.put("rows", 22L); map.put("index", 7583.691513061523D); map.put("uniques", QueryRunnerTestHelper.UNIQUES_9); List<Result<TopNResultValue>> expectedResults = Collections.singletonList( new Result<>( DateTimes.of("2011-04-01T00:00:00.000Z"), new TopNResultValue( Collections.singletonList( map ) ) ) ); assertExpectedResults(expectedResults, query); }
@Test public void testTopNOnMissingColumnWithExtractionFn() { TopNQuery query = new TopNQueryBuilder() .dataSource(QueryRunnerTestHelper.dataSource) .granularity(QueryRunnerTestHelper.allGran) .dimension(new ExtractionDimensionSpec("nonexistentColumn", "alias", new StringFormatExtractionFn("theValue"))) .metric("rows") .threshold(4) .intervals(QueryRunnerTestHelper.fullOnIntervalSpec) .aggregators(Collections.singletonList(new CountAggregatorFactory("rows"))) .build(); List<Result<TopNResultValue>> expectedResults = Collections.singletonList( new Result<>( DateTimes.of("2011-01-12T00:00:00.000Z"), new TopNResultValue( Collections.<Map<String, Object>>singletonList( ImmutableMap.<String, Object>builder() .put("alias", "theValue") .put("rows", 1209L) .build() ) ) ) ); assertExpectedResults(expectedResults, query); }
.granularity(QueryRunnerTestHelper.allGran) .dimension(QueryRunnerTestHelper.marketDimension) .metric(new DimensionTopNMetricSpec("", StringComparators.LEXICOGRAPHIC)) .threshold(4) .intervals(QueryRunnerTestHelper.firstToThird)
@Test public void testSortOnStringAsDouble() { TopNQuery query = new TopNQueryBuilder() .dataSource(QueryRunnerTestHelper.dataSource) .granularity(QueryRunnerTestHelper.allGran) .dimension(new DefaultDimensionSpec("market", "alias", ValueType.DOUBLE)) .metric(new DimensionTopNMetricSpec(null, StringComparators.NUMERIC)) .threshold(4) .intervals(QueryRunnerTestHelper.fullOnIntervalSpec) .build(); final Map<String, Object> nullAliasMap = new HashMap<>(); nullAliasMap.put("alias", null); List<Result<TopNResultValue>> expectedResults = Collections.singletonList( new Result<>( DateTimes.of("2011-01-12T00:00:00.000Z"), new TopNResultValue(Collections.singletonList(nullAliasMap)) ) ); assertExpectedResults(expectedResults, query); }
/** See {@link #runTests} */ @SuppressWarnings("unused") private void testFullOnTopN(QueryRunner runner, List<Result<TopNResultValue>> expectedResults, String failMsg) { TopNQuery query = new TopNQueryBuilder() .dataSource(dataSource) .granularity(allGran) .dimension(marketDimension) .metric(indexMetric) .threshold(3) .intervals(fullOnInterval) .aggregators( Lists.newArrayList( Iterables.concat( commonAggregators, Lists.newArrayList( new DoubleMaxAggregatorFactory("maxIndex", "index"), new DoubleMinAggregatorFactory("minIndex", "index") ) ) ) ) .postAggregators(Collections.singletonList(addRowsIndexConstant)) .build(); failMsg += " topN "; HashMap<String, Object> context = new HashMap<>(); Iterable<Result<TopNResultValue>> actualResults = runner.run(QueryPlus.wrap(query), context).toList(); TestHelper.assertExpectedResults(expectedResults, actualResults, failMsg); }
/** See {@link #runTests} */ @SuppressWarnings("unused") private void testFilteredTopN(QueryRunner runner, List<Result<TopNResultValue>> expectedResults, String failMsg) { TopNQuery query = new TopNQueryBuilder() .dataSource(dataSource) .granularity(allGran) .dimension(marketDimension) .filters(marketDimension, "spot") .metric(indexMetric) .threshold(3) .intervals(fullOnInterval) .aggregators( Lists.newArrayList( Iterables.concat( commonAggregators, Lists.newArrayList( new DoubleMaxAggregatorFactory("maxIndex", "index"), new DoubleMinAggregatorFactory("minIndex", "index") ) ) ) ) .postAggregators(Collections.singletonList(addRowsIndexConstant)) .build(); failMsg += " filtered topN "; HashMap<String, Object> context = new HashMap<>(); Iterable<Result<TopNResultValue>> actualResults = runner.run(QueryPlus.wrap(query), context).toList(); TestHelper.assertExpectedResults(expectedResults, actualResults, failMsg); }
@Test public void testTopNWithNonExistentFilter() { TopNQuery query = new TopNQueryBuilder() .dataSource(QueryRunnerTestHelper.dataSource) .granularity(QueryRunnerTestHelper.allGran) .filters(QueryRunnerTestHelper.marketDimension, "billyblank") .dimension(QueryRunnerTestHelper.marketDimension) .metric(QueryRunnerTestHelper.indexMetric) .threshold(4) .intervals(QueryRunnerTestHelper.firstToThird) .aggregators(commonAggregators) .postAggregators(Collections.singletonList(QueryRunnerTestHelper.addRowsIndexConstant)) .build(); assertExpectedResults( Collections.singletonList( new Result<>(DateTimes.of("2011-04-01T00:00:00.000Z"), new TopNResultValue(Collections.emptyList())) ), query ); }
private TopNQuery makeFilteredTopNQuery() { return new TopNQueryBuilder() .dataSource(dataSource) .granularity(allGran) .dimension(marketDimension) .metric(indexMetric) .threshold(3) .filters( new AndDimFilter( new SelectorDimFilter(marketDimension, "spot", null), new SelectorDimFilter(placementDimension, "preferred", null) ) ) .intervals(fullOnInterval) .aggregators( Lists.newArrayList( Iterables.concat( commonAggregators, Lists.newArrayList( new DoubleMaxAggregatorFactory("maxIndex", "index"), new DoubleMinAggregatorFactory("minIndex", "index") ) ) ) ) .postAggregators(Collections.singletonList(addRowsIndexConstant)) .build(); }
@Test public void testSelectDistinctWithSortAsOuterQuery() throws Exception { testQuery( "SELECT * FROM (SELECT DISTINCT dim2 FROM druid.foo ORDER BY dim2) LIMIT 10", ImmutableList.of( new TopNQueryBuilder() .dataSource(CalciteTests.DATASOURCE1) .intervals(QSS(Filtration.eternity())) .granularity(Granularities.ALL) .dimension(new DefaultDimensionSpec("dim2", "d0")) .metric(new DimensionTopNMetricSpec(null, StringComparators.LEXICOGRAPHIC)) .threshold(10) .context(QUERY_CONTEXT_DEFAULT) .build() ), NullHandling.replaceWithDefault() ? ImmutableList.of( new Object[]{""}, new Object[]{"a"}, new Object[]{"abc"} ) : ImmutableList.of( new Object[]{null}, new Object[]{""}, new Object[]{"a"}, new Object[]{"abc"} ) ); }
@Test public void testSelectDistinctWithSortAsOuterQuery2() throws Exception { testQuery( "SELECT * FROM (SELECT DISTINCT dim2 FROM druid.foo ORDER BY dim2 LIMIT 5) LIMIT 10", ImmutableList.of( new TopNQueryBuilder() .dataSource(CalciteTests.DATASOURCE1) .intervals(QSS(Filtration.eternity())) .granularity(Granularities.ALL) .dimension(new DefaultDimensionSpec("dim2", "d0")) .metric(new DimensionTopNMetricSpec(null, StringComparators.LEXICOGRAPHIC)) .threshold(5) .context(QUERY_CONTEXT_DEFAULT) .build() ), NullHandling.replaceWithDefault() ? ImmutableList.of( new Object[]{""}, new Object[]{"a"}, new Object[]{"abc"} ) : ImmutableList.of( new Object[]{null}, new Object[]{""}, new Object[]{"a"}, new Object[]{"abc"} ) ); }
@Test public void testTopNOverPartialNullDimensionWithFilterOnNOTNullValue() { TopNQuery query = new TopNQueryBuilder() .dataSource(QueryRunnerTestHelper.dataSource) .granularity(Granularities.ALL) .dimension("partial_null_column") .metric(QueryRunnerTestHelper.uniqueMetric) .filters(new SelectorDimFilter("partial_null_column", "value", null)) .threshold(1000) .intervals(QueryRunnerTestHelper.firstToThird) .aggregators(commonAggregators) .build(); List<Result<TopNResultValue>> expectedResults = Collections.singletonList( new Result<>( DateTimes.of("2011-04-01T00:00:00.000Z"), new TopNResultValue( Collections.singletonList( ImmutableMap.<String, Object>of( "partial_null_column", "value", "rows", 4L, "index", 4875.669692D, "uniques", QueryRunnerTestHelper.UNIQUES_2 ) ) ) ) ); assertExpectedResults(expectedResults, query); }
@Test public void testSelectDistinctWithSortAsOuterQuery4() throws Exception { testQuery( "SELECT * FROM (SELECT DISTINCT dim2 FROM druid.foo ORDER BY dim2 DESC LIMIT 5) LIMIT 10", ImmutableList.of( new TopNQueryBuilder() .dataSource(CalciteTests.DATASOURCE1) .intervals(QSS(Filtration.eternity())) .granularity(Granularities.ALL) .dimension(new DefaultDimensionSpec("dim2", "d0")) .metric(new InvertedTopNMetricSpec(new DimensionTopNMetricSpec(null, StringComparators.LEXICOGRAPHIC))) .threshold(5) .context(QUERY_CONTEXT_DEFAULT) .build() ), NullHandling.replaceWithDefault() ? ImmutableList.of( new Object[]{""}, new Object[]{"abc"}, new Object[]{"a"} ) : ImmutableList.of( new Object[]{null}, new Object[]{"abc"}, new Object[]{"a"}, new Object[]{""} ) ); }
.filters(QueryRunnerTestHelper.placementishDimension, "m") .dimension(QueryRunnerTestHelper.marketDimension) .metric(QueryRunnerTestHelper.indexMetric) .threshold(4) .intervals(QueryRunnerTestHelper.firstToThird) .filters(QueryRunnerTestHelper.qualityDimension, "mezzanine") .dimension(QueryRunnerTestHelper.marketDimension) .metric(QueryRunnerTestHelper.indexMetric) .threshold(4) .intervals(QueryRunnerTestHelper.firstToThird)
@Test public void testTopNWithNonExistentFilterMultiDim() { AndDimFilter andDimFilter = new AndDimFilter( new SelectorDimFilter(QueryRunnerTestHelper.marketDimension, "billyblank", null), new SelectorDimFilter(QueryRunnerTestHelper.qualityDimension, "mezzanine", null) ); TopNQuery query = new TopNQueryBuilder() .dataSource(QueryRunnerTestHelper.dataSource) .granularity(QueryRunnerTestHelper.allGran) .filters(andDimFilter) .dimension(QueryRunnerTestHelper.marketDimension) .metric(QueryRunnerTestHelper.indexMetric) .threshold(4) .intervals(QueryRunnerTestHelper.firstToThird) .aggregators(commonAggregators) .postAggregators(Collections.singletonList(QueryRunnerTestHelper.addRowsIndexConstant)) .build(); assertExpectedResults( Collections.singletonList( new Result<>(DateTimes.of("2011-04-01T00:00:00.000Z"), new TopNResultValue(Collections.emptyList())) ), query ); }
@Test public void testCloseAndPopulate() throws Exception { List<Result> expectedRes = makeTopNResults(false, objects); List<Result> expectedCacheRes = makeTopNResults(true, objects); TopNQueryBuilder builder = new TopNQueryBuilder() .dataSource("ds") .dimension("top_dim") .metric("imps") .threshold(3) .intervals("2011-01-05/2011-01-10") .aggregators(AGGS) .granularity(Granularities.ALL); QueryToolChest toolchest = new TopNQueryQueryToolChest( new TopNQueryConfig(), QueryRunnerTestHelper.NoopIntervalChunkingQueryRunnerDecorator() ); testCloseAndPopulate(expectedRes, expectedCacheRes, builder.build(), toolchest); testUseCache(expectedCacheRes, builder.build(), toolchest); }