Search in sources :

Example 31 with Expression

use of io.siddhi.query.api.expression.Expression in project siddhi by wso2.

the class AggregationParser method constructProcessExpressionExecutors.

private static List<ExpressionExecutor> constructProcessExpressionExecutors(SiddhiQueryContext siddhiQueryContext, Map<String, Table> tableMap, int baseAggregatorBeginIndex, List<Expression> finalBaseExpressions, StreamDefinition incomingOutputStreamDefinition, MetaStreamEvent processedMetaStreamEvent, List<VariableExpressionExecutor> processVariableExpressionExecutors, boolean isProcessingOnExternalTime, TimePeriod.Duration duration, boolean isDistributed, String shardId, boolean isLatestEventColAdded) {
    List<ExpressionExecutor> processExpressionExecutors = new ArrayList<>();
    List<Attribute> attributeList = incomingOutputStreamDefinition.getAttributeList();
    int i = 1;
    // Add timestamp executor
    Attribute attribute = attributeList.get(0);
    VariableExpressionExecutor variableExpressionExecutor = (VariableExpressionExecutor) ExpressionParser.parseExpression(new Variable(attribute.getName()), processedMetaStreamEvent, 0, tableMap, processVariableExpressionExecutors, true, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
    processExpressionExecutors.add(variableExpressionExecutor);
    if (isDistributed) {
        Expression shardIdExpression = Expression.value(shardId);
        ExpressionExecutor shardIdExpressionExecutor = ExpressionParser.parseExpression(shardIdExpression, processedMetaStreamEvent, 0, tableMap, processVariableExpressionExecutors, true, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        processExpressionExecutors.add(shardIdExpressionExecutor);
        i++;
    }
    if (isProcessingOnExternalTime) {
        Expression externalTimestampExpression = AttributeFunction.function("incrementalAggregator", "getAggregationStartTime", new Variable(AGG_EXTERNAL_TIMESTAMP_COL), new StringConstant(duration.name()));
        ExpressionExecutor externalTimestampExecutor = ExpressionParser.parseExpression(externalTimestampExpression, processedMetaStreamEvent, 0, tableMap, processVariableExpressionExecutors, true, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        processExpressionExecutors.add(externalTimestampExecutor);
        i++;
    }
    if (isLatestEventColAdded) {
        baseAggregatorBeginIndex = baseAggregatorBeginIndex - 1;
    }
    for (; i < baseAggregatorBeginIndex; i++) {
        attribute = attributeList.get(i);
        variableExpressionExecutor = (VariableExpressionExecutor) ExpressionParser.parseExpression(new Variable(attribute.getName()), processedMetaStreamEvent, 0, tableMap, processVariableExpressionExecutors, true, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        processExpressionExecutors.add(variableExpressionExecutor);
    }
    if (isLatestEventColAdded) {
        Expression lastTimestampExpression = AttributeFunction.function("max", new Variable(AGG_LAST_TIMESTAMP_COL));
        ExpressionExecutor latestTimestampExecutor = ExpressionParser.parseExpression(lastTimestampExpression, processedMetaStreamEvent, 0, tableMap, processVariableExpressionExecutors, true, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        processExpressionExecutors.add(latestTimestampExecutor);
    }
    for (Expression expression : finalBaseExpressions) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(expression, processedMetaStreamEvent, 0, tableMap, processVariableExpressionExecutors, true, 0, ProcessingMode.BATCH, false, siddhiQueryContext);
        processExpressionExecutors.add(expressionExecutor);
    }
    return processExpressionExecutors;
}
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) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ArrayList(java.util.ArrayList) StringConstant(io.siddhi.query.api.expression.constant.StringConstant)

Example 32 with Expression

use of io.siddhi.query.api.expression.Expression in project siddhi by wso2.

the class CollectionExpressionParser method parseInternalCollectionExpression.

/**
 * Parse the given expression and create the appropriate Executor by recursively traversing the expression.
 *
 * @param expression             Expression to be parsed
 * @param matchingMetaInfoHolder matchingMetaInfoHolder
 * @param indexedEventHolder     indexed event holder
 * @return ExpressionExecutor
 */
private static CollectionExpression parseInternalCollectionExpression(Expression expression, MatchingMetaInfoHolder matchingMetaInfoHolder, IndexedEventHolder indexedEventHolder) {
    if (expression instanceof And) {
        CollectionExpression leftCollectionExpression = parseInternalCollectionExpression(((And) expression).getLeftExpression(), matchingMetaInfoHolder, indexedEventHolder);
        CollectionExpression rightCollectionExpression = parseInternalCollectionExpression(((And) expression).getRightExpression(), matchingMetaInfoHolder, indexedEventHolder);
        if (leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.NON && rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.NON) {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.NON);
        } else if ((leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PRIMARY_KEY_RESULT_SET || leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_RESULT_SET) && (rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PRIMARY_KEY_RESULT_SET || rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_RESULT_SET)) {
            Set<String> primaryKeys = new HashSet<>();
            primaryKeys.addAll(leftCollectionExpression.getMultiPrimaryKeys());
            primaryKeys.addAll(rightCollectionExpression.getMultiPrimaryKeys());
            if (indexedEventHolder.getPrimaryKeyReferenceHolders() != null && primaryKeys.size() == indexedEventHolder.getPrimaryKeyReferenceHolders().length) {
                return new AndMultiPrimaryKeyCollectionExpression(expression, CollectionExpression.CollectionScope.PRIMARY_KEY_RESULT_SET, leftCollectionExpression, rightCollectionExpression);
            } else {
                return new AndCollectionExpression(expression, CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_RESULT_SET, leftCollectionExpression, rightCollectionExpression);
            }
        // TODO support query rewriting to group all PARTIAL_PRIMARY_KEY_RESULT_SETs together such that it can
        // build AndMultiPrimaryKeyCollectionExpression.
        } else if ((leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_RESULT_SET || leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.NON || leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.EXHAUSTIVE) && (rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_RESULT_SET || rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.NON || rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.EXHAUSTIVE)) {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.EXHAUSTIVE);
        } else {
            return new AndCollectionExpression(expression, CollectionExpression.CollectionScope.OPTIMISED_PRIMARY_KEY_OR_INDEXED_RESULT_SET, leftCollectionExpression, rightCollectionExpression);
        }
    } else if (expression instanceof Or) {
        CollectionExpression leftCollectionExpression = parseInternalCollectionExpression(((Or) expression).getLeftExpression(), matchingMetaInfoHolder, indexedEventHolder);
        CollectionExpression rightCollectionExpression = parseInternalCollectionExpression(((Or) expression).getRightExpression(), matchingMetaInfoHolder, indexedEventHolder);
        if (leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.NON && rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.NON) {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.NON);
        } else if (leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.EXHAUSTIVE || leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_RESULT_SET || rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.EXHAUSTIVE || rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_RESULT_SET) {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.EXHAUSTIVE);
        } else {
            return new OrCollectionExpression(expression, CollectionExpression.CollectionScope.OPTIMISED_PRIMARY_KEY_OR_INDEXED_RESULT_SET, leftCollectionExpression, rightCollectionExpression);
        }
    } else if (expression instanceof Not) {
        CollectionExpression notCollectionExpression = parseInternalCollectionExpression(((Not) expression).getExpression(), matchingMetaInfoHolder, indexedEventHolder);
        switch(notCollectionExpression.getCollectionScope()) {
            case NON:
                return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.NON);
            case PRIMARY_KEY_ATTRIBUTE:
                return new NotCollectionExpression(expression, CollectionExpression.CollectionScope.PRIMARY_KEY_RESULT_SET, notCollectionExpression);
            case INDEXED_ATTRIBUTE:
                return new NotCollectionExpression(expression, CollectionExpression.CollectionScope.INDEXED_RESULT_SET, notCollectionExpression);
            case PRIMARY_KEY_RESULT_SET:
            case INDEXED_RESULT_SET:
            case OPTIMISED_PRIMARY_KEY_OR_INDEXED_RESULT_SET:
                return new NotCollectionExpression(expression, CollectionExpression.CollectionScope.OPTIMISED_PRIMARY_KEY_OR_INDEXED_RESULT_SET, notCollectionExpression);
            case PARTIAL_PRIMARY_KEY_ATTRIBUTE:
            case PARTIAL_PRIMARY_KEY_RESULT_SET:
            case EXHAUSTIVE:
                return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.EXHAUSTIVE);
        }
    } else if (expression instanceof Compare) {
        CollectionExpression leftCollectionExpression = parseInternalCollectionExpression(((Compare) expression).getLeftExpression(), matchingMetaInfoHolder, indexedEventHolder);
        CollectionExpression rightCollectionExpression = parseInternalCollectionExpression(((Compare) expression).getRightExpression(), matchingMetaInfoHolder, indexedEventHolder);
        if (leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.NON && rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.NON) {
            // comparing two stream attributes with O(1) time complexity
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.NON);
        } else if ((leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.INDEXED_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_ATTRIBUTE) && rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.NON) {
            switch(leftCollectionExpression.getCollectionScope()) {
                case INDEXED_ATTRIBUTE:
                    return new CompareCollectionExpression((Compare) expression, CollectionExpression.CollectionScope.INDEXED_RESULT_SET, leftCollectionExpression, ((Compare) expression).getOperator(), rightCollectionExpression);
                case PRIMARY_KEY_ATTRIBUTE:
                    return new CompareCollectionExpression((Compare) expression, CollectionExpression.CollectionScope.PRIMARY_KEY_RESULT_SET, leftCollectionExpression, ((Compare) expression).getOperator(), rightCollectionExpression);
                case PARTIAL_PRIMARY_KEY_ATTRIBUTE:
                    return new CompareCollectionExpression((Compare) expression, CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_RESULT_SET, leftCollectionExpression, ((Compare) expression).getOperator(), rightCollectionExpression);
            }
        } else if (leftCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.NON && (rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.INDEXED_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_ATTRIBUTE)) {
            Compare.Operator operator = ((Compare) expression).getOperator();
            // moving let to right
            switch(operator) {
                case LESS_THAN:
                    operator = Compare.Operator.GREATER_THAN;
                    break;
                case GREATER_THAN:
                    operator = Compare.Operator.LESS_THAN;
                    break;
                case LESS_THAN_EQUAL:
                    operator = Compare.Operator.GREATER_THAN_EQUAL;
                    break;
                case GREATER_THAN_EQUAL:
                    operator = Compare.Operator.LESS_THAN_EQUAL;
                    break;
                case EQUAL:
                    break;
                case NOT_EQUAL:
                    break;
            }
            switch(rightCollectionExpression.getCollectionScope()) {
                case INDEXED_ATTRIBUTE:
                    return new CompareCollectionExpression((Compare) expression, CollectionExpression.CollectionScope.INDEXED_RESULT_SET, rightCollectionExpression, operator, leftCollectionExpression);
                case PRIMARY_KEY_ATTRIBUTE:
                    return new CompareCollectionExpression((Compare) expression, CollectionExpression.CollectionScope.PRIMARY_KEY_RESULT_SET, rightCollectionExpression, operator, leftCollectionExpression);
                case PARTIAL_PRIMARY_KEY_ATTRIBUTE:
                    return new CompareCollectionExpression((Compare) expression, CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_RESULT_SET, rightCollectionExpression, operator, leftCollectionExpression);
            }
        } else {
            // comparing non indexed table with stream attributes or another table attribute
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.EXHAUSTIVE);
        }
    } else if (expression instanceof Constant) {
        return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.NON);
    } else if (expression instanceof Variable) {
        if (((Variable) expression).getStreamId() == null) {
            List<String> attributeNameList = Arrays.asList(matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeNameArray());
            if (attributeNameList.contains(((Variable) expression).getAttributeName())) {
                String streamId = matchingMetaInfoHolder.getMatchingStreamDefinition().getId();
                ((Variable) expression).setStreamId(streamId);
            }
        }
        if (isCollectionVariable(matchingMetaInfoHolder, (Variable) expression)) {
            if (indexedEventHolder.isAttributeIndexed(((Variable) expression).getAttributeName())) {
                return new AttributeCollectionExpression(expression, ((Variable) expression).getAttributeName(), CollectionExpression.CollectionScope.INDEXED_ATTRIBUTE);
            } else if (indexedEventHolder.isMultiPrimaryKeyAttribute(((Variable) expression).getAttributeName())) {
                if (indexedEventHolder.getPrimaryKeyReferenceHolders() != null && indexedEventHolder.getPrimaryKeyReferenceHolders().length == 1) {
                    return new AttributeCollectionExpression(expression, ((Variable) expression).getAttributeName(), CollectionExpression.CollectionScope.PRIMARY_KEY_ATTRIBUTE);
                } else {
                    return new AttributeCollectionExpression(expression, ((Variable) expression).getAttributeName(), CollectionExpression.CollectionScope.PARTIAL_PRIMARY_KEY_ATTRIBUTE);
                }
            } else {
                return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.EXHAUSTIVE);
            }
        } else {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.NON);
        }
    } else if (expression instanceof Multiply) {
        CollectionExpression left = parseInternalCollectionExpression(((Multiply) expression).getLeftValue(), matchingMetaInfoHolder, indexedEventHolder);
        CollectionExpression right = parseInternalCollectionExpression(((Multiply) expression).getRightValue(), matchingMetaInfoHolder, indexedEventHolder);
        if (left.getCollectionScope() == CollectionExpression.CollectionScope.NON && right.getCollectionScope() == CollectionExpression.CollectionScope.NON) {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.NON);
        } else {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.EXHAUSTIVE);
        }
    } else if (expression instanceof Add) {
        CollectionExpression left = parseInternalCollectionExpression(((Add) expression).getLeftValue(), matchingMetaInfoHolder, indexedEventHolder);
        CollectionExpression right = parseInternalCollectionExpression(((Add) expression).getRightValue(), matchingMetaInfoHolder, indexedEventHolder);
        if (left.getCollectionScope() == CollectionExpression.CollectionScope.NON && right.getCollectionScope() == CollectionExpression.CollectionScope.NON) {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.NON);
        } else {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.EXHAUSTIVE);
        }
    } else if (expression instanceof Subtract) {
        CollectionExpression left = parseInternalCollectionExpression(((Subtract) expression).getLeftValue(), matchingMetaInfoHolder, indexedEventHolder);
        CollectionExpression right = parseInternalCollectionExpression(((Subtract) expression).getRightValue(), matchingMetaInfoHolder, indexedEventHolder);
        if (left.getCollectionScope() == CollectionExpression.CollectionScope.NON && right.getCollectionScope() == CollectionExpression.CollectionScope.NON) {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.NON);
        } else {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.EXHAUSTIVE);
        }
    } else if (expression instanceof Mod) {
        CollectionExpression left = parseInternalCollectionExpression(((Mod) expression).getLeftValue(), matchingMetaInfoHolder, indexedEventHolder);
        CollectionExpression right = parseInternalCollectionExpression(((Mod) expression).getRightValue(), matchingMetaInfoHolder, indexedEventHolder);
        if (left.getCollectionScope() == CollectionExpression.CollectionScope.NON && right.getCollectionScope() == CollectionExpression.CollectionScope.NON) {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.NON);
        } else {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.EXHAUSTIVE);
        }
    } else if (expression instanceof Divide) {
        CollectionExpression left = parseInternalCollectionExpression(((Divide) expression).getLeftValue(), matchingMetaInfoHolder, indexedEventHolder);
        CollectionExpression right = parseInternalCollectionExpression(((Divide) expression).getRightValue(), matchingMetaInfoHolder, indexedEventHolder);
        if (left.getCollectionScope() == CollectionExpression.CollectionScope.NON && right.getCollectionScope() == CollectionExpression.CollectionScope.NON) {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.NON);
        } else {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.EXHAUSTIVE);
        }
    } else if (expression instanceof AttributeFunction) {
        Expression[] innerExpressions = ((AttributeFunction) expression).getParameters();
        for (Expression aExpression : innerExpressions) {
            CollectionExpression aCollectionExpression = parseInternalCollectionExpression(aExpression, matchingMetaInfoHolder, indexedEventHolder);
            if (aCollectionExpression.getCollectionScope() != CollectionExpression.CollectionScope.NON) {
                return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.EXHAUSTIVE);
            }
        }
        return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.NON);
    } else if (expression instanceof In) {
        CollectionExpression inCollectionExpression = parseInternalCollectionExpression(((In) expression).getExpression(), matchingMetaInfoHolder, indexedEventHolder);
        if (inCollectionExpression.getCollectionScope() != CollectionExpression.CollectionScope.NON) {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.EXHAUSTIVE);
        }
        return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.NON);
    } else if (expression instanceof IsNull) {
        CollectionExpression nullCollectionExpression = parseInternalCollectionExpression(((IsNull) expression).getExpression(), matchingMetaInfoHolder, indexedEventHolder);
        if (nullCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.NON) {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.NON);
        } else if (nullCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.INDEXED_ATTRIBUTE) {
            return new NullCollectionExpression(expression, CollectionExpression.CollectionScope.INDEXED_RESULT_SET, ((AttributeCollectionExpression) nullCollectionExpression).getAttribute());
        } else if (nullCollectionExpression.getCollectionScope() == CollectionExpression.CollectionScope.PRIMARY_KEY_ATTRIBUTE) {
            return new NullCollectionExpression(expression, CollectionExpression.CollectionScope.PRIMARY_KEY_RESULT_SET, ((AttributeCollectionExpression) nullCollectionExpression).getAttribute());
        } else {
            return new BasicCollectionExpression(expression, CollectionExpression.CollectionScope.EXHAUSTIVE);
        }
    }
    throw new UnsupportedOperationException(expression.toString() + " not supported!");
}
Also used : Add(io.siddhi.query.api.expression.math.Add) Set(java.util.Set) HashSet(java.util.HashSet) Or(io.siddhi.query.api.expression.condition.Or) Variable(io.siddhi.query.api.expression.Variable) BasicCollectionExpression(io.siddhi.core.util.collection.expression.BasicCollectionExpression) In(io.siddhi.query.api.expression.condition.In) Constant(io.siddhi.query.api.expression.constant.Constant) AttributeCollectionExpression(io.siddhi.core.util.collection.expression.AttributeCollectionExpression) Divide(io.siddhi.query.api.expression.math.Divide) AndCollectionExpression(io.siddhi.core.util.collection.expression.AndCollectionExpression) Multiply(io.siddhi.query.api.expression.math.Multiply) NullCollectionExpression(io.siddhi.core.util.collection.expression.NullCollectionExpression) Compare(io.siddhi.query.api.expression.condition.Compare) List(java.util.List) ArrayList(java.util.ArrayList) CollectionExpression(io.siddhi.core.util.collection.expression.CollectionExpression) CompareCollectionExpression(io.siddhi.core.util.collection.expression.CompareCollectionExpression) AttributeCollectionExpression(io.siddhi.core.util.collection.expression.AttributeCollectionExpression) BasicCollectionExpression(io.siddhi.core.util.collection.expression.BasicCollectionExpression) NullCollectionExpression(io.siddhi.core.util.collection.expression.NullCollectionExpression) OrCollectionExpression(io.siddhi.core.util.collection.expression.OrCollectionExpression) NotCollectionExpression(io.siddhi.core.util.collection.expression.NotCollectionExpression) AndMultiPrimaryKeyCollectionExpression(io.siddhi.core.util.collection.expression.AndMultiPrimaryKeyCollectionExpression) AndCollectionExpression(io.siddhi.core.util.collection.expression.AndCollectionExpression) Mod(io.siddhi.query.api.expression.math.Mod) AndMultiPrimaryKeyCollectionExpression(io.siddhi.core.util.collection.expression.AndMultiPrimaryKeyCollectionExpression) AttributeFunction(io.siddhi.query.api.expression.AttributeFunction) Not(io.siddhi.query.api.expression.condition.Not) CompareCollectionExpression(io.siddhi.core.util.collection.expression.CompareCollectionExpression) CollectionExpression(io.siddhi.core.util.collection.expression.CollectionExpression) Expression(io.siddhi.query.api.expression.Expression) CompareCollectionExpression(io.siddhi.core.util.collection.expression.CompareCollectionExpression) AttributeCollectionExpression(io.siddhi.core.util.collection.expression.AttributeCollectionExpression) BasicCollectionExpression(io.siddhi.core.util.collection.expression.BasicCollectionExpression) NullCollectionExpression(io.siddhi.core.util.collection.expression.NullCollectionExpression) OrCollectionExpression(io.siddhi.core.util.collection.expression.OrCollectionExpression) NotCollectionExpression(io.siddhi.core.util.collection.expression.NotCollectionExpression) AndMultiPrimaryKeyCollectionExpression(io.siddhi.core.util.collection.expression.AndMultiPrimaryKeyCollectionExpression) AndCollectionExpression(io.siddhi.core.util.collection.expression.AndCollectionExpression) And(io.siddhi.query.api.expression.condition.And) OrCollectionExpression(io.siddhi.core.util.collection.expression.OrCollectionExpression) Subtract(io.siddhi.query.api.expression.math.Subtract) IsNull(io.siddhi.query.api.expression.condition.IsNull) NotCollectionExpression(io.siddhi.core.util.collection.expression.NotCollectionExpression)

Example 33 with Expression

use of io.siddhi.query.api.expression.Expression 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 34 with Expression

use of io.siddhi.query.api.expression.Expression in project siddhi by wso2.

the class AvgIncrementalAttributeAggregator method getBaseAggregators.

@Override
public Expression[] getBaseAggregators() {
    Expression sumAggregator = Expression.function("sum", Expression.variable(getBaseAttributes()[0].getName()));
    Expression countAggregator = Expression.function("sum", Expression.variable(getBaseAttributes()[1].getName()));
    return new Expression[] { sumAggregator, countAggregator };
}
Also used : Expression(io.siddhi.query.api.expression.Expression)

Example 35 with Expression

use of io.siddhi.query.api.expression.Expression in project siddhi by wso2.

the class DistinctCountIncrementalAttributeAggregator method init.

@Override
public void init(String attributeName, Attribute.Type attributeType) {
    Attribute set;
    Expression setInitialValue;
    // distinct-count is not supported for object types.
    if (attributeType.equals(Attribute.Type.FLOAT) || attributeType.equals(Attribute.Type.DOUBLE) || attributeType.equals(Attribute.Type.INT) || attributeType.equals(Attribute.Type.LONG) || attributeType.equals(Attribute.Type.STRING) || attributeType.equals(Attribute.Type.BOOL)) {
        set = new Attribute("AGG_SET_".concat(attributeName), Attribute.Type.OBJECT);
        setInitialValue = Expression.function("createSet", Expression.variable(attributeName));
    } else {
        throw new SiddhiAppRuntimeException("Distinct count aggregation cannot be executed on attribute type " + attributeType.toString());
    }
    this.baseAttributes = new Attribute[] { set };
    this.baseAttributesInitialValues = new Expression[] { setInitialValue };
}
Also used : ReturnAttribute(io.siddhi.annotation.ReturnAttribute) Attribute(io.siddhi.query.api.definition.Attribute) Expression(io.siddhi.query.api.expression.Expression) SiddhiAppRuntimeException(io.siddhi.core.exception.SiddhiAppRuntimeException)

Aggregations

Expression (io.siddhi.query.api.expression.Expression)44 Attribute (io.siddhi.query.api.definition.Attribute)18 Variable (io.siddhi.query.api.expression.Variable)14 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)13 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)13 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)11 ArrayList (java.util.ArrayList)11 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)10 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)10 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)9 OutputAttribute (io.siddhi.query.api.execution.query.selection.OutputAttribute)9 SiddhiQueryContext (io.siddhi.core.config.SiddhiQueryContext)7 AbstractDefinition (io.siddhi.query.api.definition.AbstractDefinition)7 AttributeFunction (io.siddhi.query.api.expression.AttributeFunction)7 Compare (io.siddhi.query.api.expression.condition.Compare)6 Table (io.siddhi.core.table.Table)5 ReturnAttribute (io.siddhi.annotation.ReturnAttribute)4 SiddhiAppRuntimeException (io.siddhi.core.exception.SiddhiAppRuntimeException)4 MatchingMetaInfoHolder (io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)4 HashMap (java.util.HashMap)4