Search in sources :

Example 11 with StoreQuery

use of org.wso2.siddhi.query.api.execution.query.StoreQuery in project siddhi by wso2.

the class SiddhiAppRuntime method getStoreQueryOutputAttributes.

/**
 * This method get the storeQuery and return the corresponding output and its types.
 *
 * @param storeQuery       this storeQuery is processed and get the output attributes.
 * @param storeQueryString this passed to report errors with context if there are any.
 * @return List of output attributes
 */
private Attribute[] getStoreQueryOutputAttributes(StoreQuery storeQuery, String storeQueryString) {
    try {
        StoreQueryRuntime storeQueryRuntime = storeQueryRuntimeMap.get(storeQuery);
        if (storeQueryRuntime == null) {
            storeQueryRuntime = StoreQueryParser.parse(storeQuery, siddhiAppContext, tableMap, windowMap, aggregationMap);
            storeQueryRuntimeMap.put(storeQuery, storeQueryRuntime);
        }
        return storeQueryRuntime.getStoreQueryOutputAttributes();
    } catch (RuntimeException e) {
        if (e instanceof SiddhiAppContextException) {
            throw new StoreQueryCreationException(((SiddhiAppContextException) e).getMessageWithOutContext(), e, ((SiddhiAppContextException) e).getQueryContextStartIndex(), ((SiddhiAppContextException) e).getQueryContextEndIndex(), null, siddhiAppContext.getSiddhiAppString());
        }
        throw new StoreQueryCreationException(e.getMessage(), e);
    }
}
Also used : SiddhiAppContextException(org.wso2.siddhi.query.api.exception.SiddhiAppContextException) StoreQueryRuntime(org.wso2.siddhi.core.query.StoreQueryRuntime) StoreQueryCreationException(org.wso2.siddhi.core.exception.StoreQueryCreationException)

Example 12 with StoreQuery

use of org.wso2.siddhi.query.api.execution.query.StoreQuery in project siddhi by wso2.

the class SiddhiAppRuntime method query.

private Event[] query(StoreQuery storeQuery, String storeQueryString) {
    try {
        if (siddhiAppContext.isStatsEnabled() && storeQueryLatencyTracker != null) {
            storeQueryLatencyTracker.markIn();
        }
        StoreQueryRuntime storeQueryRuntime = storeQueryRuntimeMap.get(storeQuery);
        if (storeQueryRuntime == null) {
            storeQueryRuntime = StoreQueryParser.parse(storeQuery, siddhiAppContext, tableMap, windowMap, aggregationMap);
            storeQueryRuntimeMap.put(storeQuery, storeQueryRuntime);
        } else {
            storeQueryRuntime.reset();
        }
        return storeQueryRuntime.execute();
    } catch (RuntimeException e) {
        if (e instanceof SiddhiAppContextException) {
            throw new StoreQueryCreationException(((SiddhiAppContextException) e).getMessageWithOutContext(), e, ((SiddhiAppContextException) e).getQueryContextStartIndex(), ((SiddhiAppContextException) e).getQueryContextEndIndex(), null, storeQueryString);
        }
        throw new StoreQueryCreationException(e.getMessage(), e);
    } finally {
        if (siddhiAppContext.isStatsEnabled() && storeQueryLatencyTracker != null) {
            storeQueryLatencyTracker.markOut();
        }
    }
}
Also used : SiddhiAppContextException(org.wso2.siddhi.query.api.exception.SiddhiAppContextException) StoreQueryRuntime(org.wso2.siddhi.core.query.StoreQueryRuntime) StoreQueryCreationException(org.wso2.siddhi.core.exception.StoreQueryCreationException)

Example 13 with StoreQuery

use of org.wso2.siddhi.query.api.execution.query.StoreQuery in project siddhi by wso2.

the class RecreateInMemoryData method recreateInMemoryData.

public void recreateInMemoryData() {
    if (incrementalExecutorMap.get(incrementalDurations.get(0)).getNextEmitTime() != -1) {
        // created. Hence this method does not have to be executed.
        return;
    }
    Event[] events;
    Long latestEventTimestamp = null;
    // Get all events from table corresponding to max duration
    Table tableForMaxDuration = aggregationTables.get(incrementalDurations.get(incrementalDurations.size() - 1));
    StoreQuery storeQuery = StoreQuery.query().from(InputStore.store(tableForMaxDuration.getTableDefinition().getId())).select(Selector.selector().orderBy(Expression.variable("AGG_TIMESTAMP")));
    StoreQueryRuntime storeQueryRuntime = StoreQueryParser.parse(storeQuery, siddhiAppContext, tableMap, windowMap, aggregationMap);
    // Get latest event timestamp in tableForMaxDuration
    events = storeQueryRuntime.execute();
    if (events != null) {
        latestEventTimestamp = (Long) events[events.length - 1].getData(0);
    }
    for (int i = incrementalDurations.size() - 1; i > 0; i--) {
        TimePeriod.Duration recreateForDuration = incrementalDurations.get(i);
        IncrementalExecutor incrementalExecutor = incrementalExecutorMap.get(recreateForDuration);
        // Get the table previous to the duration for which we need to recreate (e.g. if we want to recreate
        // for minute duration, take the second table [provided that aggregation is done for seconds])
        Table recreateFromTable = aggregationTables.get(incrementalDurations.get(i - 1));
        storeQuery = StoreQuery.query().from(InputStore.store(recreateFromTable.getTableDefinition().getId())).select(Selector.selector().orderBy(Expression.variable("AGG_TIMESTAMP")));
        storeQueryRuntime = StoreQueryParser.parse(storeQuery, siddhiAppContext, tableMap, windowMap, aggregationMap);
        events = storeQueryRuntime.execute();
        if (events != null) {
            long referenceToNextLatestEvent = (Long) events[events.length - 1].getData(0);
            String timeZoneOfNextLatestEvent = events[events.length - 1].getData(1).toString();
            if (latestEventTimestamp != null) {
                List<Event> eventsNewerThanLatestEventOfRecreateForDuration = new ArrayList<>();
                for (Event event : events) {
                    // After getting the events from recreateFromTable, get the time bucket it belongs to,
                    // if each of these events were in the recreateForDuration. This helps to identify the events,
                    // which must be processed in-memory for recreateForDuration.
                    long timeBucketForNextDuration = IncrementalTimeConverterUtil.getStartTimeOfAggregates((Long) event.getData(0), recreateForDuration, event.getData(1).toString());
                    if (timeBucketForNextDuration > latestEventTimestamp) {
                        eventsNewerThanLatestEventOfRecreateForDuration.add(event);
                    }
                }
                events = eventsNewerThanLatestEventOfRecreateForDuration.toArray(new Event[eventsNewerThanLatestEventOfRecreateForDuration.size()]);
            }
            latestEventTimestamp = referenceToNextLatestEvent;
            ComplexEventChunk<StreamEvent> complexEventChunk = new ComplexEventChunk<>(false);
            for (Event event : events) {
                StreamEvent streamEvent = streamEventPool.borrowEvent();
                streamEvent.setOutputData(event.getData());
                complexEventChunk.add(streamEvent);
            }
            incrementalExecutor.execute(complexEventChunk);
            if (i == 1) {
                TimePeriod.Duration rootDuration = incrementalDurations.get(0);
                IncrementalExecutor rootIncrementalExecutor = incrementalExecutorMap.get(rootDuration);
                long emitTimeOfLatestEventInTable = IncrementalTimeConverterUtil.getNextEmitTime(latestEventTimestamp, rootDuration, timeZoneOfNextLatestEvent);
                rootIncrementalExecutor.setValuesForInMemoryRecreateFromTable(true, emitTimeOfLatestEventInTable);
            }
        }
    }
}
Also used : Table(org.wso2.siddhi.core.table.Table) ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) TimePeriod(org.wso2.siddhi.query.api.aggregation.TimePeriod) StoreQuery(org.wso2.siddhi.query.api.execution.query.StoreQuery) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) Event(org.wso2.siddhi.core.event.Event) StoreQueryRuntime(org.wso2.siddhi.core.query.StoreQueryRuntime)

Example 14 with StoreQuery

use of org.wso2.siddhi.query.api.execution.query.StoreQuery in project siddhi by wso2.

the class StoreQueryParser method constructStoreQueryRuntime.

private static StoreQueryRuntime constructStoreQueryRuntime(Table table, StoreQuery storeQuery, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, String queryName, int metaPosition, Expression onCondition, MetaStreamEvent metaStreamEvent, List<VariableExpressionExecutor> variableExpressionExecutors) {
    metaStreamEvent.setEventType(EventType.TABLE);
    initMetaStreamEvent(metaStreamEvent, table.getTableDefinition());
    MatchingMetaInfoHolder metaStreamInfoHolder = generateMatchingMetaInfoHolder(metaStreamEvent, table.getTableDefinition());
    CompiledCondition compiledCondition = table.compileCondition(onCondition, metaStreamInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
    if (table instanceof QueryableProcessor) {
        List<Attribute> expectedOutputAttributes = buildExpectedOutputAttributes(storeQuery, siddhiAppContext, tableMap, queryName, metaPosition, metaStreamInfoHolder);
        CompiledSelection compiledSelection = ((QueryableProcessor) table).compileSelection(storeQuery.getSelector(), expectedOutputAttributes, metaStreamInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
        SelectStoreQueryRuntime storeQueryRuntime = new SelectStoreQueryRuntime((QueryableProcessor) table, compiledCondition, compiledSelection, expectedOutputAttributes, queryName);
        QueryParserHelper.reduceMetaComplexEvent(metaStreamInfoHolder.getMetaStateEvent());
        QueryParserHelper.updateVariablePosition(metaStreamInfoHolder.getMetaStateEvent(), variableExpressionExecutors);
        return storeQueryRuntime;
    } else {
        FindStoreQueryRuntime storeQueryRuntime = new FindStoreQueryRuntime(table, compiledCondition, queryName, metaStreamEvent);
        populateFindStoreQueryRuntime(storeQueryRuntime, metaStreamInfoHolder, storeQuery.getSelector(), variableExpressionExecutors, siddhiAppContext, tableMap, queryName, metaPosition);
        return storeQueryRuntime;
    }
}
Also used : CompiledCondition(org.wso2.siddhi.core.util.collection.operator.CompiledCondition) SelectStoreQueryRuntime(org.wso2.siddhi.core.query.SelectStoreQueryRuntime) Attribute(org.wso2.siddhi.query.api.definition.Attribute) MatchingMetaInfoHolder(org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) CompiledSelection(org.wso2.siddhi.core.util.collection.operator.CompiledSelection) FindStoreQueryRuntime(org.wso2.siddhi.core.query.FindStoreQueryRuntime) QueryableProcessor(org.wso2.siddhi.core.query.processor.stream.window.QueryableProcessor)

Example 15 with StoreQuery

use of org.wso2.siddhi.query.api.execution.query.StoreQuery in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitStore_query.

@Override
public Object visitStore_query(SiddhiQLParser.Store_queryContext ctx) {
    StoreQuery storeQuery = StoreQuery.query().from((InputStore) visit(ctx.store_input()));
    if (ctx.query_section() != null) {
        storeQuery = storeQuery.select((Selector) visit(ctx.query_section()));
    }
    populateQueryContext(storeQuery, ctx);
    return storeQuery;
}
Also used : StoreQuery(org.wso2.siddhi.query.api.execution.query.StoreQuery) BasicSelector(org.wso2.siddhi.query.api.execution.query.selection.BasicSelector) Selector(org.wso2.siddhi.query.api.execution.query.selection.Selector)

Aggregations

Test (org.testng.annotations.Test)8 StoreQuery (org.wso2.siddhi.query.api.execution.query.StoreQuery)7 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)4 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)4 Event (org.wso2.siddhi.core.event.Event)3 StoreQueryCreationException (org.wso2.siddhi.core.exception.StoreQueryCreationException)3 FindStoreQueryRuntime (org.wso2.siddhi.core.query.FindStoreQueryRuntime)3 StoreQueryRuntime (org.wso2.siddhi.core.query.StoreQueryRuntime)3 CompiledCondition (org.wso2.siddhi.core.util.collection.operator.CompiledCondition)3 MatchingMetaInfoHolder (org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)3 Attribute (org.wso2.siddhi.query.api.definition.Attribute)3 ArrayList (java.util.ArrayList)2 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)2 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)2 Table (org.wso2.siddhi.core.table.Table)2 SiddhiAppContextException (org.wso2.siddhi.query.api.exception.SiddhiAppContextException)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 AggregationRuntime (org.wso2.siddhi.core.aggregation.AggregationRuntime)1