Search in sources :

Example 1 with GroupBy

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

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

the class RuleParser method parseGroupBy.

private GroupBy parseGroupBy(SqlSelect sqlSelect) {
    GroupBy groupBy = null;
    SqlNodeList sqlGroupBy = sqlSelect.getGroup();
    if (sqlGroupBy != null) {
        ExpressionGenerator exprGenerator = new ExpressionGenerator(streams, catalogUdfs);
        ExpressionList exprList = (ExpressionList) sqlGroupBy.accept(exprGenerator);
        groupBy = new GroupBy(exprList.getExpressions());
        referredUdfs.addAll(exprGenerator.getReferredUdfs());
    }
    LOG.debug("GroupBy {}", groupBy);
    return groupBy;
}
Also used : GroupBy(com.hortonworks.streamline.streams.layout.component.rule.expression.GroupBy) SqlNodeList(org.apache.calcite.sql.SqlNodeList) ExpressionList(com.hortonworks.streamline.streams.layout.component.rule.expression.ExpressionList) ExpressionGenerator(com.hortonworks.streamline.streams.layout.component.rule.sql.ExpressionGenerator)

Example 3 with GroupBy

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

the class RuleProcessorRuntime method createSqlExpression.

private StormSqlExpression createSqlExpression(Rule rule) {
    List<Expression> groupByExpressions = new ArrayList<>();
    if (rule.getWindow() != null) {
        groupByExpressions.addAll(GROUP_BY_WINDOWID.getExpressions());
    }
    if (rule.getGroupBy() != null) {
        groupByExpressions.addAll(rule.getGroupBy().getExpressions());
    }
    StormSqlExpression stormSqlExpression = new StormSqlExpression(rule.getCondition(), rule.getProjection(), groupByExpressions.isEmpty() ? null : new GroupBy(groupByExpressions), groupByExpressions.isEmpty() ? null : rule.getHaving());
    LOG.info("Built stormSqlExpression {}", stormSqlExpression);
    return stormSqlExpression;
}
Also used : GroupBy(com.hortonworks.streamline.streams.layout.component.rule.expression.GroupBy) GroovyExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.GroovyExpression) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Expression(com.hortonworks.streamline.streams.layout.component.rule.expression.Expression) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression) ArrayList(java.util.ArrayList) StormSqlExpression(com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression)

Example 4 with GroupBy

use of com.hortonworks.streamline.streams.layout.component.rule.expression.GroupBy 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());
}
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) Having(com.hortonworks.streamline.streams.layout.component.rule.expression.Having) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Test(org.junit.Test)

Aggregations

GroupBy (com.hortonworks.streamline.streams.layout.component.rule.expression.GroupBy)4 Expression (com.hortonworks.streamline.streams.layout.component.rule.expression.Expression)3 FieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression)3 AggregateFunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression)2 BinaryExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression)2 Condition (com.hortonworks.streamline.streams.layout.component.rule.expression.Condition)2 FunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression)2 Literal (com.hortonworks.streamline.streams.layout.component.rule.expression.Literal)2 Projection (com.hortonworks.streamline.streams.layout.component.rule.expression.Projection)2 Test (org.junit.Test)2 ExpressionList (com.hortonworks.streamline.streams.layout.component.rule.expression.ExpressionList)1 Having (com.hortonworks.streamline.streams.layout.component.rule.expression.Having)1 ExpressionGenerator (com.hortonworks.streamline.streams.layout.component.rule.sql.ExpressionGenerator)1 GroovyExpression (com.hortonworks.streamline.streams.runtime.rule.condition.expression.GroovyExpression)1 StormSqlExpression (com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression)1 ArrayList (java.util.ArrayList)1 SqlNodeList (org.apache.calcite.sql.SqlNodeList)1