io.siddhi.query.api.execution.query.output.stream.OutputStream Java Examples

The following examples show how to use io.siddhi.query.api.execution.query.output.stream.OutputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TableQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreatingDeleteByTypeQuery() {
    Query query = Query.query();
    query.from(
            InputStream.stream("cseEventStream").
                    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("lengthBatch", Expression.value(50))
    );
    query.deleteBy("StockQuote", OutputStream.OutputEventType.ALL_EVENTS,
            Expression.compare(
                    Expression.variable("symbol"),
                    Compare.Operator.EQUAL,
                    Expression.variable("symbol").ofStream("StockQuote"))
    );

}
 
Example #2
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreatingNestedFilterQuery2() {
    Query query = Query.query();
    query.from(InputStream.stream(
            Query.query().
                    from(InputStream.stream("StockStream").
                            filter(
                                    Expression.compare(
                                            Expression.variable("price").ofStream("StockStream"),
                                            Compare.Operator.GREATER_THAN_EQUAL,
                                            Expression.value(20))
                            ).filter(Expression.isNull(Expression.variable("price").ofStream("StockStream")))).
                    select(
                            Selector.selector().
                                    select("symbol", Expression.variable("symbol")).
                                    select("avgPrice", Expression.function("avg", Expression.variable("price")))
                    ).
                    returns(OutputStream.OutputEventType.CURRENT_EVENTS))
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("avgPrice", Expression.variable("avgPrice"))
    );
    query.insertInto("IBMOutStockStream");
}
 
Example #3
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Test
public void test1() throws SiddhiParserException {
    Query query = SiddhiCompiler.parseQuery("from  StockStream[price>3]#window.length(50) " +
            "select symbol, avg(price) as avgPrice " +
            "group by symbol " +
            "having (price >= 20) " +
            "insert all events into StockQuote; "
    );
    AssertJUnit.assertNotNull(query);

    Query api = Query.query().from(InputStream.stream("StockStream").
            filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN, Expression
                    .value(3))).
            window("length", Expression.value(50))).
            select(Selector.selector().select(Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(
                            Expression.variable("price"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(20)))).
            insertInto("StockQuote", OutputStream.OutputEventType.ALL_EVENTS);
    AssertJUnit.assertEquals(api, query);

}
 
Example #4
Source File: OutputParser.java    From siddhi with Apache License 2.0 6 votes vote down vote up
public static OutputCallback constructOutputCallback(OutputStream outStream, String key,
                                                     ConcurrentMap<String, StreamJunction> streamJunctionMap,
                                                     StreamDefinition outputStreamDefinition,
                                                     SiddhiQueryContext siddhiQueryContext) {
    String id = outStream.getId();
    //Construct CallBack
    if (outStream instanceof InsertIntoStream) {
        StreamJunction outputStreamJunction = streamJunctionMap.get(id + key);
        if (outputStreamJunction == null) {
            outputStreamJunction = new StreamJunction(outputStreamDefinition,
                    siddhiQueryContext.getSiddhiAppContext().getExecutorService(),
                    siddhiQueryContext.getSiddhiAppContext().getBufferSize(), null,
                    siddhiQueryContext.getSiddhiAppContext());
            streamJunctionMap.putIfAbsent(id + key, outputStreamJunction);
        }
        InsertIntoStreamCallback insertIntoStreamCallback = new InsertIntoStreamCallback(outputStreamDefinition,
                siddhiQueryContext.getName());
        insertIntoStreamCallback.init(streamJunctionMap.get(id + key));
        return insertIntoStreamCallback;

    } else {
        throw new SiddhiAppCreationException(outStream.getClass().getName() + " not supported",
                outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
    }
}
 
Example #5
Source File: OnDemandQueryParser.java    From siddhi with Apache License 2.0 6 votes vote down vote up
private static OnDemandQueryRuntime getOnDemandQueryRuntime(OnDemandQuery onDemandQuery,
                                                            Map<String, Table> tableMap,
                                                            Map<String, Window> windowMap,
                                                            int metaPosition, LockWrapper lockWrapper,
                                                            MetaStreamEvent metaStreamEvent,
                                                            OutputStream outputStream, Expression onCondition,
                                                            SiddhiQueryContext siddhiQueryContext) {
    try {
        List<VariableExpressionExecutor> variableExpressionExecutors = new ArrayList<>();
        Table table = tableMap.get(outputStream.getId());

        if (table != null) {
            return constructOnDemandQueryRuntime(table, onDemandQuery, tableMap, windowMap,
                    metaPosition, onCondition, metaStreamEvent,
                    variableExpressionExecutors, lockWrapper, siddhiQueryContext);
        } else {
            throw new OnDemandQueryCreationException(outputStream.getId() + " is not a table.");
        }

    } finally {
        SnapshotService.getSkipStateStorageThreadLocal().set(null);
    }
}
 
Example #6
Source File: DefineAggregationTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testAggregationJoin1() {
    Query.query().
            from(
                    InputStream.joinStream(
                            InputStream.stream("s1", "cseEventStream").
                                    filter(Expression.and(
                                            Expression.compare(
                                                    Expression.add(Expression.value(7), Expression.value(9.5)),
                                                    Compare.Operator.GREATER_THAN,
                                                    Expression.variable("price").ofStream("cseEventStream")),
                                            Expression.compare(Expression.value(100),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.variable("volume").ofStream("cseEventStream")
                                            )
                                            )
                                    ).window("lengthBatch", Expression.value(50)),
                            JoinInputStream.Type.JOIN,
                            InputStore.store("s2", "StockAggregation"),
                            Within.within(Expression.value("2014-02-15T00:00:00Z"),
                                    Expression.value("2014-03-16T00:00:00Z")),
                            Expression.value("day")
                    )
            ).
            select(
                    Selector.selector().
                            select("symbol", Expression.variable("symbol").ofStream("cseEventStream")).
                            select(null, Expression.variable("symbol").ofStream("cseEventStream")).
                            groupBy(Expression.variable("symbol").ofStream("cseEventStream")).
                            having(
                                    Expression.compare(
                                            Expression.add(Expression.value(7), Expression.value(9.5)),
                                            Compare.Operator.GREATER_THAN,
                                            Expression.variable("price"))
                            )
            ).
            insertInto("StockQuote", OutputStream.OutputEventType.EXPIRED_EVENTS);
}
 
Example #7
Source File: TableQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatingUpdateByTypeQuery() {
    Query query = Query.query();
    query.from(
            InputStream.stream("cseEventStream").
                    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("lengthBatch", Expression.value(50))
    );
    query.select(
            Selector.selector().
                    select("symbol", Expression.variable("symbol")).
                    select("price", Expression.variable("price")).
                    select("volume", Expression.variable("volume"))
    );
    query.updateBy("StockQuote", OutputStream.OutputEventType.ALL_EVENTS,
            UpdateStream.updateSet().
                    set(
                            Expression.variable("price").ofStream("StockQuote"),
                            Expression.variable("price")).
                    set(
                            Expression.variable("volume").ofStream("StockQuote"),
                            Expression.variable("volume")),
            Expression.compare(
                    Expression.variable("symbol"),
                    Compare.Operator.EQUAL,
                    Expression.variable("symbol").ofStream("StockQuote")));

}
 
Example #8
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatingFilterQueryWithFaultStream3() {
    Query query = Query.query();
    query.from(
            InputStream.faultStream("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")
                                    )
                            )
                    )
    );
    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", OutputStream.OutputEventType.CURRENT_EVENTS);
    SiddhiApp.siddhiApp("test").addQuery(query);
}
 
Example #9
Source File: DefineAggregationTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testAggregationJoin2() {
    Query.query().
            from(
                    InputStream.joinStream(
                            InputStream.stream("s1", "cseEventStream").
                                    filter(Expression.and(
                                            Expression.compare(
                                                    Expression.add(Expression.value(7), Expression.value(9.5)),
                                                    Compare.Operator.GREATER_THAN,
                                                    Expression.variable("price").ofStream("cseEventStream")),
                                            Expression.compare(Expression.value(100),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.variable("volume").ofStream("cseEventStream")
                                            )
                                            )
                                    ).window("lengthBatch", Expression.value(50)),
                            JoinInputStream.Type.JOIN,
                            InputStore.store("s2", "StockAggregation"),
                            JoinInputStream.EventTrigger.LEFT,
                            Within.within(Expression.value("2014-02-15T00:00:00Z"),
                                    Expression.value("2014-03-16T00:00:00Z")),
                            Expression.value("day")
                    )
            ).
            select(
                    Selector.selector().
                            select("symbol", Expression.variable("symbol").ofStream("cseEventStream")).
                            select(null, Expression.variable("symbol").ofStream("cseEventStream")).
                            groupBy(Expression.variable("symbol").ofStream("cseEventStream")).
                            having(
                                    Expression.compare(
                                            Expression.add(Expression.value(7), Expression.value(9.5)),
                                            Compare.Operator.GREATER_THAN,
                                            Expression.variable("price"))
                            )
            ).
            insertInto("StockQuote", OutputStream.OutputEventType.EXPIRED_EVENTS);
}
 
Example #10
Source File: OnDemandQuery.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
 * Builder method to set an outPutStream to the on-demand query
 *
 * @param outputStream outPutStream for the on-demand query
 * @return updated on-demand query
 */
public OnDemandQuery outStream(OutputStream outputStream) {
    this.outputStream = outputStream;
    if (outputStream != null && outputStream.getOutputEventType() == null) {
        outputStream.setOutputEventType(OutputStream.OutputEventType.CURRENT_EVENTS);
    }
    return this;
}
 
Example #11
Source File: Query.java    From siddhi with Apache License 2.0 5 votes vote down vote up
private void updateOutputEventType(OutputRate outputRate, OutputStream outputStream) {
    if (outputStream != null && outputStream.getOutputEventType() == null) {
        if (outputRate instanceof SnapshotOutputRate) {
            outputStream.setOutputEventType(OutputStream.OutputEventType.ALL_EVENTS);
        } else {
            outputStream.setOutputEventType(OutputStream.OutputEventType.CURRENT_EVENTS);
        }
    }
}
 
Example #12
Source File: Query.java    From siddhi with Apache License 2.0 5 votes vote down vote up
public void updateOrInsertBy(String outputTableId, OutputStream.OutputEventType outputEventType,
                             UpdateSet updateSetAttributes, Expression onUpdateExpression) {
    this.outputStream = new UpdateOrInsertStream(outputTableId, outputEventType, updateSetAttributes,
            onUpdateExpression);
    updateOutputEventType(outputRate, outputStream);

}
 
Example #13
Source File: Window.java    From siddhi with Apache License 2.0 5 votes vote down vote up
StreamPublishProcessor(OutputStream.OutputEventType outputEventType) {
    this.outputEventType = outputEventType;
    this.allowCurrentEvents = (outputEventType == OutputStream.OutputEventType.CURRENT_EVENTS ||
            outputEventType == OutputStream.OutputEventType.ALL_EVENTS);
    this.allowExpiredEvents = (outputEventType == OutputStream.OutputEventType.EXPIRED_EVENTS ||
            outputEventType == OutputStream.OutputEventType.ALL_EVENTS);
}
 
Example #14
Source File: OnDemandQueryParser.java    From siddhi with Apache License 2.0 5 votes vote down vote up
public static List<Attribute> buildExpectedOutputAttributes(
        OnDemandQuery onDemandQuery, Map<String, Table> tableMap,
        int metaPosition, MatchingMetaInfoHolder metaStreamInfoHolder, SiddhiQueryContext siddhiQueryContext) {
    MetaStateEvent selectMetaStateEvent =
            new MetaStateEvent(metaStreamInfoHolder.getMetaStateEvent().getMetaStreamEvents());
    SelectorParser.parse(onDemandQuery.getSelector(),
            new ReturnStream(OutputStream.OutputEventType.CURRENT_EVENTS),
            selectMetaStateEvent, tableMap, new ArrayList<>(), metaPosition, ProcessingMode.BATCH,
            false, siddhiQueryContext);
    return selectMetaStateEvent.getOutputStreamDefinition().getAttributeList();
}
 
Example #15
Source File: OnDemandQueryParser.java    From siddhi with Apache License 2.0 5 votes vote down vote up
private static void populateFindOnDemandQueryRuntime(FindOnDemandQueryRuntime findOnDemandQueryRuntime,
                                                     MatchingMetaInfoHolder metaStreamInfoHolder, Selector selector,
                                                     List<VariableExpressionExecutor> variableExpressionExecutors,
                                                     Map<String, Table> tableMap, Map<String, Window> windowMap,
                                                     int metaPosition, boolean groupBy, LockWrapper lockWrapper,
                                                     SiddhiQueryContext siddhiQueryContext) {
    ReturnStream returnStream = new ReturnStream(OutputStream.OutputEventType.CURRENT_EVENTS);
    QuerySelector querySelector = SelectorParser.parse(selector, returnStream,
            metaStreamInfoHolder.getMetaStateEvent(), tableMap, variableExpressionExecutors,
            metaPosition, ProcessingMode.BATCH, false, siddhiQueryContext);
    PassThroughOutputRateLimiter rateLimiter = new PassThroughOutputRateLimiter(siddhiQueryContext.getName());
    rateLimiter.init(lockWrapper, groupBy, siddhiQueryContext);
    OutputCallback outputCallback = OutputParser.constructOutputCallback(returnStream,
            metaStreamInfoHolder.getMetaStateEvent().getOutputStreamDefinition(), tableMap, windowMap,
            true, siddhiQueryContext);
    rateLimiter.setOutputCallback(outputCallback);
    querySelector.setNextProcessor(rateLimiter);

    QueryParserHelper.reduceMetaComplexEvent(metaStreamInfoHolder.getMetaStateEvent());
    QueryParserHelper.updateVariablePosition(metaStreamInfoHolder.getMetaStateEvent(), variableExpressionExecutors);
    querySelector.setEventPopulator(
            StateEventPopulatorFactory.constructEventPopulator(metaStreamInfoHolder.getMetaStateEvent()));
    findOnDemandQueryRuntime.setStateEventFactory(new StateEventFactory(metaStreamInfoHolder.getMetaStateEvent()));
    findOnDemandQueryRuntime.setSelector(querySelector);
    findOnDemandQueryRuntime.setOutputAttributes(metaStreamInfoHolder.getMetaStateEvent().
            getOutputStreamDefinition().getAttributeList());
}
 
Example #16
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
     * {@inheritDoc}
     * <p>The default implementation returns the result of calling
     * {@link #visitChildren} on {@code ctx}.</p>
     *
     * @param ctx
     */
    @Override
    public Query visitQuery(@NotNull SiddhiQLParser.QueryContext ctx) {

//        query
//        : annotation* query_input query_section? output_rate? (query_output | query_publish)
//        ;

        try {
            Query query = Query.query().from((InputStream) visit(ctx.query_input()));

            if (ctx.query_section() != null) {
                query.select((Selector) visit(ctx.query_section()));
            }
            if (ctx.output_rate() != null) {
                query.output((OutputRate) visit(ctx.output_rate()));
            }
            for (SiddhiQLParser.AnnotationContext annotationContext : ctx.annotation()) {
                query.annotation((Annotation) visit(annotationContext));
            }
            if (ctx.query_output() != null) {
                query.outStream((OutputStream) visit(ctx.query_output()));
            }
            populateQueryContext(query, ctx);
            return query;
        } finally {
            activeStreams.clear();
        }
    }
 
Example #17
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test(expectedExceptions = UnsupportedAttributeTypeException.class)
public void test19() throws SiddhiParserException {
    Query query = SiddhiCompiler.parseQuery("from  StockStream[price>3]#window.length(50) " +
            "select symbol, avg(price) as avgPrice " +
            "group by symbol " +
            "having (price >= 20)" +
            "order by avgPrice desc " +
            "limit 5 " +
            "offset 3 " +
            "insert all events into StockQuote; "
    );
    AssertJUnit.assertNotNull(query);

    Query api = Query.query().from(InputStream.stream("StockStream").
            filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN, Expression
                    .value(3))).
            window("length", Expression.value(50))).
            select(Selector.selector().select(Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(
                            Expression.variable("price"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(20))).
                    orderBy(Expression.variable("avgPrice"), OrderByAttribute.Order.DESC).
                    limit(Expression.value(5)).
                    offset(Expression.value(3.8))).
            insertInto("StockQuote", OutputStream.OutputEventType.ALL_EVENTS);
}
 
Example #18
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public AnonymousInputStream visitAnonymous_stream(@NotNull SiddhiQLParser.Anonymous_streamContext ctx) {

    if (ctx.anonymous_stream() != null) {
        return (AnonymousInputStream) visit(ctx.anonymous_stream());
    }

    Set<String> activeStreamsBackup = activeStreams;

    try {
        activeStreams = new HashSet<String>();

        Query query = Query.query().from((InputStream) visit(ctx.query_input()));

        if (ctx.query_section() != null) {
            query.select((Selector) visit(ctx.query_section()));
        }
        if (ctx.output_rate() != null) {
            query.output((OutputRate) visit(ctx.output_rate()));
        }

        if (ctx.output_event_type() != null) {
            query.outStream(new ReturnStream((OutputStream.OutputEventType) visit(ctx.output_event_type())));
        } else {
            query.outStream(new ReturnStream());
        }

        AnonymousInputStream anonymousInputStream = new AnonymousInputStream(query);
        populateQueryContext(anonymousInputStream, ctx);
        return anonymousInputStream;

    } finally {
        activeStreams.clear();
        activeStreams = activeStreamsBackup;
    }
}
 
Example #19
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
     * {@inheritDoc}
     * <p>The default implementation returns the result of calling
     * {@link #visitChildren} on {@code ctx}.</p>
     *
     * @param ctx
     */
    @Override
    public OutputStream.OutputEventType visitOutput_event_type(@NotNull SiddhiQLParser.Output_event_typeContext ctx) {
//        output_event_type
//        : ALL EVENTS | EXPIRED EVENTS | CURRENT? EVENTS
//        ;

        if (ctx.ALL() != null) {
            return OutputStream.OutputEventType.ALL_EVENTS;
        } else if (ctx.EXPIRED() != null) {
            return OutputStream.OutputEventType.EXPIRED_EVENTS;
        } else {
            return OutputStream.OutputEventType.CURRENT_EVENTS;
        }
    }
 
Example #20
Source File: TestStoreForCachePreLoading.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
protected CompiledSelection compileSelection(List<SelectAttributeBuilder> selectAttributeBuilders,
                                             List<ExpressionBuilder> groupByExpressionBuilder,
                                             ExpressionBuilder havingExpressionBuilder,
                                             List<OrderByAttributeBuilder> orderByAttributeBuilders, Long limit,
                                             Long offset) {
    CompiledSelectionWithCache compiledSelectionWithCache;
    MetaStateEvent metaStateEvent = matchingMetaInfoHolderForTestOnDemandQuery.getMetaStateEvent().clone();

    ReturnStream returnStream = new ReturnStream(OutputStream.OutputEventType.CURRENT_EVENTS);
    int metaPosition = SiddhiConstants.UNKNOWN_STATE;
    List<VariableExpressionExecutor> variableExpressionExecutorsForQuerySelector = new ArrayList<>();

    if (metaStateEvent.getOutputDataAttributes().size() == 0) {
        for (Attribute outputAttribute : metaStateEvent.getMetaStreamEvents()[0].getOnAfterWindowData()) {
            metaStateEvent.getMetaStreamEvents()[0].addOutputData(outputAttribute);
        }
    }

    QuerySelector querySelector = SelectorParser.parse(selectorForTestOnDemandQuery,
            returnStream,
            metaStateEvent, tableMap,
            variableExpressionExecutorsForQuerySelector, metaPosition, ProcessingMode.BATCH,
            false, siddhiQueryContextForTestOnDemandQuery);
    QueryParserHelper.updateVariablePosition(metaStateEvent,
            variableExpressionExecutorsForQuerySelector);
    querySelector.setEventPopulator(
            StateEventPopulatorFactory.constructEventPopulator(metaStateEvent));

    compiledSelectionWithCache = new CompiledSelectionWithCache(null, querySelector, metaStateEvent, 0, null);
    return compiledSelectionWithCache;
}
 
Example #21
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
public Object visitDefinition_window(@NotNull SiddhiQLParser.Definition_windowContext ctx) {
    Source source = (Source) visit(ctx.source());
    if (source.isInnerStream) {
        throw newSiddhiParserException(ctx, " '#' cannot be used, because Windows can't be defined " +
                "as InnerStream!");
    }
    if (source.isFaultStream) {
        throw newSiddhiParserException(ctx, " '!' cannot be used, because Windows can't be defined " +
                "as FaultStream!");
    }
    WindowDefinition windowDefinition = WindowDefinition.id(source.streamId);
    List<SiddhiQLParser.Attribute_nameContext> attribute_names = ctx.attribute_name();
    List<SiddhiQLParser.Attribute_typeContext> attribute_types = ctx.attribute_type();
    for (int i = 0; i < attribute_names.size(); i++) {
        SiddhiQLParser.Attribute_nameContext attributeNameContext = attribute_names.get(i);
        SiddhiQLParser.Attribute_typeContext attributeTypeContext = attribute_types.get(i);
        windowDefinition.attribute((String) visit(attributeNameContext), (Attribute.Type) visit
                (attributeTypeContext));

    }
    for (SiddhiQLParser.AnnotationContext annotationContext : ctx.annotation()) {
        windowDefinition.annotation((Annotation) visit(annotationContext));
    }
    AttributeFunction attributeFunction = (AttributeFunction) visit(ctx.function_operation());
    Window window = new Window(attributeFunction.getNamespace(), attributeFunction.getName(), attributeFunction
            .getParameters());
    windowDefinition.window(window);

    // Optional output event type
    if (ctx.output_event_type() != null) {
        windowDefinition.setOutputEventType((OutputStream.OutputEventType) visit(ctx.output_event_type()));
    }
    populateQueryContext(windowDefinition, ctx);
    return windowDefinition;
}
 
Example #22
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void test15() {
    Query queryString = SiddhiCompiler.parseQuery("" +
            "from  StockStream[7+9.5 > price and 100 >= volume]#window.length(50) " +
            " " +
            "select symbol as symbol, price as price, volume as volume  " +
            "update StockQuote \n " +
            " on symbol==StockQuote.symbol ;"
    );
    AssertJUnit.assertNotNull(queryString);

    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("price", Expression.variable("price")).
                    select("volume", Expression.variable("volume"))
    );
    query.updateBy("StockQuote", OutputStream.OutputEventType.CURRENT_EVENTS,
            Expression.compare(
                    Expression.variable("symbol"),
                    Compare.Operator.EQUAL,
                    Expression.variable("symbol").ofStream("StockQuote")));

    AssertJUnit.assertEquals(query, queryString);
}
 
Example #23
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void test11() throws SiddhiParserException {
    Query query = SiddhiCompiler.parseQuery("from  StockStream[price>3]#window.length(50) " +
            "select symbol, avg(price) as avgPrice " +
            "group by symbol " +
            "having (price >= 20)" +
            "order by avgPrice " +
            "limit 5 " +
            "insert all events into StockQuote; "
    );
    AssertJUnit.assertNotNull(query);

    Query api = Query.query().from(InputStream.stream("StockStream").
            filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN, Expression
                    .value(3))).
            window("length", Expression.value(50))).
            select(Selector.selector().select(Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(
                            Expression.variable("price"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(20))).
                    orderBy(Expression.variable("avgPrice")).
                    limit(Expression.value(5))).
            insertInto("StockQuote", OutputStream.OutputEventType.ALL_EVENTS);
    AssertJUnit.assertEquals(api, query);

}
 
Example #24
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void test13() throws SiddhiParserException {
    Query query = SiddhiCompiler.parseQuery("from  StockStream[price>3]#window.length(50) " +
            "select symbol, avg(price) as avgPrice " +
            "group by symbol " +
            "having (price >= 20)" +
            "order by avgPrice desc " +
            "limit 5 " +
            "offset 3 " +
            "insert all events into StockQuote; "
    );
    AssertJUnit.assertNotNull(query);

    Query api = Query.query().from(InputStream.stream("StockStream").
            filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN, Expression
                    .value(3))).
            window("length", Expression.value(50))).
            select(Selector.selector().select(Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(
                            Expression.variable("price"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(20))).
                    orderBy(Expression.variable("avgPrice"), OrderByAttribute.Order.DESC).
                    limit(Expression.value(5)).
                    offset(Expression.value(3))).
            insertInto("StockQuote", OutputStream.OutputEventType.ALL_EVENTS);
    AssertJUnit.assertEquals(api, query);

}
 
Example #25
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void test12() throws SiddhiParserException {
    Query query = SiddhiCompiler.parseQuery("from  StockStream[price>3]#window.length(50) " +
            "select symbol, avg(price) as avgPrice " +
            "group by symbol " +
            "having (price >= 20)" +
            "order by avgPrice desc " +
            "limit 5 " +
            "insert all events into StockQuote; "
    );
    AssertJUnit.assertNotNull(query);

    Query api = Query.query().from(InputStream.stream("StockStream").
            filter(Expression.compare(Expression.variable("price"), Compare.Operator.GREATER_THAN, Expression
                    .value(3))).
            window("length", Expression.value(50))).
            select(Selector.selector().select(Expression.variable("symbol")).
                    select("avgPrice", Expression.function("avg", Expression.variable("price"))).
                    groupBy(Expression.variable("symbol")).
                    having(Expression.compare(
                            Expression.variable("price"),
                            Compare.Operator.GREATER_THAN_EQUAL,
                            Expression.value(20))).
                    orderBy(Expression.variable("avgPrice"), OrderByAttribute.Order.DESC).
                    limit(Expression.value(5))).
            insertInto("StockQuote", OutputStream.OutputEventType.ALL_EVENTS);
    AssertJUnit.assertEquals(api, query);

}
 
Example #26
Source File: TestStoreContainingInMemoryTable.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Override
    protected CompiledSelection compileSelection(List<SelectAttributeBuilder> selectAttributeBuilders,
                                                 List<ExpressionBuilder> groupByExpressionBuilder,
                                                 ExpressionBuilder havingExpressionBuilder,
                                                 List<OrderByAttributeBuilder> orderByAttributeBuilders, Long limit,
                                                 Long offset) {

        selectAttributeBuilders.forEach((selectAttributeBuilder -> {
            TestStoreConditionVisitor testStoreConditionVisitor = new TestStoreConditionVisitor("");
            selectAttributeBuilder.getExpressionBuilder().build(testStoreConditionVisitor);
            if (testStoreConditionVisitor.getStreamVarCount() > 0) {
                throw new SiddhiAppCreationException("testStoreContainingInMemoryTable does not support " +
                        "lookup with stream variables");
            }
        }));

        CompiledSelectionWithCache compiledSelectionWithCache;
        MetaStateEvent metaStateEvent = matchingMetaInfoHolderForTestOnDemandQuery.getMetaStateEvent().clone();

        ReturnStream returnStream = new ReturnStream(OutputStream.OutputEventType.CURRENT_EVENTS);
        int metaPosition = SiddhiConstants.UNKNOWN_STATE;
        List<VariableExpressionExecutor> variableExpressionExecutorsForQuerySelector = new ArrayList<>();

        if (metaStateEvent.getOutputDataAttributes().size() == 0) {
//            MetaStateEvent metaStateEventWithOutputData = new MetaStateEvent(metaStateEvent.getStreamEventCount());
            for (Attribute outputAttribute : metaStateEvent.getMetaStreamEvents()[0].getOnAfterWindowData()) {
                metaStateEvent.getMetaStreamEvents()[0].addOutputData(outputAttribute);
            }
        }

        if (metaStateEvent.getOutputDataAttributes().size() > 0) {
            while (metaStateEvent.getMetaStreamEvent(0).getOutputData().size() > 0) {
                metaStateEvent.getMetaStreamEvent(0).getOutputData().remove(0);
            }
        }

        QuerySelector querySelector = SelectorParser.parse(selectorForTestOnDemandQuery,
                returnStream,
                metaStateEvent, tableMap,
                variableExpressionExecutorsForQuerySelector, metaPosition, ProcessingMode.BATCH,
                false, siddhiQueryContextForTestOnDemandQuery);
        QueryParserHelper.updateVariablePosition(metaStateEvent,
                variableExpressionExecutorsForQuerySelector);
        querySelector.setEventPopulator(
                StateEventPopulatorFactory.constructEventPopulator(metaStateEvent));

        compiledSelectionWithCache = new CompiledSelectionWithCache(null, querySelector, metaStateEvent, 0, null);
        return compiledSelectionWithCache;
    }
 
Example #27
Source File: JoinQueryTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreatingJoinQuery() {
    Query query = Query.query().
            annotation(Annotation.annotation("foo").element("name", "Query1").element("summery", "Test Query")
                    .element("Custom")).
            from(
                    InputStream.joinStream(
                            InputStream.stream("s1", "cseEventStream").
                                    window("lengthBatch", Expression.value(50)),
                            JoinInputStream.Type.JOIN,
                            InputStream.stream("s2", "cseEventStream").
                                    filter(Expression.and(
                                            Expression.compare(
                                                    Expression.add(Expression.value(7), Expression.value(9.5)),
                                                    Compare.Operator.GREATER_THAN,
                                                    Expression.variable("price").ofStream("cseEventStream")),
                                            Expression.compare(Expression.value(100),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.variable("volume").ofStream("cseEventStream")
                                            )
                                            )
                                    ).window("lengthBatch", Expression.value(50)),
                            Expression.compare(
                                    Expression.variable("price").ofStream("s1"),
                                    Compare.Operator.EQUAL,
                                    Expression.variable("price").ofStream("s2"))
                    )
            ).
            select(
                    Selector.selector().
                            select("symbol", Expression.variable("symbol").ofStream("cseEventStream")).
                            select(null, Expression.variable("symbol").ofStream("cseEventStream")).
                            groupBy(Expression.variable("symbol").ofStream("cseEventStream")).
                            having(
                                    Expression.compare(
                                            Expression.add(Expression.value(7), Expression.value(9.5)),
                                            Compare.Operator.GREATER_THAN,
                                            Expression.variable("price"))
                            )
            ).
            insertInto("StockQuote", OutputStream.OutputEventType.EXPIRED_EVENTS);
    List<String> streamIds = new ArrayList<>();
    streamIds.add("cseEventStream");
    Assert.assertEquals(query.getInputStream().getAllStreamIds(), streamIds);
    Assert.assertEquals(query.getInputStream().getUniqueStreamIds(), streamIds);
}
 
Example #28
Source File: JoinQueryTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testJoinQueryToString() {
    Query query = Query.query().
            annotation(Annotation.annotation("foo").element("name", "Query1").element("summery", "Test Query")
                    .element("Custom")).
            from(
                    InputStream.joinStream(
                            InputStream.stream("s1", "cseEventStream").
                                    window("lengthBatch", Expression.value(50)),
                            JoinInputStream.Type.JOIN,
                            InputStream.stream("s2", "cseEventStream").
                                    filter(Expression.and(
                                            Expression.compare(
                                                    Expression.add(Expression.value(7), Expression.value(9.5)),
                                                    Compare.Operator.GREATER_THAN,
                                                    Expression.variable("price").ofStream("cseEventStream")),
                                            Expression.compare(Expression.value(100),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.variable("volume").ofStream("cseEventStream")
                                            )
                                            )
                                    ).window("lengthBatch", Expression.value(50)),
                            Expression.compare(
                                    Expression.variable("price").ofStream("s1"),
                                    Compare.Operator.EQUAL,
                                    Expression.variable("price").ofStream("s2"))
                    )
            ).
            select(
                    Selector.selector().
                            select("symbol", Expression.variable("symbol").ofStream("cseEventStream")).
                            select(null, Expression.variable("symbol").ofStream("cseEventStream")).
                            groupBy(Expression.variable("symbol").ofStream("cseEventStream")).
                            having(
                                    Expression.compare(
                                            Expression.add(Expression.value(7), Expression.value(9.5)),
                                            Compare.Operator.GREATER_THAN,
                                            Expression.variable("price"))
                            )
            ).
            insertInto("StockQuote", OutputStream.OutputEventType.EXPIRED_EVENTS);
    String queryString = "Query{stream=JoinInputStream{" +
            "leftInputStream=SingleInputStream{isFaultStream=false, isInnerStream=false, id='cseEventStream', " +
            "streamReferenceId='s1', streamHandlers=[Window{namespace='', function='lengthBatch', " +
            "parameters=[IntConstant{value=50}]}], windowPosition=0}, type=JOIN, " +
            "rightInputStream=SingleInputStream{isFaultStream=false, isInnerStream=false, id='cseEventStream', " +
            "streamReferenceId='s2', streamHandlers=[Filter{" +
            "filterExpression=And{leftExpression=Compare{rightExpression=Variable{id='cseEventStream', " +
            "isInnerStream=false, streamIndex=null, functionId='null', functionIndex=null," +
            " attributeName='price'}, operator=GREATER_THAN, leftExpression=Add{leftValue=IntConstant{value=7}, " +
            "rightValue=DoubleConstant{value=9.5}}}, rightExpression=Compare{" +
            "rightExpression=Variable{id='cseEventStream', isInnerStream=false, streamIndex=null, " +
            "functionId='null', functionIndex=null, attributeName='volume'}, operator=GREATER_THAN_EQUAL, " +
            "leftExpression=IntConstant{value=100}}}}, Window{namespace='', function='lengthBatch', " +
            "parameters=[IntConstant{value=50}]}], windowPosition=1}, " +
            "onCompare=Compare{rightExpression=Variable{id='s2', isInnerStream=false, streamIndex=null, " +
            "functionId='null', functionIndex=null, attributeName='price'}, operator=EQUAL, " +
            "leftExpression=Variable{id='s1', isInnerStream=false, streamIndex=null, functionId='null', " +
            "functionIndex=null, attributeName='price'}}, trigger=ALL, within=null, per=null}, " +
            "selector=Selector{selectionList=[OutputAttribute{rename='symbol', " +
            "expression=Variable{id='cseEventStream', isInnerStream=false, streamIndex=null, functionId='null', " +
            "functionIndex=null, attributeName='symbol'}}, OutputAttribute{rename='null', " +
            "expression=Variable{id='cseEventStream', isInnerStream=false, streamIndex=null, functionId='null', " +
            "functionIndex=null, attributeName='symbol'}}], groupByList=[Variable{id='cseEventStream', " +
            "isInnerStream=false, streamIndex=null, functionId='null', functionIndex=null, " +
            "attributeName='symbol'}], havingExpression=Compare{rightExpression=Variable{id='null', " +
            "isInnerStream=false, streamIndex=null, functionId='null', functionIndex=null, " +
            "attributeName='price'}, operator=GREATER_THAN, leftExpression=Add{leftValue=IntConstant{value=7}, " +
            "rightValue=DoubleConstant{value=9.5}}}, orderByList=[], limit=null, offset=null}, " +
            "outputStream=InsertIntoStream{isFaultStream=false, isInnerStream=false}, outputRate=null, " +
            "annotations=[@foo( name = \"Query1\", summery = \"Test Query\", \"Custom\")]}";
    Assert.assertEquals(query.toString(), queryString);
}
 
Example #29
Source File: JoinQueryTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreatingJoinQuery1() {
    Query query = Query.query().
            annotation(Annotation.annotation("foo").element("name", "Query1").element("summery", "Test Query")
                    .element("Custom")).
            from(
                    InputStream.joinStream(
                            InputStream.stream("s1", "cseEventStream").
                                    window("lengthBatch", Expression.value(50)),
                            JoinInputStream.Type.JOIN,
                            InputStream.stream("s2", "cseEventStream").
                                    filter(Expression.and(
                                            Expression.compare(
                                                    Expression.add(Expression.value(7), Expression.value(9.5)),
                                                    Compare.Operator.GREATER_THAN,
                                                    Expression.variable("price").ofStream("cseEventStream")),
                                            Expression.compare(Expression.value(100),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.variable("volume").ofStream("cseEventStream")
                                            )
                                            )
                                    ).window("lengthBatch", Expression.value(50)),
                            Expression.compare(
                                    Expression.variable("price").ofStream("s1"),
                                    Compare.Operator.EQUAL,
                                    Expression.variable("price").ofStream("s2"))
                    )
            ).
            select(
                    Selector.selector().
                            select("symbol", Expression.variable("symbol").ofStream("cseEventStream")).
                            select(null, Expression.variable("symbol").ofStream("cseEventStream")).
                            groupBy(Expression.variable("symbol").ofStream("cseEventStream")).
                            having(
                                    Expression.compare(
                                            Expression.add(Expression.value(7), Expression.value(9.5)),
                                            Compare.Operator.GREATER_THAN,
                                            Expression.variable("price"))
                            )
            ).
            insertInto("StockQuote", OutputStream.OutputEventType.EXPIRED_EVENTS);

    Query query1 = Query.query().
            annotation(Annotation.annotation("foo").element("name", "Query1").element("summery", "Test Query")
                    .element("Custom")).
            from(
                    InputStream.joinStream(
                            InputStream.stream("s1", "cseEventStream").
                                    window("lengthBatch", Expression.value(50)),
                            JoinInputStream.Type.JOIN,
                            InputStream.stream("s2", "cseEventStream").
                                    filter(Expression.and(
                                            Expression.compare(
                                                    Expression.add(Expression.value(7), Expression.value(9.5)),
                                                    Compare.Operator.GREATER_THAN,
                                                    Expression.variable("price").ofStream("cseEventStream")),
                                            Expression.compare(Expression.value(100),
                                                    Compare.Operator.GREATER_THAN_EQUAL,
                                                    Expression.variable("volume").ofStream("cseEventStream")
                                            )
                                            )
                                    ).window("lengthBatch", Expression.value(50)),
                            Expression.compare(
                                    Expression.variable("price").ofStream("s1"),
                                    Compare.Operator.EQUAL,
                                    Expression.variable("price").ofStream("s2"))
                    )
            ).
            select(
                    Selector.selector().
                            select("symbol", Expression.variable("symbol").ofStream("cseEventStream")).
                            select(null, Expression.variable("symbol").ofStream("cseEventStream")).
                            groupBy(Expression.variable("symbol").ofStream("cseEventStream")).
                            having(
                                    Expression.compare(
                                            Expression.add(Expression.value(7), Expression.value(9.5)),
                                            Compare.Operator.GREATER_THAN,
                                            Expression.variable("price"))
                            )
            ).
            insertInto("StockQuote", OutputStream.OutputEventType.EXPIRED_EVENTS);
    Assert.assertTrue(query.equals(query1));
    Assert.assertEquals(query.hashCode(), query1.hashCode());
}
 
Example #30
Source File: Window.java    From siddhi with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize the WindowEvent table by creating {@link WindowProcessor} to handle the events.
 *
 * @param tableMap         map of {@link Table}s
 * @param eventWindowMap   map of EventWindows
 * @param windowName       name of the query window belongs to.
 * @param findToBeExecuted will find will be executed on the window.
 */
public void init(Map<String, Table> tableMap, Map<String, Window> eventWindowMap, String windowName,
                 boolean findToBeExecuted) {
    if (this.windowProcessor != null) {
        return;
    }

    // Create and initialize MetaStreamEvent
    MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
    metaStreamEvent.addInputDefinition(windowDefinition);
    metaStreamEvent.setEventType(MetaStreamEvent.EventType.WINDOW);
    for (Attribute attribute : windowDefinition.getAttributeList()) {
        metaStreamEvent.addOutputData(attribute);
    }

    this.streamEventFactory = new StreamEventFactory(metaStreamEvent);
    StreamEventCloner streamEventCloner = new StreamEventCloner(metaStreamEvent, this.streamEventFactory);
    OutputStream.OutputEventType outputEventType = windowDefinition.getOutputEventType();
    boolean outputExpectsExpiredEvents = outputEventType != OutputStream.OutputEventType.CURRENT_EVENTS;

    SiddhiQueryContext siddhiQueryContext = new SiddhiQueryContext(siddhiAppContext, windowName);
    WindowProcessor internalWindowProcessor = (WindowProcessor) SingleInputStreamParser.generateProcessor
            (windowDefinition.getWindow(), metaStreamEvent, new ArrayList<VariableExpressionExecutor>(),
                    tableMap, false,
                    outputExpectsExpiredEvents, findToBeExecuted, siddhiQueryContext);
    internalWindowProcessor.setStreamEventCloner(streamEventCloner);
    internalWindowProcessor.constructStreamEventPopulater(metaStreamEvent, 0);

    EntryValveProcessor entryValveProcessor = null;
    if (internalWindowProcessor instanceof SchedulingProcessor) {
        entryValveProcessor = new EntryValveProcessor(this.siddhiAppContext);
        Scheduler scheduler = SchedulerParser.parse(entryValveProcessor, siddhiQueryContext);
        scheduler.init(this.lockWrapper, windowName);
        scheduler.setStreamEventFactory(streamEventFactory);
        ((SchedulingProcessor) internalWindowProcessor).setScheduler(scheduler);
    }
    if (entryValveProcessor != null) {
        entryValveProcessor.setToLast(internalWindowProcessor);
        this.windowProcessor = entryValveProcessor;
    } else {
        this.windowProcessor = internalWindowProcessor;
    }

    // StreamPublishProcessor must be the last in chain so that it can publish the events to StreamJunction
    this.windowProcessor.setToLast(new StreamPublishProcessor(outputEventType));
    this.internalWindowProcessor = internalWindowProcessor;
}