Search in sources :

Example 1 with JoinDataSource

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

the class DataSourceAnalysisTest method testJoinSimpleRightLeaningWithLeftFilter.

@Test
public void testJoinSimpleRightLeaningWithLeftFilter() {
    final JoinDataSource rightLeaningJoinStack = join(LOOKUP_LOOKYLOO, join(INLINE, subquery(LOOKUP_LOOKYLOO), "1.", JoinType.LEFT), "2.", JoinType.FULL);
    final JoinDataSource joinDataSource = join(TABLE_FOO, rightLeaningJoinStack, "3.", JoinType.RIGHT, TrueDimFilter.instance());
    final DataSourceAnalysis analysis = DataSourceAnalysis.forDataSource(joinDataSource);
    Assert.assertTrue(analysis.isConcreteBased());
    Assert.assertTrue(analysis.isConcreteTableBased());
    Assert.assertFalse(analysis.isGlobal());
    Assert.assertFalse(analysis.isQuery());
    Assert.assertEquals(joinDataSource, analysis.getDataSource());
    Assert.assertEquals(TABLE_FOO, analysis.getBaseDataSource());
    Assert.assertEquals(Optional.of(TABLE_FOO), analysis.getBaseTableDataSource());
    Assert.assertEquals(TrueDimFilter.instance(), analysis.getJoinBaseTableFilter().orElse(null));
    Assert.assertEquals(Optional.empty(), analysis.getBaseUnionDataSource());
    Assert.assertEquals(Optional.empty(), analysis.getBaseQuery());
    Assert.assertEquals(Optional.empty(), analysis.getBaseQuerySegmentSpec());
    Assert.assertEquals(ImmutableList.of(new PreJoinableClause("3.", rightLeaningJoinStack, JoinType.RIGHT, joinClause("3."))), analysis.getPreJoinableClauses());
    Assert.assertTrue(analysis.isJoin());
}
Also used : JoinDataSource(org.apache.druid.query.JoinDataSource) Test(org.junit.Test)

Example 2 with JoinDataSource

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

the class DataSourceAnalysisTest method testJoinTableUnionToLookup.

@Test
public void testJoinTableUnionToLookup() {
    final UnionDataSource unionDataSource = new UnionDataSource(ImmutableList.of(TABLE_FOO, TABLE_BAR));
    final JoinDataSource joinDataSource = join(unionDataSource, LOOKUP_LOOKYLOO, "1.", JoinType.INNER);
    final DataSourceAnalysis analysis = DataSourceAnalysis.forDataSource(joinDataSource);
    Assert.assertTrue(analysis.isConcreteBased());
    Assert.assertTrue(analysis.isConcreteTableBased());
    Assert.assertFalse(analysis.isGlobal());
    Assert.assertFalse(analysis.isQuery());
    Assert.assertEquals(joinDataSource, analysis.getDataSource());
    Assert.assertEquals(Optional.empty(), analysis.getBaseTableDataSource());
    Assert.assertEquals(Optional.empty(), analysis.getJoinBaseTableFilter());
    Assert.assertEquals(Optional.of(unionDataSource), analysis.getBaseUnionDataSource());
    Assert.assertEquals(unionDataSource, analysis.getBaseDataSource());
    Assert.assertEquals(Optional.empty(), analysis.getBaseQuery());
    Assert.assertEquals(Optional.empty(), analysis.getBaseQuerySegmentSpec());
    Assert.assertEquals(ImmutableList.of(new PreJoinableClause("1.", LOOKUP_LOOKYLOO, JoinType.INNER, joinClause("1."))), analysis.getPreJoinableClauses());
    Assert.assertTrue(analysis.isJoin());
}
Also used : JoinDataSource(org.apache.druid.query.JoinDataSource) UnionDataSource(org.apache.druid.query.UnionDataSource) Test(org.junit.Test)

Example 3 with JoinDataSource

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

the class DataSourceAnalysisTest method testJoinLookupToTable.

@Test
public void testJoinLookupToTable() {
    final JoinDataSource joinDataSource = join(LOOKUP_LOOKYLOO, TABLE_FOO, "1.", JoinType.INNER);
    final DataSourceAnalysis analysis = DataSourceAnalysis.forDataSource(joinDataSource);
    Assert.assertFalse(analysis.isConcreteBased());
    Assert.assertFalse(analysis.isConcreteTableBased());
    Assert.assertFalse(analysis.isGlobal());
    Assert.assertFalse(analysis.isQuery());
    Assert.assertEquals(joinDataSource, analysis.getDataSource());
    Assert.assertEquals(LOOKUP_LOOKYLOO, analysis.getBaseDataSource());
    Assert.assertEquals(Optional.empty(), analysis.getBaseTableDataSource());
    Assert.assertEquals(Optional.empty(), analysis.getBaseUnionDataSource());
    Assert.assertEquals(Optional.empty(), analysis.getBaseQuery());
    Assert.assertEquals(Optional.empty(), analysis.getBaseQuerySegmentSpec());
    Assert.assertEquals(Optional.empty(), analysis.getJoinBaseTableFilter());
    Assert.assertEquals(ImmutableList.of(new PreJoinableClause("1.", TABLE_FOO, JoinType.INNER, joinClause("1."))), analysis.getPreJoinableClauses());
    Assert.assertTrue(analysis.isJoin());
}
Also used : JoinDataSource(org.apache.druid.query.JoinDataSource) Test(org.junit.Test)

Example 4 with JoinDataSource

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

the class DataSourceAnalysis method forDataSource.

public static DataSourceAnalysis forDataSource(final DataSource dataSource) {
    // Strip outer queries, retaining querySegmentSpecs as we go down (lowest will become the 'baseQuerySegmentSpec').
    Query<?> baseQuery = null;
    DataSource current = dataSource;
    while (current instanceof QueryDataSource) {
        final Query<?> subQuery = ((QueryDataSource) current).getQuery();
        if (!(subQuery instanceof BaseQuery)) {
            // work properly. All builtin query types are BaseQuery, so we only expect this with funky extension queries.
            throw new IAE("Cannot analyze subquery of class[%s]", subQuery.getClass().getName());
        }
        baseQuery = subQuery;
        current = subQuery.getDataSource();
    }
    if (current instanceof JoinDataSource) {
        final Triple<DataSource, DimFilter, List<PreJoinableClause>> flattened = flattenJoin((JoinDataSource) current);
        return new DataSourceAnalysis(dataSource, flattened.first, baseQuery, flattened.second, flattened.third);
    } else {
        return new DataSourceAnalysis(dataSource, current, baseQuery, null, Collections.emptyList());
    }
}
Also used : QueryDataSource(org.apache.druid.query.QueryDataSource) JoinDataSource(org.apache.druid.query.JoinDataSource) ArrayList(java.util.ArrayList) List(java.util.List) IAE(org.apache.druid.java.util.common.IAE) DimFilter(org.apache.druid.query.filter.DimFilter) DataSource(org.apache.druid.query.DataSource) TableDataSource(org.apache.druid.query.TableDataSource) QueryDataSource(org.apache.druid.query.QueryDataSource) UnionDataSource(org.apache.druid.query.UnionDataSource) JoinDataSource(org.apache.druid.query.JoinDataSource) BaseQuery(org.apache.druid.query.BaseQuery)

Example 5 with JoinDataSource

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

the class DruidQuery method getFiltration.

/**
 * Returns a pair of DataSource and Filtration object created on the query filter. In case the, data source is
 * a join datasource, the datasource may be altered and left filter of join datasource may
 * be rid of time filters.
 * TODO: should we optimize the base table filter just like we do with query filters
 */
@VisibleForTesting
static Pair<DataSource, Filtration> getFiltration(DataSource dataSource, DimFilter filter, VirtualColumnRegistry virtualColumnRegistry) {
    if (!(dataSource instanceof JoinDataSource)) {
        return Pair.of(dataSource, toFiltration(filter, virtualColumnRegistry));
    }
    JoinDataSource joinDataSource = (JoinDataSource) dataSource;
    if (joinDataSource.getLeftFilter() == null) {
        return Pair.of(dataSource, toFiltration(filter, virtualColumnRegistry));
    }
    // TODO: We should avoid promoting the time filter as interval for right outer and full outer joins. This is not
    // done now as we apply the intervals to left base table today irrespective of the join type.
    // If the join is left or inner, we can pull the intervals up to the query. This is done
    // so that broker can prune the segments to query.
    Filtration leftFiltration = Filtration.create(joinDataSource.getLeftFilter()).optimize(virtualColumnRegistry.getFullRowSignature());
    // Adds the intervals from the join left filter to query filtration
    Filtration queryFiltration = Filtration.create(filter, leftFiltration.getIntervals()).optimize(virtualColumnRegistry.getFullRowSignature());
    JoinDataSource newDataSource = JoinDataSource.create(joinDataSource.getLeft(), joinDataSource.getRight(), joinDataSource.getRightPrefix(), joinDataSource.getConditionAnalysis(), joinDataSource.getJoinType(), leftFiltration.getDimFilter());
    return Pair.of(newDataSource, queryFiltration);
}
Also used : Filtration(org.apache.druid.sql.calcite.filtration.Filtration) JoinDataSource(org.apache.druid.query.JoinDataSource) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

JoinDataSource (org.apache.druid.query.JoinDataSource)12 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)3 UnionDataSource (org.apache.druid.query.UnionDataSource)3 IAE (org.apache.druid.java.util.common.IAE)2 DataSource (org.apache.druid.query.DataSource)2 QueryDataSource (org.apache.druid.query.QueryDataSource)2 TableDataSource (org.apache.druid.query.TableDataSource)2 DimFilter (org.apache.druid.query.filter.DimFilter)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 IntArrayList (it.unimi.dsi.fastutil.ints.IntArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 BaseQuery (org.apache.druid.query.BaseQuery)1 VirtualColumn (org.apache.druid.segment.VirtualColumn)1 Aggregation (org.apache.druid.sql.calcite.aggregation.Aggregation)1 DimensionExpression (org.apache.druid.sql.calcite.aggregation.DimensionExpression)1 Filtration (org.apache.druid.sql.calcite.filtration.Filtration)1