Search in sources :

Example 6 with PreJoinableClause

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

the class JoinableFactoryWrapperTest method test_computeJoinDataSourceCacheKey_keyChangesWithPrefix.

@Test
public void test_computeJoinDataSourceCacheKey_keyChangesWithPrefix() {
    DataSourceAnalysis analysis = EasyMock.mock(DataSourceAnalysis.class);
    EasyMock.expect(analysis.getJoinBaseTableFilter()).andReturn(Optional.empty()).anyTimes();
    JoinableFactoryWrapper joinableFactoryWrapper = new JoinableFactoryWrapper(new JoinableFactoryWithCacheKey());
    PreJoinableClause clause1 = makeGlobalPreJoinableClause("dataSource_1", "abc == xyz", "ab");
    EasyMock.expect(analysis.getPreJoinableClauses()).andReturn(Collections.singletonList(clause1)).anyTimes();
    EasyMock.replay(analysis);
    Optional<byte[]> cacheKey1 = joinableFactoryWrapper.computeJoinDataSourceCacheKey(analysis);
    Assert.assertTrue(cacheKey1.isPresent());
    Assert.assertNotEquals(0, cacheKey1.get().length);
    PreJoinableClause clause2 = makeGlobalPreJoinableClause("dataSource_1", "abc == xyz", "xy");
    EasyMock.reset(analysis);
    EasyMock.expect(analysis.getPreJoinableClauses()).andReturn(Collections.singletonList(clause2)).anyTimes();
    EasyMock.expect(analysis.getJoinBaseTableFilter()).andReturn(Optional.empty()).anyTimes();
    EasyMock.replay(analysis);
    Optional<byte[]> cacheKey2 = joinableFactoryWrapper.computeJoinDataSourceCacheKey(analysis);
    Assert.assertTrue(cacheKey2.isPresent());
    Assert.assertFalse(Arrays.equals(cacheKey1.get(), cacheKey2.get()));
}
Also used : PreJoinableClause(org.apache.druid.query.planning.PreJoinableClause) DataSourceAnalysis(org.apache.druid.query.planning.DataSourceAnalysis) NullHandlingTest(org.apache.druid.common.config.NullHandlingTest) Test(org.junit.Test)

Example 7 with PreJoinableClause

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

the class JoinableFactoryWrapperTest method test_computeJoinDataSourceCacheKey_keyChangesWithJoinType.

@Test
public void test_computeJoinDataSourceCacheKey_keyChangesWithJoinType() {
    DataSourceAnalysis analysis = EasyMock.mock(DataSourceAnalysis.class);
    EasyMock.expect(analysis.getJoinBaseTableFilter()).andReturn(Optional.empty()).anyTimes();
    JoinableFactoryWrapper joinableFactoryWrapper = new JoinableFactoryWrapper(new JoinableFactoryWithCacheKey());
    PreJoinableClause clause1 = makeGlobalPreJoinableClause("dataSource_1", "x == \"j.x\"", "j.", JoinType.LEFT);
    EasyMock.expect(analysis.getPreJoinableClauses()).andReturn(Collections.singletonList(clause1)).anyTimes();
    EasyMock.replay(analysis);
    Optional<byte[]> cacheKey1 = joinableFactoryWrapper.computeJoinDataSourceCacheKey(analysis);
    Assert.assertTrue(cacheKey1.isPresent());
    Assert.assertNotEquals(0, cacheKey1.get().length);
    PreJoinableClause clause2 = makeGlobalPreJoinableClause("dataSource_1", "x == \"j.x\"", "j.", JoinType.INNER);
    EasyMock.reset(analysis);
    EasyMock.expect(analysis.getPreJoinableClauses()).andReturn(Collections.singletonList(clause2)).anyTimes();
    EasyMock.expect(analysis.getJoinBaseTableFilter()).andReturn(Optional.empty()).anyTimes();
    EasyMock.replay(analysis);
    Optional<byte[]> cacheKey2 = joinableFactoryWrapper.computeJoinDataSourceCacheKey(analysis);
    Assert.assertTrue(cacheKey2.isPresent());
    Assert.assertFalse(Arrays.equals(cacheKey1.get(), cacheKey2.get()));
}
Also used : PreJoinableClause(org.apache.druid.query.planning.PreJoinableClause) DataSourceAnalysis(org.apache.druid.query.planning.DataSourceAnalysis) NullHandlingTest(org.apache.druid.common.config.NullHandlingTest) Test(org.junit.Test)

Example 8 with PreJoinableClause

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

the class JoinableFactoryWrapperTest method test_computeJoinDataSourceCacheKey_cachingUnsupported.

@Test
public void test_computeJoinDataSourceCacheKey_cachingUnsupported() {
    PreJoinableClause clause1 = makeGlobalPreJoinableClause("dataSource_1", "x == \"j.x\"", "j.");
    DataSource dataSource = new LookupDataSource("lookup");
    PreJoinableClause clause2 = makePreJoinableClause(dataSource, "x == \"h.x\"", "h.", JoinType.LEFT);
    DataSourceAnalysis analysis = EasyMock.mock(DataSourceAnalysis.class);
    EasyMock.expect(analysis.getPreJoinableClauses()).andReturn(Arrays.asList(clause1, clause2)).anyTimes();
    EasyMock.expect(analysis.getJoinBaseTableFilter()).andReturn(Optional.of(TrueDimFilter.instance())).anyTimes();
    EasyMock.replay(analysis);
    JoinableFactoryWrapper joinableFactoryWrapper = new JoinableFactoryWrapper(new JoinableFactoryWithCacheKey());
    Optional<byte[]> cacheKey = joinableFactoryWrapper.computeJoinDataSourceCacheKey(analysis);
    Assert.assertFalse(cacheKey.isPresent());
}
Also used : PreJoinableClause(org.apache.druid.query.planning.PreJoinableClause) LookupDataSource(org.apache.druid.query.LookupDataSource) DataSourceAnalysis(org.apache.druid.query.planning.DataSourceAnalysis) GlobalTableDataSource(org.apache.druid.query.GlobalTableDataSource) LookupDataSource(org.apache.druid.query.LookupDataSource) DataSource(org.apache.druid.query.DataSource) TableDataSource(org.apache.druid.query.TableDataSource) NullHandlingTest(org.apache.druid.common.config.NullHandlingTest) Test(org.junit.Test)

Example 9 with PreJoinableClause

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

the class Queries method withBaseDataSource.

/**
 * Rewrite "query" to refer to some specific base datasource, instead of the one it currently refers to.
 *
 * Unlike the seemingly-similar {@link Query#withDataSource}, this will walk down the datasource tree and replace
 * only the base datasource (in the sense defined in {@link DataSourceAnalysis}).
 */
public static <T> Query<T> withBaseDataSource(final Query<T> query, final DataSource newBaseDataSource) {
    final Query<T> retVal;
    if (query.getDataSource() instanceof QueryDataSource) {
        final Query<?> subQuery = ((QueryDataSource) query.getDataSource()).getQuery();
        retVal = query.withDataSource(new QueryDataSource(withBaseDataSource(subQuery, newBaseDataSource)));
    } else {
        final DataSourceAnalysis analysis = DataSourceAnalysis.forDataSource(query.getDataSource());
        DataSource current = newBaseDataSource;
        DimFilter joinBaseFilter = analysis.getJoinBaseTableFilter().orElse(null);
        for (final PreJoinableClause clause : analysis.getPreJoinableClauses()) {
            current = JoinDataSource.create(current, clause.getDataSource(), clause.getPrefix(), clause.getCondition(), clause.getJoinType(), joinBaseFilter);
            joinBaseFilter = null;
        }
        retVal = query.withDataSource(current);
    }
    // Verify postconditions, just in case.
    final DataSourceAnalysis analysis = DataSourceAnalysis.forDataSource(retVal.getDataSource());
    if (!newBaseDataSource.equals(analysis.getBaseDataSource())) {
        throw new ISE("Unable to replace base dataSource");
    }
    return retVal;
}
Also used : PreJoinableClause(org.apache.druid.query.planning.PreJoinableClause) ISE(org.apache.druid.java.util.common.ISE) DataSourceAnalysis(org.apache.druid.query.planning.DataSourceAnalysis) DimFilter(org.apache.druid.query.filter.DimFilter)

Example 10 with PreJoinableClause

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

the class JoinableFactoryWrapperTest method test_computeJoinDataSourceCacheKey_sameKeyForSameJoin.

@Test
public void test_computeJoinDataSourceCacheKey_sameKeyForSameJoin() {
    DataSourceAnalysis analysis = EasyMock.mock(DataSourceAnalysis.class);
    JoinableFactoryWrapper joinableFactoryWrapper = new JoinableFactoryWrapper(new JoinableFactoryWithCacheKey());
    PreJoinableClause clause1 = makeGlobalPreJoinableClause("dataSource_1", "x == \"j.x\"", "j.");
    EasyMock.expect(analysis.getPreJoinableClauses()).andReturn(Collections.singletonList(clause1)).anyTimes();
    EasyMock.expect(analysis.getJoinBaseTableFilter()).andReturn(Optional.empty()).anyTimes();
    EasyMock.replay(analysis);
    Optional<byte[]> cacheKey1 = joinableFactoryWrapper.computeJoinDataSourceCacheKey(analysis);
    Assert.assertTrue(cacheKey1.isPresent());
    Assert.assertNotEquals(0, cacheKey1.get().length);
    PreJoinableClause clause2 = makeGlobalPreJoinableClause("dataSource_1", "x == \"j.x\"", "j.");
    EasyMock.reset(analysis);
    EasyMock.expect(analysis.getPreJoinableClauses()).andReturn(Collections.singletonList(clause2)).anyTimes();
    EasyMock.expect(analysis.getJoinBaseTableFilter()).andReturn(Optional.empty()).anyTimes();
    EasyMock.replay(analysis);
    Optional<byte[]> cacheKey2 = joinableFactoryWrapper.computeJoinDataSourceCacheKey(analysis);
    Assert.assertTrue(cacheKey2.isPresent());
    Assert.assertArrayEquals(cacheKey1.get(), cacheKey2.get());
}
Also used : PreJoinableClause(org.apache.druid.query.planning.PreJoinableClause) DataSourceAnalysis(org.apache.druid.query.planning.DataSourceAnalysis) NullHandlingTest(org.apache.druid.common.config.NullHandlingTest) Test(org.junit.Test)

Aggregations

PreJoinableClause (org.apache.druid.query.planning.PreJoinableClause)16 NullHandlingTest (org.apache.druid.common.config.NullHandlingTest)11 DataSourceAnalysis (org.apache.druid.query.planning.DataSourceAnalysis)11 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)3 Optional (java.util.Optional)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 ISE (org.apache.druid.java.util.common.ISE)3 LookupDataSource (org.apache.druid.query.LookupDataSource)3 SegmentReference (org.apache.druid.segment.SegmentReference)3 List (java.util.List)2 Nullable (javax.annotation.Nullable)2 IAE (org.apache.druid.java.util.common.IAE)2 DataSource (org.apache.druid.query.DataSource)2 GlobalTableDataSource (org.apache.druid.query.GlobalTableDataSource)2 TableDataSource (org.apache.druid.query.TableDataSource)2 CacheKeyBuilder (org.apache.druid.query.cache.CacheKeyBuilder)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions (com.google.common.base.Preconditions)1 HashMultiset (com.google.common.collect.HashMultiset)1