Search in sources :

Example 21 with Expression

use of org.ballerinalang.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class SiddhiQLBaseVisitorImpl method visitIn_math_operation.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public Object visitIn_math_operation(@NotNull SiddhiQLParser.In_math_operationContext ctx) {
    Expression expression = Expression.in((Expression) visit(ctx.math_operation()), (String) visit(ctx.name()));
    populateQueryContext(expression, ctx);
    return expression;
}
Also used : Expression(org.ballerinalang.siddhi.query.api.expression.Expression)

Example 22 with Expression

use of org.ballerinalang.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class SiddhiQLBaseVisitorImpl method visitAddition_math_operation.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public Expression visitAddition_math_operation(@NotNull SiddhiQLParser.Addition_math_operationContext ctx) {
    Expression expression;
    if (ctx.add != null) {
        expression = Expression.add((Expression) visit(ctx.math_operation(0)), (Expression) visit(ctx.math_operation(1)));
    } else if (ctx.substract != null) {
        expression = Expression.subtract((Expression) visit(ctx.math_operation(0)), (Expression) visit(ctx.math_operation(1)));
    } else {
        throw newSiddhiParserException(ctx);
    }
    populateQueryContext(expression, ctx);
    return expression;
}
Also used : Expression(org.ballerinalang.siddhi.query.api.expression.Expression)

Example 23 with Expression

use of org.ballerinalang.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class SiddhiQLBaseVisitorImpl method visitEquality_math_operation.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public Expression visitEquality_math_operation(@NotNull SiddhiQLParser.Equality_math_operationContext ctx) {
    if (ctx.eq != null) {
        Expression expression = Expression.compare((Expression) visit(ctx.math_operation(0)), Compare.Operator.EQUAL, (Expression) visit(ctx.math_operation(1)));
        populateQueryContext(expression, ctx);
        return expression;
    } else if (ctx.not_eq != null) {
        Expression expression = Expression.compare((Expression) visit(ctx.math_operation(0)), Compare.Operator.NOT_EQUAL, (Expression) visit(ctx.math_operation(1)));
        populateQueryContext(expression, ctx);
        return expression;
    } else {
        throw newSiddhiParserException(ctx);
    }
}
Also used : Expression(org.ballerinalang.siddhi.query.api.expression.Expression)

Example 24 with Expression

use of org.ballerinalang.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class SiddhiQLBaseVisitorImpl method visitFunction_operation.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public Object visitFunction_operation(@NotNull SiddhiQLParser.Function_operationContext ctx) {
    Expression expression;
    if (ctx.function_namespace() != null) {
        if (ctx.attribute_list() != null) {
            expression = Expression.function((String) visit(ctx.function_namespace()), (String) visit(ctx.function_id()), (Expression[]) visit(ctx.attribute_list()));
        } else {
            expression = Expression.function((String) visit(ctx.function_namespace()), (String) visit(ctx.function_id()), null);
        }
    } else {
        if (ctx.attribute_list() != null) {
            expression = Expression.function((String) visit(ctx.function_id()), (Expression[]) visit(ctx.attribute_list()));
        } else {
            expression = Expression.function((String) visit(ctx.function_id()), null);
        }
    }
    populateQueryContext(expression, ctx);
    return expression;
}
Also used : Expression(org.ballerinalang.siddhi.query.api.expression.Expression)

Example 25 with Expression

use of org.ballerinalang.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class AggregationParser method populateIncomingAggregatorsAndExecutors.

private static void populateIncomingAggregatorsAndExecutors(AggregationDefinition aggregationDefinition, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, List<VariableExpressionExecutor> incomingVariableExpressionExecutors, String aggregatorName, MetaStreamEvent incomingMetaStreamEvent, List<ExpressionExecutor> incomingExpressionExecutors, List<IncrementalAttributeAggregator> incrementalAttributeAggregators, List<Variable> groupByVariableList, List<Expression> outputExpressions) {
    ExpressionExecutor[] timeStampTimeZoneExecutors = setTimeStampTimeZoneExecutors(aggregationDefinition, siddhiAppContext, tableMap, incomingVariableExpressionExecutors, aggregatorName, incomingMetaStreamEvent);
    ExpressionExecutor timestampExecutor = timeStampTimeZoneExecutors[0];
    ExpressionExecutor timeZoneExecutor = timeStampTimeZoneExecutors[1];
    Attribute timestampAttribute = new Attribute("AGG_TIMESTAMP", Attribute.Type.LONG);
    incomingMetaStreamEvent.addOutputData(timestampAttribute);
    incomingExpressionExecutors.add(timestampExecutor);
    incomingMetaStreamEvent.addOutputData(new Attribute("AGG_TIMEZONE", Attribute.Type.STRING));
    incomingExpressionExecutors.add(timeZoneExecutor);
    AbstractDefinition incomingLastInputStreamDefinition = incomingMetaStreamEvent.getLastInputDefinition();
    for (Variable groupByVariable : groupByVariableList) {
        incomingMetaStreamEvent.addOutputData(incomingLastInputStreamDefinition.getAttributeList().get(incomingLastInputStreamDefinition.getAttributePosition(groupByVariable.getAttributeName())));
        incomingExpressionExecutors.add(ExpressionParser.parseExpression(groupByVariable, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName));
    }
    // Add AGG_TIMESTAMP to output as well
    outputExpressions.add(Expression.variable("AGG_TIMESTAMP"));
    aggregationDefinition.getAttributeList().add(timestampAttribute);
    for (OutputAttribute outputAttribute : aggregationDefinition.getSelector().getSelectionList()) {
        Expression expression = outputAttribute.getExpression();
        if (expression instanceof AttributeFunction) {
            IncrementalAttributeAggregator incrementalAggregator = null;
            try {
                incrementalAggregator = (IncrementalAttributeAggregator) SiddhiClassLoader.loadExtensionImplementation(new AttributeFunction("incrementalAggregator", ((AttributeFunction) expression).getName(), ((AttributeFunction) expression).getParameters()), IncrementalAttributeAggregatorExtensionHolder.getInstance(siddhiAppContext));
            } catch (SiddhiAppCreationException ex) {
                try {
                    SiddhiClassLoader.loadExtensionImplementation((AttributeFunction) expression, FunctionExecutorExtensionHolder.getInstance(siddhiAppContext));
                    ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(expression, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
                    incomingExpressionExecutors.add(expressionExecutor);
                    incomingMetaStreamEvent.addOutputData(new Attribute(outputAttribute.getRename(), expressionExecutor.getReturnType()));
                    aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), expressionExecutor.getReturnType()));
                    outputExpressions.add(Expression.variable(outputAttribute.getRename()));
                } catch (SiddhiAppCreationException e) {
                    throw new SiddhiAppCreationException("'" + ((AttributeFunction) expression).getName() + "' is neither a incremental attribute aggregator extension or a function" + " extension", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
                }
            }
            if (incrementalAggregator != null) {
                initIncrementalAttributeAggregator(incomingLastInputStreamDefinition, (AttributeFunction) expression, incrementalAggregator);
                incrementalAttributeAggregators.add(incrementalAggregator);
                aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), incrementalAggregator.getReturnType()));
                outputExpressions.add(incrementalAggregator.aggregate());
            }
        } else {
            if (expression instanceof Variable && groupByVariableList.contains(expression)) {
                Attribute groupByAttribute = null;
                for (Attribute attribute : incomingMetaStreamEvent.getOutputData()) {
                    if (attribute.getName().equals(((Variable) expression).getAttributeName())) {
                        groupByAttribute = attribute;
                        break;
                    }
                }
                if (groupByAttribute == null) {
                    throw new SiddhiAppCreationException("Expected GroupBy attribute '" + ((Variable) expression).getAttributeName() + "' not used in aggregation '" + aggregatorName + "' processing.", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
                }
                aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), groupByAttribute.getType()));
                outputExpressions.add(Expression.variable(groupByAttribute.getName()));
            } else {
                ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(expression, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
                incomingExpressionExecutors.add(expressionExecutor);
                incomingMetaStreamEvent.addOutputData(new Attribute(outputAttribute.getRename(), expressionExecutor.getReturnType()));
                aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), expressionExecutor.getReturnType()));
                outputExpressions.add(Expression.variable(outputAttribute.getRename()));
            }
        }
    }
}
Also used : Variable(org.ballerinalang.siddhi.query.api.expression.Variable) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) OutputAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute) Expression(org.ballerinalang.siddhi.query.api.expression.Expression) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) IncrementalAttributeAggregator(org.ballerinalang.siddhi.core.query.selector.attribute.aggregator.incremental.IncrementalAttributeAggregator) AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition) OutputAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute) AttributeFunction(org.ballerinalang.siddhi.query.api.expression.AttributeFunction)

Aggregations

Expression (org.ballerinalang.siddhi.query.api.expression.Expression)32 Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)11 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)10 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)9 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)7 ExpressionExecutor (org.ballerinalang.siddhi.core.executor.ExpressionExecutor)7 Variable (org.ballerinalang.siddhi.query.api.expression.Variable)7 ArrayList (java.util.ArrayList)6 OutputAttribute (org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute)5 MetaStateEvent (org.ballerinalang.siddhi.core.event.state.MetaStateEvent)4 IncrementalAttributeAggregator (org.ballerinalang.siddhi.core.query.selector.attribute.aggregator.incremental.IncrementalAttributeAggregator)4 AttributeFunction (org.ballerinalang.siddhi.query.api.expression.AttributeFunction)4 ReturnAttribute (org.ballerinalang.siddhi.annotation.ReturnAttribute)3 Table (org.ballerinalang.siddhi.core.table.Table)3 Within (org.ballerinalang.siddhi.query.api.aggregation.Within)3 AbstractDefinition (org.ballerinalang.siddhi.query.api.definition.AbstractDefinition)3 AggregationRuntime (org.ballerinalang.siddhi.core.aggregation.AggregationRuntime)2 SingleStreamRuntime (org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime)2 CompiledCondition (org.ballerinalang.siddhi.core.util.collection.operator.CompiledCondition)2 MatchingMetaInfoHolder (org.ballerinalang.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)2