use of io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream in project siddhi by wso2.
the class Query method updateOrInsertBy.
public void updateOrInsertBy(String outputTableId, OutputStream.OutputEventType outputEventType, UpdateSet updateSetAttributes, Expression onUpdateExpression) {
this.outputStream = new UpdateOrInsertStream(outputTableId, outputEventType, updateSetAttributes, onUpdateExpression);
updateOutputEventType(outputRate, outputStream);
}
use of io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream in project siddhi by wso2.
the class OnDemandQueryParser method parse.
public static OnDemandQueryRuntime parse(OnDemandQuery onDemandQuery, String onDemandQueryString, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, Map<String, Window> windowMap, Map<String, AggregationRuntime> aggregationMap) {
final LockWrapper lockWrapper = new LockWrapper("OnDemandQueryLock");
lockWrapper.setLock(new ReentrantLock());
MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
int metaPosition = SiddhiConstants.UNKNOWN_STATE;
String queryName;
Table table;
SiddhiQueryContext siddhiQueryContext;
Expression onCondition;
SnapshotService.getSkipStateStorageThreadLocal().set(true);
switch(onDemandQuery.getType()) {
case FIND:
Within within = null;
Expression per = null;
queryName = "store_select_query_" + onDemandQuery.getInputStore().getStoreId();
siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
InputStore inputStore = onDemandQuery.getInputStore();
try {
onCondition = Expression.value(true);
metaStreamEvent.setInputReferenceId(inputStore.getStoreReferenceId());
if (inputStore instanceof AggregationInputStore) {
AggregationInputStore aggregationInputStore = (AggregationInputStore) inputStore;
if (aggregationMap.get(inputStore.getStoreId()) == null) {
throw new OnDemandQueryCreationException("Aggregation \"" + inputStore.getStoreId() + "\" has not been defined");
}
if (aggregationInputStore.getPer() != null && aggregationInputStore.getWithin() != null) {
within = aggregationInputStore.getWithin();
per = aggregationInputStore.getPer();
} else if (aggregationInputStore.getPer() != null || aggregationInputStore.getWithin() != null) {
throw new OnDemandQueryCreationException(inputStore.getStoreId() + " should either have both 'within' and 'per' " + "defined or none.");
}
if (((AggregationInputStore) inputStore).getOnCondition() != null) {
onCondition = ((AggregationInputStore) inputStore).getOnCondition();
}
} else if (inputStore instanceof ConditionInputStore) {
if (((ConditionInputStore) inputStore).getOnCondition() != null) {
onCondition = ((ConditionInputStore) inputStore).getOnCondition();
}
}
List<VariableExpressionExecutor> variableExpressionExecutors = new ArrayList<>();
table = tableMap.get(inputStore.getStoreId());
if (table != null) {
return constructOnDemandQueryRuntime(table, onDemandQuery, tableMap, windowMap, metaPosition, onCondition, metaStreamEvent, variableExpressionExecutors, lockWrapper, siddhiQueryContext);
} else {
AggregationRuntime aggregation = aggregationMap.get(inputStore.getStoreId());
if (aggregation != null) {
return constructOnDemandQueryRuntime(aggregation, onDemandQuery, tableMap, windowMap, within, per, onCondition, metaStreamEvent, variableExpressionExecutors, lockWrapper, siddhiQueryContext);
} else {
Window window = windowMap.get(inputStore.getStoreId());
if (window != null) {
return constructOnDemandQueryRuntime(window, onDemandQuery, tableMap, windowMap, metaPosition, onCondition, metaStreamEvent, variableExpressionExecutors, lockWrapper, siddhiQueryContext);
} else {
throw new OnDemandQueryCreationException(inputStore.getStoreId() + " is neither a table, aggregation or window");
}
}
}
} finally {
SnapshotService.getSkipStateStorageThreadLocal().set(null);
}
case INSERT:
InsertIntoStream inserIntoStreamt = (InsertIntoStream) onDemandQuery.getOutputStream();
queryName = "store_insert_query_" + inserIntoStreamt.getId();
siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
onCondition = Expression.value(true);
return getOnDemandQueryRuntime(onDemandQuery, tableMap, windowMap, metaPosition, lockWrapper, metaStreamEvent, inserIntoStreamt, onCondition, siddhiQueryContext);
case DELETE:
DeleteStream deleteStream = (DeleteStream) onDemandQuery.getOutputStream();
queryName = "store_delete_query_" + deleteStream.getId();
siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
onCondition = deleteStream.getOnDeleteExpression();
return getOnDemandQueryRuntime(onDemandQuery, tableMap, windowMap, metaPosition, lockWrapper, metaStreamEvent, deleteStream, onCondition, siddhiQueryContext);
case UPDATE:
UpdateStream outputStream = (UpdateStream) onDemandQuery.getOutputStream();
queryName = "store_update_query_" + outputStream.getId();
siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
onCondition = outputStream.getOnUpdateExpression();
return getOnDemandQueryRuntime(onDemandQuery, tableMap, windowMap, metaPosition, lockWrapper, metaStreamEvent, outputStream, onCondition, siddhiQueryContext);
case UPDATE_OR_INSERT:
UpdateOrInsertStream onDemandQueryOutputStream = (UpdateOrInsertStream) onDemandQuery.getOutputStream();
queryName = "store_update_or_insert_query_" + onDemandQueryOutputStream.getId();
siddhiQueryContext = new SiddhiOnDemandQueryContext(siddhiAppContext, queryName, onDemandQueryString);
onCondition = onDemandQueryOutputStream.getOnUpdateExpression();
return getOnDemandQueryRuntime(onDemandQuery, tableMap, windowMap, metaPosition, lockWrapper, metaStreamEvent, onDemandQueryOutputStream, onCondition, siddhiQueryContext);
default:
return null;
}
}
use of io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream 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());
}
}
use of io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream 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;
}
use of io.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream 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);
}
}
Aggregations