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;
}
});
}
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);
}
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);
}
}
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);
}
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();
}
}
}
}
Aggregations