Search in sources :

Example 6 with Literal

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

the class RulesProcessorMock method getRule.

private Rule getRule(long ruleId, Condition condition, TransformAction action) {
    Rule rule = new Rule();
    rule.setId(ruleId);
    rule.setName(RULE + "_" + ruleId);
    rule.setDescription(RULE + "_" + ruleId + "_desc");
    rule.setRuleProcessorName(RULE_PROCESSOR + "_" + ruleProcessorId);
    rule.setCondition(condition);
    if (ruleId % 2 == 0) {
        Projection projection = new Projection();
        Expression humidity = new FieldExpression(Field.of("humidity", Schema.Type.INTEGER));
        Expression deviceName = new FieldExpression(Field.of("devicename", Schema.Type.STRING));
        Expression incr = new FunctionExpression("INCR", "com.hortonworks.streamline.streams.runtime.storm.layout.runtime.rule.topology.RulesProcessorMock$Incr", ImmutableList.<Expression>of(humidity, new Literal("10")));
        Expression upper = new FunctionExpression("UPPER", ImmutableList.<Expression>of(deviceName));
        projection.setExpressions(ImmutableList.<Expression>of(humidity, incr, upper));
        rule.setProjection(projection);
    }
    rule.setActions(Collections.singletonList((Action) action));
    return rule;
}
Also used : Action(com.hortonworks.streamline.streams.layout.component.rule.action.Action) TransformAction(com.hortonworks.streamline.streams.layout.component.rule.action.TransformAction) FunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression) FunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression) 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) Rule(com.hortonworks.streamline.streams.layout.component.rule.Rule) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression)

Example 7 with Literal

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

the class RuleParserTest method testParseStringLiteral.

@Test
public void testParseStringLiteral() throws Exception {
    new Expectations() {

        {
            mockCatalogService.listStreamInfos(withAny(new ArrayList<QueryParam>()));
            result = mockTopologyStream;
            mockTopologyStream.getStreamId();
            result = "teststream";
            mockTopologyStream.getFields();
            result = Arrays.asList(Schema.Field.of("eventType", Schema.Type.STRING), Schema.Field.of("temperature", Schema.Type.LONG));
        }
    };
    TopologyRule topologyRule = new TopologyRule();
    topologyRule.setId(1L);
    topologyRule.setName("Test");
    topologyRule.setDescription("test rule");
    topologyRule.setTopologyId(1L);
    topologyRule.setVersionId(1L);
    topologyRule.setSql("select temperature from teststream where eventType <> 'Normal'");
    RuleParser ruleParser = new RuleParser(mockCatalogService, topologyRule.getSql(), topologyRule.getTopologyId(), topologyRule.getVersionId());
    ruleParser.parse();
    assertTrue(ruleParser.getCondition().getExpression() instanceof BinaryExpression);
    assertTrue(((BinaryExpression) ruleParser.getCondition().getExpression()).getSecond() instanceof Literal);
    Literal literal = ((Literal) ((BinaryExpression) ruleParser.getCondition().getExpression()).getSecond());
    assertEquals("'Normal'", literal.getValue());
}
Also used : Expectations(mockit.Expectations) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) Literal(com.hortonworks.streamline.streams.layout.component.rule.expression.Literal) ArrayList(java.util.ArrayList) TopologyRule(com.hortonworks.streamline.streams.catalog.TopologyRule) Test(org.junit.Test)

Example 8 with Literal

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

the class RuleParserTest method testParseAsExpressionWithCase.

@Test
public void testParseAsExpressionWithCase() throws Exception {
    new Expectations() {

        {
            mockCatalogService.listStreamInfos(withAny(new ArrayList<QueryParam>()));
            result = mockTopologyStream;
            mockTopologyStream.getStreamId();
            result = "teststream";
            mockTopologyStream.getFields();
            result = Arrays.asList(Schema.Field.of("temperature", Schema.Type.LONG), Schema.Field.of("humidity", Schema.Type.LONG));
        }
    };
    TopologyRule topologyRule = new TopologyRule();
    topologyRule.setId(1L);
    topologyRule.setName("Test");
    topologyRule.setDescription("test rule");
    topologyRule.setTopologyId(1L);
    topologyRule.setVersionId(1L);
    topologyRule.setSql("select temperature as \"temp_TEST\" from teststream where humidity > 80");
    RuleParser ruleParser = new RuleParser(mockCatalogService, topologyRule.getSql(), topologyRule.getTopologyId(), topologyRule.getVersionId());
    ruleParser.parse();
    assertEquals(new Condition(new BinaryExpression(Operator.GREATER_THAN, new FieldExpression(Schema.Field.of("humidity", Schema.Type.LONG)), new Literal("80"))), ruleParser.getCondition());
    assertEquals(new Projection(Arrays.asList(new AsExpression(new FieldExpression(Schema.Field.of("temperature", Schema.Type.LONG)), "temp_TEST"))), ruleParser.getProjection());
    assertEquals(1, ruleParser.getStreams().size());
    assertEquals(new Stream("teststream", Arrays.asList(Schema.Field.of("temperature", Schema.Type.LONG), Schema.Field.of("humidity", Schema.Type.LONG))), ruleParser.getStreams().get(0));
    assertNull(ruleParser.getGroupBy());
    assertNull(ruleParser.getHaving());
}
Also used : Expectations(mockit.Expectations) Condition(com.hortonworks.streamline.streams.layout.component.rule.expression.Condition) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) Literal(com.hortonworks.streamline.streams.layout.component.rule.expression.Literal) ArrayList(java.util.ArrayList) Projection(com.hortonworks.streamline.streams.layout.component.rule.expression.Projection) TopologyStream(com.hortonworks.streamline.streams.catalog.TopologyStream) Stream(com.hortonworks.streamline.streams.layout.component.Stream) TopologyRule(com.hortonworks.streamline.streams.catalog.TopologyRule) AsExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Test(org.junit.Test)

Example 9 with Literal

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

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

Aggregations

BinaryExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression)14 Literal (com.hortonworks.streamline.streams.layout.component.rule.expression.Literal)14 FieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression)13 Condition (com.hortonworks.streamline.streams.layout.component.rule.expression.Condition)12 Expression (com.hortonworks.streamline.streams.layout.component.rule.expression.Expression)11 Test (org.junit.Test)11 FunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression)8 AggregateFunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression)7 Projection (com.hortonworks.streamline.streams.layout.component.rule.expression.Projection)7 ArrayList (java.util.ArrayList)4 StreamlineEvent (com.hortonworks.streamline.streams.StreamlineEvent)3 TopologyRule (com.hortonworks.streamline.streams.catalog.TopologyRule)3 ArrayFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression)3 MapFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression)3 StormSqlExpression (com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression)3 HashMap (java.util.HashMap)3 Expectations (mockit.Expectations)3 TopologyStream (com.hortonworks.streamline.streams.catalog.TopologyStream)2 Stream (com.hortonworks.streamline.streams.layout.component.Stream)2 AsExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression)2