Search in sources :

Example 1 with AsExpression

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());
}
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 2 with AsExpression

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());
}
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 3 with AsExpression

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");
    }
}
Also used : ArrayFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression) FunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression) AggregateFunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression) ArrayFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression) AsExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression) 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) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression) SqlCharStringLiteral(org.apache.calcite.sql.SqlCharStringLiteral) SqlNumericLiteral(org.apache.calcite.sql.SqlNumericLiteral) AsExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

AsExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression)3 BinaryExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression)3 FieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression)3 TopologyRule (com.hortonworks.streamline.streams.catalog.TopologyRule)2 TopologyStream (com.hortonworks.streamline.streams.catalog.TopologyStream)2 Stream (com.hortonworks.streamline.streams.layout.component.Stream)2 Condition (com.hortonworks.streamline.streams.layout.component.rule.expression.Condition)2 Literal (com.hortonworks.streamline.streams.layout.component.rule.expression.Literal)2 Projection (com.hortonworks.streamline.streams.layout.component.rule.expression.Projection)2 ArrayList (java.util.ArrayList)2 Expectations (mockit.Expectations)2 Test (org.junit.Test)2 AggregateFunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression)1 ArrayFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression)1 Expression (com.hortonworks.streamline.streams.layout.component.rule.expression.Expression)1 FunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression)1 MapFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression)1 SqlCharStringLiteral (org.apache.calcite.sql.SqlCharStringLiteral)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlNumericLiteral (org.apache.calcite.sql.SqlNumericLiteral)1