Search in sources :

Example 1 with ColumnSelectorFactory

use of org.apache.druid.segment.ColumnSelectorFactory 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 ColumnSelectorFactory

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

the class DoubleMeanAggregationTest method testAggregateWithSize.

@Test
public void testAggregateWithSize() {
    Double[] values = new Double[] { 3.0, 1.0, 2.0 };
    TestObjectColumnSelector<Double> columnValueSelector = new TestObjectColumnSelector<>(values);
    ColumnSelectorFactory colSelectorFactory = EasyMock.mock(ColumnSelectorFactory.class);
    EasyMock.expect(colSelectorFactory.makeColumnValueSelector(EasyMock.anyString())).andReturn(columnValueSelector).anyTimes();
    EasyMock.replay(colSelectorFactory);
    DoubleMeanAggregatorFactory aggregatorFactory = new DoubleMeanAggregatorFactory("name", "fieldName");
    AggregatorAndSize aggregatorAndSize = aggregatorFactory.factorizeWithSize(colSelectorFactory);
    Assert.assertEquals(aggregatorFactory.getMaxIntermediateSize(), aggregatorAndSize.getInitialSizeBytes());
    Assert.assertTrue(aggregatorAndSize.getAggregator() instanceof DoubleMeanAggregator);
    Aggregator aggregator = aggregatorAndSize.getAggregator();
    for (int i = 0; i < values.length; ++i) {
        long sizeDelta = aggregator.aggregateWithSize();
        Assert.assertEquals(0L, sizeDelta);
        columnValueSelector.increment();
    }
    DoubleMeanHolder meanHolder = (DoubleMeanHolder) aggregator.get();
    Assert.assertEquals(2.0, meanHolder.mean(), 0.0);
}
Also used : AggregatorAndSize(org.apache.druid.query.aggregation.AggregatorAndSize) TestObjectColumnSelector(org.apache.druid.query.aggregation.TestObjectColumnSelector) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) Aggregator(org.apache.druid.query.aggregation.Aggregator) Test(org.junit.Test)

Example 3 with ColumnSelectorFactory

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

the class BroadcastSegmentIndexedTableTest method checkColumnSelectorFactory.

private void checkColumnSelectorFactory(String columnName) {
    try (final Closer closer = Closer.create()) {
        final int numRows = backingSegment.asStorageAdapter().getNumRows();
        final SimpleAscendingOffset offset = new SimpleAscendingOffset(numRows);
        final BaseColumn theColumn = backingSegment.asQueryableIndex().getColumnHolder(columnName).getColumn();
        closer.register(theColumn);
        final BaseObjectColumnValueSelector<?> selector = theColumn.makeColumnValueSelector(offset);
        ColumnSelectorFactory tableFactory = broadcastTable.makeColumnSelectorFactory(offset, false, closer);
        final BaseObjectColumnValueSelector<?> tableSelector = tableFactory.makeColumnValueSelector(columnName);
        // compare with base segment selector to make sure tables selector can read correct values
        for (int row = 0; row < numRows; row++) {
            offset.setCurrentOffset(row);
            Assert.assertEquals(selector.getObject(), tableSelector.getObject());
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Closer(org.apache.druid.java.util.common.io.Closer) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) SimpleAscendingOffset(org.apache.druid.segment.SimpleAscendingOffset) BaseColumn(org.apache.druid.segment.column.BaseColumn) IOException(java.io.IOException)

Example 4 with ColumnSelectorFactory

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

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

the class ExpressionSelectorsTest method test_double_bindings.

@Test
public void test_double_bindings() {
    final String columnName = "double3";
    for (StorageAdapter adapter : ADAPTERS) {
        Sequence<Cursor> cursorSequence = adapter.makeCursors(null, adapter.getInterval(), VirtualColumns.EMPTY, Granularities.ALL, false, null);
        List<Cursor> flatten = cursorSequence.toList();
        for (Cursor cursor : flatten) {
            ColumnSelectorFactory factory = cursor.getColumnSelectorFactory();
            // an assortment of plans
            ExpressionPlan plan = ExpressionPlanner.plan(adapter, Parser.parse("\"double3\"", TestExprMacroTable.INSTANCE));
            ExpressionPlan plan2 = ExpressionPlanner.plan(adapter, Parser.parse("\"double3\" + 3.0", TestExprMacroTable.INSTANCE));
            Expr.ObjectBinding bindings = ExpressionSelectors.createBindings(factory, plan);
            Expr.ObjectBinding bindings2 = ExpressionSelectors.createBindings(factory, plan2);
            ColumnValueSelector valueSelector = factory.makeColumnValueSelector(columnName);
            while (!cursor.isDone()) {
                Object bindingVal = bindings.get(columnName);
                Object bindingVal2 = bindings2.get(columnName);
                if (valueSelector.isNull()) {
                    Assert.assertNull(valueSelector.getObject());
                    Assert.assertNull(bindingVal);
                    Assert.assertNull(bindingVal2);
                } else {
                    Assert.assertEquals(valueSelector.getObject(), bindingVal);
                    Assert.assertEquals(valueSelector.getDouble(), bindingVal);
                    Assert.assertEquals(valueSelector.getObject(), bindingVal2);
                    Assert.assertEquals(valueSelector.getDouble(), bindingVal2);
                }
                cursor.advance();
            }
        }
    }
}
Also used : ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) Expr(org.apache.druid.math.expr.Expr) StorageAdapter(org.apache.druid.segment.StorageAdapter) IncrementalIndexStorageAdapter(org.apache.druid.segment.incremental.IncrementalIndexStorageAdapter) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) Cursor(org.apache.druid.segment.Cursor) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Aggregations

ColumnSelectorFactory (org.apache.druid.segment.ColumnSelectorFactory)21 ColumnValueSelector (org.apache.druid.segment.ColumnValueSelector)10 Cursor (org.apache.druid.segment.Cursor)9 DimensionSelector (org.apache.druid.segment.DimensionSelector)8 Test (org.junit.Test)8 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)7 RowBasedColumnSelectorFactory (org.apache.druid.segment.RowBasedColumnSelectorFactory)7 List (java.util.List)6 IndexedInts (org.apache.druid.segment.data.IndexedInts)6 QueryableIndexStorageAdapter (org.apache.druid.segment.QueryableIndexStorageAdapter)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 ByteBuffer (java.nio.ByteBuffer)4 Arrays (java.util.Arrays)4 Nullable (javax.annotation.Nullable)4 NullHandling (org.apache.druid.common.config.NullHandling)4 ValueMatcher (org.apache.druid.query.filter.ValueMatcher)4 RuntimeShapeInspector (org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector)4 BaseFloatColumnValueSelector (org.apache.druid.segment.BaseFloatColumnValueSelector)4 ColumnCapabilitiesImpl (org.apache.druid.segment.column.ColumnCapabilitiesImpl)4 ValueType (org.apache.druid.segment.column.ValueType)4