Search in sources :

Example 1 with StormSqlExpression

use of com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression in project streamline by hortonworks.

the class SqlBasicExprScriptTest method testBasicNoMatch.

@Test
public void testBasicNoMatch() 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 : StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression) Test(org.junit.Test)

Example 2 with StormSqlExpression

use of com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression in project streamline by hortonworks.

the class SqlBasicExprScriptTest 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.EQUALS, 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.assertEquals(1, result.size());
    StreamlineEvent resultEvent = result.iterator().next();
    Assert.assertTrue(EventCorrelationInjector.containsParentIds(resultEvent));
    Assert.assertEquals(Collections.singleton(event.getId()), EventCorrelationInjector.getParentIds(resultEvent));
}
Also used : StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression) Test(org.junit.Test)

Example 3 with StormSqlExpression

use of com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression in project streamline by hortonworks.

the class RuleProcessorRuntime method createSqlExpression.

private StormSqlExpression createSqlExpression(Rule rule) {
    List<Expression> groupByExpressions = new ArrayList<>();
    if (rule.getWindow() != null) {
        groupByExpressions.addAll(GROUP_BY_WINDOWID.getExpressions());
    }
    if (rule.getGroupBy() != null) {
        groupByExpressions.addAll(rule.getGroupBy().getExpressions());
    }
    StormSqlExpression stormSqlExpression = new StormSqlExpression(rule.getCondition(), rule.getProjection(), groupByExpressions.isEmpty() ? null : new GroupBy(groupByExpressions), groupByExpressions.isEmpty() ? null : rule.getHaving());
    LOG.info("Built stormSqlExpression {}", stormSqlExpression);
    return stormSqlExpression;
}
Also used : GroupBy(com.hortonworks.streamline.streams.layout.component.rule.expression.GroupBy) GroovyExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.GroovyExpression) 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) ArrayList(java.util.ArrayList) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression)

Example 4 with StormSqlExpression

use of com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression in project streamline by hortonworks.

the class RuleProcessorRuntime method createSqlScript.

private Script createSqlScript(Rule rule) {
    SqlEngine sqlEngine = new SqlEngine();
    LOG.info("Built sqlEngine {}", sqlEngine);
    StormSqlExpression stormSqlExpression = createSqlExpression(rule);
    SqlScript sqlScript = new SqlScript(stormSqlExpression, sqlEngine);
    LOG.info("Built SqlScript {}", sqlScript);
    SqlScript.CorrelatedValuesToStreamlineEventConverter valuesConverter = new SqlScript.CorrelatedValuesToStreamlineEventConverter(sqlScript.getOutputFields());
    sqlScript.setValuesConverter(valuesConverter);
    LOG.info("valuesConverter {}", valuesConverter);
    return sqlScript;
}
Also used : SqlEngine(com.hortonworks.streamline.streams.runtime.rule.sql.SqlEngine) SqlScript(com.hortonworks.streamline.streams.runtime.rule.sql.SqlScript) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression)

Example 5 with StormSqlExpression

use of com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression in project streamline by hortonworks.

the class SqlBasicExprScriptTest method testBasicAggregation.

@Test
public void testBasicAggregation() throws Exception {
    // SELECT STREAM DEPTID, EMPID, MIN(SALARY) FROM FOO where ID > 0 GROUP BY DEPTID, EMPID
    Expression min_salary = new AggregateFunctionExpression("MIN", ImmutableList.of(new FieldExpression(Schema.Field.of("salary", Schema.Type.INTEGER))));
    Expression deptid = new FieldExpression(Schema.Field.of("deptid", Schema.Type.INTEGER));
    Expression empid = new FieldExpression(Schema.Field.of("empid", Schema.Type.INTEGER));
    GroupBy groupBy = new GroupBy(ImmutableList.of(deptid, empid));
    Expression id = new FieldExpression(Schema.Field.of("id", Schema.Type.INTEGER));
    Expression id_gt_0 = new BinaryExpression(Operator.GREATER_THAN, id, new Literal("0"));
    Condition condition = new Condition();
    condition.setExpression(id_gt_0);
    Projection projection = new Projection();
    projection.setExpressions(ImmutableList.<Expression>of(deptid, empid, min_salary));
    sqlScript = new SqlScript(new StormSqlExpression(condition, projection, groupBy, null), new SqlEngine(), new SqlScript.CorrelatedValuesToStreamlineEventConverter(Lists.newArrayList("deptid", "empid", "MIN")));
    // (100, 100), 10
    Map<String, Object> kv1 = new HashMap<>();
    kv1.put("id", 1);
    kv1.put("deptid", 100);
    kv1.put("empid", 100);
    kv1.put("salary", 10);
    // (100, 100), 5
    Map<String, Object> kv2 = new HashMap<>();
    kv2.put("id", 2);
    kv2.put("deptid", 100);
    kv2.put("empid", 100);
    kv2.put("salary", 5);
    // (101, 101), 10
    Map<String, Object> kv3 = new HashMap<>();
    kv3.put("id", 3);
    kv3.put("deptid", 101);
    kv3.put("empid", 101);
    kv3.put("salary", 10);
    // (102, 102), 5
    Map<String, Object> kv4 = new HashMap<>();
    kv4.put("id", 4);
    kv4.put("deptid", 102);
    kv4.put("empid", 102);
    kv4.put("salary", 5);
    StreamlineEvent group1Event1 = StreamlineEventImpl.builder().fieldsAndValues(kv1).dataSourceId("1").build();
    StreamlineEvent group1Event2 = StreamlineEventImpl.builder().fieldsAndValues(kv2).dataSourceId("1").build();
    StreamlineEvent group2Event1 = StreamlineEventImpl.builder().fieldsAndValues(kv3).dataSourceId("1").build();
    StreamlineEvent group3Event1 = StreamlineEventImpl.builder().fieldsAndValues(kv4).dataSourceId("1").build();
    sqlScript.evaluate(group1Event1);
    sqlScript.evaluate(group1Event2);
    sqlScript.evaluate(group2Event1);
    sqlScript.evaluate(group3Event1);
    Collection<StreamlineEvent> result = sqlScript.evaluate(StreamlineEventImpl.GROUP_BY_TRIGGER_EVENT);
    Assert.assertEquals(3, result.size());
    Map<List<Integer>, Pair<Integer, Set<String>>> expectedGroupValueToMinAndParentIds;
    expectedGroupValueToMinAndParentIds = new HashMap<>();
    expectedGroupValueToMinAndParentIds.put(Lists.newArrayList(100, 100), new ImmutablePair<>(5, Sets.newHashSet(group1Event1.getId(), group1Event2.getId())));
    expectedGroupValueToMinAndParentIds.put(Lists.newArrayList(101, 101), new ImmutablePair<>(10, Sets.newHashSet(group2Event1.getId())));
    expectedGroupValueToMinAndParentIds.put(Lists.newArrayList(102, 102), new ImmutablePair<>(5, Sets.newHashSet(group3Event1.getId())));
    result.iterator().forEachRemaining(res -> {
        List<Integer> groupValue = Lists.newArrayList((Integer) res.get("deptid"), (Integer) res.get("empid"));
        Assert.assertTrue(expectedGroupValueToMinAndParentIds.containsKey(groupValue));
        Pair<Integer, Set<String>> minAndPairIds = expectedGroupValueToMinAndParentIds.get(groupValue);
        Integer minValue = (Integer) res.get("MIN");
        Assert.assertEquals(minValue, minAndPairIds.getLeft());
        Assert.assertTrue(EventCorrelationInjector.containsParentIds(res));
        Assert.assertEquals(minAndPairIds.getRight(), EventCorrelationInjector.getParentIds(res));
        // avoid matching multiple times
        expectedGroupValueToMinAndParentIds.remove(groupValue);
    });
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Pair(org.apache.commons.lang3.tuple.Pair) StreamlineEvent(com.hortonworks.streamline.streams.StreamlineEvent) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression) Test(org.junit.Test)

Aggregations

StormSqlExpression (com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression)8 StreamlineEvent (com.hortonworks.streamline.streams.StreamlineEvent)6 Expression (com.hortonworks.streamline.streams.layout.component.rule.expression.Expression)4 FieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression)4 Test (org.junit.Test)4 ArrayFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression)3 BinaryExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression)3 Condition (com.hortonworks.streamline.streams.layout.component.rule.expression.Condition)3 Literal (com.hortonworks.streamline.streams.layout.component.rule.expression.Literal)3 MapFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression)3 HashMap (java.util.HashMap)3 ArrayList (java.util.ArrayList)2 ImmutableList (com.google.common.collect.ImmutableList)1 GroupBy (com.hortonworks.streamline.streams.layout.component.rule.expression.GroupBy)1 GroovyExpression (com.hortonworks.streamline.streams.runtime.rule.condition.expression.GroovyExpression)1 SqlEngine (com.hortonworks.streamline.streams.runtime.rule.sql.SqlEngine)1 SqlScript (com.hortonworks.streamline.streams.runtime.rule.sql.SqlScript)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 Pair (org.apache.commons.lang3.tuple.Pair)1