Search in sources :

Example 36 with IndexedInts

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

the class DictionaryBuildingStringGroupByColumnSelectorStrategy method initColumnValues.

@Override
public void initColumnValues(ColumnValueSelector selector, int columnIndex, Object[] valuess) {
    final DimensionSelector dimSelector = (DimensionSelector) selector;
    final IndexedInts row = dimSelector.getRow();
    ArrayBasedIndexedInts newRow = (ArrayBasedIndexedInts) valuess[columnIndex];
    if (newRow == null) {
        newRow = new ArrayBasedIndexedInts();
        valuess[columnIndex] = newRow;
    }
    int rowSize = row.size();
    newRow.ensureSize(rowSize);
    for (int i = 0; i < rowSize; i++) {
        final String value = dimSelector.lookupName(row.get(i));
        final int dictId = reverseDictionary.getInt(value);
        if (dictId < 0) {
            dictionary.add(value);
            reverseDictionary.put(value, nextId);
            newRow.setValue(i, nextId);
            nextId++;
        } else {
            newRow.setValue(i, dictId);
        }
    }
    newRow.setSize(rowSize);
}
Also used : DimensionSelector(org.apache.druid.segment.DimensionSelector) ArrayBasedIndexedInts(org.apache.druid.segment.data.ArrayBasedIndexedInts) IndexedInts(org.apache.druid.segment.data.IndexedInts) ArrayBasedIndexedInts(org.apache.druid.segment.data.ArrayBasedIndexedInts)

Example 37 with IndexedInts

use of org.apache.druid.segment.data.IndexedInts 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 38 with IndexedInts

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

the class StringAnyVectorAggregatorTest method aggregateBatchWithRowsShouldAggregateAllRows.

@Test
public void aggregateBatchWithRowsShouldAggregateAllRows() {
    int[] positions = new int[] { 0, 43, 100 };
    int positionOffset = 2;
    int[] rows = new int[] { 2, 1, 0 };
    clearBufferForPositions(positionOffset, positions);
    multiValueTarget.aggregate(buf, 3, positions, rows, positionOffset);
    for (int i = 0; i < positions.length; i++) {
        int position = positions[i] + positionOffset;
        int row = rows[i];
        IndexedInts rowIndex = MULTI_VALUE_ROWS[row];
        if (rowIndex.size() == 0) {
            Assert.assertNull(multiValueTarget.get(buf, position));
        } else {
            Assert.assertEquals(multiValueSelector.lookupName(rowIndex.get(0)), multiValueTarget.get(buf, position));
        }
    }
}
Also used : IndexedInts(org.apache.druid.segment.data.IndexedInts) ArrayBasedIndexedInts(org.apache.druid.segment.data.ArrayBasedIndexedInts) InitializedNullHandlingTest(org.apache.druid.testing.InitializedNullHandlingTest) Test(org.junit.Test)

Example 39 with IndexedInts

use of org.apache.druid.segment.data.IndexedInts 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)

Example 40 with IndexedInts

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

the class IncrementalIndexStorageAdapterTest method testCursoringAndIndexUpdationInterleaving.

@Test
public void testCursoringAndIndexUpdationInterleaving() throws Exception {
    final IncrementalIndex index = indexCreator.createIndex();
    final long timestamp = System.currentTimeMillis();
    for (int i = 0; i < 2; 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(null, 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();
        // index gets more rows at this point, while other thread is iterating over the cursor
        try {
            for (int i = 0; i < 1; i++) {
                index.add(new MapBasedInputRow(timestamp, Collections.singletonList("billy"), ImmutableMap.of("billy", "v2" + i)));
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        int rowNumInCursor = 0;
        // and then, cursoring continues in the other thread
        while (!cursor.isDone()) {
            IndexedInts row = dimSelector.getRow();
            row.forEach(i -> Assert.assertTrue(i < cardinality));
            cursor.advance();
            rowNumInCursor++;
        }
        Assert.assertEquals(2, 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) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) 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

IndexedInts (org.apache.druid.segment.data.IndexedInts)63 DimensionSelector (org.apache.druid.segment.DimensionSelector)22 ValueMatcher (org.apache.druid.query.filter.ValueMatcher)14 Test (org.junit.Test)13 RuntimeShapeInspector (org.apache.druid.query.monomorphicprocessing.RuntimeShapeInspector)12 ArrayBasedIndexedInts (org.apache.druid.segment.data.ArrayBasedIndexedInts)12 Cursor (org.apache.druid.segment.Cursor)10 DefaultDimensionSpec (org.apache.druid.query.dimension.DefaultDimensionSpec)8 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)7 Predicate (com.google.common.base.Predicate)6 ByteBuffer (java.nio.ByteBuffer)6 Nullable (javax.annotation.Nullable)6 ColumnSelectorFactory (org.apache.druid.segment.ColumnSelectorFactory)6 BooleanValueMatcher (org.apache.druid.segment.filter.BooleanValueMatcher)6 List (java.util.List)5 Predicates (com.google.common.base.Predicates)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Arrays (java.util.Arrays)4 NullHandling (org.apache.druid.common.config.NullHandling)4 MapBasedInputRow (org.apache.druid.data.input.MapBasedInputRow)4