Search in sources :

Example 6 with VariableExpressionExecutor

use of io.siddhi.core.executor.VariableExpressionExecutor in project siddhi by wso2.

the class BaseIncrementalValueStore method process.

public synchronized void process(Map<String, StreamEvent> groupedByEvents) {
    for (Map.Entry<String, StreamEvent> eventEntry : groupedByEvents.entrySet()) {
        synchronized (this) {
            SiddhiAppContext.startGroupByFlow(eventEntry.getKey() + "-" + eventEntry.getValue().getTimestamp());
            ValueState state = valueStateHolder.getState();
            try {
                boolean shouldUpdate = true;
                if (shouldUpdateTimestamp != null) {
                    shouldUpdate = shouldUpdate(shouldUpdateTimestamp.execute(eventEntry.getValue()), state);
                }
                for (int i = 0; i < expressionExecutors.size(); i++) {
                    // keeping timestamp value location as null
                    ExpressionExecutor expressionExecutor = expressionExecutors.get(i);
                    if (shouldUpdate) {
                        state.setValue(expressionExecutor.execute(eventEntry.getValue()), i + 1);
                    } else if (!(expressionExecutor instanceof VariableExpressionExecutor)) {
                        state.setValue(expressionExecutor.execute(eventEntry.getValue()), i + 1);
                    }
                }
                setProcessed(true);
            } finally {
                valueStateHolder.returnState(state);
                SiddhiAppContext.stopGroupByFlow();
            }
        }
    }
}
Also used : VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) StreamEvent(io.siddhi.core.event.stream.StreamEvent) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with VariableExpressionExecutor

use of io.siddhi.core.executor.VariableExpressionExecutor in project siddhi by wso2.

the class IncrementalDataPurger method init.

public void init(AggregationDefinition aggregationDefinition, StreamEventFactory streamEventFactory, Map<TimePeriod.Duration, Table> aggregationTables, Boolean isProcessingOnExternalTime, SiddhiQueryContext siddhiQueryContext, List<TimePeriod.Duration> activeIncrementalDurations, String timeZone, Map<String, Window> windowMap, Map<String, AggregationRuntime> aggregationMap) {
    this.siddhiQueryContext = siddhiQueryContext;
    this.aggregationDefinition = aggregationDefinition;
    List<Annotation> annotations = aggregationDefinition.getAnnotations();
    this.streamEventFactory = streamEventFactory;
    this.aggregationTables = aggregationTables;
    this.activeIncrementalDurations = activeIncrementalDurations;
    this.windowMap = windowMap;
    this.aggregationMap = aggregationMap;
    if (isProcessingOnExternalTime) {
        purgingTimestampField = AGG_EXTERNAL_TIMESTAMP_COL;
    } else {
        purgingTimestampField = AGG_START_TIMESTAMP_COL;
    }
    aggregatedTimestampAttribute = new Attribute(purgingTimestampField, Attribute.Type.LONG);
    VariableExpressionExecutor variableExpressionExecutor = new VariableExpressionExecutor(aggregatedTimestampAttribute, 0, 1);
    variableExpressionExecutorList.add(variableExpressionExecutor);
    for (Map.Entry<TimePeriod.Duration, Table> entry : aggregationTables.entrySet()) {
        this.tableMap.put(entry.getValue().getTableDefinition().getId(), entry.getValue());
        switch(entry.getKey()) {
            case SECONDS:
                retentionPeriods.put(entry.getKey(), Expression.Time.sec(120).value());
                minimumDurationMap.put(entry.getKey(), Expression.Time.sec(120).value());
                break;
            case MINUTES:
                retentionPeriods.put(entry.getKey(), Expression.Time.hour(24).value());
                minimumDurationMap.put(entry.getKey(), Expression.Time.minute(120).value());
                break;
            case HOURS:
                retentionPeriods.put(entry.getKey(), Expression.Time.day(30).value());
                minimumDurationMap.put(entry.getKey(), Expression.Time.hour(25).value());
                break;
            case DAYS:
                retentionPeriods.put(entry.getKey(), Expression.Time.year(1).value());
                minimumDurationMap.put(entry.getKey(), Expression.Time.day(32).value());
                break;
            case MONTHS:
                retentionPeriods.put(entry.getKey(), RETAIN_ALL);
                minimumDurationMap.put(entry.getKey(), Expression.Time.month(13).value());
                break;
            case YEARS:
                retentionPeriods.put(entry.getKey(), RETAIN_ALL);
                minimumDurationMap.put(entry.getKey(), 0L);
        }
    }
    this.timeZone = timeZone;
    Map<String, Annotation> annotationTypes = new HashMap<>();
    for (Annotation annotation : annotations) {
        annotationTypes.put(annotation.getName().toLowerCase(), annotation);
    }
    Annotation purge = annotationTypes.get(SiddhiConstants.NAMESPACE_PURGE);
    if (purge != null) {
        if (purge.getElement(SiddhiConstants.ANNOTATION_ELEMENT_ENABLE) != null) {
            String purgeEnable = purge.getElement(SiddhiConstants.ANNOTATION_ELEMENT_ENABLE);
            if (!("true".equalsIgnoreCase(purgeEnable) || "false".equalsIgnoreCase(purgeEnable))) {
                throw new SiddhiAppCreationException("Invalid value for enable: " + purgeEnable + "." + " Please use true or false");
            } else {
                purgingEnabled = Boolean.parseBoolean(purgeEnable);
            }
        }
        if (purgingEnabled) {
            // If interval is defined, default value of 15 min will be replaced by user input value
            if (purge.getElement(SiddhiConstants.ANNOTATION_ELEMENT_INTERVAL) != null) {
                String interval = purge.getElement(SiddhiConstants.ANNOTATION_ELEMENT_INTERVAL);
                purgeExecutionInterval = timeToLong(interval);
            }
            List<Annotation> retentions = purge.getAnnotations(SiddhiConstants.NAMESPACE_RETENTION_PERIOD);
            if (retentions != null && !retentions.isEmpty()) {
                Annotation retention = retentions.get(0);
                List<Element> elements = retention.getElements();
                for (Element element : elements) {
                    TimePeriod.Duration duration = normalizeDuration(element.getKey());
                    if (!activeIncrementalDurations.contains(duration)) {
                        throw new SiddhiAppCreationException(duration + " granularity cannot be purged since " + "aggregation has not performed in " + duration + " granularity");
                    }
                    if (element.getValue().equalsIgnoreCase(RETAIN_ALL_VALUES)) {
                        retentionPeriods.put(duration, RETAIN_ALL);
                    } else {
                        if (timeToLong(element.getValue()) >= minimumDurationMap.get(duration)) {
                            retentionPeriods.put(duration, timeToLong(element.getValue()));
                        } else {
                            throw new SiddhiAppCreationException(duration + " granularity cannot be purge" + " with a retention of '" + element.getValue() + "', minimum retention" + " should be greater  than " + TimeUnit.MILLISECONDS.toMinutes(minimumDurationMap.get(duration)) + " minutes");
                        }
                    }
                }
            }
        }
    }
    compiledConditionsHolder = createCompileConditions(aggregationTables, tableMap);
}
Also used : Table(io.siddhi.core.table.Table) Attribute(io.siddhi.query.api.definition.Attribute) OrderByAttribute(io.siddhi.query.api.execution.query.selection.OrderByAttribute) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) HashMap(java.util.HashMap) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) TimePeriod(io.siddhi.query.api.aggregation.TimePeriod) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) Element(io.siddhi.query.api.annotation.Element) Time.normalizeDuration(io.siddhi.query.api.expression.Expression.Time.normalizeDuration) Annotation(io.siddhi.query.api.annotation.Annotation) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap)

Example 8 with VariableExpressionExecutor

use of io.siddhi.core.executor.VariableExpressionExecutor 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 9 with VariableExpressionExecutor

use of io.siddhi.core.executor.VariableExpressionExecutor 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 10 with VariableExpressionExecutor

use of io.siddhi.core.executor.VariableExpressionExecutor in project siddhi by wso2.

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 metaComplexEvent            Meta event used to collect execution info of stream associated with query
 * @param tableMap                    Table Map
 * @param variableExpressionExecutors variable expression executors
 * @param metaPosition                helps to identify the meta position of aggregates
 * @param processingMode              processing mode of the query
 * @param outputExpectsExpiredEvents  is expired events sent as output
 * @param siddhiQueryContext          current siddhi query context
 * @return QuerySelector
 */
public static QuerySelector parse(Selector selector, OutputStream outputStream, MetaComplexEvent metaComplexEvent, Map<String, Table> tableMap, List<VariableExpressionExecutor> variableExpressionExecutors, int metaPosition, ProcessingMode processingMode, boolean outputExpectsExpiredEvents, SiddhiQueryContext siddhiQueryContext) {
    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;
    }
    boolean groupBy = !selector.getGroupByList().isEmpty();
    id = outputStream.getId();
    containsAggregatorThreadLocal.remove();
    QuerySelector querySelector = new QuerySelector(id, selector, currentOn, expiredOn, siddhiQueryContext);
    List<AttributeProcessor> attributeProcessors = getAttributeProcessors(selector, id, metaComplexEvent, tableMap, variableExpressionExecutors, outputStream, metaPosition, processingMode, outputExpectsExpiredEvents, groupBy, siddhiQueryContext);
    querySelector.setAttributeProcessorList(attributeProcessors, "true".equals(containsAggregatorThreadLocal.get()));
    containsAggregatorThreadLocal.remove();
    ConditionExpressionExecutor havingCondition = generateHavingExecutor(selector.getHavingExpression(), metaComplexEvent, tableMap, variableExpressionExecutors, siddhiQueryContext);
    querySelector.setHavingConditionExecutor(havingCondition, "true".equals(containsAggregatorThreadLocal.get()));
    containsAggregatorThreadLocal.remove();
    if (!selector.getGroupByList().isEmpty()) {
        List<Expression> groupByExpressionList = selector.getGroupByList().stream().map(groupByVariable -> (Expression) groupByVariable).collect(Collectors.toList());
        querySelector.setGroupByKeyGenerator(new GroupByKeyGenerator(groupByExpressionList, metaComplexEvent, SiddhiConstants.UNKNOWN_STATE, null, variableExpressionExecutors, siddhiQueryContext));
    }
    if (!selector.getOrderByList().isEmpty()) {
        querySelector.setOrderByEventComparator(new OrderByEventComparator(selector.getOrderByList(), metaComplexEvent, SiddhiConstants.HAVING_STATE, null, variableExpressionExecutors, siddhiQueryContext));
    }
    if (selector.getLimit() != null) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression((Expression) selector.getLimit(), metaComplexEvent, SiddhiConstants.HAVING_STATE, tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        containsAggregatorThreadLocal.remove();
        querySelector.setLimit(((Number) (((ConstantExpressionExecutor) expressionExecutor).getValue())).longValue());
    }
    if (selector.getOffset() != null) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression((Expression) selector.getOffset(), metaComplexEvent, SiddhiConstants.HAVING_STATE, tableMap, variableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        containsAggregatorThreadLocal.remove();
        querySelector.setOffset(((Number) (((ConstantExpressionExecutor) expressionExecutor).getValue())).longValue());
    }
    return querySelector;
}
Also used : Selector(io.siddhi.query.api.execution.query.selection.Selector) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) GroupByKeyGenerator(io.siddhi.core.query.selector.GroupByKeyGenerator) Variable(io.siddhi.query.api.expression.Variable) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) SiddhiConstants(io.siddhi.core.util.SiddhiConstants) QuerySelector(io.siddhi.core.query.selector.QuerySelector) Expression(io.siddhi.query.api.expression.Expression) ArrayList(java.util.ArrayList) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) AttributeProcessor(io.siddhi.core.query.selector.attribute.processor.AttributeProcessor) Table(io.siddhi.core.table.Table) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) MetaComplexEvent(io.siddhi.core.event.MetaComplexEvent) MetaStateEventAttribute(io.siddhi.core.event.state.MetaStateEventAttribute) Map(java.util.Map) SiddhiQueryContext(io.siddhi.core.config.SiddhiQueryContext) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) ProcessingMode(io.siddhi.core.query.processor.ProcessingMode) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) OrderByEventComparator(io.siddhi.core.query.selector.OrderByEventComparator) ConditionExpressionExecutor(io.siddhi.core.executor.condition.ConditionExpressionExecutor) Attribute(io.siddhi.query.api.definition.Attribute) DuplicateAttributeException(io.siddhi.query.api.exception.DuplicateAttributeException) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) Collectors(java.util.stream.Collectors) List(java.util.List) OutputStream(io.siddhi.query.api.execution.query.output.stream.OutputStream) 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) Expression(io.siddhi.query.api.expression.Expression) GroupByKeyGenerator(io.siddhi.core.query.selector.GroupByKeyGenerator) QuerySelector(io.siddhi.core.query.selector.QuerySelector) AttributeProcessor(io.siddhi.core.query.selector.attribute.processor.AttributeProcessor) ConditionExpressionExecutor(io.siddhi.core.executor.condition.ConditionExpressionExecutor) OrderByEventComparator(io.siddhi.core.query.selector.OrderByEventComparator)

Aggregations

VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)48 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)32 Attribute (io.siddhi.query.api.definition.Attribute)27 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)23 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)23 ArrayList (java.util.ArrayList)20 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)18 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)15 Variable (io.siddhi.query.api.expression.Variable)14 OutputAttribute (io.siddhi.query.api.execution.query.selection.OutputAttribute)13 Expression (io.siddhi.query.api.expression.Expression)13 Map (java.util.Map)13 HashMap (java.util.HashMap)11 AbstractDefinition (io.siddhi.query.api.definition.AbstractDefinition)10 StreamEvent (io.siddhi.core.event.stream.StreamEvent)9 Table (io.siddhi.core.table.Table)8 MatchingMetaInfoHolder (io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)8 SiddhiQueryContext (io.siddhi.core.config.SiddhiQueryContext)7 CompiledCondition (io.siddhi.core.util.collection.operator.CompiledCondition)7 AndConditionExpressionExecutor (io.siddhi.core.executor.condition.AndConditionExpressionExecutor)5