Search in sources :

Example 1 with VectorCursor

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

the class BaseFilterTest method selectCountUsingVectorizedFilteredAggregator.

private long selectCountUsingVectorizedFilteredAggregator(final DimFilter dimFilter) {
    Preconditions.checkState(makeFilter(dimFilter).canVectorizeMatcher(adapter), "Cannot vectorize filter: %s", dimFilter);
    try (final VectorCursor cursor = makeVectorCursor(null)) {
        final FilteredAggregatorFactory aggregatorFactory = new FilteredAggregatorFactory(new CountAggregatorFactory("count"), maybeOptimize(dimFilter));
        final VectorAggregator aggregator = aggregatorFactory.factorizeVector(cursor.getColumnSelectorFactory());
        final ByteBuffer buf = ByteBuffer.allocate(aggregatorFactory.getMaxIntermediateSizeWithNulls() * 2);
        // Use two slots: one for each form of aggregate.
        aggregator.init(buf, 0);
        aggregator.init(buf, aggregatorFactory.getMaxIntermediateSizeWithNulls());
        for (; !cursor.isDone(); cursor.advance()) {
            aggregator.aggregate(buf, 0, 0, cursor.getCurrentVectorSize());
            final int[] positions = new int[cursor.getCurrentVectorSize()];
            Arrays.fill(positions, aggregatorFactory.getMaxIntermediateSizeWithNulls());
            final int[] allRows = new int[cursor.getCurrentVectorSize()];
            for (int i = 0; i < allRows.length; i++) {
                allRows[i] = i;
            }
            aggregator.aggregate(buf, cursor.getCurrentVectorSize(), positions, allRows, 0);
        }
        final long val1 = (long) aggregator.get(buf, 0);
        final long val2 = (long) aggregator.get(buf, aggregatorFactory.getMaxIntermediateSizeWithNulls());
        if (val1 != val2) {
            throw new ISE("Oh no, val1[%d] != val2[%d]", val1, val2);
        }
        return val1;
    }
}
Also used : FilteredAggregatorFactory(org.apache.druid.query.aggregation.FilteredAggregatorFactory) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) VectorAggregator(org.apache.druid.query.aggregation.VectorAggregator) ISE(org.apache.druid.java.util.common.ISE) VectorCursor(org.apache.druid.segment.vector.VectorCursor) ByteBuffer(java.nio.ByteBuffer)

Example 2 with VectorCursor

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

the class VectorGroupByEngineIteratorTest method testCreateOneGrouperAndCloseItWhenClose.

@Test
public void testCreateOneGrouperAndCloseItWhenClose() throws IOException {
    final Interval interval = TestIndex.DATA_INTERVAL;
    final AggregatorFactory factory = new DoubleSumAggregatorFactory("index", "index");
    final GroupByQuery query = GroupByQuery.builder().setDataSource(QueryRunnerTestHelper.DATA_SOURCE).setGranularity(QueryRunnerTestHelper.DAY_GRAN).setInterval(interval).setDimensions(new DefaultDimensionSpec("market", null, null)).setAggregatorSpecs(factory).build();
    final StorageAdapter storageAdapter = new QueryableIndexStorageAdapter(TestIndex.getMMappedTestIndex());
    final ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[4096]);
    final VectorCursor cursor = storageAdapter.makeVectorCursor(Filters.toFilter(query.getDimFilter()), interval, query.getVirtualColumns(), false, QueryContexts.getVectorSize(query), null);
    final List<GroupByVectorColumnSelector> dimensions = query.getDimensions().stream().map(dimensionSpec -> ColumnProcessors.makeVectorProcessor(dimensionSpec, GroupByVectorColumnProcessorFactory.instance(), cursor.getColumnSelectorFactory())).collect(Collectors.toList());
    final MutableObject<VectorGrouper> grouperCaptor = new MutableObject<>();
    final VectorGroupByEngineIterator iterator = new VectorGroupByEngineIterator(query, new GroupByQueryConfig(), storageAdapter, cursor, interval, dimensions, byteBuffer, null) {

        @Override
        VectorGrouper makeGrouper() {
            grouperCaptor.setValue(Mockito.spy(super.makeGrouper()));
            return grouperCaptor.getValue();
        }
    };
    iterator.close();
    Mockito.verify(grouperCaptor.getValue()).close();
}
Also used : VectorGroupByEngineIterator(org.apache.druid.query.groupby.epinephelinae.vector.VectorGroupByEngine.VectorGroupByEngineIterator) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) GroupByQueryConfig(org.apache.druid.query.groupby.GroupByQueryConfig) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test) IOException(java.io.IOException) StorageAdapter(org.apache.druid.segment.StorageAdapter) Collectors(java.util.stream.Collectors) QueryContexts(org.apache.druid.query.QueryContexts) ColumnProcessors(org.apache.druid.segment.ColumnProcessors) ByteBuffer(java.nio.ByteBuffer) TestIndex(org.apache.druid.segment.TestIndex) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) VectorCursor(org.apache.druid.segment.vector.VectorCursor) Mockito(org.mockito.Mockito) Interval(org.joda.time.Interval) List(java.util.List) QueryRunnerTestHelper(org.apache.druid.query.QueryRunnerTestHelper) VectorGrouper(org.apache.druid.query.groupby.epinephelinae.VectorGrouper) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) MutableObject(org.apache.commons.lang3.mutable.MutableObject) Filters(org.apache.druid.segment.filter.Filters) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) GroupByQueryConfig(org.apache.druid.query.groupby.GroupByQueryConfig) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) StorageAdapter(org.apache.druid.segment.StorageAdapter) QueryableIndexStorageAdapter(org.apache.druid.segment.QueryableIndexStorageAdapter) DoubleSumAggregatorFactory(org.apache.druid.query.aggregation.DoubleSumAggregatorFactory) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) ByteBuffer(java.nio.ByteBuffer) VectorCursor(org.apache.druid.segment.vector.VectorCursor) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) VectorGrouper(org.apache.druid.query.groupby.epinephelinae.VectorGrouper) VectorGroupByEngineIterator(org.apache.druid.query.groupby.epinephelinae.vector.VectorGroupByEngine.VectorGroupByEngineIterator) Interval(org.joda.time.Interval) MutableObject(org.apache.commons.lang3.mutable.MutableObject) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 3 with VectorCursor

use of org.apache.druid.segment.vector.VectorCursor 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 4 with VectorCursor

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

the class TimeseriesQueryEngine method processVectorized.

private Sequence<Result<TimeseriesResultValue>> processVectorized(final TimeseriesQuery query, final StorageAdapter adapter, @Nullable final Filter filter, final Interval queryInterval, final Granularity gran, final boolean descending) {
    final boolean skipEmptyBuckets = query.isSkipEmptyBuckets();
    final List<AggregatorFactory> aggregatorSpecs = query.getAggregatorSpecs();
    final VectorCursor cursor = adapter.makeVectorCursor(filter, queryInterval, query.getVirtualColumns(), descending, QueryContexts.getVectorSize(query), null);
    if (cursor == null) {
        return Sequences.empty();
    }
    final Closer closer = Closer.create();
    closer.register(cursor);
    try {
        final VectorCursorGranularizer granularizer = VectorCursorGranularizer.create(adapter, cursor, gran, queryInterval);
        if (granularizer == null) {
            return Sequences.empty();
        }
        final VectorColumnSelectorFactory columnSelectorFactory = cursor.getColumnSelectorFactory();
        final AggregatorAdapters aggregators = closer.register(AggregatorAdapters.factorizeVector(columnSelectorFactory, query.getAggregatorSpecs()));
        final ResourceHolder<ByteBuffer> bufferHolder = closer.register(bufferPool.take());
        final ByteBuffer buffer = bufferHolder.get();
        if (aggregators.spaceNeeded() > buffer.remaining()) {
            throw new ISE("Not enough space for aggregators, needed [%,d] bytes but have only [%,d].", aggregators.spaceNeeded(), buffer.remaining());
        }
        return Sequences.withBaggage(Sequences.simple(granularizer.getBucketIterable()).map(bucketInterval -> {
            // Whether or not the current bucket is empty
            boolean emptyBucket = true;
            while (!cursor.isDone()) {
                granularizer.setCurrentOffsets(bucketInterval);
                if (granularizer.getEndOffset() > granularizer.getStartOffset()) {
                    if (emptyBucket) {
                        aggregators.init(buffer, 0);
                    }
                    aggregators.aggregateVector(buffer, 0, granularizer.getStartOffset(), granularizer.getEndOffset());
                    emptyBucket = false;
                }
                if (!granularizer.advanceCursorWithinBucket()) {
                    break;
                }
            }
            if (emptyBucket && skipEmptyBuckets) {
                // Return null, will get filtered out later by the Objects::nonNull filter.
                return null;
            }
            final TimeseriesResultBuilder bob = new TimeseriesResultBuilder(gran.toDateTime(bucketInterval.getStartMillis()));
            if (emptyBucket) {
                aggregators.init(buffer, 0);
            }
            for (int i = 0; i < aggregatorSpecs.size(); i++) {
                bob.addMetric(aggregatorSpecs.get(i).getName(), aggregators.get(buffer, 0, i));
            }
            return bob.build();
        }).filter(Objects::nonNull), closer);
    } catch (Throwable t1) {
        try {
            closer.close();
        } catch (Throwable t2) {
            t1.addSuppressed(t2);
        }
        throw t1;
    }
}
Also used : Closer(org.apache.druid.java.util.common.io.Closer) AggregatorAdapters(org.apache.druid.query.aggregation.AggregatorAdapters) AggregatorFactory(org.apache.druid.query.aggregation.AggregatorFactory) VectorCursor(org.apache.druid.segment.vector.VectorCursor) ByteBuffer(java.nio.ByteBuffer) VectorColumnSelectorFactory(org.apache.druid.segment.vector.VectorColumnSelectorFactory) VectorCursorGranularizer(org.apache.druid.query.vector.VectorCursorGranularizer) ISE(org.apache.druid.java.util.common.ISE)

Example 5 with VectorCursor

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

the class ToObjectVectorColumnProcessorFactoryTest method testRead.

@Test
public void testRead() {
    try (final VectorCursor cursor = makeCursor()) {
        final Supplier<Object[]> qualitySupplier = ColumnProcessors.makeVectorProcessor("quality", ToObjectVectorColumnProcessorFactory.INSTANCE, cursor.getColumnSelectorFactory());
        final Supplier<Object[]> qualityLongSupplier = ColumnProcessors.makeVectorProcessor("qualityLong", ToObjectVectorColumnProcessorFactory.INSTANCE, cursor.getColumnSelectorFactory());
        final Supplier<Object[]> qualityFloatSupplier = ColumnProcessors.makeVectorProcessor("qualityFloat", ToObjectVectorColumnProcessorFactory.INSTANCE, cursor.getColumnSelectorFactory());
        final Supplier<Object[]> qualityDoubleSupplier = ColumnProcessors.makeVectorProcessor("qualityDouble", ToObjectVectorColumnProcessorFactory.INSTANCE, cursor.getColumnSelectorFactory());
        final Supplier<Object[]> placementSupplier = ColumnProcessors.makeVectorProcessor("placement", ToObjectVectorColumnProcessorFactory.INSTANCE, cursor.getColumnSelectorFactory());
        final Supplier<Object[]> qualityUniquesSupplier = ColumnProcessors.makeVectorProcessor("quality_uniques", ToObjectVectorColumnProcessorFactory.INSTANCE, cursor.getColumnSelectorFactory());
    }
}
Also used : VectorCursor(org.apache.druid.segment.vector.VectorCursor) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Aggregations

VectorCursor (org.apache.druid.segment.vector.VectorCursor)10 ArrayList (java.util.ArrayList)5 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)4 ByteBuffer (java.nio.ByteBuffer)3 Closer (org.apache.druid.java.util.common.io.Closer)3 Expr (org.apache.druid.math.expr.Expr)3 ExpressionType (org.apache.druid.math.expr.ExpressionType)3 ColumnInspector (org.apache.druid.segment.ColumnInspector)3 QueryableIndexStorageAdapter (org.apache.druid.segment.QueryableIndexStorageAdapter)3 Test (org.junit.Test)3 ImmutableList (com.google.common.collect.ImmutableList)2 IOException (java.io.IOException)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Nullable (javax.annotation.Nullable)2 ISE (org.apache.druid.java.util.common.ISE)2 Granularities (org.apache.druid.java.util.common.granularity.Granularities)2 Sequence (org.apache.druid.java.util.common.guava.Sequence)2 ExprMacroTable (org.apache.druid.math.expr.ExprMacroTable)2 Parser (org.apache.druid.math.expr.Parser)2