Search in sources :

Example 46 with DimensionSelector

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

the class DictionaryBuildingStringGroupByColumnSelectorStrategy method getOnlyValue.

@Override
public Object getOnlyValue(ColumnValueSelector selector) {
    final DimensionSelector dimSelector = (DimensionSelector) selector;
    final IndexedInts row = dimSelector.getRow();
    Preconditions.checkState(row.size() < 2, "Not supported for multi-value dimensions");
    if (row.size() == 0) {
        return GROUP_BY_MISSING_VALUE;
    }
    final String value = dimSelector.lookupName(row.get(0));
    final int dictId = reverseDictionary.getInt(value);
    if (dictId < 0) {
        dictionary.add(value);
        reverseDictionary.put(value, nextId);
        return nextId++;
    } else {
        return dictId;
    }
}
Also used : DimensionSelector(org.apache.druid.segment.DimensionSelector) IndexedInts(org.apache.druid.segment.data.IndexedInts) ArrayBasedIndexedInts(org.apache.druid.segment.data.ArrayBasedIndexedInts)

Example 47 with DimensionSelector

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

the class TopNMetricSpecOptimizationsTest method testShouldNotOptimizeLexicographic.

@Test
public void testShouldNotOptimizeLexicographic() {
    // query interval is smaller than segment interval, no filters, can ignoreAfterThreshold
    int cardinality = 1234;
    int threshold = 4;
    TopNQuery query = new TopNQueryBuilder().dataSource(QueryRunnerTestHelper.DATA_SOURCE).granularity(QueryRunnerTestHelper.ALL_GRAN).dimension(QueryRunnerTestHelper.MARKET_DIMENSION).metric(QueryRunnerTestHelper.INDEX_METRIC).threshold(threshold).intervals("2018-05-30T00:00:00Z/2018-05-30T01:00:00Z").aggregators(AGGS).postAggregators(QueryRunnerTestHelper.ADD_ROWS_INDEX_CONSTANT).build();
    StorageAdapter adapter = makeFakeStorageAdapter("2018-05-30T00:00:00Z", "2018-05-31T00:00:00Z", cardinality);
    DimensionSelector dimSelector = makeFakeDimSelector(cardinality);
    BaseTopNAlgorithm.AggregatorArrayProvider arrayProviderToTest = new BaseTopNAlgorithm.AggregatorArrayProvider(dimSelector, query, cardinality, adapter);
    arrayProviderToTest.ignoreAfterThreshold();
    Pair<Integer, Integer> thePair = arrayProviderToTest.computeStartEnd(cardinality);
    Assert.assertEquals(new Integer(0), thePair.lhs);
    Assert.assertEquals(new Integer(cardinality), thePair.rhs);
}
Also used : DimensionSelector(org.apache.druid.segment.DimensionSelector) StorageAdapter(org.apache.druid.segment.StorageAdapter) Test(org.junit.Test)

Example 48 with DimensionSelector

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

the class TopNMetricSpecOptimizationsTest method testAgainShouldNotOptimizeLexicographic.

@Test
public void testAgainShouldNotOptimizeLexicographic() {
    // query interval is larger than segment interval, no filters, can NOT ignoreAfterThreshold
    int cardinality = 1234;
    int threshold = 4;
    TopNQuery query = new TopNQueryBuilder().dataSource(QueryRunnerTestHelper.DATA_SOURCE).granularity(QueryRunnerTestHelper.ALL_GRAN).dimension(QueryRunnerTestHelper.MARKET_DIMENSION).metric(QueryRunnerTestHelper.INDEX_METRIC).threshold(threshold).intervals("2018-05-30T00:00:00Z/2018-05-31T00:00:00Z").aggregators(AGGS).postAggregators(QueryRunnerTestHelper.ADD_ROWS_INDEX_CONSTANT).build();
    StorageAdapter adapter = makeFakeStorageAdapter("2018-05-30T00:00:00Z", "2018-05-30T01:00:00Z", cardinality);
    DimensionSelector dimSelector = makeFakeDimSelector(cardinality);
    BaseTopNAlgorithm.AggregatorArrayProvider arrayProviderToTest = new BaseTopNAlgorithm.AggregatorArrayProvider(dimSelector, query, cardinality, adapter);
    Pair<Integer, Integer> thePair = arrayProviderToTest.computeStartEnd(cardinality);
    Assert.assertEquals(new Integer(0), thePair.lhs);
    Assert.assertEquals(new Integer(cardinality), thePair.rhs);
}
Also used : DimensionSelector(org.apache.druid.segment.DimensionSelector) StorageAdapter(org.apache.druid.segment.StorageAdapter) Test(org.junit.Test)

Example 49 with DimensionSelector

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

the class IncrementalIndexStorageAdapterTest method testResetSanity.

@Test
public void testResetSanity() throws IOException {
    IncrementalIndex index = indexCreator.createIndex();
    DateTime t = DateTimes.nowUtc();
    Interval interval = new Interval(t.minusMinutes(1), t.plusMinutes(1));
    index.add(new MapBasedInputRow(t.minus(1).getMillis(), Collections.singletonList("billy"), ImmutableMap.of("billy", "hi")));
    index.add(new MapBasedInputRow(t.minus(1).getMillis(), Collections.singletonList("sally"), ImmutableMap.of("sally", "bo")));
    IncrementalIndexStorageAdapter adapter = new IncrementalIndexStorageAdapter(index);
    for (boolean descending : Arrays.asList(false, true)) {
        Sequence<Cursor> cursorSequence = adapter.makeCursors(new SelectorFilter("sally", "bo"), interval, VirtualColumns.EMPTY, Granularities.NONE, descending, null);
        Cursor cursor = cursorSequence.limit(1).toList().get(0);
        DimensionSelector dimSelector;
        dimSelector = cursor.getColumnSelectorFactory().makeDimensionSelector(new DefaultDimensionSpec("sally", "sally"));
        Assert.assertEquals("bo", dimSelector.lookupName(dimSelector.getRow().get(0)));
        index.add(new MapBasedInputRow(t.minus(1).getMillis(), Collections.singletonList("sally"), ImmutableMap.of("sally", "ah")));
        // Cursor reset should not be affected by out of order values
        cursor.reset();
        dimSelector = cursor.getColumnSelectorFactory().makeDimensionSelector(new DefaultDimensionSpec("sally", "sally"));
        Assert.assertEquals("bo", dimSelector.lookupName(dimSelector.getRow().get(0)));
    }
}
Also used : SelectorFilter(org.apache.druid.segment.filter.SelectorFilter) DimensionSelector(org.apache.druid.segment.DimensionSelector) MapBasedInputRow(org.apache.druid.data.input.MapBasedInputRow) Cursor(org.apache.druid.segment.Cursor) DateTime(org.joda.time.DateTime) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) Interval(org.joda.time.Interval) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 50 with DimensionSelector

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

the class IncrementalIndexStorageAdapterTest method testCursorDictionaryRaceConditionFix.

@Test
public void testCursorDictionaryRaceConditionFix() throws Exception {
    // Tests the dictionary ID race condition bug described at https://github.com/apache/druid/pull/6340
    final IncrementalIndex index = indexCreator.createIndex();
    final long timestamp = System.currentTimeMillis();
    for (int i = 0; i < 5; i++) {
        index.add(new MapBasedInputRow(timestamp, Collections.singletonList("billy"), ImmutableMap.of("billy", "v1" + i)));
    }
    final StorageAdapter sa = new IncrementalIndexStorageAdapter(index);
    Sequence<Cursor> cursors = sa.makeCursors(new DictionaryRaceTestFilter(index, timestamp), Intervals.utc(timestamp - 60_000, timestamp + 60_000), VirtualColumns.EMPTY, Granularities.ALL, false, null);
    final AtomicInteger assertCursorsNotEmpty = new AtomicInteger(0);
    cursors.map(cursor -> {
        DimensionSelector dimSelector = cursor.getColumnSelectorFactory().makeDimensionSelector(new DefaultDimensionSpec("billy", "billy"));
        int cardinality = dimSelector.getValueCardinality();
        int rowNumInCursor = 0;
        while (!cursor.isDone()) {
            IndexedInts row = dimSelector.getRow();
            row.forEach(i -> Assert.assertTrue(i < cardinality));
            cursor.advance();
            rowNumInCursor++;
        }
        Assert.assertEquals(5, rowNumInCursor);
        assertCursorsNotEmpty.incrementAndGet();
        return null;
    }).toList();
    Assert.assertEquals(1, assertCursorsNotEmpty.get());
}
Also used : GroupByQueryEngine(org.apache.druid.query.groupby.GroupByQueryEngine) Arrays(java.util.Arrays) MapBasedRow(org.apache.druid.data.input.MapBasedRow) IndexedInts(org.apache.druid.segment.data.IndexedInts) StorageAdapter(org.apache.druid.segment.StorageAdapter) ByteBuffer(java.nio.ByteBuffer) Row(org.apache.druid.data.input.Row) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) ColumnSelectorFactory(org.apache.druid.segment.ColumnSelectorFactory) LongSumAggregatorFactory(org.apache.druid.query.aggregation.LongSumAggregatorFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Parameterized(org.junit.runners.Parameterized) DateTimes(org.apache.druid.java.util.common.DateTimes) Sequence(org.apache.druid.java.util.common.guava.Sequence) TopNResultValue(org.apache.druid.query.topn.TopNResultValue) SelectorFilter(org.apache.druid.segment.filter.SelectorFilter) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) JavaScriptAggregatorFactory(org.apache.druid.query.aggregation.JavaScriptAggregatorFactory) Set(java.util.Set) List(java.util.List) CloseableStupidPool(org.apache.druid.collections.CloseableStupidPool) Predicate(com.google.common.base.Predicate) TopNQueryEngine(org.apache.druid.query.topn.TopNQueryEngine) BitmapIndexSelector(org.apache.druid.query.filter.BitmapIndexSelector) Iterables(com.google.common.collect.Iterables) Intervals(org.apache.druid.java.util.common.Intervals) DruidDoublePredicate(org.apache.druid.query.filter.DruidDoublePredicate) DimFilters(org.apache.druid.query.filter.DimFilters) RunWith(org.junit.runner.RunWith) MapBasedInputRow(org.apache.druid.data.input.MapBasedInputRow) JavaScriptConfig(org.apache.druid.js.JavaScriptConfig) DruidLongPredicate(org.apache.druid.query.filter.DruidLongPredicate) Interval(org.joda.time.Interval) Lists(com.google.common.collect.Lists) Predicates(com.google.common.base.Predicates) Suppliers(com.google.common.base.Suppliers) GroupByQuery(org.apache.druid.query.groupby.GroupByQuery) DimensionSelector(org.apache.druid.segment.DimensionSelector) CountAggregatorFactory(org.apache.druid.query.aggregation.CountAggregatorFactory) DruidFloatPredicate(org.apache.druid.query.filter.DruidFloatPredicate) ValueMatcher(org.apache.druid.query.filter.ValueMatcher) BitmapResultFactory(org.apache.druid.query.BitmapResultFactory) VirtualColumns(org.apache.druid.segment.VirtualColumns) TopNQueryBuilder(org.apache.druid.query.topn.TopNQueryBuilder) GroupByQueryConfig(org.apache.druid.query.groupby.GroupByQueryConfig) DateTime(org.joda.time.DateTime) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test) IOException(java.io.IOException) ColumnSelector(org.apache.druid.segment.ColumnSelector) Granularities(org.apache.druid.java.util.common.granularity.Granularities) Result(org.apache.druid.query.Result) Rule(org.junit.Rule) Cursor(org.apache.druid.segment.Cursor) NullHandling(org.apache.druid.common.config.NullHandling) DruidPredicateFactory(org.apache.druid.query.filter.DruidPredicateFactory) CloserRule(org.apache.druid.segment.CloserRule) Assert(org.junit.Assert) Filters(org.apache.druid.segment.filter.Filters) Collections(java.util.Collections) Filter(org.apache.druid.query.filter.Filter) DimensionSelector(org.apache.druid.segment.DimensionSelector) StorageAdapter(org.apache.druid.segment.StorageAdapter) Cursor(org.apache.druid.segment.Cursor) DefaultDimensionSpec(org.apache.druid.query.dimension.DefaultDimensionSpec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexedInts(org.apache.druid.segment.data.IndexedInts) MapBasedInputRow(org.apache.druid.data.input.MapBasedInputRow) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

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