use of com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression 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());
}
use of com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression 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());
}
use of com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression 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());
}
use of com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression 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());
}
use of com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression 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());
}
Aggregations