Search in sources :

Example 1 with StoreQuery

use of org.ballerinalang.siddhi.query.api.execution.query.StoreQuery in project ballerina by ballerina-lang.

the class SiddhiCompiler method parseStoreQuery.

public static StoreQuery parseStoreQuery(String storeQuery) throws SiddhiParserException {
    ANTLRInputStream input = new ANTLRInputStream(storeQuery);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.store_query_final();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (StoreQuery) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) StoreQuery(org.ballerinalang.siddhi.query.api.execution.query.StoreQuery) SiddhiQLBaseVisitorImpl(org.ballerinalang.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 2 with StoreQuery

use of org.ballerinalang.siddhi.query.api.execution.query.StoreQuery in project ballerina by ballerina-lang.

the class QueryStoreTestCase method test2.

@Test
public void test2() {
    StoreQuery query = SiddhiCompiler.parseStoreQuery("" + "from StockTable " + "select symbol, price " + "group by symbol " + "having (7 > price) ;");
    AssertJUnit.assertNotNull(query);
    StoreQuery api = StoreQuery.query().from(InputStore.store("StockTable")).select(Selector.selector().select("symbol", Expression.variable("symbol")).select(Expression.variable("price")).groupBy(Expression.variable("symbol")).having(Expression.compare(Expression.value(7), Compare.Operator.GREATER_THAN, Expression.variable("price"))));
    AssertJUnit.assertEquals(api, query);
}
Also used : StoreQuery(org.ballerinalang.siddhi.query.api.execution.query.StoreQuery) Test(org.testng.annotations.Test)

Example 3 with StoreQuery

use of org.ballerinalang.siddhi.query.api.execution.query.StoreQuery in project ballerina by ballerina-lang.

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.ballerinalang.siddhi.core.table.Table) ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) TimePeriod(org.ballerinalang.siddhi.query.api.aggregation.TimePeriod) StoreQuery(org.ballerinalang.siddhi.query.api.execution.query.StoreQuery) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList) Event(org.ballerinalang.siddhi.core.event.Event) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StoreQueryRuntime(org.ballerinalang.siddhi.core.query.StoreQueryRuntime)

Example 4 with StoreQuery

use of org.ballerinalang.siddhi.query.api.execution.query.StoreQuery in project ballerina by ballerina-lang.

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.ballerinalang.siddhi.query.api.execution.query.StoreQuery) BasicSelector(org.ballerinalang.siddhi.query.api.execution.query.selection.BasicSelector) Selector(org.ballerinalang.siddhi.query.api.execution.query.selection.Selector)

Example 5 with StoreQuery

use of org.ballerinalang.siddhi.query.api.execution.query.StoreQuery in project ballerina by ballerina-lang.

the class QueryStoreTestCase method test4.

@Test
public void test4() {
    StoreQuery query = SiddhiCompiler.parseStoreQuery("" + "from StockTable " + "on price > 40 " + "within '2017/01/*' " + "per 'day' " + "select symbol, price " + "group by symbol " + "having (7 > price) ;");
    AssertJUnit.assertNotNull(query);
    StoreQuery api = StoreQuery.query().from(InputStore.store("StockTable").on(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN, Expression.value(40)), Within.within(Expression.value("2017/01/*")), Expression.value("day"))).select(Selector.selector().select("symbol", Expression.variable("symbol")).select(Expression.variable("price")).groupBy(Expression.variable("symbol")).having(Expression.compare(Expression.value(7), Compare.Operator.GREATER_THAN, Expression.variable("price"))));
    AssertJUnit.assertEquals(api, query);
}
Also used : StoreQuery(org.ballerinalang.siddhi.query.api.execution.query.StoreQuery) Test(org.testng.annotations.Test)

Aggregations

StoreQuery (org.ballerinalang.siddhi.query.api.execution.query.StoreQuery)7 Test (org.testng.annotations.Test)4 ArrayList (java.util.ArrayList)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 ComplexEventChunk (org.ballerinalang.siddhi.core.event.ComplexEventChunk)1 Event (org.ballerinalang.siddhi.core.event.Event)1 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)1 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)1 StoreQueryRuntime (org.ballerinalang.siddhi.core.query.StoreQueryRuntime)1 Table (org.ballerinalang.siddhi.core.table.Table)1 TimePeriod (org.ballerinalang.siddhi.query.api.aggregation.TimePeriod)1 BasicSelector (org.ballerinalang.siddhi.query.api.execution.query.selection.BasicSelector)1 Selector (org.ballerinalang.siddhi.query.api.execution.query.selection.Selector)1 SiddhiQLBaseVisitorImpl (org.ballerinalang.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl)1