Search in sources :

Example 61 with ExpressionVirtualColumn

use of org.apache.druid.segment.virtual.ExpressionVirtualColumn in project druid by druid-io.

the class GroupByQueryRunnerTest method testGroupByWithStringVirtualColumn.

@Test
public void testGroupByWithStringVirtualColumn() {
    GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setVirtualColumns(new ExpressionVirtualColumn("vc", "quality + 'x'", ColumnType.STRING, TestExprMacroTable.INSTANCE)).setDimensions(new DefaultDimensionSpec("vc", "alias")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx", "index")).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
    List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "alias", "automotivex", "rows", 1L, "idx", 135L), makeRow(query, "2011-04-01", "alias", "businessx", "rows", 1L, "idx", 118L), makeRow(query, "2011-04-01", "alias", "entertainmentx", "rows", 1L, "idx", 158L), makeRow(query, "2011-04-01", "alias", "healthx", "rows", 1L, "idx", 120L), makeRow(query, "2011-04-01", "alias", "mezzaninex", "rows", 3L, "idx", 2870L), makeRow(query, "2011-04-01", "alias", "newsx", "rows", 1L, "idx", 121L), makeRow(query, "2011-04-01", "alias", "premiumx", "rows", 3L, "idx", 2900L), makeRow(query, "2011-04-01", "alias", "technologyx", "rows", 1L, "idx", 78L), makeRow(query, "2011-04-01", "alias", "travelx", "rows", 1L, "idx", 119L), makeRow(query, "2011-04-02", "alias", "automotivex", "rows", 1L, "idx", 147L), makeRow(query, "2011-04-02", "alias", "businessx", "rows", 1L, "idx", 112L), makeRow(query, "2011-04-02", "alias", "entertainmentx", "rows", 1L, "idx", 166L), makeRow(query, "2011-04-02", "alias", "healthx", "rows", 1L, "idx", 113L), makeRow(query, "2011-04-02", "alias", "mezzaninex", "rows", 3L, "idx", 2447L), makeRow(query, "2011-04-02", "alias", "newsx", "rows", 1L, "idx", 114L), makeRow(query, "2011-04-02", "alias", "premiumx", "rows", 3L, "idx", 2505L), makeRow(query, "2011-04-02", "alias", "technologyx", "rows", 1L, "idx", 97L), makeRow(query, "2011-04-02", "alias", "travelx", "rows", 1L, "idx", 126L));
    Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "virtual-column");
}
Also used : ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 62 with ExpressionVirtualColumn

use of org.apache.druid.segment.virtual.ExpressionVirtualColumn in project druid by druid-io.

the class GroupByQueryQueryToolChestTest method testMultiColumnCacheStrategy.

@Test
public void testMultiColumnCacheStrategy() throws Exception {
    final GroupByQuery query1 = GroupByQuery.builder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(ImmutableList.of(new DefaultDimensionSpec("test", "test", ColumnType.STRING), new DefaultDimensionSpec("v0", "v0", ColumnType.STRING))).setVirtualColumns(new ExpressionVirtualColumn("v0", "concat('foo', test)", ColumnType.STRING, TestExprMacroTable.INSTANCE)).setAggregatorSpecs(Arrays.asList(QueryRunnerTestHelper.ROWS_COUNT, getComplexAggregatorFactoryForValueType(ValueType.STRING))).setPostAggregatorSpecs(ImmutableList.of(new ConstantPostAggregator("post", 10))).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
    CacheStrategy<ResultRow, Object, GroupByQuery> strategy = new GroupByQueryQueryToolChest(null, null).getCacheStrategy(query1);
    // test timestamps that result in integer size millis
    final ResultRow result1 = ResultRow.of(123L, "val1", "fooval1", 1, getIntermediateComplexValue(ValueType.STRING, "val1"));
    Object preparedValue = strategy.prepareForSegmentLevelCache().apply(result1);
    ObjectMapper objectMapper = TestHelper.makeJsonMapper();
    Object fromCacheValue = objectMapper.readValue(objectMapper.writeValueAsBytes(preparedValue), strategy.getCacheObjectClazz());
    ResultRow fromCacheResult = strategy.pullFromSegmentLevelCache().apply(fromCacheValue);
    Assert.assertEquals(result1, fromCacheResult);
}
Also used : ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) ConstantPostAggregator(org.apache.druid.query.aggregation.post.ConstantPostAggregator) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 63 with ExpressionVirtualColumn

use of org.apache.druid.segment.virtual.ExpressionVirtualColumn in project druid by druid-io.

the class GroupByQueryRunnerTest method testGroupByWithStringVirtualColumnVectorizable.

@Test
public void testGroupByWithStringVirtualColumnVectorizable() {
    GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setVirtualColumns(new ExpressionVirtualColumn("vc", "cast(quality, 'STRING')", ColumnType.STRING, TestExprMacroTable.INSTANCE)).setDimensions(new DefaultDimensionSpec("vc", "alias")).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx", "index")).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
    List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "alias", "automotive", "rows", 1L, "idx", 135L), makeRow(query, "2011-04-01", "alias", "business", "rows", 1L, "idx", 118L), makeRow(query, "2011-04-01", "alias", "entertainment", "rows", 1L, "idx", 158L), makeRow(query, "2011-04-01", "alias", "health", "rows", 1L, "idx", 120L), makeRow(query, "2011-04-01", "alias", "mezzanine", "rows", 3L, "idx", 2870L), makeRow(query, "2011-04-01", "alias", "news", "rows", 1L, "idx", 121L), makeRow(query, "2011-04-01", "alias", "premium", "rows", 3L, "idx", 2900L), makeRow(query, "2011-04-01", "alias", "technology", "rows", 1L, "idx", 78L), makeRow(query, "2011-04-01", "alias", "travel", "rows", 1L, "idx", 119L), makeRow(query, "2011-04-02", "alias", "automotive", "rows", 1L, "idx", 147L), makeRow(query, "2011-04-02", "alias", "business", "rows", 1L, "idx", 112L), makeRow(query, "2011-04-02", "alias", "entertainment", "rows", 1L, "idx", 166L), makeRow(query, "2011-04-02", "alias", "health", "rows", 1L, "idx", 113L), makeRow(query, "2011-04-02", "alias", "mezzanine", "rows", 3L, "idx", 2447L), makeRow(query, "2011-04-02", "alias", "news", "rows", 1L, "idx", 114L), makeRow(query, "2011-04-02", "alias", "premium", "rows", 3L, "idx", 2505L), makeRow(query, "2011-04-02", "alias", "technology", "rows", 1L, "idx", 97L), makeRow(query, "2011-04-02", "alias", "travel", "rows", 1L, "idx", 126L));
    Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "virtual-column");
}
Also used : ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 64 with ExpressionVirtualColumn

use of org.apache.druid.segment.virtual.ExpressionVirtualColumn in project druid by druid-io.

the class GroupByQueryRunnerTest method testExtractionStringSpecWithMultiValueVirtualDimAsInput.

@Test
public void testExtractionStringSpecWithMultiValueVirtualDimAsInput() {
    if (config.getDefaultStrategy().equals(GroupByStrategySelector.STRATEGY_V1)) {
        expectedException.expect(UnsupportedOperationException.class);
        expectedException.expectMessage("GroupBy v1 does not support dimension selectors with unknown cardinality");
    }
    cannotVectorize();
    GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setVirtualColumns(new ExpressionVirtualColumn("v0", "mv_to_array(placementish)", ColumnType.STRING_ARRAY, ExprMacroTable.nil())).setDimensions(new ExtractionDimensionSpec("v0", "alias", ColumnType.STRING, new SubstringDimExtractionFn(1, 1))).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx", "index")).setGranularity(QueryRunnerTestHelper.ALL_GRAN).build();
    List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "alias", null, "rows", 26L, "idx", 12446L), makeRow(query, "2011-04-01", "alias", "r", "rows", 26L, "idx", 12446L));
    Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "multi-value-extraction-spec-as-string-dim-groupby-arrays");
}
Also used : ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) SubstringDimExtractionFn(org.apache.druid.query.extraction.SubstringDimExtractionFn) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 65 with ExpressionVirtualColumn

use of org.apache.druid.segment.virtual.ExpressionVirtualColumn in project druid by druid-io.

the class GroupByQueryRunnerTest method testGroupByWithSubtotalsSpecGeneral.

@Test
public void testGroupByWithSubtotalsSpecGeneral() {
    if (!config.getDefaultStrategy().equals(GroupByStrategySelector.STRATEGY_V2)) {
        return;
    }
    GroupByQuery query = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setVirtualColumns(new ExpressionVirtualColumn("alias", "quality", ColumnType.STRING, TestExprMacroTable.INSTANCE)).setDimensions(Lists.newArrayList(new DefaultDimensionSpec("quality", "quality2"), new DefaultDimensionSpec("market", "market2"), new DefaultDimensionSpec("alias", "alias2"))).setAggregatorSpecs(Arrays.asList(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx", "index"))).setPostAggregatorSpecs(Collections.singletonList(new FieldAccessPostAggregator("idxPostAgg", "idx"))).setGranularity(QueryRunnerTestHelper.DAY_GRAN).setSubtotalsSpec(ImmutableList.of(ImmutableList.of("alias2"), ImmutableList.of("market2"), ImmutableList.of())).build();
    List<ResultRow> expectedResults = Arrays.asList(makeRow(query, "2011-04-01", "alias2", "automotive", "rows", 1L, "idx", 135L, "idxPostAgg", 135L), makeRow(query, "2011-04-01", "alias2", "business", "rows", 1L, "idx", 118L, "idxPostAgg", 118L), makeRow(query, "2011-04-01", "alias2", "entertainment", "rows", 1L, "idx", 158L, "idxPostAgg", 158L), makeRow(query, "2011-04-01", "alias2", "health", "rows", 1L, "idx", 120L, "idxPostAgg", 120L), makeRow(query, "2011-04-01", "alias2", "mezzanine", "rows", 3L, "idx", 2870L, "idxPostAgg", 2870L), makeRow(query, "2011-04-01", "alias2", "news", "rows", 1L, "idx", 121L, "idxPostAgg", 121L), makeRow(query, "2011-04-01", "alias2", "premium", "rows", 3L, "idx", 2900L, "idxPostAgg", 2900L), makeRow(query, "2011-04-01", "alias2", "technology", "rows", 1L, "idx", 78L, "idxPostAgg", 78L), makeRow(query, "2011-04-01", "alias2", "travel", "rows", 1L, "idx", 119L, "idxPostAgg", 119L), makeRow(query, "2011-04-02", "alias2", "automotive", "rows", 1L, "idx", 147L, "idxPostAgg", 147L), makeRow(query, "2011-04-02", "alias2", "business", "rows", 1L, "idx", 112L, "idxPostAgg", 112L), makeRow(query, "2011-04-02", "alias2", "entertainment", "rows", 1L, "idx", 166L, "idxPostAgg", 166L), makeRow(query, "2011-04-02", "alias2", "health", "rows", 1L, "idx", 113L, "idxPostAgg", 113L), makeRow(query, "2011-04-02", "alias2", "mezzanine", "rows", 3L, "idx", 2447L, "idxPostAgg", 2447L), makeRow(query, "2011-04-02", "alias2", "news", "rows", 1L, "idx", 114L, "idxPostAgg", 114L), makeRow(query, "2011-04-02", "alias2", "premium", "rows", 3L, "idx", 2505L, "idxPostAgg", 2505L), makeRow(query, "2011-04-02", "alias2", "technology", "rows", 1L, "idx", 97L, "idxPostAgg", 97L), makeRow(query, "2011-04-02", "alias2", "travel", "rows", 1L, "idx", 126L, "idxPostAgg", 126L), makeRow(query, "2011-04-01T00:00:00.000Z", "market2", "spot", "rows", 9L, "idx", 1102L, "idxPostAgg", 1102L), makeRow(query, "2011-04-01T00:00:00.000Z", "market2", "total_market", "rows", 2L, "idx", 2836L, "idxPostAgg", 2836L), makeRow(query, "2011-04-01T00:00:00.000Z", "market2", "upfront", "rows", 2L, "idx", 2681L, "idxPostAgg", 2681L), makeRow(query, "2011-04-02T00:00:00.000Z", "market2", "spot", "rows", 9L, "idx", 1120L, "idxPostAgg", 1120L), makeRow(query, "2011-04-02T00:00:00.000Z", "market2", "total_market", "rows", 2L, "idx", 2514L, "idxPostAgg", 2514L), makeRow(query, "2011-04-02T00:00:00.000Z", "market2", "upfront", "rows", 2L, "idx", 2193L, "idxPostAgg", 2193L), makeRow(query, "2011-04-01T00:00:00.000Z", "rows", 13L, "idx", 6619L, "idxPostAgg", 6619L), makeRow(query, "2011-04-02T00:00:00.000Z", "rows", 13L, "idx", 5827L, "idxPostAgg", 5827L));
    Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, query);
    TestHelper.assertExpectedObjects(expectedResults, results, "subtotal");
}
Also used : ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Aggregations

ExpressionVirtualColumn (org.apache.druid.segment.virtual.ExpressionVirtualColumn)86 Test (org.junit.Test)73 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)58 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)45 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)26 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)24 QueryableIndexSegment (org.apache.druid.segment.QueryableIndexSegment)18 GroupByQueryRunnerTest (org.apache.druid.query.groupby.GroupByQueryRunnerTest)17 GroupByQuery (org.apache.druid.query.groupby.GroupByQuery)16 LegacySegmentSpec (org.apache.druid.query.spec.LegacySegmentSpec)15 IncrementalIndexSegment (org.apache.druid.segment.IncrementalIndexSegment)15 ResultRow (org.apache.druid.query.groupby.ResultRow)13 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)13 BaseCalciteQueryTest (org.apache.druid.sql.calcite.BaseCalciteQueryTest)11 VirtualColumns (org.apache.druid.segment.VirtualColumns)10 ImmutableList (com.google.common.collect.ImmutableList)8 TimeUnit (java.util.concurrent.TimeUnit)8 NullHandling (org.apache.druid.common.config.NullHandling)8 Closer (org.apache.druid.java.util.common.io.Closer)8 QueryDataSource (org.apache.druid.query.QueryDataSource)8