Search in sources :

Example 1 with OrCollectionExpression

use of org.wso2.siddhi.core.util.collection.expression.OrCollectionExpression in project siddhi by wso2.

the class CollectionExpressionParser method buildCollectionExecutor.

public static CollectionExecutor buildCollectionExecutor(CollectionExpression collectionExpression, MatchingMetaInfoHolder matchingMetaInfoHolder, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, SiddhiAppContext siddhiAppContext, boolean isFirst, String queryName) {
    if (collectionExpression instanceof AttributeCollectionExpression) {
        ExpressionExecutor expressionExecutor = null;
        if (isFirst) {
            expressionExecutor = ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
        }
        return new CompareCollectionExecutor(expressionExecutor, matchingMetaInfoHolder.getStoreEventIndex(), ((AttributeCollectionExpression) collectionExpression).getAttribute(), Compare.Operator.EQUAL, new ConstantExpressionExecutor(true, Attribute.Type.BOOL));
    } else if (collectionExpression instanceof CompareCollectionExpression) {
        ExpressionExecutor valueExpressionExecutor = ExpressionParser.parseExpression(((CompareCollectionExpression) collectionExpression).getValueCollectionExpression().getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
        AttributeCollectionExpression attributeCollectionExpression = ((AttributeCollectionExpression) ((CompareCollectionExpression) collectionExpression).getAttributeCollectionExpression());
        ExpressionExecutor expressionExecutor = null;
        if (isFirst) {
            expressionExecutor = ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
        }
        return new CompareCollectionExecutor(expressionExecutor, matchingMetaInfoHolder.getStoreEventIndex(), attributeCollectionExpression.getAttribute(), ((CompareCollectionExpression) collectionExpression).getOperator(), valueExpressionExecutor);
    } else if (collectionExpression instanceof NullCollectionExpression) {
        ExpressionExecutor expressionExecutor = null;
        if (isFirst) {
            expressionExecutor = ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
        }
        return new CompareCollectionExecutor(expressionExecutor, matchingMetaInfoHolder.getStoreEventIndex(), ((NullCollectionExpression) collectionExpression).getAttribute(), Compare.Operator.EQUAL, new ConstantExpressionExecutor(null, Attribute.Type.OBJECT));
    } else if (collectionExpression instanceof AndMultiPrimaryKeyCollectionExpression) {
        Map<String, ExpressionExecutor> multiPrimaryKeyExpressionExecutors = buildMultiPrimaryKeyExpressionExecutors(collectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, queryName);
        List<Attribute> attributes = matchingMetaInfoHolder.getStoreDefinition().getAttributeList();
        StringBuilder compositePrimaryKey = new StringBuilder();
        List<ExpressionExecutor> sortedExecutors = new ArrayList<ExpressionExecutor>();
        for (Attribute attribute : attributes) {
            ExpressionExecutor expressionExecutor = multiPrimaryKeyExpressionExecutors.get(attribute.getName());
            if (expressionExecutor != null) {
                sortedExecutors.add(expressionExecutor);
                compositePrimaryKey.append(attribute.getName()).append(SiddhiConstants.KEY_DELIMITER);
            }
        }
        return new AndMultiPrimaryKeyCollectionExecutor(compositePrimaryKey.toString(), sortedExecutors);
    } else if (collectionExpression instanceof AndCollectionExpression) {
        CollectionExpression leftCollectionExpression = ((AndCollectionExpression) collectionExpression).getLeftCollectionExpression();
        CollectionExpression rightCollectionExpression = ((AndCollectionExpression) collectionExpression).getRightCollectionExpression();
        ExpressionExecutor expressionExecutor = null;
        CollectionExecutor aCollectionExecutor = null;
        ExhaustiveCollectionExecutor exhaustiveCollectionExecutor = null;
        CollectionExecutor leftCollectionExecutor;
        CollectionExecutor rightCollectionExecutor;
        switch(leftCollectionExpression.getCollectionScope()) {
            case NON:
                switch(rightCollectionExpression.getCollectionScope()) {
                    case NON:
                        expressionExecutor = ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
                        return new NonCollectionExecutor(expressionExecutor);
                    case INDEXED_ATTRIBUTE:
                    case INDEXED_RESULT_SET:
                    case PRIMARY_KEY_ATTRIBUTE:
                    case PRIMARY_KEY_RESULT_SET:
                    case PARTIAL_PRIMARY_KEY_RESULT_SET:
                    case OPTIMISED_PRIMARY_KEY_OR_INDEXED_RESULT_SET:
                    case EXHAUSTIVE:
                        expressionExecutor = ExpressionParser.parseExpression(leftCollectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
                        aCollectionExecutor = buildCollectionExecutor(rightCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, isFirst, queryName);
                        return new NonAndCollectionExecutor(expressionExecutor, aCollectionExecutor, rightCollectionExpression.getCollectionScope());
                }
                break;
            case INDEXED_ATTRIBUTE:
            case PRIMARY_KEY_ATTRIBUTE:
                switch(rightCollectionExpression.getCollectionScope()) {
                    case NON:
                        expressionExecutor = ExpressionParser.parseExpression(rightCollectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
                        aCollectionExecutor = buildCollectionExecutor(leftCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, isFirst, queryName);
                        return new NonAndCollectionExecutor(expressionExecutor, aCollectionExecutor, rightCollectionExpression.getCollectionScope());
                    case INDEXED_ATTRIBUTE:
                    case INDEXED_RESULT_SET:
                    case PRIMARY_KEY_ATTRIBUTE:
                    case PRIMARY_KEY_RESULT_SET:
                    case OPTIMISED_PRIMARY_KEY_OR_INDEXED_RESULT_SET:
                        exhaustiveCollectionExecutor = new ExhaustiveCollectionExecutor(ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName), matchingMetaInfoHolder.getStoreEventIndex());
                        leftCollectionExecutor = buildCollectionExecutor(leftCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, false, queryName);
                        rightCollectionExecutor = buildCollectionExecutor(rightCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, false, queryName);
                        return new AnyAndCollectionExecutor(leftCollectionExecutor, rightCollectionExecutor, exhaustiveCollectionExecutor);
                    case PARTIAL_PRIMARY_KEY_RESULT_SET:
                    case EXHAUSTIVE:
                        leftCollectionExecutor = buildCollectionExecutor(leftCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, isFirst, queryName);
                        if (isFirst || leftCollectionExecutor.getDefaultCost() == CollectionExecutor.Cost.SINGLE_RETURN_INDEX_MATCHING) {
                            exhaustiveCollectionExecutor = new ExhaustiveCollectionExecutor(ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName), matchingMetaInfoHolder.getStoreEventIndex());
                        }
                        return new CompareExhaustiveAndCollectionExecutor(leftCollectionExecutor, exhaustiveCollectionExecutor);
                }
                break;
            case INDEXED_RESULT_SET:
            case PRIMARY_KEY_RESULT_SET:
            case OPTIMISED_PRIMARY_KEY_OR_INDEXED_RESULT_SET:
                switch(rightCollectionExpression.getCollectionScope()) {
                    case NON:
                        expressionExecutor = ExpressionParser.parseExpression(rightCollectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
                        aCollectionExecutor = buildCollectionExecutor(leftCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, isFirst, queryName);
                        return new NonAndCollectionExecutor(expressionExecutor, aCollectionExecutor, rightCollectionExpression.getCollectionScope());
                    case INDEXED_ATTRIBUTE:
                    case PRIMARY_KEY_ATTRIBUTE:
                        exhaustiveCollectionExecutor = new ExhaustiveCollectionExecutor(ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName), matchingMetaInfoHolder.getStoreEventIndex());
                        leftCollectionExecutor = buildCollectionExecutor(leftCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, false, queryName);
                        rightCollectionExecutor = buildCollectionExecutor(rightCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, false, queryName);
                        return new AnyAndCollectionExecutor(rightCollectionExecutor, leftCollectionExecutor, exhaustiveCollectionExecutor);
                    case INDEXED_RESULT_SET:
                    case PRIMARY_KEY_RESULT_SET:
                    case OPTIMISED_PRIMARY_KEY_OR_INDEXED_RESULT_SET:
                        exhaustiveCollectionExecutor = new ExhaustiveCollectionExecutor(ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName), matchingMetaInfoHolder.getStoreEventIndex());
                        leftCollectionExecutor = buildCollectionExecutor(leftCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, false, queryName);
                        rightCollectionExecutor = buildCollectionExecutor(rightCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, false, queryName);
                        return new AnyAndCollectionExecutor(leftCollectionExecutor, rightCollectionExecutor, exhaustiveCollectionExecutor);
                    case PARTIAL_PRIMARY_KEY_RESULT_SET:
                    case EXHAUSTIVE:
                        leftCollectionExecutor = buildCollectionExecutor(leftCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, isFirst, queryName);
                        if (isFirst || leftCollectionExecutor.getDefaultCost() == CollectionExecutor.Cost.SINGLE_RETURN_INDEX_MATCHING) {
                            exhaustiveCollectionExecutor = new ExhaustiveCollectionExecutor(ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName), matchingMetaInfoHolder.getStoreEventIndex());
                        }
                        return new CompareExhaustiveAndCollectionExecutor(leftCollectionExecutor, exhaustiveCollectionExecutor);
                }
                break;
            case PARTIAL_PRIMARY_KEY_RESULT_SET:
            case EXHAUSTIVE:
                switch(rightCollectionExpression.getCollectionScope()) {
                    case NON:
                        expressionExecutor = ExpressionParser.parseExpression(rightCollectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
                        aCollectionExecutor = buildCollectionExecutor(leftCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, isFirst, queryName);
                        return new NonAndCollectionExecutor(expressionExecutor, aCollectionExecutor, rightCollectionExpression.getCollectionScope());
                    case INDEXED_ATTRIBUTE:
                    case INDEXED_RESULT_SET:
                    case PRIMARY_KEY_ATTRIBUTE:
                    case PRIMARY_KEY_RESULT_SET:
                    case OPTIMISED_PRIMARY_KEY_OR_INDEXED_RESULT_SET:
                        rightCollectionExecutor = buildCollectionExecutor(rightCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, isFirst, queryName);
                        if (isFirst || rightCollectionExecutor.getDefaultCost() == CollectionExecutor.Cost.SINGLE_RETURN_INDEX_MATCHING) {
                            exhaustiveCollectionExecutor = new ExhaustiveCollectionExecutor(ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName), matchingMetaInfoHolder.getStoreEventIndex());
                        }
                        return new CompareExhaustiveAndCollectionExecutor(rightCollectionExecutor, exhaustiveCollectionExecutor);
                    case PARTIAL_PRIMARY_KEY_RESULT_SET:
                    case EXHAUSTIVE:
                        if (isFirst) {
                            expressionExecutor = ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
                        }
                        return new ExhaustiveCollectionExecutor(expressionExecutor, matchingMetaInfoHolder.getStoreEventIndex());
                }
                break;
        }
    } else if (collectionExpression instanceof OrCollectionExpression) {
        CollectionExpression leftCollectionExpression = ((OrCollectionExpression) collectionExpression).getLeftCollectionExpression();
        CollectionExpression rightCollectionExpression = ((OrCollectionExpression) collectionExpression).getRightCollectionExpression();
        ExpressionExecutor expressionExecutor = null;
        CollectionExecutor aCollectionExecutor = null;
        CollectionExecutor leftCollectionExecutor;
        CollectionExecutor rightCollectionExecutor;
        if (leftCollectionExpression.getCollectionScope() == NON && rightCollectionExpression.getCollectionScope() == NON) {
            expressionExecutor = ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
            return new NonCollectionExecutor(expressionExecutor);
        } else if ((leftCollectionExpression.getCollectionScope() == EXHAUSTIVE && leftCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_RESULT_SET) || (rightCollectionExpression.getCollectionScope() == EXHAUSTIVE && rightCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_RESULT_SET)) {
            if (isFirst) {
                expressionExecutor = ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
            }
            return new ExhaustiveCollectionExecutor(expressionExecutor, matchingMetaInfoHolder.getStoreEventIndex());
        } else {
            if (isFirst) {
                aCollectionExecutor = new ExhaustiveCollectionExecutor(ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName), matchingMetaInfoHolder.getStoreEventIndex());
            }
            leftCollectionExecutor = buildCollectionExecutor(leftCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, isFirst, queryName);
            rightCollectionExecutor = buildCollectionExecutor(rightCollectionExpression, matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, isFirst, queryName);
            return new OrCollectionExecutor(leftCollectionExecutor, rightCollectionExecutor, aCollectionExecutor);
        }
    } else if (collectionExpression instanceof NotCollectionExpression) {
        ExpressionExecutor expressionExecutor = null;
        switch(collectionExpression.getCollectionScope()) {
            case NON:
                expressionExecutor = ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
                return new NonCollectionExecutor(expressionExecutor);
            case INDEXED_ATTRIBUTE:
            case INDEXED_RESULT_SET:
            case PRIMARY_KEY_ATTRIBUTE:
            case PRIMARY_KEY_RESULT_SET:
            case OPTIMISED_PRIMARY_KEY_OR_INDEXED_RESULT_SET:
                ExhaustiveCollectionExecutor exhaustiveCollectionExecutor = null;
                if (isFirst) {
                    exhaustiveCollectionExecutor = new ExhaustiveCollectionExecutor(ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName), matchingMetaInfoHolder.getStoreEventIndex());
                }
                CollectionExecutor notCollectionExecutor = buildCollectionExecutor(((NotCollectionExpression) collectionExpression).getCollectionExpression(), matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiAppContext, isFirst, queryName);
                return new NotCollectionExecutor(notCollectionExecutor, exhaustiveCollectionExecutor);
            case PARTIAL_PRIMARY_KEY_RESULT_SET:
            case EXHAUSTIVE:
                if (isFirst) {
                    expressionExecutor = ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
                }
                return new ExhaustiveCollectionExecutor(expressionExecutor, matchingMetaInfoHolder.getStoreEventIndex());
        }
    } else {
        // Basic
        ExpressionExecutor expressionExecutor = null;
        if (collectionExpression.getCollectionScope() == NON) {
            expressionExecutor = ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
            return new NonCollectionExecutor(expressionExecutor);
        } else {
            // EXHAUSTIVE
            if (isFirst) {
                expressionExecutor = ExpressionParser.parseExpression(collectionExpression.getExpression(), matchingMetaInfoHolder.getMetaStateEvent(), matchingMetaInfoHolder.getCurrentState(), tableMap, variableExpressionExecutors, siddhiAppContext, false, 0, queryName);
            }
            return new ExhaustiveCollectionExecutor(expressionExecutor, matchingMetaInfoHolder.getStoreEventIndex());
        }
    }
    throw new UnsupportedOperationException(collectionExpression.getClass().getName() + " not supported!");
}
Also used : Attribute(org.wso2.siddhi.query.api.definition.Attribute) AttributeCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AttributeCollectionExpression) CompareCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.CompareCollectionExecutor) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor) CompareExhaustiveAndCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.CompareExhaustiveAndCollectionExecutor) AndCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AndCollectionExpression) NullCollectionExpression(org.wso2.siddhi.core.util.collection.expression.NullCollectionExpression) List(java.util.List) ArrayList(java.util.ArrayList) NotCollectionExpression(org.wso2.siddhi.core.util.collection.expression.NotCollectionExpression) AndCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AndCollectionExpression) BasicCollectionExpression(org.wso2.siddhi.core.util.collection.expression.BasicCollectionExpression) CompareCollectionExpression(org.wso2.siddhi.core.util.collection.expression.CompareCollectionExpression) CollectionExpression(org.wso2.siddhi.core.util.collection.expression.CollectionExpression) OrCollectionExpression(org.wso2.siddhi.core.util.collection.expression.OrCollectionExpression) AttributeCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AttributeCollectionExpression) AndMultiPrimaryKeyCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AndMultiPrimaryKeyCollectionExpression) NullCollectionExpression(org.wso2.siddhi.core.util.collection.expression.NullCollectionExpression) NonCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.NonCollectionExecutor) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) ExhaustiveCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.ExhaustiveCollectionExecutor) NotCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.NotCollectionExecutor) AndMultiPrimaryKeyCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AndMultiPrimaryKeyCollectionExpression) AndMultiPrimaryKeyCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.AndMultiPrimaryKeyCollectionExecutor) OrCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.OrCollectionExecutor) NonAndCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.NonAndCollectionExecutor) CompareCollectionExpression(org.wso2.siddhi.core.util.collection.expression.CompareCollectionExpression) OrCollectionExpression(org.wso2.siddhi.core.util.collection.expression.OrCollectionExpression) ExhaustiveCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.ExhaustiveCollectionExecutor) OrCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.OrCollectionExecutor) AndMultiPrimaryKeyCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.AndMultiPrimaryKeyCollectionExecutor) NonAndCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.NonAndCollectionExecutor) CompareExhaustiveAndCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.CompareExhaustiveAndCollectionExecutor) CollectionExecutor(org.wso2.siddhi.core.util.collection.executor.CollectionExecutor) NotCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.NotCollectionExecutor) NonCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.NonCollectionExecutor) CompareCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.CompareCollectionExecutor) AnyAndCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.AnyAndCollectionExecutor) Map(java.util.Map) HashMap(java.util.HashMap) AnyAndCollectionExecutor(org.wso2.siddhi.core.util.collection.executor.AnyAndCollectionExecutor) NotCollectionExpression(org.wso2.siddhi.core.util.collection.expression.NotCollectionExpression)

Example 2 with OrCollectionExpression

use of org.wso2.siddhi.core.util.collection.expression.OrCollectionExpression 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() == NON && rightCollectionExpression.getCollectionScope() == NON) {
            return new BasicCollectionExpression(expression, NON);
        } else if ((leftCollectionExpression.getCollectionScope() == PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == PRIMARY_KEY_RESULT_SET || leftCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_RESULT_SET) && (rightCollectionExpression.getCollectionScope() == PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == PRIMARY_KEY_RESULT_SET || rightCollectionExpression.getCollectionScope() == 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, PRIMARY_KEY_RESULT_SET, leftCollectionExpression, rightCollectionExpression);
            } else {
                return new AndCollectionExpression(expression, 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() == PARTIAL_PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_RESULT_SET || leftCollectionExpression.getCollectionScope() == NON || leftCollectionExpression.getCollectionScope() == EXHAUSTIVE) && (rightCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_RESULT_SET || rightCollectionExpression.getCollectionScope() == NON || rightCollectionExpression.getCollectionScope() == EXHAUSTIVE)) {
            return new BasicCollectionExpression(expression, EXHAUSTIVE);
        } else {
            return new AndCollectionExpression(expression, 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() == NON && rightCollectionExpression.getCollectionScope() == NON) {
            return new BasicCollectionExpression(expression, NON);
        } else if (leftCollectionExpression.getCollectionScope() == EXHAUSTIVE || leftCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_RESULT_SET || rightCollectionExpression.getCollectionScope() == EXHAUSTIVE || rightCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_RESULT_SET) {
            return new BasicCollectionExpression(expression, EXHAUSTIVE);
        } else {
            return new OrCollectionExpression(expression, 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, NON);
            case PRIMARY_KEY_ATTRIBUTE:
                return new NotCollectionExpression(expression, PRIMARY_KEY_RESULT_SET, notCollectionExpression);
            case INDEXED_ATTRIBUTE:
                return new NotCollectionExpression(expression, 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, 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, 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() == NON && rightCollectionExpression.getCollectionScope() == NON) {
            // comparing two stream attributes with O(1) time complexity
            return new BasicCollectionExpression(expression, NON);
        } else if ((leftCollectionExpression.getCollectionScope() == INDEXED_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == PRIMARY_KEY_ATTRIBUTE || leftCollectionExpression.getCollectionScope() == PARTIAL_PRIMARY_KEY_ATTRIBUTE) && rightCollectionExpression.getCollectionScope() == NON) {
            switch(leftCollectionExpression.getCollectionScope()) {
                case INDEXED_ATTRIBUTE:
                    return new CompareCollectionExpression((Compare) expression, INDEXED_RESULT_SET, leftCollectionExpression, ((Compare) expression).getOperator(), rightCollectionExpression);
                case PRIMARY_KEY_ATTRIBUTE:
                    return new CompareCollectionExpression((Compare) expression, PRIMARY_KEY_RESULT_SET, leftCollectionExpression, ((Compare) expression).getOperator(), rightCollectionExpression);
                case PARTIAL_PRIMARY_KEY_ATTRIBUTE:
                    return new CompareCollectionExpression((Compare) expression, PARTIAL_PRIMARY_KEY_RESULT_SET, leftCollectionExpression, ((Compare) expression).getOperator(), rightCollectionExpression);
            }
        } else if (leftCollectionExpression.getCollectionScope() == NON && (rightCollectionExpression.getCollectionScope() == INDEXED_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == PRIMARY_KEY_ATTRIBUTE || rightCollectionExpression.getCollectionScope() == 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, INDEXED_RESULT_SET, rightCollectionExpression, operator, leftCollectionExpression);
                case PRIMARY_KEY_ATTRIBUTE:
                    return new CompareCollectionExpression((Compare) expression, PRIMARY_KEY_RESULT_SET, rightCollectionExpression, operator, leftCollectionExpression);
                case PARTIAL_PRIMARY_KEY_ATTRIBUTE:
                    return new CompareCollectionExpression((Compare) expression, PARTIAL_PRIMARY_KEY_RESULT_SET, rightCollectionExpression, operator, leftCollectionExpression);
            }
        } else {
            // comparing non indexed table with stream attributes or another table attribute
            return new BasicCollectionExpression(expression, EXHAUSTIVE);
        }
    } else if (expression instanceof Constant) {
        return new BasicCollectionExpression(expression, NON);
    } else if (expression instanceof Variable) {
        if (isCollectionVariable(matchingMetaInfoHolder, (Variable) expression)) {
            if (indexedEventHolder.isAttributeIndexed(((Variable) expression).getAttributeName())) {
                return new AttributeCollectionExpression(expression, ((Variable) expression).getAttributeName(), INDEXED_ATTRIBUTE);
            } else if (indexedEventHolder.isMultiPrimaryKeyAttribute(((Variable) expression).getAttributeName())) {
                if (indexedEventHolder.getPrimaryKeyReferenceHolders() != null && indexedEventHolder.getPrimaryKeyReferenceHolders().length == 1) {
                    return new AttributeCollectionExpression(expression, ((Variable) expression).getAttributeName(), PRIMARY_KEY_ATTRIBUTE);
                } else {
                    return new AttributeCollectionExpression(expression, ((Variable) expression).getAttributeName(), PARTIAL_PRIMARY_KEY_ATTRIBUTE);
                }
            } else {
                return new BasicCollectionExpression(expression, EXHAUSTIVE);
            }
        } else {
            return new BasicCollectionExpression(expression, 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() == NON && right.getCollectionScope() == NON) {
            return new BasicCollectionExpression(expression, NON);
        } else {
            return new BasicCollectionExpression(expression, 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() == NON && right.getCollectionScope() == NON) {
            return new BasicCollectionExpression(expression, NON);
        } else {
            return new BasicCollectionExpression(expression, 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() == NON && right.getCollectionScope() == NON) {
            return new BasicCollectionExpression(expression, NON);
        } else {
            return new BasicCollectionExpression(expression, 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() == NON && right.getCollectionScope() == NON) {
            return new BasicCollectionExpression(expression, NON);
        } else {
            return new BasicCollectionExpression(expression, 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() == NON && right.getCollectionScope() == NON) {
            return new BasicCollectionExpression(expression, NON);
        } else {
            return new BasicCollectionExpression(expression, EXHAUSTIVE);
        }
    } else if (expression instanceof AttributeFunction) {
        Expression[] innerExpressions = ((AttributeFunction) expression).getParameters();
        for (Expression aExpression : innerExpressions) {
            CollectionExpression aCollectionExpression = parseInternalCollectionExpression(aExpression, matchingMetaInfoHolder, indexedEventHolder);
            if (aCollectionExpression.getCollectionScope() != NON) {
                return new BasicCollectionExpression(expression, EXHAUSTIVE);
            }
        }
        return new BasicCollectionExpression(expression, NON);
    } else if (expression instanceof In) {
        CollectionExpression inCollectionExpression = parseInternalCollectionExpression(((In) expression).getExpression(), matchingMetaInfoHolder, indexedEventHolder);
        if (inCollectionExpression.getCollectionScope() != NON) {
            return new BasicCollectionExpression(expression, EXHAUSTIVE);
        }
        return new BasicCollectionExpression(expression, NON);
    } else if (expression instanceof IsNull) {
        CollectionExpression nullCollectionExpression = parseInternalCollectionExpression(((IsNull) expression).getExpression(), matchingMetaInfoHolder, indexedEventHolder);
        if (nullCollectionExpression.getCollectionScope() == NON) {
            return new BasicCollectionExpression(expression, NON);
        } else if (nullCollectionExpression.getCollectionScope() == INDEXED_ATTRIBUTE) {
            return new NullCollectionExpression(expression, INDEXED_RESULT_SET, ((AttributeCollectionExpression) nullCollectionExpression).getAttribute());
        } else if (nullCollectionExpression.getCollectionScope() == PRIMARY_KEY_ATTRIBUTE) {
            return new NullCollectionExpression(expression, PRIMARY_KEY_RESULT_SET, ((AttributeCollectionExpression) nullCollectionExpression).getAttribute());
        } else {
            return new BasicCollectionExpression(expression, EXHAUSTIVE);
        }
    }
    throw new UnsupportedOperationException(expression.toString() + " not supported!");
}
Also used : Add(org.wso2.siddhi.query.api.expression.math.Add) Set(java.util.Set) HashSet(java.util.HashSet) Or(org.wso2.siddhi.query.api.expression.condition.Or) Variable(org.wso2.siddhi.query.api.expression.Variable) BasicCollectionExpression(org.wso2.siddhi.core.util.collection.expression.BasicCollectionExpression) In(org.wso2.siddhi.query.api.expression.condition.In) Constant(org.wso2.siddhi.query.api.expression.constant.Constant) AttributeCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AttributeCollectionExpression) Divide(org.wso2.siddhi.query.api.expression.math.Divide) AndCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AndCollectionExpression) Multiply(org.wso2.siddhi.query.api.expression.math.Multiply) NullCollectionExpression(org.wso2.siddhi.core.util.collection.expression.NullCollectionExpression) Compare(org.wso2.siddhi.query.api.expression.condition.Compare) NotCollectionExpression(org.wso2.siddhi.core.util.collection.expression.NotCollectionExpression) AndCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AndCollectionExpression) BasicCollectionExpression(org.wso2.siddhi.core.util.collection.expression.BasicCollectionExpression) CompareCollectionExpression(org.wso2.siddhi.core.util.collection.expression.CompareCollectionExpression) CollectionExpression(org.wso2.siddhi.core.util.collection.expression.CollectionExpression) OrCollectionExpression(org.wso2.siddhi.core.util.collection.expression.OrCollectionExpression) AttributeCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AttributeCollectionExpression) AndMultiPrimaryKeyCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AndMultiPrimaryKeyCollectionExpression) NullCollectionExpression(org.wso2.siddhi.core.util.collection.expression.NullCollectionExpression) Mod(org.wso2.siddhi.query.api.expression.math.Mod) AndMultiPrimaryKeyCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AndMultiPrimaryKeyCollectionExpression) AttributeFunction(org.wso2.siddhi.query.api.expression.AttributeFunction) Not(org.wso2.siddhi.query.api.expression.condition.Not) CompareCollectionExpression(org.wso2.siddhi.core.util.collection.expression.CompareCollectionExpression) NotCollectionExpression(org.wso2.siddhi.core.util.collection.expression.NotCollectionExpression) AndCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AndCollectionExpression) BasicCollectionExpression(org.wso2.siddhi.core.util.collection.expression.BasicCollectionExpression) CompareCollectionExpression(org.wso2.siddhi.core.util.collection.expression.CompareCollectionExpression) CollectionExpression(org.wso2.siddhi.core.util.collection.expression.CollectionExpression) OrCollectionExpression(org.wso2.siddhi.core.util.collection.expression.OrCollectionExpression) AttributeCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AttributeCollectionExpression) Expression(org.wso2.siddhi.query.api.expression.Expression) AndMultiPrimaryKeyCollectionExpression(org.wso2.siddhi.core.util.collection.expression.AndMultiPrimaryKeyCollectionExpression) NullCollectionExpression(org.wso2.siddhi.core.util.collection.expression.NullCollectionExpression) And(org.wso2.siddhi.query.api.expression.condition.And) OrCollectionExpression(org.wso2.siddhi.core.util.collection.expression.OrCollectionExpression) Subtract(org.wso2.siddhi.query.api.expression.math.Subtract) IsNull(org.wso2.siddhi.query.api.expression.condition.IsNull) NotCollectionExpression(org.wso2.siddhi.core.util.collection.expression.NotCollectionExpression)

Aggregations

AndCollectionExpression (org.wso2.siddhi.core.util.collection.expression.AndCollectionExpression)2 AndMultiPrimaryKeyCollectionExpression (org.wso2.siddhi.core.util.collection.expression.AndMultiPrimaryKeyCollectionExpression)2 AttributeCollectionExpression (org.wso2.siddhi.core.util.collection.expression.AttributeCollectionExpression)2 BasicCollectionExpression (org.wso2.siddhi.core.util.collection.expression.BasicCollectionExpression)2 CollectionExpression (org.wso2.siddhi.core.util.collection.expression.CollectionExpression)2 CompareCollectionExpression (org.wso2.siddhi.core.util.collection.expression.CompareCollectionExpression)2 NotCollectionExpression (org.wso2.siddhi.core.util.collection.expression.NotCollectionExpression)2 NullCollectionExpression (org.wso2.siddhi.core.util.collection.expression.NullCollectionExpression)2 OrCollectionExpression (org.wso2.siddhi.core.util.collection.expression.OrCollectionExpression)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)1 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)1 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)1 AndMultiPrimaryKeyCollectionExecutor (org.wso2.siddhi.core.util.collection.executor.AndMultiPrimaryKeyCollectionExecutor)1 AnyAndCollectionExecutor (org.wso2.siddhi.core.util.collection.executor.AnyAndCollectionExecutor)1