use of org.apache.druid.segment.ColumnSelectorFactory in project druid by druid-io.
the class RowBasedGrouperHelper method createResultRowBasedColumnSelectorFactory.
/**
* Creates a {@link ColumnSelectorFactory} that can read rows which originate as results of the provided "query".
*
* @param query a groupBy query
* @param supplier supplier of result rows from the query
* @param finalization whether the column capabilities reported by this factory should reflect finalized types
*/
public static ColumnSelectorFactory createResultRowBasedColumnSelectorFactory(final GroupByQuery query, final Supplier<ResultRow> supplier, final RowSignature.Finalization finalization) {
final RowSignature signature = query.getResultRowSignature(finalization);
final RowAdapter<ResultRow> adapter = new RowAdapter<ResultRow>() {
@Override
public ToLongFunction<ResultRow> timestampFunction() {
if (query.getResultRowHasTimestamp()) {
return row -> row.getLong(0);
} else {
final long timestamp = query.getUniversalTimestamp().getMillis();
return row -> timestamp;
}
}
@Override
public Function<ResultRow, Object> columnFunction(final String columnName) {
final int columnIndex = signature.indexOf(columnName);
if (columnIndex < 0) {
return row -> null;
} else {
return row -> row.get(columnIndex);
}
}
};
// Decorate "signature" so that it returns hasMultipleValues = false. (groupBy does not return multiple values.)
final ColumnInspector decoratedSignature = new ColumnInspector() {
@Nullable
@Override
public ColumnCapabilities getColumnCapabilities(String column) {
final ColumnCapabilities baseCapabilities = signature.getColumnCapabilities(column);
if (baseCapabilities == null || baseCapabilities.hasMultipleValues().isFalse()) {
return baseCapabilities;
} else {
return ColumnCapabilitiesImpl.copyOf(baseCapabilities).setHasMultipleValues(false);
}
}
};
return RowBasedColumnSelectorFactory.create(adapter, supplier::get, decoratedSignature, false);
}
use of org.apache.druid.segment.ColumnSelectorFactory in project druid by druid-io.
the class GroupByQueryEngineV2 method processNonVectorized.
private static Sequence<ResultRow> processNonVectorized(final GroupByQuery query, final StorageAdapter storageAdapter, final ByteBuffer processingBuffer, @Nullable final DateTime fudgeTimestamp, final GroupByQueryConfig querySpecificConfig, @Nullable final Filter filter, final Interval interval) {
final Sequence<Cursor> cursors = storageAdapter.makeCursors(filter, interval, query.getVirtualColumns(), query.getGranularity(), false, null);
return cursors.flatMap(cursor -> new BaseSequence<>(new BaseSequence.IteratorMaker<ResultRow, GroupByEngineIterator<?>>() {
@Override
public GroupByEngineIterator make() {
final ColumnSelectorFactory columnSelectorFactory = cursor.getColumnSelectorFactory();
final ColumnSelectorPlus<GroupByColumnSelectorStrategy>[] selectorPlus = DimensionHandlerUtils.createColumnSelectorPluses(STRATEGY_FACTORY, query.getDimensions(), columnSelectorFactory);
final GroupByColumnSelectorPlus[] dims = createGroupBySelectorPlus(selectorPlus, query.getResultRowDimensionStart());
final int cardinalityForArrayAggregation = getCardinalityForArrayAggregation(querySpecificConfig, query, storageAdapter, processingBuffer);
if (cardinalityForArrayAggregation >= 0) {
return new ArrayAggregateIterator(query, querySpecificConfig, cursor, processingBuffer, fudgeTimestamp, dims, hasNoExplodingDimensions(columnSelectorFactory, query.getDimensions()), cardinalityForArrayAggregation);
} else {
return new HashAggregateIterator(query, querySpecificConfig, cursor, processingBuffer, fudgeTimestamp, dims, hasNoExplodingDimensions(columnSelectorFactory, query.getDimensions()));
}
}
@Override
public void cleanup(GroupByEngineIterator iterFromMake) {
iterFromMake.close();
}
}));
}
use of org.apache.druid.segment.ColumnSelectorFactory in project druid by druid-io.
the class SketchAggregatorFactoryTest method testFactorizeSized.
@Test
public void testFactorizeSized() {
ColumnSelectorFactory colSelectorFactory = EasyMock.mock(ColumnSelectorFactory.class);
EasyMock.expect(colSelectorFactory.makeColumnValueSelector(EasyMock.anyString())).andReturn(EasyMock.createMock(ColumnValueSelector.class)).anyTimes();
EasyMock.replay(colSelectorFactory);
AggregatorAndSize aggregatorAndSize = AGGREGATOR_16384.factorizeWithSize(colSelectorFactory);
Assert.assertEquals(48, aggregatorAndSize.getInitialSizeBytes());
aggregatorAndSize = AGGREGATOR_32768.factorizeWithSize(colSelectorFactory);
Assert.assertEquals(48, aggregatorAndSize.getInitialSizeBytes());
}
use of org.apache.druid.segment.ColumnSelectorFactory in project druid by druid-io.
the class ExpressionAggregationBenchmark method compute.
private double compute(final Function<ColumnSelectorFactory, BufferAggregator> aggregatorFactory) {
final QueryableIndexStorageAdapter adapter = new QueryableIndexStorageAdapter(index);
final Sequence<Cursor> cursors = adapter.makeCursors(null, index.getDataInterval(), VirtualColumns.EMPTY, Granularities.ALL, false, null);
final List<Double> results = cursors.map(cursor -> {
final BufferAggregator bufferAggregator = aggregatorFactory.apply(cursor.getColumnSelectorFactory());
bufferAggregator.init(aggregationBuffer, 0);
while (!cursor.isDone()) {
bufferAggregator.aggregate(aggregationBuffer, 0);
cursor.advance();
}
final Double dbl = (Double) bufferAggregator.get(aggregationBuffer, 0);
bufferAggregator.close();
return dbl;
}).toList();
return Iterables.getOnlyElement(results);
}
use of org.apache.druid.segment.ColumnSelectorFactory in project druid by druid-io.
the class RowBasedGrouperHelper method getResultRowPredicate.
/**
* Returns a predicate that filters result rows from a particular "subquery" based on the intervals and dim filters
* from "query".
*
* @param query outer query
* @param subquery inner query
*/
private static Predicate<ResultRow> getResultRowPredicate(final GroupByQuery query, final GroupByQuery subquery) {
final List<Interval> queryIntervals = query.getIntervals();
final Filter filter = Filters.convertToCNFFromQueryContext(query, Filters.toFilter(query.getDimFilter()));
final SettableSupplier<ResultRow> rowSupplier = new SettableSupplier<>();
final ColumnSelectorFactory columnSelectorFactory = query.getVirtualColumns().wrap(RowBasedGrouperHelper.createResultRowBasedColumnSelectorFactory(subquery, rowSupplier, RowSignature.Finalization.UNKNOWN));
final ValueMatcher filterMatcher = filter == null ? BooleanValueMatcher.of(true) : filter.makeMatcher(columnSelectorFactory);
if (subquery.getUniversalTimestamp() != null && queryIntervals.stream().noneMatch(itvl -> itvl.contains(subquery.getUniversalTimestamp()))) {
// By the way, if there's a universal timestamp that _does_ match the query intervals, we do nothing special here.
return row -> false;
}
return row -> {
if (subquery.getResultRowHasTimestamp()) {
boolean inInterval = false;
for (Interval queryInterval : queryIntervals) {
if (queryInterval.contains(row.getLong(0))) {
inInterval = true;
break;
}
}
if (!inInterval) {
return false;
}
}
rowSupplier.set(row);
return filterMatcher.matches();
};
}
Aggregations