Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public final class AggregateMetricDoubleArrayBlock extends AbstractNonThreadSafeRefCounted implements AggregateMetricDoubleBlock {
Expand Down Expand Up @@ -310,4 +311,13 @@ public Block getMetricBlock(int index) {
}
throw new UnsupportedOperationException("Received an index (" + index + ") outside of range for AggregateMetricDoubleBlock.");
}

@Override
public String toString() {
String valuesString = Stream.of(AggregateMetricDoubleBlockBuilder.Metric.values())
.map(metric -> metric.getLabel() + "=" + getMetricBlock(metric.getIndex()))
.collect(Collectors.joining(", ", "[", "]"));

return getClass().getSimpleName() + valuesString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ protected int elementSize() {
throw new UnsupportedOperationException("Not available on aggregate_metric_double");
}

@Override
public long estimatedBytes() {
return minBuilder.estimatedBytes() + maxBuilder.estimatedBytes() + sumBuilder.estimatedBytes() + countBuilder.estimatedBytes();
}

@Override
public AggregateMetricDoubleBlockBuilder copyFrom(Block b, int beginInclusive, int endExclusive) {
Block minBlock;
Expand All @@ -83,6 +88,35 @@ public AggregateMetricDoubleBlockBuilder copyFrom(Block b, int beginInclusive, i
return this;
}

public AggregateMetricDoubleBlockBuilder copyFrom(AggregateMetricDoubleBlock block, int position) {
if (block.isNull(position)) {
appendNull();
return this;
}

if (block.minBlock().isNull(position)) {
min().appendNull();
} else {
min().appendDouble(block.minBlock().getDouble(position));
}
if (block.maxBlock().isNull(position)) {
max().appendNull();
} else {
max().appendDouble(block.maxBlock().getDouble(position));
}
if (block.sumBlock().isNull(position)) {
sum().appendNull();
} else {
sum().appendDouble(block.sumBlock().getDouble(position));
}
if (block.countBlock().isNull(position)) {
count().appendNull();
} else {
count().appendInt(block.countBlock().getInt(position));
}
return this;
}

@Override
public AggregateMetricDoubleBlockBuilder appendNull() {
minBuilder.appendNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,30 +441,46 @@ public final AggregateMetricDoubleBlock newConstantAggregateMetricDoubleBlock(
int positions
) {
try (AggregateMetricDoubleBlockBuilder builder = newAggregateMetricDoubleBlockBuilder(positions)) {
if (value.min() != null) {
builder.min().appendDouble(value.min());
} else {
builder.min().appendNull();
}
if (value.max() != null) {
builder.max().appendDouble(value.max());
} else {
builder.max().appendNull();
}
if (value.sum() != null) {
builder.sum().appendDouble(value.sum());
} else {
builder.sum().appendNull();
}
if (value.count() != null) {
builder.count().appendInt(value.count());
} else {
builder.count().appendNull();
for (int i = 0; i < positions; i++) {
if (value.min() != null) {
builder.min().appendDouble(value.min());
} else {
builder.min().appendNull();
}
if (value.max() != null) {
builder.max().appendDouble(value.max());
} else {
builder.max().appendNull();
}
if (value.sum() != null) {
builder.sum().appendDouble(value.sum());
} else {
builder.sum().appendNull();
}
if (value.count() != null) {
builder.count().appendInt(value.count());
} else {
builder.count().appendNull();
}
}
return builder.build();
}
}

public final AggregateMetricDoubleBlock newAggregateMetricDoubleBlock(
double[] minValues,
double[] maxValues,
double[] sumValues,
int[] countValues,
int positions
) {
DoubleBlock min = newDoubleArrayVector(minValues, positions).asBlock();
DoubleBlock max = newDoubleArrayVector(maxValues, positions).asBlock();
DoubleBlock sum = newDoubleArrayVector(sumValues, positions).asBlock();
IntBlock count = newIntArrayVector(countValues, positions).asBlock();
return new AggregateMetricDoubleArrayBlock(min, max, sum, count);
}

/**
* Returns the maximum number of bytes that a Block should be backed by a primitive array before switching to using BigArrays.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public class AggregateMetricDoubleBlockEqualityTests extends ComputeTestCase {

static final BlockFactory blockFactory = TestBlockFactory.getNonBreakingInstance();

// TODO: Add additional tests

public void testEmptyBlock() {
// all these "empty" blocks should be equivalent
var partialMetricBuilder = blockFactory.newAggregateMetricDoubleBlockBuilder(0);
Expand Down Expand Up @@ -54,6 +52,198 @@ public void testEmptyBlock() {
Releasables.close(blocks);
}

public void testBlockEqualityRegularBlocks() {
// all these blocks should be equivalent
AggregateMetricDoubleBlockBuilder builder1 = blockFactory.newAggregateMetricDoubleBlockBuilder(3);
appendValues(builder1, 1.23, 68392.1, 99999.1, 5);
appendValues(builder1, 582.1, 10942, 209301.4, 25);
appendValues(builder1, 8952.564, 30921.23, 18592950.14, 1000);
AggregateMetricDoubleBlockBuilder builder2 = blockFactory.newAggregateMetricDoubleBlockBuilder(6);
appendValues(builder2, 1.23, 68392.1, 99999.1, 5);
appendValues(builder2, 1.23, 68392.1, 99999.1, 10);
appendValues(builder2, 582.1, 10942, 209301.4, 25);
appendValues(builder2, 999.1, 10942, 209301.4, 50);
appendValues(builder2, 10000.564, 30921.23, 18592950.14, 2000);
appendValues(builder2, 8952.564, 30921.23, 18592950.14, 1000);
AggregateMetricDoubleBlockBuilder builder3 = blockFactory.newAggregateMetricDoubleBlockBuilder(5);
builder3.appendNull();
builder3.appendNull();
appendValues(builder3, 1.23, 68392.1, 99999.1, 5);
appendValues(builder3, 582.1, 10942, 209301.4, 25);
appendValues(builder3, 8952.564, 30921.23, 18592950.14, 1000);
builder3.appendNull();

List<AggregateMetricDoubleBlock> blocks = List.of(
builder1.build(),
builder2.build().filter(0, 2, 5),
builder3.build().filter(2, 3, 4)
);
assertAllEquals(blocks);
}

public void testBlockEqualityConstantLikeBlocks() {
// all these blocks should be equivalent
AggregateMetricDoubleBlockBuilder builder1 = blockFactory.newAggregateMetricDoubleBlockBuilder(2);
appendValues(builder1, 12.3, 987.6, 4821.3, 6);
appendValues(builder1, 12.3, 987.6, 4821.3, 6);
AggregateMetricDoubleBlockBuilder builder2 = blockFactory.newAggregateMetricDoubleBlockBuilder(3);
appendValues(builder2, 12.3, 987.6, 4821.3, 6);
appendValues(builder2, 95.2, 10852.2, 20000.5, 5);
appendValues(builder2, 12.3, 987.6, 4821.3, 6);
appendValues(builder2, 1.1, 2.2, 3.3, 2);
DoubleBlock min1 = blockFactory.newDoubleBlockBuilder(4).appendDouble(12.3).appendDouble(12.3).build();
DoubleBlock max1 = blockFactory.newDoubleBlockBuilder(4).appendDouble(987.6).appendDouble(987.6).build();
DoubleBlock sum1 = blockFactory.newDoubleBlockBuilder(4).appendDouble(4821.3).appendDouble(4821.3).build();
IntBlock count1 = blockFactory.newIntBlockBuilder(4).appendInt(6).appendInt(6).build();
CompositeBlock compositeBlock1 = new CompositeBlock(new Block[] { min1, max1, sum1, count1 });
DoubleBlock min2 = blockFactory.newDoubleBlockBuilder(4)
.appendDouble(591.1)
.appendDouble(11.1)
.appendDouble(12.3)
.appendDouble(12.3)
.build();
DoubleBlock max2 = blockFactory.newDoubleBlockBuilder(4)
.appendDouble(198441.1)
.appendDouble(89235982.1)
.appendDouble(987.6)
.appendDouble(987.6)
.build();
DoubleBlock sum2 = blockFactory.newDoubleBlockBuilder(4)
.appendDouble(13498198.2)
.appendDouble(4901245982.1)
.appendDouble(4821.3)
.appendDouble(4821.3)
.build();
IntBlock count2 = blockFactory.newIntBlockBuilder(4).appendInt(100).appendInt(200).appendInt(6).appendInt(6).build();
CompositeBlock compositeBlock2 = new CompositeBlock(new Block[] { min2, max2, sum2, count2 });

List<AggregateMetricDoubleBlock> moreBlocks = List.of(
builder1.build(),
builder2.build().filter(0, 2),
AggregateMetricDoubleArrayBlock.fromCompositeBlock(compositeBlock1),
AggregateMetricDoubleArrayBlock.fromCompositeBlock(compositeBlock2).filter(2, 3),
blockFactory.newConstantAggregateMetricDoubleBlock(
new AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral(12.3, 987.6, 4821.3, 6),
4
).filter(1, 3),
blockFactory.newConstantAggregateMetricDoubleBlock(
new AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral(12.3, 987.6, 4821.3, 6),
2
)
);
assertAllEquals(moreBlocks);
}

public void testBlockEqualityPartialSubmetrics() {
// blocks with partial submetrics appended in different orders (column vs row)
AggregateMetricDoubleBlockBuilder builder1 = blockFactory.newAggregateMetricDoubleBlockBuilder(3);
builder1.min().appendDouble(1.23).appendDouble(582.1).appendNull();
builder1.max().appendDouble(68392.1).appendNull();
builder1.max().appendDouble(30921.23);
builder1.sum().appendNull();
builder1.sum().appendDouble(99999.1).appendNull();
builder1.count().appendNull().appendNull();
builder1.count().appendInt(1000);

AggregateMetricDoubleBlockBuilder builder2 = blockFactory.newAggregateMetricDoubleBlockBuilder(6);
builder2.appendNull();
builder2.appendNull();
builder2.min().appendDouble(1.23);
builder2.max().appendDouble(68392.1);
builder2.sum().appendNull();
builder2.count().appendNull();
builder2.min().appendDouble(582.1);
builder2.max().appendNull();
builder2.sum().appendDouble(99999.1);
builder2.count().appendNull();
builder2.appendNull();
builder2.min().appendNull();
builder2.max().appendDouble(30921.23);
builder2.sum().appendNull();
builder2.count().appendInt(1000);

List<AggregateMetricDoubleBlock> evenMoreBlocks = List.of(builder1.build(), builder2.build().filter(2, 3, 5));
assertAllEquals(evenMoreBlocks);
}

public void testBlockInequality() {
// all these blocks should NOT be equivalent
AggregateMetricDoubleBlockBuilder builder1 = blockFactory.newAggregateMetricDoubleBlockBuilder(3);
appendValues(builder1, 1.1, 6.1, 7.2, 2);
builder1.appendNull();
appendValues(builder1, 1.3, 6.3, 11.9, 3);
// same values as builder1, in a different order
AggregateMetricDoubleBlockBuilder builder2 = blockFactory.newAggregateMetricDoubleBlockBuilder(3);
appendValues(builder2, 1.3, 6.3, 11.9, 3);
builder2.appendNull();
appendValues(builder2, 1.1, 6.1, 7.2, 2);
// first 2/3 values match builder1, last 2/3 values match builder2
AggregateMetricDoubleBlockBuilder builder3 = blockFactory.newAggregateMetricDoubleBlockBuilder(3);
appendValues(builder3, 1.1, 6.1, 7.2, 2);
builder3.appendNull();
appendValues(builder3, 1.1, 6.1, 7.2, 2);
// matches the first 2 values of builder1
AggregateMetricDoubleBlockBuilder builder4 = blockFactory.newAggregateMetricDoubleBlockBuilder(2);
appendValues(builder4, 1.1, 6.1, 7.2, 2);
builder4.appendNull();
// like builder1 without null value
AggregateMetricDoubleBlockBuilder builder5 = blockFactory.newAggregateMetricDoubleBlockBuilder(2);
appendValues(builder5, 1.1, 6.1, 7.2, 2);
appendValues(builder5, 1.3, 6.3, 11.9, 3);
// like builder1 but first value's sum is null
AggregateMetricDoubleBlockBuilder builder6 = blockFactory.newAggregateMetricDoubleBlockBuilder(3);
builder6.min().appendDouble(1.1);
builder6.max().appendDouble(6.1);
builder6.sum().appendNull();
builder6.count().appendInt(2);
builder6.appendNull();
appendValues(builder6, 1.3, 6.3, 11.9, 3);
// like builder6 but max and sum are swapped
AggregateMetricDoubleBlockBuilder builder7 = blockFactory.newAggregateMetricDoubleBlockBuilder(3);
builder7.min().appendDouble(1.1);
builder7.max().appendNull();
builder7.sum().appendDouble(6.1);
builder7.count().appendInt(2);
builder7.appendNull();
appendValues(builder7, 1.3, 6.3, 11.9, 3);

List<AggregateMetricDoubleBlock> notEqualBlocks = List.of(
builder1.build(),
builder2.build(),
builder3.build(),
builder4.build(),
builder5.build(),
builder6.build(),
builder7.build(),
blockFactory.newConstantAggregateMetricDoubleBlock(
new AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral(1.1, 6.1, 7.2, 2),
1
),
blockFactory.newConstantAggregateMetricDoubleBlock(
new AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral(1.1, 6.1, 7.2, 2),
3
)
);
assertAllNotEquals(notEqualBlocks);
}

public void testSimpleBlockWithSingleNull() {
AggregateMetricDoubleBlockBuilder builder1 = blockFactory.newAggregateMetricDoubleBlockBuilder(3);
appendValues(builder1, 1.1, 6.1, 7.2, 2);
builder1.appendNull();
appendValues(builder1, 1.3, 6.3, 11.9, 3);
AggregateMetricDoubleBlockBuilder builder2 = blockFactory.newAggregateMetricDoubleBlockBuilder(3);
appendValues(builder2, 1.1, 6.1, 7.2, 2);
builder2.appendNull();
appendValues(builder2, 1.3, 6.3, 11.9, 3);

List<AggregateMetricDoubleBlock> blocks = List.of(builder1.build(), builder2.build());

assertEquals(3, blocks.get(0).getPositionCount());
assertTrue(blocks.get(0).isNull(1));
assertAllEquals(blocks);
}

public void testSimpleBlockWithManyNulls() {
int positions = randomIntBetween(1, 256);
boolean grow = randomBoolean();
Expand Down Expand Up @@ -86,4 +276,24 @@ static void assertAllEquals(List<?> objs) {
}
}
}

static void assertAllNotEquals(List<?> objs) {
for (Object obj1 : objs) {
for (Object obj2 : objs) {
if (obj1 == obj2) {
continue; // skip self
}
assertNotEquals(obj1, obj2);
assertNotEquals(obj2, obj1);
assertNotEquals(obj1.hashCode(), obj2.hashCode());
}
}
}

static void appendValues(AggregateMetricDoubleBlockBuilder builder, double min, double max, double sum, int count) {
builder.min().appendDouble(min);
builder.max().appendDouble(max);
builder.sum().appendDouble(sum);
builder.count().appendInt(count);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ void testEmpty(BlockFactory bf) {
assertZeroPositionsAndRelease(bf.newBooleanBlockBuilder(0).build());
assertZeroPositionsAndRelease(bf.newBooleanArrayVector(new boolean[] {}, 0));
assertZeroPositionsAndRelease(bf.newBooleanVectorBuilder(0).build());
assertZeroPositionsAndRelease(bf.newAggregateMetricDoubleBlockBuilder(0).build());
}

public void testSmallSingleValueDenseGrowthInt() {
Expand Down Expand Up @@ -168,6 +169,17 @@ public void testSmallSingleValueDenseGrowthBoolean() {
}
}

public void testSmallSingleValueDenseGrowthAggregateMetricDouble() {
for (int initialSize : List.of(0, 1, 2, 3, 4, 5)) {
try (var blockBuilder = blockFactory.newBooleanBlockBuilder(initialSize)) {
IntStream.range(0, 10).forEach(i -> blockBuilder.appendBoolean(i % 3 == 0));
BooleanBlock block = blockBuilder.build();
assertSingleValueDenseBlock(block);
block.close();
}
}
}

static void assertSingleValueDenseBlock(Block initialBlock) {
final int positionCount = initialBlock.getPositionCount();
int depth = randomIntBetween(1, 5);
Expand Down
Loading