use of com.yahoo.bullet.query.expressions.Expression in project bullet-core by yahoo.
the class SimpleEqualityPartitioner method addFieldToMapping.
private void addFieldToMapping(FieldExpression fieldExpression, ValueExpression valueExpression, Map<String, Set<Serializable>> mapping) {
if (fieldExpression.getKey() instanceof Expression || fieldExpression.getSubKey() instanceof Expression) {
return;
}
String field = fieldExpression.getName();
if (fieldSet.contains(field)) {
Serializable value = valueExpression.getValue();
mapping.computeIfAbsent(field, s -> new HashSet<>()).add(value);
}
}
use of com.yahoo.bullet.query.expressions.Expression in project bullet-core by yahoo.
the class FieldEvaluator method getFieldExtractor.
private static FieldExtractor getFieldExtractor(FieldExpression fieldExpression) {
final String field = fieldExpression.getField();
final Serializable key = fieldExpression.getKey();
final Serializable subKey = fieldExpression.getSubKey();
final Type fieldType = fieldExpression.getType() != null ? fieldExpression.getType() : Type.UNKNOWN;
if (key instanceof String) {
if (subKey instanceof String) {
return record -> record.typedGet(field, (String) key, (String) subKey, getSuperSuperType(Type.COMPLEX_MAPS, fieldType));
} else if (subKey instanceof Expression) {
final Evaluator subKeyEvaluator = ((Expression) subKey).getEvaluator();
return record -> {
TypedObject subKeyArg = subKeyEvaluator.evaluate(record);
if (subKeyArg.isNull()) {
return TypedObject.NULL;
}
return record.typedGet(field, (String) key, (String) subKeyArg.getValue(), getSuperSuperType(Type.COMPLEX_MAPS, fieldType));
};
} else {
return record -> record.typedGet(field, (String) key, getSuperType(Type.MAPS, fieldType));
}
} else if (key instanceof Integer) {
if (subKey instanceof String) {
return record -> record.typedGet(field, (Integer) key, (String) subKey, getSuperSuperType(Type.COMPLEX_LISTS, fieldType));
} else if (subKey instanceof Expression) {
final Evaluator subKeyEvaluator = ((Expression) subKey).getEvaluator();
return record -> {
TypedObject subKeyArg = subKeyEvaluator.evaluate(record);
if (subKeyArg.isNull()) {
return TypedObject.NULL;
}
return record.typedGet(field, (Integer) key, (String) subKeyArg.getValue(), getSuperSuperType(Type.COMPLEX_LISTS, fieldType));
};
} else {
return record -> record.typedGet(field, (Integer) key, getSuperType(Type.LISTS, fieldType));
}
} else if (key instanceof Expression) {
final Evaluator keyEvaluator = ((Expression) key).getEvaluator();
if (subKey instanceof String) {
return record -> {
TypedObject keyArg = keyEvaluator.evaluate(record);
if (keyArg.isNull()) {
return TypedObject.NULL;
}
Type type = keyArg.getType();
if (Type.isNumeric(type)) {
return record.typedGet(field, ((Number) keyArg.getValue()).intValue(), (String) subKey, getSuperSuperType(Type.COMPLEX_LISTS, fieldType));
} else {
return record.typedGet(field, (String) keyArg.getValue(), (String) subKey, getSuperSuperType(Type.COMPLEX_MAPS, fieldType));
}
};
} else if (subKey instanceof Expression) {
final Evaluator subKeyEvaluator = ((Expression) subKey).getEvaluator();
return record -> {
TypedObject keyArg = keyEvaluator.evaluate(record);
if (keyArg.isNull()) {
return TypedObject.NULL;
}
TypedObject subKeyArg = subKeyEvaluator.evaluate(record);
if (subKeyArg.isNull()) {
return TypedObject.NULL;
}
Type type = keyArg.getType();
if (Type.isNumeric(type)) {
return record.typedGet(field, ((Number) keyArg.getValue()).intValue(), (String) subKeyArg.getValue(), getSuperSuperType(Type.COMPLEX_LISTS, fieldType));
} else {
return record.typedGet(field, (String) keyArg.getValue(), (String) subKeyArg.getValue(), getSuperSuperType(Type.COMPLEX_MAPS, fieldType));
}
};
} else {
return record -> {
TypedObject keyArg = keyEvaluator.evaluate(record);
if (keyArg.isNull()) {
return TypedObject.NULL;
}
Type type = keyArg.getType();
if (Type.isNumeric(type)) {
return record.typedGet(field, ((Number) keyArg.getValue()).intValue(), getSuperType(Type.LISTS, fieldType));
} else {
return record.typedGet(field, (String) keyArg.getValue(), getSuperType(Type.MAPS, fieldType));
}
};
}
} else {
return record -> record.typedGet(field, fieldType);
}
}
use of com.yahoo.bullet.query.expressions.Expression 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.expressions.Expression in project bullet-core by yahoo.
the class QuerierTest method testLogicFilterAnd.
@Test
public void testLogicFilterAnd() {
// legacy test
Expression filter = new BinaryExpression(new BinaryExpression(new FieldExpression("field"), new ValueExpression("abc"), Operation.EQUALS), new BinaryExpression(new FieldExpression("id"), new ValueExpression("1"), Operation.EQUALS), Operation.AND);
Query query = new Query(new Projection(), filter, new Raw(null), null, new Window(), null);
Querier querier = make(Querier.Mode.PARTITION, query);
querier.consume(RecordBox.get().add("field", "abc").add("id", "2").getRecord());
Assert.assertFalse(querier.hasNewData());
querier.consume(RecordBox.get().add("field", "abc").add("id", "1").getRecord());
Assert.assertTrue(querier.hasNewData());
}
use of com.yahoo.bullet.query.expressions.Expression 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