Search in sources :

Example 1 with LookupExtractor

use of io.druid.query.lookup.LookupExtractor in project druid by druid-io.

the class InDimFilter method optimizeLookup.

private InDimFilter optimizeLookup() {
    if (extractionFn instanceof LookupExtractionFn && ((LookupExtractionFn) extractionFn).isOptimize()) {
        LookupExtractionFn exFn = (LookupExtractionFn) extractionFn;
        LookupExtractor lookup = exFn.getLookup();
        final List<String> keys = new ArrayList<>();
        for (String value : values) {
            // We cannot do an unapply()-based optimization if the selector value
            // and the replaceMissingValuesWith value are the same, since we have to match on
            // all values that are not present in the lookup.
            final String convertedValue = Strings.emptyToNull(value);
            if (!exFn.isRetainMissingValue() && Objects.equals(convertedValue, exFn.getReplaceMissingValueWith())) {
                return this;
            }
            keys.addAll(lookup.unapply(convertedValue));
            // If the selector value is overwritten in the lookup map, don't add selector value to keys.
            if (exFn.isRetainMissingValue() && lookup.apply(convertedValue) == null) {
                keys.add(convertedValue);
            }
        }
        if (keys.isEmpty()) {
            return this;
        } else {
            return new InDimFilter(dimension, keys, null);
        }
    }
    return this;
}
Also used : LookupExtractionFn(io.druid.query.lookup.LookupExtractionFn) LongArrayList(it.unimi.dsi.fastutil.longs.LongArrayList) ArrayList(java.util.ArrayList) IntArrayList(it.unimi.dsi.fastutil.ints.IntArrayList) LookupExtractor(io.druid.query.lookup.LookupExtractor)

Example 2 with LookupExtractor

use of io.druid.query.lookup.LookupExtractor in project druid by druid-io.

the class CalciteTests method createOperatorTable.

public static DruidOperatorTable createOperatorTable() {
    try {
        final Injector injector = Guice.createInjector(new Module() {

            @Override
            public void configure(final Binder binder) {
                // This Module is just to get a LookupReferencesManager with a usable "lookyloo" lookup.
                final LookupReferencesManager mock = EasyMock.createMock(LookupReferencesManager.class);
                EasyMock.expect(mock.get(EasyMock.eq("lookyloo"))).andReturn(new LookupExtractorFactory() {

                    @Override
                    public boolean start() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean close() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public boolean replaces(@Nullable final LookupExtractorFactory other) {
                        throw new UnsupportedOperationException();
                    }

                    @Nullable
                    @Override
                    public LookupIntrospectHandler getIntrospectHandler() {
                        throw new UnsupportedOperationException();
                    }

                    @Override
                    public LookupExtractor get() {
                        return new MapLookupExtractor(ImmutableMap.of("a", "xa", "abc", "xabc"), false);
                    }
                }).anyTimes();
                EasyMock.replay(mock);
                binder.bind(LookupReferencesManager.class).toInstance(mock);
            }
        });
        final Set<SqlAggregator> aggregators = new HashSet<>();
        final Set<SqlExtractionOperator> extractionOperators = new HashSet<>();
        for (Class<? extends SqlAggregator> clazz : SqlModule.DEFAULT_AGGREGATOR_CLASSES) {
            aggregators.add(injector.getInstance(clazz));
        }
        for (Class<? extends SqlExtractionOperator> clazz : SqlModule.DEFAULT_EXTRACTION_OPERATOR_CLASSES) {
            extractionOperators.add(injector.getInstance(clazz));
        }
        return new DruidOperatorTable(aggregators, extractionOperators);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : LookupExtractorFactory(io.druid.query.lookup.LookupExtractorFactory) LookupReferencesManager(io.druid.query.lookup.LookupReferencesManager) DruidOperatorTable(io.druid.sql.calcite.planner.DruidOperatorTable) LookupExtractor(io.druid.query.lookup.LookupExtractor) MapLookupExtractor(io.druid.query.extraction.MapLookupExtractor) LookupIntrospectHandler(io.druid.query.lookup.LookupIntrospectHandler) Binder(com.google.inject.Binder) SqlExtractionOperator(io.druid.sql.calcite.expression.SqlExtractionOperator) Injector(com.google.inject.Injector) SqlAggregator(io.druid.sql.calcite.aggregation.SqlAggregator) MapLookupExtractor(io.druid.query.extraction.MapLookupExtractor) Module(com.google.inject.Module) SqlModule(io.druid.sql.guice.SqlModule) Nullable(javax.annotation.Nullable) HashSet(java.util.HashSet)

Example 3 with LookupExtractor

use of io.druid.query.lookup.LookupExtractor in project druid by druid-io.

the class FloatFilteringTest method testFloatFilterWithExtractionFn.

@Test
public void testFloatFilterWithExtractionFn() {
    final Map<String, String> stringMap = new HashMap<>();
    stringMap.put("1.0", "Monday");
    stringMap.put("2.0", "Tuesday");
    stringMap.put("3.0", "Wednesday");
    stringMap.put("4.0", "Thursday");
    stringMap.put("5.0", "Friday");
    stringMap.put("6.0", "Saturday");
    LookupExtractor mapExtractor = new MapLookupExtractor(stringMap, false);
    LookupExtractionFn exfn = new LookupExtractionFn(mapExtractor, false, "UNKNOWN", false, true);
    assertFilterMatches(new SelectorDimFilter(FLOAT_COLUMN, "Monday", exfn), ImmutableList.<String>of("1"));
    assertFilterMatches(new SelectorDimFilter(FLOAT_COLUMN, "Notaday", exfn), ImmutableList.<String>of());
    assertFilterMatches(new BoundDimFilter(FLOAT_COLUMN, "Fridax", "Fridaz", false, false, null, exfn, StringComparators.ALPHANUMERIC), ImmutableList.<String>of("5"));
    assertFilterMatches(new BoundDimFilter(FLOAT_COLUMN, "Friday", "Friday", true, true, null, exfn, StringComparators.ALPHANUMERIC), ImmutableList.<String>of());
    assertFilterMatches(new InDimFilter(FLOAT_COLUMN, Arrays.asList("Caturday", "Saturday", "Tuesday"), exfn), ImmutableList.<String>of("2", "6"));
    // test InFilter HashSet implementation
    List<String> bigList = Arrays.asList("Saturday", "Tuesday", "Caturday", "Xanaday", "Vojuday", "Gribaday", "Kipoday", "Dheferday", "Fakeday", "Qeearaday", "Hello", "World", "1", "2", "3", "4", "5", "6", "7");
    assertFilterMatches(new InDimFilter(FLOAT_COLUMN, bigList, exfn), ImmutableList.<String>of("2", "6"));
    String jsFn = "function(x) { return(x === 'Wednesday' || x === 'Thursday') }";
    assertFilterMatches(new JavaScriptDimFilter(FLOAT_COLUMN, jsFn, exfn, JavaScriptConfig.getEnabledInstance()), ImmutableList.<String>of("3", "4"));
    assertFilterMatches(new RegexDimFilter(FLOAT_COLUMN, ".*day", exfn), ImmutableList.<String>of("1", "2", "3", "4", "5", "6"));
    assertFilterMatches(new SearchQueryDimFilter(FLOAT_COLUMN, new ContainsSearchQuerySpec("s", true), exfn), ImmutableList.<String>of("2", "3", "4"));
}
Also used : RegexDimFilter(io.druid.query.filter.RegexDimFilter) BoundDimFilter(io.druid.query.filter.BoundDimFilter) HashMap(java.util.HashMap) ContainsSearchQuerySpec(io.druid.query.search.search.ContainsSearchQuerySpec) MapLookupExtractor(io.druid.query.extraction.MapLookupExtractor) LookupExtractor(io.druid.query.lookup.LookupExtractor) LookupExtractionFn(io.druid.query.lookup.LookupExtractionFn) SelectorDimFilter(io.druid.query.filter.SelectorDimFilter) InDimFilter(io.druid.query.filter.InDimFilter) SearchQueryDimFilter(io.druid.query.filter.SearchQueryDimFilter) MapLookupExtractor(io.druid.query.extraction.MapLookupExtractor) JavaScriptDimFilter(io.druid.query.filter.JavaScriptDimFilter) Test(org.junit.Test)

Example 4 with LookupExtractor

use of io.druid.query.lookup.LookupExtractor in project druid by druid-io.

the class LongFilteringTest method testLongFilterWithExtractionFn.

@Test
public void testLongFilterWithExtractionFn() {
    final Map<String, String> stringMap = new HashMap<>();
    stringMap.put("1", "Monday");
    stringMap.put("2", "Tuesday");
    stringMap.put("3", "Wednesday");
    stringMap.put("4", "Thursday");
    stringMap.put("5", "Friday");
    stringMap.put("6", "Saturday");
    LookupExtractor mapExtractor = new MapLookupExtractor(stringMap, false);
    LookupExtractionFn exfn = new LookupExtractionFn(mapExtractor, false, "UNKNOWN", false, true);
    assertFilterMatches(new SelectorDimFilter(LONG_COLUMN, "Monday", exfn), ImmutableList.<String>of("1"));
    assertFilterMatches(new SelectorDimFilter(LONG_COLUMN, "Notaday", exfn), ImmutableList.<String>of());
    assertFilterMatches(new BoundDimFilter(LONG_COLUMN, "Fridax", "Fridaz", false, false, null, exfn, StringComparators.ALPHANUMERIC), ImmutableList.<String>of("5"));
    assertFilterMatches(new BoundDimFilter(LONG_COLUMN, "Friday", "Friday", true, true, null, exfn, StringComparators.ALPHANUMERIC), ImmutableList.<String>of());
    assertFilterMatches(new InDimFilter(LONG_COLUMN, Arrays.asList("Caturday", "Saturday", "Tuesday"), exfn), ImmutableList.<String>of("2", "6"));
    // test InFilter HashSet implementation
    List<String> bigList = Arrays.asList("Saturday", "Tuesday", "Caturday", "Xanaday", "Vojuday", "Gribaday", "Kipoday", "Dheferday", "Fakeday", "Qeearaday", "Hello", "World", "1", "2", "3", "4", "5", "6", "7");
    assertFilterMatches(new InDimFilter(LONG_COLUMN, bigList, exfn), ImmutableList.<String>of("2", "6"));
    String jsFn = "function(x) { return(x === 'Wednesday' || x === 'Thursday') }";
    assertFilterMatches(new JavaScriptDimFilter(LONG_COLUMN, jsFn, exfn, JavaScriptConfig.getEnabledInstance()), ImmutableList.<String>of("3", "4"));
    assertFilterMatches(new RegexDimFilter(LONG_COLUMN, ".*day", exfn), ImmutableList.<String>of("1", "2", "3", "4", "5", "6"));
    assertFilterMatches(new SearchQueryDimFilter(LONG_COLUMN, new ContainsSearchQuerySpec("s", true), exfn), ImmutableList.<String>of("2", "3", "4"));
}
Also used : RegexDimFilter(io.druid.query.filter.RegexDimFilter) BoundDimFilter(io.druid.query.filter.BoundDimFilter) HashMap(java.util.HashMap) ContainsSearchQuerySpec(io.druid.query.search.search.ContainsSearchQuerySpec) MapLookupExtractor(io.druid.query.extraction.MapLookupExtractor) LookupExtractor(io.druid.query.lookup.LookupExtractor) LookupExtractionFn(io.druid.query.lookup.LookupExtractionFn) SelectorDimFilter(io.druid.query.filter.SelectorDimFilter) InDimFilter(io.druid.query.filter.InDimFilter) SearchQueryDimFilter(io.druid.query.filter.SearchQueryDimFilter) MapLookupExtractor(io.druid.query.extraction.MapLookupExtractor) JavaScriptDimFilter(io.druid.query.filter.JavaScriptDimFilter) Test(org.junit.Test)

Example 5 with LookupExtractor

use of io.druid.query.lookup.LookupExtractor in project druid by druid-io.

the class SelectorFilterTest method testSelectorWithLookupExtractionFn.

@Test
public void testSelectorWithLookupExtractionFn() {
    final Map<String, String> stringMap = ImmutableMap.of("1", "HELLO", "a", "HELLO", "def", "HELLO", "abc", "UNKNOWN");
    LookupExtractor mapExtractor = new MapLookupExtractor(stringMap, false);
    LookupExtractionFn lookupFn = new LookupExtractionFn(mapExtractor, false, "UNKNOWN", false, true);
    assertFilterMatches(new SelectorDimFilter("dim0", "HELLO", lookupFn), ImmutableList.of("1"));
    assertFilterMatches(new SelectorDimFilter("dim0", "UNKNOWN", lookupFn), ImmutableList.of("0", "2", "3", "4", "5"));
    assertFilterMatches(new SelectorDimFilter("dim1", "HELLO", lookupFn), ImmutableList.of("3", "4"));
    assertFilterMatches(new SelectorDimFilter("dim1", "UNKNOWN", lookupFn), ImmutableList.of("0", "1", "2", "5"));
    assertFilterMatches(new SelectorDimFilter("dim2", "HELLO", lookupFn), ImmutableList.of("0", "3"));
    assertFilterMatches(new SelectorDimFilter("dim2", "UNKNOWN", lookupFn), ImmutableList.of("0", "1", "2", "4", "5"));
    assertFilterMatches(new SelectorDimFilter("dim3", "HELLO", lookupFn), ImmutableList.<String>of());
    assertFilterMatches(new SelectorDimFilter("dim3", "UNKNOWN", lookupFn), ImmutableList.of("0", "1", "2", "3", "4", "5"));
    assertFilterMatches(new SelectorDimFilter("dim4", "HELLO", lookupFn), ImmutableList.<String>of());
    assertFilterMatches(new SelectorDimFilter("dim4", "UNKNOWN", lookupFn), ImmutableList.of("0", "1", "2", "3", "4", "5"));
    final Map<String, String> stringMap2 = ImmutableMap.of("2", "5");
    LookupExtractor mapExtractor2 = new MapLookupExtractor(stringMap2, false);
    LookupExtractionFn lookupFn2 = new LookupExtractionFn(mapExtractor2, true, null, false, true);
    assertFilterMatches(new SelectorDimFilter("dim0", "5", lookupFn2), ImmutableList.of("2", "5"));
    final Map<String, String> stringMap3 = ImmutableMap.of("1", "");
    LookupExtractor mapExtractor3 = new MapLookupExtractor(stringMap3, false);
    LookupExtractionFn lookupFn3 = new LookupExtractionFn(mapExtractor3, false, null, false, true);
    assertFilterMatches(new SelectorDimFilter("dim0", null, lookupFn3), ImmutableList.of("0", "1", "2", "3", "4", "5"));
    final Map<String, String> stringMap4 = ImmutableMap.of("9", "4");
    LookupExtractor mapExtractor4 = new MapLookupExtractor(stringMap4, false);
    LookupExtractionFn lookupFn4 = new LookupExtractionFn(mapExtractor4, true, null, false, true);
    final Map<String, String> stringMap5 = ImmutableMap.of("5", "44");
    LookupExtractor mapExtractor5 = new MapLookupExtractor(stringMap5, false);
    LookupExtractionFn lookupFn5 = new LookupExtractionFn(mapExtractor5, true, null, false, true);
    final Map<String, String> stringMap6 = ImmutableMap.of("5", "5");
    LookupExtractor mapExtractor6 = new MapLookupExtractor(stringMap6, false);
    LookupExtractionFn lookupFn6 = new LookupExtractionFn(mapExtractor6, true, null, false, true);
    // optimize() tests, check that filter was converted to the proper form
    SelectorDimFilter optFilter1 = new SelectorDimFilter("dim1", "UNKNOWN", lookupFn);
    SelectorDimFilter optFilter2 = new SelectorDimFilter("dim0", "5", lookupFn2);
    SelectorDimFilter optFilter3 = new SelectorDimFilter("dim0", null, lookupFn3);
    SelectorDimFilter optFilter4 = new SelectorDimFilter("dim0", "5", lookupFn4);
    SelectorDimFilter optFilter5 = new SelectorDimFilter("dim0", "5", lookupFn5);
    SelectorDimFilter optFilter6 = new SelectorDimFilter("dim0", "5", lookupFn6);
    InDimFilter optFilter2Optimized = new InDimFilter("dim0", Arrays.asList("2", "5"), null);
    SelectorDimFilter optFilter4Optimized = new SelectorDimFilter("dim0", "5", null);
    SelectorDimFilter optFilter6Optimized = new SelectorDimFilter("dim0", "5", null);
    Assert.assertTrue(optFilter1.equals(optFilter1.optimize()));
    Assert.assertTrue(optFilter2Optimized.equals(optFilter2.optimize()));
    Assert.assertTrue(optFilter3.equals(optFilter3.optimize()));
    Assert.assertTrue(optFilter4Optimized.equals(optFilter4.optimize()));
    Assert.assertTrue(optFilter5.equals(optFilter5.optimize()));
    Assert.assertTrue(optFilter6Optimized.equals(optFilter6.optimize()));
    assertFilterMatches(optFilter1, ImmutableList.of("0", "1", "2", "5"));
    assertFilterMatches(optFilter2, ImmutableList.of("2", "5"));
    assertFilterMatches(optFilter3, ImmutableList.of("0", "1", "2", "3", "4", "5"));
    assertFilterMatches(optFilter4, ImmutableList.of("5"));
    assertFilterMatches(optFilter5, ImmutableList.<String>of());
    assertFilterMatches(optFilter6, ImmutableList.of("5"));
    // tests that ExtractionDimFilter (identical to SelectorDimFilter now) optimize() with lookup works
    // remove these when ExtractionDimFilter is removed.
    assertFilterMatches(new ExtractionDimFilter("dim1", "UNKNOWN", lookupFn, null), ImmutableList.of("0", "1", "2", "5"));
    assertFilterMatches(new ExtractionDimFilter("dim0", "5", lookupFn2, null), ImmutableList.of("2", "5"));
    assertFilterMatches(new ExtractionDimFilter("dim0", null, lookupFn3, null), ImmutableList.of("0", "1", "2", "3", "4", "5"));
}
Also used : LookupExtractionFn(io.druid.query.lookup.LookupExtractionFn) SelectorDimFilter(io.druid.query.filter.SelectorDimFilter) InDimFilter(io.druid.query.filter.InDimFilter) MapLookupExtractor(io.druid.query.extraction.MapLookupExtractor) ExtractionDimFilter(io.druid.query.filter.ExtractionDimFilter) MapLookupExtractor(io.druid.query.extraction.MapLookupExtractor) LookupExtractor(io.druid.query.lookup.LookupExtractor) Test(org.junit.Test)

Aggregations

LookupExtractor (io.druid.query.lookup.LookupExtractor)8 MapLookupExtractor (io.druid.query.extraction.MapLookupExtractor)7 LookupExtractionFn (io.druid.query.lookup.LookupExtractionFn)7 Test (org.junit.Test)6 InDimFilter (io.druid.query.filter.InDimFilter)4 SelectorDimFilter (io.druid.query.filter.SelectorDimFilter)4 BoundDimFilter (io.druid.query.filter.BoundDimFilter)3 JavaScriptDimFilter (io.druid.query.filter.JavaScriptDimFilter)3 RegexDimFilter (io.druid.query.filter.RegexDimFilter)3 SearchQueryDimFilter (io.druid.query.filter.SearchQueryDimFilter)3 ContainsSearchQuerySpec (io.druid.query.search.search.ContainsSearchQuerySpec)3 HashMap (java.util.HashMap)3 Binder (com.google.inject.Binder)1 Injector (com.google.inject.Injector)1 Module (com.google.inject.Module)1 ExtractionDimFilter (io.druid.query.filter.ExtractionDimFilter)1 LookupExtractorFactory (io.druid.query.lookup.LookupExtractorFactory)1 LookupIntrospectHandler (io.druid.query.lookup.LookupIntrospectHandler)1 LookupReferencesManager (io.druid.query.lookup.LookupReferencesManager)1 SqlAggregator (io.druid.sql.calcite.aggregation.SqlAggregator)1