Search in sources :

Example 41 with Query

use of io.siddhi.query.api.execution.query.Query in project siddhi by wso2.

the class SimpleQueryTestCase method testQuery1.

@Test
public void testQuery1() {
    Query query = Query.query();
    query.from(InputStream.stream("StockStream").filter(Expression.and(Expression.compare(Expression.add(Expression.value(7), Expression.value(9.5)), Compare.Operator.GREATER_THAN, Expression.variable("price")), Expression.compare(Expression.value(100), Compare.Operator.GREATER_THAN_EQUAL, Expression.variable("volume")))).window("length", Expression.value(50)));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("avgPrice", Expression.function("avg", Expression.variable("price"))).groupBy(Expression.variable("symbol")).having(Expression.compare(Expression.variable("avgPrice"), Compare.Operator.GREATER_THAN_EQUAL, Expression.value(50))));
    query.insertInto("OutStockStream");
}
Also used : Query(io.siddhi.query.api.execution.query.Query) Test(org.testng.annotations.Test)

Example 42 with Query

use of io.siddhi.query.api.execution.query.Query in project siddhi by wso2.

the class SimpleQueryTestCase method testCreatingFilterQueryWithFaultStream2.

@Test
public void testCreatingFilterQueryWithFaultStream2() {
    Query query = Query.query();
    query.from(InputStream.faultStream("e1", "StockStream").filter(Expression.and(Expression.compare(Expression.add(Expression.value(7), Expression.value(9.5)), Compare.Operator.GREATER_THAN, Expression.variable("price").ofStream("e1")), Expression.compare(Expression.value(100), Compare.Operator.GREATER_THAN_EQUAL, Expression.variable("volume").ofStream("e1")))));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("avgPrice", Expression.function("avg", Expression.variable("symbol"))).groupBy(Expression.variable("symbol")).having(Expression.compare(Expression.variable("avgPrice"), Compare.Operator.GREATER_THAN_EQUAL, Expression.value(50))));
    query.insertIntoFault("OutStockStream");
    SiddhiApp.siddhiApp("test").addQuery(query);
}
Also used : Query(io.siddhi.query.api.execution.query.Query) Test(org.testng.annotations.Test)

Example 43 with Query

use of io.siddhi.query.api.execution.query.Query in project siddhi by wso2.

the class SimpleQueryTestCase method testQuery3.

@Test
public void testQuery3() {
    Query query = Query.query();
    query.from(InputStream.stream("StockStream").filter(Expression.and(Expression.compare(Expression.add(Expression.value(7), Expression.value(9.5)), Compare.Operator.GREATER_THAN, Expression.variable("price")), Expression.compare(Expression.value(100), Compare.Operator.GREATER_THAN_EQUAL, Expression.variable("volume")))).function("bar", Expression.value("price")).window("length", Expression.value(50)).function("foo", Expression.value(67), Expression.value(89)).filter(Expression.compare(Expression.value(10), Compare.Operator.LESS_THAN_EQUAL, Expression.variable("price"))));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("avgPrice", Expression.function("avg", Expression.variable("price"))).groupBy(Expression.variable("symbol")).having(Expression.compare(Expression.variable("avgPrice"), Compare.Operator.GREATER_THAN_EQUAL, Expression.value(50))));
    query.insertInto("OutStockStream");
}
Also used : Query(io.siddhi.query.api.execution.query.Query) Test(org.testng.annotations.Test)

Example 44 with Query

use of io.siddhi.query.api.execution.query.Query in project siddhi by wso2.

the class SiddhiCompiler method parseQuery.

public static Query parseQuery(String query) throws SiddhiParserException {
    CharStream input = CharStreams.fromString(query);
    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.query_final();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (Query) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) OnDemandQuery(io.siddhi.query.api.execution.query.OnDemandQuery) Query(io.siddhi.query.api.execution.query.Query) StoreQuery(io.siddhi.query.api.execution.query.StoreQuery) SiddhiQLBaseVisitorImpl(io.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 45 with Query

use of io.siddhi.query.api.execution.query.Query in project siddhi by wso2.

the class PartitionRuntimeImpl method addQuery.

public void addQuery(QueryRuntimeImpl metaQueryRuntime) {
    Query query = metaQueryRuntime.getQuery();
    if (query.getOutputStream() instanceof InsertIntoStream && metaQueryRuntime.getOutputCallback() instanceof InsertIntoStreamCallback) {
        InsertIntoStreamCallback insertIntoStreamCallback = (InsertIntoStreamCallback) metaQueryRuntime.getOutputCallback();
        StreamDefinition streamDefinition = insertIntoStreamCallback.getOutputStreamDefinition();
        String id = streamDefinition.getId();
        if (((InsertIntoStream) query.getOutputStream()).isInnerStream()) {
            metaQueryRuntime.setToLocalStream(true);
            localStreamDefinitionMap.putIfAbsent(id, streamDefinition);
            DefinitionParserHelper.validateOutputStream(streamDefinition, localStreamDefinitionMap.get(id));
            StreamJunction outputStreamJunction = localStreamJunctionMap.get(id);
            if (outputStreamJunction == null) {
                outputStreamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), null, siddhiAppContext);
                localStreamJunctionMap.putIfAbsent(id, outputStreamJunction);
            }
            insertIntoStreamCallback.init(localStreamJunctionMap.get(id));
        } else {
            streamDefinitionMap.putIfAbsent(id, streamDefinition);
            DefinitionParserHelper.validateOutputStream(streamDefinition, streamDefinitionMap.get(id));
            StreamJunction outputStreamJunction = streamJunctionMap.get(id);
            if (outputStreamJunction == null) {
                outputStreamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), null, siddhiAppContext);
                streamJunctionMap.putIfAbsent(id, outputStreamJunction);
            }
            insertIntoStreamCallback.init(streamJunctionMap.get(id));
        }
    } else if (query.getOutputStream() instanceof InsertIntoStream && metaQueryRuntime.getOutputCallback() instanceof InsertIntoWindowCallback) {
        InsertIntoWindowCallback insertIntoWindowCallback = (InsertIntoWindowCallback) metaQueryRuntime.getOutputCallback();
        StreamDefinition streamDefinition = insertIntoWindowCallback.getOutputStreamDefinition();
        String id = streamDefinition.getId();
        DefinitionParserHelper.validateOutputStream(streamDefinition, windowDefinitionMap.get(id));
        StreamJunction outputStreamJunction = streamJunctionMap.get(id);
        if (outputStreamJunction == null) {
            outputStreamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), null, siddhiAppContext);
            streamJunctionMap.putIfAbsent(id, outputStreamJunction);
        }
        insertIntoWindowCallback.getWindow().setPublisher(streamJunctionMap.get(insertIntoWindowCallback.getOutputStreamDefinition().getId()).constructPublisher());
    }
    if (metaQueryRuntime.isFromLocalStream()) {
        for (int i = 0; i < metaQueryRuntime.getStreamRuntime().getSingleStreamRuntimes().size(); i++) {
            String streamId = metaQueryRuntime.getStreamRuntime().getSingleStreamRuntimes().get(i).getProcessStreamReceiver().getStreamId();
            if (streamId.startsWith("#")) {
                StreamDefinition streamDefinition = (StreamDefinition) localStreamDefinitionMap.get(streamId);
                StreamJunction streamJunction = localStreamJunctionMap.get(streamId);
                if (streamJunction == null) {
                    streamJunction = new StreamJunction(streamDefinition, siddhiAppContext.getExecutorService(), siddhiAppContext.getBufferSize(), null, siddhiAppContext);
                    localStreamJunctionMap.put(streamId, streamJunction);
                }
                streamJunction.subscribe(metaQueryRuntime.getStreamRuntime().getSingleStreamRuntimes().get(i).getProcessStreamReceiver());
            }
        }
    }
    queryRuntimeList.add(metaQueryRuntime);
}
Also used : Query(io.siddhi.query.api.execution.query.Query) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) InsertIntoWindowCallback(io.siddhi.core.query.output.callback.InsertIntoWindowCallback) InsertIntoStream(io.siddhi.query.api.execution.query.output.stream.InsertIntoStream) StreamJunction(io.siddhi.core.stream.StreamJunction) InsertIntoStreamCallback(io.siddhi.core.query.output.callback.InsertIntoStreamCallback)

Aggregations

Query (io.siddhi.query.api.execution.query.Query)192 Test (org.testng.annotations.Test)181 SiddhiApp (io.siddhi.query.api.SiddhiApp)86 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)85 SiddhiManager (io.siddhi.core.SiddhiManager)85 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)85 InputHandler (io.siddhi.core.stream.input.InputHandler)82 Event (io.siddhi.core.event.Event)81 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)74 Partition (io.siddhi.query.api.execution.partition.Partition)14 StreamCallback (io.siddhi.core.stream.output.StreamCallback)8 ArrayList (java.util.ArrayList)7 QueryRuntimeImpl (io.siddhi.core.query.QueryRuntimeImpl)4 OnDemandQuery (io.siddhi.query.api.execution.query.OnDemandQuery)4 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)3 SingleStreamRuntime (io.siddhi.core.query.input.stream.single.SingleStreamRuntime)3 Window (io.siddhi.core.window.Window)3 AbstractDefinition (io.siddhi.query.api.definition.AbstractDefinition)3 ExecutionElement (io.siddhi.query.api.execution.ExecutionElement)3 JoinInputStream (io.siddhi.query.api.execution.query.input.stream.JoinInputStream)3