Search in sources :

Example 1 with VectorObjectSelector

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

the class ExpressionVectorSelectorsTest method sanityTestVectorizedExpressionSelectors.

public static void sanityTestVectorizedExpressionSelectors(String expression, @Nullable ExpressionType outputType, QueryableIndex index, Closer closer, int rowsPerSegment) {
    final List<Object> results = new ArrayList<>(rowsPerSegment);
    final VirtualColumns virtualColumns = VirtualColumns.create(ImmutableList.of(new ExpressionVirtualColumn("v", expression, ExpressionType.toColumnType(outputType), TestExprMacroTable.INSTANCE)));
    final QueryableIndexStorageAdapter storageAdapter = new QueryableIndexStorageAdapter(index);
    VectorCursor cursor = storageAdapter.makeVectorCursor(null, index.getDataInterval(), virtualColumns, false, 512, null);
    ColumnCapabilities capabilities = virtualColumns.getColumnCapabilities(storageAdapter, "v");
    int rowCount = 0;
    if (capabilities.isDictionaryEncoded().isTrue()) {
        SingleValueDimensionVectorSelector selector = cursor.getColumnSelectorFactory().makeSingleValueDimensionSelector(DefaultDimensionSpec.of("v"));
        while (!cursor.isDone()) {
            int[] row = selector.getRowVector();
            for (int i = 0; i < selector.getCurrentVectorSize(); i++, rowCount++) {
                results.add(selector.lookupName(row[i]));
            }
            cursor.advance();
        }
    } else {
        VectorValueSelector selector = null;
        VectorObjectSelector objectSelector = null;
        if (outputType != null && outputType.isNumeric()) {
            selector = cursor.getColumnSelectorFactory().makeValueSelector("v");
        } else {
            objectSelector = cursor.getColumnSelectorFactory().makeObjectSelector("v");
        }
        while (!cursor.isDone()) {
            boolean[] nulls;
            switch(outputType.getType()) {
                case LONG:
                    nulls = selector.getNullVector();
                    long[] longs = selector.getLongVector();
                    for (int i = 0; i < selector.getCurrentVectorSize(); i++, rowCount++) {
                        results.add(nulls != null && nulls[i] ? null : longs[i]);
                    }
                    break;
                case DOUBLE:
                    // special case to test floats just to get coverage on getFloatVector
                    if ("float2".equals(expression)) {
                        nulls = selector.getNullVector();
                        float[] floats = selector.getFloatVector();
                        for (int i = 0; i < selector.getCurrentVectorSize(); i++, rowCount++) {
                            results.add(nulls != null && nulls[i] ? null : (double) floats[i]);
                        }
                    } else {
                        nulls = selector.getNullVector();
                        double[] doubles = selector.getDoubleVector();
                        for (int i = 0; i < selector.getCurrentVectorSize(); i++, rowCount++) {
                            results.add(nulls != null && nulls[i] ? null : doubles[i]);
                        }
                    }
                    break;
                case STRING:
                    Object[] objects = objectSelector.getObjectVector();
                    for (int i = 0; i < objectSelector.getCurrentVectorSize(); i++, rowCount++) {
                        results.add(objects[i]);
                    }
                    break;
            }
            cursor.advance();
        }
    }
    closer.register(cursor);
    Sequence<Cursor> cursors = new QueryableIndexStorageAdapter(index).makeCursors(null, index.getDataInterval(), virtualColumns, Granularities.ALL, false, null);
    int rowCountCursor = cursors.map(nonVectorized -> {
        final ColumnValueSelector nonSelector = nonVectorized.getColumnSelectorFactory().makeColumnValueSelector("v");
        int rows = 0;
        while (!nonVectorized.isDone()) {
            Assert.assertEquals(StringUtils.format("Failed at row %s", rows), nonSelector.getObject(), results.get(rows));
            rows++;
            nonVectorized.advance();
        }
        return rows;
    }).accumulate(0, (acc, in) -> acc + in);
    Assert.assertTrue(rowCountCursor > 0);
    Assert.assertEquals(rowCountCursor, rowCount);
}
Also used : BeforeClass(org.junit.BeforeClass) SegmentGenerator(org.apache.druid.segment.generator.SegmentGenerator) RunWith(org.junit.runner.RunWith) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) Parser(org.apache.druid.math.expr.Parser) ArrayList(java.util.ArrayList) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) GeneratorBasicSchemas(org.apache.druid.segment.generator.GeneratorBasicSchemas) VectorCursor(org.apache.druid.segment.vector.VectorCursor) ImmutableList(com.google.common.collect.ImmutableList) ExpressionType(org.apache.druid.math.expr.ExpressionType) Expr(org.apache.druid.math.expr.Expr) SingleValueDimensionVectorSelector(org.apache.druid.segment.vector.SingleValueDimensionVectorSelector) Parameterized(org.junit.runners.Parameterized) Nullable(javax.annotation.Nullable) Before(org.junit.Before) Sequence(org.apache.druid.java.util.common.guava.Sequence) ColumnInspector(org.apache.druid.segment.ColumnInspector) AfterClass(org.junit.AfterClass) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) VirtualColumns(org.apache.druid.segment.VirtualColumns) Closer(org.apache.druid.java.util.common.io.Closer) QueryableIndex(org.apache.druid.segment.QueryableIndex) StringUtils(org.apache.druid.java.util.common.StringUtils) GeneratorSchemaInfo(org.apache.druid.segment.generator.GeneratorSchemaInfo) VectorObjectSelector(org.apache.druid.segment.vector.VectorObjectSelector) Test(org.junit.Test) IOException(java.io.IOException) TestExprMacroTable(org.apache.druid.query.expression.TestExprMacroTable) VectorValueSelector(org.apache.druid.segment.vector.VectorValueSelector) Collectors(java.util.stream.Collectors) Granularities(org.apache.druid.java.util.common.granularity.Granularities) ExprMacroTable(org.apache.druid.math.expr.ExprMacroTable) List(java.util.List) Cursor(org.apache.druid.segment.Cursor) LinearShardSpec(org.apache.druid.timeline.partition.LinearShardSpec) DataSegment(org.apache.druid.timeline.DataSegment) ColumnCapabilities(org.apache.druid.segment.column.ColumnCapabilities) Assert(org.junit.Assert) ArrayList(java.util.ArrayList) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) VectorCursor(org.apache.druid.segment.vector.VectorCursor) Cursor(org.apache.druid.segment.Cursor) VectorCursor(org.apache.druid.segment.vector.VectorCursor) ColumnCapabilities(org.apache.druid.segment.column.ColumnCapabilities) VectorValueSelector(org.apache.druid.segment.vector.VectorValueSelector) SingleValueDimensionVectorSelector(org.apache.druid.segment.vector.SingleValueDimensionVectorSelector) VectorObjectSelector(org.apache.druid.segment.vector.VectorObjectSelector) VirtualColumns(org.apache.druid.segment.VirtualColumns) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector)

Example 2 with VectorObjectSelector

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

the class ApproximateHistogramFoldingVectorAggregatorTest method setup.

@Before
public void setup() {
    h1 = new ApproximateHistogram(5);
    h2 = new ApproximateHistogram(5);
    for (int i = 0; i < 5; ++i) {
        h1.offer(FLOATS[i]);
    }
    for (int i = 5; i < FLOATS.length; ++i) {
        h2.offer(FLOATS[i]);
    }
    VectorObjectSelector vectorObjectSelector = createMock(VectorObjectSelector.class);
    expect(vectorObjectSelector.getObjectVector()).andReturn(new Object[] { h1, null, h2, null }).anyTimes();
    EasyMock.replay(vectorObjectSelector);
    vectorColumnSelectorFactory = createMock(VectorColumnSelectorFactory.class);
    expect(vectorColumnSelectorFactory.makeObjectSelector("field")).andReturn(vectorObjectSelector).anyTimes();
    expect(vectorColumnSelectorFactory.getColumnCapabilities("field")).andReturn(new ColumnCapabilitiesImpl().setType(ApproximateHistogramAggregatorFactory.TYPE));
    expect(vectorColumnSelectorFactory.getColumnCapabilities("string_field")).andReturn(new ColumnCapabilitiesImpl().setType(ColumnType.STRING));
    expect(vectorColumnSelectorFactory.getColumnCapabilities("double_field")).andReturn(new ColumnCapabilitiesImpl().setType(ColumnType.STRING));
    EasyMock.replay(vectorColumnSelectorFactory);
}
Also used : VectorObjectSelector(org.apache.druid.segment.vector.VectorObjectSelector) VectorColumnSelectorFactory(org.apache.druid.segment.vector.VectorColumnSelectorFactory) ColumnCapabilitiesImpl(org.apache.druid.segment.column.ColumnCapabilitiesImpl) Before(org.junit.Before)

Example 3 with VectorObjectSelector

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

the class VirtualColumns method makeVectorObjectSelector.

/**
 * Create a column vector object selector.
 *
 * @throws IllegalArgumentException if the virtual column does not exist (see {@link #exists(String)}
 */
public VectorObjectSelector makeVectorObjectSelector(String columnName, VectorColumnSelectorFactory factory) {
    final VirtualColumn virtualColumn = getVirtualColumnForSelector(columnName);
    final VectorObjectSelector selector = virtualColumn.makeVectorObjectSelector(columnName, factory);
    Preconditions.checkNotNull(selector, "selector");
    return selector;
}
Also used : VectorObjectSelector(org.apache.druid.segment.vector.VectorObjectSelector)

Example 4 with VectorObjectSelector

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

the class ComplexColumn method makeVectorObjectSelector.

@Override
default VectorObjectSelector makeVectorObjectSelector(ReadableVectorOffset offset) {
    return new VectorObjectSelector() {

        final Object[] vector = new Object[offset.getMaxVectorSize()];

        private int id = ReadableVectorInspector.NULL_ID;

        @Override
        public Object[] getObjectVector() {
            if (id == offset.getId()) {
                return vector;
            }
            if (offset.isContiguous()) {
                final int startOffset = offset.getStartOffset();
                final int vectorSize = offset.getCurrentVectorSize();
                for (int i = 0; i < vectorSize; i++) {
                    vector[i] = getRowValue(startOffset + i);
                }
            } else {
                final int[] offsets = offset.getOffsets();
                final int vectorSize = offset.getCurrentVectorSize();
                for (int i = 0; i < vectorSize; i++) {
                    vector[i] = getRowValue(offsets[i]);
                }
            }
            id = offset.getId();
            return vector;
        }

        @Override
        public int getCurrentVectorSize() {
            return offset.getCurrentVectorSize();
        }

        @Override
        public int getMaxVectorSize() {
            return offset.getMaxVectorSize();
        }
    };
}
Also used : VectorObjectSelector(org.apache.druid.segment.vector.VectorObjectSelector) ExtensionPoint(org.apache.druid.guice.annotations.ExtensionPoint)

Example 5 with VectorObjectSelector

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

the class BaseFilterTest method selectColumnValuesMatchingFilterUsingVectorVirtualColumnCursor.

private List<String> selectColumnValuesMatchingFilterUsingVectorVirtualColumnCursor(final DimFilter filter, final String virtualColumn, final String selectColumn) {
    final Expr parsedIdentifier = Parser.parse(selectColumn, TestExprMacroTable.INSTANCE);
    try (final VectorCursor cursor = makeVectorCursor(makeFilter(filter))) {
        final ExpressionType outputType = parsedIdentifier.getOutputType(cursor.getColumnSelectorFactory());
        final List<String> values = new ArrayList<>();
        if (outputType.is(ExprType.STRING)) {
            final VectorObjectSelector objectSelector = cursor.getColumnSelectorFactory().makeObjectSelector(virtualColumn);
            while (!cursor.isDone()) {
                final Object[] rowVector = objectSelector.getObjectVector();
                for (int i = 0; i < cursor.getCurrentVectorSize(); i++) {
                    values.add((String) rowVector[i]);
                }
                cursor.advance();
            }
        } else {
            final VectorValueSelector valueSelector = cursor.getColumnSelectorFactory().makeValueSelector(virtualColumn);
            while (!cursor.isDone()) {
                final boolean[] nulls = valueSelector.getNullVector();
                if (outputType.is(ExprType.DOUBLE)) {
                    final double[] doubles = valueSelector.getDoubleVector();
                    for (int i = 0; i < cursor.getCurrentVectorSize(); i++) {
                        if (nulls != null && nulls[i]) {
                            values.add(null);
                        } else {
                            values.add(String.valueOf(doubles[i]));
                        }
                    }
                } else {
                    final long[] longs = valueSelector.getLongVector();
                    for (int i = 0; i < cursor.getCurrentVectorSize(); i++) {
                        if (nulls != null && nulls[i]) {
                            values.add(null);
                        } else {
                            values.add(String.valueOf(longs[i]));
                        }
                    }
                }
                cursor.advance();
            }
        }
        return values;
    }
}
Also used : ArrayList(java.util.ArrayList) VectorCursor(org.apache.druid.segment.vector.VectorCursor) Expr(org.apache.druid.math.expr.Expr) VectorValueSelector(org.apache.druid.segment.vector.VectorValueSelector) VectorObjectSelector(org.apache.druid.segment.vector.VectorObjectSelector) ExpressionType(org.apache.druid.math.expr.ExpressionType)

Aggregations

VectorObjectSelector (org.apache.druid.segment.vector.VectorObjectSelector)5 ArrayList (java.util.ArrayList)2 Expr (org.apache.druid.math.expr.Expr)2 ExpressionType (org.apache.druid.math.expr.ExpressionType)2 VectorCursor (org.apache.druid.segment.vector.VectorCursor)2 VectorValueSelector (org.apache.druid.segment.vector.VectorValueSelector)2 Before (org.junit.Before)2 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 ExtensionPoint (org.apache.druid.guice.annotations.ExtensionPoint)1 StringUtils (org.apache.druid.java.util.common.StringUtils)1 Granularities (org.apache.druid.java.util.common.granularity.Granularities)1 Sequence (org.apache.druid.java.util.common.guava.Sequence)1 Closer (org.apache.druid.java.util.common.io.Closer)1 ExprMacroTable (org.apache.druid.math.expr.ExprMacroTable)1 Parser (org.apache.druid.math.expr.Parser)1 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)1