Search in sources :

Example 26 with ValueMatcher

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

the class AndFilter method makeMatcher.

@Override
public ValueMatcher makeMatcher(BitmapIndexSelector selector, ColumnSelectorFactory columnSelectorFactory, RowOffsetMatcherFactory rowOffsetMatcherFactory) {
    final List<ValueMatcher> matchers = new ArrayList<>();
    final List<ImmutableBitmap> bitmaps = new ArrayList<>();
    for (Filter filter : filters) {
        if (filter.supportsBitmapIndex(selector)) {
            bitmaps.add(filter.getBitmapIndex(selector));
        } else {
            ValueMatcher matcher = filter.makeMatcher(columnSelectorFactory);
            matchers.add(matcher);
        }
    }
    if (bitmaps.size() > 0) {
        ImmutableBitmap combinedBitmap = selector.getBitmapFactory().intersection(bitmaps);
        ValueMatcher offsetMatcher = rowOffsetMatcherFactory.makeRowOffsetMatcher(combinedBitmap);
        matchers.add(0, offsetMatcher);
    }
    return makeMatcher(matchers.toArray(BooleanFilter.EMPTY_VALUE_MATCHER_ARRAY));
}
Also used : VectorValueMatcher(org.apache.druid.query.filter.vector.VectorValueMatcher) ValueMatcher(org.apache.druid.query.filter.ValueMatcher) BaseVectorValueMatcher(org.apache.druid.query.filter.vector.BaseVectorValueMatcher) ImmutableBitmap(org.apache.druid.collections.bitmap.ImmutableBitmap) BooleanFilter(org.apache.druid.query.filter.BooleanFilter) Filter(org.apache.druid.query.filter.Filter) ArrayList(java.util.ArrayList)

Example 27 with ValueMatcher

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

the class ForwardingFilteredDimensionSelector method makeValueMatcher.

@Override
public ValueMatcher makeValueMatcher(final String value) {
    IdLookup idLookup = idLookup();
    if (idLookup != null) {
        final int valueId = idLookup.lookupId(value);
        if (valueId >= 0 || value == null) {
            return new ValueMatcher() {

                @Override
                public boolean matches() {
                    final IndexedInts baseRow = selector.getRow();
                    final int baseRowSize = baseRow.size();
                    boolean nullRow = true;
                    for (int i = 0; i < baseRowSize; i++) {
                        int forwardedValue = idMapping.getForwardedId(baseRow.get(i));
                        if (forwardedValue >= 0) {
                            // valueId is -1, we don't want to return true from matches().
                            if (forwardedValue == valueId) {
                                return true;
                            }
                            nullRow = false;
                        }
                    }
                    // null should match empty rows in multi-value columns
                    return nullRow && value == null;
                }

                @Override
                public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
                    inspector.visit("selector", selector);
                }
            };
        } else {
            return BooleanValueMatcher.of(false);
        }
    } else {
        // Employ precomputed BitSet optimization
        return makeValueMatcher(Predicates.equalTo(value));
    }
}
Also used : IdLookup(org.apache.druid.segment.IdLookup) ValueMatcher(org.apache.druid.query.filter.ValueMatcher) BooleanValueMatcher(org.apache.druid.segment.filter.BooleanValueMatcher) IndexedInts(org.apache.druid.segment.data.IndexedInts) ArrayBasedIndexedInts(org.apache.druid.segment.data.ArrayBasedIndexedInts) RuntimeShapeInspector(org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector)

Example 28 with ValueMatcher

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

the class PredicateFilteredDimensionSelector method makeValueMatcher.

@Override
public ValueMatcher makeValueMatcher(final Predicate<String> matcherPredicate) {
    final boolean matchNull = predicate.apply(null);
    return new ValueMatcher() {

        @Override
        public boolean matches() {
            final IndexedInts baseRow = selector.getRow();
            final int baseRowSize = baseRow.size();
            boolean nullRow = true;
            for (int i = 0; i < baseRowSize; ++i) {
                String rowValue = lookupName(baseRow.get(i));
                if (predicate.apply(rowValue)) {
                    if (matcherPredicate.apply(rowValue)) {
                        return true;
                    }
                    nullRow = false;
                }
            }
            // null should match empty rows in multi-value columns
            return nullRow && matchNull;
        }

        @Override
        public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
            // PredicateFilteredDimensionSelector.this inspects selector and predicate as well.
            inspector.visit("selector", PredicateFilteredDimensionSelector.this);
            inspector.visit("matcherPredicate", matcherPredicate);
        }
    };
}
Also used : ValueMatcher(org.apache.druid.query.filter.ValueMatcher) ArrayBasedIndexedInts(org.apache.druid.segment.data.ArrayBasedIndexedInts) IndexedInts(org.apache.druid.segment.data.IndexedInts) RuntimeShapeInspector(org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector)

Example 29 with ValueMatcher

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

the class RowBasedGrouperHelper method getResultRowPredicate.

/**
 * Returns a predicate that filters result rows from a particular "subquery" based on the intervals and dim filters
 * from "query".
 *
 * @param query    outer query
 * @param subquery inner query
 */
private static Predicate<ResultRow> getResultRowPredicate(final GroupByQuery query, final GroupByQuery subquery) {
    final List<Interval> queryIntervals = query.getIntervals();
    final Filter filter = Filters.convertToCNFFromQueryContext(query, Filters.toFilter(query.getDimFilter()));
    final SettableSupplier<ResultRow> rowSupplier = new SettableSupplier<>();
    final ColumnSelectorFactory columnSelectorFactory = query.getVirtualColumns().wrap(RowBasedGrouperHelper.createResultRowBasedColumnSelectorFactory(subquery, rowSupplier, RowSignature.Finalization.UNKNOWN));
    final ValueMatcher filterMatcher = filter == null ? BooleanValueMatcher.of(true) : filter.makeMatcher(columnSelectorFactory);
    if (subquery.getUniversalTimestamp() != null && queryIntervals.stream().noneMatch(itvl -> itvl.contains(subquery.getUniversalTimestamp()))) {
        // By the way, if there's a universal timestamp that _does_ match the query intervals, we do nothing special here.
        return row -> false;
    }
    return row -> {
        if (subquery.getResultRowHasTimestamp()) {
            boolean inInterval = false;
            for (Interval queryInterval : queryIntervals) {
                if (queryInterval.contains(row.getLong(0))) {
                    inInterval = true;
                    break;
                }
            }
            if (!inInterval) {
                return false;
            }
        }
        rowSupplier.set(row);
        return filterMatcher.matches();
    };
}
Also used : ResultRow(org.apache.druid.query.groupby.ResultRow) SettableSupplier(org.apache.druid.common.guava.SettableSupplier) Arrays(java.util.Arrays) Comparators(org.apache.druid.java.util.common.guava.Comparators) IntArrayUtils(org.apache.druid.common.utils.IntArrayUtils) DimensionHandlerUtils(org.apache.druid.segment.DimensionHandlerUtils) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) AllGranularity(org.apache.druid.java.util.common.granularity.AllGranularity) IndexedInts(org.apache.druid.segment.data.IndexedInts) ByteBuffer(java.nio.ByteBuffer) Pair(org.apache.druid.java.util.common.Pair) DefaultLimitSpec(org.apache.druid.query.groupby.orderby.DefaultLimitSpec) BaseFloatColumnValueSelector(org.apache.druid.segment.BaseFloatColumnValueSelector) OrderByColumnSpec(org.apache.druid.query.groupby.orderby.OrderByColumnSpec) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) RowAdapter(org.apache.druid.segment.RowAdapter) ColumnSelectorStrategyFactory(org.apache.druid.query.dimension.ColumnSelectorStrategyFactory) JsonValue(com.fasterxml.jackson.annotation.JsonValue) GroupingAggregatorFactory(org.apache.druid.query.aggregation.GroupingAggregatorFactory) BufferComparator(org.apache.druid.query.groupby.epinephelinae.Grouper.BufferComparator) Object2IntOpenHashMap(it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap) IAE(org.apache.druid.java.util.common.IAE) ToLongFunction(java.util.function.ToLongFunction) Longs(com.google.common.primitives.Longs) RowBasedColumnSelectorFactory(org.apache.druid.segment.RowBasedColumnSelectorFactory) ResultRow(org.apache.druid.query.groupby.ResultRow) Predicate(java.util.function.Predicate) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) Set(java.util.Set) ISE(org.apache.druid.java.util.common.ISE) ValueType(org.apache.druid.segment.column.ValueType) Collectors(java.util.stream.Collectors) List(java.util.List) ColumnCapabilitiesImpl(org.apache.druid.segment.column.ColumnCapabilitiesImpl) BooleanValueMatcher(org.apache.druid.segment.filter.BooleanValueMatcher) DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) ColumnCapabilities(org.apache.druid.segment.column.ColumnCapabilities) BaseDoubleColumnValueSelector(org.apache.druid.segment.BaseDoubleColumnValueSelector) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Accumulator(org.apache.druid.java.util.common.guava.Accumulator) IntStream(java.util.stream.IntStream) ColumnSelectorPlus(org.apache.druid.query.ColumnSelectorPlus) ComparableList(org.apache.druid.segment.data.ComparableList) Supplier(com.google.common.base.Supplier) BaseQuery(org.apache.druid.query.BaseQuery) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Interval(org.joda.time.Interval) SettableSupplier(org.apache.druid.common.guava.SettableSupplier) ColumnSelectorStrategy(org.apache.druid.query.dimension.ColumnSelectorStrategy) StringComparators(org.apache.druid.query.ordering.StringComparators) ComparableStringArray(org.apache.druid.segment.data.ComparableStringArray) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) DimensionSelector(org.apache.druid.segment.DimensionSelector) Nullable(javax.annotation.Nullable) ValueMatcher(org.apache.druid.query.filter.ValueMatcher) ColumnInspector(org.apache.druid.segment.ColumnInspector) StringComparator(org.apache.druid.query.ordering.StringComparator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GroupByQueryConfig(org.apache.druid.query.groupby.GroupByQueryConfig) DateTime(org.joda.time.DateTime) Ints(com.google.common.primitives.Ints) BaseLongColumnValueSelector(org.apache.druid.segment.BaseLongColumnValueSelector) Object2IntMap(it.unimi.dsi.fastutil.objects.Object2IntMap) NullHandling(org.apache.druid.common.config.NullHandling) RowSignature(org.apache.druid.segment.column.RowSignature) Closeable(java.io.Closeable) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) ColumnType(org.apache.druid.segment.column.ColumnType) Preconditions(com.google.common.base.Preconditions) BitSet(java.util.BitSet) IntArrays(it.unimi.dsi.fastutil.ints.IntArrays) Comparator(java.util.Comparator) Filters(org.apache.druid.segment.filter.Filters) ReferenceCountingResourceHolder(org.apache.druid.collections.ReferenceCountingResourceHolder) Filter(org.apache.druid.query.filter.Filter) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) RowBasedColumnSelectorFactory(org.apache.druid.segment.RowBasedColumnSelectorFactory) BooleanValueMatcher(org.apache.druid.segment.filter.BooleanValueMatcher) ValueMatcher(org.apache.druid.query.filter.ValueMatcher) Filter(org.apache.druid.query.filter.Filter) Interval(org.joda.time.Interval)

Example 30 with ValueMatcher

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

the class ExpressionMultiValueDimensionSelector method makeValueMatcher.

@Override
public ValueMatcher makeValueMatcher(Predicate<String> predicate) {
    return new ValueMatcher() {

        @Override
        public boolean matches() {
            ExprEval evaluated = getEvaluated();
            if (evaluated.isArray()) {
                List<String> array = getArray(evaluated);
                return array.stream().anyMatch(x -> predicate.apply(x));
            }
            return predicate.apply(getValue(evaluated));
        }

        @Override
        public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
            inspector.visit("selector", baseSelector);
            inspector.visit("predicate", predicate);
        }
    };
}
Also used : ExprEval(org.apache.druid.math.expr.ExprEval) ValueMatcher(org.apache.druid.query.filter.ValueMatcher) RuntimeShapeInspector(org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector)

Aggregations

ValueMatcher (org.apache.druid.query.filter.ValueMatcher)59 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)33 Test (org.junit.Test)33 RuntimeShapeInspector (org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector)19 IndexedInts (org.apache.druid.segment.data.IndexedInts)11 BooleanValueMatcher (org.apache.druid.segment.filter.BooleanValueMatcher)9 VectorValueMatcher (org.apache.druid.query.filter.vector.VectorValueMatcher)7 BaseVectorValueMatcher (org.apache.druid.query.filter.vector.BaseVectorValueMatcher)6 ArrayList (java.util.ArrayList)5 BitSet (java.util.BitSet)5 Nullable (javax.annotation.Nullable)5 Filter (org.apache.druid.query.filter.Filter)5 DimensionSelector (org.apache.druid.segment.DimensionSelector)5 ArrayBasedIndexedInts (org.apache.druid.segment.data.ArrayBasedIndexedInts)5 Predicate (com.google.common.base.Predicate)4 IdLookup (org.apache.druid.segment.IdLookup)4 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)3 BooleanFilter (org.apache.druid.query.filter.BooleanFilter)3 ConstantDimensionSelector (org.apache.druid.segment.ConstantDimensionSelector)3 ConstantMultiValueDimensionSelector (org.apache.druid.segment.ConstantMultiValueDimensionSelector)3