Search in sources :

Example 11 with Expression

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

the class StormSqlExpressionTest method testCreateFunctionDuplicate.

@Test
public void testCreateFunctionDuplicate() throws Exception {
    Condition condition = new Condition();
    Expression f1 = new FunctionExpression("FLOOR", "com.hortonworks.streamline.Floor", ImmutableList.of(new Literal("100.5")));
    Expression f2 = new FunctionExpression("FLOOR", "com.hortonworks.streamline.Floor", ImmutableList.of(new Literal("2.5")));
    Expression expression1 = new BinaryExpression(Operator.GREATER_THAN, new FieldExpression(Schema.Field.of("x", Schema.Type.INTEGER)), f1);
    Expression expression2 = new BinaryExpression(Operator.GREATER_THAN, new FieldExpression(Schema.Field.of("x", Schema.Type.INTEGER)), f2);
    Expression expression = new BinaryExpression(Operator.AND, expression1, expression2);
    condition.setExpression(expression);
    stormSqlExpression = new StormSqlExpression(condition);
    assertEquals(Arrays.asList("CREATE FUNCTION FLOOR AS 'com.hortonworks.streamline.Floor'"), stormSqlExpression.createFunctions());
}
Also used : Condition(com.hortonworks.streamline.streams.layout.component.rule.expression.Condition) FunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression) AggregateFunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) FunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression) AggregateFunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression) 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) Literal(com.hortonworks.streamline.streams.layout.component.rule.expression.Literal) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Test(org.junit.Test)

Example 12 with Expression

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

the class StormSqlExpressionTest method testCreateSelectDuplicateField.

@Test
public void testCreateSelectDuplicateField() throws Exception {
    Condition condition = new Condition();
    Expression expression1 = new BinaryExpression(Operator.GREATER_THAN, new FieldExpression(Schema.Field.of("x", Schema.Type.INTEGER)), new Literal("100"));
    Expression expression2 = new BinaryExpression(Operator.GREATER_THAN, new FieldExpression(Schema.Field.of("x", Schema.Type.INTEGER)), new Literal("50"));
    Expression expression = new BinaryExpression(Operator.AND, expression1, expression2);
    condition.setExpression(expression);
    stormSqlExpression = new StormSqlExpression(condition);
    assertEquals("CREATE EXTERNAL TABLE RULETABLE (\"x\" INTEGER) LOCATION 'schema:///RULETABLE'", stormSqlExpression.createTable("schema"));
    assertEquals("SELECT STREAM \"x\" FROM RULETABLE WHERE RULETABLE.\"x\" > 100 AND RULETABLE.\"x\" > 50", stormSqlExpression.select());
}
Also used : Condition(com.hortonworks.streamline.streams.layout.component.rule.expression.Condition) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) FunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression) AggregateFunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression) 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) Literal(com.hortonworks.streamline.streams.layout.component.rule.expression.Literal) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Test(org.junit.Test)

Example 13 with Expression

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

the class StormSqlExpressionTest method testCreateSelectProjectGroupBy.

@Test
public void testCreateSelectProjectGroupBy() throws Exception {
    // SELECT STREAM ID, MIN(SALARY) FROM FOO where ID > 0 GROUP BY (ID) HAVING ID > 2 AND MAX(SALARY) > 5
    Expression min_salary = new AggregateFunctionExpression("MIN", ImmutableList.of(new FieldExpression(Schema.Field.of("salary", Schema.Type.INTEGER))));
    Expression max_salary = new AggregateFunctionExpression("MAX", ImmutableList.of(new FieldExpression(Schema.Field.of("salary", Schema.Type.INTEGER))));
    Expression id = new FieldExpression(Schema.Field.of("id", Schema.Type.INTEGER));
    Expression id_gt_0 = new BinaryExpression(Operator.GREATER_THAN, id, new Literal("0"));
    Expression id_gt_2 = new BinaryExpression(Operator.GREATER_THAN, id, new Literal("2"));
    Expression max_salary_gt_5 = new BinaryExpression(Operator.GREATER_THAN, max_salary, new Literal("5"));
    GroupBy groupBy_id = new GroupBy();
    groupBy_id.setExpression(Collections.singletonList(id));
    Having having_id_gt_2 = new Having();
    having_id_gt_2.setExpression(new BinaryExpression(Operator.AND, id_gt_2, max_salary_gt_5));
    Condition condition = new Condition();
    condition.setExpression(id_gt_0);
    Projection projection = new Projection();
    projection.setExpressions(ImmutableList.<Expression>of(id, min_salary));
    stormSqlExpression = new StormSqlExpression(condition, projection, groupBy_id, having_id_gt_2);
    assertEquals("CREATE EXTERNAL TABLE RULETABLE (\"id\" INTEGER PRIMARY KEY, \"salary\" INTEGER) LOCATION 'schema:///RULETABLE'", stormSqlExpression.createTable("schema"));
    assertEquals("SELECT STREAM RULETABLE.\"id\", MIN(RULETABLE.\"salary\") FROM RULETABLE WHERE RULETABLE.\"id\" > 0 GROUP BY RULETABLE.\"id\" HAVING RULETABLE.\"id\" > 2 AND MAX(RULETABLE.\"salary\") > 5", stormSqlExpression.select());
}
Also used : Condition(com.hortonworks.streamline.streams.layout.component.rule.expression.Condition) AggregateFunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression) GroupBy(com.hortonworks.streamline.streams.layout.component.rule.expression.GroupBy) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) FunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression) AggregateFunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression) 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) Literal(com.hortonworks.streamline.streams.layout.component.rule.expression.Literal) Projection(com.hortonworks.streamline.streams.layout.component.rule.expression.Projection) Having(com.hortonworks.streamline.streams.layout.component.rule.expression.Having) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Test(org.junit.Test)

Example 14 with Expression

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

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

the class SqlNestedExprScriptTest method testBasic.

@Test
public void testBasic() throws Exception {
    Condition condition = new Condition();
    Expression x = new FieldExpression(Schema.Field.of("x", Schema.Type.INTEGER));
    condition.setExpression(new BinaryExpression(Operator.NOT_EQUAL, x, new Literal("100")));
    sqlScript = new SqlScript(new StormSqlExpression(condition), new SqlEngine(), new SqlScript.CorrelatedValuesToStreamlineEventConverter(Collections.singletonList("x")));
    Map<String, Object> kv = new HashMap<>();
    kv.put("x", 100);
    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) 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) Test(org.junit.Test)

Aggregations

Expression (com.hortonworks.streamline.streams.layout.component.rule.expression.Expression)17 FieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression)15 BinaryExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression)14 Condition (com.hortonworks.streamline.streams.layout.component.rule.expression.Condition)12 FunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression)11 Literal (com.hortonworks.streamline.streams.layout.component.rule.expression.Literal)11 Test (org.junit.Test)10 AggregateFunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression)8 ArrayFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression)6 MapFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression)6 Projection (com.hortonworks.streamline.streams.layout.component.rule.expression.Projection)5 StormSqlExpression (com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression)4 StreamlineEvent (com.hortonworks.streamline.streams.StreamlineEvent)3 GroupBy (com.hortonworks.streamline.streams.layout.component.rule.expression.GroupBy)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ExpressionTranslator (com.hortonworks.streamline.streams.layout.component.rule.expression.ExpressionTranslator)2 Rule (com.hortonworks.streamline.streams.layout.component.rule.Rule)1 Action (com.hortonworks.streamline.streams.layout.component.rule.action.Action)1 TransformAction (com.hortonworks.streamline.streams.layout.component.rule.action.TransformAction)1