Search in sources :

Example 1 with JoinProcessor

use of io.siddhi.core.query.input.stream.join.JoinProcessor in project siddhi by wso2.

the class QueryParser method parse.

/**
 * Parse a query and return corresponding QueryRuntime.
 *
 * @param query                    query to be parsed.
 * @param siddhiAppContext         associated Siddhi app context.
 * @param streamDefinitionMap      keyvalue containing user given stream definitions.
 * @param tableDefinitionMap       keyvalue containing table definitions.
 * @param windowDefinitionMap      keyvalue containing window definition map.
 * @param aggregationDefinitionMap keyvalue containing aggregation definition map.
 * @param tableMap                 keyvalue containing event tables.
 * @param aggregationMap           keyvalue containing aggrigation runtimes.
 * @param windowMap                keyvalue containing event window map.
 * @param lockSynchronizer         Lock synchronizer for sync the lock across queries.
 * @param queryIndex               query index to identify unknown query by number
 * @param partitioned              is the query partitioned
 * @param partitionId              The ID of the partition
 * @return queryRuntime
 */
public static QueryRuntimeImpl parse(Query query, SiddhiAppContext siddhiAppContext, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, Map<String, AggregationRuntime> aggregationMap, Map<String, Window> windowMap, LockSynchronizer lockSynchronizer, String queryIndex, boolean partitioned, String partitionId) {
    List<VariableExpressionExecutor> executors = new ArrayList<>();
    QueryRuntimeImpl queryRuntime;
    Element nameElement = null;
    LatencyTracker latencyTracker = null;
    LockWrapper lockWrapper = null;
    try {
        nameElement = AnnotationHelper.getAnnotationElement("info", "name", query.getAnnotations());
        String queryName;
        if (nameElement != null) {
            queryName = nameElement.getValue();
        } else {
            queryName = "query_" + queryIndex;
        }
        SiddhiQueryContext siddhiQueryContext = new SiddhiQueryContext(siddhiAppContext, queryName, partitionId);
        siddhiQueryContext.setPartitioned(partitioned);
        latencyTracker = QueryParserHelper.createLatencyTracker(siddhiAppContext, siddhiQueryContext.getName(), SiddhiConstants.METRIC_INFIX_QUERIES, null);
        siddhiQueryContext.setLatencyTracker(latencyTracker);
        OutputStream.OutputEventType outputEventType = query.getOutputStream().getOutputEventType();
        if (query.getOutputRate() != null && query.getOutputRate() instanceof SnapshotOutputRate) {
            if (outputEventType != OutputStream.OutputEventType.ALL_EVENTS) {
                throw new SiddhiAppCreationException("As query '" + siddhiQueryContext.getName() + "' is performing snapshot rate limiting, it can only insert '" + OutputStream.OutputEventType.ALL_EVENTS + "' but it is inserting '" + outputEventType + "'!", query.getOutputStream().getQueryContextStartIndex(), query.getOutputStream().getQueryContextEndIndex());
            }
        }
        siddhiQueryContext.setOutputEventType(outputEventType);
        boolean outputExpectsExpiredEvents = false;
        if (outputEventType != OutputStream.OutputEventType.CURRENT_EVENTS) {
            outputExpectsExpiredEvents = true;
        }
        StreamRuntime streamRuntime = InputStreamParser.parse(query.getInputStream(), query, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, windowMap, aggregationMap, executors, outputExpectsExpiredEvents, siddhiQueryContext);
        QuerySelector selector;
        if (streamRuntime.getQuerySelector() != null) {
            selector = streamRuntime.getQuerySelector();
        } else {
            selector = SelectorParser.parse(query.getSelector(), query.getOutputStream(), streamRuntime.getMetaComplexEvent(), tableMap, executors, SiddhiConstants.UNKNOWN_STATE, streamRuntime.getProcessingMode(), outputExpectsExpiredEvents, siddhiQueryContext);
        }
        boolean isWindow = query.getInputStream() instanceof JoinInputStream;
        if (!isWindow && query.getInputStream() instanceof SingleInputStream) {
            for (StreamHandler streamHandler : ((SingleInputStream) query.getInputStream()).getStreamHandlers()) {
                if (streamHandler instanceof io.siddhi.query.api.execution.query.input.handler.Window) {
                    isWindow = true;
                    break;
                }
            }
        }
        Element synchronizedElement = AnnotationHelper.getAnnotationElement("synchronized", null, query.getAnnotations());
        if (synchronizedElement != null) {
            if (!("false".equalsIgnoreCase(synchronizedElement.getValue()))) {
                // Query LockWrapper does not need a unique
                lockWrapper = new LockWrapper("");
                // id since it will
                // not be passed to the LockSynchronizer.
                // LockWrapper does not have a default lock
                lockWrapper.setLock(new ReentrantLock());
            }
        } else {
            if (isWindow || !(streamRuntime instanceof SingleStreamRuntime)) {
                if (streamRuntime instanceof JoinStreamRuntime) {
                    // If at least one Window is involved in the join, use the LockWrapper of that window
                    // for the query as well.
                    // If join is between two EventWindows, sync the locks of the LockWrapper of those windows
                    // and use either of them for query.
                    MetaStateEvent metaStateEvent = (MetaStateEvent) streamRuntime.getMetaComplexEvent();
                    MetaStreamEvent[] metaStreamEvents = metaStateEvent.getMetaStreamEvents();
                    if (metaStreamEvents[0].getEventType() == EventType.WINDOW && metaStreamEvents[1].getEventType() == EventType.WINDOW) {
                        LockWrapper leftLockWrapper = windowMap.get(metaStreamEvents[0].getLastInputDefinition().getId()).getLock();
                        LockWrapper rightLockWrapper = windowMap.get(metaStreamEvents[1].getLastInputDefinition().getId()).getLock();
                        if (!leftLockWrapper.equals(rightLockWrapper)) {
                            // Sync the lock across both wrappers
                            lockSynchronizer.sync(leftLockWrapper, rightLockWrapper);
                        }
                        // Can use either leftLockWrapper or rightLockWrapper since both of them will hold the
                        // same lock internally
                        // If either of their lock is updated later, the other lock also will be update by the
                        // LockSynchronizer.
                        lockWrapper = leftLockWrapper;
                    } else if (metaStreamEvents[0].getEventType() == EventType.WINDOW) {
                        // Share the same wrapper as the query lock wrapper
                        lockWrapper = windowMap.get(metaStreamEvents[0].getLastInputDefinition().getId()).getLock();
                    } else if (metaStreamEvents[1].getEventType() == EventType.WINDOW) {
                        // Share the same wrapper as the query lock wrapper
                        lockWrapper = windowMap.get(metaStreamEvents[1].getLastInputDefinition().getId()).getLock();
                    } else {
                        // Join does not contain any Window
                        // Query LockWrapper does not need a unique
                        lockWrapper = new LockWrapper("");
                        // id since
                        // it will not be passed to the LockSynchronizer.
                        // LockWrapper does not have a default lock
                        lockWrapper.setLock(new ReentrantLock());
                    }
                } else {
                    lockWrapper = new LockWrapper("");
                    lockWrapper.setLock(new ReentrantLock());
                }
            }
        }
        OutputRateLimiter outputRateLimiter = OutputParser.constructOutputRateLimiter(query.getOutputStream().getId(), query.getOutputRate(), query.getSelector().getGroupByList().size() != 0, isWindow, siddhiQueryContext);
        if (outputRateLimiter instanceof WrappedSnapshotOutputRateLimiter) {
            selector.setBatchingEnabled(false);
        }
        boolean groupBy = !query.getSelector().getGroupByList().isEmpty();
        OutputCallback outputCallback = OutputParser.constructOutputCallback(query.getOutputStream(), streamRuntime.getMetaComplexEvent().getOutputStreamDefinition(), tableMap, windowMap, !(streamRuntime instanceof SingleStreamRuntime) || groupBy, siddhiQueryContext);
        QueryParserHelper.reduceMetaComplexEvent(streamRuntime.getMetaComplexEvent());
        QueryParserHelper.updateVariablePosition(streamRuntime.getMetaComplexEvent(), executors);
        QueryParserHelper.initStreamRuntime(streamRuntime, streamRuntime.getMetaComplexEvent(), lockWrapper, siddhiQueryContext.getName());
        // Update cache compile selection variable expression executors
        if (streamRuntime instanceof JoinStreamRuntime) {
            streamRuntime.getSingleStreamRuntimes().forEach((singleStreamRuntime -> {
                Processor processorChain = singleStreamRuntime.getProcessorChain();
                if (processorChain instanceof JoinProcessor) {
                    CompiledSelection compiledSelection = ((JoinProcessor) processorChain).getCompiledSelection();
                    if (compiledSelection instanceof AbstractQueryableRecordTable.CompiledSelectionWithCache) {
                        List<VariableExpressionExecutor> variableExpressionExecutors = ((AbstractQueryableRecordTable.CompiledSelectionWithCache) compiledSelection).getVariableExpressionExecutorsForQuerySelector();
                        QueryParserHelper.updateVariablePosition(streamRuntime.getMetaComplexEvent(), variableExpressionExecutors);
                    }
                }
            }));
        }
        selector.setEventPopulator(StateEventPopulatorFactory.constructEventPopulator(streamRuntime.getMetaComplexEvent()));
        queryRuntime = new QueryRuntimeImpl(query, streamRuntime, selector, outputRateLimiter, outputCallback, streamRuntime.getMetaComplexEvent(), siddhiQueryContext);
        if (outputRateLimiter instanceof WrappedSnapshotOutputRateLimiter) {
            selector.setBatchingEnabled(false);
            ((WrappedSnapshotOutputRateLimiter) outputRateLimiter).init(streamRuntime.getMetaComplexEvent().getOutputStreamDefinition().getAttributeList().size(), selector.getAttributeProcessorList(), streamRuntime.getMetaComplexEvent());
        }
        outputRateLimiter.init(lockWrapper, groupBy, siddhiQueryContext);
    } catch (DuplicateDefinitionException e) {
        if (nameElement != null) {
            throw new DuplicateDefinitionException(e.getMessageWithOutContext() + ", when creating query " + nameElement.getValue(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
        } else {
            throw new DuplicateDefinitionException(e.getMessage(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
        }
    } catch (Throwable t) {
        ExceptionUtil.populateQueryContext(t, query, siddhiAppContext);
        throw t;
    }
    return queryRuntime;
}
Also used : DuplicateDefinitionException(io.siddhi.query.api.exception.DuplicateDefinitionException) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) AnnotationHelper(io.siddhi.query.api.util.AnnotationHelper) QueryRuntimeImpl(io.siddhi.core.query.QueryRuntimeImpl) OutputCallback(io.siddhi.core.query.output.callback.OutputCallback) SiddhiAppContext(io.siddhi.core.config.SiddhiAppContext) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) SingleInputStream(io.siddhi.query.api.execution.query.input.stream.SingleInputStream) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) SiddhiConstants(io.siddhi.core.util.SiddhiConstants) CompiledSelection(io.siddhi.core.util.collection.operator.CompiledSelection) QuerySelector(io.siddhi.core.query.selector.QuerySelector) ArrayList(java.util.ArrayList) StreamRuntime(io.siddhi.core.query.input.stream.StreamRuntime) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) Query(io.siddhi.query.api.execution.query.Query) Table(io.siddhi.core.table.Table) LockSynchronizer(io.siddhi.core.util.lock.LockSynchronizer) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) AggregationRuntime(io.siddhi.core.aggregation.AggregationRuntime) Map(java.util.Map) SiddhiQueryContext(io.siddhi.core.config.SiddhiQueryContext) SingleStreamRuntime(io.siddhi.core.query.input.stream.single.SingleStreamRuntime) WrappedSnapshotOutputRateLimiter(io.siddhi.core.query.output.ratelimit.snapshot.WrappedSnapshotOutputRateLimiter) LockWrapper(io.siddhi.core.util.lock.LockWrapper) ReentrantLock(java.util.concurrent.locks.ReentrantLock) QueryRuntime(io.siddhi.core.query.QueryRuntime) LatencyTracker(io.siddhi.core.util.statistics.LatencyTracker) EventType(io.siddhi.core.event.stream.MetaStreamEvent.EventType) OutputRateLimiter(io.siddhi.core.query.output.ratelimit.OutputRateLimiter) ExceptionUtil(io.siddhi.core.util.ExceptionUtil) StateEventPopulatorFactory(io.siddhi.core.event.state.populater.StateEventPopulatorFactory) Element(io.siddhi.query.api.annotation.Element) AbstractQueryableRecordTable(io.siddhi.core.table.record.AbstractQueryableRecordTable) List(java.util.List) JoinStreamRuntime(io.siddhi.core.query.input.stream.join.JoinStreamRuntime) OutputStream(io.siddhi.query.api.execution.query.output.stream.OutputStream) SnapshotOutputRate(io.siddhi.query.api.execution.query.output.ratelimit.SnapshotOutputRate) Processor(io.siddhi.core.query.processor.Processor) Window(io.siddhi.core.window.Window) QueryParserHelper(io.siddhi.core.util.parser.helper.QueryParserHelper) JoinProcessor(io.siddhi.core.query.input.stream.join.JoinProcessor) JoinInputStream(io.siddhi.query.api.execution.query.input.stream.JoinInputStream) StreamHandler(io.siddhi.query.api.execution.query.input.handler.StreamHandler) Processor(io.siddhi.core.query.processor.Processor) JoinProcessor(io.siddhi.core.query.input.stream.join.JoinProcessor) Element(io.siddhi.query.api.annotation.Element) OutputStream(io.siddhi.query.api.execution.query.output.stream.OutputStream) ArrayList(java.util.ArrayList) OutputCallback(io.siddhi.core.query.output.callback.OutputCallback) SiddhiQueryContext(io.siddhi.core.config.SiddhiQueryContext) AbstractQueryableRecordTable(io.siddhi.core.table.record.AbstractQueryableRecordTable) JoinInputStream(io.siddhi.query.api.execution.query.input.stream.JoinInputStream) SingleInputStream(io.siddhi.query.api.execution.query.input.stream.SingleInputStream) WrappedSnapshotOutputRateLimiter(io.siddhi.core.query.output.ratelimit.snapshot.WrappedSnapshotOutputRateLimiter) OutputRateLimiter(io.siddhi.core.query.output.ratelimit.OutputRateLimiter) StreamHandler(io.siddhi.query.api.execution.query.input.handler.StreamHandler) ArrayList(java.util.ArrayList) List(java.util.List) StreamRuntime(io.siddhi.core.query.input.stream.StreamRuntime) SingleStreamRuntime(io.siddhi.core.query.input.stream.single.SingleStreamRuntime) JoinStreamRuntime(io.siddhi.core.query.input.stream.join.JoinStreamRuntime) JoinProcessor(io.siddhi.core.query.input.stream.join.JoinProcessor) Window(io.siddhi.core.window.Window) ReentrantLock(java.util.concurrent.locks.ReentrantLock) DuplicateDefinitionException(io.siddhi.query.api.exception.DuplicateDefinitionException) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) SingleStreamRuntime(io.siddhi.core.query.input.stream.single.SingleStreamRuntime) JoinStreamRuntime(io.siddhi.core.query.input.stream.join.JoinStreamRuntime) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) QuerySelector(io.siddhi.core.query.selector.QuerySelector) LockWrapper(io.siddhi.core.util.lock.LockWrapper) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) SnapshotOutputRate(io.siddhi.query.api.execution.query.output.ratelimit.SnapshotOutputRate) QueryRuntimeImpl(io.siddhi.core.query.QueryRuntimeImpl) CompiledSelection(io.siddhi.core.util.collection.operator.CompiledSelection) LatencyTracker(io.siddhi.core.util.statistics.LatencyTracker) WrappedSnapshotOutputRateLimiter(io.siddhi.core.query.output.ratelimit.snapshot.WrappedSnapshotOutputRateLimiter) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 2 with JoinProcessor

use of io.siddhi.core.query.input.stream.join.JoinProcessor in project siddhi by wso2.

the class JoinInputStreamParser method insertJoinProcessorsAndGetFindable.

private static FindableProcessor insertJoinProcessorsAndGetFindable(JoinProcessor preJoinProcessor, JoinProcessor postJoinProcessor, SingleStreamRuntime streamRuntime, boolean outputExpectsExpiredEvents, InputStream inputStream, SiddhiQueryContext siddhiQueryContext) {
    Processor lastProcessor = streamRuntime.getProcessorChain();
    Processor prevLastProcessor = null;
    boolean containFindable = false;
    if (lastProcessor != null) {
        containFindable = lastProcessor instanceof FindableProcessor;
        while (lastProcessor.getNextProcessor() != null) {
            prevLastProcessor = lastProcessor;
            lastProcessor = lastProcessor.getNextProcessor();
            if (!containFindable) {
                containFindable = lastProcessor instanceof FindableProcessor;
            }
        }
    }
    if (!containFindable) {
        try {
            WindowProcessor windowProcessor = new EmptyWindowProcessor();
            ExpressionExecutor[] expressionExecutors = new ExpressionExecutor[1];
            expressionExecutors[0] = new ConstantExpressionExecutor(0, Attribute.Type.INT);
            ConfigReader configReader = siddhiQueryContext.getSiddhiContext().getConfigManager().generateConfigReader("", "lengthBatch");
            windowProcessor.initProcessor(((MetaStreamEvent) streamRuntime.getMetaComplexEvent()), expressionExecutors, configReader, outputExpectsExpiredEvents, true, false, inputStream, siddhiQueryContext);
            if (lastProcessor != null) {
                prevLastProcessor = lastProcessor;
                prevLastProcessor.setNextProcessor(windowProcessor);
                lastProcessor = windowProcessor;
            } else {
                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(io.siddhi.core.exception.OperationNotSupportedException) FindableProcessor(io.siddhi.core.query.processor.stream.window.FindableProcessor) QueryableProcessor(io.siddhi.core.query.processor.stream.window.QueryableProcessor) EmptyWindowProcessor(io.siddhi.core.query.processor.stream.window.EmptyWindowProcessor) FindableProcessor(io.siddhi.core.query.processor.stream.window.FindableProcessor) Processor(io.siddhi.core.query.processor.Processor) JoinProcessor(io.siddhi.core.query.input.stream.join.JoinProcessor) TableWindowProcessor(io.siddhi.core.query.processor.stream.window.TableWindowProcessor) WindowProcessor(io.siddhi.core.query.processor.stream.window.WindowProcessor) WindowWindowProcessor(io.siddhi.core.query.processor.stream.window.WindowWindowProcessor) AggregateWindowProcessor(io.siddhi.core.query.processor.stream.window.AggregateWindowProcessor) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(io.siddhi.core.util.config.ConfigReader) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) EmptyWindowProcessor(io.siddhi.core.query.processor.stream.window.EmptyWindowProcessor) EmptyWindowProcessor(io.siddhi.core.query.processor.stream.window.EmptyWindowProcessor) TableWindowProcessor(io.siddhi.core.query.processor.stream.window.TableWindowProcessor) WindowProcessor(io.siddhi.core.query.processor.stream.window.WindowProcessor) WindowWindowProcessor(io.siddhi.core.query.processor.stream.window.WindowWindowProcessor) AggregateWindowProcessor(io.siddhi.core.query.processor.stream.window.AggregateWindowProcessor) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 3 with JoinProcessor

use of io.siddhi.core.query.input.stream.join.JoinProcessor in project siddhi by wso2.

the class JoinInputStreamParser method parseInputStream.

public static StreamRuntime parseInputStream(JoinInputStream joinInputStream, Query query, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, Map<String, Window> windowMap, Map<String, AggregationRuntime> aggregationMap, List<VariableExpressionExecutor> executors, boolean outputExpectsExpiredEvents, SiddhiQueryContext siddhiQueryContext) {
    try {
        ProcessStreamReceiver leftProcessStreamReceiver;
        ProcessStreamReceiver rightProcessStreamReceiver;
        MetaStreamEvent leftMetaStreamEvent = new MetaStreamEvent();
        MetaStreamEvent rightMetaStreamEvent = new MetaStreamEvent();
        String leftInputStreamId = ((SingleInputStream) joinInputStream.getLeftInputStream()).getStreamId();
        String rightInputStreamId = ((SingleInputStream) joinInputStream.getRightInputStream()).getStreamId();
        boolean leftOuterJoinProcessor = false;
        boolean rightOuterJoinProcessor = false;
        if (joinInputStream.getAllStreamIds().size() == 2) {
            setEventType(streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, leftMetaStreamEvent, leftInputStreamId);
            setEventType(streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, rightMetaStreamEvent, rightInputStreamId);
            leftProcessStreamReceiver = new ProcessStreamReceiver(leftInputStreamId, siddhiQueryContext);
            rightProcessStreamReceiver = new ProcessStreamReceiver(rightInputStreamId, siddhiQueryContext);
            if ((leftMetaStreamEvent.getEventType() == TABLE || leftMetaStreamEvent.getEventType() == AGGREGATE) && (rightMetaStreamEvent.getEventType() == TABLE || rightMetaStreamEvent.getEventType() == AGGREGATE)) {
                throw new SiddhiAppCreationException("Both inputs of join " + leftInputStreamId + " and " + rightInputStreamId + " are from static sources");
            }
            if (leftMetaStreamEvent.getEventType() != AGGREGATE && rightMetaStreamEvent.getEventType() != AGGREGATE) {
                if (joinInputStream.getPer() != null) {
                    throw new SiddhiAppCreationException("When joining " + leftInputStreamId + " and " + rightInputStreamId + " 'per' cannot be used as neither of them is an aggregation ");
                } else if (joinInputStream.getWithin() != null) {
                    throw new SiddhiAppCreationException("When joining " + leftInputStreamId + " and " + rightInputStreamId + " 'within' cannot be used as neither of them is an aggregation ");
                }
            }
        } else {
            if (windowDefinitionMap.containsKey(joinInputStream.getAllStreamIds().get(0))) {
                leftMetaStreamEvent.setEventType(WINDOW);
                rightMetaStreamEvent.setEventType(WINDOW);
                rightProcessStreamReceiver = new MultiProcessStreamReceiver(joinInputStream.getAllStreamIds().get(0), 1, new Object(), siddhiQueryContext);
                leftProcessStreamReceiver = rightProcessStreamReceiver;
            } else if (streamDefinitionMap.containsKey(joinInputStream.getAllStreamIds().get(0))) {
                rightProcessStreamReceiver = new MultiProcessStreamReceiver(joinInputStream.getAllStreamIds().get(0), 2, new Object(), siddhiQueryContext);
                leftProcessStreamReceiver = rightProcessStreamReceiver;
            } else {
                throw new SiddhiAppCreationException("Input of join is from static source " + leftInputStreamId + " and " + rightInputStreamId);
            }
        }
        SingleStreamRuntime leftStreamRuntime = SingleInputStreamParser.parseInputStream((SingleInputStream) joinInputStream.getLeftInputStream(), executors, streamDefinitionMap, leftMetaStreamEvent.getEventType() != TABLE ? null : tableDefinitionMap, leftMetaStreamEvent.getEventType() != WINDOW ? null : windowDefinitionMap, leftMetaStreamEvent.getEventType() != AGGREGATE ? null : aggregationDefinitionMap, tableMap, leftMetaStreamEvent, leftProcessStreamReceiver, true, outputExpectsExpiredEvents, true, false, siddhiQueryContext);
        for (VariableExpressionExecutor variableExpressionExecutor : executors) {
            variableExpressionExecutor.getPosition()[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = 0;
        }
        int size = executors.size();
        SingleStreamRuntime rightStreamRuntime = SingleInputStreamParser.parseInputStream((SingleInputStream) joinInputStream.getRightInputStream(), executors, streamDefinitionMap, rightMetaStreamEvent.getEventType() != TABLE ? null : tableDefinitionMap, rightMetaStreamEvent.getEventType() != WINDOW ? null : windowDefinitionMap, rightMetaStreamEvent.getEventType() != AGGREGATE ? null : aggregationDefinitionMap, tableMap, rightMetaStreamEvent, rightProcessStreamReceiver, true, outputExpectsExpiredEvents, true, false, siddhiQueryContext);
        for (int i = size; i < executors.size(); i++) {
            VariableExpressionExecutor variableExpressionExecutor = executors.get(i);
            variableExpressionExecutor.getPosition()[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = 1;
        }
        setStreamRuntimeProcessorChain(leftMetaStreamEvent, leftStreamRuntime, leftInputStreamId, tableMap, windowMap, aggregationMap, executors, outputExpectsExpiredEvents, joinInputStream.getWithin(), joinInputStream.getPer(), query.getSelector().getGroupByList(), siddhiQueryContext, joinInputStream.getLeftInputStream());
        setStreamRuntimeProcessorChain(rightMetaStreamEvent, rightStreamRuntime, rightInputStreamId, tableMap, windowMap, aggregationMap, executors, outputExpectsExpiredEvents, joinInputStream.getWithin(), joinInputStream.getPer(), query.getSelector().getGroupByList(), siddhiQueryContext, joinInputStream.getRightInputStream());
        MetaStateEvent metaStateEvent = new MetaStateEvent(2);
        metaStateEvent.addEvent(leftMetaStreamEvent);
        metaStateEvent.addEvent(rightMetaStreamEvent);
        switch(joinInputStream.getType()) {
            case FULL_OUTER_JOIN:
                leftOuterJoinProcessor = true;
                rightOuterJoinProcessor = true;
                break;
            case RIGHT_OUTER_JOIN:
                rightOuterJoinProcessor = true;
                break;
            case LEFT_OUTER_JOIN:
                leftOuterJoinProcessor = true;
                break;
        }
        JoinProcessor leftPreJoinProcessor = new JoinProcessor(true, true, leftOuterJoinProcessor, 0, siddhiQueryContext.getSiddhiAppContext().getName(), siddhiQueryContext.getName());
        JoinProcessor leftPostJoinProcessor = new JoinProcessor(true, false, leftOuterJoinProcessor, 0, siddhiQueryContext.getSiddhiAppContext().getName(), siddhiQueryContext.getName());
        FindableProcessor leftFindableProcessor = insertJoinProcessorsAndGetFindable(leftPreJoinProcessor, leftPostJoinProcessor, leftStreamRuntime, outputExpectsExpiredEvents, joinInputStream.getLeftInputStream(), siddhiQueryContext);
        JoinProcessor rightPreJoinProcessor = new JoinProcessor(false, true, rightOuterJoinProcessor, 1, siddhiQueryContext.getSiddhiAppContext().getName(), siddhiQueryContext.getName());
        JoinProcessor rightPostJoinProcessor = new JoinProcessor(false, false, rightOuterJoinProcessor, 1, siddhiQueryContext.getSiddhiAppContext().getName(), siddhiQueryContext.getName());
        FindableProcessor rightFindableProcessor = insertJoinProcessorsAndGetFindable(rightPreJoinProcessor, rightPostJoinProcessor, rightStreamRuntime, outputExpectsExpiredEvents, joinInputStream.getRightInputStream(), siddhiQueryContext);
        leftPreJoinProcessor.setFindableProcessor(rightFindableProcessor);
        leftPostJoinProcessor.setFindableProcessor(rightFindableProcessor);
        rightPreJoinProcessor.setFindableProcessor(leftFindableProcessor);
        rightPostJoinProcessor.setFindableProcessor(leftFindableProcessor);
        Expression compareCondition = joinInputStream.getOnCompare();
        if (compareCondition == null) {
            compareCondition = Expression.value(true);
        }
        QuerySelector querySelector = null;
        if (!(rightFindableProcessor instanceof TableWindowProcessor || rightFindableProcessor instanceof AggregateWindowProcessor) && (joinInputStream.getTrigger() != JoinInputStream.EventTrigger.LEFT)) {
            MatchingMetaInfoHolder leftMatchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(metaStateEvent, 1, leftMetaStreamEvent.getLastInputDefinition(), SiddhiConstants.UNKNOWN_STATE);
            CompiledCondition rightCompiledCondition = leftFindableProcessor.compileCondition(compareCondition, leftMatchingMetaInfoHolder, executors, tableMap, siddhiQueryContext);
            List<Attribute> expectedOutputAttributes = new ArrayList<>();
            CompiledSelection rightCompiledSelection = null;
            if (leftFindableProcessor instanceof TableWindowProcessor && ((TableWindowProcessor) leftFindableProcessor).isOptimisableLookup()) {
                querySelector = SelectorParser.parse(query.getSelector(), query.getOutputStream(), metaStateEvent, tableMap, executors, SiddhiConstants.UNKNOWN_STATE, ProcessingMode.BATCH, outputExpectsExpiredEvents, siddhiQueryContext);
                expectedOutputAttributes = metaStateEvent.getOutputStreamDefinition().getAttributeList();
                try {
                    rightCompiledSelection = ((QueryableProcessor) leftFindableProcessor).compileSelection(query.getSelector(), expectedOutputAttributes, leftMatchingMetaInfoHolder, executors, tableMap, siddhiQueryContext);
                } catch (SiddhiAppCreationException | SiddhiAppValidationException | QueryableRecordTableException e) {
                    if (log.isDebugEnabled()) {
                        log.debug("Performing select clause in databases failed for query: '" + siddhiQueryContext.getName() + "' within Siddhi app '" + siddhiQueryContext.getSiddhiAppContext().getName() + "' hence reverting back to " + "querying only with where clause. Reason for failure: " + e.getMessage(), e);
                    }
                // Nothing to override
                }
            }
            populateJoinProcessors(rightMetaStreamEvent, rightInputStreamId, rightPreJoinProcessor, rightPostJoinProcessor, rightCompiledCondition, rightCompiledSelection, expectedOutputAttributes);
        }
        if (!(leftFindableProcessor instanceof TableWindowProcessor || leftFindableProcessor instanceof AggregateWindowProcessor) && (joinInputStream.getTrigger() != JoinInputStream.EventTrigger.RIGHT)) {
            MatchingMetaInfoHolder rightMatchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(metaStateEvent, 0, rightMetaStreamEvent.getLastInputDefinition(), SiddhiConstants.UNKNOWN_STATE);
            CompiledCondition leftCompiledCondition = rightFindableProcessor.compileCondition(compareCondition, rightMatchingMetaInfoHolder, executors, tableMap, siddhiQueryContext);
            List<Attribute> expectedOutputAttributes = new ArrayList<>();
            CompiledSelection leftCompiledSelection = null;
            if (rightFindableProcessor instanceof TableWindowProcessor && ((TableWindowProcessor) rightFindableProcessor).isOptimisableLookup()) {
                querySelector = SelectorParser.parse(query.getSelector(), query.getOutputStream(), metaStateEvent, tableMap, executors, SiddhiConstants.UNKNOWN_STATE, ProcessingMode.BATCH, outputExpectsExpiredEvents, siddhiQueryContext);
                expectedOutputAttributes = metaStateEvent.getOutputStreamDefinition().getAttributeList();
                try {
                    leftCompiledSelection = ((QueryableProcessor) rightFindableProcessor).compileSelection(query.getSelector(), expectedOutputAttributes, rightMatchingMetaInfoHolder, executors, tableMap, siddhiQueryContext);
                } catch (SiddhiAppCreationException | SiddhiAppValidationException | QueryableRecordTableException e) {
                    if (log.isDebugEnabled()) {
                        log.debug("Performing select clause in databases failed for query: '" + siddhiQueryContext.getName() + "' within Siddhi app '" + siddhiQueryContext.getSiddhiAppContext().getName() + "' hence reverting back to " + "querying only with where clause. Reason for failure: " + e.getMessage(), e);
                    }
                // Nothing to override
                }
            }
            populateJoinProcessors(leftMetaStreamEvent, leftInputStreamId, leftPreJoinProcessor, leftPostJoinProcessor, leftCompiledCondition, leftCompiledSelection, expectedOutputAttributes);
        }
        JoinStreamRuntime joinStreamRuntime = new JoinStreamRuntime(siddhiQueryContext, metaStateEvent);
        joinStreamRuntime.addRuntime(leftStreamRuntime);
        joinStreamRuntime.addRuntime(rightStreamRuntime);
        joinStreamRuntime.setQuerySelector(querySelector);
        return joinStreamRuntime;
    } catch (Throwable t) {
        ExceptionUtil.populateQueryContext(t, joinInputStream, siddhiQueryContext.getSiddhiAppContext(), siddhiQueryContext);
        throw t;
    }
}
Also used : ProcessStreamReceiver(io.siddhi.core.query.input.ProcessStreamReceiver) MultiProcessStreamReceiver(io.siddhi.core.query.input.MultiProcessStreamReceiver) TableWindowProcessor(io.siddhi.core.query.processor.stream.window.TableWindowProcessor) Attribute(io.siddhi.query.api.definition.Attribute) ArrayList(java.util.ArrayList) AggregateWindowProcessor(io.siddhi.core.query.processor.stream.window.AggregateWindowProcessor) SingleInputStream(io.siddhi.query.api.execution.query.input.stream.SingleInputStream) QueryableRecordTableException(io.siddhi.core.exception.QueryableRecordTableException) JoinProcessor(io.siddhi.core.query.input.stream.join.JoinProcessor) FindableProcessor(io.siddhi.core.query.processor.stream.window.FindableProcessor) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) SingleStreamRuntime(io.siddhi.core.query.input.stream.single.SingleStreamRuntime) JoinStreamRuntime(io.siddhi.core.query.input.stream.join.JoinStreamRuntime) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) MultiProcessStreamReceiver(io.siddhi.core.query.input.MultiProcessStreamReceiver) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) QuerySelector(io.siddhi.core.query.selector.QuerySelector) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) CompiledCondition(io.siddhi.core.util.collection.operator.CompiledCondition) Expression(io.siddhi.query.api.expression.Expression) MatchingMetaInfoHolder(io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) CompiledSelection(io.siddhi.core.util.collection.operator.CompiledSelection) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 4 with JoinProcessor

use of io.siddhi.core.query.input.stream.join.JoinProcessor in project siddhi by wso2.

the class QueryParserHelper method initSingleStreamRuntime.

private static void initSingleStreamRuntime(SingleStreamRuntime singleStreamRuntime, int streamEventChainIndex, MetaComplexEvent metaComplexEvent, StateEventFactory stateEventFactory, LockWrapper lockWrapper, String queryName) {
    MetaStreamEvent metaStreamEvent;
    if (metaComplexEvent instanceof MetaStateEvent) {
        metaStreamEvent = ((MetaStateEvent) metaComplexEvent).getMetaStreamEvent(streamEventChainIndex);
    } else {
        metaStreamEvent = (MetaStreamEvent) metaComplexEvent;
    }
    StreamEventFactory streamEventFactory = new StreamEventFactory(metaStreamEvent);
    ProcessStreamReceiver processStreamReceiver = singleStreamRuntime.getProcessStreamReceiver();
    processStreamReceiver.setMetaStreamEvent(metaStreamEvent);
    processStreamReceiver.setStreamEventFactory(streamEventFactory);
    processStreamReceiver.setLockWrapper(lockWrapper);
    processStreamReceiver.init();
    Processor processor = singleStreamRuntime.getProcessorChain();
    while (processor != null) {
        if (processor instanceof SchedulingProcessor) {
            ((SchedulingProcessor) processor).getScheduler().setStreamEventFactory(streamEventFactory);
            ((SchedulingProcessor) processor).getScheduler().init(lockWrapper, queryName);
        }
        if (processor instanceof AbstractStreamProcessor) {
            ((AbstractStreamProcessor) processor).setStreamEventCloner(new StreamEventCloner(metaStreamEvent, streamEventFactory));
            ((AbstractStreamProcessor) processor).constructStreamEventPopulater(metaStreamEvent, streamEventChainIndex);
        }
        if (stateEventFactory != null && processor instanceof JoinProcessor) {
            if (((JoinProcessor) processor).getCompiledCondition() instanceof IncrementalAggregateCompileCondition) {
                IncrementalAggregateCompileCondition compiledCondition = (IncrementalAggregateCompileCondition) ((JoinProcessor) processor).getCompiledCondition();
                compiledCondition.init();
                ComplexEventPopulater complexEventPopulater = StreamEventPopulaterFactory.constructEventPopulator(metaStreamEvent, 0, compiledCondition.getAdditionalAttributes());
                compiledCondition.setComplexEventPopulater(complexEventPopulater);
            }
            ((JoinProcessor) processor).setStateEventFactory(stateEventFactory);
        }
        if (stateEventFactory != null && processor instanceof StreamPreStateProcessor) {
            ((StreamPreStateProcessor) processor).setStateEventFactory(stateEventFactory);
            ((StreamPreStateProcessor) processor).setStreamEventFactory(streamEventFactory);
            ((StreamPreStateProcessor) processor).setStreamEventCloner(new StreamEventCloner(metaStreamEvent, streamEventFactory));
            if (metaComplexEvent instanceof MetaStateEvent) {
                ((StreamPreStateProcessor) processor).setStateEventCloner(new StateEventCloner(((MetaStateEvent) metaComplexEvent), stateEventFactory));
            }
        }
        processor = processor.getNextProcessor();
    }
}
Also used : ProcessStreamReceiver(io.siddhi.core.query.input.ProcessStreamReceiver) AbstractStreamProcessor(io.siddhi.core.query.processor.stream.AbstractStreamProcessor) Processor(io.siddhi.core.query.processor.Processor) SchedulingProcessor(io.siddhi.core.query.processor.SchedulingProcessor) StreamPreStateProcessor(io.siddhi.core.query.input.stream.state.StreamPreStateProcessor) JoinProcessor(io.siddhi.core.query.input.stream.join.JoinProcessor) AbstractStreamProcessor(io.siddhi.core.query.processor.stream.AbstractStreamProcessor) StreamEventFactory(io.siddhi.core.event.stream.StreamEventFactory) IncrementalAggregateCompileCondition(io.siddhi.core.util.collection.operator.IncrementalAggregateCompileCondition) ComplexEventPopulater(io.siddhi.core.event.stream.populater.ComplexEventPopulater) StreamPreStateProcessor(io.siddhi.core.query.input.stream.state.StreamPreStateProcessor) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) SchedulingProcessor(io.siddhi.core.query.processor.SchedulingProcessor) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) JoinProcessor(io.siddhi.core.query.input.stream.join.JoinProcessor) StateEventCloner(io.siddhi.core.event.state.StateEventCloner) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)4 JoinProcessor (io.siddhi.core.query.input.stream.join.JoinProcessor)4 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)3 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)3 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)3 Processor (io.siddhi.core.query.processor.Processor)3 ProcessStreamReceiver (io.siddhi.core.query.input.ProcessStreamReceiver)2 JoinStreamRuntime (io.siddhi.core.query.input.stream.join.JoinStreamRuntime)2 SingleStreamRuntime (io.siddhi.core.query.input.stream.single.SingleStreamRuntime)2 AggregateWindowProcessor (io.siddhi.core.query.processor.stream.window.AggregateWindowProcessor)2 FindableProcessor (io.siddhi.core.query.processor.stream.window.FindableProcessor)2 TableWindowProcessor (io.siddhi.core.query.processor.stream.window.TableWindowProcessor)2 QuerySelector (io.siddhi.core.query.selector.QuerySelector)2 CompiledSelection (io.siddhi.core.util.collection.operator.CompiledSelection)2 SingleInputStream (io.siddhi.query.api.execution.query.input.stream.SingleInputStream)2 ArrayList (java.util.ArrayList)2 AggregationRuntime (io.siddhi.core.aggregation.AggregationRuntime)1 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)1 SiddhiQueryContext (io.siddhi.core.config.SiddhiQueryContext)1 StateEventCloner (io.siddhi.core.event.state.StateEventCloner)1