Search in sources :

Example 1 with Expression

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);
    }
}
Also used : IntStream(java.util.stream.IntStream) Query(com.yahoo.bullet.query.Query) TypedObject(com.yahoo.bullet.typesystem.TypedObject) ValueExpression(com.yahoo.bullet.query.expressions.ValueExpression) BulletRecord(com.yahoo.bullet.record.BulletRecord) Set(java.util.Set) HashMap(java.util.HashMap) Expression(com.yahoo.bullet.query.expressions.Expression) Collectors(java.util.stream.Collectors) Operation(com.yahoo.bullet.query.expressions.Operation) Serializable(java.io.Serializable) HashSet(java.util.HashSet) Objects(java.util.Objects) FieldExpression(com.yahoo.bullet.query.expressions.FieldExpression) List(java.util.List) BulletConfig(com.yahoo.bullet.common.BulletConfig) Map(java.util.Map) Utilities.isNull(com.yahoo.bullet.common.Utilities.isNull) Collections(java.util.Collections) BinaryExpression(com.yahoo.bullet.query.expressions.BinaryExpression) Serializable(java.io.Serializable) ValueExpression(com.yahoo.bullet.query.expressions.ValueExpression) Expression(com.yahoo.bullet.query.expressions.Expression) FieldExpression(com.yahoo.bullet.query.expressions.FieldExpression) BinaryExpression(com.yahoo.bullet.query.expressions.BinaryExpression) HashSet(java.util.HashSet)

Example 2 with Expression

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);
    }
}
Also used : FieldExpression(com.yahoo.bullet.query.expressions.FieldExpression) TypedObject(com.yahoo.bullet.typesystem.TypedObject) BulletRecord(com.yahoo.bullet.record.BulletRecord) Set(java.util.Set) Expression(com.yahoo.bullet.query.expressions.Expression) Type(com.yahoo.bullet.typesystem.Type) Serializable(java.io.Serializable) Serializable(java.io.Serializable) Type(com.yahoo.bullet.typesystem.Type) TypedObject(com.yahoo.bullet.typesystem.TypedObject) FieldExpression(com.yahoo.bullet.query.expressions.FieldExpression) Expression(com.yahoo.bullet.query.expressions.Expression)

Example 3 with Expression

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);
}
Also used : Window(com.yahoo.bullet.query.Window) LateralView(com.yahoo.bullet.query.tablefunctions.LateralView) 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) Explode(com.yahoo.bullet.query.tablefunctions.Explode) Field(com.yahoo.bullet.query.Field) BulletRecord(com.yahoo.bullet.record.BulletRecord) 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) TableFunction(com.yahoo.bullet.query.tablefunctions.TableFunction) Test(org.testng.annotations.Test) BulletConfigTest(com.yahoo.bullet.common.BulletConfigTest)

Example 4 with Expression

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());
}
Also used : Window(com.yahoo.bullet.query.Window) BinaryExpression(com.yahoo.bullet.query.expressions.BinaryExpression) Query(com.yahoo.bullet.query.Query) 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) ValueExpression(com.yahoo.bullet.query.expressions.ValueExpression) Projection(com.yahoo.bullet.query.Projection) Raw(com.yahoo.bullet.query.aggregations.Raw) FieldExpression(com.yahoo.bullet.query.expressions.FieldExpression) Test(org.testng.annotations.Test) BulletConfigTest(com.yahoo.bullet.common.BulletConfigTest)

Example 5 with Expression

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()));
}
Also used : Window(com.yahoo.bullet.query.Window) Query(com.yahoo.bullet.query.Query) ListExpression(com.yahoo.bullet.query.expressions.ListExpression) Projection(com.yahoo.bullet.query.Projection) Raw(com.yahoo.bullet.query.aggregations.Raw) FieldExpression(com.yahoo.bullet.query.expressions.FieldExpression) RecordBox(com.yahoo.bullet.result.RecordBox) Field(com.yahoo.bullet.query.Field) 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) ValueExpression(com.yahoo.bullet.query.expressions.ValueExpression) BulletConfig(com.yahoo.bullet.common.BulletConfig) Test(org.testng.annotations.Test) BulletConfigTest(com.yahoo.bullet.common.BulletConfigTest)

Aggregations

Expression (com.yahoo.bullet.query.expressions.Expression)14 FieldExpression (com.yahoo.bullet.query.expressions.FieldExpression)13 Query (com.yahoo.bullet.query.Query)12 BinaryExpression (com.yahoo.bullet.query.expressions.BinaryExpression)12 ValueExpression (com.yahoo.bullet.query.expressions.ValueExpression)12 BulletConfigTest (com.yahoo.bullet.common.BulletConfigTest)10 Projection (com.yahoo.bullet.query.Projection)10 Window (com.yahoo.bullet.query.Window)10 Raw (com.yahoo.bullet.query.aggregations.Raw)10 ListExpression (com.yahoo.bullet.query.expressions.ListExpression)10 UnaryExpression (com.yahoo.bullet.query.expressions.UnaryExpression)10 Test (org.testng.annotations.Test)10 BulletRecord (com.yahoo.bullet.record.BulletRecord)7 Field (com.yahoo.bullet.query.Field)5 BulletConfig (com.yahoo.bullet.common.BulletConfig)4 TableFunction (com.yahoo.bullet.query.tablefunctions.TableFunction)4 HashMap (java.util.HashMap)4 Operation (com.yahoo.bullet.query.expressions.Operation)3 Computation (com.yahoo.bullet.query.postaggregations.Computation)3 Culling (com.yahoo.bullet.query.postaggregations.Culling)3