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);
}
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);
}
}
}
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);
}
}
}
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);
}
}
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);
}
Aggregations