use of org.apache.druid.segment.DimensionSelector in project druid by druid-io.
the class VirtualColumnsTest method testMakeSelectors.
@Test
public void testMakeSelectors() {
final VirtualColumns virtualColumns = makeVirtualColumns();
final BaseObjectColumnValueSelector objectSelector = virtualColumns.makeColumnValueSelector("expr", null);
final DimensionSelector dimensionSelector = virtualColumns.makeDimensionSelector(new DefaultDimensionSpec("expr", "x"), null);
final DimensionSelector extractionDimensionSelector = virtualColumns.makeDimensionSelector(new ExtractionDimensionSpec("expr", "x", new BucketExtractionFn(1.0, 0.5)), null);
final BaseFloatColumnValueSelector floatSelector = virtualColumns.makeColumnValueSelector("expr", null);
final BaseLongColumnValueSelector longSelector = virtualColumns.makeColumnValueSelector("expr", null);
Assert.assertEquals(1L, objectSelector.getObject());
Assert.assertEquals("1", dimensionSelector.lookupName(dimensionSelector.getRow().get(0)));
Assert.assertEquals("0.5", extractionDimensionSelector.lookupName(extractionDimensionSelector.getRow().get(0)));
Assert.assertEquals(1.0f, floatSelector.getFloat(), 0.0f);
Assert.assertEquals(1L, longSelector.getLong());
}
use of org.apache.druid.segment.DimensionSelector in project druid by druid-io.
the class VirtualColumnsTest method testMakeSelectorsWithDotSupport.
@Test
public void testMakeSelectorsWithDotSupport() {
final VirtualColumns virtualColumns = makeVirtualColumns();
final BaseObjectColumnValueSelector objectSelector = virtualColumns.makeColumnValueSelector("foo.5", null);
final DimensionSelector dimensionSelector = virtualColumns.makeDimensionSelector(new DefaultDimensionSpec("foo.5", "x"), null);
final BaseFloatColumnValueSelector floatSelector = virtualColumns.makeColumnValueSelector("foo.5", null);
final BaseLongColumnValueSelector longSelector = virtualColumns.makeColumnValueSelector("foo.5", null);
Assert.assertEquals(5L, objectSelector.getObject());
Assert.assertEquals("5", dimensionSelector.lookupName(dimensionSelector.getRow().get(0)));
Assert.assertEquals(5.0f, floatSelector.getFloat(), 0.0f);
Assert.assertEquals(5L, longSelector.getLong());
}
use of org.apache.druid.segment.DimensionSelector in project druid by druid-io.
the class ExpressionSelectorsTest method test_single_value_string_bindings.
@Test
public void test_single_value_string_bindings() {
final String columnName = "string3";
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();
ExpressionPlan plan = ExpressionPlanner.plan(adapter, Parser.parse("\"string3\"", TestExprMacroTable.INSTANCE));
ExpressionPlan plan2 = ExpressionPlanner.plan(adapter, Parser.parse("concat(\"string3\", 'foo')", TestExprMacroTable.INSTANCE));
Expr.ObjectBinding bindings = ExpressionSelectors.createBindings(factory, plan);
Expr.ObjectBinding bindings2 = ExpressionSelectors.createBindings(factory, plan2);
DimensionSelector dimSelector = factory.makeDimensionSelector(DefaultDimensionSpec.of(columnName));
ColumnValueSelector valueSelector = factory.makeColumnValueSelector(columnName);
// realtime index needs to handle as multi-value in case any new values are added during processing
final boolean isMultiVal = factory.getColumnCapabilities(columnName) == null || factory.getColumnCapabilities(columnName).hasMultipleValues().isMaybeTrue();
while (!cursor.isDone()) {
Object dimSelectorVal = dimSelector.getObject();
Object valueSelectorVal = valueSelector.getObject();
Object bindingVal = bindings.get(columnName);
Object bindingVal2 = bindings2.get(columnName);
if (dimSelectorVal == null) {
Assert.assertNull(dimSelectorVal);
Assert.assertNull(valueSelectorVal);
Assert.assertNull(bindingVal);
if (isMultiVal) {
Assert.assertNull(((Object[]) bindingVal2)[0]);
} else {
Assert.assertNull(bindingVal2);
}
} else {
if (isMultiVal) {
Assert.assertEquals(dimSelectorVal, ((Object[]) bindingVal)[0]);
Assert.assertEquals(valueSelectorVal, ((Object[]) bindingVal)[0]);
Assert.assertEquals(dimSelectorVal, ((Object[]) bindingVal2)[0]);
Assert.assertEquals(valueSelectorVal, ((Object[]) bindingVal2)[0]);
} else {
Assert.assertEquals(dimSelectorVal, bindingVal);
Assert.assertEquals(valueSelectorVal, bindingVal);
Assert.assertEquals(dimSelectorVal, bindingVal2);
Assert.assertEquals(valueSelectorVal, bindingVal2);
}
}
cursor.advance();
}
}
}
}
use of org.apache.druid.segment.DimensionSelector in project druid by druid-io.
the class LookupJoinMatcherTest method verifyMatch.
private void verifyMatch(String expectedKey, String expectedValue) {
DimensionSelector selector = target.getColumnSelectorFactory().makeDimensionSelector(DefaultDimensionSpec.of("k"));
Assert.assertEquals(-1, selector.getValueCardinality());
Assert.assertEquals(expectedKey, selector.lookupName(0));
Assert.assertEquals(expectedKey, selector.lookupName(0));
Assert.assertNull(selector.idLookup());
selector = target.getColumnSelectorFactory().makeDimensionSelector(DefaultDimensionSpec.of("v"));
Assert.assertEquals(-1, selector.getValueCardinality());
Assert.assertEquals(expectedValue, selector.lookupName(0));
Assert.assertEquals(expectedValue, selector.lookupName(0));
Assert.assertNull(selector.idLookup());
}
use of org.apache.druid.segment.DimensionSelector in project druid by druid-io.
the class TimeExtractionTopNAlgorithm method scanAndAggregate.
@Override
protected long scanAndAggregate(TopNParams params, int[] dimValSelector, Map<Comparable<?>, Aggregator[]> aggregatesStore) {
final Cursor cursor = params.getCursor();
final DimensionSelector dimSelector = params.getDimSelector();
long processedRows = 0;
while (!cursor.isDone()) {
final Comparable<?> key = dimensionValueConverter.apply(dimSelector.lookupName(dimSelector.getRow().get(0)));
Aggregator[] theAggregators = aggregatesStore.computeIfAbsent(key, k -> makeAggregators(cursor, query.getAggregatorSpecs()));
for (Aggregator aggregator : theAggregators) {
aggregator.aggregate();
}
cursor.advance();
processedRows++;
}
return processedRows;
}
Aggregations