Search in sources :

Example 56 with ValueMatcher

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

the class ExpressionVirtualColumnTest method testDimensionSelectorWithExtraction.

@Test
public void testDimensionSelectorWithExtraction() {
    final DimensionSelector selector = X_PLUS_Y.makeDimensionSelector(new ExtractionDimensionSpec("expr", "x", new BucketExtractionFn(1.0, 0.0)), COLUMN_SELECTOR_FACTORY);
    final ValueMatcher nullMatcher = selector.makeValueMatcher((String) null);
    final ValueMatcher fiveMatcher = selector.makeValueMatcher("5");
    final ValueMatcher nonNullMatcher = selector.makeValueMatcher(Predicates.notNull());
    CURRENT_ROW.set(ROW0);
    Assert.assertEquals(true, nullMatcher.matches());
    Assert.assertEquals(false, fiveMatcher.matches());
    Assert.assertEquals(false, nonNullMatcher.matches());
    Assert.assertEquals(null, selector.lookupName(selector.getRow().get(0)));
    CURRENT_ROW.set(ROW1);
    if (NullHandling.replaceWithDefault()) {
        Assert.assertEquals(false, nullMatcher.matches());
        Assert.assertEquals(false, fiveMatcher.matches());
        Assert.assertEquals(true, nonNullMatcher.matches());
        Assert.assertEquals("4", selector.lookupName(selector.getRow().get(0)));
    } else {
        // y is null in row1
        Assert.assertEquals(true, nullMatcher.matches());
        Assert.assertEquals(false, fiveMatcher.matches());
        Assert.assertEquals(false, nonNullMatcher.matches());
        Assert.assertEquals(null, selector.lookupName(selector.getRow().get(0)));
    }
    CURRENT_ROW.set(ROW2);
    Assert.assertEquals(false, nullMatcher.matches());
    Assert.assertEquals(true, fiveMatcher.matches());
    Assert.assertEquals(true, nonNullMatcher.matches());
    Assert.assertEquals("5.1", selector.lookupName(selector.getRow().get(0)));
    CURRENT_ROW.set(ROW3);
    Assert.assertEquals(false, nullMatcher.matches());
    Assert.assertEquals(true, fiveMatcher.matches());
    Assert.assertEquals(true, nonNullMatcher.matches());
    Assert.assertEquals("5", selector.lookupName(selector.getRow().get(0)));
}
Also used : ConstantMultiValueDimensionSelector(org.apache.druid.segment.ConstantMultiValueDimensionSelector) DimensionSelector(org.apache.druid.segment.DimensionSelector) ConstantDimensionSelector(org.apache.druid.segment.ConstantDimensionSelector) BucketExtractionFn(org.apache.druid.query.extraction.BucketExtractionFn) ValueMatcher(org.apache.druid.query.filter.ValueMatcher) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 57 with ValueMatcher

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

the class AndFilter method makeMatcher.

private static ValueMatcher makeMatcher(final ValueMatcher[] baseMatchers) {
    Preconditions.checkState(baseMatchers.length > 0);
    if (baseMatchers.length == 1) {
        return baseMatchers[0];
    }
    return new ValueMatcher() {

        @Override
        public boolean matches() {
            for (ValueMatcher matcher : baseMatchers) {
                if (!matcher.matches()) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
            inspector.visit("firstBaseMatcher", baseMatchers[0]);
            inspector.visit("secondBaseMatcher", baseMatchers[1]);
        // Don't inspect the 3rd and all consequent baseMatchers, cut runtime shape combinations at this point.
        // Anyway if the filter is so complex, Hotspot won't inline all calls because of the inline limit.
        }
    };
}
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) RuntimeShapeInspector(org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector)

Example 58 with ValueMatcher

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

the class BaseFilterTest method selectColumnValuesMatchingFilterUsingRowBasedColumnSelectorFactory.

private List<String> selectColumnValuesMatchingFilterUsingRowBasedColumnSelectorFactory(final DimFilter filter, final String selectColumn) {
    // Generate rowSignature
    final RowSignature.Builder rowSignatureBuilder = RowSignature.builder();
    for (String columnName : Iterables.concat(adapter.getAvailableDimensions(), adapter.getAvailableMetrics())) {
        rowSignatureBuilder.add(columnName, adapter.getColumnCapabilities(columnName).toColumnType());
    }
    // Perform test
    final SettableSupplier<InputRow> rowSupplier = new SettableSupplier<>();
    final ValueMatcher matcher = makeFilter(filter).makeMatcher(VIRTUAL_COLUMNS.wrap(RowBasedColumnSelectorFactory.create(RowAdapters.standardRow(), rowSupplier::get, rowSignatureBuilder.build(), false)));
    final List<String> values = new ArrayList<>();
    for (InputRow row : rows) {
        rowSupplier.set(row);
        if (matcher.matches()) {
            values.add((String) row.getRaw(selectColumn));
        }
    }
    return values;
}
Also used : SettableSupplier(org.apache.druid.common.guava.SettableSupplier) VectorValueMatcher(org.apache.druid.query.filter.vector.VectorValueMatcher) ValueMatcher(org.apache.druid.query.filter.ValueMatcher) InputRow(org.apache.druid.data.input.InputRow) ArrayList(java.util.ArrayList) RowSignature(org.apache.druid.segment.column.RowSignature)

Example 59 with ValueMatcher

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

the class ExpressionTestHelper method testFilter.

void testFilter(final SqlOperator op, final List<? extends RexNode> exprs, final List<VirtualColumn> expectedVirtualColumns, final DimFilter expectedFilter, final boolean expectedResult) {
    final RexNode rexNode = rexBuilder.makeCall(op, exprs);
    final VirtualColumnRegistry virtualColumnRegistry = VirtualColumnRegistry.create(rowSignature, TestExprMacroTable.INSTANCE);
    final DimFilter filter = Expressions.toFilter(PLANNER_CONTEXT, rowSignature, virtualColumnRegistry, rexNode);
    Assert.assertEquals("Filter for: " + rexNode, expectedFilter, filter);
    final List<VirtualColumn> virtualColumns = filter.getRequiredColumns().stream().map(virtualColumnRegistry::getVirtualColumn).filter(Objects::nonNull).sorted(Comparator.comparing(VirtualColumn::getOutputName)).collect(Collectors.toList());
    Assert.assertEquals("Virtual columns for: " + rexNode, expectedVirtualColumns.stream().sorted(Comparator.comparing(VirtualColumn::getOutputName)).collect(Collectors.toList()), virtualColumns);
    final ValueMatcher matcher = expectedFilter.toFilter().makeMatcher(new VirtualizedColumnSelectorFactory(RowBasedColumnSelectorFactory.create(RowAdapters.standardRow(), () -> new MapBasedRow(0L, bindings), rowSignature, false), VirtualColumns.create(virtualColumns)));
    Assert.assertEquals("Result for: " + rexNode, expectedResult, matcher.matches());
}
Also used : MapBasedRow(org.apache.druid.data.input.MapBasedRow) VirtualColumnRegistry(org.apache.druid.sql.calcite.rel.VirtualColumnRegistry) ValueMatcher(org.apache.druid.query.filter.ValueMatcher) Objects(java.util.Objects) VirtualColumn(org.apache.druid.segment.VirtualColumn) DimFilter(org.apache.druid.query.filter.DimFilter) VirtualizedColumnSelectorFactory(org.apache.druid.segment.virtual.VirtualizedColumnSelectorFactory) RexNode(org.apache.calcite.rex.RexNode)

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