Search in sources :

Example 1 with ConstantExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor in project ballerina by ballerina-lang.

the class ExpressionParser method parseExpression.

/**
 * Parse the given expression and create the appropriate Executor by recursively traversing the expression
 *
 * @param expression              Expression to be parsed
 * @param metaEvent               Meta Event
 * @param currentState            Current state number
 * @param tableMap                Event Table Map
 * @param executorList            List to hold VariableExpressionExecutors to update after query parsing @return
 * @param siddhiAppContext        SiddhiAppContext
 * @param groupBy                 is for groupBy expression
 * @param defaultStreamEventIndex Default StreamEvent Index
 * @param queryName               query name of expression belongs to.
 * @return ExpressionExecutor
 */
public static ExpressionExecutor parseExpression(Expression expression, MetaComplexEvent metaEvent, int currentState, Map<String, Table> tableMap, List<VariableExpressionExecutor> executorList, SiddhiAppContext siddhiAppContext, boolean groupBy, int defaultStreamEventIndex, String queryName) {
    try {
        if (expression instanceof And) {
            return new AndConditionExpressionExecutor(parseExpression(((And) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((And) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
        } else if (expression instanceof Or) {
            return new OrConditionExpressionExecutor(parseExpression(((Or) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Or) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
        } else if (expression instanceof Not) {
            return new NotConditionExpressionExecutor(parseExpression(((Not) expression).getExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
        } else if (expression instanceof Compare) {
            if (((Compare) expression).getOperator() == Compare.Operator.EQUAL) {
                return parseEqualCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
            } else if (((Compare) expression).getOperator() == Compare.Operator.NOT_EQUAL) {
                return parseNotEqualCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
            } else if (((Compare) expression).getOperator() == Compare.Operator.GREATER_THAN) {
                return parseGreaterThanCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
            } else if (((Compare) expression).getOperator() == Compare.Operator.GREATER_THAN_EQUAL) {
                return parseGreaterThanEqualCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
            } else if (((Compare) expression).getOperator() == Compare.Operator.LESS_THAN) {
                return parseLessThanCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
            } else if (((Compare) expression).getOperator() == Compare.Operator.LESS_THAN_EQUAL) {
                return parseLessThanEqualCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
            }
        } else if (expression instanceof Constant) {
            if (expression instanceof BoolConstant) {
                return new ConstantExpressionExecutor(((BoolConstant) expression).getValue(), Attribute.Type.BOOL);
            } else if (expression instanceof StringConstant) {
                return new ConstantExpressionExecutor(((StringConstant) expression).getValue(), Attribute.Type.STRING);
            } else if (expression instanceof IntConstant) {
                return new ConstantExpressionExecutor(((IntConstant) expression).getValue(), Attribute.Type.INT);
            } else if (expression instanceof LongConstant) {
                return new ConstantExpressionExecutor(((LongConstant) expression).getValue(), Attribute.Type.LONG);
            } else if (expression instanceof FloatConstant) {
                return new ConstantExpressionExecutor(((FloatConstant) expression).getValue(), Attribute.Type.FLOAT);
            } else if (expression instanceof DoubleConstant) {
                return new ConstantExpressionExecutor(((DoubleConstant) expression).getValue(), Attribute.Type.DOUBLE);
            }
        } else if (expression instanceof Variable) {
            return parseVariable((Variable) expression, metaEvent, currentState, executorList, defaultStreamEventIndex);
        } else if (expression instanceof Multiply) {
            ExpressionExecutor left = parseExpression(((Multiply) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            ExpressionExecutor right = parseExpression(((Multiply) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            Attribute.Type type = parseArithmeticOperationResultType(left, right);
            switch(type) {
                case INT:
                    return new MultiplyExpressionExecutorInt(left, right);
                case LONG:
                    return new MultiplyExpressionExecutorLong(left, right);
                case FLOAT:
                    return new MultiplyExpressionExecutorFloat(left, right);
                case DOUBLE:
                    return new MultiplyExpressionExecutorDouble(left, right);
                // Will not happen. Handled in parseArithmeticOperationResultType()
                default:
            }
        } else if (expression instanceof Add) {
            ExpressionExecutor left = parseExpression(((Add) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            ExpressionExecutor right = parseExpression(((Add) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            Attribute.Type type = parseArithmeticOperationResultType(left, right);
            switch(type) {
                case INT:
                    return new AddExpressionExecutorInt(left, right);
                case LONG:
                    return new AddExpressionExecutorLong(left, right);
                case FLOAT:
                    return new AddExpressionExecutorFloat(left, right);
                case DOUBLE:
                    return new AddExpressionExecutorDouble(left, right);
                // Will not happen. Handled in parseArithmeticOperationResultType()
                default:
            }
        } else if (expression instanceof Subtract) {
            ExpressionExecutor left = parseExpression(((Subtract) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            ExpressionExecutor right = parseExpression(((Subtract) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            Attribute.Type type = parseArithmeticOperationResultType(left, right);
            switch(type) {
                case INT:
                    return new SubtractExpressionExecutorInt(left, right);
                case LONG:
                    return new SubtractExpressionExecutorLong(left, right);
                case FLOAT:
                    return new SubtractExpressionExecutorFloat(left, right);
                case DOUBLE:
                    return new SubtractExpressionExecutorDouble(left, right);
                // Will not happen. Handled in parseArithmeticOperationResultType()
                default:
            }
        } else if (expression instanceof Mod) {
            ExpressionExecutor left = parseExpression(((Mod) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            ExpressionExecutor right = parseExpression(((Mod) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            Attribute.Type type = parseArithmeticOperationResultType(left, right);
            switch(type) {
                case INT:
                    return new ModExpressionExecutorInt(left, right);
                case LONG:
                    return new ModExpressionExecutorLong(left, right);
                case FLOAT:
                    return new ModExpressionExecutorFloat(left, right);
                case DOUBLE:
                    return new ModExpressionExecutorDouble(left, right);
                // Will not happen. Handled in parseArithmeticOperationResultType()
                default:
            }
        } else if (expression instanceof Divide) {
            ExpressionExecutor left = parseExpression(((Divide) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            ExpressionExecutor right = parseExpression(((Divide) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            Attribute.Type type = parseArithmeticOperationResultType(left, right);
            switch(type) {
                case INT:
                    return new DivideExpressionExecutorInt(left, right);
                case LONG:
                    return new DivideExpressionExecutorLong(left, right);
                case FLOAT:
                    return new DivideExpressionExecutorFloat(left, right);
                case DOUBLE:
                    return new DivideExpressionExecutorDouble(left, right);
                // Will not happen. Handled in parseArithmeticOperationResultType()
                default:
            }
        } else if (expression instanceof AttributeFunction) {
            // extensions
            Object executor;
            try {
                if ((siddhiAppContext.isFunctionExist(((AttributeFunction) expression).getName())) && (((AttributeFunction) expression).getNamespace()).isEmpty()) {
                    executor = new ScriptFunctionExecutor(((AttributeFunction) expression).getName());
                } else {
                    executor = SiddhiClassLoader.loadExtensionImplementation((AttributeFunction) expression, FunctionExecutorExtensionHolder.getInstance(siddhiAppContext));
                }
            } catch (SiddhiAppCreationException ex) {
                try {
                    executor = SiddhiClassLoader.loadExtensionImplementation((AttributeFunction) expression, AttributeAggregatorExtensionHolder.getInstance(siddhiAppContext));
                } catch (SiddhiAppCreationException e) {
                    throw new SiddhiAppCreationException("'" + ((AttributeFunction) expression).getName() + "' is" + " neither a function extension nor an aggregated attribute extension", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
                }
            }
            ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(((AttributeFunction) expression).getNamespace(), ((AttributeFunction) expression).getName());
            if (executor instanceof FunctionExecutor) {
                FunctionExecutor expressionExecutor = (FunctionExecutor) executor;
                Expression[] innerExpressions = ((AttributeFunction) expression).getParameters();
                ExpressionExecutor[] innerExpressionExecutors = parseInnerExpression(innerExpressions, metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
                expressionExecutor.initExecutor(innerExpressionExecutors, siddhiAppContext, queryName, configReader);
                if (expressionExecutor.getReturnType() == Attribute.Type.BOOL) {
                    return new BoolConditionExpressionExecutor(expressionExecutor);
                }
                return expressionExecutor;
            } else {
                AttributeAggregator attributeAggregator = (AttributeAggregator) executor;
                Expression[] innerExpressions = ((AttributeFunction) expression).getParameters();
                ExpressionExecutor[] innerExpressionExecutors = parseInnerExpression(innerExpressions, metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
                attributeAggregator.initAggregator(innerExpressionExecutors, siddhiAppContext, configReader);
                AbstractAggregationAttributeExecutor aggregationAttributeProcessor;
                if (groupBy) {
                    aggregationAttributeProcessor = new GroupByAggregationAttributeExecutor(attributeAggregator, innerExpressionExecutors, configReader, siddhiAppContext, queryName);
                } else {
                    aggregationAttributeProcessor = new AggregationAttributeExecutor(attributeAggregator, innerExpressionExecutors, siddhiAppContext, queryName);
                }
                SelectorParser.getContainsAggregatorThreadLocal().set("true");
                return aggregationAttributeProcessor;
            }
        } else if (expression instanceof In) {
            Table table = tableMap.get(((In) expression).getSourceId());
            MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(metaEvent, defaultStreamEventIndex, table.getTableDefinition(), defaultStreamEventIndex);
            CompiledCondition compiledCondition = table.compileCondition(((In) expression).getExpression(), matchingMetaInfoHolder, siddhiAppContext, executorList, tableMap, queryName);
            return new InConditionExpressionExecutor(table, compiledCondition, matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents().length, metaEvent instanceof StateEvent, 0);
        } else if (expression instanceof IsNull) {
            IsNull isNull = (IsNull) expression;
            if (isNull.getExpression() != null) {
                ExpressionExecutor innerExpressionExecutor = parseExpression(isNull.getExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
                return new IsNullConditionExpressionExecutor(innerExpressionExecutor);
            } else {
                String streamId = isNull.getStreamId();
                Integer streamIndex = isNull.getStreamIndex();
                if (metaEvent instanceof MetaStateEvent) {
                    int[] eventPosition = new int[2];
                    if (streamIndex != null) {
                        if (streamIndex <= LAST) {
                            eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = streamIndex + 1;
                        } else {
                            eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = streamIndex;
                        }
                    } else {
                        eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = defaultStreamEventIndex;
                    }
                    eventPosition[STREAM_EVENT_CHAIN_INDEX] = UNKNOWN_STATE;
                    MetaStateEvent metaStateEvent = (MetaStateEvent) metaEvent;
                    if (streamId == null) {
                        throw new SiddhiAppCreationException("IsNull does not support streamId being null", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
                    } else {
                        MetaStreamEvent[] metaStreamEvents = metaStateEvent.getMetaStreamEvents();
                        for (int i = 0, metaStreamEventsLength = metaStreamEvents.length; i < metaStreamEventsLength; i++) {
                            MetaStreamEvent metaStreamEvent = metaStreamEvents[i];
                            AbstractDefinition definition = metaStreamEvent.getLastInputDefinition();
                            if (metaStreamEvent.getInputReferenceId() == null) {
                                if (definition.getId().equals(streamId)) {
                                    eventPosition[STREAM_EVENT_CHAIN_INDEX] = i;
                                    break;
                                }
                            } else {
                                if (metaStreamEvent.getInputReferenceId().equals(streamId)) {
                                    eventPosition[STREAM_EVENT_CHAIN_INDEX] = i;
                                    if (currentState > -1 && metaStreamEvents[currentState].getInputReferenceId() != null && streamIndex != null && streamIndex <= LAST) {
                                        if (streamId.equals(metaStreamEvents[currentState].getInputReferenceId())) {
                                            eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = streamIndex;
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    return new IsNullStreamConditionExpressionExecutor(eventPosition);
                } else {
                    return new IsNullStreamConditionExpressionExecutor(null);
                }
            }
        }
        throw new UnsupportedOperationException(expression.toString() + " not supported!");
    } catch (Throwable t) {
        ExceptionUtil.populateQueryContext(t, expression, siddhiAppContext);
        throw t;
    }
}
Also used : Add(org.ballerinalang.siddhi.query.api.expression.math.Add) Or(org.ballerinalang.siddhi.query.api.expression.condition.Or) DivideExpressionExecutorFloat(org.ballerinalang.siddhi.core.executor.math.divide.DivideExpressionExecutorFloat) ModExpressionExecutorDouble(org.ballerinalang.siddhi.core.executor.math.mod.ModExpressionExecutorDouble) ModExpressionExecutorInt(org.ballerinalang.siddhi.core.executor.math.mod.ModExpressionExecutorInt) EqualCompareConditionExpressionExecutorStringString(org.ballerinalang.siddhi.core.executor.condition.compare.equal.EqualCompareConditionExpressionExecutorStringString) NotEqualCompareConditionExpressionExecutorStringString(org.ballerinalang.siddhi.core.executor.condition.compare.notequal.NotEqualCompareConditionExpressionExecutorStringString) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) Divide(org.ballerinalang.siddhi.query.api.expression.math.Divide) MultiplyExpressionExecutorLong(org.ballerinalang.siddhi.core.executor.math.multiply.MultiplyExpressionExecutorLong) AddExpressionExecutorLong(org.ballerinalang.siddhi.core.executor.math.add.AddExpressionExecutorLong) Multiply(org.ballerinalang.siddhi.query.api.expression.math.Multiply) AddExpressionExecutorInt(org.ballerinalang.siddhi.core.executor.math.add.AddExpressionExecutorInt) Compare(org.ballerinalang.siddhi.query.api.expression.condition.Compare) DivideExpressionExecutorDouble(org.ballerinalang.siddhi.core.executor.math.divide.DivideExpressionExecutorDouble) ScriptFunctionExecutor(org.ballerinalang.siddhi.core.executor.function.ScriptFunctionExecutor) FunctionExecutor(org.ballerinalang.siddhi.core.executor.function.FunctionExecutor) AddExpressionExecutorFloat(org.ballerinalang.siddhi.core.executor.math.add.AddExpressionExecutorFloat) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) AttributeAggregator(org.ballerinalang.siddhi.core.query.selector.attribute.aggregator.AttributeAggregator) AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition) GroupByAggregationAttributeExecutor(org.ballerinalang.siddhi.core.query.selector.attribute.processor.executor.GroupByAggregationAttributeExecutor) Not(org.ballerinalang.siddhi.query.api.expression.condition.Not) MultiplyExpressionExecutorFloat(org.ballerinalang.siddhi.core.executor.math.multiply.MultiplyExpressionExecutorFloat) CompiledCondition(org.ballerinalang.siddhi.core.util.collection.operator.CompiledCondition) And(org.ballerinalang.siddhi.query.api.expression.condition.And) MatchingMetaInfoHolder(org.ballerinalang.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) SubtractExpressionExecutorLong(org.ballerinalang.siddhi.core.executor.math.subtract.SubtractExpressionExecutorLong) IsNullStreamConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.IsNullStreamConditionExpressionExecutor) Subtract(org.ballerinalang.siddhi.query.api.expression.math.Subtract) StringConstant(org.ballerinalang.siddhi.query.api.expression.constant.StringConstant) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) DoubleConstant(org.ballerinalang.siddhi.query.api.expression.constant.DoubleConstant) AbstractAggregationAttributeExecutor(org.ballerinalang.siddhi.core.query.selector.attribute.processor.executor.AbstractAggregationAttributeExecutor) Variable(org.ballerinalang.siddhi.query.api.expression.Variable) MultiplyExpressionExecutorDouble(org.ballerinalang.siddhi.core.executor.math.multiply.MultiplyExpressionExecutorDouble) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) In(org.ballerinalang.siddhi.query.api.expression.condition.In) FloatConstant(org.ballerinalang.siddhi.query.api.expression.constant.FloatConstant) StringConstant(org.ballerinalang.siddhi.query.api.expression.constant.StringConstant) LongConstant(org.ballerinalang.siddhi.query.api.expression.constant.LongConstant) DoubleConstant(org.ballerinalang.siddhi.query.api.expression.constant.DoubleConstant) BoolConstant(org.ballerinalang.siddhi.query.api.expression.constant.BoolConstant) IntConstant(org.ballerinalang.siddhi.query.api.expression.constant.IntConstant) Constant(org.ballerinalang.siddhi.query.api.expression.constant.Constant) ModExpressionExecutorLong(org.ballerinalang.siddhi.core.executor.math.mod.ModExpressionExecutorLong) ScriptFunctionExecutor(org.ballerinalang.siddhi.core.executor.function.ScriptFunctionExecutor) InConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.InConditionExpressionExecutor) FloatConstant(org.ballerinalang.siddhi.query.api.expression.constant.FloatConstant) AddExpressionExecutorDouble(org.ballerinalang.siddhi.core.executor.math.add.AddExpressionExecutorDouble) AndConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor) MultiplyExpressionExecutorInt(org.ballerinalang.siddhi.core.executor.math.multiply.MultiplyExpressionExecutorInt) IsNullConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.IsNullConditionExpressionExecutor) SubtractExpressionExecutorInt(org.ballerinalang.siddhi.core.executor.math.subtract.SubtractExpressionExecutorInt) IntConstant(org.ballerinalang.siddhi.query.api.expression.constant.IntConstant) SubtractExpressionExecutorDouble(org.ballerinalang.siddhi.core.executor.math.subtract.SubtractExpressionExecutorDouble) GroupByAggregationAttributeExecutor(org.ballerinalang.siddhi.core.query.selector.attribute.processor.executor.GroupByAggregationAttributeExecutor) AggregationAttributeExecutor(org.ballerinalang.siddhi.core.query.selector.attribute.processor.executor.AggregationAttributeExecutor) AbstractAggregationAttributeExecutor(org.ballerinalang.siddhi.core.query.selector.attribute.processor.executor.AbstractAggregationAttributeExecutor) LongConstant(org.ballerinalang.siddhi.query.api.expression.constant.LongConstant) BoolConstant(org.ballerinalang.siddhi.query.api.expression.constant.BoolConstant) Mod(org.ballerinalang.siddhi.query.api.expression.math.Mod) Table(org.ballerinalang.siddhi.core.table.Table) InConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.InConditionExpressionExecutor) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) NotConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.NotConditionExpressionExecutor) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) BoolConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.BoolConditionExpressionExecutor) IsNullStreamConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.IsNullStreamConditionExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) OrConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.OrConditionExpressionExecutor) AndConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor) IsNullConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.IsNullConditionExpressionExecutor) ConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.ConditionExpressionExecutor) ConfigReader(org.ballerinalang.siddhi.core.util.config.ConfigReader) OrConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.OrConditionExpressionExecutor) DivideExpressionExecutorLong(org.ballerinalang.siddhi.core.executor.math.divide.DivideExpressionExecutorLong) AttributeFunction(org.ballerinalang.siddhi.query.api.expression.AttributeFunction) DivideExpressionExecutorInt(org.ballerinalang.siddhi.core.executor.math.divide.DivideExpressionExecutorInt) SubtractExpressionExecutorFloat(org.ballerinalang.siddhi.core.executor.math.subtract.SubtractExpressionExecutorFloat) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent) NotConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.NotConditionExpressionExecutor) ModExpressionExecutorFloat(org.ballerinalang.siddhi.core.executor.math.mod.ModExpressionExecutorFloat) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent) StateEvent(org.ballerinalang.siddhi.core.event.state.StateEvent) IsNull(org.ballerinalang.siddhi.query.api.expression.condition.IsNull) BoolConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.BoolConditionExpressionExecutor)

Example 2 with ConstantExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor in project ballerina by ballerina-lang.

the class JoinInputStreamParser method insertJoinProcessorsAndGetFindable.

private static FindableProcessor insertJoinProcessorsAndGetFindable(JoinProcessor preJoinProcessor, JoinProcessor postJoinProcessor, SingleStreamRuntime streamRuntime, SiddhiAppContext siddhiAppContext, boolean outputExpectsExpiredEvents, String queryName, InputStream inputStream) {
    Processor lastProcessor = streamRuntime.getProcessorChain();
    Processor prevLastProcessor = null;
    if (lastProcessor != null) {
        while (lastProcessor.getNextProcessor() != null) {
            prevLastProcessor = lastProcessor;
            lastProcessor = lastProcessor.getNextProcessor();
        }
    }
    if (lastProcessor == null) {
        try {
            WindowProcessor windowProcessor = new LengthWindowProcessor();
            ExpressionExecutor[] expressionExecutors = new ExpressionExecutor[1];
            expressionExecutors[0] = new ConstantExpressionExecutor(0, Attribute.Type.INT);
            ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader("", "length");
            windowProcessor.initProcessor(((MetaStreamEvent) streamRuntime.getMetaComplexEvent()).getLastInputDefinition(), expressionExecutors, configReader, siddhiAppContext, outputExpectsExpiredEvents, queryName, inputStream);
            lastProcessor = windowProcessor;
        } catch (Throwable t) {
            throw new SiddhiAppCreationException(t);
        }
    }
    if (lastProcessor instanceof FindableProcessor) {
        if (prevLastProcessor != null) {
            prevLastProcessor.setNextProcessor(preJoinProcessor);
        } else {
            streamRuntime.setProcessorChain(preJoinProcessor);
        }
        preJoinProcessor.setNextProcessor(lastProcessor);
        lastProcessor.setNextProcessor(postJoinProcessor);
        return (FindableProcessor) lastProcessor;
    } else {
        throw new OperationNotSupportedException("Stream " + ((MetaStreamEvent) streamRuntime.getMetaComplexEvent()).getLastInputDefinition().getId() + "'s last processor " + lastProcessor.getClass().getCanonicalName() + " is not an instance of " + FindableProcessor.class.getCanonicalName() + " hence join cannot be proceed");
    }
}
Also used : OperationNotSupportedException(org.ballerinalang.siddhi.core.exception.OperationNotSupportedException) LengthWindowProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.LengthWindowProcessor) FindableProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.FindableProcessor) Processor(org.ballerinalang.siddhi.core.query.processor.Processor) LengthWindowProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.LengthWindowProcessor) AggregateWindowProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.AggregateWindowProcessor) JoinProcessor(org.ballerinalang.siddhi.core.query.input.stream.join.JoinProcessor) FindableProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.FindableProcessor) WindowProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.WindowProcessor) TableWindowProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.TableWindowProcessor) WindowWindowProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.WindowWindowProcessor) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(org.ballerinalang.siddhi.core.util.config.ConfigReader) LengthWindowProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.LengthWindowProcessor) AggregateWindowProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.AggregateWindowProcessor) WindowProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.WindowProcessor) TableWindowProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.TableWindowProcessor) WindowWindowProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.WindowWindowProcessor) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)

Example 3 with ConstantExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor in project ballerina by ballerina-lang.

the class SelectorParser method parse.

/**
 * Parse Selector portion of a query and return corresponding QuerySelector.
 *
 * @param selector                    selector to be parsed
 * @param outputStream                output stream
 * @param siddhiAppContext            query to be parsed
 * @param metaComplexEvent            Meta event used to collect execution info of stream associated with query
 * @param tableMap                    Table Map
 * @param variableExpressionExecutors variable expression executors
 * @param queryName                   query name of selector belongs to.
 * @param metaPosition                helps to identify the meta position of aggregates
 * @return QuerySelector
 */
public static QuerySelector parse(Selector selector, OutputStream outputStream, SiddhiAppContext siddhiAppContext, MetaComplexEvent metaComplexEvent, Map<String, Table> tableMap, List<VariableExpressionExecutor> variableExpressionExecutors, String queryName, int metaPosition) {
    boolean currentOn = false;
    boolean expiredOn = false;
    String id = null;
    if (outputStream.getOutputEventType() == OutputStream.OutputEventType.CURRENT_EVENTS || outputStream.getOutputEventType() == OutputStream.OutputEventType.ALL_EVENTS) {
        currentOn = true;
    }
    if (outputStream.getOutputEventType() == OutputStream.OutputEventType.EXPIRED_EVENTS || outputStream.getOutputEventType() == OutputStream.OutputEventType.ALL_EVENTS) {
        expiredOn = true;
    }
    id = outputStream.getId();
    containsAggregatorThreadLocal.remove();
    QuerySelector querySelector = new QuerySelector(id, selector, currentOn, expiredOn, siddhiAppContext);
    List<AttributeProcessor> attributeProcessors = getAttributeProcessors(selector, id, siddhiAppContext, metaComplexEvent, tableMap, variableExpressionExecutors, outputStream, queryName, metaPosition);
    querySelector.setAttributeProcessorList(attributeProcessors, "true".equals(containsAggregatorThreadLocal.get()));
    containsAggregatorThreadLocal.remove();
    ConditionExpressionExecutor havingCondition = generateHavingExecutor(selector.getHavingExpression(), metaComplexEvent, siddhiAppContext, tableMap, variableExpressionExecutors, queryName);
    querySelector.setHavingConditionExecutor(havingCondition, "true".equals(containsAggregatorThreadLocal.get()));
    containsAggregatorThreadLocal.remove();
    if (!selector.getGroupByList().isEmpty()) {
        querySelector.setGroupByKeyGenerator(new GroupByKeyGenerator(selector.getGroupByList(), metaComplexEvent, SiddhiConstants.UNKNOWN_STATE, null, variableExpressionExecutors, siddhiAppContext, queryName));
    }
    if (!selector.getOrderByList().isEmpty()) {
        querySelector.setOrderByEventComparator(new OrderByEventComparator(selector.getOrderByList(), metaComplexEvent, SiddhiConstants.HAVING_STATE, null, variableExpressionExecutors, siddhiAppContext, queryName));
    }
    if (selector.getLimit() != null) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression((Expression) selector.getLimit(), metaComplexEvent, SiddhiConstants.HAVING_STATE, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
        containsAggregatorThreadLocal.remove();
        querySelector.setLimit(((Number) (((ConstantExpressionExecutor) expressionExecutor).getValue())).longValue());
    }
    return querySelector;
}
Also used : ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) ConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.ConditionExpressionExecutor) GroupByKeyGenerator(org.ballerinalang.siddhi.core.query.selector.GroupByKeyGenerator) QuerySelector(org.ballerinalang.siddhi.core.query.selector.QuerySelector) AttributeProcessor(org.ballerinalang.siddhi.core.query.selector.attribute.processor.AttributeProcessor) ConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.ConditionExpressionExecutor) OrderByEventComparator(org.ballerinalang.siddhi.core.query.selector.OrderByEventComparator)

Example 4 with ConstantExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor in project ballerina by ballerina-lang.

the class TimeLengthWindowProcessor method init.

@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, boolean outputExpectsExpiredEvents, SiddhiAppContext siddhiAppContext) {
    this.siddhiAppContext = siddhiAppContext;
    expiredEventChunk = new ComplexEventChunk<StreamEvent>(false);
    if (attributeExpressionExecutors.length == 2) {
        length = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue();
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
                timeInMilliSeconds = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
            } else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.LONG) {
                timeInMilliSeconds = (Long) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
            } else {
                throw new SiddhiAppValidationException("TimeLength window's first parameter attribute should " + "be either int or long, but found " + attributeExpressionExecutors[0].getReturnType());
            }
        } else {
            throw new SiddhiAppValidationException("TimeLength window should have constant parameter " + "attributes but found a dynamic attribute " + attributeExpressionExecutors[0].getClass().getCanonicalName());
        }
    } else {
        throw new SiddhiAppValidationException("TimeLength window should only have two parameters (<int> " + "windowTime,<int> windowLength), but found " + attributeExpressionExecutors.length + " input " + "attributes");
    }
}
Also used : StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) SiddhiAppValidationException(org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)

Example 5 with ConstantExpressionExecutor

use of org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor in project ballerina by ballerina-lang.

the class AbstractQueryableRecordTable method compileSelection.

public CompiledSelection compileSelection(Selector selector, List<Attribute> expectedOutputAttributes, MatchingMetaInfoHolder matchingMetaInfoHolder, SiddhiAppContext siddhiAppContext, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, String queryName) {
    List<OutputAttribute> outputAttributes = selector.getSelectionList();
    if (outputAttributes.size() == 0) {
        MetaStreamEvent metaStreamEvent = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvent(matchingMetaInfoHolder.getStoreEventIndex());
        List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
        for (Attribute attribute : attributeList) {
            outputAttributes.add(new OutputAttribute(new Variable(attribute.getName())));
        }
    }
    List<SelectAttributeBuilder> selectAttributeBuilders = new ArrayList<>(outputAttributes.size());
    for (OutputAttribute outputAttribute : outputAttributes) {
        ExpressionBuilder expressionBuilder = new ExpressionBuilder(outputAttribute.getExpression(), matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
        selectAttributeBuilders.add(new SelectAttributeBuilder(expressionBuilder, outputAttribute.getRename()));
    }
    List<ExpressionBuilder> groupByExpressionBuilders = null;
    if (selector.getGroupByList().size() != 0) {
        groupByExpressionBuilders = new ArrayList<>(outputAttributes.size());
        for (Variable variable : selector.getGroupByList()) {
            groupByExpressionBuilders.add(new ExpressionBuilder(variable, matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName));
        }
    }
    ExpressionBuilder havingExpressionBuilder = null;
    if (selector.getHavingExpression() != null) {
        havingExpressionBuilder = new ExpressionBuilder(selector.getHavingExpression(), matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
    }
    List<OrderByAttributeBuilder> orderByAttributeBuilders = null;
    if (selector.getOrderByList().size() != 0) {
        orderByAttributeBuilders = new ArrayList<>(selector.getOrderByList().size());
        for (OrderByAttribute orderByAttribute : selector.getOrderByList()) {
            ExpressionBuilder expressionBuilder = new ExpressionBuilder(orderByAttribute.getVariable(), matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
            orderByAttributeBuilders.add(new OrderByAttributeBuilder(expressionBuilder, orderByAttribute.getOrder()));
        }
    }
    Long limit = null;
    if (selector.getLimit() != null) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression((Expression) selector.getLimit(), matchingMetaInfoHolder.getMetaStateEvent(), SiddhiConstants.HAVING_STATE, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
        limit = ((Number) (((ConstantExpressionExecutor) expressionExecutor).getValue())).longValue();
    }
    CompiledSelection compiledSelection = compileSelection(selectAttributeBuilders, groupByExpressionBuilders, havingExpressionBuilder, orderByAttributeBuilders, limit);
    Map<String, ExpressionExecutor> expressionExecutorMap = new HashMap<>();
    if (selectAttributeBuilders.size() != 0) {
        for (SelectAttributeBuilder selectAttributeBuilder : selectAttributeBuilders) {
            expressionExecutorMap.putAll(selectAttributeBuilder.getExpressionBuilder().getVariableExpressionExecutorMap());
        }
    }
    if (groupByExpressionBuilders != null && groupByExpressionBuilders.size() != 0) {
        for (ExpressionBuilder groupByExpressionBuilder : groupByExpressionBuilders) {
            expressionExecutorMap.putAll(groupByExpressionBuilder.getVariableExpressionExecutorMap());
        }
    }
    if (havingExpressionBuilder != null) {
        expressionExecutorMap.putAll(havingExpressionBuilder.getVariableExpressionExecutorMap());
    }
    if (orderByAttributeBuilders != null && orderByAttributeBuilders.size() != 0) {
        for (OrderByAttributeBuilder orderByAttributeBuilder : orderByAttributeBuilders) {
            expressionExecutorMap.putAll(orderByAttributeBuilder.getExpressionBuilder().getVariableExpressionExecutorMap());
        }
    }
    return new RecordStoreCompiledSelection(expressionExecutorMap, compiledSelection);
}
Also used : Variable(org.ballerinalang.siddhi.query.api.expression.Variable) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) 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) OrderByAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OrderByAttribute) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OutputAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OutputAttribute) CompiledSelection(org.ballerinalang.siddhi.core.util.collection.operator.CompiledSelection) OrderByAttribute(org.ballerinalang.siddhi.query.api.execution.query.selection.OrderByAttribute) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

ConstantExpressionExecutor (org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)15 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)10 ExpressionExecutor (org.ballerinalang.siddhi.core.executor.ExpressionExecutor)9 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)6 Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)6 SiddhiAppValidationException (org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException)6 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)5 AndConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)2 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)2 ConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.ConditionExpressionExecutor)2 GreaterThanCompareConditionExpressionExecutorIntInt (org.ballerinalang.siddhi.core.executor.condition.compare.greaterthan.GreaterThanCompareConditionExpressionExecutorIntInt)2 AddExpressionExecutorFloat (org.ballerinalang.siddhi.core.executor.math.add.AddExpressionExecutorFloat)2 ConfigReader (org.ballerinalang.siddhi.core.util.config.ConfigReader)2 Variable (org.ballerinalang.siddhi.query.api.expression.Variable)2 Test (org.testng.annotations.Test)2 List (java.util.List)1 Map (java.util.Map)1 MetaStateEvent (org.ballerinalang.siddhi.core.event.state.MetaStateEvent)1