Search in sources :

Example 6 with StoreQuery

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

the class QueryStoreTestCase method test3.

@Test
public void test3() {
    StoreQuery query = SiddhiCompiler.parseStoreQuery("" + "from StockTable " + "on price > 40 " + "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)))).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.wso2.siddhi.query.api.execution.query.StoreQuery) Test(org.testng.annotations.Test)

Example 7 with StoreQuery

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

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.wso2.siddhi.query.api.execution.query.StoreQuery) SiddhiQLBaseVisitorImpl(org.wso2.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 8 with StoreQuery

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

the class StoreQueryTableTestCase method test10.

@Test
public void test10() throws InterruptedException {
    log.info("Test10 table");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long);" + "@PrimaryKey('symbol') " + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 75.6f, 100L });
    stockStream.send(new Object[] { "WSO2", 57.6f, 100L });
    Thread.sleep(500);
    String storeQuery = "" + "from StockTable " + "on volume > 10 " + "select symbol, price, sum(volume) as totalVolume ";
    Event[] events = siddhiAppRuntime.query(storeQuery);
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    AssertJUnit.assertEquals(200L, events[0].getData()[2]);
    events = siddhiAppRuntime.query(storeQuery);
    EventPrinter.print(events);
    AssertJUnit.assertEquals(1, events.length);
    AssertJUnit.assertEquals(200L, events[0].getData()[2]);
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 9 with StoreQuery

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

the class StoreQueryTableTestCase method test12.

@Test
public void test12() throws InterruptedException {
    log.info("Test12 - Test output attributes and its types for table");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long);" + "@PrimaryKey('symbol') " + "define table StockTable (symbol string, price float, volume long); ";
    String storeQuery = "" + "from StockTable " + "select * ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams);
    siddhiAppRuntime.start();
    Attribute[] actualAttributeArray = siddhiAppRuntime.getStoreQueryOutputAttributes(SiddhiCompiler.parseStoreQuery(storeQuery));
    Attribute symbolAttribute = new Attribute("symbol", Attribute.Type.STRING);
    Attribute priceAttribute = new Attribute("price", Attribute.Type.FLOAT);
    Attribute volumeAttribute = new Attribute("volume", Attribute.Type.LONG);
    Attribute[] expectedAttributeArray = new Attribute[] { symbolAttribute, priceAttribute, volumeAttribute };
    AssertJUnit.assertArrayEquals(expectedAttributeArray, actualAttributeArray);
    storeQuery = "" + "from StockTable " + "select symbol, sum(volume) as totalVolume ;";
    actualAttributeArray = siddhiAppRuntime.getStoreQueryOutputAttributes(SiddhiCompiler.parseStoreQuery(storeQuery));
    Attribute totalVolumeAttribute = new Attribute("totalVolume", Attribute.Type.LONG);
    expectedAttributeArray = new Attribute[] { symbolAttribute, totalVolumeAttribute };
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertArrayEquals(expectedAttributeArray, actualAttributeArray);
}
Also used : Attribute(org.wso2.siddhi.query.api.definition.Attribute) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 10 with StoreQuery

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

the class StoreQueryTableTestCase method test11.

@Test
public void test11() throws InterruptedException {
    log.info("Test10 table");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long);" + "@PrimaryKey('symbol') " + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
    stockStream.send(new Object[] { "IBM", 75.6f, 100L });
    stockStream.send(new Object[] { "WSO2", 57.6f, 100L });
    Thread.sleep(500);
    String storeQuery = "" + "from StockTable " + "on volume > 10 " + "select symbol, price, sum(volume) as totalVolume " + "group by symbol ";
    Event[] events = siddhiAppRuntime.query(storeQuery);
    EventPrinter.print(events);
    AssertJUnit.assertEquals(2, events.length);
    AssertJUnit.assertEquals(100L, events[0].getData()[2]);
    AssertJUnit.assertEquals(100L, events[1].getData()[2]);
    events = siddhiAppRuntime.query(storeQuery);
    EventPrinter.print(events);
    AssertJUnit.assertEquals(2, events.length);
    AssertJUnit.assertEquals(100L, events[0].getData()[2]);
    AssertJUnit.assertEquals(100L, events[1].getData()[2]);
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

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