Search in sources :

Example 6 with ExtractionDimensionSpec

use of org.apache.druid.query.dimension.ExtractionDimensionSpec in project druid by druid-io.

the class CalciteQueryTest method testCountDistinctOfSubstring.

@Test
public void testCountDistinctOfSubstring() throws Exception {
    // Cannot vectorize due to extraction dimension spec.
    cannotVectorize();
    testQuery("SELECT COUNT(DISTINCT SUBSTRING(dim1, 1, 1)) FROM druid.foo WHERE dim1 <> ''", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).filters(not(selector("dim1", "", null))).granularity(Granularities.ALL).aggregators(aggregators(new CardinalityAggregatorFactory("a0", null, dimensions(new ExtractionDimensionSpec("dim1", null, new SubstringDimExtractionFn(0, 1))), false, true))).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 4L }));
}
Also used : SubstringDimExtractionFn(org.apache.druid.query.extraction.SubstringDimExtractionFn) CardinalityAggregatorFactory(org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) Test(org.junit.Test)

Example 7 with ExtractionDimensionSpec

use of org.apache.druid.query.dimension.ExtractionDimensionSpec in project druid by druid-io.

the class CalciteQueryTest method testGroupByLimitPushdownExtraction.

@Test
public void testGroupByLimitPushdownExtraction() throws Exception {
    cannotVectorize();
    testQuery("SELECT dim4, substring(dim5, 1, 1), count(*) FROM druid.numfoo WHERE dim4 = 'a' GROUP BY 1,2 LIMIT 2", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE3).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setDimensions(dimensions(new DefaultDimensionSpec("v0", "_d0"), new ExtractionDimensionSpec("dim5", "_d1", new SubstringDimExtractionFn(0, 1)))).setVirtualColumns(expressionVirtualColumn("v0", "'a'", ColumnType.STRING)).setDimFilter(selector("dim4", "a", null)).setAggregatorSpecs(aggregators(new CountAggregatorFactory("a0"))).setLimitSpec(new DefaultLimitSpec(ImmutableList.of(), 2)).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { "a", "a", 2L }, new Object[] { "a", "b", 1L }));
}
Also used : SubstringDimExtractionFn(org.apache.druid.query.extraction.SubstringDimExtractionFn) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) DefaultLimitSpec(org.apache.druid.query.groupby.orderby.DefaultLimitSpec) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) Test(org.junit.Test)

Example 8 with ExtractionDimensionSpec

use of org.apache.druid.query.dimension.ExtractionDimensionSpec in project druid by druid-io.

the class CalciteQueryTest method testFilterAndGroupByLookup.

@Test
public void testFilterAndGroupByLookup() throws Exception {
    // Cannot vectorize due to extraction dimension specs.
    cannotVectorize();
    final RegisteredLookupExtractionFn extractionFn = new RegisteredLookupExtractionFn(null, "lookyloo", false, null, null, true);
    testQuery("SELECT LOOKUP(dim1, 'lookyloo'), COUNT(*) FROM foo\n" + "WHERE LOOKUP(dim1, 'lookyloo') <> 'xxx'\n" + "GROUP BY LOOKUP(dim1, 'lookyloo')", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setDimFilter(not(selector("dim1", "xxx", extractionFn))).setDimensions(dimensions(new ExtractionDimensionSpec("dim1", "d0", ColumnType.STRING, extractionFn))).setAggregatorSpecs(aggregators(new CountAggregatorFactory("a0"))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { NULL_STRING, 5L }, new Object[] { "xabc", 1L }));
}
Also used : CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) RegisteredLookupExtractionFn(org.apache.druid.query.lookup.RegisteredLookupExtractionFn) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) Test(org.junit.Test)

Example 9 with ExtractionDimensionSpec

use of org.apache.druid.query.dimension.ExtractionDimensionSpec in project druid by druid-io.

the class GroupByQueryRunnerTest method testGroupByNestedDoubleTimeExtractionFnWithLongOutputTypes.

@Test
public void testGroupByNestedDoubleTimeExtractionFnWithLongOutputTypes() {
    // Cannot vectorize due to extraction dimension spec.
    cannotVectorize();
    if (config.getDefaultStrategy().equals(GroupByStrategySelector.STRATEGY_V1)) {
        expectedException.expect(UnsupportedOperationException.class);
        expectedException.expectMessage("GroupBy v1 only supports dimensions with an outputType of STRING.");
    }
    GroupByQuery subquery = makeQueryBuilder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("quality", "alias"), new ExtractionDimensionSpec(ColumnHolder.TIME_COLUMN_NAME, "time_day", ColumnType.LONG, new TimeFormatExtractionFn(null, null, null, Granularities.DAY, true))).setDimFilter(new SelectorDimFilter("quality", "technology", null)).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT).setGranularity(QueryRunnerTestHelper.DAY_GRAN).build();
    GroupByQuery outerQuery = makeQueryBuilder().setDataSource(subquery).setQuerySegmentSpec(QueryRunnerTestHelper.FIRST_TO_THIRD).setDimensions(new DefaultDimensionSpec("alias", "alias"), new ExtractionDimensionSpec("time_day", "time_week", ColumnType.LONG, new TimeFormatExtractionFn(null, null, null, Granularities.WEEK, true))).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT).setGranularity(QueryRunnerTestHelper.ALL_GRAN).build();
    List<ResultRow> expectedResults = Collections.singletonList(makeRow(outerQuery, "2011-04-01", "alias", "technology", "time_week", 1301270400000L, "rows", 2L));
    Iterable<ResultRow> results = GroupByQueryRunnerTestHelper.runQuery(factory, runner, outerQuery);
    TestHelper.assertExpectedObjects(expectedResults, results, "extraction-fn");
}
Also used : TimeFormatExtractionFn(org.apache.druid.query.extraction.TimeFormatExtractionFn) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 10 with ExtractionDimensionSpec

use of org.apache.druid.query.dimension.ExtractionDimensionSpec in project druid by druid-io.

the class DefaultGroupByQueryMetricsTest method testDefaultGroupByQueryMetricsQuery.

/**
 * Tests that passed a query {@link DefaultGroupByQueryMetrics} produces events with a certain set of dimensions,
 * no more, no less.
 */
@Test
public void testDefaultGroupByQueryMetricsQuery() {
    CachingEmitter cachingEmitter = new CachingEmitter();
    ServiceEmitter serviceEmitter = new ServiceEmitter("", "", cachingEmitter);
    DefaultGroupByQueryMetrics queryMetrics = new DefaultGroupByQueryMetrics();
    GroupByQuery.Builder builder = GroupByQuery.builder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setInterval("2011-04-02/2011-04-04").setDimensions(new ExtractionDimensionSpec("quality", "alias", new LookupExtractionFn(new MapLookupExtractor(ImmutableMap.of("mezzanine", "mezzanine0"), false), false, null, true, false))).setAggregatorSpecs(QueryRunnerTestHelper.ROWS_COUNT, new LongSumAggregatorFactory("idx", "index")).setGranularity(new PeriodGranularity(new Period("P1M"), null, null)).setDimFilter(new SelectorDimFilter("quality", "mezzanine", null)).setContext(ImmutableMap.of(QueryContexts.BY_SEGMENT_KEY, true));
    GroupByQuery query = builder.build();
    queryMetrics.query(query);
    queryMetrics.reportQueryTime(0).emit(serviceEmitter);
    Map<String, Object> actualEvent = cachingEmitter.getLastEmittedEvent().toMap();
    Assert.assertEquals(16, actualEvent.size());
    Assert.assertTrue(actualEvent.containsKey("feed"));
    Assert.assertTrue(actualEvent.containsKey("timestamp"));
    Assert.assertEquals("", actualEvent.get("host"));
    Assert.assertEquals("", actualEvent.get("service"));
    Assert.assertEquals(QueryRunnerTestHelper.DATA_SOURCE, actualEvent.get(DruidMetrics.DATASOURCE));
    Assert.assertEquals(query.getType(), actualEvent.get(DruidMetrics.TYPE));
    Interval expectedInterval = Intervals.of("2011-04-02/2011-04-04");
    Assert.assertEquals(Collections.singletonList(expectedInterval.toString()), actualEvent.get(DruidMetrics.INTERVAL));
    Assert.assertEquals("true", actualEvent.get("hasFilters"));
    Assert.assertEquals(expectedInterval.toDuration().toString(), actualEvent.get("duration"));
    Assert.assertEquals("", actualEvent.get(DruidMetrics.ID));
    Assert.assertEquals(ImmutableMap.of(QueryContexts.BY_SEGMENT_KEY, true), actualEvent.get("context"));
    // GroupBy-specific dimensions
    Assert.assertEquals("1", actualEvent.get("numDimensions"));
    Assert.assertEquals("2", actualEvent.get("numMetrics"));
    Assert.assertEquals("0", actualEvent.get("numComplexMetrics"));
    // Metric
    Assert.assertEquals("query/time", actualEvent.get("metric"));
    Assert.assertEquals(0L, actualEvent.get("value"));
}
Also used : ServiceEmitter(org.apache.druid.java.util.emitter.service.ServiceEmitter) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) PeriodGranularity(org.apache.druid.java.util.common.granularity.PeriodGranularity) Period(org.joda.time.Period) LookupExtractionFn(org.apache.druid.query.lookup.LookupExtractionFn) CachingEmitter(org.apache.druid.query.CachingEmitter) SelectorDimFilter(org.apache.druid.query.filter.SelectorDimFilter) MapLookupExtractor(org.apache.druid.query.extraction.MapLookupExtractor) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) Interval(org.joda.time.Interval) DefaultQueryMetricsTest(org.apache.druid.query.DefaultQueryMetricsTest) Test(org.junit.Test)

Aggregations

ExtractionDimensionSpec (org.apache.druid.query.dimension.ExtractionDimensionSpec)87 Test (org.junit.Test)82 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)62 LookupExtractionFn (org.apache.druid.query.lookup.LookupExtractionFn)40 RegexDimExtractionFn (org.apache.druid.query.extraction.RegexDimExtractionFn)32 Result (org.apache.druid.query.Result)30 TimeFormatExtractionFn (org.apache.druid.query.extraction.TimeFormatExtractionFn)29 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)27 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)26 JavaScriptExtractionFn (org.apache.druid.query.extraction.JavaScriptExtractionFn)22 SubstringDimExtractionFn (org.apache.druid.query.extraction.SubstringDimExtractionFn)22 StrlenExtractionFn (org.apache.druid.query.extraction.StrlenExtractionFn)21 ExtractionFn (org.apache.druid.query.extraction.ExtractionFn)20 MapLookupExtractor (org.apache.druid.query.extraction.MapLookupExtractor)20 StringFormatExtractionFn (org.apache.druid.query.extraction.StringFormatExtractionFn)20 DimExtractionFn (org.apache.druid.query.extraction.DimExtractionFn)19 SelectorDimFilter (org.apache.druid.query.filter.SelectorDimFilter)13 CascadeExtractionFn (org.apache.druid.query.extraction.CascadeExtractionFn)10 SearchQuerySpecDimExtractionFn (org.apache.druid.query.extraction.SearchQuerySpecDimExtractionFn)10 HashMap (java.util.HashMap)8