use of org.apache.druid.segment.virtual.ExpressionVirtualColumn in project druid by druid-io.
the class GroupByQueryRunnerTest method testGroupByOrderLimit.
@Test
public void testGroupByOrderLimit() {
GroupByQuery.Builder builder = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setInterval("2011-04-02/2011-04-04").setDimensions(new DefaultDimensionSpec("quality", "alias")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx", "index")).addOrderByColumn("rows").addOrderByColumn("alias", OrderByColumnSpec.Direction.DESCENDING).setGranularity(new PeriodGranularity(new Period("P1M"), null, null));
final GroupByQuery query = builder.build();
List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "alias", "travel", "rows", 2L, "idx", 243L), makeRow(query, "2011-04-01", "alias", "technology", "rows", 2L, "idx", 177L), makeRow(query, "2011-04-01", "alias", "news", "rows", 2L, "idx", 221L), makeRow(query, "2011-04-01", "alias", "health", "rows", 2L, "idx", 216L), makeRow(query, "2011-04-01", "alias", "entertainment", "rows", 2L, "idx", 319L), makeRow(query, "2011-04-01", "alias", "business", "rows", 2L, "idx", 217L), makeRow(query, "2011-04-01", "alias", "automotive", "rows", 2L, "idx", 269L), makeRow(query, "2011-04-01", "alias", "premium", "rows", 6L, "idx", 4416L), makeRow(query, "2011-04-01", "alias", "mezzanine", "rows", 6L, "idx", 4420L));
QueryRunner<ResultRow> mergeRunner = factory.getToolchest().mergeResults(runner);
TestHelper.assertExpectedObjects(expectedResults, mergeRunner.run(QueryPlus.wrap(query)), "no-limit");
TestHelper.assertExpectedObjects(Iterables.limit(expectedResults, 5), mergeRunner.run(QueryPlus.wrap(builder.setLimit(5).build())), "limited");
// Now try it with an expression based aggregator.
List<AggregatorFactory> aggregatorSpecs = Arrays.asList(QueryRunnerTestHelper.ROWS_COUNT, new DoubleSumAggregatorFactory("idx", null, "index / 2 + indexMin", TestExprMacroTable.INSTANCE));
builder.setLimit(Integer.MAX_VALUE).setAggregatorSpecs(aggregatorSpecs);
expectedResults = makeRows(builder.build(), new String[] { "__time", "alias", "rows", "idx" }, new Object[] { "2011-04-01", "travel", 2L, 365.4876403808594D }, new Object[] { "2011-04-01", "technology", 2L, 267.3737487792969D }, new Object[] { "2011-04-01", "news", 2L, 333.3147277832031D }, new Object[] { "2011-04-01", "health", 2L, 325.467529296875D }, new Object[] { "2011-04-01", "entertainment", 2L, 479.916015625D }, new Object[] { "2011-04-01", "business", 2L, 328.083740234375D }, new Object[] { "2011-04-01", "automotive", 2L, 405.5966796875D }, new Object[] { "2011-04-01", "premium", 6L, 6627.927734375D }, new Object[] { "2011-04-01", "mezzanine", 6L, 6635.47998046875D });
TestHelper.assertExpectedObjects(expectedResults, mergeRunner.run(QueryPlus.wrap(builder.build())), "no-limit");
TestHelper.assertExpectedObjects(Iterables.limit(expectedResults, 5), mergeRunner.run(QueryPlus.wrap(builder.setLimit(5).build())), "limited");
// Now try it with an expression virtual column.
ExpressionVirtualColumn expressionVirtualColumn = new ExpressionVirtualColumn("expr", "index / 2 + indexMin", ColumnType.FLOAT, TestExprMacroTable.INSTANCE);
List<AggregatorFactory> aggregatorSpecs2 = Arrays.asList(QueryRunnerTestHelper.ROWS_COUNT, new DoubleSumAggregatorFactory("idx", "expr"));
builder.setLimit(Integer.MAX_VALUE).setVirtualColumns(expressionVirtualColumn).setAggregatorSpecs(aggregatorSpecs2);
TestHelper.assertExpectedObjects(expectedResults, mergeRunner.run(QueryPlus.wrap(builder.build())), "no-limit");
TestHelper.assertExpectedObjects(Iterables.limit(expectedResults, 5), mergeRunner.run(QueryPlus.wrap(builder.setLimit(5).build())), "limited");
}
use of org.apache.druid.segment.virtual.ExpressionVirtualColumn in project druid by druid-io.
the class GroupByQueryRunnerTest method testGroupByCardinalityAggOnMultiStringExpression.
@Test
public void testGroupByCardinalityAggOnMultiStringExpression() {
GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setVirtualColumns(new ExpressionVirtualColumn("v0", "concat(quality,market)", ColumnType.STRING, TestExprMacroTable.INSTANCE)).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new CardinalityAggregatorFactory("numVals", ImmutableList.of(DefaultDimensionSpec.of("v0")), false)).setGranularity(QueryRunnerTestHelper.ALL_GRAN).build();
List<ResultRow> expectedResults = Collections.singletonList(makeRow(query, "2011-04-01", "rows", 26L, "numVals", 13.041435202975777d));
Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
TestHelper.assertExpectedObjects(expectedResults, results, "cardinality-agg");
}
use of org.apache.druid.segment.virtual.ExpressionVirtualColumn in project druid by druid-io.
the class GroupByQueryRunnerTest method testMergeResultsAcrossMultipleDaysWithLimitAndOrderByUsingMathExpressions.
@Test
public void testMergeResultsAcrossMultipleDaysWithLimitAndOrderByUsingMathExpressions() {
final int limit = 14;
GroupByQuery.Builder builder = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setInterval(QueryRunnerTestHelper.FIRST_TO_THIRD).setVirtualColumns(new ExpressionVirtualColumn("expr", "index * 2 + indexMin / 10", ColumnType.FLOAT, TestExprMacroTable.INSTANCE)).setDimensions(new DefaultDimensionSpec("quality", "alias")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx", "expr")).setGranularity(Granularities.DAY).setLimit(limit).addOrderByColumn("idx", OrderByColumnSpec.Direction.DESCENDING);
GroupByQuery fullQuery = builder.build();
List<ResultRow> expectedResults = Arrays.asList(makeRow(fullQuery, "2011-04-01", "alias", "premium", "rows", 3L, "idx", 6090L), makeRow(fullQuery, "2011-04-01", "alias", "mezzanine", "rows", 3L, "idx", 6030L), makeRow(fullQuery, "2011-04-01", "alias", "entertainment", "rows", 1L, "idx", 333L), makeRow(fullQuery, "2011-04-01", "alias", "automotive", "rows", 1L, "idx", 285L), makeRow(fullQuery, "2011-04-01", "alias", "news", "rows", 1L, "idx", 255L), makeRow(fullQuery, "2011-04-01", "alias", "health", "rows", 1L, "idx", 252L), makeRow(fullQuery, "2011-04-01", "alias", "travel", "rows", 1L, "idx", 251L), makeRow(fullQuery, "2011-04-01", "alias", "business", "rows", 1L, "idx", 248L), makeRow(fullQuery, "2011-04-01", "alias", "technology", "rows", 1L, "idx", 165L), makeRow(fullQuery, "2011-04-02", "alias", "premium", "rows", 3L, "idx", 5262L), makeRow(fullQuery, "2011-04-02", "alias", "mezzanine", "rows", 3L, "idx", 5141L), makeRow(fullQuery, "2011-04-02", "alias", "entertainment", "rows", 1L, "idx", 348L), makeRow(fullQuery, "2011-04-02", "alias", "automotive", "rows", 1L, "idx", 309L), makeRow(fullQuery, "2011-04-02", "alias", "travel", "rows", 1L, "idx", 265L));
QueryRunner<ResultRow> mergeRunner = factory.getToolchest().mergeResults(runner);
TestHelper.assertExpectedObjects(Iterables.limit(expectedResults, limit), mergeRunner.run(QueryPlus.wrap(fullQuery)), StringUtils.format("limit: %d", limit));
}
use of org.apache.druid.segment.virtual.ExpressionVirtualColumn in project druid by druid-io.
the class JoinFilterAnalyzerTest method test_filterPushDown_factConcatExpressionToCountryLeftFilterOnChannelAndCountryNameUsingLookup.
@Test
public void test_filterPushDown_factConcatExpressionToCountryLeftFilterOnChannelAndCountryNameUsingLookup() {
JoinableClause factExprToCountry = new JoinableClause(FACT_TO_COUNTRY_ON_ISO_CODE_PREFIX, LookupJoinable.wrap(countryIsoCodeToNameLookup), JoinType.LEFT, JoinConditionAnalysis.forExpression(StringUtils.format("\"%sk\" == concat(countryIsoCode, regionIsoCode)", FACT_TO_COUNTRY_ON_ISO_CODE_PREFIX), FACT_TO_COUNTRY_ON_ISO_CODE_PREFIX, ExprMacroTable.nil()));
List<JoinableClause> joinableClauses = ImmutableList.of(factExprToCountry);
Filter filter = new AndFilter(ImmutableList.of(new SelectorFilter("channel", "#en.wikipedia"), new SelectorFilter("c1.v", "Usca")));
JoinFilterPreAnalysis joinFilterPreAnalysis = makeDefaultConfigPreAnalysis(filter, joinableClauses, VirtualColumns.EMPTY);
HashJoinSegmentStorageAdapter adapter = new HashJoinSegmentStorageAdapter(factSegment.asStorageAdapter(), joinableClauses, joinFilterPreAnalysis);
JoinTestHelper.verifyCursors(adapter.makeCursors(filter, Intervals.ETERNITY, VirtualColumns.EMPTY, Granularities.ALL, false, null), ImmutableList.of("page", FACT_TO_COUNTRY_ON_ISO_CODE_PREFIX + "v"), ImmutableList.of(new Object[] { "President of India", "Usca" }, new Object[] { "Otjiwarongo Airport", "Usca" }, new Object[] { "Carlo Curti", "Usca" }));
ExpressionVirtualColumn expectedVirtualColumn = new ExpressionVirtualColumn("JOIN-FILTER-PUSHDOWN-VIRTUAL-COLUMN-0", "concat(countryIsoCode, regionIsoCode)", ColumnType.STRING, ExprMacroTable.nil());
JoinFilterSplit expectedFilterSplit = new JoinFilterSplit(new AndFilter(ImmutableList.of(new SelectorFilter("channel", "#en.wikipedia"), new InDimFilter("JOIN-FILTER-PUSHDOWN-VIRTUAL-COLUMN-0", ImmutableSet.of("USCA"), null, null).toFilter())), new SelectorFilter("c1.v", "Usca"), ImmutableSet.of(expectedVirtualColumn));
JoinFilterSplit actualFilterSplit = JoinFilterAnalyzer.splitFilter(joinFilterPreAnalysis);
Assert.assertEquals(expectedFilterSplit.getBaseTableFilter(), actualFilterSplit.getBaseTableFilter());
Assert.assertEquals(expectedFilterSplit.getJoinTableFilter(), actualFilterSplit.getJoinTableFilter());
ExpressionVirtualColumn actualVirtualColumn = (ExpressionVirtualColumn) actualFilterSplit.getPushDownVirtualColumns().iterator().next();
compareExpressionVirtualColumns(expectedVirtualColumn, actualVirtualColumn);
}
use of org.apache.druid.segment.virtual.ExpressionVirtualColumn in project druid by druid-io.
the class JoinFilterAnalyzerTest method test_filterPushDown_factExpressionsToRegionToCountryLeftFilterOnChannelAndCountryName.
@Test
public void test_filterPushDown_factExpressionsToRegionToCountryLeftFilterOnChannelAndCountryName() {
JoinableClause factExprToRegon = new JoinableClause(FACT_TO_REGION_PREFIX, new IndexedTableJoinable(regionsTable), JoinType.LEFT, JoinConditionAnalysis.forExpression(StringUtils.format("\"%sregionIsoCode\" == reverse(regionIsoCode) && \"%scountryIsoCode\" == reverse(countryIsoCode)", FACT_TO_REGION_PREFIX, FACT_TO_REGION_PREFIX), FACT_TO_REGION_PREFIX, ExprMacroTable.nil()));
List<JoinableClause> joinableClauses = ImmutableList.of(factExprToRegon, regionToCountry(JoinType.LEFT));
Filter originalFilter = new AndFilter(ImmutableList.of(new SelectorFilter("channel", "#en.wikipedia"), new SelectorFilter("rtc.countryName", "States United")));
JoinFilterPreAnalysis joinFilterPreAnalysis = makeDefaultConfigPreAnalysis(originalFilter, joinableClauses, VirtualColumns.EMPTY);
HashJoinSegmentStorageAdapter adapter = new HashJoinSegmentStorageAdapter(factSegment.asStorageAdapter(), joinableClauses, joinFilterPreAnalysis);
JoinTestHelper.verifyCursors(adapter.makeCursors(originalFilter, Intervals.ETERNITY, VirtualColumns.EMPTY, Granularities.ALL, false, null), ImmutableList.of("page", FACT_TO_REGION_PREFIX + "regionName", REGION_TO_COUNTRY_PREFIX + "countryName"), ImmutableList.of(new Object[] { "Old Anatolian Turkish", "Ainigriv", "States United" }));
JoinFilterSplit expectedFilterSplit = new JoinFilterSplit(new AndFilter(ImmutableList.of(new SelectorFilter("channel", "#en.wikipedia"), new InDimFilter("JOIN-FILTER-PUSHDOWN-VIRTUAL-COLUMN-0", ImmutableSet.of("SU"), null, null).toFilter())), new SelectorFilter("rtc.countryName", "States United"), ImmutableSet.of());
JoinFilterSplit actualFilterSplit = JoinFilterAnalyzer.splitFilter(joinFilterPreAnalysis);
ExpressionVirtualColumn expectedVirtualColumn = new ExpressionVirtualColumn("JOIN-FILTER-PUSHDOWN-VIRTUAL-COLUMN-0", "reverse(countryIsoCode)", ColumnType.STRING, ExprMacroTable.nil());
Assert.assertEquals(expectedFilterSplit.getBaseTableFilter(), actualFilterSplit.getBaseTableFilter());
Assert.assertEquals(expectedFilterSplit.getJoinTableFilter(), actualFilterSplit.getJoinTableFilter());
ExpressionVirtualColumn actualVirtualColumn = (ExpressionVirtualColumn) actualFilterSplit.getPushDownVirtualColumns().iterator().next();
compareExpressionVirtualColumns(expectedVirtualColumn, actualVirtualColumn);
}
Aggregations