use of com.yahoo.bullet.query.aggregations.GroupBy in project bullet-core by yahoo.
the class TupleSketchingStrategyTest method testCombining.
@Test
public void testCombining() {
List<String> fields = asList("fieldA", "fieldB");
TupleSketchingStrategy groupBy = makeGroupBy(fields, 5, new GroupOperation(COUNT, null, "count"), new GroupOperation(SUM, "price", "priceSum"));
BulletRecord recordA = RecordBox.get().add("fieldA", "foo").add("fieldB", "bar").add("price", 3).getRecord();
BulletRecord recordB = RecordBox.get().add("fieldA", "null").add("fieldB", "bar").add("price", 1).getRecord();
IntStream.range(0, 10).mapToObj(i -> recordA).forEach(groupBy::consume);
IntStream.range(0, 9).mapToObj(i -> recordB).forEach(groupBy::consume);
IntStream.range(0, 20).mapToObj(i -> recordA).forEach(groupBy::consume);
groupBy.consume(recordB);
byte[] firstSerialized = groupBy.getData();
// Remake it
groupBy = makeGroupBy(fields, 5, new GroupOperation(COUNT, null, "count"), new GroupOperation(SUM, "price", "priceSum"));
BulletRecord recordC = RecordBox.get().add("fieldA", "foo").add("fieldB", "bar").add("price", 3).getRecord();
BulletRecord recordD = RecordBox.get().addNull("fieldA").addNull("fieldB").add("price", 10).getRecord();
IntStream.range(0, 30).mapToObj(i -> recordC).forEach(groupBy::consume);
IntStream.range(0, 10).mapToObj(i -> recordD).forEach(groupBy::consume);
byte[] secondSerialized = groupBy.getData();
groupBy = makeGroupBy(fields, 5, new GroupOperation(COUNT, null, "count"), new GroupOperation(SUM, "price", "priceSum"));
groupBy.combine(firstSerialized);
groupBy.combine(secondSerialized);
Clip aggregate = groupBy.getResult();
Assert.assertNotNull(aggregate);
List<BulletRecord> records = aggregate.getRecords();
Assert.assertEquals(records.size(), 3);
// count = 10 + 20 + 30, price = 10*3 + 20*3 + 30*3
BulletRecord expectedA = RecordBox.get().add("fieldA", "foo").add("fieldB", "bar").add("count", 60L).add("priceSum", 180.0).getRecord();
// count = 9 + 1, price = 9*1 + 1*1
BulletRecord expectedB = RecordBox.get().add("fieldA", "null").add("fieldB", "bar").add("count", 10L).add("priceSum", 10.0).getRecord();
// count = 10, price = 10*10
BulletRecord expectedC = RecordBox.get().add("fieldA", "null").add("fieldB", "null").add("count", 10L).add("priceSum", 100.0).getRecord();
assertContains(records, expectedA);
assertContains(records, expectedB);
assertContains(records, expectedC);
Assert.assertEquals(groupBy.getRecords(), aggregate.getRecords());
Assert.assertEquals(groupBy.getMetadata().asMap(), aggregate.getMeta().asMap());
}
use of com.yahoo.bullet.query.aggregations.GroupBy in project bullet-core by yahoo.
the class TupleSketchingStrategyTest method testResetting.
@Test
public void testResetting() {
List<String> fields = asList("fieldA", "fieldB");
TupleSketchingStrategy groupBy = makeGroupBy(fields, 5, new GroupOperation(COUNT, null, "count"), new GroupOperation(SUM, "price", "priceSum"));
BulletRecord recordA = RecordBox.get().add("fieldA", "foo").add("fieldB", "bar").add("price", 3).getRecord();
BulletRecord recordB = RecordBox.get().add("fieldA", "null").add("fieldB", "bar").add("price", 1).getRecord();
IntStream.range(0, 30).mapToObj(i -> recordA).forEach(groupBy::consume);
IntStream.range(0, 10).mapToObj(i -> recordB).forEach(groupBy::consume);
BulletRecord expectedA = RecordBox.get().add("fieldA", "foo").add("fieldB", "bar").add("count", 30L).add("priceSum", 90.0).getRecord();
BulletRecord expectedB = RecordBox.get().add("fieldA", "null").add("fieldB", "bar").add("count", 10L).add("priceSum", 10.0).getRecord();
Clip aggregate = groupBy.getResult();
Assert.assertNotNull(aggregate);
List<BulletRecord> records = aggregate.getRecords();
Assert.assertEquals(records.size(), 2);
assertContains(records, expectedA);
assertContains(records, expectedB);
Assert.assertEquals(groupBy.getRecords(), aggregate.getRecords());
Assert.assertEquals(groupBy.getMetadata().asMap(), aggregate.getMeta().asMap());
groupBy.reset();
BulletRecord recordC = RecordBox.get().add("fieldA", "foo").add("fieldB", "bar").add("price", 3).getRecord();
BulletRecord recordD = RecordBox.get().addNull("fieldA").addNull("fieldB").add("price", 10).getRecord();
IntStream.range(0, 10).mapToObj(i -> recordB).forEach(groupBy::consume);
IntStream.range(0, 30).mapToObj(i -> recordC).forEach(groupBy::consume);
IntStream.range(0, 10).mapToObj(i -> recordD).forEach(groupBy::consume);
aggregate = groupBy.getResult();
Assert.assertNotNull(aggregate);
records = aggregate.getRecords();
Assert.assertEquals(records.size(), 3);
expectedA = RecordBox.get().add("fieldA", "null").add("fieldB", "bar").add("count", 10L).add("priceSum", 10.0).getRecord();
expectedB = RecordBox.get().add("fieldA", "foo").add("fieldB", "bar").add("count", 30L).add("priceSum", 90.0).getRecord();
BulletRecord expectedC = RecordBox.get().add("fieldA", "null").add("fieldB", "null").add("count", 10L).add("priceSum", 100.0).getRecord();
assertContains(records, expectedA);
assertContains(records, expectedB);
assertContains(records, expectedC);
Assert.assertEquals(groupBy.getRecords(), aggregate.getRecords());
Assert.assertEquals(groupBy.getMetadata().asMap(), aggregate.getMeta().asMap());
}
use of com.yahoo.bullet.query.aggregations.GroupBy in project bullet-core by yahoo.
the class TupleSketchingStrategyTest method testMoreGroupsThanNominalEntries.
@Test
public void testMoreGroupsThanNominalEntries() {
Map<String, String> fields = singletonMap("fieldA", "A");
// Nominal Entries is 32. Aggregation size is also 32
TupleSketchingStrategy groupBy = makeGroupBy(makeConfiguration(32), fields, 32, singletonList(new GroupOperation(COUNT, null, "count")), ALL_METADATA);
// Generate 4 batches of 64 records with 0 - 63 in fieldA.
IntStream.range(0, 256).mapToObj(i -> RecordBox.get().add("fieldA", i % 64).getRecord()).forEach(groupBy::consume);
Clip aggregate = groupBy.getResult();
Assert.assertNotNull(aggregate);
Map<String, Object> meta = aggregate.getMeta().asMap();
Assert.assertEquals(meta.size(), 1);
List<BulletRecord> records = aggregate.getRecords();
Assert.assertEquals(records.size(), 32);
Set<String> groups = new HashSet<>();
for (BulletRecord record : records) {
groups.add((String) record.typedGet("A").getValue());
Assert.assertEquals(record.typedGet("count").getValue(), 4L);
}
Assert.assertEquals(groups.size(), 32);
Assert.assertEquals(groupBy.getRecords(), aggregate.getRecords());
Assert.assertEquals(groupBy.getMetadata().asMap(), aggregate.getMeta().asMap());
}
use of com.yahoo.bullet.query.aggregations.GroupBy in project bullet-core by yahoo.
the class TupleSketchingStrategyTest method testCombiningAndConsuming.
@Test
public void testCombiningAndConsuming() {
List<String> fields = asList("fieldA", "fieldB");
TupleSketchingStrategy groupBy = makeGroupBy(fields, 5, new GroupOperation(COUNT, null, "count"), new GroupOperation(SUM, "price", "priceSum"));
BulletRecord recordA = RecordBox.get().add("fieldA", "foo").add("fieldB", "bar").add("price", 3).getRecord();
BulletRecord recordB = RecordBox.get().add("fieldA", "null").add("fieldB", "bar").add("price", 1).getRecord();
IntStream.range(0, 30).mapToObj(i -> recordA).forEach(groupBy::consume);
IntStream.range(0, 10).mapToObj(i -> recordB).forEach(groupBy::consume);
byte[] serialized = groupBy.getData();
// Remake it
groupBy = makeGroupBy(fields, 5, new GroupOperation(COUNT, null, "count"), new GroupOperation(SUM, "price", "priceSum"));
BulletRecord recordC = RecordBox.get().add("fieldA", "foo").add("fieldB", "bar").add("price", 3).getRecord();
BulletRecord recordD = RecordBox.get().addNull("fieldA").addNull("fieldB").add("price", 10).getRecord();
IntStream.range(0, 30).mapToObj(i -> recordC).forEach(groupBy::consume);
IntStream.range(0, 10).mapToObj(i -> recordD).forEach(groupBy::consume);
groupBy.combine(serialized);
Clip aggregate = groupBy.getResult();
Assert.assertNotNull(aggregate);
List<BulletRecord> records = aggregate.getRecords();
Assert.assertEquals(records.size(), 3);
BulletRecord expectedA = RecordBox.get().add("fieldA", "foo").add("fieldB", "bar").add("count", 60L).add("priceSum", 180.0).getRecord();
BulletRecord expectedB = RecordBox.get().add("fieldA", "null").add("fieldB", "bar").add("count", 10L).add("priceSum", 10.0).getRecord();
BulletRecord expectedC = RecordBox.get().add("fieldA", "null").add("fieldB", "null").add("count", 10L).add("priceSum", 100.0).getRecord();
assertContains(records, expectedA);
assertContains(records, expectedB);
assertContains(records, expectedC);
Assert.assertEquals(groupBy.getRecords(), aggregate.getRecords());
Assert.assertEquals(groupBy.getMetadata().asMap(), aggregate.getMeta().asMap());
}
use of com.yahoo.bullet.query.aggregations.GroupBy in project bullet-core by yahoo.
the class TupleSketchingStrategyTest method testMetadata.
@Test
public void testMetadata() {
Map<String, String> fields = singletonMap("fieldA", "fieldA");
// Nominal Entries is 32. Aggregation size is also 32
TupleSketchingStrategy groupBy = makeGroupBy(makeConfiguration(32), fields, 32, singletonList(new GroupOperation(COUNT, null, "count")), ALL_METADATA);
// Generate 4 batches of 64 records with 0 - 63 in fieldA.
IntStream.range(0, 256).mapToObj(i -> RecordBox.get().add("fieldA", i % 64).getRecord()).forEach(groupBy::consume);
Clip aggregate = groupBy.getResult();
Assert.assertNotNull(aggregate);
List<BulletRecord> records = aggregate.getRecords();
Assert.assertEquals(records.size(), 32);
records.forEach(r -> Assert.assertTrue(Integer.valueOf((String) r.typedGet("fieldA").getValue()) < 64));
Map<String, Object> meta = aggregate.getMeta().asMap();
Assert.assertEquals(meta.size(), 1);
Map<String, Object> stats = (Map<String, Object>) meta.get("aggregate_stats");
Assert.assertEquals(stats.size(), 4);
Assert.assertTrue((Boolean) stats.get("isEstimate"));
double theta = (Double) stats.get("theta");
Assert.assertTrue(theta <= 1.0);
double groupEstimate = (Double) stats.get("uniquesApprox");
Assert.assertTrue(stats.containsKey("stddev"));
Map<String, Map<String, Double>> standardDeviations = (Map<String, Map<String, Double>>) stats.get("stddev");
Assert.assertEquals(standardDeviations.size(), 3);
double upperOneSigma = standardDeviations.get(KMVSketch.META_STD_DEV_1).get(KMVSketch.META_STD_DEV_UB);
double lowerOneSigma = standardDeviations.get(KMVSketch.META_STD_DEV_1).get(KMVSketch.META_STD_DEV_LB);
double upperTwoSigma = standardDeviations.get(KMVSketch.META_STD_DEV_2).get(KMVSketch.META_STD_DEV_UB);
double lowerTwoSigma = standardDeviations.get(KMVSketch.META_STD_DEV_2).get(KMVSketch.META_STD_DEV_LB);
double upperThreeSigma = standardDeviations.get(KMVSketch.META_STD_DEV_3).get(KMVSketch.META_STD_DEV_UB);
double lowerThreeSigma = standardDeviations.get(KMVSketch.META_STD_DEV_3).get(KMVSketch.META_STD_DEV_LB);
Assert.assertTrue(groupEstimate >= lowerOneSigma);
Assert.assertTrue(groupEstimate <= upperOneSigma);
Assert.assertTrue(groupEstimate >= lowerTwoSigma);
Assert.assertTrue(groupEstimate <= upperTwoSigma);
Assert.assertTrue(groupEstimate >= lowerThreeSigma);
Assert.assertTrue(groupEstimate <= upperThreeSigma);
Assert.assertTrue(groupEstimate <= upperThreeSigma);
Assert.assertEquals(groupBy.getRecords(), aggregate.getRecords());
Assert.assertEquals(groupBy.getMetadata().asMap(), aggregate.getMeta().asMap());
}
Aggregations