Search in sources :

Example 1 with ArrayFieldExpression

use of com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression 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 2 with ArrayFieldExpression

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

the class GroovyExpressionTest method testArray.

@Test
public void testArray() throws Exception {
    Condition condition = new Condition();
    condition.setExpression(new BinaryExpression(Operator.LESS_THAN, new ArrayFieldExpression(getVariable("arr"), 100), getVariable("b")));
    GroovyExpression expr = new GroovyExpression(condition);
    Assert.assertEquals("arr[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) Test(org.junit.Test)

Example 3 with ArrayFieldExpression

use of com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression 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 ArrayFieldExpression

use of com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression 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

ArrayFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression)4 BinaryExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression)4 Condition (com.hortonworks.streamline.streams.layout.component.rule.expression.Condition)3 MapFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression)3 Expression (com.hortonworks.streamline.streams.layout.component.rule.expression.Expression)2 FieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression)2 Test (org.junit.Test)2 StreamlineEvent (com.hortonworks.streamline.streams.StreamlineEvent)1 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 Literal (com.hortonworks.streamline.streams.layout.component.rule.expression.Literal)1 StormSqlExpression (com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 SqlCharStringLiteral (org.apache.calcite.sql.SqlCharStringLiteral)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlNumericLiteral (org.apache.calcite.sql.SqlNumericLiteral)1