Search in sources :

Example 31 with Attribute

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

the class TestStoreForCacheMiss method compileSelection.

@Override
protected CompiledSelection compileSelection(List<SelectAttributeBuilder> selectAttributeBuilders, List<ExpressionBuilder> groupByExpressionBuilder, ExpressionBuilder havingExpressionBuilder, List<OrderByAttributeBuilder> orderByAttributeBuilders, Long limit, Long offset) {
    selectAttributeBuilders.forEach((selectAttributeBuilder -> {
        TestStoreConditionVisitor testStoreConditionVisitor = new TestStoreConditionVisitor("");
        selectAttributeBuilder.getExpressionBuilder().build(testStoreConditionVisitor);
        if (testStoreConditionVisitor.getStreamVarCount() > 0) {
            throw new SiddhiAppCreationException("testStoreContainingInMemoryTable does not support " + "lookup with stream variables");
        }
    }));
    CompiledSelectionWithCache compiledSelectionWithCache;
    MetaStateEvent metaStateEvent = matchingMetaInfoHolderForTestOnDemandQuery.getMetaStateEvent().clone();
    ReturnStream returnStream = new ReturnStream(OutputStream.OutputEventType.CURRENT_EVENTS);
    int metaPosition = SiddhiConstants.UNKNOWN_STATE;
    List<VariableExpressionExecutor> variableExpressionExecutorsForQuerySelector = new ArrayList<>();
    if (metaStateEvent.getOutputDataAttributes().size() == 0) {
        for (Attribute outputAttribute : metaStateEvent.getMetaStreamEvents()[0].getOnAfterWindowData()) {
            metaStateEvent.getMetaStreamEvents()[0].addOutputData(outputAttribute);
        }
    }
    QuerySelector querySelector = SelectorParser.parse(selectorForTestOnDemandQuery, returnStream, metaStateEvent, tableMap, variableExpressionExecutorsForQuerySelector, metaPosition, ProcessingMode.BATCH, false, siddhiQueryContextForTestOnDemandQuery);
    QueryParserHelper.updateVariablePosition(metaStateEvent, variableExpressionExecutorsForQuerySelector);
    querySelector.setEventPopulator(StateEventPopulatorFactory.constructEventPopulator(metaStateEvent));
    compiledSelectionWithCache = new CompiledSelectionWithCache(null, querySelector, metaStateEvent, 0, null);
    return compiledSelectionWithCache;
}
Also used : Extension(io.siddhi.annotation.Extension) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) SiddhiConstants(io.siddhi.core.util.SiddhiConstants) CompiledSelection(io.siddhi.core.util.collection.operator.CompiledSelection) Annotation(io.siddhi.query.api.annotation.Annotation) QuerySelector(io.siddhi.core.query.selector.QuerySelector) StreamEvent(io.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList) RecordIterator(io.siddhi.core.table.record.RecordIterator) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) ExpressionBuilder(io.siddhi.core.table.record.ExpressionBuilder) Map(java.util.Map) InMemoryTable(io.siddhi.core.table.InMemoryTable) SelectorParser(io.siddhi.core.util.parser.SelectorParser) TableDefinition(io.siddhi.query.api.definition.TableDefinition) LinkedList(java.util.LinkedList) ProcessingMode(io.siddhi.core.query.processor.ProcessingMode) ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) Event(io.siddhi.core.event.Event) Example(io.siddhi.annotation.Example) ComplexEvent(io.siddhi.core.event.ComplexEvent) Attribute(io.siddhi.query.api.definition.Attribute) CompiledCondition(io.siddhi.core.util.collection.operator.CompiledCondition) OnDemandQueryRuntimeUtil.executeSelector(io.siddhi.core.util.OnDemandQueryRuntimeUtil.executeSelector) ConfigReader(io.siddhi.core.util.config.ConfigReader) StateEventFactory(io.siddhi.core.event.state.StateEventFactory) StateEventPopulatorFactory(io.siddhi.core.event.state.populater.StateEventPopulatorFactory) AbstractQueryableRecordTable(io.siddhi.core.table.record.AbstractQueryableRecordTable) List(java.util.List) CompiledExpression(io.siddhi.core.util.collection.operator.CompiledExpression) OutputStream(io.siddhi.query.api.execution.query.output.stream.OutputStream) ConnectionUnavailableException(io.siddhi.core.exception.ConnectionUnavailableException) StateEvent(io.siddhi.core.event.state.StateEvent) QueryParserHelper(io.siddhi.core.util.parser.helper.QueryParserHelper) ReturnStream(io.siddhi.query.api.execution.query.output.stream.ReturnStream) Attribute(io.siddhi.query.api.definition.Attribute) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ArrayList(java.util.ArrayList) QuerySelector(io.siddhi.core.query.selector.QuerySelector) ReturnStream(io.siddhi.query.api.execution.query.output.stream.ReturnStream) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent)

Example 32 with Attribute

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

the class TestStoreForCacheMiss method init.

@Override
protected void init(TableDefinition tableDefinition, ConfigReader configReader) {
    inMemoryTable = new InMemoryTable();
    MetaStreamEvent cacheTableMetaStreamEvent = new MetaStreamEvent();
    cacheTableMetaStreamEvent.addInputDefinition(tableDefinition);
    for (Attribute attribute : tableDefinition.getAttributeList()) {
        cacheTableMetaStreamEvent.addOutputData(attribute);
    }
    StreamEventCloner testTableStreamEventCloner = new StreamEventCloner(cacheTableMetaStreamEvent, storeEventPool);
    TableDefinition testStoreContainingIMTableDefinition = TableDefinition.id(tableDefinition.getId());
    for (Attribute attribute : tableDefinition.getAttributeList()) {
        testStoreContainingIMTableDefinition.attribute(attribute.getName(), attribute.getType());
    }
    for (Annotation annotation : tableDefinition.getAnnotations()) {
        if (!annotation.getName().equalsIgnoreCase("Store")) {
            testStoreContainingIMTableDefinition.annotation(annotation);
        }
    }
    inMemoryTable.init(testStoreContainingIMTableDefinition, storeEventPool, testTableStreamEventCloner, configReader, siddhiAppContext, recordTableHandler);
}
Also used : InMemoryTable(io.siddhi.core.table.InMemoryTable) Attribute(io.siddhi.query.api.definition.Attribute) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) TableDefinition(io.siddhi.query.api.definition.TableDefinition) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) Annotation(io.siddhi.query.api.annotation.Annotation)

Example 33 with Attribute

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

the class TestStoreConditionVisitor method beginVisitStreamVariable.

@Override
public void beginVisitStreamVariable(String id, String streamId, String attributeName, Attribute.Type type) {
    String name;
    if (nextProcessContainsPattern) {
        name = this.generatePatternStreamVarName();
        nextProcessContainsPattern = false;
    } else {
        name = this.generateStreamVarName();
    }
    this.placeholders.put(name, new Attribute(id, type));
    condition.append("[").append(name).append("]").append(TestStoreTableConstants.WHITESPACE);
}
Also used : Attribute(io.siddhi.query.api.definition.Attribute)

Example 34 with Attribute

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

the class TestStoreForCachePreLoading method init.

@Override
protected void init(TableDefinition tableDefinition, ConfigReader configReader) {
    inMemoryTable = new InMemoryTable();
    MetaStreamEvent cacheTableMetaStreamEvent = new MetaStreamEvent();
    cacheTableMetaStreamEvent.addInputDefinition(tableDefinition);
    for (Attribute attribute : tableDefinition.getAttributeList()) {
        cacheTableMetaStreamEvent.addOutputData(attribute);
    }
    StreamEventCloner testTableStreamEventCloner = new StreamEventCloner(cacheTableMetaStreamEvent, storeEventPool);
    TableDefinition testStoreContainingIMTableDefinition = TableDefinition.id(tableDefinition.getId());
    for (Attribute attribute : tableDefinition.getAttributeList()) {
        testStoreContainingIMTableDefinition.attribute(attribute.getName(), attribute.getType());
    }
    for (Annotation annotation : tableDefinition.getAnnotations()) {
        if (!annotation.getName().equalsIgnoreCase("Store")) {
            testStoreContainingIMTableDefinition.annotation(annotation);
        }
    }
    inMemoryTable.init(testStoreContainingIMTableDefinition, storeEventPool, testTableStreamEventCloner, configReader, siddhiAppContext, recordTableHandler);
    ComplexEventChunk<StreamEvent> originalData = new ComplexEventChunk<>();
    StreamEvent data1 = new StreamEvent(0, 0, 3);
    data1.setOutputData(new Object[] { "WSO2", 55.6f, 100L });
    originalData.add(data1);
    StreamEvent data2 = new StreamEvent(0, 0, 3);
    data2.setOutputData(new Object[] { "IBM", 75.6f, 100L });
    originalData.add(data2);
    inMemoryTable.add(originalData);
}
Also used : InMemoryTable(io.siddhi.core.table.InMemoryTable) ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) Attribute(io.siddhi.query.api.definition.Attribute) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) TableDefinition(io.siddhi.query.api.definition.TableDefinition) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) Annotation(io.siddhi.query.api.annotation.Annotation)

Example 35 with Attribute

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

the class IncrementalAggregateCompileCondition method find.

public StreamEvent find(StateEvent matchingEvent, Map<TimePeriod.Duration, Executor> incrementalExecutorMap, Map<TimePeriod.Duration, List<ExpressionExecutor>> aggregateProcessingExecutorsMap, Map<TimePeriod.Duration, GroupByKeyGenerator> groupByKeyGeneratorMap, ExpressionExecutor shouldUpdateTimestamp, String timeZone) {
    ComplexEventChunk<StreamEvent> complexEventChunkToHoldWithinMatches = new ComplexEventChunk<>();
    // Create matching event if it is on-demand query
    int additionTimestampAttributesSize = this.timestampFilterExecutors.size() + 2;
    Long[] timestampFilters = new Long[additionTimestampAttributesSize];
    if (matchingEvent.getStreamEvent(0) == null) {
        StreamEvent streamEvent = new StreamEvent(0, additionTimestampAttributesSize, 0);
        matchingEvent.addEvent(0, streamEvent);
    }
    Long[] startTimeEndTime = (Long[]) startTimeEndTimeExpressionExecutor.execute(matchingEvent);
    if (startTimeEndTime == null) {
        throw new SiddhiAppRuntimeException("Start and end times for within duration cannot be retrieved");
    }
    timestampFilters[0] = startTimeEndTime[0];
    timestampFilters[1] = startTimeEndTime[1];
    if (isDistributed) {
        for (int i = 0; i < additionTimestampAttributesSize - 2; i++) {
            timestampFilters[i + 2] = ((Long) this.timestampFilterExecutors.get(i).execute(matchingEvent));
        }
    }
    complexEventPopulater.populateComplexEvent(matchingEvent.getStreamEvent(0), timestampFilters);
    // Get all the aggregates within the given duration, from table corresponding to "per" duration
    // Retrieve per value
    String perValueAsString = perExpressionExecutor.execute(matchingEvent).toString();
    TimePeriod.Duration perValue;
    try {
        // Per time function verification
        perValue = normalizeDuration(perValueAsString);
    } catch (SiddhiAppValidationException e) {
        throw new SiddhiAppRuntimeException("Aggregation Query's per value is expected to be of a valid time function of the " + "following " + TimePeriod.Duration.SECONDS + ", " + TimePeriod.Duration.MINUTES + ", " + TimePeriod.Duration.HOURS + ", " + TimePeriod.Duration.DAYS + ", " + TimePeriod.Duration.MONTHS + ", " + TimePeriod.Duration.YEARS + ".");
    }
    if (!incrementalExecutorMap.keySet().contains(perValue)) {
        throw new SiddhiAppRuntimeException("The aggregate values for " + perValue.toString() + " granularity cannot be provided since aggregation definition " + aggregationName + " does not contain " + perValue.toString() + " duration");
    }
    Table tableForPerDuration = aggregationTableMap.get(perValue);
    StreamEvent withinMatchFromPersistedEvents;
    if (isOptimisedLookup) {
        withinMatchFromPersistedEvents = query(tableForPerDuration, matchingEvent, withinTableCompiledConditions.get(perValue), withinTableCompiledSelection.get(perValue), tableMetaStreamEvent.getLastInputDefinition().getAttributeList().toArray(new Attribute[0]));
    } else {
        withinMatchFromPersistedEvents = tableForPerDuration.find(matchingEvent, withinTableCompiledConditions.get(perValue));
    }
    complexEventChunkToHoldWithinMatches.add(withinMatchFromPersistedEvents);
    // Optimization step.
    long oldestInMemoryEventTimestamp = getOldestInMemoryEventTimestamp(incrementalExecutorMap, activeIncrementalDurations, perValue);
    // If processing on external time, the in-memory data also needs to be queried
    if (isProcessingOnExternalTime || requiresAggregatingInMemoryData(oldestInMemoryEventTimestamp, startTimeEndTime)) {
        if (isDistributed) {
            int perValueIndex = this.activeIncrementalDurations.indexOf(perValue);
            if (perValueIndex != 0) {
                Map<TimePeriod.Duration, CompiledCondition> lowerGranularityLookups = new HashMap<>();
                for (int i = 0; i < perValueIndex; i++) {
                    TimePeriod.Duration key = this.activeIncrementalDurations.get(i);
                    lowerGranularityLookups.put(key, withinTableLowerGranularityCompileCondition.get(key));
                }
                List<StreamEvent> eventChunks = lowerGranularityLookups.entrySet().stream().map((entry) -> {
                    Table table = aggregationTableMap.get(entry.getKey());
                    if (isOptimisedLookup) {
                        return query(table, matchingEvent, entry.getValue(), withinTableCompiledSelection.get(entry.getKey()), tableMetaStreamEvent.getLastInputDefinition().getAttributeList().toArray(new Attribute[0]));
                    } else {
                        return table.find(matchingEvent, entry.getValue());
                    }
                }).filter(Objects::nonNull).collect(Collectors.toList());
                eventChunks.forEach(complexEventChunkToHoldWithinMatches::add);
            }
        } else {
            TimePeriod.Duration rootDuration = activeIncrementalDurations.get(0);
            IncrementalDataAggregator incrementalDataAggregator = new IncrementalDataAggregator(activeIncrementalDurations, perValue, oldestInMemoryEventTimestamp, aggregateProcessingExecutorsMap.get(rootDuration), shouldUpdateTimestamp, groupByKeyGeneratorMap.get(rootDuration) != null, tableMetaStreamEvent, timeZone);
            ComplexEventChunk<StreamEvent> aggregatedInMemoryEventChunk;
            // Aggregate in-memory data and create an event chunk out of it
            aggregatedInMemoryEventChunk = incrementalDataAggregator.aggregateInMemoryData(incrementalExecutorMap);
            // Get the in-memory aggregate data, which is within given duration
            StreamEvent withinMatchFromInMemory = ((Operator) inMemoryStoreCompileCondition).find(matchingEvent, aggregatedInMemoryEventChunk, tableEventCloner);
            complexEventChunkToHoldWithinMatches.add(withinMatchFromInMemory);
        }
    }
    ComplexEventChunk<StreamEvent> processedEvents;
    if (isDistributed || isProcessingOnExternalTime) {
        List<ExpressionExecutor> expressionExecutors = aggregateProcessingExecutorsMap.get(perValue);
        GroupByKeyGenerator groupByKeyGenerator = groupByKeyGeneratorMap.get(perValue);
        OutOfOrderEventsDataAggregator outOfOrderEventsDataAggregator = new OutOfOrderEventsDataAggregator(expressionExecutors, shouldUpdateTimestamp, groupByKeyGenerator, tableMetaStreamEvent);
        processedEvents = outOfOrderEventsDataAggregator.aggregateData(complexEventChunkToHoldWithinMatches);
    } else {
        processedEvents = complexEventChunkToHoldWithinMatches;
    }
    // Get the final event chunk from the data which is within given duration. This event chunk contains the values
    // in the select clause of an aggregate definition.
    ComplexEventChunk<StreamEvent> aggregateSelectionComplexEventChunk = createAggregateSelectionEventChunk(processedEvents, outputExpressionExecutors);
    // Execute the on compile condition
    return ((Operator) onCompiledCondition).find(matchingEvent, aggregateSelectionComplexEventChunk, aggregateEventCloner);
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) HashMap(java.util.HashMap) Attribute(io.siddhi.query.api.definition.Attribute) GroupByKeyGenerator(io.siddhi.core.query.selector.GroupByKeyGenerator) SiddhiAppRuntimeException(io.siddhi.core.exception.SiddhiAppRuntimeException) OutOfOrderEventsDataAggregator(io.siddhi.core.aggregation.OutOfOrderEventsDataAggregator) Table(io.siddhi.core.table.Table) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) TimePeriod(io.siddhi.query.api.aggregation.TimePeriod) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) Time.normalizeDuration(io.siddhi.query.api.expression.Expression.Time.normalizeDuration) IncrementalDataAggregator(io.siddhi.core.aggregation.IncrementalDataAggregator)

Aggregations

Attribute (io.siddhi.query.api.definition.Attribute)86 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)43 ArrayList (java.util.ArrayList)29 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)27 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)23 OutputAttribute (io.siddhi.query.api.execution.query.selection.OutputAttribute)23 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)22 StreamEvent (io.siddhi.core.event.stream.StreamEvent)21 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)19 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)18 Variable (io.siddhi.query.api.expression.Variable)18 Expression (io.siddhi.query.api.expression.Expression)17 Test (org.testng.annotations.Test)17 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)15 HashMap (java.util.HashMap)15 StreamEventCloner (io.siddhi.core.event.stream.StreamEventCloner)14 MatchingMetaInfoHolder (io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)14 TableDefinition (io.siddhi.query.api.definition.TableDefinition)13 Map (java.util.Map)13 Annotation (io.siddhi.query.api.annotation.Annotation)12