Search in sources :

Example 21 with DimensionSelector

use of io.druid.segment.DimensionSelector in project druid by druid-io.

the class ListFilteredDimensionSpecTest method testDecoratorWithWhitelist.

@Test
public void testDecoratorWithWhitelist() {
    ListFilteredDimensionSpec spec = new ListFilteredDimensionSpec(new DefaultDimensionSpec("foo", "bar"), ImmutableSet.of("c", "g"), true);
    DimensionSelector selector = spec.decorate(TestDimensionSelector.instance);
    Assert.assertEquals(2, selector.getValueCardinality());
    IndexedInts row = selector.getRow();
    Assert.assertEquals(2, row.size());
    Assert.assertEquals(0, row.get(0));
    Assert.assertEquals(1, row.get(1));
    Assert.assertEquals("c", selector.lookupName(0));
    Assert.assertEquals("g", selector.lookupName(1));
    Assert.assertEquals(0, selector.idLookup().lookupId("c"));
    Assert.assertEquals(1, selector.idLookup().lookupId("g"));
}
Also used : DimensionSelector(io.druid.segment.DimensionSelector) IndexedInts(io.druid.segment.data.IndexedInts) Test(org.junit.Test)

Example 22 with DimensionSelector

use of io.druid.segment.DimensionSelector 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 23 with DimensionSelector

use of io.druid.segment.DimensionSelector in project druid by druid-io.

the class StringGroupByColumnSelectorStrategy method initColumnValues.

@Override
public void initColumnValues(ColumnValueSelector selector, int columnIndex, Object[] valuess) {
    DimensionSelector dimSelector = (DimensionSelector) selector;
    IndexedInts row = dimSelector.getRow();
    valuess[columnIndex] = row;
}
Also used : DimensionSelector(io.druid.segment.DimensionSelector) IndexedInts(io.druid.segment.data.IndexedInts)

Example 24 with DimensionSelector

use of io.druid.segment.DimensionSelector in project druid by druid-io.

the class ExpressionVirtualColumnTest method testDimensionSelector.

@Test
public void testDimensionSelector() {
    final DimensionSelector selector = XPLUSY.makeDimensionSelector(new DefaultDimensionSpec("expr", "x"), COLUMN_SELECTOR_FACTORY);
    final ValueMatcher nullMatcher = selector.makeValueMatcher((String) null);
    final ValueMatcher fiveMatcher = selector.makeValueMatcher("5");
    final ValueMatcher nonNullMatcher = selector.makeValueMatcher(Predicates.<String>notNull());
    COLUMN_SELECTOR_FACTORY.setRow(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)));
    COLUMN_SELECTOR_FACTORY.setRow(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)));
    COLUMN_SELECTOR_FACTORY.setRow(ROW2);
    Assert.assertEquals(false, nullMatcher.matches());
    Assert.assertEquals(false, fiveMatcher.matches());
    Assert.assertEquals(true, nonNullMatcher.matches());
    Assert.assertEquals("5.1", selector.lookupName(selector.getRow().get(0)));
}
Also used : DimensionSelector(io.druid.segment.DimensionSelector) ValueMatcher(io.druid.query.filter.ValueMatcher) DefaultDimensionSpec(io.druid.query.dimension.DefaultDimensionSpec) Test(org.junit.Test)

Example 25 with DimensionSelector

use of io.druid.segment.DimensionSelector in project druid by druid-io.

the class ExpressionVirtualColumnTest method testDimensionSelectorWithExtraction.

@Test
public void testDimensionSelectorWithExtraction() {
    final DimensionSelector selector = XPLUSY.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.<String>notNull());
    COLUMN_SELECTOR_FACTORY.setRow(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)));
    COLUMN_SELECTOR_FACTORY.setRow(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)));
    COLUMN_SELECTOR_FACTORY.setRow(ROW2);
    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 : DimensionSelector(io.druid.segment.DimensionSelector) BucketExtractionFn(io.druid.query.extraction.BucketExtractionFn) ValueMatcher(io.druid.query.filter.ValueMatcher) ExtractionDimensionSpec(io.druid.query.dimension.ExtractionDimensionSpec) Test(org.junit.Test)

Aggregations

DimensionSelector (io.druid.segment.DimensionSelector)26 IndexedInts (io.druid.segment.data.IndexedInts)14 DefaultDimensionSpec (io.druid.query.dimension.DefaultDimensionSpec)12 Test (org.junit.Test)11 Cursor (io.druid.segment.Cursor)9 ValueMatcher (io.druid.query.filter.ValueMatcher)5 ObjectColumnSelector (io.druid.segment.ObjectColumnSelector)5 FloatColumnSelector (io.druid.segment.FloatColumnSelector)4 LongColumnSelector (io.druid.segment.LongColumnSelector)4 Interval (org.joda.time.Interval)4 DimensionSpec (io.druid.query.dimension.DimensionSpec)3 ExtractionFn (io.druid.query.extraction.ExtractionFn)3 RuntimeShapeInspector (io.druid.query.monomorphicprocessing.RuntimeShapeInspector)3 TestFloatColumnSelector (io.druid.segment.TestFloatColumnSelector)3 TestLongColumnSelector (io.druid.segment.TestLongColumnSelector)3 VirtualColumns (io.druid.segment.VirtualColumns)3 ByteBuffer (java.nio.ByteBuffer)3 ImmutableList (com.google.common.collect.ImmutableList)2 MapBasedInputRow (io.druid.data.input.MapBasedInputRow)2 ExtractionDimensionSpec (io.druid.query.dimension.ExtractionDimensionSpec)2