Search in sources :

Example 16 with IndexedInts

use of org.apache.druid.segment.data.IndexedInts 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 17 with IndexedInts

use of org.apache.druid.segment.data.IndexedInts in project druid by druid-io.

the class ForwardingFilteredDimensionSelector method getRow.

@Override
public IndexedInts getRow() {
    IndexedInts baseRow = selector.getRow();
    int baseRowSize = baseRow.size();
    row.ensureSize(baseRowSize);
    int resultSize = 0;
    for (int i = 0; i < baseRowSize; i++) {
        int forwardedValue = idMapping.getForwardedId(baseRow.get(i));
        if (forwardedValue >= 0) {
            row.setValue(resultSize, forwardedValue);
            resultSize++;
        }
    }
    row.setSize(resultSize);
    return row;
}
Also used : IndexedInts(org.apache.druid.segment.data.IndexedInts) ArrayBasedIndexedInts(org.apache.druid.segment.data.ArrayBasedIndexedInts)

Example 18 with IndexedInts

use of org.apache.druid.segment.data.IndexedInts 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 19 with IndexedInts

use of org.apache.druid.segment.data.IndexedInts in project druid by druid-io.

the class PredicateFilteredDimensionSelector method getRow.

@Override
public IndexedInts getRow() {
    IndexedInts baseRow = selector.getRow();
    int baseRowSize = baseRow.size();
    row.ensureSize(baseRowSize);
    int resultSize = 0;
    for (int i = 0; i < baseRowSize; i++) {
        int id = baseRow.get(i);
        if (predicate.apply(selector.lookupName(id))) {
            row.setValue(resultSize, id);
            resultSize++;
        }
    }
    row.setSize(resultSize);
    return row;
}
Also used : ArrayBasedIndexedInts(org.apache.druid.segment.data.ArrayBasedIndexedInts) IndexedInts(org.apache.druid.segment.data.IndexedInts)

Example 20 with IndexedInts

use of org.apache.druid.segment.data.IndexedInts in project druid by druid-io.

the class MultiValueStringVectorValueMatcher method makeMatcher.

private VectorValueMatcher makeMatcher(final Predicate<String> predicate) {
    final boolean matchNull = predicate.apply(null);
    if (selector.getValueCardinality() > 0) {
        final BitSet checkedIds = new BitSet(selector.getValueCardinality());
        final BitSet matchingIds = new BitSet(selector.getValueCardinality());
        // Lazy matcher; only check an id if matches() is called.
        return new BaseVectorValueMatcher(selector) {

            private final VectorMatch match = VectorMatch.wrap(new int[selector.getMaxVectorSize()]);

            @Override
            public ReadableVectorMatch match(final ReadableVectorMatch mask) {
                final IndexedInts[] vector = selector.getRowVector();
                final int[] selection = match.getSelection();
                int numRows = 0;
                for (int i = 0; i < mask.getSelectionSize(); i++) {
                    final int rowNum = mask.getSelection()[i];
                    final IndexedInts ints = vector[rowNum];
                    final int n = ints.size();
                    if (n == 0) {
                        // null should match empty rows in multi-value columns
                        if (matchNull) {
                            selection[numRows++] = rowNum;
                        }
                    } else {
                        for (int j = 0; j < n; j++) {
                            final int id = ints.get(j);
                            final boolean matches;
                            if (checkedIds.get(id)) {
                                matches = matchingIds.get(id);
                            } else {
                                matches = predicate.apply(selector.lookupName(id));
                                checkedIds.set(id);
                                if (matches) {
                                    matchingIds.set(id);
                                }
                            }
                            if (matches) {
                                selection[numRows++] = rowNum;
                                break;
                            }
                        }
                    }
                }
                match.setSelectionSize(numRows);
                assert match.isValid(mask);
                return match;
            }
        };
    } else {
        // Evaluate "lookupName" and "predicate" on every row.
        return new BaseVectorValueMatcher(selector) {

            final VectorMatch match = VectorMatch.wrap(new int[selector.getMaxVectorSize()]);

            @Override
            public ReadableVectorMatch match(final ReadableVectorMatch mask) {
                final IndexedInts[] vector = selector.getRowVector();
                final int[] selection = match.getSelection();
                int numRows = 0;
                for (int i = 0; i < mask.getSelectionSize(); i++) {
                    final int rowNum = mask.getSelection()[i];
                    final IndexedInts ints = vector[rowNum];
                    final int n = ints.size();
                    if (n == 0) {
                        // null should match empty rows in multi-value columns
                        if (matchNull) {
                            selection[numRows++] = rowNum;
                        }
                    } else {
                        for (int j = 0; j < n; j++) {
                            final int id = ints.get(j);
                            if (predicate.apply(selector.lookupName(id))) {
                                selection[numRows++] = rowNum;
                                break;
                            }
                        }
                    }
                }
                match.setSelectionSize(numRows);
                assert match.isValid(mask);
                return match;
            }
        };
    }
}
Also used : IndexedInts(org.apache.druid.segment.data.IndexedInts) BitSet(java.util.BitSet)

Aggregations

IndexedInts (org.apache.druid.segment.data.IndexedInts)63 DimensionSelector (org.apache.druid.segment.DimensionSelector)22 ValueMatcher (org.apache.druid.query.filter.ValueMatcher)14 Test (org.junit.Test)13 RuntimeShapeInspector (org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector)12 ArrayBasedIndexedInts (org.apache.druid.segment.data.ArrayBasedIndexedInts)12 Cursor (org.apache.druid.segment.Cursor)10 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)8 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)7 Predicate (com.google.common.base.Predicate)6 ByteBuffer (java.nio.ByteBuffer)6 Nullable (javax.annotation.Nullable)6 ColumnSelectorFactory (org.apache.druid.segment.ColumnSelectorFactory)6 BooleanValueMatcher (org.apache.druid.segment.filter.BooleanValueMatcher)6 List (java.util.List)5 Predicates (com.google.common.base.Predicates)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Arrays (java.util.Arrays)4 NullHandling (org.apache.druid.common.config.NullHandling)4 MapBasedInputRow (org.apache.druid.data.input.MapBasedInputRow)4