Search in sources :

Example 6 with DefaultDimensionSpec

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

the class CalciteQueryTest method testGroupingSetsWithOrderByDimension.

@Test
public void testGroupingSetsWithOrderByDimension() throws Exception {
    // Cannot vectorize due to virtual columns.
    cannotVectorize();
    testQuery("SELECT dim2, gran, SUM(cnt)\n" + "FROM (SELECT FLOOR(__time TO MONTH) AS gran, COALESCE(dim2, '') dim2, cnt FROM druid.foo) AS x\n" + "GROUP BY GROUPING SETS ( (), (dim2), (gran) )\n" + "ORDER BY gran, dim2 DESC", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "case_searched(notnull(\"dim2\"),\"dim2\",'')", ColumnType.STRING), expressionVirtualColumn("v1", "timestamp_floor(\"__time\",'P1M',null,'UTC')", ColumnType.LONG)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0"), new DefaultDimensionSpec("v1", "d1", ColumnType.LONG))).setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("a0", "cnt"))).setSubtotalsSpec(ImmutableList.of(ImmutableList.of("d0"), ImmutableList.of("d1"), ImmutableList.of())).setLimitSpec(new DefaultLimitSpec(ImmutableList.of(new OrderByColumnSpec("d1", Direction.ASCENDING, StringComparators.NUMERIC), new OrderByColumnSpec("d0", Direction.DESCENDING, StringComparators.LEXICOGRAPHIC)), Integer.MAX_VALUE)).setContext(withTimestampResultContext(QUERY_CONTEXT_DEFAULT, "d1", 1, Granularities.MONTH)).build()), ImmutableList.of(new Object[] { "abc", null, 1L }, new Object[] { "a", null, 2L }, new Object[] { "", null, 3L }, new Object[] { NULL_STRING, null, 6L }, new Object[] { NULL_STRING, timestamp("2000-01-01"), 3L }, new Object[] { NULL_STRING, timestamp("2001-01-01"), 3L }));
}
Also used : OrderByColumnSpec(org.apache.druid.query.groupby.orderby.OrderByColumnSpec) DefaultLimitSpec(org.apache.druid.query.groupby.orderby.DefaultLimitSpec) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 7 with DefaultDimensionSpec

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

the class CalciteQueryTest method testGroupByExtractFloorTime.

@Test
public void testGroupByExtractFloorTime() throws Exception {
    // Cannot vectorize due to virtual columns.
    cannotVectorize();
    testQuery("SELECT\n" + "EXTRACT(YEAR FROM FLOOR(__time TO YEAR)) AS \"year\", SUM(cnt)\n" + "FROM druid.foo\n" + "GROUP BY EXTRACT(YEAR FROM FLOOR(__time TO YEAR))", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "timestamp_extract(timestamp_floor(\"__time\",'P1Y',null,'UTC'),'YEAR','UTC')", ColumnType.LONG)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0", ColumnType.LONG))).setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("a0", "cnt"))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 2000L, 3L }, new Object[] { 2001L, 3L }));
}
Also used : LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 8 with DefaultDimensionSpec

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

the class CalciteQueryTest method testGroupingAggregatorDifferentOrder.

@Test
public void testGroupingAggregatorDifferentOrder() throws Exception {
    requireMergeBuffers(3);
    // Cannot vectorize due to virtual columns.
    cannotVectorize();
    testQuery("SELECT dim2, gran, SUM(cnt), GROUPING(gran, dim2)\n" + "FROM (SELECT FLOOR(__time TO MONTH) AS gran, COALESCE(dim2, '') dim2, cnt FROM druid.foo) AS x\n" + "GROUP BY GROUPING SETS ( (dim2, gran), (dim2), (gran), () )", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE1).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setVirtualColumns(expressionVirtualColumn("v0", "case_searched(notnull(\"dim2\"),\"dim2\",'')", ColumnType.STRING), expressionVirtualColumn("v1", "timestamp_floor(\"__time\",'P1M',null,'UTC')", ColumnType.LONG)).setDimensions(dimensions(new DefaultDimensionSpec("v0", "d0"), new DefaultDimensionSpec("v1", "d1", ColumnType.LONG))).setAggregatorSpecs(aggregators(new LongSumAggregatorFactory("a0", "cnt"), new GroupingAggregatorFactory("a1", Arrays.asList("v1", "v0")))).setSubtotalsSpec(ImmutableList.of(ImmutableList.of("d0", "d1"), ImmutableList.of("d0"), ImmutableList.of("d1"), ImmutableList.of())).setContext(withTimestampResultContext(QUERY_CONTEXT_DEFAULT, "d1", 1, Granularities.MONTH)).build()), ImmutableList.of(new Object[] { "", timestamp("2000-01-01"), 2L, 0L }, new Object[] { "", timestamp("2001-01-01"), 1L, 0L }, new Object[] { "a", timestamp("2000-01-01"), 1L, 0L }, new Object[] { "a", timestamp("2001-01-01"), 1L, 0L }, new Object[] { "abc", timestamp("2001-01-01"), 1L, 0L }, new Object[] { "", null, 3L, 2L }, new Object[] { "a", null, 2L, 2L }, new Object[] { "abc", null, 1L, 2L }, new Object[] { NULL_STRING, timestamp("2000-01-01"), 3L, 1L }, new Object[] { NULL_STRING, timestamp("2001-01-01"), 3L, 1L }, new Object[] { NULL_STRING, null, 6L, 3L }));
}
Also used : GroupingAggregatorFactory(org.apache.druid.query.aggregation.GroupingAggregatorFactory) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 9 with DefaultDimensionSpec

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

the class CalciteQueryTest method testApproxCountDistinct.

@Test
public void testApproxCountDistinct() throws Exception {
    // Cannot vectorize due to virtual columns.
    cannotVectorize();
    testQuery("SELECT\n" + "  SUM(cnt),\n" + // uppercase
    "  APPROX_COUNT_DISTINCT(dim2),\n" + // lowercase; also, filtered
    "  approx_count_distinct(dim2) FILTER(WHERE dim2 <> ''),\n" + // on extractionFn
    "  APPROX_COUNT_DISTINCT(SUBSTRING(dim2, 1, 1)),\n" + // on expression
    "  APPROX_COUNT_DISTINCT(SUBSTRING(dim2, 1, 1) || 'x'),\n" + // on native hyperUnique column
    "  approx_count_distinct(unique_dim1)\n" + "FROM druid.foo", ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).virtualColumns(expressionVirtualColumn("v0", "concat(substring(\"dim2\", 0, 1),'x')", ColumnType.STRING)).aggregators(aggregators(new LongSumAggregatorFactory("a0", "cnt"), new CardinalityAggregatorFactory("a1", null, dimensions(new DefaultDimensionSpec("dim2", "dim2")), false, true), new FilteredAggregatorFactory(new CardinalityAggregatorFactory("a2", null, dimensions(new DefaultDimensionSpec("dim2", "dim2")), false, true), not(selector("dim2", "", null))), new CardinalityAggregatorFactory("a3", null, dimensions(new ExtractionDimensionSpec("dim2", "dim2", ColumnType.STRING, new SubstringDimExtractionFn(0, 1))), false, true), new CardinalityAggregatorFactory("a4", null, dimensions(new DefaultDimensionSpec("v0", "v0", ColumnType.STRING)), false, true), new HyperUniquesAggregatorFactory("a5", "unique_dim1", false, true))).context(QUERY_CONTEXT_DEFAULT).build()), NullHandling.replaceWithDefault() ? ImmutableList.of(new Object[] { 6L, 3L, 2L, 2L, 2L, 6L }) : ImmutableList.of(new Object[] { 6L, 3L, 2L, 1L, 1L, 6L }));
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) SubstringDimExtractionFn(org.apache.druid.query.extraction.SubstringDimExtractionFn) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) HyperUniquesAggregatorFactory(org.apache.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory) CardinalityAggregatorFactory(org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) Test(org.junit.Test)

Example 10 with DefaultDimensionSpec

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

the class CalciteQueryTest method testCountAndAverageByConstantVirtualColumn.

@Test
public void testCountAndAverageByConstantVirtualColumn() throws Exception {
    List<VirtualColumn> virtualColumns;
    List<AggregatorFactory> aggs;
    if (useDefault) {
        aggs = ImmutableList.of(new FilteredAggregatorFactory(new CountAggregatorFactory("a0"), not(selector("v0", null, null))), new LongSumAggregatorFactory("a1:sum", null, "325323", TestExprMacroTable.INSTANCE), new CountAggregatorFactory("a1:count"));
        virtualColumns = ImmutableList.of(expressionVirtualColumn("v0", "'10.1'", ColumnType.STRING));
    } else {
        aggs = ImmutableList.of(new FilteredAggregatorFactory(new CountAggregatorFactory("a0"), not(selector("v0", null, null))), new LongSumAggregatorFactory("a1:sum", "v1"), new FilteredAggregatorFactory(new CountAggregatorFactory("a1:count"), not(selector("v1", null, null))));
        virtualColumns = ImmutableList.of(expressionVirtualColumn("v0", "'10.1'", ColumnType.STRING), expressionVirtualColumn("v1", "325323", ColumnType.LONG));
    }
    testQuery("SELECT dim5, COUNT(dim1), AVG(l1) FROM druid.numfoo WHERE dim1 = '10.1' AND l1 = 325323 GROUP BY dim5", ImmutableList.of(GroupByQuery.builder().setDataSource(CalciteTests.DATASOURCE3).setInterval(querySegmentSpec(Filtration.eternity())).setDimFilter(and(selector("dim1", "10.1", null), selector("l1", "325323", null))).setGranularity(Granularities.ALL).setVirtualColumns(VirtualColumns.create(virtualColumns)).setDimensions(new DefaultDimensionSpec("dim5", "_d0", ColumnType.STRING)).setAggregatorSpecs(aggs).setPostAggregatorSpecs(ImmutableList.of(new ArithmeticPostAggregator("a1", "quotient", ImmutableList.of(new FieldAccessPostAggregator(null, "a1:sum"), new FieldAccessPostAggregator(null, "a1:count"))))).setContext(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { "ab", 1L, 325323L }));
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) ArithmeticPostAggregator(org.apache.druid.query.aggregation.post.ArithmeticPostAggregator) FinalizingFieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FinalizingFieldAccessPostAggregator) FieldAccessPostAggregator(org.apache.druid.query.aggregation.post.FieldAccessPostAggregator) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) VirtualColumn(org.apache.druid.segment.VirtualColumn) StringLastAggregatorFactory(org.apache.druid.query.aggregation.last.StringLastAggregatorFactory) LongAnyAggregatorFactory(org.apache.druid.query.aggregation.any.LongAnyAggregatorFactory) FloatLastAggregatorFactory(org.apache.druid.query.aggregation.last.FloatLastAggregatorFactory) DoubleLastAggregatorFactory(org.apache.druid.query.aggregation.last.DoubleLastAggregatorFactory) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) DoubleMinAggregatorFactory(org.apache.druid.query.aggregation.DoubleMinAggregatorFactory) LongLastAggregatorFactory(org.apache.druid.query.aggregation.last.LongLastAggregatorFactory) LongFirstAggregatorFactory(org.apache.druid.query.aggregation.first.LongFirstAggregatorFactory) FloatMinAggregatorFactory(org.apache.druid.query.aggregation.FloatMinAggregatorFactory) DoubleAnyAggregatorFactory(org.apache.druid.query.aggregation.any.DoubleAnyAggregatorFactory) FloatFirstAggregatorFactory(org.apache.druid.query.aggregation.first.FloatFirstAggregatorFactory) StringFirstAggregatorFactory(org.apache.druid.query.aggregation.first.StringFirstAggregatorFactory) LongMinAggregatorFactory(org.apache.druid.query.aggregation.LongMinAggregatorFactory) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) GroupingAggregatorFactory(org.apache.druid.query.aggregation.GroupingAggregatorFactory) FloatAnyAggregatorFactory(org.apache.druid.query.aggregation.any.FloatAnyAggregatorFactory) HyperUniquesAggregatorFactory(org.apache.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) FloatMaxAggregatorFactory(org.apache.druid.query.aggregation.FloatMaxAggregatorFactory) DoubleFirstAggregatorFactory(org.apache.druid.query.aggregation.first.DoubleFirstAggregatorFactory) StringAnyAggregatorFactory(org.apache.druid.query.aggregation.any.StringAnyAggregatorFactory) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) DoubleMaxAggregatorFactory(org.apache.druid.query.aggregation.DoubleMaxAggregatorFactory) CardinalityAggregatorFactory(org.apache.druid.query.aggregation.cardinality.CardinalityAggregatorFactory) ExpressionLambdaAggregatorFactory(org.apache.druid.query.aggregation.ExpressionLambdaAggregatorFactory) LongMaxAggregatorFactory(org.apache.druid.query.aggregation.LongMaxAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Aggregations

DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)494 Test (org.junit.Test)461 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)261 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)235 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)107 DefaultLimitSpec (org.apache.druid.query.groupby.orderby.DefaultLimitSpec)106 OrderByColumnSpec (org.apache.druid.query.groupby.orderby.OrderByColumnSpec)104 GroupByQuery (org.apache.druid.query.groupby.GroupByQuery)53 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)53 ExpressionVirtualColumn (org.apache.druid.segment.virtual.ExpressionVirtualColumn)53 QueryDataSource (org.apache.druid.query.QueryDataSource)52 SelectorDimFilter (org.apache.druid.query.filter.SelectorDimFilter)43 TableDataSource (org.apache.druid.query.TableDataSource)41 Result (org.apache.druid.query.Result)37 ResultRow (org.apache.druid.query.groupby.ResultRow)35 GroupByQueryRunnerTest (org.apache.druid.query.groupby.GroupByQueryRunnerTest)33 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)30 TopNQueryBuilder (org.apache.druid.query.topn.TopNQueryBuilder)28 ExtractionDimensionSpec (org.apache.druid.query.dimension.ExtractionDimensionSpec)26