Search in sources :

Example 11 with AggregationDefinition

use of org.wso2.siddhi.query.api.definition.AggregationDefinition in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitSiddhi_app.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public SiddhiApp visitSiddhi_app(@NotNull SiddhiQLParser.Siddhi_appContext ctx) {
    SiddhiApp siddhiApp = SiddhiApp.siddhiApp();
    for (SiddhiQLParser.App_annotationContext annotationContext : ctx.app_annotation()) {
        siddhiApp.annotation((Annotation) visit(annotationContext));
    }
    for (SiddhiQLParser.Definition_streamContext streamContext : ctx.definition_stream()) {
        siddhiApp.defineStream((StreamDefinition) visit(streamContext));
    }
    for (SiddhiQLParser.Definition_tableContext tableContext : ctx.definition_table()) {
        siddhiApp.defineTable((TableDefinition) visit(tableContext));
    }
    for (SiddhiQLParser.Definition_functionContext functionContext : ctx.definition_function()) {
        siddhiApp.defineFunction((FunctionDefinition) visit(functionContext));
    }
    for (SiddhiQLParser.Definition_windowContext windowContext : ctx.definition_window()) {
        siddhiApp.defineWindow((WindowDefinition) visit(windowContext));
    }
    for (SiddhiQLParser.Definition_aggregationContext aggregationContext : ctx.definition_aggregation()) {
        siddhiApp.defineAggregation((AggregationDefinition) visit(aggregationContext));
    }
    for (SiddhiQLParser.Execution_elementContext executionElementContext : ctx.execution_element()) {
        ExecutionElement executionElement = (ExecutionElement) visit(executionElementContext);
        if (executionElement instanceof Partition) {
            siddhiApp.addPartition((Partition) executionElement);
        } else if (executionElement instanceof Query) {
            siddhiApp.addQuery((Query) executionElement);
        } else {
            throw newSiddhiParserException(ctx);
        }
    }
    for (SiddhiQLParser.Definition_triggerContext triggerContext : ctx.definition_trigger()) {
        siddhiApp.defineTrigger((TriggerDefinition) visit(triggerContext));
    }
    populateQueryContext(siddhiApp, ctx);
    return siddhiApp;
}
Also used : SiddhiQLParser(org.wso2.siddhi.query.compiler.SiddhiQLParser) Partition(org.wso2.siddhi.query.api.execution.partition.Partition) SiddhiApp(org.wso2.siddhi.query.api.SiddhiApp) StoreQuery(org.wso2.siddhi.query.api.execution.query.StoreQuery) Query(org.wso2.siddhi.query.api.execution.query.Query) ExecutionElement(org.wso2.siddhi.query.api.execution.ExecutionElement)

Example 12 with AggregationDefinition

use of org.wso2.siddhi.query.api.definition.AggregationDefinition in project siddhi by wso2.

the class IncrementalAggregateCompileCondition method find.

public StreamEvent find(StateEvent matchingEvent, AggregationDefinition aggregationDefinition, Map<TimePeriod.Duration, IncrementalExecutor> incrementalExecutorMap, Map<TimePeriod.Duration, Table> aggregationTables, List<TimePeriod.Duration> incrementalDurations, List<ExpressionExecutor> baseExecutors, ExpressionExecutor timestampExecutor, List<ExpressionExecutor> outputExpressionExecutors, SiddhiAppContext siddhiAppContext) {
    ComplexEventChunk<StreamEvent> complexEventChunkToHoldWithinMatches = new ComplexEventChunk<>(true);
    // Retrieve per value
    String perValueAsString = perExpressionExecutor.execute(matchingEvent).toString();
    TimePeriod.Duration perValue = TimePeriod.Duration.valueOf(perValueAsString.toUpperCase());
    if (!incrementalExecutorMap.keySet().contains(perValue)) {
        throw new SiddhiAppRuntimeException("The aggregate values for " + perValue.toString() + " granularity cannot be provided since aggregation definition " + aggregationDefinition.getId() + " does not contain " + perValue.toString() + " duration");
    }
    Table tableForPerDuration = aggregationTables.get(perValue);
    Long[] startTimeEndTime = (Long[]) startTimeEndTimeExpressionExecutor.execute(matchingEvent);
    if (startTimeEndTime == null) {
        throw new SiddhiAppRuntimeException("Start and end times for within duration cannot be retrieved");
    }
    complexEventPopulater.populateComplexEvent(matchingEvent.getStreamEvent(0), startTimeEndTime);
    // Get all the aggregates within the given duration, from table corresponding to "per" duration
    StreamEvent withinMatchFromPersistedEvents = tableForPerDuration.find(matchingEvent, withinTableCompiledConditions.get(perValue));
    complexEventChunkToHoldWithinMatches.add(withinMatchFromPersistedEvents);
    // Optimization step.
    // Get the newest and oldest event timestamps from in-memory, and
    // check whether at least one of those timestamps fall out of the given time range. If that's the case,
    // there's no need to iterate through in-memory data.
    long oldestInMemoryEventTimestamp = getOldestInMemoryEventTimestamp(incrementalExecutorMap, incrementalDurations, perValue);
    long newestInMemoryEventTimestamp = getNewestInMemoryEventTimestamp(incrementalExecutorMap, incrementalDurations, perValue);
    if (requiresAggregatingInMemoryData(newestInMemoryEventTimestamp, oldestInMemoryEventTimestamp, startTimeEndTime)) {
        IncrementalDataAggregator incrementalDataAggregator = new IncrementalDataAggregator(incrementalDurations, perValue, baseExecutors, timestampExecutor, tableMetaStreamEvent, siddhiAppContext);
        // Aggregate in-memory data and create an event chunk out of it
        ComplexEventChunk<StreamEvent> 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);
    }
    // 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(complexEventChunkToHoldWithinMatches, outputExpressionExecutors);
    // Execute the on compile condition
    return ((Operator) onCompiledCondition).find(matchingEvent, aggregateSelectionComplexEventChunk, aggregateEventCloner);
}
Also used : Table(org.wso2.siddhi.core.table.Table) ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) TimePeriod(org.wso2.siddhi.query.api.aggregation.TimePeriod) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) IncrementalDataAggregator(org.wso2.siddhi.core.aggregation.IncrementalDataAggregator) SiddhiAppRuntimeException(org.wso2.siddhi.core.exception.SiddhiAppRuntimeException)

Example 13 with AggregationDefinition

use of org.wso2.siddhi.query.api.definition.AggregationDefinition in project siddhi by wso2.

the class SiddhiAppRuntimeBuilder method defineAggregation.

public void defineAggregation(AggregationDefinition aggregationDefinition) {
    AggregationRuntime aggregationRuntime = AggregationParser.parse(aggregationDefinition, siddhiAppContext, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, windowMap, aggregationMap, this);
    DefinitionParserHelper.validateDefinition(aggregationDefinition, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap);
    aggregationDefinitionMap.putIfAbsent(aggregationDefinition.getId(), aggregationDefinition);
    ProcessStreamReceiver processStreamReceiver = aggregationRuntime.getSingleStreamRuntime().getProcessStreamReceiver();
    streamJunctionMap.get(processStreamReceiver.getStreamId()).subscribe(processStreamReceiver);
    aggregationMap.putIfAbsent(aggregationDefinition.getId(), aggregationRuntime);
}
Also used : ProcessStreamReceiver(org.wso2.siddhi.core.query.input.ProcessStreamReceiver) AggregationRuntime(org.wso2.siddhi.core.aggregation.AggregationRuntime)

Aggregations

AggregationDefinition (org.wso2.siddhi.query.api.definition.AggregationDefinition)6 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)4 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)3 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)3 TimePeriod (org.wso2.siddhi.query.api.aggregation.TimePeriod)3 AbstractDefinition (org.wso2.siddhi.query.api.definition.AbstractDefinition)3 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)3 Expression (org.wso2.siddhi.query.api.expression.Expression)3 Variable (org.wso2.siddhi.query.api.expression.Variable)3 Test (org.testng.annotations.Test)2 AggregationRuntime (org.wso2.siddhi.core.aggregation.AggregationRuntime)2 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)2 IncrementalAttributeAggregator (org.wso2.siddhi.core.query.selector.attribute.aggregator.incremental.IncrementalAttributeAggregator)2 Table (org.wso2.siddhi.core.table.Table)2 Attribute (org.wso2.siddhi.query.api.definition.Attribute)2 TableDefinition (org.wso2.siddhi.query.api.definition.TableDefinition)2 WindowDefinition (org.wso2.siddhi.query.api.definition.WindowDefinition)2 DuplicateDefinitionException (org.wso2.siddhi.query.api.exception.DuplicateDefinitionException)2 OutputAttribute (org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute)2 SiddhiQLParser (org.wso2.siddhi.query.compiler.SiddhiQLParser)2