Search in sources :

Example 6 with IdLookup

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

the class FilteredAggregatorTest method makeColumnSelector.

private ColumnSelectorFactory makeColumnSelector(final TestFloatColumnSelector selector) {
    return new ColumnSelectorFactory() {

        @Override
        public DimensionSelector makeDimensionSelector(DimensionSpec dimensionSpec) {
            final String dimensionName = dimensionSpec.getDimension();
            if ("dim".equals(dimensionName)) {
                return dimensionSpec.decorate(new AbstractDimensionSelector() {

                    @Override
                    public IndexedInts getRow() {
                        SingleIndexedInt row = new SingleIndexedInt();
                        if (selector.getIndex() % 3 == 2) {
                            row.setValue(1);
                        } else {
                            row.setValue(0);
                        }
                        return row;
                    }

                    @Override
                    public ValueMatcher makeValueMatcher(String value) {
                        return DimensionSelectorUtils.makeValueMatcherGeneric(this, value);
                    }

                    @Override
                    public ValueMatcher makeValueMatcher(Predicate<String> predicate) {
                        return DimensionSelectorUtils.makeValueMatcherGeneric(this, predicate);
                    }

                    @Override
                    public int getValueCardinality() {
                        return 2;
                    }

                    @Override
                    public String lookupName(int id) {
                        switch(id) {
                            case 0:
                                return "a";
                            case 1:
                                return "b";
                            default:
                                throw new IllegalArgumentException();
                        }
                    }

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

                    @Nullable
                    @Override
                    public IdLookup idLookup() {
                        return new IdLookup() {

                            @Override
                            public int lookupId(String name) {
                                switch(name) {
                                    case "a":
                                        return 0;
                                    case "b":
                                        return 1;
                                    default:
                                        throw new IllegalArgumentException();
                                }
                            }
                        };
                    }

                    @Override
                    public Class classOfObject() {
                        return Object.class;
                    }

                    @Override
                    public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
                    // Don't care about runtime shape in tests
                    }
                });
            } else {
                throw new UnsupportedOperationException();
            }
        }

        @Override
        public ColumnValueSelector<?> makeColumnValueSelector(String columnName) {
            if ("value".equals(columnName)) {
                return selector;
            } else {
                throw new UnsupportedOperationException();
            }
        }

        @Override
        public ColumnCapabilities getColumnCapabilities(String columnName) {
            ColumnCapabilitiesImpl caps;
            if ("value".equals(columnName)) {
                caps = new ColumnCapabilitiesImpl();
                caps.setType(ColumnType.FLOAT);
                caps.setDictionaryEncoded(false);
                caps.setHasBitmapIndexes(false);
            } else {
                caps = new ColumnCapabilitiesImpl();
                caps.setType(ColumnType.STRING);
                caps.setDictionaryEncoded(true);
                caps.setHasBitmapIndexes(true);
            }
            return caps;
        }
    };
}
Also used : DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) ValueMatcher(org.apache.druid.query.filter.ValueMatcher) SingleIndexedInt(org.apache.druid.segment.data.SingleIndexedInt) RuntimeShapeInspector(org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector) IdLookup(org.apache.druid.segment.IdLookup) AbstractDimensionSelector(org.apache.druid.segment.AbstractDimensionSelector) IndexedInts(org.apache.druid.segment.data.IndexedInts) Nullable(javax.annotation.Nullable) ColumnCapabilitiesImpl(org.apache.druid.segment.column.ColumnCapabilitiesImpl)

Example 7 with IdLookup

use of org.apache.druid.segment.IdLookup 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 8 with IdLookup

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

the class VectorValueMatcherColumnProcessorFactoryTest method testMultiValueString.

@Test
public void testMultiValueString() {
    IdLookup lookup = EasyMock.createMock(IdLookup.class);
    MultiValueDimensionVectorSelector selector = EasyMock.createMock(MultiValueDimensionVectorSelector.class);
    EasyMock.expect(selector.getCurrentVectorSize()).andReturn(CURRENT_SIZE).anyTimes();
    EasyMock.expect(selector.getMaxVectorSize()).andReturn(VECTOR_SIZE).anyTimes();
    EasyMock.expect(selector.getValueCardinality()).andReturn(11).anyTimes();
    EasyMock.expect(selector.nameLookupPossibleInAdvance()).andReturn(false).anyTimes();
    EasyMock.expect(selector.idLookup()).andReturn(lookup).anyTimes();
    EasyMock.expect(lookup.lookupId("any value")).andReturn(-1).anyTimes();
    EasyMock.expect(lookup.lookupId(null)).andReturn(0).anyTimes();
    EasyMock.replay(selector, lookup);
    VectorValueMatcherFactory matcherFactory = VectorValueMatcherColumnProcessorFactory.instance().makeMultiValueDimensionProcessor(new ColumnCapabilitiesImpl().setType(ColumnType.STRING).setHasMultipleValues(false).setHasBitmapIndexes(true).setDictionaryValuesUnique(true).setDictionaryValuesSorted(true).setDictionaryEncoded(true), selector);
    Assert.assertTrue(matcherFactory instanceof MultiValueStringVectorValueMatcher);
    VectorValueMatcher valueNotExistMatcher = matcherFactory.makeMatcher("any value");
    Assert.assertTrue(valueNotExistMatcher instanceof BooleanVectorValueMatcher);
    Assert.assertEquals(VECTOR_SIZE, valueNotExistMatcher.getMaxVectorSize());
    Assert.assertEquals(CURRENT_SIZE, valueNotExistMatcher.getCurrentVectorSize());
    VectorValueMatcher valueExistMatcher = matcherFactory.makeMatcher((String) null);
    Assert.assertFalse(valueExistMatcher instanceof BooleanVectorValueMatcher);
    Assert.assertEquals(VECTOR_SIZE, valueExistMatcher.getMaxVectorSize());
    Assert.assertEquals(CURRENT_SIZE, valueExistMatcher.getCurrentVectorSize());
    EasyMock.verify(selector, lookup);
}
Also used : IdLookup(org.apache.druid.segment.IdLookup) MultiValueDimensionVectorSelector(org.apache.druid.segment.vector.MultiValueDimensionVectorSelector) ColumnCapabilitiesImpl(org.apache.druid.segment.column.ColumnCapabilitiesImpl) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 9 with IdLookup

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

the class SingleValueStringVectorValueMatcher method makeMatcher.

@Override
public VectorValueMatcher makeMatcher(@Nullable final String value) {
    final String etnValue = NullHandling.emptyToNullIfNeeded(value);
    final VectorValueMatcher booleanMatcher = toBooleanMatcherIfPossible(selector, s -> Objects.equals(s, etnValue));
    if (booleanMatcher != null) {
        return booleanMatcher;
    }
    final IdLookup idLookup = selector.idLookup();
    final int id;
    if (idLookup != null) {
        // Optimization when names can be looked up to IDs ahead of time.
        id = idLookup.lookupId(etnValue);
        if (id < 0) {
            // Value doesn't exist in this column.
            return BooleanVectorValueMatcher.of(selector, false);
        }
        // Check for "id".
        return new BaseVectorValueMatcher(selector) {

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

            @Override
            public ReadableVectorMatch match(final ReadableVectorMatch mask) {
                final int[] 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];
                    if (vector[rowNum] == id) {
                        selection[numRows++] = rowNum;
                    }
                }
                match.setSelectionSize(numRows);
                assert match.isValid(mask);
                return match;
            }
        };
    } else {
        return makeMatcher(s -> Objects.equals(s, etnValue));
    }
}
Also used : IdLookup(org.apache.druid.segment.IdLookup)

Example 10 with IdLookup

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

the class MultiValueStringVectorValueMatcher method makeMatcher.

@Override
public VectorValueMatcher makeMatcher(@Nullable final String value) {
    final String etnValue = NullHandling.emptyToNullIfNeeded(NullHandling.emptyToNullIfNeeded(value));
    final IdLookup idLookup = selector.idLookup();
    final int id;
    if (idLookup != null) {
        // Optimization when names can be looked up to IDs ahead of time.
        id = idLookup.lookupId(etnValue);
        if (id < 0) {
            // Value doesn't exist in this column.
            return BooleanVectorValueMatcher.of(selector, false);
        }
        // Check for "id".
        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 (etnValue == null) {
                            selection[numRows++] = rowNum;
                        }
                    } else {
                        for (int j = 0; j < n; j++) {
                            if (ints.get(j) == id) {
                                selection[numRows++] = rowNum;
                                break;
                            }
                        }
                    }
                }
                match.setSelectionSize(numRows);
                assert match.isValid(mask);
                return match;
            }
        };
    } else {
        return makeMatcher(s -> Objects.equals(s, etnValue));
    }
}
Also used : IdLookup(org.apache.druid.segment.IdLookup) IndexedInts(org.apache.druid.segment.data.IndexedInts)

Aggregations

IdLookup (org.apache.druid.segment.IdLookup)11 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)6 Test (org.junit.Test)6 Nullable (javax.annotation.Nullable)5 ColumnCapabilitiesImpl (org.apache.druid.segment.column.ColumnCapabilitiesImpl)5 IndexedInts (org.apache.druid.segment.data.IndexedInts)5 ValueMatcher (org.apache.druid.query.filter.ValueMatcher)4 RuntimeShapeInspector (org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector)4 SingleValueDimensionVectorSelector (org.apache.druid.segment.vector.SingleValueDimensionVectorSelector)3 Predicate (com.google.common.base.Predicate)2 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)2 AbstractDimensionSelector (org.apache.druid.segment.AbstractDimensionSelector)2 ColumnSelectorFactory (org.apache.druid.segment.ColumnSelectorFactory)2 SingleIndexedInt (org.apache.druid.segment.data.SingleIndexedInt)2 BooleanValueMatcher (org.apache.druid.segment.filter.BooleanValueMatcher)2 MultiValueDimensionVectorSelector (org.apache.druid.segment.vector.MultiValueDimensionVectorSelector)2 Predicates (com.google.common.base.Predicates)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Arrays (java.util.Arrays)1