Search in sources :

Example 1 with Projection

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

the class StormSqlExpressionTest method testGroupByMultipleFields.

@Test
public void testGroupByMultipleFields() throws Exception {
    // SELECT STREAM 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(min_salary));
    stormSqlExpression = new StormSqlExpression(condition, projection, groupBy, null);
    assertEquals("CREATE EXTERNAL TABLE RULETABLE (\"salary\" INTEGER, \"id\" INTEGER, \"deptid\" INTEGER PRIMARY KEY, " + "\"empid\" INTEGER) LOCATION 'schema:///RULETABLE'", stormSqlExpression.createTable("schema"));
    assertEquals("SELECT STREAM MIN(RULETABLE.\"salary\") FROM RULETABLE WHERE RULETABLE.\"id\" > 0 GROUP BY RULETABLE.\"deptid\",RULETABLE.\"empid\"", stormSqlExpression.select());
}
Also used : Condition(com.hortonworks.streamline.streams.layout.component.rule.expression.Condition) AggregateFunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression) GroupBy(com.hortonworks.streamline.streams.layout.component.rule.expression.GroupBy) 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) Projection(com.hortonworks.streamline.streams.layout.component.rule.expression.Projection) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Test(org.junit.Test)

Example 2 with Projection

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

the class StormSqlExpressionTest method testProjectAndCondition.

@Test
public void testProjectAndCondition() throws Exception {
    Expression x = new FieldExpression(Schema.Field.of("x", Schema.Type.INTEGER));
    Expression y = new FieldExpression(Schema.Field.of("y", Schema.Type.INTEGER));
    Expression x_gt_100 = new BinaryExpression(Operator.GREATER_THAN, x, new Literal("100"));
    Projection projection = new Projection();
    projection.setExpressions(ImmutableList.<Expression>of(y));
    Condition condition = new Condition();
    condition.setExpression(x_gt_100);
    stormSqlExpression = new StormSqlExpression(condition, projection);
    assertEquals("CREATE EXTERNAL TABLE RULETABLE (\"y\" INTEGER, \"x\" INTEGER) LOCATION 'schema:///RULETABLE'", stormSqlExpression.createTable("schema"));
    assertEquals("SELECT STREAM RULETABLE.\"y\" FROM RULETABLE WHERE RULETABLE.\"x\" > 100", 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) Projection(com.hortonworks.streamline.streams.layout.component.rule.expression.Projection) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Test(org.junit.Test)

Example 3 with Projection

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

the class StormSqlExpressionTest method testCreateSelectProject.

@Test
public void testCreateSelectProject() throws Exception {
    Condition condition = new Condition();
    Expression function = new FunctionExpression("SUM", "com.hortonworks.streamline.MyPlus", ImmutableList.of(new FieldExpression(Schema.Field.of("x", Schema.Type.INTEGER)), new FieldExpression(Schema.Field.of("y", Schema.Type.INTEGER))));
    Expression z = new FieldExpression(Schema.Field.of("z", Schema.Type.INTEGER));
    Expression z_gt_100 = new BinaryExpression(Operator.GREATER_THAN, z, new Literal("100"));
    condition.setExpression(z_gt_100);
    Projection projection = new Projection();
    projection.setExpressions(ImmutableList.<Expression>of(function));
    stormSqlExpression = new StormSqlExpression(condition, projection);
    assertEquals("CREATE EXTERNAL TABLE RULETABLE (\"x\" INTEGER, \"y\" INTEGER, \"z\" INTEGER) LOCATION 'schema:///RULETABLE'", stormSqlExpression.createTable("schema"));
    assertEquals("SELECT STREAM SUM(RULETABLE.\"x\", RULETABLE.\"y\") FROM RULETABLE WHERE RULETABLE.\"z\" > 100", stormSqlExpression.select());
    assertEquals(Arrays.asList("CREATE FUNCTION SUM AS 'com.hortonworks.streamline.MyPlus'"), 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) Projection(com.hortonworks.streamline.streams.layout.component.rule.expression.Projection) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Test(org.junit.Test)

Example 4 with Projection

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

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

the class RuleParser method parseProjection.

private Projection parseProjection(SqlSelect sqlSelect) {
    Projection projection;
    ExpressionGenerator exprGenerator = new ExpressionGenerator(streams, catalogUdfs);
    ExpressionList exprList = (ExpressionList) sqlSelect.getSelectList().accept(exprGenerator);
    if (exprList.getExpressions().size() == 1 && exprList.getExpressions().get(0) == STAR) {
        projection = null;
    } else {
        projection = new Projection(exprList.getExpressions());
    }
    referredUdfs.addAll(exprGenerator.getReferredUdfs());
    LOG.debug("Projection {}", projection);
    return projection;
}
Also used : Projection(com.hortonworks.streamline.streams.layout.component.rule.expression.Projection) ExpressionList(com.hortonworks.streamline.streams.layout.component.rule.expression.ExpressionList) ExpressionGenerator(com.hortonworks.streamline.streams.layout.component.rule.sql.ExpressionGenerator)

Aggregations

Projection (com.hortonworks.streamline.streams.layout.component.rule.expression.Projection)8 BinaryExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression)7 FieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression)7 Literal (com.hortonworks.streamline.streams.layout.component.rule.expression.Literal)7 Condition (com.hortonworks.streamline.streams.layout.component.rule.expression.Condition)6 Test (org.junit.Test)6 Expression (com.hortonworks.streamline.streams.layout.component.rule.expression.Expression)5 FunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression)5 AggregateFunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression)4 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 AsExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AsExpression)2 GroupBy (com.hortonworks.streamline.streams.layout.component.rule.expression.GroupBy)2 ArrayList (java.util.ArrayList)2 Expectations (mockit.Expectations)2 Rule (com.hortonworks.streamline.streams.layout.component.rule.Rule)1 Action (com.hortonworks.streamline.streams.layout.component.rule.action.Action)1 TransformAction (com.hortonworks.streamline.streams.layout.component.rule.action.TransformAction)1 ExpressionList (com.hortonworks.streamline.streams.layout.component.rule.expression.ExpressionList)1