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;
}
};
}
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));
}
}
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);
}
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));
}
}
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));
}
}
Aggregations