Search in sources :

Example 1 with Computation

use of com.yahoo.bullet.query.postaggregations.Computation in project bullet-core by yahoo.

the class ComputationStrategyTest method testComputation.

@Test
public void testComputation() {
    // Computations are done using the fields in the original record and then the new fields are written at the end.
    // a = 2 * a + 5 * b
    // b = a * b
    List<Field> fields = new ArrayList<>();
    fields.add(new Field("a", new BinaryExpression(new BinaryExpression(new ValueExpression(2), new FieldExpression("a"), Operation.MUL), new BinaryExpression(new ValueExpression(5), new FieldExpression("b"), Operation.MUL), Operation.ADD)));
    fields.add(new Field("b", new BinaryExpression(new FieldExpression("a"), new FieldExpression("b"), Operation.MUL)));
    List<BulletRecord> records = new ArrayList<>();
    records.add(RecordBox.get().add("a", 5).add("b", 2).add("c", 0).getRecord());
    records.add(RecordBox.get().add("a", 2).add("b", 4).add("c", 1).getRecord());
    records.add(RecordBox.get().add("a", 1).add("b", 7).add("c", 2).getRecord());
    Clip clip = new Clip();
    clip.add(records);
    ComputationStrategy strategy = (ComputationStrategy) new Computation(fields).getPostStrategy();
    Clip result = strategy.execute(clip);
    Assert.assertEquals(result.getRecords().size(), 3);
    Assert.assertEquals(result.getRecords().get(0).typedGet("a").getValue(), 20);
    Assert.assertEquals(result.getRecords().get(0).typedGet("b").getValue(), 10);
    Assert.assertEquals(result.getRecords().get(0).typedGet("c").getValue(), 0);
    Assert.assertEquals(result.getRecords().get(1).typedGet("a").getValue(), 24);
    Assert.assertEquals(result.getRecords().get(1).typedGet("b").getValue(), 8);
    Assert.assertEquals(result.getRecords().get(1).typedGet("c").getValue(), 1);
    Assert.assertEquals(result.getRecords().get(2).typedGet("a").getValue(), 37);
    Assert.assertEquals(result.getRecords().get(2).typedGet("b").getValue(), 7);
    Assert.assertEquals(result.getRecords().get(2).typedGet("c").getValue(), 2);
}
Also used : Clip(com.yahoo.bullet.result.Clip) Field(com.yahoo.bullet.query.Field) BulletRecord(com.yahoo.bullet.record.BulletRecord) BinaryExpression(com.yahoo.bullet.query.expressions.BinaryExpression) Computation(com.yahoo.bullet.query.postaggregations.Computation) ValueExpression(com.yahoo.bullet.query.expressions.ValueExpression) ArrayList(java.util.ArrayList) FieldExpression(com.yahoo.bullet.query.expressions.FieldExpression) Test(org.testng.annotations.Test)

Example 2 with Computation

use of com.yahoo.bullet.query.postaggregations.Computation in project bullet-core by yahoo.

the class QuerierTest method testComputationAndCulling.

@Test
public void testComputationAndCulling() {
    Projection projection = new Projection(Arrays.asList(new Field("a", new FieldExpression("a")), new Field("b", new FieldExpression("b"))), false);
    Expression filter = new UnaryExpression(new FieldExpression("a"), Operation.IS_NOT_NULL);
    Expression expression = new BinaryExpression(new FieldExpression("a"), new ValueExpression(2L), Operation.ADD);
    Computation computation = new Computation(Collections.singletonList(new Field("newName", expression)));
    Culling culling = new Culling(singleton("a"));
    Query query = new Query(projection, filter, new Raw(500), Arrays.asList(computation, culling), new Window(), null);
    Querier querier = make(Querier.Mode.ALL, query);
    IntStream.range(0, 4).forEach(i -> querier.consume(RecordBox.get().add("a", i).add("b", i).getRecord()));
    List<BulletRecord> result = querier.getResult().getRecords();
    Assert.assertEquals(result.size(), 4);
    Assert.assertEquals(result.get(0).typedGet("newName").getValue(), 2L);
    Assert.assertFalse(result.get(0).hasField("a"));
    Assert.assertEquals(result.get(0).typedGet("b").getValue(), 0);
    Assert.assertEquals(result.get(1).typedGet("newName").getValue(), 3L);
    Assert.assertFalse(result.get(1).hasField("a"));
    Assert.assertEquals(result.get(1).typedGet("b").getValue(), 1);
    Assert.assertEquals(result.get(2).typedGet("newName").getValue(), 4L);
    Assert.assertFalse(result.get(2).hasField("a"));
    Assert.assertEquals(result.get(2).typedGet("b").getValue(), 2);
    Assert.assertEquals(result.get(3).typedGet("newName").getValue(), 5L);
    Assert.assertFalse(result.get(3).hasField("a"));
    Assert.assertEquals(result.get(3).typedGet("b").getValue(), 3);
}
Also used : Window(com.yahoo.bullet.query.Window) Query(com.yahoo.bullet.query.Query) Projection(com.yahoo.bullet.query.Projection) Raw(com.yahoo.bullet.query.aggregations.Raw) UnaryExpression(com.yahoo.bullet.query.expressions.UnaryExpression) FieldExpression(com.yahoo.bullet.query.expressions.FieldExpression) Field(com.yahoo.bullet.query.Field) BulletRecord(com.yahoo.bullet.record.BulletRecord) BinaryExpression(com.yahoo.bullet.query.expressions.BinaryExpression) FieldExpression(com.yahoo.bullet.query.expressions.FieldExpression) ListExpression(com.yahoo.bullet.query.expressions.ListExpression) UnaryExpression(com.yahoo.bullet.query.expressions.UnaryExpression) BinaryExpression(com.yahoo.bullet.query.expressions.BinaryExpression) ValueExpression(com.yahoo.bullet.query.expressions.ValueExpression) Expression(com.yahoo.bullet.query.expressions.Expression) Computation(com.yahoo.bullet.query.postaggregations.Computation) ValueExpression(com.yahoo.bullet.query.expressions.ValueExpression) Culling(com.yahoo.bullet.query.postaggregations.Culling) Test(org.testng.annotations.Test) BulletConfigTest(com.yahoo.bullet.common.BulletConfigTest)

Aggregations

Field (com.yahoo.bullet.query.Field)2 BinaryExpression (com.yahoo.bullet.query.expressions.BinaryExpression)2 FieldExpression (com.yahoo.bullet.query.expressions.FieldExpression)2 ValueExpression (com.yahoo.bullet.query.expressions.ValueExpression)2 Computation (com.yahoo.bullet.query.postaggregations.Computation)2 BulletRecord (com.yahoo.bullet.record.BulletRecord)2 Test (org.testng.annotations.Test)2 BulletConfigTest (com.yahoo.bullet.common.BulletConfigTest)1 Projection (com.yahoo.bullet.query.Projection)1 Query (com.yahoo.bullet.query.Query)1 Window (com.yahoo.bullet.query.Window)1 Raw (com.yahoo.bullet.query.aggregations.Raw)1 Expression (com.yahoo.bullet.query.expressions.Expression)1 ListExpression (com.yahoo.bullet.query.expressions.ListExpression)1 UnaryExpression (com.yahoo.bullet.query.expressions.UnaryExpression)1 Culling (com.yahoo.bullet.query.postaggregations.Culling)1 Clip (com.yahoo.bullet.result.Clip)1 ArrayList (java.util.ArrayList)1