Search in sources :

Example 6 with InsertIntoStream

use of io.siddhi.query.api.execution.query.output.stream.InsertIntoStream in project siddhi by wso2.

the class OutputParser method constructOutputCallback.

public static OutputCallback constructOutputCallback(final OutputStream outStream, StreamDefinition outputStreamDefinition, Map<String, Table> tableMap, Map<String, Window> eventWindowMap, boolean convertToStreamEvent, SiddhiQueryContext siddhiQueryContext) {
    String id = outStream.getId();
    Table table = null;
    Window window = null;
    if (id != null) {
        table = tableMap.get(id);
        window = eventWindowMap.get(id);
    }
    StreamEventFactory streamEventFactory = null;
    StreamEventConverter streamEventConverter = null;
    MetaStreamEvent tableMetaStreamEvent = null;
    if (table != null) {
        tableMetaStreamEvent = new MetaStreamEvent();
        tableMetaStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
        TableDefinition matchingTableDefinition = TableDefinition.id("");
        for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
            tableMetaStreamEvent.addOutputData(attribute);
            matchingTableDefinition.attribute(attribute.getName(), attribute.getType());
        }
        matchingTableDefinition.setQueryContextStartIndex(outStream.getQueryContextStartIndex());
        matchingTableDefinition.setQueryContextEndIndex(outStream.getQueryContextEndIndex());
        tableMetaStreamEvent.addInputDefinition(matchingTableDefinition);
        streamEventFactory = new StreamEventFactory(tableMetaStreamEvent);
        streamEventConverter = new ZeroStreamEventConverter();
    }
    // Construct CallBack
    if (outStream instanceof InsertIntoStream || outStream instanceof ReturnStream) {
        if (window != null) {
            if (!siddhiQueryContext.isPartitioned()) {
                return new InsertIntoWindowCallback(window, outputStreamDefinition, siddhiQueryContext.getName());
            } else {
                return new InsertIntoWindowEndPartitionCallback(window, outputStreamDefinition, siddhiQueryContext.getName());
            }
        } else if (table != null) {
            DefinitionParserHelper.validateOutputStream(outputStreamDefinition, table.getTableDefinition());
            return new InsertIntoTableCallback(table, outputStreamDefinition, convertToStreamEvent, streamEventFactory, streamEventConverter, siddhiQueryContext.getName());
        } else {
            if (!siddhiQueryContext.isPartitioned() || outputStreamDefinition.getId().startsWith("#")) {
                return new InsertIntoStreamCallback(outputStreamDefinition, siddhiQueryContext.getName());
            } else {
                return new InsertIntoStreamEndPartitionCallback(outputStreamDefinition, siddhiQueryContext.getName());
            }
        }
    } else if (outStream instanceof DeleteStream || outStream instanceof UpdateStream || outStream instanceof UpdateOrInsertStream) {
        if (table != null) {
            if (outStream instanceof UpdateStream) {
                if (((UpdateStream) outStream).getUpdateSet() == null) {
                    TableDefinition tableDefinition = table.getTableDefinition();
                    for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
                        if (!tableDefinition.getAttributeList().contains(attribute)) {
                            throw new SiddhiAppCreationException("Attribute " + attribute + " does not exist on " + "Event Table " + tableDefinition, outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
                        }
                    }
                }
            }
            if (outStream instanceof UpdateOrInsertStream) {
                TableDefinition tableDefinition = table.getTableDefinition();
                for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
                    if (!tableDefinition.getAttributeList().contains(attribute)) {
                        throw new SiddhiAppCreationException("Attribute " + attribute + " does not exist on " + "Event Table " + tableDefinition, outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
                    }
                }
            }
            if (outStream instanceof DeleteStream) {
                try {
                    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
                    CompiledCondition compiledCondition = table.compileCondition((((DeleteStream) outStream).getOnDeleteExpression()), matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
                    StateEventFactory stateEventFactory = new StateEventFactory(matchingMetaInfoHolder.getMetaStateEvent());
                    return new DeleteTableCallback(table, compiledCondition, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventFactory, streamEventFactory, streamEventConverter, siddhiQueryContext.getName());
                } catch (SiddhiAppValidationException e) {
                    throw new SiddhiAppCreationException("Cannot create delete for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiQueryContext.getName(), siddhiQueryContext.getSiddhiAppContext().getSiddhiAppString());
                }
            } else if (outStream instanceof UpdateStream) {
                try {
                    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
                    CompiledCondition compiledCondition = table.compileCondition((((UpdateStream) outStream).getOnUpdateExpression()), matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
                    UpdateSet updateSet = ((UpdateStream) outStream).getUpdateSet();
                    if (updateSet == null) {
                        updateSet = new UpdateSet();
                        for (Attribute attribute : matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeList()) {
                            updateSet.set(new Variable(attribute.getName()), new Variable(attribute.getName()));
                        }
                    }
                    CompiledUpdateSet compiledUpdateSet = table.compileUpdateSet(updateSet, matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
                    StateEventFactory stateEventFactory = new StateEventFactory(matchingMetaInfoHolder.getMetaStateEvent());
                    return new UpdateTableCallback(table, compiledCondition, compiledUpdateSet, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventFactory, streamEventFactory, streamEventConverter, siddhiQueryContext.getName());
                } catch (SiddhiAppValidationException e) {
                    throw new SiddhiAppCreationException("Cannot create update for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiQueryContext.getSiddhiAppContext());
                }
            } else {
                DefinitionParserHelper.validateOutputStream(outputStreamDefinition, table.getTableDefinition());
                try {
                    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
                    CompiledCondition compiledCondition = table.compileCondition((((UpdateOrInsertStream) outStream).getOnUpdateExpression()), matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
                    UpdateSet updateSet = ((UpdateOrInsertStream) outStream).getUpdateSet();
                    if (updateSet == null) {
                        updateSet = new UpdateSet();
                        for (Attribute attribute : matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeList()) {
                            updateSet.set(new Variable(attribute.getName()), new Variable(attribute.getName()));
                        }
                    }
                    CompiledUpdateSet compiledUpdateSet = table.compileUpdateSet(updateSet, matchingMetaInfoHolder, null, tableMap, siddhiQueryContext);
                    StateEventFactory stateEventFactory = new StateEventFactory(matchingMetaInfoHolder.getMetaStateEvent());
                    return new UpdateOrInsertTableCallback(table, compiledCondition, compiledUpdateSet, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventFactory, streamEventFactory, streamEventConverter, siddhiQueryContext.getName());
                } catch (SiddhiAppValidationException e) {
                    throw new SiddhiAppCreationException("Cannot create update or insert into for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiQueryContext.getSiddhiAppContext());
                }
            }
        } else {
            throw new SiddhiAppCreationException("Event table with id :" + id + " does not exist", outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
        }
    } else {
        throw new SiddhiAppCreationException(outStream.getClass().getName() + " not supported", outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
    }
}
Also used : InsertIntoWindowEndPartitionCallback(io.siddhi.core.query.output.callback.InsertIntoWindowEndPartitionCallback) Variable(io.siddhi.query.api.expression.Variable) Attribute(io.siddhi.query.api.definition.Attribute) InsertIntoWindowCallback(io.siddhi.core.query.output.callback.InsertIntoWindowCallback) UpdateStream(io.siddhi.query.api.execution.query.output.stream.UpdateStream) ZeroStreamEventConverter(io.siddhi.core.event.stream.converter.ZeroStreamEventConverter) UpdateTableCallback(io.siddhi.core.query.output.callback.UpdateTableCallback) DeleteStream(io.siddhi.query.api.execution.query.output.stream.DeleteStream) InsertIntoStreamEndPartitionCallback(io.siddhi.core.query.output.callback.InsertIntoStreamEndPartitionCallback) UpdateOrInsertTableCallback(io.siddhi.core.query.output.callback.UpdateOrInsertTableCallback) TableDefinition(io.siddhi.query.api.definition.TableDefinition) InsertIntoTableCallback(io.siddhi.core.query.output.callback.InsertIntoTableCallback) Window(io.siddhi.core.window.Window) Table(io.siddhi.core.table.Table) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) StreamEventFactory(io.siddhi.core.event.stream.StreamEventFactory) StreamEventConverter(io.siddhi.core.event.stream.converter.StreamEventConverter) ZeroStreamEventConverter(io.siddhi.core.event.stream.converter.ZeroStreamEventConverter) InsertIntoStream(io.siddhi.query.api.execution.query.output.stream.InsertIntoStream) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) ReturnStream(io.siddhi.query.api.execution.query.output.stream.ReturnStream) InsertIntoStreamCallback(io.siddhi.core.query.output.callback.InsertIntoStreamCallback) CompiledUpdateSet(io.siddhi.core.table.CompiledUpdateSet) UpdateOrInsertStream(io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream) CompiledCondition(io.siddhi.core.util.collection.operator.CompiledCondition) MatchingMetaInfoHolder(io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) DeleteTableCallback(io.siddhi.core.query.output.callback.DeleteTableCallback) StateEventFactory(io.siddhi.core.event.state.StateEventFactory) UpdateSet(io.siddhi.query.api.execution.query.output.stream.UpdateSet) CompiledUpdateSet(io.siddhi.core.table.CompiledUpdateSet) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 7 with InsertIntoStream

use of io.siddhi.query.api.execution.query.output.stream.InsertIntoStream in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitStore_query.

@Override
public Object visitStore_query(SiddhiQLParser.Store_queryContext ctx) {
    OutputStream outputStream;
    OnDemandQuery onDemandQuery = OnDemandQuery.query();
    if (ctx.FROM() != null) {
        onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.FIND);
        onDemandQuery.from((InputStore) visit(ctx.store_input()));
        if (ctx.query_section() != null) {
            onDemandQuery = onDemandQuery.select((Selector) visit(ctx.query_section()));
        }
    } else if (ctx.query_section() != null) {
        onDemandQuery.select((Selector) visit(ctx.query_section()));
        if (ctx.UPDATE() != null && ctx.OR() != null) {
            onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.UPDATE_OR_INSERT);
            Source source = (Source) visit(ctx.target());
            if (source.isInnerStream || source.isFaultStream) {
                throw newSiddhiParserException(ctx, "UPDATE OR INTO INSERT can be only used with Tables!");
            }
            if (ctx.set_clause() != null) {
                outputStream = new UpdateOrInsertStream(source.streamId, (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
            } else {
                outputStream = new UpdateOrInsertStream(source.streamId, (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
            }
            onDemandQuery.outStream(outputStream);
        } else if (ctx.INSERT() != null) {
            onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.INSERT);
            Source source = (Source) visit(ctx.target());
            if (source.isInnerStream || source.isFaultStream) {
                throw newSiddhiParserException(ctx, "INSERT can be only used with Tables!");
            }
            outputStream = new InsertIntoStream(source.streamId);
            populateQueryContext(outputStream, ctx);
            onDemandQuery.outStream(outputStream);
        } else if (ctx.store_query_output() != null) {
            outputStream = (OutputStream) visit(ctx.store_query_output());
            if (outputStream instanceof DeleteStream) {
                onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.DELETE);
            } else if (outputStream instanceof UpdateStream) {
                onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.UPDATE);
            }
            onDemandQuery.outStream(outputStream);
        }
    } else if (ctx.store_query_output() != null) {
        outputStream = (OutputStream) visit(ctx.store_query_output());
        if (outputStream instanceof DeleteStream) {
            onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.DELETE);
        } else if (outputStream instanceof UpdateStream) {
            onDemandQuery.setType(OnDemandQuery.OnDemandQueryType.UPDATE);
        }
        onDemandQuery.outStream(outputStream);
    }
    populateQueryContext(onDemandQuery, ctx);
    return onDemandQuery;
}
Also used : UpdateOrInsertStream(io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream) DeleteStream(io.siddhi.query.api.execution.query.output.stream.DeleteStream) OutputStream(io.siddhi.query.api.execution.query.output.stream.OutputStream) InsertIntoStream(io.siddhi.query.api.execution.query.output.stream.InsertIntoStream) UpdateStream(io.siddhi.query.api.execution.query.output.stream.UpdateStream) OnDemandQuery(io.siddhi.query.api.execution.query.OnDemandQuery) BasicSelector(io.siddhi.query.api.execution.query.selection.BasicSelector) Selector(io.siddhi.query.api.execution.query.selection.Selector)

Example 8 with InsertIntoStream

use of io.siddhi.query.api.execution.query.output.stream.InsertIntoStream in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitQuery_output.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public OutputStream visitQuery_output(@NotNull SiddhiQLParser.Query_outputContext ctx) {
    if (ctx.INSERT() != null) {
        Source source = (Source) visit(ctx.target());
        if (ctx.UPDATE() != null && ctx.OR() != null) {
            if (source.isInnerStream || source.isFaultStream) {
                throw newSiddhiParserException(ctx, "UPDATE OR INTO INSERT be only used with Tables!");
            }
            if (ctx.output_event_type() != null) {
                if (ctx.set_clause() != null) {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                } else {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                }
            } else {
                if (ctx.set_clause() != null) {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                } else {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                }
            }
        } else {
            if (ctx.output_event_type() != null) {
                OutputStream outputStream = new InsertIntoStream(source.streamId, source.isInnerStream, source.isFaultStream, (OutputStream.OutputEventType) visit(ctx.output_event_type()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            } else {
                OutputStream outputStream = new InsertIntoStream(source.streamId, source.isInnerStream, source.isFaultStream);
                populateQueryContext(outputStream, ctx);
                return outputStream;
            }
        }
    } else if (ctx.DELETE() != null) {
        Source source = (Source) visit(ctx.target());
        if (source.isInnerStream || source.isFaultStream) {
            throw newSiddhiParserException(ctx, "DELETE can be only used with Tables!");
        }
        Expression expression = null;
        if (ctx.expression() != null) {
            expression = (Expression) visit(ctx.expression());
        }
        if (ctx.output_event_type() != null) {
            OutputStream outputStream = new DeleteStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), expression);
            populateQueryContext(outputStream, ctx);
            return outputStream;
        } else {
            OutputStream outputStream = new DeleteStream(source.streamId, expression);
            populateQueryContext(outputStream, ctx);
            return outputStream;
        }
    } else if (ctx.UPDATE() != null) {
        Source source = (Source) visit(ctx.target());
        if (source.isInnerStream || source.isFaultStream) {
            throw newSiddhiParserException(ctx, "UPDATE can be only used with Tables!");
        }
        if (ctx.output_event_type() != null) {
            if (ctx.set_clause() != null) {
                OutputStream outputStream = new UpdateStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            } else {
                OutputStream outputStream = new UpdateStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            }
        } else {
            if (ctx.set_clause() != null) {
                OutputStream outputStream = new UpdateStream(source.streamId, (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            } else {
                OutputStream outputStream = new UpdateStream(source.streamId, (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            }
        }
    } else if (ctx.RETURN() != null) {
        if (ctx.output_event_type() != null) {
            OutputStream outputStream = new ReturnStream((OutputStream.OutputEventType) visit(ctx.output_event_type()));
            populateQueryContext(outputStream, ctx);
            return outputStream;
        } else {
            OutputStream outputStream = new ReturnStream();
            populateQueryContext(outputStream, ctx);
            return outputStream;
        }
    } else {
        throw newSiddhiParserException(ctx);
    }
}
Also used : UpdateOrInsertStream(io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream) Expression(io.siddhi.query.api.expression.Expression) DeleteStream(io.siddhi.query.api.execution.query.output.stream.DeleteStream) OutputStream(io.siddhi.query.api.execution.query.output.stream.OutputStream) InsertIntoStream(io.siddhi.query.api.execution.query.output.stream.InsertIntoStream) UpdateStream(io.siddhi.query.api.execution.query.output.stream.UpdateStream) ReturnStream(io.siddhi.query.api.execution.query.output.stream.ReturnStream)

Example 9 with InsertIntoStream

use of io.siddhi.query.api.execution.query.output.stream.InsertIntoStream in project siddhi by wso2.

the class Query method insertIntoFault.

public Query insertIntoFault(String outputStreamId) {
    this.outputStream = new InsertIntoStream(outputStreamId, false, true);
    updateOutputEventType(outputRate, outputStream);
    return this;
}
Also used : InsertIntoStream(io.siddhi.query.api.execution.query.output.stream.InsertIntoStream)

Example 10 with InsertIntoStream

use of io.siddhi.query.api.execution.query.output.stream.InsertIntoStream in project siddhi by wso2.

the class Query method insertIntoFault.

public Query insertIntoFault(String outputStreamId, OutputStream.OutputEventType outputEventType) {
    this.outputStream = new InsertIntoStream(outputStreamId, false, true, outputEventType);
    updateOutputEventType(outputRate, outputStream);
    return this;
}
Also used : InsertIntoStream(io.siddhi.query.api.execution.query.output.stream.InsertIntoStream)

Aggregations

InsertIntoStream (io.siddhi.query.api.execution.query.output.stream.InsertIntoStream)12 DeleteStream (io.siddhi.query.api.execution.query.output.stream.DeleteStream)4 UpdateOrInsertStream (io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream)4 UpdateStream (io.siddhi.query.api.execution.query.output.stream.UpdateStream)4 InsertIntoStreamCallback (io.siddhi.core.query.output.callback.InsertIntoStreamCallback)3 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)2 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)2 InsertIntoWindowCallback (io.siddhi.core.query.output.callback.InsertIntoWindowCallback)2 StreamJunction (io.siddhi.core.stream.StreamJunction)2 Table (io.siddhi.core.table.Table)2 Window (io.siddhi.core.window.Window)2 OutputStream (io.siddhi.query.api.execution.query.output.stream.OutputStream)2 ReturnStream (io.siddhi.query.api.execution.query.output.stream.ReturnStream)2 Expression (io.siddhi.query.api.expression.Expression)2 AggregationRuntime (io.siddhi.core.aggregation.AggregationRuntime)1 SiddhiOnDemandQueryContext (io.siddhi.core.config.SiddhiOnDemandQueryContext)1 SiddhiQueryContext (io.siddhi.core.config.SiddhiQueryContext)1 StateEventFactory (io.siddhi.core.event.state.StateEventFactory)1 StreamEventFactory (io.siddhi.core.event.stream.StreamEventFactory)1 StreamEventConverter (io.siddhi.core.event.stream.converter.StreamEventConverter)1