@Deprecated public TopNQueryBuilder copy(TopNQuery query) { return new TopNQueryBuilder(query); }
@Override public TopNQuery withQuerySegmentSpec(QuerySegmentSpec querySegmentSpec) { return new TopNQueryBuilder(this).intervals(querySegmentSpec).build(); }
@Override public Query<Result<TopNResultValue>> withDataSource(DataSource dataSource) { return new TopNQueryBuilder(this).dataSource(dataSource).build(); }
public TopNQuery withDimensionSpec(DimensionSpec spec) { return new TopNQueryBuilder(this).dimension(spec).build(); }
public TopNQuery withDimFilter(DimFilter dimFilter) { return new TopNQueryBuilder(this).filters(dimFilter).build(); }
public TopNQuery withAggregatorSpecs(List<AggregatorFactory> aggregatorSpecs) { return new TopNQueryBuilder(this).aggregators(aggregatorSpecs).build(); }
public TopNQuery withThreshold(int threshold) { return new TopNQueryBuilder(this).threshold(threshold).build(); }
@Override public Query<Result<TopNResultValue>> optimizeForSegment(PerSegmentQueryOptimizationContext optimizationContext) { return new TopNQueryBuilder(this).aggregators(optimizeAggs(optimizationContext)).build(); }
@Override public TopNQuery withOverriddenContext(Map<String, Object> contextOverrides) { return new TopNQueryBuilder(this).context(computeOverriddenContext(getContext(), contextOverrides)).build(); }
@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); }
@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); }
@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 ); }
@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); }