Search in sources :

Example 1 with BaseObjectColumnValueSelector

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

the class DumpSegment method runDump.

private void runDump(final Injector injector, final QueryableIndex index) throws IOException {
    final ObjectMapper objectMapper = injector.getInstance(Key.get(ObjectMapper.class, Json.class));
    final QueryableIndexStorageAdapter adapter = new QueryableIndexStorageAdapter(index);
    final List<String> columnNames = getColumnsToInclude(index);
    final DimFilter filter = filterJson != null ? objectMapper.readValue(filterJson, DimFilter.class) : null;
    final Sequence<Cursor> cursors = adapter.makeCursors(Filters.toFilter(filter), index.getDataInterval().withChronology(ISOChronology.getInstanceUTC()), VirtualColumns.EMPTY, Granularities.ALL, false, null);
    withOutputStream(new Function<OutputStream, Object>() {

        @Override
        public Object apply(final OutputStream out) {
            final Sequence<Object> sequence = Sequences.map(cursors, new Function<Cursor, Object>() {

                @Override
                public Object apply(Cursor cursor) {
                    ColumnSelectorFactory columnSelectorFactory = cursor.getColumnSelectorFactory();
                    final List<BaseObjectColumnValueSelector> selectors = columnNames.stream().map(columnSelectorFactory::makeColumnValueSelector).collect(Collectors.toList());
                    while (!cursor.isDone()) {
                        final Map<String, Object> row = Maps.newLinkedHashMap();
                        for (int i = 0; i < columnNames.size(); i++) {
                            final String columnName = columnNames.get(i);
                            final Object value = selectors.get(i).getObject();
                            if (timeISO8601 && columnNames.get(i).equals(ColumnHolder.TIME_COLUMN_NAME)) {
                                row.put(columnName, new DateTime(value, DateTimeZone.UTC).toString());
                            } else {
                                row.put(columnName, value);
                            }
                        }
                        try {
                            out.write(objectMapper.writeValueAsBytes(row));
                            out.write('\n');
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                        cursor.advance();
                    }
                    return null;
                }
            });
            evaluateSequenceForSideEffects(sequence);
            return null;
        }
    });
}
Also used : ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BaseObjectColumnValueSelector(org.apache.druid.segment.BaseObjectColumnValueSelector) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) Json(org.apache.druid.guice.annotations.Json) Sequence(org.apache.druid.java.util.common.guava.Sequence) IOException(java.io.IOException) Cursor(org.apache.druid.segment.Cursor) DateTime(org.joda.time.DateTime) Function(com.google.common.base.Function) DimFilter(org.apache.druid.query.filter.DimFilter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with BaseObjectColumnValueSelector

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

the class ExpressionVirtualColumnTest method testMultiObjectSelectorMakesRightSelector.

@Test
public void testMultiObjectSelectorMakesRightSelector() {
    DimensionSpec spec = new DefaultDimensionSpec("expr", "expr");
    // do some ugly faking to test if SingleStringInputDeferredEvaluationExpressionDimensionSelector is created for multi-value expressions when possible
    ColumnSelectorFactory factory = new ColumnSelectorFactory() {

        @Override
        public DimensionSelector makeDimensionSelector(DimensionSpec dimensionSpec) {
            DimensionSelector delegate = COLUMN_SELECTOR_FACTORY.makeDimensionSelector(dimensionSpec);
            DimensionSelector faker = new DimensionSelector() {

                @Override
                public IndexedInts getRow() {
                    return delegate.getRow();
                }

                @Override
                public ValueMatcher makeValueMatcher(@Nullable String value) {
                    return delegate.makeValueMatcher(value);
                }

                @Override
                public ValueMatcher makeValueMatcher(Predicate<String> predicate) {
                    return delegate.makeValueMatcher(predicate);
                }

                @Override
                public void inspectRuntimeShape(RuntimeShapeInspector inspector) {
                    delegate.inspectRuntimeShape(inspector);
                }

                @Nullable
                @Override
                public Object getObject() {
                    return delegate.getObject();
                }

                @Override
                public Class<?> classOfObject() {
                    return delegate.classOfObject();
                }

                @Override
                public int getValueCardinality() {
                    // value doesn't matter as long as not CARDINALITY_UNKNOWN
                    return 3;
                }

                @Nullable
                @Override
                public String lookupName(int id) {
                    return null;
                }

                @Override
                public boolean nameLookupPossibleInAdvance() {
                    // fake this so when SingleStringInputDeferredEvaluationExpressionDimensionSelector it doesn't explode
                    return true;
                }

                @Nullable
                @Override
                public IdLookup idLookup() {
                    return name -> 0;
                }
            };
            return faker;
        }

        @Override
        public ColumnValueSelector makeColumnValueSelector(String columnName) {
            return COLUMN_SELECTOR_FACTORY.makeColumnValueSelector(columnName);
        }

        @Nullable
        @Override
        public ColumnCapabilities getColumnCapabilities(String column) {
            return new ColumnCapabilitiesImpl().setType(ColumnType.STRING).setHasMultipleValues(true).setDictionaryEncoded(true);
        }
    };
    final BaseObjectColumnValueSelector selectorImplicit = SCALE_LIST_SELF_IMPLICIT.makeDimensionSelector(spec, factory);
    final BaseObjectColumnValueSelector selectorExplicit = SCALE_LIST_SELF_EXPLICIT.makeDimensionSelector(spec, factory);
    Assert.assertTrue(selectorImplicit instanceof SingleStringInputDeferredEvaluationExpressionDimensionSelector);
    Assert.assertTrue(selectorExplicit instanceof ExpressionMultiValueDimensionSelector);
}
Also used : Arrays(java.util.Arrays) RuntimeShapeInspector(org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector) ConstantMultiValueDimensionSelector(org.apache.druid.segment.ConstantMultiValueDimensionSelector) MapBasedInputRow(org.apache.druid.data.input.MapBasedInputRow) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) RowAdapters(org.apache.druid.segment.RowAdapters) Parser(org.apache.druid.math.expr.Parser) IdLookup(org.apache.druid.segment.IdLookup) IndexedInts(org.apache.druid.segment.data.IndexedInts) BaseFloatColumnValueSelector(org.apache.druid.segment.BaseFloatColumnValueSelector) Row(org.apache.druid.data.input.Row) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) ImmutableList(com.google.common.collect.ImmutableList) Predicates(com.google.common.base.Predicates) DimensionSelector(org.apache.druid.segment.DimensionSelector) BucketExtractionFn(org.apache.druid.query.extraction.BucketExtractionFn) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) BaseObjectColumnValueSelector(org.apache.druid.segment.BaseObjectColumnValueSelector) Nullable(javax.annotation.Nullable) ValueMatcher(org.apache.druid.query.filter.ValueMatcher) DateTimes(org.apache.druid.java.util.common.DateTimes) RowBasedColumnSelectorFactory(org.apache.druid.segment.RowBasedColumnSelectorFactory) ImmutableMap(com.google.common.collect.ImmutableMap) ValueType(org.apache.druid.segment.column.ValueType) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test) TestExprMacroTable(org.apache.druid.query.expression.TestExprMacroTable) ExprEval(org.apache.druid.math.expr.ExprEval) InputRow(org.apache.druid.data.input.InputRow) BaseLongColumnValueSelector(org.apache.druid.segment.BaseLongColumnValueSelector) ColumnCapabilitiesImpl(org.apache.druid.segment.column.ColumnCapabilitiesImpl) Predicate(com.google.common.base.Predicate) NullHandling(org.apache.druid.common.config.NullHandling) RowSignature(org.apache.druid.segment.column.RowSignature) DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) ColumnCapabilities(org.apache.druid.segment.column.ColumnCapabilities) ColumnType(org.apache.druid.segment.column.ColumnType) Assert(org.junit.Assert) ConstantDimensionSelector(org.apache.druid.segment.ConstantDimensionSelector) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) ConstantMultiValueDimensionSelector(org.apache.druid.segment.ConstantMultiValueDimensionSelector) DimensionSelector(org.apache.druid.segment.DimensionSelector) ConstantDimensionSelector(org.apache.druid.segment.ConstantDimensionSelector) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) RowBasedColumnSelectorFactory(org.apache.druid.segment.RowBasedColumnSelectorFactory) RuntimeShapeInspector(org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector) BaseObjectColumnValueSelector(org.apache.druid.segment.BaseObjectColumnValueSelector) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Predicate(com.google.common.base.Predicate) Nullable(javax.annotation.Nullable) ColumnCapabilitiesImpl(org.apache.druid.segment.column.ColumnCapabilitiesImpl) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 3 with BaseObjectColumnValueSelector

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

the class ExpressionVirtualColumnTest method testObjectSelector.

@Test
public void testObjectSelector() {
    final BaseObjectColumnValueSelector selector = X_PLUS_Y.makeColumnValueSelector("expr", COLUMN_SELECTOR_FACTORY);
    CURRENT_ROW.set(ROW0);
    Assert.assertEquals(null, selector.getObject());
    CURRENT_ROW.set(ROW1);
    if (NullHandling.replaceWithDefault()) {
        Assert.assertEquals(4L, selector.getObject());
    } else {
        // y is null for row1
        Assert.assertEquals(null, selector.getObject());
    }
    CURRENT_ROW.set(ROW2);
    Assert.assertEquals(5.1d, selector.getObject());
    CURRENT_ROW.set(ROW3);
    Assert.assertEquals(5L, selector.getObject());
}
Also used : BaseObjectColumnValueSelector(org.apache.druid.segment.BaseObjectColumnValueSelector) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 4 with BaseObjectColumnValueSelector

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

the class VirtualColumnsTest method testMakeSelectors.

@Test
public void testMakeSelectors() {
    final VirtualColumns virtualColumns = makeVirtualColumns();
    final BaseObjectColumnValueSelector objectSelector = virtualColumns.makeColumnValueSelector("expr", null);
    final DimensionSelector dimensionSelector = virtualColumns.makeDimensionSelector(new DefaultDimensionSpec("expr", "x"), null);
    final DimensionSelector extractionDimensionSelector = virtualColumns.makeDimensionSelector(new ExtractionDimensionSpec("expr", "x", new BucketExtractionFn(1.0, 0.5)), null);
    final BaseFloatColumnValueSelector floatSelector = virtualColumns.makeColumnValueSelector("expr", null);
    final BaseLongColumnValueSelector longSelector = virtualColumns.makeColumnValueSelector("expr", null);
    Assert.assertEquals(1L, objectSelector.getObject());
    Assert.assertEquals("1", dimensionSelector.lookupName(dimensionSelector.getRow().get(0)));
    Assert.assertEquals("0.5", extractionDimensionSelector.lookupName(extractionDimensionSelector.getRow().get(0)));
    Assert.assertEquals(1.0f, floatSelector.getFloat(), 0.0f);
    Assert.assertEquals(1L, longSelector.getLong());
}
Also used : BaseFloatColumnValueSelector(org.apache.druid.segment.BaseFloatColumnValueSelector) DimensionSelector(org.apache.druid.segment.DimensionSelector) BucketExtractionFn(org.apache.druid.query.extraction.BucketExtractionFn) BaseObjectColumnValueSelector(org.apache.druid.segment.BaseObjectColumnValueSelector) BaseLongColumnValueSelector(org.apache.druid.segment.BaseLongColumnValueSelector) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) VirtualColumns(org.apache.druid.segment.VirtualColumns) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 5 with BaseObjectColumnValueSelector

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

the class VirtualColumnsTest method testMakeSelectorsWithDotSupport.

@Test
public void testMakeSelectorsWithDotSupport() {
    final VirtualColumns virtualColumns = makeVirtualColumns();
    final BaseObjectColumnValueSelector objectSelector = virtualColumns.makeColumnValueSelector("foo.5", null);
    final DimensionSelector dimensionSelector = virtualColumns.makeDimensionSelector(new DefaultDimensionSpec("foo.5", "x"), null);
    final BaseFloatColumnValueSelector floatSelector = virtualColumns.makeColumnValueSelector("foo.5", null);
    final BaseLongColumnValueSelector longSelector = virtualColumns.makeColumnValueSelector("foo.5", null);
    Assert.assertEquals(5L, objectSelector.getObject());
    Assert.assertEquals("5", dimensionSelector.lookupName(dimensionSelector.getRow().get(0)));
    Assert.assertEquals(5.0f, floatSelector.getFloat(), 0.0f);
    Assert.assertEquals(5L, longSelector.getLong());
}
Also used : BaseFloatColumnValueSelector(org.apache.druid.segment.BaseFloatColumnValueSelector) DimensionSelector(org.apache.druid.segment.DimensionSelector) BaseObjectColumnValueSelector(org.apache.druid.segment.BaseObjectColumnValueSelector) BaseLongColumnValueSelector(org.apache.druid.segment.BaseLongColumnValueSelector) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) VirtualColumns(org.apache.druid.segment.VirtualColumns) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Aggregations

BaseObjectColumnValueSelector (org.apache.druid.segment.BaseObjectColumnValueSelector)12 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)6 Test (org.junit.Test)6 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)5 BaseFloatColumnValueSelector (org.apache.druid.segment.BaseFloatColumnValueSelector)4 BaseLongColumnValueSelector (org.apache.druid.segment.BaseLongColumnValueSelector)4 DimensionSelector (org.apache.druid.segment.DimensionSelector)4 ExtractionDimensionSpec (org.apache.druid.query.dimension.ExtractionDimensionSpec)3 ImmutableList (com.google.common.collect.ImmutableList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 HyperLogLogCollector (org.apache.druid.hll.HyperLogLogCollector)2 DateTimes (org.apache.druid.java.util.common.DateTimes)2 IAE (org.apache.druid.java.util.common.IAE)2 Sequence (org.apache.druid.java.util.common.guava.Sequence)2 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)2 BucketExtractionFn (org.apache.druid.query.extraction.BucketExtractionFn)2 ColumnSelectorFactory (org.apache.druid.segment.ColumnSelectorFactory)2 VirtualColumns (org.apache.druid.segment.VirtualColumns)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1