Search in sources :

Example 21 with DimensionSelector

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());
}
Also used : BaseFloatColumnValueSelector(org.apache.druid.segment.BaseFloatColumnValueSelector) DimensionSelector(org.apache.druid.segment.DimensionSelector) BucketExtractionFn(org.apache.druid.query.extraction.BucketExtractionFn) BaseObjectColumnValueSelector(org.apache.druid.segment.BaseObjectColumnValueSelector) BaseLongColumnValueSelector(org.apache.druid.segment.BaseLongColumnValueSelector) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) VirtualColumns(org.apache.druid.segment.VirtualColumns) ExtractionDimensionSpec(org.apache.druid.query.dimension.ExtractionDimensionSpec) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 22 with DimensionSelector

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());
}
Also used : BaseFloatColumnValueSelector(org.apache.druid.segment.BaseFloatColumnValueSelector) DimensionSelector(org.apache.druid.segment.DimensionSelector) BaseObjectColumnValueSelector(org.apache.druid.segment.BaseObjectColumnValueSelector) BaseLongColumnValueSelector(org.apache.druid.segment.BaseLongColumnValueSelector) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) VirtualColumns(org.apache.druid.segment.VirtualColumns) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 23 with DimensionSelector

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();
            }
        }
    }
}
Also used : BaseSingleValueDimensionSelector(org.apache.druid.segment.BaseSingleValueDimensionSelector) DimensionSelector(org.apache.druid.segment.DimensionSelector) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) 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) Expr(org.apache.druid.math.expr.Expr) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 24 with DimensionSelector

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());
}
Also used : DimensionSelector(org.apache.druid.segment.DimensionSelector)

Example 25 with DimensionSelector

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;
}
Also used : DimensionSelector(org.apache.druid.segment.DimensionSelector) Aggregator(org.apache.druid.query.aggregation.Aggregator) Cursor(org.apache.druid.segment.Cursor)

Aggregations

DimensionSelector (org.apache.druid.segment.DimensionSelector)66 Test (org.junit.Test)36 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)31 Cursor (org.apache.druid.segment.Cursor)24 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)24 IndexedInts (org.apache.druid.segment.data.IndexedInts)22 List (java.util.List)14 VirtualColumns (org.apache.druid.segment.VirtualColumns)14 ImmutableList (com.google.common.collect.ImmutableList)12 StorageAdapter (org.apache.druid.segment.StorageAdapter)12 ArrayList (java.util.ArrayList)11 Intervals (org.apache.druid.java.util.common.Intervals)11 Granularities (org.apache.druid.java.util.common.granularity.Granularities)11 Sequence (org.apache.druid.java.util.common.guava.Sequence)11 QueryableIndexStorageAdapter (org.apache.druid.segment.QueryableIndexStorageAdapter)11 NullHandling (org.apache.druid.common.config.NullHandling)10 ColumnSelectorFactory (org.apache.druid.segment.ColumnSelectorFactory)9 DataSegment (org.apache.druid.timeline.DataSegment)9 ConstantDimensionSelector (org.apache.druid.segment.ConstantDimensionSelector)8 ValueMatcher (org.apache.druid.query.filter.ValueMatcher)7