Search in sources :

Example 6 with AbstractDefinition

use of io.siddhi.query.api.definition.AbstractDefinition in project siddhi by wso2.

the class SelectorParser method getAttributeProcessors.

/**
 * Method to construct AttributeProcessor list for the selector.
 *
 * @param selector                    Selector
 * @param id                          stream id
 * @param metaComplexEvent            meta ComplexEvent
 * @param tableMap                    Table Map
 * @param variableExpressionExecutors list of VariableExpressionExecutors
 * @param outputStream                output stream
 * @param processingMode              processing mode of the query
 * @param outputExpectsExpiredEvents  is expired events sent as output
 * @param groupBy                     is Attributes groupBy
 * @param siddhiQueryContext          current siddhi query context  @return list of AttributeProcessors
 */
private static List<AttributeProcessor> getAttributeProcessors(Selector selector, String id, MetaComplexEvent metaComplexEvent, Map<String, Table> tableMap, List<VariableExpressionExecutor> variableExpressionExecutors, OutputStream outputStream, int metaPosition, ProcessingMode processingMode, boolean outputExpectsExpiredEvents, boolean groupBy, SiddhiQueryContext siddhiQueryContext) {
    List<AttributeProcessor> attributeProcessorList = new ArrayList<>();
    StreamDefinition outputDefinition = StreamDefinition.id(id);
    outputDefinition.setQueryContextStartIndex(outputStream.getQueryContextStartIndex());
    outputDefinition.setQueryContextEndIndex(outputStream.getQueryContextEndIndex());
    List<OutputAttribute> outputAttributes = selector.getSelectionList();
    if (selector.getSelectionList().size() == 0) {
        if (metaComplexEvent instanceof MetaStreamEvent) {
            List<Attribute> attributeList = ((MetaStreamEvent) metaComplexEvent).getLastInputDefinition().getAttributeList();
            for (Attribute attribute : attributeList) {
                Variable variable = new Variable(attribute.getName());
                variable.setQueryContextStartIndex(selector.getQueryContextStartIndex());
                variable.setQueryContextEndIndex(selector.getQueryContextEndIndex());
                OutputAttribute outputAttribute = new OutputAttribute(variable);
                outputAttribute.setQueryContextStartIndex(selector.getQueryContextStartIndex());
                outputAttribute.setQueryContextEndIndex(selector.getQueryContextEndIndex());
                outputAttributes.add(outputAttribute);
            }
        } else {
            int position = 0;
            for (MetaStreamEvent metaStreamEvent : ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents()) {
                if (metaPosition == SiddhiConstants.UNKNOWN_STATE || metaPosition == position) {
                    List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
                    for (Attribute attribute : attributeList) {
                        Variable variable = new Variable(attribute.getName());
                        variable.setQueryContextStartIndex(selector.getQueryContextStartIndex());
                        variable.setQueryContextEndIndex(selector.getQueryContextEndIndex());
                        OutputAttribute outputAttribute = new OutputAttribute(variable);
                        outputAttribute.setQueryContextStartIndex(selector.getQueryContextStartIndex());
                        outputAttribute.setQueryContextEndIndex(selector.getQueryContextEndIndex());
                        if (!outputAttributes.contains(outputAttribute)) {
                            outputAttributes.add(outputAttribute);
                        } else {
                            List<AbstractDefinition> definitions = new ArrayList<>();
                            for (MetaStreamEvent aMetaStreamEvent : ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents()) {
                                definitions.add(aMetaStreamEvent.getLastInputDefinition());
                            }
                            throw new DuplicateAttributeException("Duplicate attribute exist in streams " + definitions, outputStream.getQueryContextStartIndex(), outputStream.getQueryContextEndIndex());
                        }
                    }
                }
                ++position;
            }
        }
    }
    int i = 0;
    for (OutputAttribute outputAttribute : outputAttributes) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(outputAttribute.getExpression(), metaComplexEvent, SiddhiConstants.UNKNOWN_STATE, tableMap, variableExpressionExecutors, groupBy, 0, processingMode, outputExpectsExpiredEvents, siddhiQueryContext);
        if (expressionExecutor instanceof VariableExpressionExecutor) {
            // for variables we will directly put
            // value at conversion stage
            VariableExpressionExecutor executor = ((VariableExpressionExecutor) expressionExecutor);
            if (metaComplexEvent instanceof MetaStateEvent) {
                ((MetaStateEvent) metaComplexEvent).addOutputDataAllowingDuplicate(new MetaStateEventAttribute(executor.getAttribute(), executor.getPosition()));
            } else {
                ((MetaStreamEvent) metaComplexEvent).addOutputDataAllowingDuplicate(executor.getAttribute());
            }
            outputDefinition.attribute(outputAttribute.getRename(), ((VariableExpressionExecutor) expressionExecutor).getAttribute().getType());
        } else {
            // To maintain output variable positions
            if (metaComplexEvent instanceof MetaStateEvent) {
                ((MetaStateEvent) metaComplexEvent).addOutputDataAllowingDuplicate(null);
            } else {
                ((MetaStreamEvent) metaComplexEvent).addOutputDataAllowingDuplicate(null);
            }
            AttributeProcessor attributeProcessor = new AttributeProcessor(expressionExecutor);
            attributeProcessor.setOutputPosition(i);
            attributeProcessorList.add(attributeProcessor);
            outputDefinition.attribute(outputAttribute.getRename(), attributeProcessor.getOutputType());
        }
        i++;
    }
    metaComplexEvent.setOutputDefinition(outputDefinition);
    return attributeProcessorList;
}
Also used : Variable(io.siddhi.query.api.expression.Variable) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) ConditionExpressionExecutor(io.siddhi.core.executor.condition.ConditionExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) MetaStateEventAttribute(io.siddhi.core.event.state.MetaStateEventAttribute) Attribute(io.siddhi.query.api.definition.Attribute) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ArrayList(java.util.ArrayList) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) DuplicateAttributeException(io.siddhi.query.api.exception.DuplicateAttributeException) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) MetaStateEventAttribute(io.siddhi.core.event.state.MetaStateEventAttribute) AttributeProcessor(io.siddhi.core.query.selector.attribute.processor.AttributeProcessor) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 7 with AbstractDefinition

use of io.siddhi.query.api.definition.AbstractDefinition in project siddhi by wso2.

the class ExpressionParser method constructEventExpressionExecutor.

private static ExpressionExecutor constructEventExpressionExecutor(Variable variable, int currentState, SiddhiQueryContext siddhiQueryContext, int[] eventPosition, MetaStateEvent metaStateEvent) {
    AbstractDefinition definition;
    MetaStreamEvent[] metaStreamEvents = metaStateEvent.getMetaStreamEvents();
    for (int i = 0, metaStreamEventsLength = metaStreamEvents.length; i < metaStreamEventsLength; i++) {
        MetaStreamEvent metaStreamEvent = metaStreamEvents[i];
        definition = metaStreamEvent.getLastInputDefinition();
        if (metaStreamEvent.getInputReferenceId() == null) {
            if (definition.getId().equals(variable.getAttributeName())) {
                eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = i;
                FunctionExecutor functionExecutor = new EventVariableFunctionExecutor(eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX], eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN]);
                functionExecutor.initExecutor(new ExpressionExecutor[0], ProcessingMode.BATCH, null, false, siddhiQueryContext);
                return functionExecutor;
            }
        } else {
            if (metaStreamEvent.getInputReferenceId().equals(variable.getAttributeName())) {
                eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = i;
                if (currentState > -1 && metaStreamEvents[currentState].getInputReferenceId() != null && variable.getStreamIndex() != null && variable.getStreamIndex() <= SiddhiConstants.LAST) {
                    if (variable.getAttributeName().equals(metaStreamEvents[currentState].getInputReferenceId())) {
                        eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN] = variable.getStreamIndex();
                    }
                }
                FunctionExecutor functionExecutor = new EventVariableFunctionExecutor(eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX], eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN]);
                functionExecutor.initExecutor(new ExpressionExecutor[0], ProcessingMode.BATCH, null, false, siddhiQueryContext);
                return functionExecutor;
            }
        }
    }
    return null;
}
Also used : FunctionExecutor(io.siddhi.core.executor.function.FunctionExecutor) EventVariableFunctionExecutor(io.siddhi.core.executor.EventVariableFunctionExecutor) MultiValueVariableFunctionExecutor(io.siddhi.core.executor.MultiValueVariableFunctionExecutor) ScriptFunctionExecutor(io.siddhi.core.executor.function.ScriptFunctionExecutor) EventVariableFunctionExecutor(io.siddhi.core.executor.EventVariableFunctionExecutor) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 8 with AbstractDefinition

use of io.siddhi.query.api.definition.AbstractDefinition in project siddhi by wso2.

the class OnDemandQueryParser method constructRegularOnDemandQueryRuntime.

private static OnDemandQueryRuntime constructRegularOnDemandQueryRuntime(Table table, OnDemandQuery onDemandQuery, Map<String, Table> tableMap, Map<String, Window> windowMap, int metaPosition, Expression onCondition, MetaStreamEvent metaStreamEvent, List<VariableExpressionExecutor> variableExpressionExecutors, LockWrapper lockWrapper, SiddhiQueryContext siddhiQueryContext) {
    MatchingMetaInfoHolder matchingMetaInfoHolder;
    AbstractDefinition inputDefinition;
    QuerySelector querySelector;
    switch(onDemandQuery.getType()) {
        case FIND:
            initMetaStreamEvent(metaStreamEvent, table.getTableDefinition());
            matchingMetaInfoHolder = generateMatchingMetaInfoHolder(metaStreamEvent, table.getTableDefinition());
            CompiledCondition compiledCondition = table.compileCondition(onCondition, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiQueryContext);
            FindOnDemandQueryRuntime findOnDemandQueryRuntime = new FindOnDemandQueryRuntime(table, compiledCondition, siddhiQueryContext.getName(), metaStreamEvent);
            populateFindOnDemandQueryRuntime(findOnDemandQueryRuntime, matchingMetaInfoHolder, onDemandQuery.getSelector(), variableExpressionExecutors, tableMap, windowMap, metaPosition, !onDemandQuery.getSelector().getGroupByList().isEmpty(), lockWrapper, siddhiQueryContext);
            return findOnDemandQueryRuntime;
        case INSERT:
            initMetaStreamEvent(metaStreamEvent, getInputDefinition(onDemandQuery, table));
            matchingMetaInfoHolder = generateMatchingMetaInfoHolder(metaStreamEvent, table.getTableDefinition());
            querySelector = getQuerySelector(matchingMetaInfoHolder, variableExpressionExecutors, tableMap, windowMap, metaPosition, onDemandQuery, lockWrapper, siddhiQueryContext);
            InsertOnDemandQueryRuntime insertOnDemandQueryRuntime = new InsertOnDemandQueryRuntime(siddhiQueryContext.getName(), metaStreamEvent);
            insertOnDemandQueryRuntime.setStateEventFactory(new StateEventFactory(matchingMetaInfoHolder.getMetaStateEvent()));
            insertOnDemandQueryRuntime.setSelector(querySelector);
            insertOnDemandQueryRuntime.setOutputAttributes(matchingMetaInfoHolder.getMetaStateEvent().getOutputStreamDefinition().getAttributeList());
            return insertOnDemandQueryRuntime;
        case DELETE:
            inputDefinition = getInputDefinition(onDemandQuery, table);
            initMetaStreamEvent(metaStreamEvent, inputDefinition);
            matchingMetaInfoHolder = generateMatchingMetaInfoHolder(metaStreamEvent, inputDefinition, table.getTableDefinition());
            querySelector = getQuerySelector(matchingMetaInfoHolder, variableExpressionExecutors, tableMap, windowMap, metaPosition, onDemandQuery, lockWrapper, siddhiQueryContext);
            DeleteOnDemandQueryRuntime deleteOnDemandQueryRuntime = new DeleteOnDemandQueryRuntime(siddhiQueryContext.getName(), metaStreamEvent);
            deleteOnDemandQueryRuntime.setStateEventFactory(new StateEventFactory(matchingMetaInfoHolder.getMetaStateEvent()));
            deleteOnDemandQueryRuntime.setSelector(querySelector);
            deleteOnDemandQueryRuntime.setOutputAttributes(matchingMetaInfoHolder.getMetaStateEvent().getOutputStreamDefinition().getAttributeList());
            return deleteOnDemandQueryRuntime;
        case UPDATE:
            inputDefinition = getInputDefinition(onDemandQuery, table);
            initMetaStreamEvent(metaStreamEvent, inputDefinition);
            matchingMetaInfoHolder = generateMatchingMetaInfoHolder(metaStreamEvent, inputDefinition, table.getTableDefinition());
            querySelector = getQuerySelector(matchingMetaInfoHolder, variableExpressionExecutors, tableMap, windowMap, metaPosition, onDemandQuery, lockWrapper, siddhiQueryContext);
            UpdateOnDemandQueryRuntime updateOnDemandQueryRuntime = new UpdateOnDemandQueryRuntime(siddhiQueryContext.getName(), metaStreamEvent);
            updateOnDemandQueryRuntime.setStateEventFactory(new StateEventFactory(matchingMetaInfoHolder.getMetaStateEvent()));
            updateOnDemandQueryRuntime.setSelector(querySelector);
            updateOnDemandQueryRuntime.setOutputAttributes(matchingMetaInfoHolder.getMetaStateEvent().getOutputStreamDefinition().getAttributeList());
            return updateOnDemandQueryRuntime;
        case UPDATE_OR_INSERT:
            inputDefinition = getInputDefinition(onDemandQuery, table);
            initMetaStreamEvent(metaStreamEvent, inputDefinition);
            matchingMetaInfoHolder = generateMatchingMetaInfoHolder(metaStreamEvent, inputDefinition, table.getTableDefinition());
            querySelector = getQuerySelector(matchingMetaInfoHolder, variableExpressionExecutors, tableMap, windowMap, metaPosition, onDemandQuery, lockWrapper, siddhiQueryContext);
            UpdateOrInsertOnDemandQueryRuntime updateOrInsertIntoOnDemandQueryRuntime = new UpdateOrInsertOnDemandQueryRuntime(siddhiQueryContext.getName(), metaStreamEvent);
            updateOrInsertIntoOnDemandQueryRuntime.setStateEventFactory(new StateEventFactory(matchingMetaInfoHolder.getMetaStateEvent()));
            updateOrInsertIntoOnDemandQueryRuntime.setSelector(querySelector);
            updateOrInsertIntoOnDemandQueryRuntime.setOutputAttributes(matchingMetaInfoHolder.getMetaStateEvent().getOutputStreamDefinition().getAttributeList());
            return updateOrInsertIntoOnDemandQueryRuntime;
        default:
            return null;
    }
}
Also used : CompiledCondition(io.siddhi.core.util.collection.operator.CompiledCondition) FindOnDemandQueryRuntime(io.siddhi.core.query.FindOnDemandQueryRuntime) UpdateOrInsertOnDemandQueryRuntime(io.siddhi.core.query.UpdateOrInsertOnDemandQueryRuntime) InsertOnDemandQueryRuntime(io.siddhi.core.query.InsertOnDemandQueryRuntime) DeleteOnDemandQueryRuntime(io.siddhi.core.query.DeleteOnDemandQueryRuntime) MatchingMetaInfoHolder(io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) StateEventFactory(io.siddhi.core.event.state.StateEventFactory) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) QuerySelector(io.siddhi.core.query.selector.QuerySelector) UpdateOnDemandQueryRuntime(io.siddhi.core.query.UpdateOnDemandQueryRuntime) UpdateOrInsertOnDemandQueryRuntime(io.siddhi.core.query.UpdateOrInsertOnDemandQueryRuntime)

Example 9 with AbstractDefinition

use of io.siddhi.query.api.definition.AbstractDefinition in project siddhi by wso2.

the class PartitionParser method parse.

public static PartitionRuntimeImpl parse(SiddhiAppRuntimeBuilder siddhiAppRuntimeBuilder, Partition partition, SiddhiAppContext siddhiAppContext, int queryIndex, int partitionIndex) {
    ConcurrentMap<String, AbstractDefinition> streamDefinitionMap = siddhiAppRuntimeBuilder.getStreamDefinitionMap();
    ConcurrentMap<String, AbstractDefinition> windowDefinitionMap = siddhiAppRuntimeBuilder.getWindowDefinitionMap();
    validateStreamPartitions(partition.getPartitionTypeMap(), streamDefinitionMap, windowDefinitionMap);
    PartitionRuntimeImpl partitionRuntime = new PartitionRuntimeImpl(streamDefinitionMap, windowDefinitionMap, siddhiAppRuntimeBuilder.getStreamJunctions(), partition, partitionIndex, siddhiAppContext);
    for (Query query : partition.getQueryList()) {
        List<VariableExpressionExecutor> executors = new ArrayList<VariableExpressionExecutor>();
        ConcurrentMap<String, AbstractDefinition> combinedStreamMap = new ConcurrentHashMap<String, AbstractDefinition>();
        combinedStreamMap.putAll(streamDefinitionMap);
        combinedStreamMap.putAll(windowDefinitionMap);
        combinedStreamMap.putAll(partitionRuntime.getLocalStreamDefinitionMap());
        QueryRuntimeImpl queryRuntime = QueryParser.parse(query, siddhiAppContext, combinedStreamMap, siddhiAppRuntimeBuilder.getTableDefinitionMap(), siddhiAppRuntimeBuilder.getWindowDefinitionMap(), siddhiAppRuntimeBuilder.getAggregationDefinitionMap(), siddhiAppRuntimeBuilder.getTableMap(), siddhiAppRuntimeBuilder.getAggregationMap(), siddhiAppRuntimeBuilder.getWindowMap(), siddhiAppRuntimeBuilder.getLockSynchronizer(), String.valueOf(queryIndex), true, partitionRuntime.getPartitionName());
        queryIndex++;
        MetaStateEvent metaStateEvent = createMetaEventForPartitioner(queryRuntime.getMetaComplexEvent());
        partitionRuntime.addQuery(queryRuntime);
        partitionRuntime.addPartitionReceiver(queryRuntime, executors, metaStateEvent);
        QueryParserHelper.reduceMetaComplexEvent(metaStateEvent);
        if (queryRuntime.getMetaComplexEvent() instanceof MetaStateEvent) {
            QueryParserHelper.updateVariablePosition(metaStateEvent, executors);
        } else {
            QueryParserHelper.updateVariablePosition(metaStateEvent.getMetaStreamEvent(0), executors);
        }
    }
    partitionRuntime.init();
    return partitionRuntime;
}
Also used : QueryRuntimeImpl(io.siddhi.core.query.QueryRuntimeImpl) Query(io.siddhi.query.api.execution.query.Query) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ArrayList(java.util.ArrayList) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PartitionRuntimeImpl(io.siddhi.core.partition.PartitionRuntimeImpl) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent)

Example 10 with AbstractDefinition

use of io.siddhi.query.api.definition.AbstractDefinition in project siddhi by wso2.

the class ExpressionBuilder method buildVariableExecutors.

private void buildVariableExecutors(Expression expression, ExpressionVisitor expressionVisitor) {
    try {
        if (expression instanceof And) {
            expressionVisitor.beginVisitAnd();
            expressionVisitor.beginVisitAndLeftOperand();
            buildVariableExecutors(((And) expression).getLeftExpression(), expressionVisitor);
            expressionVisitor.endVisitAndLeftOperand();
            expressionVisitor.beginVisitAndRightOperand();
            buildVariableExecutors(((And) expression).getRightExpression(), expressionVisitor);
            expressionVisitor.endVisitAndRightOperand();
            expressionVisitor.endVisitAnd();
        } else if (expression instanceof Or) {
            expressionVisitor.beginVisitOr();
            expressionVisitor.beginVisitOrLeftOperand();
            buildVariableExecutors(((Or) expression).getLeftExpression(), expressionVisitor);
            expressionVisitor.endVisitOrLeftOperand();
            expressionVisitor.beginVisitOrRightOperand();
            buildVariableExecutors(((Or) expression).getRightExpression(), expressionVisitor);
            expressionVisitor.endVisitOrRightOperand();
            expressionVisitor.endVisitOr();
        } else if (expression instanceof Not) {
            expressionVisitor.beginVisitNot();
            buildVariableExecutors(((Not) expression).getExpression(), expressionVisitor);
            expressionVisitor.endVisitNot();
        } else if (expression instanceof Compare) {
            expressionVisitor.beginVisitCompare(((Compare) expression).getOperator());
            expressionVisitor.beginVisitCompareLeftOperand(((Compare) expression).getOperator());
            buildVariableExecutors(((Compare) expression).getLeftExpression(), expressionVisitor);
            expressionVisitor.endVisitCompareLeftOperand(((Compare) expression).getOperator());
            expressionVisitor.beginVisitCompareRightOperand(((Compare) expression).getOperator());
            buildVariableExecutors(((Compare) expression).getRightExpression(), expressionVisitor);
            expressionVisitor.endVisitCompareRightOperand(((Compare) expression).getOperator());
            expressionVisitor.endVisitCompare(((Compare) expression).getOperator());
        } else if (expression instanceof Add) {
            expressionVisitor.beginVisitMath(ExpressionVisitor.MathOperator.ADD);
            expressionVisitor.beginVisitMathLeftOperand(ExpressionVisitor.MathOperator.ADD);
            buildVariableExecutors(((Add) expression).getLeftValue(), expressionVisitor);
            expressionVisitor.endVisitMathLeftOperand(ExpressionVisitor.MathOperator.ADD);
            expressionVisitor.beginVisitMathRightOperand(ExpressionVisitor.MathOperator.ADD);
            buildVariableExecutors(((Add) expression).getRightValue(), expressionVisitor);
            expressionVisitor.endVisitMathRightOperand(ExpressionVisitor.MathOperator.ADD);
            expressionVisitor.endVisitMath(ExpressionVisitor.MathOperator.ADD);
        } else if (expression instanceof Subtract) {
            expressionVisitor.beginVisitMath(ExpressionVisitor.MathOperator.SUBTRACT);
            expressionVisitor.beginVisitMathLeftOperand(ExpressionVisitor.MathOperator.SUBTRACT);
            buildVariableExecutors(((Subtract) expression).getLeftValue(), expressionVisitor);
            expressionVisitor.endVisitMathLeftOperand(ExpressionVisitor.MathOperator.SUBTRACT);
            expressionVisitor.beginVisitMathRightOperand(ExpressionVisitor.MathOperator.SUBTRACT);
            buildVariableExecutors(((Subtract) expression).getRightValue(), expressionVisitor);
            expressionVisitor.endVisitMathRightOperand(ExpressionVisitor.MathOperator.SUBTRACT);
            expressionVisitor.endVisitMath(ExpressionVisitor.MathOperator.SUBTRACT);
        } else if (expression instanceof Divide) {
            expressionVisitor.beginVisitMath(ExpressionVisitor.MathOperator.DIVIDE);
            expressionVisitor.beginVisitMathLeftOperand(ExpressionVisitor.MathOperator.DIVIDE);
            buildVariableExecutors(((Divide) expression).getLeftValue(), expressionVisitor);
            expressionVisitor.endVisitMathLeftOperand(ExpressionVisitor.MathOperator.DIVIDE);
            expressionVisitor.beginVisitMathRightOperand(ExpressionVisitor.MathOperator.DIVIDE);
            buildVariableExecutors(((Divide) expression).getRightValue(), expressionVisitor);
            expressionVisitor.endVisitMathRightOperand(ExpressionVisitor.MathOperator.DIVIDE);
            expressionVisitor.endVisitMath(ExpressionVisitor.MathOperator.DIVIDE);
        } else if (expression instanceof Multiply) {
            expressionVisitor.beginVisitMath(ExpressionVisitor.MathOperator.MULTIPLY);
            expressionVisitor.beginVisitMathLeftOperand(ExpressionVisitor.MathOperator.MULTIPLY);
            buildVariableExecutors(((Multiply) expression).getLeftValue(), expressionVisitor);
            expressionVisitor.endVisitMathLeftOperand(ExpressionVisitor.MathOperator.MULTIPLY);
            expressionVisitor.beginVisitMathRightOperand(ExpressionVisitor.MathOperator.MULTIPLY);
            buildVariableExecutors(((Multiply) expression).getRightValue(), expressionVisitor);
            expressionVisitor.endVisitMathRightOperand(ExpressionVisitor.MathOperator.MULTIPLY);
            expressionVisitor.endVisitMath(ExpressionVisitor.MathOperator.MULTIPLY);
        } else if (expression instanceof Mod) {
            expressionVisitor.beginVisitMath(ExpressionVisitor.MathOperator.MOD);
            expressionVisitor.beginVisitMathLeftOperand(ExpressionVisitor.MathOperator.MOD);
            buildVariableExecutors(((Mod) expression).getLeftValue(), expressionVisitor);
            expressionVisitor.endVisitMathLeftOperand(ExpressionVisitor.MathOperator.MOD);
            expressionVisitor.beginVisitMathRightOperand(ExpressionVisitor.MathOperator.MOD);
            buildVariableExecutors(((Mod) expression).getRightValue(), expressionVisitor);
            expressionVisitor.endVisitMathRightOperand(ExpressionVisitor.MathOperator.MOD);
            expressionVisitor.endVisitMath(ExpressionVisitor.MathOperator.MOD);
        } else if (expression instanceof IsNull) {
            IsNull isNull = (IsNull) expression;
            if (isNull.getExpression() != null) {
                expressionVisitor.beginVisitIsNull(null);
                buildVariableExecutors(((IsNull) expression).getExpression(), expressionVisitor);
                expressionVisitor.endVisitIsNull(null);
            } else {
                String streamId = isNull.getStreamId();
                MetaStateEvent metaStateEvent = matchingMetaInfoHolder.getMetaStateEvent();
                if (streamId == null) {
                    throw new SiddhiAppCreationException("IsNull does not support streamId being null");
                } else {
                    AbstractDefinition definitionOutput = null;
                    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)) {
                                definitionOutput = definition;
                                break;
                            }
                        } else {
                            if (metaStreamEvent.getInputReferenceId().equals(streamId)) {
                                definitionOutput = definition;
                                break;
                            }
                        }
                    }
                    if (definitionOutput != null) {
                        expressionVisitor.beginVisitIsNull(definitionOutput.getId());
                        expressionVisitor.endVisitIsNull(definitionOutput.getId());
                    } else {
                        expressionVisitor.beginVisitIsNull(null);
                        expressionVisitor.endVisitIsNull(null);
                    }
                }
            }
        } else if (expression instanceof In) {
            expressionVisitor.beginVisitIn(((In) expression).getSourceId());
            buildVariableExecutors(((In) expression).getExpression(), expressionVisitor);
            expressionVisitor.endVisitIn(((In) expression).getSourceId());
        } else if (expression instanceof Constant) {
            if (expression instanceof DoubleConstant) {
                expressionVisitor.beginVisitConstant(((DoubleConstant) expression).getValue(), Attribute.Type.DOUBLE);
                expressionVisitor.endVisitConstant(((DoubleConstant) expression).getValue(), Attribute.Type.DOUBLE);
            } else if (expression instanceof StringConstant) {
                expressionVisitor.beginVisitConstant(((StringConstant) expression).getValue(), Attribute.Type.STRING);
                expressionVisitor.endVisitConstant(((StringConstant) expression).getValue(), Attribute.Type.STRING);
            } else if (expression instanceof IntConstant) {
                expressionVisitor.beginVisitConstant(((IntConstant) expression).getValue(), Attribute.Type.INT);
                expressionVisitor.endVisitConstant(((IntConstant) expression).getValue(), Attribute.Type.INT);
            } else if (expression instanceof BoolConstant) {
                expressionVisitor.beginVisitConstant(((BoolConstant) expression).getValue(), Attribute.Type.BOOL);
                expressionVisitor.endVisitConstant(((BoolConstant) expression).getValue(), Attribute.Type.BOOL);
            } else if (expression instanceof FloatConstant) {
                expressionVisitor.beginVisitConstant(((FloatConstant) expression).getValue(), Attribute.Type.FLOAT);
                expressionVisitor.endVisitConstant(((FloatConstant) expression).getValue(), Attribute.Type.FLOAT);
            } else if (expression instanceof LongConstant) {
                expressionVisitor.beginVisitConstant(((LongConstant) expression).getValue(), Attribute.Type.LONG);
                expressionVisitor.endVisitConstant(((LongConstant) expression).getValue(), Attribute.Type.LONG);
            } else {
                throw new OperationNotSupportedException("No constant exist with type " + expression.getClass().getName());
            }
        } else if (expression instanceof AttributeFunction) {
            expressionVisitor.beginVisitAttributeFunction(((AttributeFunction) expression).getNamespace(), ((AttributeFunction) expression).getName());
            Expression[] expressions = ((AttributeFunction) expression).getParameters();
            if (expressions != null) {
                for (int i = 0; i < expressions.length; i++) {
                    expressionVisitor.beginVisitParameterAttributeFunction(i);
                    buildVariableExecutors(expressions[i], expressionVisitor);
                    expressionVisitor.endVisitParameterAttributeFunction(i);
                }
            }
            expressionVisitor.endVisitAttributeFunction(((AttributeFunction) expression).getNamespace(), ((AttributeFunction) expression).getName());
        } else if (expression instanceof Variable) {
            Variable variable = ((Variable) expression);
            String attributeName = variable.getAttributeName();
            AbstractDefinition definition;
            Attribute.Type type = null;
            int streamEventChainIndex = matchingMetaInfoHolder.getCurrentState();
            if (variable.getStreamId() == null) {
                MetaStreamEvent[] metaStreamEvents = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents();
                if (streamEventChainIndex == UNKNOWN_STATE) {
                    String firstInput = null;
                    for (int i = 0; i < metaStreamEvents.length; i++) {
                        MetaStreamEvent metaStreamEvent = metaStreamEvents[i];
                        definition = metaStreamEvent.getLastInputDefinition();
                        if (type == null) {
                            try {
                                type = definition.getAttributeType(attributeName);
                                firstInput = "Input Stream: " + definition.getId() + " with " + "reference: " + metaStreamEvent.getInputReferenceId();
                                streamEventChainIndex = i;
                            } catch (AttributeNotExistException e) {
                            // do nothing
                            }
                        } else {
                            try {
                                definition.getAttributeType(attributeName);
                                throw new SiddhiAppValidationException(firstInput + " and Input Stream: " + definition.getId() + " with " + "reference: " + metaStreamEvent.getInputReferenceId() + " contains attribute " + "with same" + " name '" + attributeName + "'");
                            } catch (AttributeNotExistException e) {
                            // do nothing as its expected
                            }
                        }
                    }
                    if (streamEventChainIndex != UNKNOWN_STATE) {
                        if (matchingMetaInfoHolder.getMatchingStreamEventIndex() == streamEventChainIndex) {
                            buildStreamVariableExecutor(variable, streamEventChainIndex, expressionVisitor, type);
                        } else {
                            buildStoreVariableExecutor(variable, expressionVisitor, type, matchingMetaInfoHolder.getStoreDefinition());
                        }
                    } else {
                        // Having state : i.e attribute is in the select clause
                        definition = matchingMetaInfoHolder.getMetaStateEvent().getOutputStreamDefinition();
                        try {
                            type = definition.getAttributeType(attributeName);
                            buildStoreVariableExecutor(variable, expressionVisitor, type, matchingMetaInfoHolder.getStoreDefinition());
                        } catch (AttributeNotExistException e) {
                        // do nothing as its not expected
                        }
                    }
                } else {
                    MetaStreamEvent metaStreamEvent = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvent(matchingMetaInfoHolder.getCurrentState());
                    definition = metaStreamEvent.getLastInputDefinition();
                    try {
                        type = definition.getAttributeType(attributeName);
                    } catch (AttributeNotExistException e) {
                        // Having state : i.e attribute is in the select clause
                        definition = matchingMetaInfoHolder.getMetaStateEvent().getOutputStreamDefinition();
                        try {
                            type = definition.getAttributeType(attributeName);
                            buildStoreVariableExecutor(variable, expressionVisitor, type, matchingMetaInfoHolder.getStoreDefinition());
                        } catch (AttributeNotExistException e1) {
                            throw new SiddhiAppValidationException(e1.getMessageWithOutContext() + " Input Stream: " + definition.getId() + " with " + "reference: " + metaStreamEvent.getInputReferenceId(), e1.getQueryContextStartIndex(), e1.getQueryContextEndIndex(), siddhiQueryContext.getSiddhiAppContext().getName(), siddhiQueryContext.getSiddhiAppContext().getSiddhiAppString());
                        }
                    }
                    if (matchingMetaInfoHolder.getCurrentState() == matchingMetaInfoHolder.getMatchingStreamEventIndex()) {
                        buildStreamVariableExecutor(variable, streamEventChainIndex, expressionVisitor, type);
                    } else {
                        buildStoreVariableExecutor(variable, expressionVisitor, type, matchingMetaInfoHolder.getStoreDefinition());
                    }
                }
            } else {
                MetaStreamEvent[] metaStreamEvents = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents();
                for (int i = 0, metaStreamEventsLength = metaStreamEvents.length; i < metaStreamEventsLength; i++) {
                    MetaStreamEvent metaStreamEvent = metaStreamEvents[i];
                    definition = metaStreamEvent.getLastInputDefinition();
                    if (metaStreamEvent.getInputReferenceId() == null) {
                        if (definition.getId().equals(variable.getStreamId())) {
                            type = definition.getAttributeType(attributeName);
                            streamEventChainIndex = i;
                            break;
                        }
                    } else {
                        if (metaStreamEvent.getInputReferenceId().equals(variable.getStreamId())) {
                            type = definition.getAttributeType(attributeName);
                            streamEventChainIndex = i;
                            break;
                        }
                    }
                }
                if (matchingMetaInfoHolder.getMatchingStreamEventIndex() == streamEventChainIndex) {
                    buildStreamVariableExecutor(variable, streamEventChainIndex, expressionVisitor, type);
                } else {
                    buildStoreVariableExecutor(variable, expressionVisitor, type, matchingMetaInfoHolder.getStoreDefinition());
                }
            }
        }
    } catch (Throwable t) {
        ExceptionUtil.populateQueryContext(t, expression, siddhiQueryContext.getSiddhiAppContext(), siddhiQueryContext);
        throw t;
    }
}
Also used : Add(io.siddhi.query.api.expression.math.Add) DoubleConstant(io.siddhi.query.api.expression.constant.DoubleConstant) Or(io.siddhi.query.api.expression.condition.Or) Variable(io.siddhi.query.api.expression.Variable) In(io.siddhi.query.api.expression.condition.In) StringConstant(io.siddhi.query.api.expression.constant.StringConstant) DoubleConstant(io.siddhi.query.api.expression.constant.DoubleConstant) FloatConstant(io.siddhi.query.api.expression.constant.FloatConstant) LongConstant(io.siddhi.query.api.expression.constant.LongConstant) IntConstant(io.siddhi.query.api.expression.constant.IntConstant) Constant(io.siddhi.query.api.expression.constant.Constant) BoolConstant(io.siddhi.query.api.expression.constant.BoolConstant) FloatConstant(io.siddhi.query.api.expression.constant.FloatConstant) Divide(io.siddhi.query.api.expression.math.Divide) AttributeNotExistException(io.siddhi.query.api.exception.AttributeNotExistException) Multiply(io.siddhi.query.api.expression.math.Multiply) IntConstant(io.siddhi.query.api.expression.constant.IntConstant) Compare(io.siddhi.query.api.expression.condition.Compare) LongConstant(io.siddhi.query.api.expression.constant.LongConstant) OperationNotSupportedException(io.siddhi.core.exception.OperationNotSupportedException) BoolConstant(io.siddhi.query.api.expression.constant.BoolConstant) Mod(io.siddhi.query.api.expression.math.Mod) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) AttributeFunction(io.siddhi.query.api.expression.AttributeFunction) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) Not(io.siddhi.query.api.expression.condition.Not) Expression(io.siddhi.query.api.expression.Expression) And(io.siddhi.query.api.expression.condition.And) Subtract(io.siddhi.query.api.expression.math.Subtract) IsNull(io.siddhi.query.api.expression.condition.IsNull) StringConstant(io.siddhi.query.api.expression.constant.StringConstant) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

AbstractDefinition (io.siddhi.query.api.definition.AbstractDefinition)24 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)12 Attribute (io.siddhi.query.api.definition.Attribute)12 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)9 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)9 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)8 ArrayList (java.util.ArrayList)8 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)7 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)7 Variable (io.siddhi.query.api.expression.Variable)7 Expression (io.siddhi.query.api.expression.Expression)6 Table (io.siddhi.core.table.Table)5 OutputAttribute (io.siddhi.query.api.execution.query.selection.OutputAttribute)5 AttributeFunction (io.siddhi.query.api.expression.AttributeFunction)5 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)4 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)4 SiddhiAppValidationException (io.siddhi.query.api.exception.SiddhiAppValidationException)4 AndConditionExpressionExecutor (io.siddhi.core.executor.condition.AndConditionExpressionExecutor)3 BoolConditionExpressionExecutor (io.siddhi.core.executor.condition.BoolConditionExpressionExecutor)3 ConditionExpressionExecutor (io.siddhi.core.executor.condition.ConditionExpressionExecutor)3