Search in sources :

Example 11 with ValueMatcher

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

the class DimensionSelectorUtils method makeDictionaryEncodedValueMatcherGeneric.

private static ValueMatcher makeDictionaryEncodedValueMatcherGeneric(final DimensionSelector selector, final int valueId, final boolean matchNull) {
    if (valueId >= 0) {
        return new ValueMatcher() {

            @Override
            public boolean matches() {
                final IndexedInts row = selector.getRow();
                final int size = row.size();
                if (size == 0) {
                    // null should match empty rows in multi-value columns
                    return matchNull;
                } else {
                    for (int i = 0; i < size; ++i) {
                        if (row.get(i) == valueId) {
                            return true;
                        }
                    }
                    return false;
                }
            }
        };
    } else {
        if (matchNull) {
            return new ValueMatcher() {

                @Override
                public boolean matches() {
                    final IndexedInts row = selector.getRow();
                    final int size = row.size();
                    return size == 0;
                }
            };
        } else {
            return BooleanValueMatcher.of(false);
        }
    }
}
Also used : ValueMatcher(io.druid.query.filter.ValueMatcher) BooleanValueMatcher(io.druid.segment.filter.BooleanValueMatcher) IndexedInts(io.druid.segment.data.IndexedInts)

Example 12 with ValueMatcher

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

the class DimensionSelectorUtils method makeDictionaryEncodedValueMatcherGeneric.

private static ValueMatcher makeDictionaryEncodedValueMatcherGeneric(final DimensionSelector selector, Predicate<String> predicate) {
    final BitSet predicateMatchingValueIds = makePredicateMatchingSet(selector, predicate);
    final boolean matchNull = predicate.apply(null);
    return new ValueMatcher() {

        @Override
        public boolean matches() {
            final IndexedInts row = selector.getRow();
            final int size = row.size();
            if (size == 0) {
                // null should match empty rows in multi-value columns
                return matchNull;
            } else {
                for (int i = 0; i < size; ++i) {
                    if (predicateMatchingValueIds.get(row.get(i))) {
                        return true;
                    }
                }
                return false;
            }
        }
    };
}
Also used : ValueMatcher(io.druid.query.filter.ValueMatcher) BooleanValueMatcher(io.druid.segment.filter.BooleanValueMatcher) IndexedInts(io.druid.segment.data.IndexedInts) BitSet(java.util.BitSet)

Example 13 with ValueMatcher

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

the class DimensionSelectorUtils method makeNonDictionaryEncodedValueMatcherGeneric.

private static ValueMatcher makeNonDictionaryEncodedValueMatcherGeneric(final DimensionSelector selector, final String value) {
    return new ValueMatcher() {

        @Override
        public boolean matches() {
            final IndexedInts row = selector.getRow();
            final int size = row.size();
            if (size == 0) {
                // null should match empty rows in multi-value columns
                return value == null;
            } else {
                for (int i = 0; i < size; ++i) {
                    if (Objects.equals(selector.lookupName(row.get(i)), value)) {
                        return true;
                    }
                }
                return false;
            }
        }
    };
}
Also used : ValueMatcher(io.druid.query.filter.ValueMatcher) BooleanValueMatcher(io.druid.segment.filter.BooleanValueMatcher) IndexedInts(io.druid.segment.data.IndexedInts)

Example 14 with ValueMatcher

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

the class RowBasedColumnSelectorFactory method makeDimensionSelectorUndecorated.

private DimensionSelector makeDimensionSelectorUndecorated(DimensionSpec dimensionSpec) {
    final String dimension = dimensionSpec.getDimension();
    final ExtractionFn extractionFn = dimensionSpec.getExtractionFn();
    if (Column.TIME_COLUMN_NAME.equals(dimensionSpec.getDimension())) {
        if (extractionFn == null) {
            throw new UnsupportedOperationException("time dimension must provide an extraction function");
        }
        return new DimensionSelector() {

            @Override
            public IndexedInts getRow() {
                return ZeroIndexedInts.instance();
            }

            @Override
            public ValueMatcher makeValueMatcher(final String value) {
                return new ValueMatcher() {

                    @Override
                    public boolean matches() {
                        String rowValue = extractionFn.apply(row.get().getTimestampFromEpoch());
                        return Objects.equals(rowValue, value);
                    }
                };
            }

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

                    @Override
                    public boolean matches() {
                        String rowValue = extractionFn.apply(row.get().getTimestampFromEpoch());
                        return predicate.apply(rowValue);
                    }
                };
            }

            @Override
            public int getValueCardinality() {
                return DimensionSelector.CARDINALITY_UNKNOWN;
            }

            @Override
            public String lookupName(int id) {
                return extractionFn.apply(row.get().getTimestampFromEpoch());
            }

            @Override
            public boolean nameLookupPossibleInAdvance() {
                return false;
            }

            @Nullable
            @Override
            public IdLookup idLookup() {
                return null;
            }

            @Override
            public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
                inspector.visit("row", row);
                inspector.visit("extractionFn", extractionFn);
            }
        };
    } else {
        return new DimensionSelector() {

            @Override
            public IndexedInts getRow() {
                final List<String> dimensionValues = row.get().getDimension(dimension);
                return RangeIndexedInts.create(dimensionValues != null ? dimensionValues.size() : 0);
            }

            @Override
            public ValueMatcher makeValueMatcher(final String value) {
                if (extractionFn == null) {
                    return new ValueMatcher() {

                        @Override
                        public boolean matches() {
                            final List<String> dimensionValues = row.get().getDimension(dimension);
                            if (dimensionValues == null || dimensionValues.isEmpty()) {
                                return value == null;
                            }
                            for (String dimensionValue : dimensionValues) {
                                if (Objects.equals(Strings.emptyToNull(dimensionValue), value)) {
                                    return true;
                                }
                            }
                            return false;
                        }
                    };
                } else {
                    return new ValueMatcher() {

                        @Override
                        public boolean matches() {
                            final List<String> dimensionValues = row.get().getDimension(dimension);
                            if (dimensionValues == null || dimensionValues.isEmpty()) {
                                return value == null;
                            }
                            for (String dimensionValue : dimensionValues) {
                                if (Objects.equals(extractionFn.apply(Strings.emptyToNull(dimensionValue)), value)) {
                                    return true;
                                }
                            }
                            return false;
                        }
                    };
                }
            }

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

                        @Override
                        public boolean matches() {
                            final List<String> dimensionValues = row.get().getDimension(dimension);
                            if (dimensionValues == null || dimensionValues.isEmpty()) {
                                return matchNull;
                            }
                            for (String dimensionValue : dimensionValues) {
                                if (predicate.apply(Strings.emptyToNull(dimensionValue))) {
                                    return true;
                                }
                            }
                            return false;
                        }
                    };
                } else {
                    return new ValueMatcher() {

                        @Override
                        public boolean matches() {
                            final List<String> dimensionValues = row.get().getDimension(dimension);
                            if (dimensionValues == null || dimensionValues.isEmpty()) {
                                return matchNull;
                            }
                            for (String dimensionValue : dimensionValues) {
                                if (predicate.apply(extractionFn.apply(Strings.emptyToNull(dimensionValue)))) {
                                    return true;
                                }
                            }
                            return false;
                        }
                    };
                }
            }

            @Override
            public int getValueCardinality() {
                return DimensionSelector.CARDINALITY_UNKNOWN;
            }

            @Override
            public String lookupName(int id) {
                final String value = Strings.emptyToNull(row.get().getDimension(dimension).get(id));
                return extractionFn == null ? value : extractionFn.apply(value);
            }

            @Override
            public boolean nameLookupPossibleInAdvance() {
                return false;
            }

            @Nullable
            @Override
            public IdLookup idLookup() {
                return null;
            }

            @Override
            public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
                inspector.visit("row", row);
                inspector.visit("extractionFn", extractionFn);
            }
        };
    }
}
Also used : ExtractionFn(io.druid.query.extraction.ExtractionFn) DimensionSelector(io.druid.segment.DimensionSelector) ValueMatcher(io.druid.query.filter.ValueMatcher) RuntimeShapeInspector(io.druid.query.monomorphicprocessing.RuntimeShapeInspector) Predicate(com.google.common.base.Predicate)

Example 15 with ValueMatcher

use of io.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 = forwardMapping.get(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;
                }
            };
        } else {
            return BooleanValueMatcher.of(false);
        }
    } else {
        // Employ precomputed BitSet optimization
        return makeValueMatcher(Predicates.equalTo(value));
    }
}
Also used : IdLookup(io.druid.segment.IdLookup) BooleanValueMatcher(io.druid.segment.filter.BooleanValueMatcher) ValueMatcher(io.druid.query.filter.ValueMatcher) ArrayBasedIndexedInts(io.druid.segment.data.ArrayBasedIndexedInts) IndexedInts(io.druid.segment.data.IndexedInts)

Aggregations

ValueMatcher (io.druid.query.filter.ValueMatcher)18 IndexedInts (io.druid.segment.data.IndexedInts)9 BooleanValueMatcher (io.druid.segment.filter.BooleanValueMatcher)9 DimensionSelector (io.druid.segment.DimensionSelector)5 ArrayBasedIndexedInts (io.druid.segment.data.ArrayBasedIndexedInts)5 ExtractionFn (io.druid.query.extraction.ExtractionFn)4 RuntimeShapeInspector (io.druid.query.monomorphicprocessing.RuntimeShapeInspector)4 Filter (io.druid.query.filter.Filter)3 BitSet (java.util.BitSet)3 Predicate (com.google.common.base.Predicate)2 ImmutableBitmap (io.druid.collections.bitmap.ImmutableBitmap)2 SettableSupplier (io.druid.common.guava.SettableSupplier)2 DimensionSpec (io.druid.query.dimension.DimensionSpec)2 BooleanFilter (io.druid.query.filter.BooleanFilter)2 IdLookup (io.druid.segment.IdLookup)2 ArrayList (java.util.ArrayList)2 Nullable (javax.annotation.Nullable)2 DateTime (org.joda.time.DateTime)2 Interval (org.joda.time.Interval)2 Test (org.junit.Test)2