Search in sources :

Example 6 with Expression

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

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

the class StormSqlExpression method handleProjection.

private void handleProjection() {
    if (projection != null) {
        for (Expression expr : projection.getExpressions()) {
            ExpressionTranslator translator = new StormSqlExpressionTranslator();
            expr.accept(translator);
            stormSqlFields.addAll(translator.getFields());
            functions.addAll(translator.getFunctions());
            aggregateFunctions.addAll(translator.getAggregateFunctions());
            projectedFields.add(translator.getTranslatedExpression());
            if (!translator.getAliases().isEmpty()) {
                outputFields.add(translator.getAliases().get(0));
            } else {
                outputFields.add(translator.getUnquotedTranslatedExpression());
            }
        }
    }
}
Also used : FunctionExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression) Expression(com.hortonworks.streamline.streams.layout.component.rule.expression.Expression) ExpressionTranslator(com.hortonworks.streamline.streams.layout.component.rule.expression.ExpressionTranslator)

Example 8 with Expression

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

the class GroovyExpressionTest method testOrString.

@Test
public void testOrString() throws Exception {
    Condition condition = new Condition();
    Expression left = new BinaryExpression(Operator.LESS_THAN, getVariable("a"), getVariable("b"));
    Expression right = new BinaryExpression(Operator.GREATER_THAN, getVariable("c"), getVariable("d"));
    condition.setExpression(new BinaryExpression(Operator.OR, left, right));
    GroovyExpression expr = new GroovyExpression(condition);
    Assert.assertEquals("a < b || c > d", expr.asString());
}
Also used : Condition(com.hortonworks.streamline.streams.layout.component.rule.expression.Condition) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) ArrayFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Expression(com.hortonworks.streamline.streams.layout.component.rule.expression.Expression) Test(org.junit.Test)

Example 9 with Expression

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

the class GroovyExpressionTest method testAndAsString.

@Test
public void testAndAsString() throws Exception {
    Condition condition = new Condition();
    Expression left = new BinaryExpression(Operator.OR, getVariable("a"), getVariable("b"));
    Expression right = new BinaryExpression(Operator.OR, getVariable("c"), getVariable("d"));
    condition.setExpression(new BinaryExpression(Operator.AND, left, right));
    GroovyExpression expr = new GroovyExpression(condition);
    Assert.assertEquals("(a || b) && (c || d)", expr.asString());
}
Also used : Condition(com.hortonworks.streamline.streams.layout.component.rule.expression.Condition) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) ArrayFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression) BinaryExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression) MapFieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression) FieldExpression(com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression) Expression(com.hortonworks.streamline.streams.layout.component.rule.expression.Expression) Test(org.junit.Test)

Example 10 with Expression

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

Expression (com.hortonworks.streamline.streams.layout.component.rule.expression.Expression)17 FieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FieldExpression)15 BinaryExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.BinaryExpression)14 Condition (com.hortonworks.streamline.streams.layout.component.rule.expression.Condition)12 FunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.FunctionExpression)11 Literal (com.hortonworks.streamline.streams.layout.component.rule.expression.Literal)11 Test (org.junit.Test)10 AggregateFunctionExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.AggregateFunctionExpression)8 ArrayFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.ArrayFieldExpression)6 MapFieldExpression (com.hortonworks.streamline.streams.layout.component.rule.expression.MapFieldExpression)6 Projection (com.hortonworks.streamline.streams.layout.component.rule.expression.Projection)5 StormSqlExpression (com.hortonworks.streamline.streams.runtime.rule.condition.expression.StormSqlExpression)4 StreamlineEvent (com.hortonworks.streamline.streams.StreamlineEvent)3 GroupBy (com.hortonworks.streamline.streams.layout.component.rule.expression.GroupBy)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ExpressionTranslator (com.hortonworks.streamline.streams.layout.component.rule.expression.ExpressionTranslator)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