use of org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder in project siddhi by wso2.
the class StoreQueryParser method buildExpectedOutputAttributes.
private static List<Attribute> buildExpectedOutputAttributes(StoreQuery storeQuery, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, String queryName, int metaPosition, MatchingMetaInfoHolder metaStreamInfoHolder) {
MetaStateEvent selectMetaStateEvent = new MetaStateEvent(metaStreamInfoHolder.getMetaStateEvent().getMetaStreamEvents());
SelectorParser.parse(storeQuery.getSelector(), new ReturnStream(OutputStream.OutputEventType.CURRENT_EVENTS), siddhiAppContext, selectMetaStateEvent, tableMap, new ArrayList<>(), queryName, metaPosition);
return selectMetaStateEvent.getOutputStreamDefinition().getAttributeList();
}
use of org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder in project siddhi by wso2.
the class AggregationRuntime method compileExpression.
public CompiledCondition compileExpression(Expression expression, Within within, Expression per, MatchingMetaInfoHolder matchingMetaInfoHolder, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, String queryName, SiddhiAppContext siddhiAppContext) {
Map<TimePeriod.Duration, CompiledCondition> withinTableCompiledConditions = new HashMap<>();
CompiledCondition withinInMemoryCompileCondition;
CompiledCondition onCompiledCondition;
List<Attribute> additionalAttributes = new ArrayList<>();
// Define additional attribute list
additionalAttributes.add(new Attribute("_START", Attribute.Type.LONG));
additionalAttributes.add(new Attribute("_END", Attribute.Type.LONG));
// Get table definition. Table definitions for all the tables used to persist aggregates are similar.
// Therefore it's enough to get the definition from one table.
AbstractDefinition tableDefinition = ((Table) aggregationTables.values().toArray()[0]).getTableDefinition();
// Alter existing meta stream event or create new one if a meta stream doesn't exist
// After calling this method the original MatchingMetaInfoHolder's meta stream event would be altered
MetaStreamEvent newMetaStreamEventWithStartEnd = createNewMetaStreamEventWithStartEnd(matchingMetaInfoHolder, additionalAttributes);
MatchingMetaInfoHolder alteredMatchingMetaInfoHolder = null;
// Alter meta info holder to contain stream event and aggregate both when it's a store query
if (matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents().length == 1) {
matchingMetaInfoHolder = alterMetaInfoHolderForStoreQuery(newMetaStreamEventWithStartEnd, matchingMetaInfoHolder);
alteredMatchingMetaInfoHolder = matchingMetaInfoHolder;
}
// Create new MatchingMetaInfoHolder containing newMetaStreamEventWithStartEnd and table meta event
MatchingMetaInfoHolder streamTableMetaInfoHolderWithStartEnd = createNewStreamTableMetaInfoHolder(newMetaStreamEventWithStartEnd, tableDefinition);
// Create per expression executor
ExpressionExecutor perExpressionExecutor = ExpressionParser.parseExpression(per, matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
if (perExpressionExecutor.getReturnType() != Attribute.Type.STRING) {
throw new SiddhiAppCreationException("Query " + queryName + "'s per value expected a string but found " + perExpressionExecutor.getReturnType(), per.getQueryContextStartIndex(), per.getQueryContextEndIndex());
}
// Create within expression
Expression withinExpression;
Expression start = Expression.variable(additionalAttributes.get(0).getName());
Expression end = Expression.variable(additionalAttributes.get(1).getName());
Expression compareWithStartTime = Compare.compare(start, Compare.Operator.LESS_THAN_EQUAL, Expression.variable("AGG_TIMESTAMP"));
Expression compareWithEndTime = Compare.compare(Expression.variable("AGG_TIMESTAMP"), Compare.Operator.LESS_THAN, end);
withinExpression = Expression.and(compareWithStartTime, compareWithEndTime);
// Create start and end time expression
Expression startEndTimeExpression;
if (within.getTimeRange().size() == 1) {
startEndTimeExpression = new AttributeFunction("incrementalAggregator", "startTimeEndTime", within.getTimeRange().get(0));
} else {
// within.getTimeRange().size() == 2
startEndTimeExpression = new AttributeFunction("incrementalAggregator", "startTimeEndTime", within.getTimeRange().get(0), within.getTimeRange().get(1));
}
ExpressionExecutor startTimeEndTimeExpressionExecutor = ExpressionParser.parseExpression(startEndTimeExpression, matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
// These compile conditions are used to check whether the aggregates in tables are within the given duration.
for (Map.Entry<TimePeriod.Duration, Table> entry : aggregationTables.entrySet()) {
CompiledCondition withinTableCompileCondition = entry.getValue().compileCondition(withinExpression, streamTableMetaInfoHolderWithStartEnd, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
withinTableCompiledConditions.put(entry.getKey(), withinTableCompileCondition);
}
// Create compile condition for in-memory data.
// This compile condition is used to check whether the running aggregates (in-memory data)
// are within given duration
withinInMemoryCompileCondition = OperatorParser.constructOperator(new ComplexEventChunk<>(true), withinExpression, streamTableMetaInfoHolderWithStartEnd, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
// On compile condition.
// After finding all the aggregates belonging to within duration, the final on condition (given as
// "on stream1.name == aggregator.nickName ..." in the join query) must be executed on that data.
// This condition is used for that purpose.
onCompiledCondition = OperatorParser.constructOperator(new ComplexEventChunk<>(true), expression, matchingMetaInfoHolder, siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
return new IncrementalAggregateCompileCondition(withinTableCompiledConditions, withinInMemoryCompileCondition, onCompiledCondition, tableMetaStreamEvent, aggregateMetaSteamEvent, additionalAttributes, alteredMatchingMetaInfoHolder, perExpressionExecutor, startTimeEndTimeExpressionExecutor);
}
use of org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder in project siddhi by wso2.
the class AggregationRuntime method alterMetaInfoHolderForStoreQuery.
private static MatchingMetaInfoHolder alterMetaInfoHolderForStoreQuery(MetaStreamEvent newMetaStreamEventWithStartEnd, MatchingMetaInfoHolder matchingMetaInfoHolder) {
MetaStateEvent metaStateEvent = new MetaStateEvent(2);
MetaStreamEvent incomingMetaStreamEvent = matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvent(0);
metaStateEvent.addEvent(newMetaStreamEventWithStartEnd);
metaStateEvent.addEvent(incomingMetaStreamEvent);
return new MatchingMetaInfoHolder(metaStateEvent, 0, 1, newMetaStreamEventWithStartEnd.getLastInputDefinition(), incomingMetaStreamEvent.getLastInputDefinition(), UNKNOWN_STATE);
}
use of org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder in project siddhi by wso2.
the class AggregationRuntime method createNewStreamTableMetaInfoHolder.
private static MatchingMetaInfoHolder createNewStreamTableMetaInfoHolder(MetaStreamEvent metaStreamEventWithStartEnd, AbstractDefinition tableDefinition) {
MetaStateEvent metaStateEvent = new MetaStateEvent(2);
MetaStreamEvent metaStreamEventForTable = new MetaStreamEvent();
metaStreamEventForTable.setEventType(MetaStreamEvent.EventType.TABLE);
initMetaStreamEvent(metaStreamEventForTable, tableDefinition);
metaStateEvent.addEvent(metaStreamEventWithStartEnd);
metaStateEvent.addEvent(metaStreamEventForTable);
return new MatchingMetaInfoHolder(metaStateEvent, 0, 1, metaStreamEventWithStartEnd.getLastInputDefinition(), tableDefinition, UNKNOWN_STATE);
}
use of org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder in project siddhi by wso2.
the class CollectionExpressionParser method buildMultiPrimaryKeyExpressionExecutors.
private static Map<String, ExpressionExecutor> buildMultiPrimaryKeyExpressionExecutors(CollectionExpression collectionExpression, MatchingMetaInfoHolder matchingMetaInfoHolder, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, SiddhiAppContext siddhiAppContext, String queryName) {
if (collectionExpression instanceof AndMultiPrimaryKeyCollectionExpression) {
CollectionExpression leftCollectionExpression = ((AndMultiPrimaryKeyCollectionExpression) collectionExpression).getLeftCollectionExpression();
CollectionExpression rightCollectionExpression = ((AndMultiPrimaryKeyCollectionExpression) collectionExpression).getRightCollectionExpression();
Map<String, ExpressionExecutor> expressionExecutors = buildMultiPrimaryKeyExpressionExecutors(leftCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, queryName);
expressionExecutors.putAll(buildMultiPrimaryKeyExpressionExecutors(rightCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, queryName));
return expressionExecutors;
} else if (collectionExpression instanceof AndCollectionExpression) {
CollectionExpression leftCollectionExpression = ((AndCollectionExpression) collectionExpression).getLeftCollectionExpression();
CollectionExpression rightCollectionExpression = ((AndCollectionExpression) collectionExpression).getLeftCollectionExpression();
Map<String, ExpressionExecutor> expressionExecutors = buildMultiPrimaryKeyExpressionExecutors(leftCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, queryName);
expressionExecutors.putAll(buildMultiPrimaryKeyExpressionExecutors(rightCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, queryName));
return expressionExecutors;
} else if (collectionExpression instanceof CompareCollectionExpression) {
if (((CompareCollectionExpression) collectionExpression).getOperator() == Compare.Operator.EQUAL) {
CollectionExpression attributeCollectionExpression = ((CompareCollectionExpression) collectionExpression).getAttributeCollectionExpression();
if (attributeCollectionExpression instanceof AttributeCollectionExpression) {
String attribue = ((AttributeCollectionExpression) attributeCollectionExpression).getAttribute();
CollectionExpression valueCollectionExpression = ((CompareCollectionExpression) collectionExpression).getValueCollectionExpression();
ExpressionExecutor valueExpressionExecutor = ExpressionParser.parseExpression(valueCollectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
Map<String, ExpressionExecutor> expressionExecutors = new HashMap<String, ExpressionExecutor>();
expressionExecutors.put(attribue, valueExpressionExecutor);
return expressionExecutors;
} else {
throw new SiddhiAppCreationException("Only attribute EQUAL " + "comparision supported for multiple primary key optimization, " + "but found '" + attributeCollectionExpression.getClass() + "'", collectionExpression.getExpression().getQueryContextStartIndex(), collectionExpression.getExpression().getQueryContextEndIndex());
}
} else {
throw new SiddhiAppCreationException("Only '" + Compare.Operator.EQUAL + "' supported for multiple " + "primary key for multiple primary key optimization, but found '" + ((CompareCollectionExpression) collectionExpression).getOperator() + "'", collectionExpression.getExpression().getQueryContextStartIndex(), collectionExpression.getExpression().getQueryContextEndIndex());
}
} else {
// Attribute Collection
throw new SiddhiAppCreationException("Only 'AND' and '" + Compare.Operator.EQUAL + "' operators are " + "supported for multiple primary key optimization, but found '" + ((CompareCollectionExpression) collectionExpression).getOperator() + "'", collectionExpression.getExpression().getQueryContextStartIndex(), collectionExpression.getExpression().getQueryContextEndIndex());
}
}
Aggregations