use of org.wso2.siddhi.core.config.SiddhiAppContext in project siddhi by wso2.
the class AggregationParser method setTimeStampTimeZoneExecutors.
private static ExpressionExecutor[] setTimeStampTimeZoneExecutors(AggregationDefinition aggregationDefinition, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, List<VariableExpressionExecutor> variableExpressionExecutors, String aggregatorName, MetaStreamEvent metaStreamEvent) {
Expression timestampExpression = aggregationDefinition.getAggregateAttribute();
Expression timeZoneExpression;
ExpressionExecutor timestampExecutor;
ExpressionExecutor timeZoneExecutor;
boolean isSystemTimeBased = false;
// When execution is based on system time, the system's time zone would be used.
if (timestampExpression == null) {
isSystemTimeBased = true;
timestampExpression = AttributeFunction.function("currentTimeMillis", null);
}
timestampExecutor = ExpressionParser.parseExpression(timestampExpression, metaStreamEvent, 0, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
if (timestampExecutor.getReturnType() == Attribute.Type.STRING) {
Expression expression = AttributeFunction.function("incrementalAggregator", "timestampInMilliseconds", timestampExpression);
timestampExecutor = ExpressionParser.parseExpression(expression, metaStreamEvent, 0, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
timeZoneExpression = AttributeFunction.function("incrementalAggregator", "getTimeZone", timestampExpression);
timeZoneExecutor = ExpressionParser.parseExpression(timeZoneExpression, metaStreamEvent, 0, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
} else if (timestampExecutor.getReturnType() == Attribute.Type.LONG) {
if (isSystemTimeBased) {
timeZoneExpression = AttributeFunction.function("incrementalAggregator", "getTimeZone", null);
timeZoneExecutor = ExpressionParser.parseExpression(timeZoneExpression, metaStreamEvent, 0, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
} else {
// If long value is given, it's assumed that the
timeZoneExpression = Expression.value("+00:00");
// time zone is GMT
timeZoneExecutor = ExpressionParser.parseExpression(timeZoneExpression, metaStreamEvent, 0, tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
}
} else {
throw new SiddhiAppCreationException("AggregationDefinition '" + aggregationDefinition.getId() + "'s aggregateAttribute expects " + "long or string, but found " + timestampExecutor.getReturnType() + ". " + "Hence, can't create the siddhi app '" + siddhiAppContext.getName() + "'", timestampExpression.getQueryContextStartIndex(), timestampExpression.getQueryContextEndIndex());
}
return new ExpressionExecutor[] { timestampExecutor, timeZoneExecutor };
}
use of org.wso2.siddhi.core.config.SiddhiAppContext in project siddhi by wso2.
the class AggregationParser method populateIncomingAggregatorsAndExecutors.
private static void populateIncomingAggregatorsAndExecutors(AggregationDefinition aggregationDefinition, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, List<VariableExpressionExecutor> incomingVariableExpressionExecutors, String aggregatorName, MetaStreamEvent incomingMetaStreamEvent, List<ExpressionExecutor> incomingExpressionExecutors, List<IncrementalAttributeAggregator> incrementalAttributeAggregators, List<Variable> groupByVariableList, List<Expression> outputExpressions) {
ExpressionExecutor[] timeStampTimeZoneExecutors = setTimeStampTimeZoneExecutors(aggregationDefinition, siddhiAppContext, tableMap, incomingVariableExpressionExecutors, aggregatorName, incomingMetaStreamEvent);
ExpressionExecutor timestampExecutor = timeStampTimeZoneExecutors[0];
ExpressionExecutor timeZoneExecutor = timeStampTimeZoneExecutors[1];
Attribute timestampAttribute = new Attribute("AGG_TIMESTAMP", Attribute.Type.LONG);
incomingMetaStreamEvent.addOutputData(timestampAttribute);
incomingExpressionExecutors.add(timestampExecutor);
incomingMetaStreamEvent.addOutputData(new Attribute("AGG_TIMEZONE", Attribute.Type.STRING));
incomingExpressionExecutors.add(timeZoneExecutor);
AbstractDefinition incomingLastInputStreamDefinition = incomingMetaStreamEvent.getLastInputDefinition();
for (Variable groupByVariable : groupByVariableList) {
incomingMetaStreamEvent.addOutputData(incomingLastInputStreamDefinition.getAttributeList().get(incomingLastInputStreamDefinition.getAttributePosition(groupByVariable.getAttributeName())));
incomingExpressionExecutors.add(ExpressionParser.parseExpression(groupByVariable, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName));
}
// Add AGG_TIMESTAMP to output as well
outputExpressions.add(Expression.variable("AGG_TIMESTAMP"));
aggregationDefinition.getAttributeList().add(timestampAttribute);
for (OutputAttribute outputAttribute : aggregationDefinition.getSelector().getSelectionList()) {
Expression expression = outputAttribute.getExpression();
if (expression instanceof AttributeFunction) {
IncrementalAttributeAggregator incrementalAggregator = null;
try {
incrementalAggregator = (IncrementalAttributeAggregator) SiddhiClassLoader.loadExtensionImplementation(new AttributeFunction("incrementalAggregator", ((AttributeFunction) expression).getName(), ((AttributeFunction) expression).getParameters()), IncrementalAttributeAggregatorExtensionHolder.getInstance(siddhiAppContext));
} catch (SiddhiAppCreationException ex) {
try {
SiddhiClassLoader.loadExtensionImplementation((AttributeFunction) expression, FunctionExecutorExtensionHolder.getInstance(siddhiAppContext));
ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(expression, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
incomingExpressionExecutors.add(expressionExecutor);
incomingMetaStreamEvent.addOutputData(new Attribute(outputAttribute.getRename(), expressionExecutor.getReturnType()));
aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), expressionExecutor.getReturnType()));
outputExpressions.add(Expression.variable(outputAttribute.getRename()));
} catch (SiddhiAppCreationException e) {
throw new SiddhiAppCreationException("'" + ((AttributeFunction) expression).getName() + "' is neither a incremental attribute aggregator extension or a function" + " extension", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
}
}
if (incrementalAggregator != null) {
initIncrementalAttributeAggregator(incomingLastInputStreamDefinition, (AttributeFunction) expression, incrementalAggregator);
incrementalAttributeAggregators.add(incrementalAggregator);
aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), incrementalAggregator.getReturnType()));
outputExpressions.add(incrementalAggregator.aggregate());
}
} else {
if (expression instanceof Variable && groupByVariableList.contains(expression)) {
Attribute groupByAttribute = null;
for (Attribute attribute : incomingMetaStreamEvent.getOutputData()) {
if (attribute.getName().equals(((Variable) expression).getAttributeName())) {
groupByAttribute = attribute;
break;
}
}
if (groupByAttribute == null) {
throw new SiddhiAppCreationException("Expected GroupBy attribute '" + ((Variable) expression).getAttributeName() + "' not used in aggregation '" + aggregatorName + "' processing.", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
}
aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), groupByAttribute.getType()));
outputExpressions.add(Expression.variable(groupByAttribute.getName()));
} else {
ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(expression, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, siddhiAppContext, false, 0, aggregatorName);
incomingExpressionExecutors.add(expressionExecutor);
incomingMetaStreamEvent.addOutputData(new Attribute(outputAttribute.getRename(), expressionExecutor.getReturnType()));
aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), expressionExecutor.getReturnType()));
outputExpressions.add(Expression.variable(outputAttribute.getRename()));
}
}
}
}
use of org.wso2.siddhi.core.config.SiddhiAppContext in project siddhi by wso2.
the class AggregationParser method buildIncrementalExecutors.
private static Map<TimePeriod.Duration, IncrementalExecutor> buildIncrementalExecutors(boolean isProcessingOnExternalTime, MetaStreamEvent processedMetaStreamEvent, List<ExpressionExecutor> processExpressionExecutors, GroupByKeyGenerator groupByKeyGenerator, int bufferSize, boolean ignoreEventsOlderThanBuffer, List<TimePeriod.Duration> incrementalDurations, Map<TimePeriod.Duration, Table> aggregationTables, SiddhiAppContext siddhiAppContext, String aggregatorName) {
Map<TimePeriod.Duration, IncrementalExecutor> incrementalExecutorMap = new HashMap<>();
// Create incremental executors
IncrementalExecutor child;
IncrementalExecutor root = null;
for (int i = incrementalDurations.size() - 1; i >= 0; i--) {
// Base incremental expression executors created using new meta
boolean isRoot = false;
if (i == 0) {
isRoot = true;
}
child = root;
TimePeriod.Duration duration = incrementalDurations.get(i);
IncrementalExecutor incrementalExecutor = new IncrementalExecutor(duration, cloneExpressionExecutors(processExpressionExecutors), groupByKeyGenerator, processedMetaStreamEvent, bufferSize, ignoreEventsOlderThanBuffer, child, isRoot, aggregationTables.get(duration), isProcessingOnExternalTime, siddhiAppContext, aggregatorName);
incrementalExecutorMap.put(duration, incrementalExecutor);
root = incrementalExecutor;
}
return incrementalExecutorMap;
}
use of org.wso2.siddhi.core.config.SiddhiAppContext in project siddhi by wso2.
the class AggregationParser method constructProcessExpressionExecutors.
private static List<ExpressionExecutor> constructProcessExpressionExecutors(SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, String aggregatorName, int baseAggregatorBeginIndex, List<Expression> finalBaseAggregators, StreamDefinition incomingOutputStreamDefinition, MetaStreamEvent processedMetaStreamEvent, List<VariableExpressionExecutor> processVariableExpressionExecutors, boolean groupBy) {
List<ExpressionExecutor> processExpressionExecutors = new ArrayList<>();
List<Attribute> attributeList = incomingOutputStreamDefinition.getAttributeList();
for (int i = 0; i < baseAggregatorBeginIndex; i++) {
Attribute attribute = attributeList.get(i);
VariableExpressionExecutor variableExpressionExecutor = (VariableExpressionExecutor) ExpressionParser.parseExpression(new Variable(attribute.getName()), processedMetaStreamEvent, 0, tableMap, processVariableExpressionExecutors, siddhiAppContext, groupBy, 0, aggregatorName);
processExpressionExecutors.add(variableExpressionExecutor);
}
for (Expression expression : finalBaseAggregators) {
ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(expression, processedMetaStreamEvent, 0, tableMap, processVariableExpressionExecutors, siddhiAppContext, groupBy, 0, aggregatorName);
processExpressionExecutors.add(expressionExecutor);
}
return processExpressionExecutors;
}
use of org.wso2.siddhi.core.config.SiddhiAppContext 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