Search in sources :

Example 1 with AttributeProcessor

use of io.siddhi.core.query.selector.attribute.processor.AttributeProcessor in project siddhi by wso2.

the class QuerySelector method processNoGroupBy.

private ComplexEventChunk processNoGroupBy(ComplexEventChunk complexEventChunk) {
    complexEventChunk.reset();
    synchronized (this) {
        while (complexEventChunk.hasNext()) {
            ComplexEvent event = complexEventChunk.next();
            switch(event.getType()) {
                case CURRENT:
                case EXPIRED:
                    eventPopulator.populateStateEvent(event);
                    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                        attributeProcessor.process(event);
                    }
                    if (((event.getType() != StreamEvent.Type.CURRENT || !currentOn) && (event.getType() != StreamEvent.Type.EXPIRED || !expiredOn)) || ((havingConditionExecutor != null && !havingConditionExecutor.execute(event)))) {
                        complexEventChunk.remove();
                    }
                    break;
                case RESET:
                    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                        attributeProcessor.process(event);
                    }
                    break;
                case TIMER:
                    complexEventChunk.remove();
                    break;
            }
        }
    }
    if (isOrderBy) {
        orderEventChunk(complexEventChunk);
    }
    if (offset != SiddhiConstants.UNKNOWN_STATE) {
        offsetEventChunk(complexEventChunk);
    }
    if (limit != SiddhiConstants.UNKNOWN_STATE) {
        limitEventChunk(complexEventChunk);
    }
    complexEventChunk.reset();
    if (complexEventChunk.hasNext()) {
        return complexEventChunk;
    }
    return null;
}
Also used : ComplexEvent(io.siddhi.core.event.ComplexEvent) GroupedComplexEvent(io.siddhi.core.event.GroupedComplexEvent) AttributeProcessor(io.siddhi.core.query.selector.attribute.processor.AttributeProcessor)

Example 2 with AttributeProcessor

use of io.siddhi.core.query.selector.attribute.processor.AttributeProcessor in project siddhi by wso2.

the class QuerySelector method processInBatchGroupBy.

private ComplexEventChunk processInBatchGroupBy(ComplexEventChunk complexEventChunk) {
    Map<String, ComplexEvent> groupedEvents = new LinkedHashMap<String, ComplexEvent>();
    complexEventChunk.reset();
    synchronized (this) {
        while (complexEventChunk.hasNext()) {
            ComplexEvent event = complexEventChunk.next();
            switch(event.getType()) {
                case CURRENT:
                case EXPIRED:
                    eventPopulator.populateStateEvent(event);
                    String groupByKey = groupByKeyGenerator.constructEventKey(event);
                    SiddhiAppContext.startGroupByFlow(groupByKey);
                    try {
                        for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                            attributeProcessor.process(event);
                        }
                        if (!(havingConditionExecutor != null && !havingConditionExecutor.execute(event))) {
                            if ((event.getType() == StreamEvent.Type.CURRENT && currentOn) || (event.getType() == StreamEvent.Type.EXPIRED && expiredOn)) {
                                complexEventChunk.remove();
                                groupedEvents.put(groupByKey, event);
                            }
                        }
                    } finally {
                        SiddhiAppContext.stopGroupByFlow();
                    }
                    break;
                case TIMER:
                    break;
                case RESET:
                    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                        attributeProcessor.process(event);
                    }
                    break;
            }
        }
    }
    if (groupedEvents.size() != 0) {
        complexEventChunk.clear();
        for (Map.Entry<String, ComplexEvent> groupedEventEntry : groupedEvents.entrySet()) {
            complexEventChunk.add(new GroupedComplexEvent(groupedEventEntry.getKey(), groupedEventEntry.getValue()));
        }
        if (isOrderBy) {
            orderEventChunk(complexEventChunk);
        }
        if (offset != SiddhiConstants.UNKNOWN_STATE) {
            offsetEventChunk(complexEventChunk);
        }
        if (limit != SiddhiConstants.UNKNOWN_STATE) {
            limitEventChunk(complexEventChunk);
        }
        complexEventChunk.reset();
        return complexEventChunk;
    }
    return null;
}
Also used : ComplexEvent(io.siddhi.core.event.ComplexEvent) GroupedComplexEvent(io.siddhi.core.event.GroupedComplexEvent) GroupedComplexEvent(io.siddhi.core.event.GroupedComplexEvent) AttributeProcessor(io.siddhi.core.query.selector.attribute.processor.AttributeProcessor) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with AttributeProcessor

use of io.siddhi.core.query.selector.attribute.processor.AttributeProcessor in project siddhi by wso2.

the class QuerySelector method processGroupBy.

private ComplexEventChunk<ComplexEvent> processGroupBy(ComplexEventChunk complexEventChunk) {
    complexEventChunk.reset();
    ComplexEventChunk<ComplexEvent> currentComplexEventChunk = new ComplexEventChunk<ComplexEvent>();
    synchronized (this) {
        int limitCount = 0;
        while (complexEventChunk.hasNext()) {
            ComplexEvent event = complexEventChunk.next();
            switch(event.getType()) {
                case CURRENT:
                case EXPIRED:
                    eventPopulator.populateStateEvent(event);
                    String groupByKey = groupByKeyGenerator.constructEventKey(event);
                    SiddhiAppContext.startGroupByFlow(groupByKey);
                    try {
                        for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                            attributeProcessor.process(event);
                        }
                        if ((event.getType() == StreamEvent.Type.CURRENT && currentOn) || (event.getType() == StreamEvent.Type.EXPIRED && expiredOn)) {
                            if (!(havingConditionExecutor != null && !havingConditionExecutor.execute(event))) {
                                complexEventChunk.remove();
                                if (limit == SiddhiConstants.UNKNOWN_STATE) {
                                    currentComplexEventChunk.add(new GroupedComplexEvent(groupByKey, event));
                                } else {
                                    if (limitCount < limit) {
                                        currentComplexEventChunk.add(new GroupedComplexEvent(groupByKey, event));
                                        limitCount++;
                                    }
                                }
                            }
                        }
                    } finally {
                        SiddhiAppContext.stopGroupByFlow();
                    }
                    break;
                case TIMER:
                    break;
                case RESET:
                    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                        attributeProcessor.process(event);
                    }
                    break;
            }
        }
    }
    if (isOrderBy) {
        orderEventChunk(complexEventChunk);
    }
    if (offset != SiddhiConstants.UNKNOWN_STATE) {
        offsetEventChunk(complexEventChunk);
    }
    if (limit != SiddhiConstants.UNKNOWN_STATE) {
        limitEventChunk(complexEventChunk);
    }
    currentComplexEventChunk.reset();
    if (currentComplexEventChunk.hasNext()) {
        return currentComplexEventChunk;
    }
    return null;
}
Also used : ComplexEvent(io.siddhi.core.event.ComplexEvent) GroupedComplexEvent(io.siddhi.core.event.GroupedComplexEvent) ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) GroupedComplexEvent(io.siddhi.core.event.GroupedComplexEvent) AttributeProcessor(io.siddhi.core.query.selector.attribute.processor.AttributeProcessor)

Example 4 with AttributeProcessor

use of io.siddhi.core.query.selector.attribute.processor.AttributeProcessor in project siddhi by wso2.

the class WrappedSnapshotOutputRateLimiter method init.

public void init(int outPutAttributeSize, List<AttributeProcessor> attributeProcessorList, MetaComplexEvent metaComplexEvent) {
    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
        if (attributeProcessor.getExpressionExecutor() instanceof AttributeAggregatorExecutor<?>) {
            aggregateAttributePositionList.add(attributeProcessor.getOutputPosition());
        }
    }
    if (windowed) {
        if (groupBy) {
            if (outPutAttributeSize == aggregateAttributePositionList.size()) {
                // All Aggregation
                outputRateLimiter = new AllAggregationGroupByWindowedPerSnapshotOutputRateLimiter(value, this, groupBy, siddhiQueryContext);
            } else if (aggregateAttributePositionList.size() > 0) {
                // Some Aggregation
                outputRateLimiter = new AggregationGroupByWindowedPerSnapshotOutputRateLimiter(value, aggregateAttributePositionList, this, groupBy, siddhiQueryContext);
            } else {
                // No aggregation
                // GroupBy is same as Non GroupBy
                outputRateLimiter = new WindowedPerSnapshotOutputRateLimiter(value, this, groupBy, siddhiQueryContext);
            }
        } else {
            if (outPutAttributeSize == aggregateAttributePositionList.size()) {
                // All Aggregation
                outputRateLimiter = new AllAggregationPerSnapshotOutputRateLimiter(value, this, groupBy, siddhiQueryContext);
            } else if (aggregateAttributePositionList.size() > 0) {
                // Some Aggregation
                outputRateLimiter = new AggregationWindowedPerSnapshotOutputRateLimiter(value, aggregateAttributePositionList, this, groupBy, siddhiQueryContext);
            } else {
                // No aggregation
                outputRateLimiter = new WindowedPerSnapshotOutputRateLimiter(value, this, groupBy, siddhiQueryContext);
            }
        }
    } else {
        if (groupBy) {
            outputRateLimiter = new GroupByPerSnapshotOutputRateLimiter(value, this, groupBy, siddhiQueryContext);
        } else {
            outputRateLimiter = new PerSnapshotOutputRateLimiter(value, this, groupBy, siddhiQueryContext);
        }
    }
    if (metaComplexEvent instanceof MetaStateEvent) {
        StateEventFactory stateEventFactory = new StateEventFactory((MetaStateEvent) metaComplexEvent);
        outputRateLimiter.setStateEventCloner(new StateEventCloner((MetaStateEvent) metaComplexEvent, stateEventFactory));
    } else {
        StreamEventFactory streamEventFactory = new StreamEventFactory((MetaStreamEvent) metaComplexEvent);
        outputRateLimiter.setStreamEventCloner(new StreamEventCloner((MetaStreamEvent) metaComplexEvent, streamEventFactory));
    }
}
Also used : StreamEventFactory(io.siddhi.core.event.stream.StreamEventFactory) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) StateEventFactory(io.siddhi.core.event.state.StateEventFactory) StreamEventCloner(io.siddhi.core.event.stream.StreamEventCloner) AttributeProcessor(io.siddhi.core.query.selector.attribute.processor.AttributeProcessor) StateEventCloner(io.siddhi.core.event.state.StateEventCloner) AttributeAggregatorExecutor(io.siddhi.core.query.selector.attribute.aggregator.AttributeAggregatorExecutor) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 5 with AttributeProcessor

use of io.siddhi.core.query.selector.attribute.processor.AttributeProcessor in project siddhi by wso2.

the class SelectorParser method getAttributeProcessors.

/**
 * Method to construct AttributeProcessor list for the selector.
 *
 * @param selector                    Selector
 * @param id                          stream id
 * @param metaComplexEvent            meta ComplexEvent
 * @param tableMap                    Table Map
 * @param variableExpressionExecutors list of VariableExpressionExecutors
 * @param outputStream                output stream
 * @param processingMode              processing mode of the query
 * @param outputExpectsExpiredEvents  is expired events sent as output
 * @param groupBy                     is Attributes groupBy
 * @param siddhiQueryContext          current siddhi query context  @return list of AttributeProcessors
 */
private static List<AttributeProcessor> getAttributeProcessors(Selector selector, String id, MetaComplexEvent metaComplexEvent, Map<String, Table> tableMap, List<VariableExpressionExecutor> variableExpressionExecutors, OutputStream outputStream, int metaPosition, ProcessingMode processingMode, boolean outputExpectsExpiredEvents, boolean groupBy, SiddhiQueryContext siddhiQueryContext) {
    List<AttributeProcessor> attributeProcessorList = new ArrayList<>();
    StreamDefinition outputDefinition = StreamDefinition.id(id);
    outputDefinition.setQueryContextStartIndex(outputStream.getQueryContextStartIndex());
    outputDefinition.setQueryContextEndIndex(outputStream.getQueryContextEndIndex());
    List<OutputAttribute> outputAttributes = selector.getSelectionList();
    if (selector.getSelectionList().size() == 0) {
        if (metaComplexEvent instanceof MetaStreamEvent) {
            List<Attribute> attributeList = ((MetaStreamEvent) metaComplexEvent).getLastInputDefinition().getAttributeList();
            for (Attribute attribute : attributeList) {
                Variable variable = new Variable(attribute.getName());
                variable.setQueryContextStartIndex(selector.getQueryContextStartIndex());
                variable.setQueryContextEndIndex(selector.getQueryContextEndIndex());
                OutputAttribute outputAttribute = new OutputAttribute(variable);
                outputAttribute.setQueryContextStartIndex(selector.getQueryContextStartIndex());
                outputAttribute.setQueryContextEndIndex(selector.getQueryContextEndIndex());
                outputAttributes.add(outputAttribute);
            }
        } else {
            int position = 0;
            for (MetaStreamEvent metaStreamEvent : ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents()) {
                if (metaPosition == SiddhiConstants.UNKNOWN_STATE || metaPosition == position) {
                    List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
                    for (Attribute attribute : attributeList) {
                        Variable variable = new Variable(attribute.getName());
                        variable.setQueryContextStartIndex(selector.getQueryContextStartIndex());
                        variable.setQueryContextEndIndex(selector.getQueryContextEndIndex());
                        OutputAttribute outputAttribute = new OutputAttribute(variable);
                        outputAttribute.setQueryContextStartIndex(selector.getQueryContextStartIndex());
                        outputAttribute.setQueryContextEndIndex(selector.getQueryContextEndIndex());
                        if (!outputAttributes.contains(outputAttribute)) {
                            outputAttributes.add(outputAttribute);
                        } else {
                            List<AbstractDefinition> definitions = new ArrayList<>();
                            for (MetaStreamEvent aMetaStreamEvent : ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents()) {
                                definitions.add(aMetaStreamEvent.getLastInputDefinition());
                            }
                            throw new DuplicateAttributeException("Duplicate attribute exist in streams " + definitions, outputStream.getQueryContextStartIndex(), outputStream.getQueryContextEndIndex());
                        }
                    }
                }
                ++position;
            }
        }
    }
    int i = 0;
    for (OutputAttribute outputAttribute : outputAttributes) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(outputAttribute.getExpression(), metaComplexEvent, SiddhiConstants.UNKNOWN_STATE, tableMap, variableExpressionExecutors, groupBy, 0, processingMode, outputExpectsExpiredEvents, siddhiQueryContext);
        if (expressionExecutor instanceof VariableExpressionExecutor) {
            // for variables we will directly put
            // value at conversion stage
            VariableExpressionExecutor executor = ((VariableExpressionExecutor) expressionExecutor);
            if (metaComplexEvent instanceof MetaStateEvent) {
                ((MetaStateEvent) metaComplexEvent).addOutputDataAllowingDuplicate(new MetaStateEventAttribute(executor.getAttribute(), executor.getPosition()));
            } else {
                ((MetaStreamEvent) metaComplexEvent).addOutputDataAllowingDuplicate(executor.getAttribute());
            }
            outputDefinition.attribute(outputAttribute.getRename(), ((VariableExpressionExecutor) expressionExecutor).getAttribute().getType());
        } else {
            // To maintain output variable positions
            if (metaComplexEvent instanceof MetaStateEvent) {
                ((MetaStateEvent) metaComplexEvent).addOutputDataAllowingDuplicate(null);
            } else {
                ((MetaStreamEvent) metaComplexEvent).addOutputDataAllowingDuplicate(null);
            }
            AttributeProcessor attributeProcessor = new AttributeProcessor(expressionExecutor);
            attributeProcessor.setOutputPosition(i);
            attributeProcessorList.add(attributeProcessor);
            outputDefinition.attribute(outputAttribute.getRename(), attributeProcessor.getOutputType());
        }
        i++;
    }
    metaComplexEvent.setOutputDefinition(outputDefinition);
    return attributeProcessorList;
}
Also used : Variable(io.siddhi.query.api.expression.Variable) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) ConditionExpressionExecutor(io.siddhi.core.executor.condition.ConditionExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) MetaStateEventAttribute(io.siddhi.core.event.state.MetaStateEventAttribute) Attribute(io.siddhi.query.api.definition.Attribute) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ArrayList(java.util.ArrayList) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) OutputAttribute(io.siddhi.query.api.execution.query.selection.OutputAttribute) DuplicateAttributeException(io.siddhi.query.api.exception.DuplicateAttributeException) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) MetaStateEventAttribute(io.siddhi.core.event.state.MetaStateEventAttribute) AttributeProcessor(io.siddhi.core.query.selector.attribute.processor.AttributeProcessor) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

AttributeProcessor (io.siddhi.core.query.selector.attribute.processor.AttributeProcessor)7 ComplexEvent (io.siddhi.core.event.ComplexEvent)4 GroupedComplexEvent (io.siddhi.core.event.GroupedComplexEvent)4 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)3 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)3 MetaStateEventAttribute (io.siddhi.core.event.state.MetaStateEventAttribute)2 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)2 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)2 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)2 ConditionExpressionExecutor (io.siddhi.core.executor.condition.ConditionExpressionExecutor)2 AbstractDefinition (io.siddhi.query.api.definition.AbstractDefinition)2 Attribute (io.siddhi.query.api.definition.Attribute)2 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)2 DuplicateAttributeException (io.siddhi.query.api.exception.DuplicateAttributeException)2 OutputAttribute (io.siddhi.query.api.execution.query.selection.OutputAttribute)2 Variable (io.siddhi.query.api.expression.Variable)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 SiddhiQueryContext (io.siddhi.core.config.SiddhiQueryContext)1 ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)1