Search in sources :

Example 91 with QueryDataSource

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

the class CalciteCorrelatedQueryTest method testCorrelatedSubqueryWithCorrelatedQueryFilter_Scan.

@Test
@Parameters(source = QueryContextForJoinProvider.class)
public void testCorrelatedSubqueryWithCorrelatedQueryFilter_Scan(Map<String, Object> queryContext) throws Exception {
    cannotVectorize();
    queryContext = withLeftDirectAccessEnabled(queryContext);
    testQuery("select country, ANY_VALUE(\n" + "        select max(\"users\") from (\n" + "            select floor(__time to day), count(user) \"users\" from visits f where f.country = visits.country and f.city = 'A' group by 1\n" + "        )\n" + "     ) as \"dailyVisits\"\n" + "from visits \n" + " where city = 'B'" + " group by 1", queryContext, ImmutableList.of(GroupByQuery.builder().setDataSource(join(new TableDataSource(CalciteTests.USERVISITDATASOURCE), new QueryDataSource(GroupByQuery.builder().setDataSource(GroupByQuery.builder().setDataSource(CalciteTests.USERVISITDATASOURCE).setQuerySegmentSpec(querySegmentSpec(Intervals.ETERNITY)).setVirtualColumns(new ExpressionVirtualColumn("v0", "timestamp_floor(\"__time\",'P1D',null,'UTC')", ColumnType.LONG, TestExprMacroTable.INSTANCE)).setDimensions(new DefaultDimensionSpec("v0", "d0", ColumnType.LONG), new DefaultDimensionSpec("country", "d1")).setAggregatorSpecs(new FilteredAggregatorFactory(new CountAggregatorFactory("a0"), not(selector("user", null, null)))).setDimFilter(and(selector("city", "A", null), not(selector("country", null, null)))).setContext(withTimestampResultContext(queryContext, "d0", Granularities.DAY)).setGranularity(new AllGranularity()).build()).setQuerySegmentSpec(querySegmentSpec(Intervals.ETERNITY)).setDimensions(new DefaultDimensionSpec("d1", "_d0")).setAggregatorSpecs(new LongMaxAggregatorFactory("_a0", "a0")).setGranularity(new AllGranularity()).setContext(queryContext).build()), "j0.", equalsCondition(makeColumnExpression("country"), makeColumnExpression("j0._d0")), JoinType.LEFT, selector("city", "B", null))).setQuerySegmentSpec(querySegmentSpec(Intervals.ETERNITY)).setDimensions(new DefaultDimensionSpec("country", "d0")).setAggregatorSpecs(new LongAnyAggregatorFactory("a0", "j0._a0")).setGranularity(new AllGranularity()).setContext(queryContext).build()), ImmutableList.of(new Object[] { "canada", 2L }));
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) ExpressionVirtualColumn(org.apache.druid.segment.virtual.ExpressionVirtualColumn) TableDataSource(org.apache.druid.query.TableDataSource) QueryDataSource(org.apache.druid.query.QueryDataSource) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) LongAnyAggregatorFactory(org.apache.druid.query.aggregation.any.LongAnyAggregatorFactory) AllGranularity(org.apache.druid.java.util.common.granularity.AllGranularity) LongMaxAggregatorFactory(org.apache.druid.query.aggregation.LongMaxAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 92 with QueryDataSource

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

the class CalciteJoinQueryTest method testInnerJoinLookupTableTable.

@Test
@Parameters(source = QueryContextForJoinProvider.class)
public void testInnerJoinLookupTableTable(Map<String, Object> queryContext) throws Exception {
    // Regression test for https://github.com/apache/druid/issues/9646.
    // Cannot vectorize JOIN operator.
    cannotVectorize();
    testQuery("SELECT l.k, l.v, SUM(f.m1), SUM(nf.m1)\n" + "FROM lookup.lookyloo l\n" + "INNER JOIN druid.foo f on f.dim1 = l.k\n" + "INNER JOIN druid.numfoo nf on nf.dim1 = l.k\n" + "GROUP BY 1, 2 ORDER BY 2", queryContext, ImmutableList.of(GroupByQuery.builder().setDataSource(join(join(new LookupDataSource("lookyloo"), new QueryDataSource(newScanQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).columns("dim1", "m1").context(QUERY_CONTEXT_DEFAULT).build()), "j0.", equalsCondition(makeColumnExpression("k"), makeColumnExpression("j0.dim1")), JoinType.INNER), new QueryDataSource(newScanQueryBuilder().dataSource(CalciteTests.DATASOURCE3).intervals(querySegmentSpec(Filtration.eternity())).columns("dim1", "m1").context(QUERY_CONTEXT_DEFAULT).build()), "_j0.", equalsCondition(makeColumnExpression("k"), makeColumnExpression("_j0.dim1")), JoinType.INNER)).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setDimensions(dimensions(new DefaultDimensionSpec("k", "d0"), new DefaultDimensionSpec("v", "d1"))).setAggregatorSpecs(aggregators(new DoubleSumAggregatorFactory("a0", "j0.m1"), new DoubleSumAggregatorFactory("a1", "_j0.m1"))).setLimitSpec(new DefaultLimitSpec(ImmutableList.of(new OrderByColumnSpec("d1", OrderByColumnSpec.Direction.ASCENDING)), null)).setContext(queryContext).build()), ImmutableList.of(new Object[] { "abc", "xabc", 6d, 6d }));
}
Also used : OrderByColumnSpec(org.apache.druid.query.groupby.orderby.OrderByColumnSpec) QueryDataSource(org.apache.druid.query.QueryDataSource) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) DefaultLimitSpec(org.apache.druid.query.groupby.orderby.DefaultLimitSpec) LookupDataSource(org.apache.druid.query.LookupDataSource) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 93 with QueryDataSource

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

the class CalciteJoinQueryTest method testInnerJoinTwoLookupsToTableUsingNumericColumn.

@Test
@Parameters(source = QueryContextForJoinProvider.class)
public void testInnerJoinTwoLookupsToTableUsingNumericColumn(Map<String, Object> queryContext) throws Exception {
    // Regression test for https://github.com/apache/druid/issues/9646.
    // Cannot vectorize JOIN operator.
    cannotVectorize();
    testQuery("SELECT COUNT(*)\n" + "FROM foo\n" + "INNER JOIN lookup.lookyloo l1 ON l1.k = foo.m1\n" + "INNER JOIN lookup.lookyloo l2 ON l2.k = l1.k", queryContext, ImmutableList.of(Druids.newTimeseriesQueryBuilder().dataSource(join(join(new TableDataSource(CalciteTests.DATASOURCE1), new QueryDataSource(newScanQueryBuilder().dataSource(new LookupDataSource("lookyloo")).intervals(querySegmentSpec(Filtration.eternity())).virtualColumns(expressionVirtualColumn("v0", "CAST(\"k\", 'DOUBLE')", ColumnType.FLOAT)).columns("k", "v0").context(QUERY_CONTEXT_DEFAULT).build()), "j0.", equalsCondition(DruidExpression.ofColumn(ColumnType.FLOAT, "m1"), DruidExpression.ofColumn(ColumnType.FLOAT, "j0.v0")), JoinType.INNER), new LookupDataSource("lookyloo"), "_j0.", equalsCondition(makeColumnExpression("j0.k"), makeColumnExpression("_j0.k")), JoinType.INNER)).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).aggregators(new CountAggregatorFactory("a0")).context(QUERY_CONTEXT_DEFAULT).build()), ImmutableList.of(new Object[] { 1L }));
}
Also used : GlobalTableDataSource(org.apache.druid.query.GlobalTableDataSource) TableDataSource(org.apache.druid.query.TableDataSource) QueryDataSource(org.apache.druid.query.QueryDataSource) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) LookupDataSource(org.apache.druid.query.LookupDataSource) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 94 with QueryDataSource

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

the class CalciteJoinQueryTest method testInnerJoinQueryOfLookup.

@Test
@Parameters(source = QueryContextForJoinProvider.class)
public void testInnerJoinQueryOfLookup(Map<String, Object> queryContext) throws Exception {
    // Cannot vectorize the subquery.
    cannotVectorize();
    testQuery("SELECT dim1, dim2, t1.v, t1.v\n" + "FROM foo\n" + "INNER JOIN \n" + "  (SELECT SUBSTRING(k, 1, 1) k, LATEST(v, 10) v FROM lookup.lookyloo GROUP BY 1) t1\n" + "  ON foo.dim2 = t1.k", queryContext, ImmutableList.of(newScanQueryBuilder().dataSource(join(new TableDataSource(CalciteTests.DATASOURCE1), new QueryDataSource(GroupByQuery.builder().setDataSource(new LookupDataSource("lookyloo")).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setDimensions(new ExtractionDimensionSpec("k", "d0", new SubstringDimExtractionFn(0, 1))).setAggregatorSpecs(new StringLastAggregatorFactory("a0", "v", null, 10)).build()), "j0.", equalsCondition(makeColumnExpression("dim2"), makeColumnExpression("j0.d0")), JoinType.INNER)).intervals(querySegmentSpec(Filtration.eternity())).columns("dim1", "dim2", "j0.a0").context(queryContext).build()), ImmutableList.of(new Object[] { "", "a", "xabc", "xabc" }, new Object[] { "1", "a", "xabc", "xabc" }));
}
Also used : SubstringDimExtractionFn(org.apache.druid.query.extraction.SubstringDimExtractionFn) GlobalTableDataSource(org.apache.druid.query.GlobalTableDataSource) TableDataSource(org.apache.druid.query.TableDataSource) QueryDataSource(org.apache.druid.query.QueryDataSource) LookupDataSource(org.apache.druid.query.LookupDataSource) StringLastAggregatorFactory(org.apache.druid.query.aggregation.last.StringLastAggregatorFactory) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 95 with QueryDataSource

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

the class CalciteJoinQueryTest method testSemiAndAntiJoinSimultaneouslyUsingExplicitJoins.

@Test
@Parameters(source = QueryContextForJoinProvider.class)
public void testSemiAndAntiJoinSimultaneouslyUsingExplicitJoins(Map<String, Object> queryContext) throws Exception {
    cannotVectorize();
    testQuery("SELECT dim1, COUNT(*) FROM\n" + "foo\n" + "INNER JOIN (SELECT MAX(__time) t FROM foo) t0 on t0.t = foo.__time\n" + "LEFT JOIN (SELECT MIN(__time) t FROM foo) t1 on t1.t = foo.__time\n" + "WHERE dim1 IN ('abc', 'def') AND t1.t is null\n" + "GROUP BY 1", queryContext, ImmutableList.of(GroupByQuery.builder().setDataSource(join(join(new TableDataSource(CalciteTests.DATASOURCE1), new QueryDataSource(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).aggregators(new LongMaxAggregatorFactory("a0", "__time")).context(QUERY_CONTEXT_DEFAULT).build()), "j0.", "(\"__time\" == \"j0.a0\")", JoinType.INNER), new QueryDataSource(Druids.newTimeseriesQueryBuilder().dataSource(CalciteTests.DATASOURCE1).intervals(querySegmentSpec(Filtration.eternity())).granularity(Granularities.ALL).aggregators(new LongMinAggregatorFactory("a0", "__time")).context(QUERY_CONTEXT_DEFAULT).build()), "_j0.", "(\"__time\" == \"_j0.a0\")", JoinType.LEFT)).setInterval(querySegmentSpec(Filtration.eternity())).setGranularity(Granularities.ALL).setDimFilter(and(in("dim1", ImmutableList.of("abc", "def"), null), selector("_j0.a0", null, null))).setDimensions(dimensions(new DefaultDimensionSpec("dim1", "d0", ColumnType.STRING))).setAggregatorSpecs(aggregators(new CountAggregatorFactory("a0"))).setContext(queryContext).build()), ImmutableList.of(new Object[] { "abc", 1L }));
}
Also used : GlobalTableDataSource(org.apache.druid.query.GlobalTableDataSource) TableDataSource(org.apache.druid.query.TableDataSource) QueryDataSource(org.apache.druid.query.QueryDataSource) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) LongMaxAggregatorFactory(org.apache.druid.query.aggregation.LongMaxAggregatorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) LongMinAggregatorFactory(org.apache.druid.query.aggregation.LongMinAggregatorFactory) Parameters(junitparams.Parameters) Test(org.junit.Test)

Aggregations

QueryDataSource (org.apache.druid.query.QueryDataSource)95 Test (org.junit.Test)81 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)51 TableDataSource (org.apache.druid.query.TableDataSource)44 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)41 Parameters (junitparams.Parameters)30 GlobalTableDataSource (org.apache.druid.query.GlobalTableDataSource)29 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)23 FilteredAggregatorFactory (org.apache.druid.query.aggregation.FilteredAggregatorFactory)14 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)14 LookupDataSource (org.apache.druid.query.LookupDataSource)13 SelectorDimFilter (org.apache.druid.query.filter.SelectorDimFilter)11 GroupByQuery (org.apache.druid.query.groupby.GroupByQuery)11 ArithmeticPostAggregator (org.apache.druid.query.aggregation.post.ArithmeticPostAggregator)10 FieldAccessPostAggregator (org.apache.druid.query.aggregation.post.FieldAccessPostAggregator)10 DataSource (org.apache.druid.query.DataSource)8 DoubleSumAggregatorFactory (org.apache.druid.query.aggregation.DoubleSumAggregatorFactory)8 LongMaxAggregatorFactory (org.apache.druid.query.aggregation.LongMaxAggregatorFactory)8 ExpressionVirtualColumn (org.apache.druid.segment.virtual.ExpressionVirtualColumn)8 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)8