Search in sources :

Example 1 with AttributeProcessor

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

the class QuerySelector method processInBatchNoGroupBy.

private ComplexEventChunk processInBatchNoGroupBy(ComplexEventChunk complexEventChunk) {
    complexEventChunk.reset();
    ComplexEvent lastEvent = null;
    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 (!(havingConditionExecutor != null && !havingConditionExecutor.execute(event))) {
                        if ((event.getType() == StreamEvent.Type.CURRENT && currentOn) || (event.getType() == StreamEvent.Type.EXPIRED && expiredOn)) {
                            complexEventChunk.remove();
                            lastEvent = event;
                        }
                    }
                    break;
                case TIMER:
                    break;
                case RESET:
                    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                        attributeProcessor.process(event);
                    }
                    break;
            }
        }
    }
    if (lastEvent != null) {
        complexEventChunk.clear();
        if (limit == SiddhiConstants.UNKNOWN_STATE || limit > 0) {
            complexEventChunk.add(lastEvent);
        }
        return complexEventChunk;
    }
    return null;
}
Also used : GroupedComplexEvent(org.wso2.siddhi.core.event.GroupedComplexEvent) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) AttributeProcessor(org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor)

Example 2 with AttributeProcessor

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

the class QuerySelector method clone.

public QuerySelector clone(String key) {
    QuerySelector clonedQuerySelector = new QuerySelector(id + key, selector, currentOn, expiredOn, siddhiAppContext);
    List<AttributeProcessor> clonedAttributeProcessorList = new ArrayList<AttributeProcessor>();
    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
        clonedAttributeProcessorList.add(attributeProcessor.cloneProcessor(key));
    }
    clonedQuerySelector.attributeProcessorList = clonedAttributeProcessorList;
    clonedQuerySelector.isGroupBy = isGroupBy;
    clonedQuerySelector.containsAggregator = containsAggregator;
    clonedQuerySelector.groupByKeyGenerator = groupByKeyGenerator;
    clonedQuerySelector.havingConditionExecutor = havingConditionExecutor;
    clonedQuerySelector.eventPopulator = eventPopulator;
    clonedQuerySelector.batchingEnabled = batchingEnabled;
    clonedQuerySelector.isOrderBy = isOrderBy;
    clonedQuerySelector.orderByEventComparator = orderByEventComparator;
    clonedQuerySelector.limit = limit;
    return clonedQuerySelector;
}
Also used : ArrayList(java.util.ArrayList) AttributeProcessor(org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor)

Example 3 with AttributeProcessor

use of org.wso2.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>(complexEventChunk.isBatch());
    synchronized (this) {
        int limitCount = 0;
        while (complexEventChunk.hasNext()) {
            ComplexEvent event = complexEventChunk.next();
            switch(event.getType()) {
                case CURRENT:
                case EXPIRED:
                    eventPopulator.populateStateEvent(event);
                    String groupedByKey = groupByKeyGenerator.constructEventKey(event);
                    GroupByAggregationAttributeExecutor.getKeyThreadLocal().set(groupedByKey);
                    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(groupedByKey, event));
                            } else {
                                if (limitCount < limit) {
                                    currentComplexEventChunk.add(new GroupedComplexEvent(groupedByKey, event));
                                    limitCount++;
                                }
                            }
                        }
                    }
                    GroupByAggregationAttributeExecutor.getKeyThreadLocal().remove();
                    break;
                case TIMER:
                    break;
                case RESET:
                    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                        attributeProcessor.process(event);
                    }
                    break;
            }
        }
    }
    if (isOrderBy) {
        orderEventChunk(complexEventChunk);
    }
    if (limit != SiddhiConstants.UNKNOWN_STATE) {
        limitEventChunk(complexEventChunk);
    }
    currentComplexEventChunk.reset();
    if (currentComplexEventChunk.hasNext()) {
        return currentComplexEventChunk;
    }
    return null;
}
Also used : GroupedComplexEvent(org.wso2.siddhi.core.event.GroupedComplexEvent) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) GroupedComplexEvent(org.wso2.siddhi.core.event.GroupedComplexEvent) AttributeProcessor(org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor)

Example 4 with AttributeProcessor

use of org.wso2.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 (limit != SiddhiConstants.UNKNOWN_STATE) {
        limitEventChunk(complexEventChunk);
    }
    complexEventChunk.reset();
    if (complexEventChunk.hasNext()) {
        return complexEventChunk;
    }
    return null;
}
Also used : GroupedComplexEvent(org.wso2.siddhi.core.event.GroupedComplexEvent) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) AttributeProcessor(org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor)

Example 5 with AttributeProcessor

use of org.wso2.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 siddhiAppContext            siddhi app context
 * @param metaComplexEvent            meta ComplexEvent
 * @param tableMap                    Table Map
 * @param variableExpressionExecutors list of VariableExpressionExecutors
 * @param outputStream
 * @return list of AttributeProcessors
 */
private static List<AttributeProcessor> getAttributeProcessors(Selector selector, String id, SiddhiAppContext siddhiAppContext, MetaComplexEvent metaComplexEvent, Map<String, Table> tableMap, List<VariableExpressionExecutor> variableExpressionExecutors, OutputStream outputStream, String queryName, int metaPosition) {
    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) {
                outputAttributes.add(new OutputAttribute(new Variable(attribute.getName())));
            }
        } 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) {
                        OutputAttribute outputAttribute = new OutputAttribute(new Variable(attribute.getName()));
                        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, siddhiAppContext, !(selector.getGroupByList().isEmpty()), 0, queryName);
        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(org.wso2.siddhi.query.api.expression.Variable) StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) ConditionExpressionExecutor(org.wso2.siddhi.core.executor.condition.ConditionExpressionExecutor) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor) Attribute(org.wso2.siddhi.query.api.definition.Attribute) MetaStateEventAttribute(org.wso2.siddhi.core.event.state.MetaStateEventAttribute) OutputAttribute(org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ArrayList(java.util.ArrayList) AbstractDefinition(org.wso2.siddhi.query.api.definition.AbstractDefinition) OutputAttribute(org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute) DuplicateAttributeException(org.wso2.siddhi.query.api.exception.DuplicateAttributeException) MetaStateEvent(org.wso2.siddhi.core.event.state.MetaStateEvent) MetaStateEventAttribute(org.wso2.siddhi.core.event.state.MetaStateEventAttribute) AttributeProcessor(org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

AttributeProcessor (org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor)8 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)4 GroupedComplexEvent (org.wso2.siddhi.core.event.GroupedComplexEvent)4 ArrayList (java.util.ArrayList)2 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)2 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)2 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)2 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)2 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)2 ConditionExpressionExecutor (org.wso2.siddhi.core.executor.condition.ConditionExpressionExecutor)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)1 MetaStateEventAttribute (org.wso2.siddhi.core.event.state.MetaStateEventAttribute)1 StateEventCloner (org.wso2.siddhi.core.event.state.StateEventCloner)1 StateEventPool (org.wso2.siddhi.core.event.state.StateEventPool)1 StreamEventCloner (org.wso2.siddhi.core.event.stream.StreamEventCloner)1 StreamEventPool (org.wso2.siddhi.core.event.stream.StreamEventPool)1 GroupByKeyGenerator (org.wso2.siddhi.core.query.selector.GroupByKeyGenerator)1 OrderByEventComparator (org.wso2.siddhi.core.query.selector.OrderByEventComparator)1