Search in sources :

Example 1 with MapFieldExpression

use of com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression in project streamline by hortonworks.

the class GroovyExpressionTest method testMap.

@Test
public void testMap() throws Exception {
    Condition condition = new Condition();
    condition.setExpression(new BinaryExpression(Operator.LESS_THAN, new MapFieldExpression(getVariable("map"), "foo"), getVariable("b")));
    GroovyExpression expr = new GroovyExpression(condition);
    Assert.assertEquals("map['foo'] < b", expr.asString());
}
Also used : Condition(com.hortonworks.streamline.streams.layout.component.rule.expression.Condition) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression) Test(org.junit.Test)

Example 2 with MapFieldExpression

use of com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression in project streamline by hortonworks.

the class GroovyExpressionTest method testNested.

@Test
public void testNested() throws Exception {
    Condition condition = new Condition();
    condition.setExpression(new BinaryExpression(Operator.LESS_THAN, new ArrayFieldExpression(new MapFieldExpression(getVariable("map"), "foo"), 100), getVariable("b")));
    GroovyExpression expr = new GroovyExpression(condition);
    Assert.assertEquals("map['foo'][100] < b", expr.asString());
}
Also used : Condition(com.hortonworks.streamline.streams.layout.component.rule.expression.Condition) ArrayFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression) Test(org.junit.Test)

Example 3 with MapFieldExpression

use of com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression in project streamline by hortonworks.

the class ExpressionGenerator method visitSqlSpecialOperator.

private Expression visitSqlSpecialOperator(SqlSpecialOperator specialOperator, List<SqlNode> operands) {
    if (specialOperator.getName().equalsIgnoreCase("ITEM")) {
        Expression left = operands.get(0).accept(this);
        SqlNode right = operands.get(1);
        if (right instanceof SqlNumericLiteral) {
            SqlNumericLiteral index = (SqlNumericLiteral) right;
            if (!index.isInteger()) {
                throw new IllegalArgumentException("Invalid array index " + index);
            }
            return new ArrayFieldExpression(left, Integer.parseInt(index.toValue()));
        } else if (right instanceof SqlCharStringLiteral) {
            String key = ((SqlCharStringLiteral) right).toValue();
            return new MapFieldExpression(left, key);
        } else {
            throw new IllegalArgumentException("Item right operand '" + right + "' must be numeric or character type");
        }
    } else if (specialOperator.getName().equalsIgnoreCase("AS")) {
        Expression left = operands.get(0).accept(this);
        String alias = operands.get(1).toString();
        return new AsExpression(left, alias);
    } else {
        throw new UnsupportedOperationException("Operator " + specialOperator + " not implemented");
    }
}
Also used : ArrayFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression) FunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression) AggregateFunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression) ArrayFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression) AsExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Expression(com.hortonworks.streamline.streams.layout.component.rule.expression.Expression) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression) SqlCharStringLiteral(org.apache.calcite.sql.SqlCharStringLiteral) SqlNumericLiteral(org.apache.calcite.sql.SqlNumericLiteral) AsExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression) SqlNode(org.apache.calcite.sql.SqlNode)

Example 4 with MapFieldExpression

use of com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression in project streamline by hortonworks.

the class SqlNestedExprScriptTest method testEvaluateNestedMap.

// @Test
public void testEvaluateNestedMap() throws Exception {
    Condition condition = new Condition();
    Expression y_b = new MapFieldExpression(new FieldExpression(Schema.Field.of("y", Schema.Type.NESTED)), "b");
    condition.setExpression(new BinaryExpression(Operator.LESS_THAN, y_b, new Literal("100")));
    sqlScript = new SqlScript(new StormSqlExpression(condition), new SqlEngine(), new SqlScript.CorrelatedValuesToStreamlineEventConverter(Collections.singletonList("y")));
    Map<String, Object> nested = new HashMap<>();
    nested.put("a", 5);
    nested.put("b", 10);
    Map<String, Object> kv = new HashMap<>();
    kv.put("x", 10);
    kv.put("y", nested);
    StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(kv).dataSourceId("1").build();
    Collection<StreamlineEvent> result = sqlScript.evaluate(event);
    Assert.assertEquals(1, result.size());
}
Also used : Condition(com.hortonworks.streamline.streams.layout.component.rule.expression.Condition) HashMap(java.util.HashMap) StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression) ArrayFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) ArrayFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Expression(com.hortonworks.streamline.streams.layout.component.rule.expression.Expression) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression) Literal(com.hortonworks.streamline.streams.layout.component.rule.expression.Literal) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression)

Example 5 with MapFieldExpression

use of com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression in project streamline by hortonworks.

the class SqlNestedExprScriptTest method testEvaluateNestedMapList.

// @Test
public void testEvaluateNestedMapList() throws Exception {
    Condition condition = new Condition();
    Expression y_a_0 = new ArrayFieldExpression(new MapFieldExpression(new FieldExpression(Schema.Field.of("y", Schema.Type.NESTED)), "a"), 0);
    condition.setExpression(new BinaryExpression(Operator.LESS_THAN, y_a_0, new Literal("100")));
    sqlScript = new SqlScript(new StormSqlExpression(condition), new SqlEngine(), new SqlScript.CorrelatedValuesToStreamlineEventConverter(Collections.singletonList("y")));
    List<Integer> nestedList = new ArrayList<>();
    nestedList.add(500);
    nestedList.add(1);
    Map<String, Object> nestedMap = new HashMap<>();
    nestedMap.put("a", nestedList);
    Map<String, Object> kv = new HashMap<>();
    kv.put("x", 10);
    kv.put("y", nestedMap);
    StreamlineEvent event = StreamlineEventImpl.builder().fieldsAndValues(kv).dataSourceId("1").build();
    Collection<StreamlineEvent> result = sqlScript.evaluate(event);
    Assert.assertTrue(result.isEmpty());
}
Also used : Condition(com.hortonworks.streamline.streams.layout.component.rule.expression.Condition) ArrayFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression) HashMap(java.util.HashMap) StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) ArrayList(java.util.ArrayList) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression) ArrayFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) ArrayFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Expression(com.hortonworks.streamline.streams.layout.component.rule.expression.Expression) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression) Literal(com.hortonworks.streamline.streams.layout.component.rule.expression.Literal) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression)

Aggregations

BinaryExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression)5 MapFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression)5 ArrayFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression)4 Condition (com.hortonworks.streamline.streams.layout.component.rule.expression.Condition)4 Expression (com.hortonworks.streamline.streams.layout.component.rule.expression.Expression)3 FieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression)3 StreamlineEvent (com.hortonworks.streamline.streams.StreamlineEvent)2 Literal (com.hortonworks.streamline.streams.layout.component.rule.expression.Literal)2 StormSqlExpression (com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 AggregateFunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression)1 AsExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression)1 FunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression)1 ArrayList (java.util.ArrayList)1 SqlCharStringLiteral (org.apache.calcite.sql.SqlCharStringLiteral)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlNumericLiteral (org.apache.calcite.sql.SqlNumericLiteral)1