Search in sources :

Example 21 with AbstractDefinition

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;
}
Also used : PersistedAggregationResultsProcessor(io.siddhi.core.aggregation.persistedaggregation.config.PersistedAggregationResultsProcessor) AbstractStreamProcessor(io.siddhi.core.query.processor.stream.AbstractStreamProcessor) Attribute(io.siddhi.query.api.definition.Attribute) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) ConfigReader(io.siddhi.core.util.config.ConfigReader) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition)

Example 22 with AbstractDefinition

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);
    }
}
Also used : Variable(io.siddhi.query.api.expression.Variable) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) Attribute(io.siddhi.query.api.definition.Attribute) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) Expression(io.siddhi.query.api.expression.Expression) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) IncrementalAttributeAggregator(io.siddhi.core.query.selector.attribute.aggregator.incremental.IncrementalAttributeAggregator) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) AttributeFunction(io.siddhi.query.api.expression.AttributeFunction) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor)

Example 23 with AbstractDefinition

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));
    }
}
Also used : StreamEventClonerHolder(io.siddhi.core.event.stream.holder.StreamEventClonerHolder) AnnotationHelper(io.siddhi.query.api.util.AnnotationHelper) SiddhiAppContext(io.siddhi.core.config.SiddhiAppContext) StreamEventFactory(io.siddhi.core.event.stream.StreamEventFactory) PrimaryKeyReferenceHolder(io.siddhi.core.table.holder.PrimaryKeyReferenceHolder) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) SiddhiConstants(io.siddhi.core.util.SiddhiConstants) Attribute(io.siddhi.query.api.definition.Attribute) Annotation(io.siddhi.query.api.annotation.Annotation) HashMap(java.util.HashMap) IndexEventHolderForCache(io.siddhi.core.table.holder.IndexEventHolderForCache) Element(io.siddhi.query.api.annotation.Element) IndexEventHolder(io.siddhi.core.table.holder.IndexEventHolder) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) ListEventHolder(io.siddhi.core.table.holder.ListEventHolder) Logger(org.apache.logging.log4j.Logger) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) ZeroStreamEventConverter(io.siddhi.core.event.stream.converter.ZeroStreamEventConverter) OperationNotSupportedException(io.siddhi.core.exception.OperationNotSupportedException) Map(java.util.Map) EventHolder(io.siddhi.core.table.holder.EventHolder) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) LogManager(org.apache.logging.log4j.LogManager) OperationNotSupportedException(io.siddhi.core.exception.OperationNotSupportedException) IndexEventHolder(io.siddhi.core.table.holder.IndexEventHolder) HashMap(java.util.HashMap) Attribute(io.siddhi.query.api.definition.Attribute) Element(io.siddhi.query.api.annotation.Element) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) PrimaryKeyReferenceHolder(io.siddhi.core.table.holder.PrimaryKeyReferenceHolder) Annotation(io.siddhi.query.api.annotation.Annotation) StreamEventClonerHolder(io.siddhi.core.event.stream.holder.StreamEventClonerHolder) ZeroStreamEventConverter(io.siddhi.core.event.stream.converter.ZeroStreamEventConverter) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) ListEventHolder(io.siddhi.core.table.holder.ListEventHolder) IndexEventHolderForCache(io.siddhi.core.table.holder.IndexEventHolderForCache) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 24 with AbstractDefinition

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);
}
Also used : SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition)

Aggregations

AbstractDefinition (io.siddhi.query.api.definition.AbstractDefinition)24 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)12 Attribute (io.siddhi.query.api.definition.Attribute)12 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)9 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)9 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)8 ArrayList (java.util.ArrayList)8 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)7 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)7 Variable (io.siddhi.query.api.expression.Variable)7 Expression (io.siddhi.query.api.expression.Expression)6 Table (io.siddhi.core.table.Table)5 OutputAttribute (io.siddhi.query.api.execution.query.selection.OutputAttribute)5 AttributeFunction (io.siddhi.query.api.expression.AttributeFunction)5 SiddhiAppContext (io.siddhi.core.config.SiddhiAppContext)4 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)4 SiddhiAppValidationException (io.siddhi.query.api.exception.SiddhiAppValidationException)4 AndConditionExpressionExecutor (io.siddhi.core.executor.condition.AndConditionExpressionExecutor)3 BoolConditionExpressionExecutor (io.siddhi.core.executor.condition.BoolConditionExpressionExecutor)3 ConditionExpressionExecutor (io.siddhi.core.executor.condition.ConditionExpressionExecutor)3