use of com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression in project streamline by hortonworks.
the class RuleParserTest method testParseSimple.
@Test
public void testParseSimple() 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 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"))), 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());
assertTrue(ruleParser.getCondition().getExpression() instanceof BinaryExpression);
assertTrue(((BinaryExpression) ruleParser.getCondition().getExpression()).getSecond() instanceof Literal);
Literal literal = ((Literal) ((BinaryExpression) ruleParser.getCondition().getExpression()).getSecond());
assertEquals("80", literal.getValue());
}
use of com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression 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.AsExpression 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");
}
}
Aggregations