use of io.siddhi.query.api.definition.AbstractDefinition in project siddhi by wso2.
the class AggregationParser method getCudProcessor.
private static Processor getCudProcessor(StreamHandler streamHandler, SiddhiQueryContext siddhiQueryContext, MetaStreamEvent metaStreamEvent, ExpressionExecutor[] attributeExpressionExecutors, TimePeriod.Duration duration) {
ConfigReader configReader = siddhiQueryContext.getSiddhiContext().getConfigManager().generateConfigReader(((StreamFunction) streamHandler).getNamespace(), ((StreamFunction) streamHandler).getName());
AbstractStreamProcessor abstractStreamProcessor;
abstractStreamProcessor = (StreamProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, StreamProcessorExtensionHolder.getInstance(siddhiQueryContext.getSiddhiAppContext()));
abstractStreamProcessor.initProcessor(metaStreamEvent, attributeExpressionExecutors, configReader, false, false, false, streamHandler, siddhiQueryContext);
if (metaStreamEvent.getInputDefinitions().size() == 2) {
AbstractDefinition outputDefinition = metaStreamEvent.getInputDefinitions().get(1);
List<Attribute> outputAttributes = outputDefinition.getAttributeList();
for (Attribute attribute : outputAttributes) {
metaStreamEvent.addOutputData(attribute);
}
metaStreamEvent.setOutputDefinition((StreamDefinition) outputDefinition);
}
abstractStreamProcessor.constructStreamEventPopulater(metaStreamEvent, 0);
abstractStreamProcessor.setNextProcessor(new PersistedAggregationResultsProcessor(duration));
return abstractStreamProcessor;
}
use of io.siddhi.query.api.definition.AbstractDefinition in project siddhi by wso2.
the class AggregationParser method populateIncomingAggregatorsAndExecutors.
private static void populateIncomingAggregatorsAndExecutors(AggregationDefinition aggregationDefinition, SiddhiQueryContext siddhiQueryContext, Map<String, Table> tableMap, List<VariableExpressionExecutor> incomingVariableExpressionExecutors, MetaStreamEvent incomingMetaStreamEvent, List<ExpressionExecutor> incomingExpressionExecutors, List<IncrementalAttributeAggregator> incrementalAttributeAggregators, List<Variable> groupByVariableList, List<Expression> outputExpressions, boolean isProcessingOnExternalTime, boolean isDistributed, String shardId) {
boolean isLatestEventAdded = false;
ExpressionExecutor timestampExecutor = getTimeStampExecutor(siddhiQueryContext, tableMap, incomingVariableExpressionExecutors, incomingMetaStreamEvent);
Attribute timestampAttribute = new Attribute(AGG_START_TIMESTAMP_COL, Attribute.Type.LONG);
incomingMetaStreamEvent.addOutputData(timestampAttribute);
incomingExpressionExecutors.add(timestampExecutor);
if (isDistributed) {
ExpressionExecutor nodeIdExpExecutor = new ConstantExpressionExecutor(shardId, Attribute.Type.STRING);
incomingExpressionExecutors.add(nodeIdExpExecutor);
incomingMetaStreamEvent.addOutputData(new Attribute(AGG_SHARD_ID_COL, Attribute.Type.STRING));
}
ExpressionExecutor externalTimestampExecutor = null;
if (isProcessingOnExternalTime) {
Expression externalTimestampExpression = aggregationDefinition.getAggregateAttribute();
externalTimestampExecutor = ExpressionParser.parseExpression(externalTimestampExpression, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
if (externalTimestampExecutor.getReturnType() == Attribute.Type.STRING) {
Expression expression = AttributeFunction.function("incrementalAggregator", "timestampInMilliseconds", externalTimestampExpression);
externalTimestampExecutor = ExpressionParser.parseExpression(expression, incomingMetaStreamEvent, 0, tableMap, incomingVariableExpressionExecutors, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
} else if (externalTimestampExecutor.getReturnType() != Attribute.Type.LONG) {
throw new SiddhiAppCreationException("Aggregation Definition '" + aggregationDefinition.getId() + "'s timestamp attribute expects " + "long or string, but found " + externalTimestampExecutor.getReturnType() + ". Hence, can't " + "create the siddhi app '" + siddhiQueryContext.getSiddhiAppContext().getName() + "'", externalTimestampExpression.getQueryContextStartIndex(), externalTimestampExpression.getQueryContextEndIndex());
}
Attribute externalTimestampAttribute = new Attribute(AGG_EXTERNAL_TIMESTAMP_COL, Attribute.Type.LONG);
incomingMetaStreamEvent.addOutputData(externalTimestampAttribute);
incomingExpressionExecutors.add(externalTimestampExecutor);
}
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, false, 0, ProcessingMode.BATCH, false, siddhiQueryContext));
}
// Add AGG_TIMESTAMP to output as well
aggregationDefinition.getAttributeList().add(timestampAttribute);
// check and set whether the aggregation in happened on an external timestamp
if (isProcessingOnExternalTime) {
outputExpressions.add(Expression.variable(AGG_EXTERNAL_TIMESTAMP_COL));
} else {
outputExpressions.add(Expression.variable(AGG_START_TIMESTAMP_COL));
}
for (OutputAttribute outputAttribute : aggregationDefinition.getSelector().getSelectionList()) {
Expression expression = outputAttribute.getExpression();
// If the select contains the aggregation function expression type will be AttributeFunction
if (expression instanceof AttributeFunction) {
IncrementalAttributeAggregator incrementalAggregator = null;
try {
incrementalAggregator = (IncrementalAttributeAggregator) SiddhiClassLoader.loadExtensionImplementation(new AttributeFunction("incrementalAggregator", ((AttributeFunction) expression).getName(), ((AttributeFunction) expression).getParameters()), IncrementalAttributeAggregatorExtensionHolder.getInstance(siddhiQueryContext.getSiddhiAppContext()));
} catch (SiddhiAppCreationException ex) {
try {
SiddhiClassLoader.loadExtensionImplementation((AttributeFunction) expression, FunctionExecutorExtensionHolder.getInstance(siddhiQueryContext.getSiddhiAppContext()));
processAggregationSelectors(aggregationDefinition, siddhiQueryContext, tableMap, incomingVariableExpressionExecutors, incomingMetaStreamEvent, incomingExpressionExecutors, outputExpressions, outputAttribute, expression);
} 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 '" + siddhiQueryContext.getName() + "' processing.", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
}
aggregationDefinition.getAttributeList().add(new Attribute(outputAttribute.getRename(), groupByAttribute.getType()));
outputExpressions.add(Expression.variable(groupByAttribute.getName()));
} else {
isLatestEventAdded = true;
processAggregationSelectors(aggregationDefinition, siddhiQueryContext, tableMap, incomingVariableExpressionExecutors, incomingMetaStreamEvent, incomingExpressionExecutors, outputExpressions, outputAttribute, expression);
}
}
}
if (isProcessingOnExternalTime && isLatestEventAdded) {
Attribute lastEventTimeStamp = new Attribute(AGG_LAST_TIMESTAMP_COL, Attribute.Type.LONG);
incomingMetaStreamEvent.addOutputData(lastEventTimeStamp);
incomingExpressionExecutors.add(externalTimestampExecutor);
}
}
use of io.siddhi.query.api.definition.AbstractDefinition in project siddhi by wso2.
the class EventHolderPasser method parse.
public static EventHolder parse(AbstractDefinition tableDefinition, StreamEventFactory tableStreamEventFactory, SiddhiAppContext siddhiAppContext, boolean isCacheTable) {
ZeroStreamEventConverter eventConverter = new ZeroStreamEventConverter();
PrimaryKeyReferenceHolder[] primaryKeyReferenceHolders = null;
Map<String, Integer> indexMetaData = new HashMap<String, Integer>();
// primaryKey.
Annotation primaryKeyAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_PRIMARY_KEY, tableDefinition.getAnnotations());
if (primaryKeyAnnotation != null) {
if (primaryKeyAnnotation.getElements().size() == 0) {
throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_PRIMARY_KEY + " annotation " + "contains " + primaryKeyAnnotation.getElements().size() + " element, at '" + tableDefinition.getId() + "'");
}
primaryKeyReferenceHolders = primaryKeyAnnotation.getElements().stream().map(element -> element.getValue().trim()).map(key -> new PrimaryKeyReferenceHolder(key, tableDefinition.getAttributePosition(key))).toArray(PrimaryKeyReferenceHolder[]::new);
}
for (Annotation indexAnnotation : AnnotationHelper.getAnnotations(SiddhiConstants.ANNOTATION_INDEX, tableDefinition.getAnnotations())) {
if (indexAnnotation.getElements().size() == 0) {
throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_INDEX + " annotation of " + "in-memory table should contain only one index element, but found " + indexAnnotation.getElements().size() + " element", indexAnnotation.getQueryContextStartIndex(), indexAnnotation.getQueryContextEndIndex());
} else if (indexAnnotation.getElements().size() > 1) {
throw new SiddhiAppValidationException(SiddhiConstants.ANNOTATION_INDEX + " annotation of the " + "in-memory table should only contain one index element but found " + indexAnnotation.getElements().size() + " elements. To use multiple indexes, " + "define multiple '@index(<index key>)' annotations with one index element " + "per each index key", indexAnnotation.getQueryContextStartIndex(), indexAnnotation.getQueryContextEndIndex());
}
for (Element element : indexAnnotation.getElements()) {
Integer previousValue = indexMetaData.put(element.getValue().trim(), tableDefinition.getAttributePosition(element.getValue().trim()));
if (previousValue != null) {
throw new SiddhiAppValidationException("Multiple " + SiddhiConstants.ANNOTATION_INDEX + " " + "annotations defined with same attribute '" + element.getValue().trim() + "', at '" + tableDefinition.getId() + "'", indexAnnotation.getQueryContextStartIndex(), indexAnnotation.getQueryContextEndIndex());
}
}
}
// not support indexBy.
Annotation indexByAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_INDEX_BY, tableDefinition.getAnnotations());
if (indexByAnnotation != null) {
throw new OperationNotSupportedException(SiddhiConstants.ANNOTATION_INDEX_BY + " annotation is not " + "supported anymore, please use @PrimaryKey or @Index annotations instead," + " at '" + tableDefinition.getId() + "'");
}
if (primaryKeyReferenceHolders != null || indexMetaData.size() > 0) {
boolean isNumeric = false;
if (primaryKeyReferenceHolders != null) {
if (primaryKeyReferenceHolders.length == 1) {
Attribute.Type type = tableDefinition.getAttributeType(primaryKeyReferenceHolders[0].getPrimaryKeyAttribute());
if (type == Attribute.Type.DOUBLE || type == Attribute.Type.FLOAT || type == Attribute.Type.INT || type == Attribute.Type.LONG) {
isNumeric = true;
}
}
}
if (isCacheTable) {
return new IndexEventHolderForCache(tableStreamEventFactory, eventConverter, primaryKeyReferenceHolders, isNumeric, indexMetaData, tableDefinition, siddhiAppContext);
} else {
return new IndexEventHolder(tableStreamEventFactory, eventConverter, primaryKeyReferenceHolders, isNumeric, indexMetaData, tableDefinition, siddhiAppContext);
}
} else {
MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
for (Attribute attribute : tableDefinition.getAttributeList()) {
metaStreamEvent.addOutputData(attribute);
}
StreamEventCloner streamEventCloner = new StreamEventCloner(metaStreamEvent, tableStreamEventFactory);
return new ListEventHolder(tableStreamEventFactory, eventConverter, new StreamEventClonerHolder(streamEventCloner));
}
}
use of io.siddhi.query.api.definition.AbstractDefinition in project siddhi by wso2.
the class SingleInputStreamParser method initMetaStreamEvent.
/**
* Method to generate MetaStreamEvent reagent to the given input stream. Empty definition will be created and
* definition and reference is will be set accordingly in this method.
*
* @param inputStream InputStream
* @param streamDefinitionMap StreamDefinition Map
* @param tableDefinitionMap TableDefinition Map
* @param aggregationDefinitionMap AggregationDefinition Map
* @param multiValue Event has the possibility to produce multiple values
* @param metaStreamEvent MetaStreamEvent
*/
private static void initMetaStreamEvent(SingleInputStream inputStream, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, boolean multiValue, MetaStreamEvent metaStreamEvent) {
String streamId = inputStream.getStreamId();
if (!inputStream.isInnerStream() && windowDefinitionMap != null && windowDefinitionMap.containsKey(streamId)) {
AbstractDefinition inputDefinition = windowDefinitionMap.get(streamId);
if (!metaStreamEvent.getInputDefinitions().contains(inputDefinition)) {
metaStreamEvent.addInputDefinition(inputDefinition);
}
} else if (streamDefinitionMap != null && streamDefinitionMap.containsKey(streamId)) {
AbstractDefinition inputDefinition = streamDefinitionMap.get(streamId);
metaStreamEvent.addInputDefinition(inputDefinition);
} else if (!inputStream.isInnerStream() && tableDefinitionMap != null && tableDefinitionMap.containsKey(streamId)) {
AbstractDefinition inputDefinition = tableDefinitionMap.get(streamId);
metaStreamEvent.addInputDefinition(inputDefinition);
} else if (!inputStream.isInnerStream() && aggregationDefinitionMap != null && aggregationDefinitionMap.containsKey(streamId)) {
AbstractDefinition inputDefinition = aggregationDefinitionMap.get(streamId);
metaStreamEvent.addInputDefinition(inputDefinition);
} else {
throw new SiddhiAppCreationException("Stream/table/window/aggregation definition with ID '" + inputStream.getStreamId() + "' has not been defined", inputStream.getQueryContextStartIndex(), inputStream.getQueryContextEndIndex());
}
if ((inputStream.getStreamReferenceId() != null) && !(inputStream.getStreamId()).equals(inputStream.getStreamReferenceId())) {
// if ref id is provided
metaStreamEvent.setInputReferenceId(inputStream.getStreamReferenceId());
}
metaStreamEvent.setMultiValue(multiValue);
}
Aggregations