Search in sources :

Example 6 with JoinDataSource

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

the class DataSourceAnalysis method flattenJoin.

/**
 * Flatten a datasource into two parts: the left-hand side datasource (the 'base' datasource), and a list of join
 * clauses, if any.
 *
 * @throws IllegalArgumentException if dataSource cannot be fully flattened.
 */
private static Triple<DataSource, DimFilter, List<PreJoinableClause>> flattenJoin(final JoinDataSource dataSource) {
    DataSource current = dataSource;
    DimFilter currentDimFilter = null;
    final List<PreJoinableClause> preJoinableClauses = new ArrayList<>();
    while (current instanceof JoinDataSource) {
        final JoinDataSource joinDataSource = (JoinDataSource) current;
        current = joinDataSource.getLeft();
        if (currentDimFilter != null) {
            throw new IAE("Left filters are only allowed when left child is direct table access");
        }
        currentDimFilter = joinDataSource.getLeftFilter();
        preJoinableClauses.add(new PreJoinableClause(joinDataSource.getRightPrefix(), joinDataSource.getRight(), joinDataSource.getJoinType(), joinDataSource.getConditionAnalysis()));
    }
    // Join clauses were added in the order we saw them while traversing down, but we need to apply them in the
    // going-up order. So reverse them.
    Collections.reverse(preJoinableClauses);
    return Triple.of(current, currentDimFilter, preJoinableClauses);
}
Also used : ArrayList(java.util.ArrayList) JoinDataSource(org.apache.druid.query.JoinDataSource) 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)

Example 7 with JoinDataSource

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

the class DataSourceAnalysisTest method testJoinSimpleLeftLeaningWithLeftFilter.

@Test
public void testJoinSimpleLeftLeaningWithLeftFilter() {
    final JoinDataSource joinDataSource = join(join(join(TABLE_FOO, LOOKUP_LOOKYLOO, "1.", JoinType.INNER, TrueDimFilter.instance()), INLINE, "2.", JoinType.LEFT), subquery(LOOKUP_LOOKYLOO), "3.", JoinType.FULL);
    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.getBaseUnionDataSource());
    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.")), new PreJoinableClause("2.", INLINE, JoinType.LEFT, joinClause("2.")), new PreJoinableClause("3.", subquery(LOOKUP_LOOKYLOO), JoinType.FULL, joinClause("3."))), analysis.getPreJoinableClauses());
    Assert.assertTrue(analysis.isJoin());
}
Also used : JoinDataSource(org.apache.druid.query.JoinDataSource) Test(org.junit.Test)

Example 8 with JoinDataSource

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

the class DataSourceAnalysisTest method testJoinOverTableSubquery.

@Test
public void testJoinOverTableSubquery() {
    final JoinDataSource joinDataSource = join(TABLE_FOO, subquery(TABLE_FOO), "1.", JoinType.INNER, TrueDimFilter.instance());
    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(TABLE_FOO, analysis.getBaseDataSource());
    Assert.assertEquals(TrueDimFilter.instance(), analysis.getJoinBaseTableFilter().orElse(null));
    Assert.assertEquals(Optional.of(TABLE_FOO), analysis.getBaseTableDataSource());
    Assert.assertEquals(Optional.empty(), analysis.getBaseUnionDataSource());
    Assert.assertEquals(ImmutableList.of(new PreJoinableClause("1.", subquery(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 9 with JoinDataSource

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

the class DataSourceAnalysisTest method testJoinLookupToLookup.

@Test
public void testJoinLookupToLookup() {
    final JoinDataSource joinDataSource = join(LOOKUP_LOOKYLOO, LOOKUP_LOOKYLOO, "1.", JoinType.INNER);
    final DataSourceAnalysis analysis = DataSourceAnalysis.forDataSource(joinDataSource);
    Assert.assertTrue(analysis.isConcreteBased());
    Assert.assertFalse(analysis.isConcreteTableBased());
    Assert.assertTrue(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.", LOOKUP_LOOKYLOO, JoinType.INNER, joinClause("1."))), analysis.getPreJoinableClauses());
    Assert.assertTrue(analysis.isJoin());
}
Also used : JoinDataSource(org.apache.druid.query.JoinDataSource) Test(org.junit.Test)

Example 10 with JoinDataSource

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

the class DataSourceAnalysisTest method testJoinSimpleLeftLeaning.

@Test
public void testJoinSimpleLeftLeaning() {
    // Join of a table onto a variety of simple joinable objects (lookup, inline, subquery) with a left-leaning
    // structure (no right children are joins themselves).
    final JoinDataSource joinDataSource = join(join(join(TABLE_FOO, LOOKUP_LOOKYLOO, "1.", JoinType.INNER), INLINE, "2.", JoinType.LEFT), subquery(LOOKUP_LOOKYLOO), "3.", JoinType.FULL);
    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(Optional.empty(), analysis.getJoinBaseTableFilter());
    Assert.assertEquals(Optional.empty(), analysis.getBaseUnionDataSource());
    Assert.assertEquals(Optional.empty(), analysis.getBaseUnionDataSource());
    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.")), new PreJoinableClause("2.", INLINE, JoinType.LEFT, joinClause("2.")), new PreJoinableClause("3.", subquery(LOOKUP_LOOKYLOO), JoinType.FULL, joinClause("3."))), analysis.getPreJoinableClauses());
    Assert.assertTrue(analysis.isJoin());
}
Also used : JoinDataSource(org.apache.druid.query.JoinDataSource) Test(org.junit.Test)

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