Search in sources :

Example 11 with SerializablePairLongString

use of org.apache.druid.query.aggregation.SerializablePairLongString in project druid by druid-io.

the class StringFirstBufferAggregatorTest method testNullBufferAggregate.

@Test
public void testNullBufferAggregate() {
    final long[] timestamps = { 2222L, 1111L, 3333L, 4444L, 5555L };
    final String[] strings = { null, "AAAA", "BBBB", "DDDD", "EEEE" };
    Integer maxStringBytes = 1024;
    TestLongColumnSelector longColumnSelector = new TestLongColumnSelector(timestamps);
    TestObjectColumnSelector<String> objectColumnSelector = new TestObjectColumnSelector<>(strings);
    StringFirstAggregatorFactory factory = new StringFirstAggregatorFactory("billy", "billy", null, maxStringBytes);
    StringFirstBufferAggregator agg = new StringFirstBufferAggregator(longColumnSelector, objectColumnSelector, maxStringBytes, false);
    ByteBuffer buf = ByteBuffer.allocate(factory.getMaxIntermediateSize());
    int position = 0;
    agg.init(buf, position);
    // noinspection ForLoopReplaceableByForEach
    for (int i = 0; i < timestamps.length; i++) {
        aggregateBuffer(longColumnSelector, objectColumnSelector, agg, buf, position);
    }
    SerializablePairLongString sp = ((SerializablePairLongString) agg.get(buf, position));
    Assert.assertEquals("expected last string value", strings[1], sp.rhs);
    Assert.assertEquals("last string timestamp is the biggest", new Long(timestamps[1]), sp.lhs);
}
Also used : TestObjectColumnSelector(org.apache.druid.query.aggregation.TestObjectColumnSelector) SerializablePairLongString(org.apache.druid.query.aggregation.SerializablePairLongString) ByteBuffer(java.nio.ByteBuffer) TestLongColumnSelector(org.apache.druid.query.aggregation.TestLongColumnSelector) SerializablePairLongString(org.apache.druid.query.aggregation.SerializablePairLongString) Test(org.junit.Test)

Example 12 with SerializablePairLongString

use of org.apache.druid.query.aggregation.SerializablePairLongString in project druid by druid-io.

the class StringFirstAggregator method aggregate.

@Override
public void aggregate() {
    if (timeSelector.isNull()) {
        return;
    }
    if (needsFoldCheck) {
        // Less efficient code path when folding is a possibility (we must read the value selector first just in case
        // it's a foldable object).
        final SerializablePairLongString inPair = StringFirstLastUtils.readPairFromSelectors(timeSelector, valueSelector);
        if (inPair != null && inPair.lhs < firstTime) {
            firstTime = inPair.lhs;
            firstValue = StringUtils.fastLooseChop(inPair.rhs, maxStringBytes);
        }
    } else {
        final long time = timeSelector.getLong();
        if (time < firstTime) {
            final String value = DimensionHandlerUtils.convertObjectToString(valueSelector.getObject());
            firstTime = time;
            firstValue = StringUtils.fastLooseChop(value, maxStringBytes);
        }
    }
}
Also used : SerializablePairLongString(org.apache.druid.query.aggregation.SerializablePairLongString) SerializablePairLongString(org.apache.druid.query.aggregation.SerializablePairLongString)

Example 13 with SerializablePairLongString

use of org.apache.druid.query.aggregation.SerializablePairLongString in project druid by druid-io.

the class StringFirstBufferAggregator method aggregate.

@Override
public void aggregate(ByteBuffer buf, int position) {
    if (timeSelector.isNull()) {
        return;
    }
    if (needsFoldCheck) {
        // Less efficient code path when folding is a possibility (we must read the value selector first just in case
        // it's a foldable object).
        final SerializablePairLongString inPair = StringFirstLastUtils.readPairFromSelectors(timeSelector, valueSelector);
        if (inPair != null) {
            final long firstTime = buf.getLong(position);
            if (inPair.lhs < firstTime) {
                StringFirstLastUtils.writePair(buf, position, new SerializablePairLongString(inPair.lhs, inPair.rhs), maxStringBytes);
            }
        }
    } else {
        final long time = timeSelector.getLong();
        final long firstTime = buf.getLong(position);
        if (time < firstTime) {
            final String value = DimensionHandlerUtils.convertObjectToString(valueSelector.getObject());
            StringFirstLastUtils.writePair(buf, position, new SerializablePairLongString(time, value), maxStringBytes);
        }
    }
}
Also used : SerializablePairLongString(org.apache.druid.query.aggregation.SerializablePairLongString) SerializablePairLongString(org.apache.druid.query.aggregation.SerializablePairLongString)

Example 14 with SerializablePairLongString

use of org.apache.druid.query.aggregation.SerializablePairLongString in project druid by druid-io.

the class StringFirstLastUtils method readPair.

public static SerializablePairLongString readPair(final ByteBuffer buf, final int position) {
    ByteBuffer copyBuffer = buf.duplicate();
    copyBuffer.position(position);
    Long timeValue = copyBuffer.getLong();
    int stringSizeBytes = copyBuffer.getInt();
    if (stringSizeBytes >= 0) {
        byte[] valueBytes = new byte[stringSizeBytes];
        copyBuffer.get(valueBytes, 0, stringSizeBytes);
        return new SerializablePairLongString(timeValue, StringUtils.fromUtf8(valueBytes));
    } else {
        return new SerializablePairLongString(timeValue, null);
    }
}
Also used : SerializablePairLongString(org.apache.druid.query.aggregation.SerializablePairLongString) ByteBuffer(java.nio.ByteBuffer)

Example 15 with SerializablePairLongString

use of org.apache.druid.query.aggregation.SerializablePairLongString in project druid by druid-io.

the class StringFirstLastUtils method readPairFromSelectors.

@Nullable
public static SerializablePairLongString readPairFromSelectors(final BaseLongColumnValueSelector timeSelector, final BaseObjectColumnValueSelector<?> valueSelector) {
    final long time;
    final String string;
    // Need to read this first (before time), just in case it's a SerializablePairLongString (we don't know; it's
    // detected at query time).
    final Object object = valueSelector.getObject();
    if (object instanceof SerializablePairLongString) {
        final SerializablePairLongString pair = (SerializablePairLongString) object;
        time = pair.lhs;
        string = pair.rhs;
    } else if (object != null) {
        time = timeSelector.getLong();
        string = DimensionHandlerUtils.convertObjectToString(object);
    } else {
        // Don't aggregate nulls.
        return null;
    }
    return new SerializablePairLongString(time, string);
}
Also used : SerializablePairLongString(org.apache.druid.query.aggregation.SerializablePairLongString) SerializablePairLongString(org.apache.druid.query.aggregation.SerializablePairLongString) Nullable(javax.annotation.Nullable)

Aggregations

SerializablePairLongString (org.apache.druid.query.aggregation.SerializablePairLongString)23 Test (org.junit.Test)17 ByteBuffer (java.nio.ByteBuffer)11 TestLongColumnSelector (org.apache.druid.query.aggregation.TestLongColumnSelector)8 TestObjectColumnSelector (org.apache.druid.query.aggregation.TestObjectColumnSelector)8 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)4 Result (org.apache.druid.query.Result)3 TimeseriesQuery (org.apache.druid.query.timeseries.TimeseriesQuery)2 TimeseriesQueryEngine (org.apache.druid.query.timeseries.TimeseriesQueryEngine)2 TimeseriesResultValue (org.apache.druid.query.timeseries.TimeseriesResultValue)2 QueryableIndexStorageAdapter (org.apache.druid.segment.QueryableIndexStorageAdapter)2 IncrementalIndexStorageAdapter (org.apache.druid.segment.incremental.IncrementalIndexStorageAdapter)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Nullable (javax.annotation.Nullable)1 TableDataSource (org.apache.druid.query.TableDataSource)1 CountAggregatorFactory (org.apache.druid.query.aggregation.CountAggregatorFactory)1 LongSumAggregatorFactory (org.apache.druid.query.aggregation.LongSumAggregatorFactory)1 StringLastAggregatorFactory (org.apache.druid.query.aggregation.last.StringLastAggregatorFactory)1 ConstantPostAggregator (org.apache.druid.query.aggregation.post.ConstantPostAggregator)1 MultipleIntervalSegmentSpec (org.apache.druid.query.spec.MultipleIntervalSegmentSpec)1