Search in sources :

Example 16 with ColumnSelectorFactory

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

the class DoubleMeanAggregatorFactoryTest method testFactorizeWithSize.

@Test
public void testFactorizeWithSize() {
    ColumnSelectorFactory colSelectorFactory = EasyMock.mock(ColumnSelectorFactory.class);
    EasyMock.expect(colSelectorFactory.makeColumnValueSelector(EasyMock.anyString())).andReturn(EasyMock.createMock(ColumnValueSelector.class)).anyTimes();
    EasyMock.replay(colSelectorFactory);
    DoubleMeanAggregatorFactory factory = new DoubleMeanAggregatorFactory("name", "fieldName");
    AggregatorAndSize aggregatorAndSize = factory.factorizeWithSize(colSelectorFactory);
    Assert.assertEquals(DoubleMeanHolder.MAX_INTERMEDIATE_SIZE, aggregatorAndSize.getInitialSizeBytes());
    Assert.assertTrue(aggregatorAndSize.getAggregator() instanceof DoubleMeanAggregator);
}
Also used : AggregatorAndSize(org.apache.druid.query.aggregation.AggregatorAndSize) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) Test(org.junit.Test)

Example 17 with ColumnSelectorFactory

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

the class HashJoinEngine method makeJoinCursor.

/**
 * Creates a cursor that represents the join of {@param leftCursor} with {@param joinableClause}. The resulting
 * cursor may generate nulls on the left-hand side (for righty joins; see {@link JoinType#isRighty()}) or on
 * the right-hand side (for lefty joins; see {@link JoinType#isLefty()}). Columns that start with the
 * joinable clause's prefix (see {@link JoinableClause#getPrefix()}) will come from the Joinable's column selector
 * factory, and all other columns will come from the leftCursor's column selector factory.
 *
 * Ensuring that the joinable clause's prefix does not conflict with any columns from "leftCursor" is the
 * responsibility of the caller. If there is such a conflict (for example, if the joinable clause's prefix is "j.",
 * and the leftCursor has a field named "j.j.abrams"), then the field from the leftCursor will be shadowed and will
 * not be queryable through the returned Cursor. This happens even if the right-hand joinable doesn't actually have a
 * column with this name.
 */
public static Cursor makeJoinCursor(final Cursor leftCursor, final JoinableClause joinableClause, final boolean descending, final Closer closer) {
    final ColumnSelectorFactory leftColumnSelectorFactory = leftCursor.getColumnSelectorFactory();
    final JoinMatcher joinMatcher = joinableClause.getJoinable().makeJoinMatcher(leftColumnSelectorFactory, joinableClause.getCondition(), joinableClause.getJoinType().isRighty(), descending, closer);
    class JoinColumnSelectorFactory implements ColumnSelectorFactory {

        @Override
        public DimensionSelector makeDimensionSelector(DimensionSpec dimensionSpec) {
            if (joinableClause.includesColumn(dimensionSpec.getDimension())) {
                return joinMatcher.getColumnSelectorFactory().makeDimensionSelector(dimensionSpec.withDimension(joinableClause.unprefix(dimensionSpec.getDimension())));
            } else {
                final DimensionSelector leftSelector = leftColumnSelectorFactory.makeDimensionSelector(dimensionSpec);
                if (!joinableClause.getJoinType().isRighty()) {
                    return leftSelector;
                } else {
                    return new PossiblyNullDimensionSelector(leftSelector, joinMatcher::matchingRemainder);
                }
            }
        }

        @Override
        public ColumnValueSelector makeColumnValueSelector(String column) {
            if (joinableClause.includesColumn(column)) {
                return joinMatcher.getColumnSelectorFactory().makeColumnValueSelector(joinableClause.unprefix(column));
            } else {
                final ColumnValueSelector<?> leftSelector = leftColumnSelectorFactory.makeColumnValueSelector(column);
                if (!joinableClause.getJoinType().isRighty()) {
                    return leftSelector;
                } else {
                    return new PossiblyNullColumnValueSelector<>(leftSelector, joinMatcher::matchingRemainder);
                }
            }
        }

        @Nullable
        @Override
        public ColumnCapabilities getColumnCapabilities(String column) {
            if (joinableClause.includesColumn(column)) {
                return joinMatcher.getColumnSelectorFactory().getColumnCapabilities(joinableClause.unprefix(column));
            } else {
                return leftColumnSelectorFactory.getColumnCapabilities(column);
            }
        }
    }
    final JoinColumnSelectorFactory joinColumnSelectorFactory = new JoinColumnSelectorFactory();
    class JoinCursor implements Cursor {

        public void initialize() {
            matchCurrentPosition();
            if (!joinableClause.getJoinType().isLefty()) {
                while (!joinMatcher.hasMatch() && !isDone()) {
                    advance();
                    matchCurrentPosition();
                }
            }
        }

        @Override
        @Nonnull
        public ColumnSelectorFactory getColumnSelectorFactory() {
            return joinColumnSelectorFactory;
        }

        @Override
        @Nonnull
        public DateTime getTime() {
            return leftCursor.getTime();
        }

        @Override
        public void advance() {
            advanceUninterruptibly();
            BaseQuery.checkInterrupted();
        }

        private void matchCurrentPosition() {
            if (leftCursor.isDone()) {
                if (joinableClause.getJoinType().isRighty() && !joinMatcher.matchingRemainder()) {
                    // Warning! The way this engine handles "righty" joins is flawed: it generates the 'remainder' rows
                    // per-segment, but this should really be done globally. This should be improved in the future.
                    joinMatcher.matchRemainder();
                }
            } else {
                joinMatcher.matchCondition();
            }
        }

        @Override
        public void advanceUninterruptibly() {
            if (joinMatcher.hasMatch()) {
                joinMatcher.nextMatch();
                if (joinMatcher.hasMatch()) {
                    return;
                }
            }
            assert !joinMatcher.hasMatch();
            if (leftCursor.isDone()) {
                // No right-hand matches and nothing on the left cursor. We're done; return.
                assert isDone();
                return;
            }
            do {
                // No more right-hand side matches; advance the left-hand side.
                leftCursor.advanceUninterruptibly();
                // Update joinMatcher state to match new cursor position.
                matchCurrentPosition();
            // If this is not a left/full join, and joinMatcher didn't match anything, then keep advancing until we find
            // left rows that have matching right rows.
            } while (!joinableClause.getJoinType().isLefty() && !joinMatcher.hasMatch() && !leftCursor.isDone());
        }

        @Override
        public boolean isDone() {
            return leftCursor.isDone() && !joinMatcher.hasMatch();
        }

        @Override
        public boolean isDoneOrInterrupted() {
            return isDone() || Thread.currentThread().isInterrupted();
        }

        @Override
        public void reset() {
            leftCursor.reset();
            joinMatcher.reset();
        }
    }
    final JoinCursor joinCursor = new JoinCursor();
    joinCursor.initialize();
    return joinCursor;
}
Also used : DimensionSpec(org.apache.druid.query.dimension.DimensionSpec) DimensionSelector(org.apache.druid.segment.DimensionSelector) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) Cursor(org.apache.druid.segment.Cursor)

Example 18 with ColumnSelectorFactory

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

the class IncrementalIndexRowIterator method makeRowPointer.

private static RowPointer makeRowPointer(IncrementalIndex incrementalIndex, IncrementalIndexRowHolder rowHolder, RowNumCounter rowNumCounter) {
    ColumnSelectorFactory columnSelectorFactory = new IncrementalIndexColumnSelectorFactory(new IncrementalIndexStorageAdapter(incrementalIndex), VirtualColumns.EMPTY, false, rowHolder);
    ColumnValueSelector[] dimensionSelectors = incrementalIndex.getDimensions().stream().map(dim -> {
        ColumnValueSelector selectorWithUnsortedValues = columnSelectorFactory.makeColumnValueSelector(dim.getName());
        return dim.getIndexer().convertUnsortedValuesToSorted(selectorWithUnsortedValues);
    }).toArray(ColumnValueSelector[]::new);
    List<DimensionHandler> dimensionHandlers = incrementalIndex.getDimensions().stream().map(IncrementalIndex.DimensionDesc::getHandler).collect(Collectors.toList());
    ColumnValueSelector[] metricSelectors = incrementalIndex.getMetricNames().stream().map(columnSelectorFactory::makeColumnValueSelector).toArray(ColumnValueSelector[]::new);
    return new RowPointer(rowHolder, dimensionSelectors, dimensionHandlers, metricSelectors, incrementalIndex.getMetricNames(), rowNumCounter);
}
Also used : VirtualColumns(org.apache.druid.segment.VirtualColumns) Iterator(java.util.Iterator) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) IndexableAdapter(org.apache.druid.segment.IndexableAdapter) TimeAndDimsPointer(org.apache.druid.segment.TimeAndDimsPointer) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) List(java.util.List) DimensionHandler(org.apache.druid.segment.DimensionHandler) RowPointer(org.apache.druid.segment.RowPointer) RowNumCounter(org.apache.druid.segment.RowNumCounter) TransformableRowIterator(org.apache.druid.segment.TransformableRowIterator) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) DimensionHandler(org.apache.druid.segment.DimensionHandler) RowPointer(org.apache.druid.segment.RowPointer) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector)

Example 19 with ColumnSelectorFactory

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

the class ExpressionSelectorsTest method test_long_bindings.

@Test
public void test_long_bindings() {
    final String columnName = "long3";
    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("\"long3\"", TestExprMacroTable.INSTANCE));
            ExpressionPlan plan2 = ExpressionPlanner.plan(adapter, Parser.parse("\"long3\" + 3", 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.getLong(), bindingVal);
                    Assert.assertEquals(valueSelector.getObject(), bindingVal2);
                    Assert.assertEquals(valueSelector.getLong(), 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)

Example 20 with ColumnSelectorFactory

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

the class BaseFilterTest method selectColumnValuesMatchingFilterUsingVectorizedPostFiltering.

private List<String> selectColumnValuesMatchingFilterUsingVectorizedPostFiltering(final DimFilter filter, final String selectColumn) {
    final Filter theFilter = makeFilter(filter);
    final Filter postFilteringFilter = new Filter() {

        @Override
        public <T> T getBitmapResult(BitmapIndexSelector selector, BitmapResultFactory<T> bitmapResultFactory) {
            throw new UnsupportedOperationException();
        }

        @Override
        public ValueMatcher makeMatcher(ColumnSelectorFactory factory) {
            return theFilter.makeMatcher(factory);
        }

        @Override
        public boolean supportsBitmapIndex(BitmapIndexSelector selector) {
            return false;
        }

        @Override
        public boolean shouldUseBitmapIndex(BitmapIndexSelector selector) {
            return false;
        }

        @Override
        public VectorValueMatcher makeVectorMatcher(VectorColumnSelectorFactory factory) {
            return theFilter.makeVectorMatcher(factory);
        }

        @Override
        public boolean canVectorizeMatcher(ColumnInspector inspector) {
            return theFilter.canVectorizeMatcher(inspector);
        }

        @Override
        public Set<String> getRequiredColumns() {
            return null;
        }

        @Override
        public boolean supportsSelectivityEstimation(ColumnSelector columnSelector, BitmapIndexSelector indexSelector) {
            return false;
        }

        @Override
        public double estimateSelectivity(BitmapIndexSelector indexSelector) {
            return 1.0;
        }
    };
    try (final VectorCursor cursor = makeVectorCursor(postFilteringFilter)) {
        final SingleValueDimensionVectorSelector selector = cursor.getColumnSelectorFactory().makeSingleValueDimensionSelector(new DefaultDimensionSpec(selectColumn, selectColumn));
        final List<String> values = new ArrayList<>();
        while (!cursor.isDone()) {
            final int[] rowVector = selector.getRowVector();
            for (int i = 0; i < cursor.getCurrentVectorSize(); i++) {
                values.add(selector.lookupName(rowVector[i]));
            }
            cursor.advance();
        }
        return values;
    }
}
Also used : ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) RowBasedColumnSelectorFactory(org.apache.druid.segment.RowBasedColumnSelectorFactory) VectorColumnSelectorFactory(org.apache.druid.segment.vector.VectorColumnSelectorFactory) ArrayList(java.util.ArrayList) ColumnInspector(org.apache.druid.segment.ColumnInspector) VectorCursor(org.apache.druid.segment.vector.VectorCursor) VectorColumnSelectorFactory(org.apache.druid.segment.vector.VectorColumnSelectorFactory) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) DimFilter(org.apache.druid.query.filter.DimFilter) Filter(org.apache.druid.query.filter.Filter) ColumnSelector(org.apache.druid.segment.ColumnSelector) BitmapResultFactory(org.apache.druid.query.BitmapResultFactory) BitmapIndexSelector(org.apache.druid.query.filter.BitmapIndexSelector) SingleValueDimensionVectorSelector(org.apache.druid.segment.vector.SingleValueDimensionVectorSelector)

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