use of com.yahoo.bullet.query.Field 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);
}
use of com.yahoo.bullet.query.Field in project bullet-core by yahoo.
the class QuerierTest method testTableFunction.
@Test
public void testTableFunction() {
TableFunction tableFunction = new LateralView(new Explode(new FieldExpression("map"), "key", "value", true));
Projection projection = new Projection(Arrays.asList(new Field("key", new FieldExpression("key")), new Field("value", new FieldExpression("value")), new Field("abc", new FieldExpression("abc"))), false);
Expression filter = new UnaryExpression(new FieldExpression("map"), Operation.IS_NOT_NULL);
Query query = new Query(tableFunction, projection, filter, new Raw(500), null, new Window(), null);
Querier querier = make(Querier.Mode.ALL, query);
querier.consume(RecordBox.get().addMap("map", Pair.of("a", 0), Pair.of("b", 1), Pair.of("c", 2)).add("abc", 1).getRecord());
querier.consume(RecordBox.get().add("abc", 2).getRecord());
querier.consume(RecordBox.get().add("abc", 3).add("map", new HashMap<>()).getRecord());
List<BulletRecord> result = querier.getResult().getRecords();
Assert.assertEquals(result.size(), 4);
Assert.assertEquals(result.get(0).fieldCount(), 3);
Assert.assertEquals(result.get(0).typedGet("key").getValue(), "a");
Assert.assertEquals(result.get(0).typedGet("value").getValue(), 0);
Assert.assertEquals(result.get(0).typedGet("abc").getValue(), 1);
Assert.assertEquals(result.get(1).fieldCount(), 3);
Assert.assertEquals(result.get(1).typedGet("key").getValue(), "b");
Assert.assertEquals(result.get(1).typedGet("value").getValue(), 1);
Assert.assertEquals(result.get(1).typedGet("abc").getValue(), 1);
Assert.assertEquals(result.get(2).fieldCount(), 3);
Assert.assertEquals(result.get(2).typedGet("key").getValue(), "c");
Assert.assertEquals(result.get(2).typedGet("value").getValue(), 2);
Assert.assertEquals(result.get(2).typedGet("abc").getValue(), 1);
Assert.assertEquals(result.get(3).fieldCount(), 1);
Assert.assertEquals(result.get(3).typedGet("abc").getValue(), 3);
}
use of com.yahoo.bullet.query.Field in project bullet-core by yahoo.
the class QuerierTest method testCopyProjection.
@Test
public void testCopyProjection() {
Projection projection = new Projection(Collections.singletonList(new Field("mid", new FieldExpression("map_field", "id"))), true);
Query query = new Query(projection, null, new Raw(null), null, new Window(), null);
BulletConfig config = new BulletConfig();
query.configure(config);
Querier querier = new Querier(makeRunningQuery("", query), config);
RecordBox boxA = RecordBox.get().addMap("map_field", Pair.of("id", "23"));
BulletRecord expected = boxA.getRecord().copy();
expected.setString("mid", "23");
querier.consume(boxA.getRecord());
Assert.assertFalse(querier.isClosed());
Assert.assertEquals(querier.getData(), getListBytes(expected));
}
use of com.yahoo.bullet.query.Field in project bullet-core by yahoo.
the class ProjectionTest method testProjectOldRecord.
@Test
public void testProjectOldRecord() {
BulletRecord record = RecordBox.get().add("a", 2).add("b", 4).add("c", 6).getRecord();
List<Field> fields = Arrays.asList(new Field("a", new BinaryExpression(new FieldExpression("a"), new FieldExpression("b"), Operation.MUL)), new Field("b", new BinaryExpression(new FieldExpression("b"), new FieldExpression("c"), Operation.MUL)), new Field("d", new BinaryExpression(new FieldExpression("b"), new FieldExpression("c"), Operation.ADD)), new Field("e", new BinaryExpression(new FieldExpression("e"), new FieldExpression("f"), Operation.SUB)), new Field("f", new FieldExpression("f")), new Field("h", new UnaryExpression(new FieldExpression("a"), Operation.SIZE_OF)));
Projection projection = new Projection(fields);
BulletRecord oldRecord = projection.project(record);
Assert.assertEquals(oldRecord.fieldCount(), 4);
Assert.assertEquals(oldRecord.typedGet("a").getValue(), 8);
Assert.assertEquals(oldRecord.typedGet("b").getValue(), 24);
Assert.assertEquals(oldRecord.typedGet("c").getValue(), 6);
Assert.assertEquals(oldRecord.typedGet("d").getValue(), 10);
Assert.assertEquals(oldRecord, record);
}
use of com.yahoo.bullet.query.Field in project bullet-core by yahoo.
the class QuerierTest method testFilteringProjection.
@Test
public void testFilteringProjection() {
Projection projection = new Projection(Collections.singletonList(new Field("mid", new FieldExpression("map_field", "id"))), false);
Expression filter = new BinaryExpression(new FieldExpression("map_field", "id"), new ListExpression(Arrays.asList(new ValueExpression("1"), new ValueExpression("23"))), Operation.EQUALS_ANY);
Query query = new Query(projection, filter, new Raw(null), null, new Window(), null);
BulletConfig config = new BulletConfig();
query.configure(config);
Querier querier = new Querier(makeRunningQuery("", query), config);
RecordBox boxA = RecordBox.get().addMap("map_field", Pair.of("id", "3"));
querier.consume(boxA.getRecord());
Assert.assertFalse(querier.isClosed());
Assert.assertNull(querier.getData());
RecordBox boxB = RecordBox.get().addMap("map_field", Pair.of("id", "23"));
RecordBox expected = RecordBox.get().add("mid", "23");
querier.consume(boxB.getRecord());
Assert.assertFalse(querier.isClosed());
Assert.assertEquals(querier.getData(), getListBytes(expected.getRecord()));
}
Aggregations