Search in sources :

Example 1 with ValueIteratorSource

use of org.teiid.query.sql.util.ValueIteratorSource in project teiid by teiid.

the class Evaluator method evaluate.

private Boolean evaluate(AbstractSetCriteria criteria, List<?> tuple) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
    // Evaluate expression
    Object leftValue = null;
    try {
        leftValue = evaluate(criteria.getExpression(), tuple);
    } catch (ExpressionEvaluationException e) {
        throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30323, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30323, criteria));
    }
    Boolean result = Boolean.FALSE;
    ValueIterator valueIter = null;
    if (criteria instanceof SetCriteria) {
        SetCriteria set = (SetCriteria) criteria;
        // Shortcut if null
        if (leftValue == null) {
            if (!set.getValues().isEmpty()) {
                return null;
            }
            return criteria.isNegated();
        }
        if (set.isAllConstants()) {
            boolean exists = set.getValues().contains(new Constant(leftValue, criteria.getExpression().getType()));
            if (!exists) {
                if (set.getValues().contains(Constant.NULL_CONSTANT)) {
                    return null;
                }
                return criteria.isNegated();
            }
            return !criteria.isNegated();
        }
        valueIter = new CollectionValueIterator(((SetCriteria) criteria).getValues());
    } else if (criteria instanceof DependentSetCriteria) {
        DependentSetCriteria ref = (DependentSetCriteria) criteria;
        VariableContext vc = getContext(criteria).getVariableContext();
        ValueIteratorSource vis = (ValueIteratorSource) vc.getGlobalValue(ref.getContextSymbol());
        if (leftValue == null) {
            return null;
        }
        Set<Object> values;
        try {
            values = vis.getCachedSet(ref.getValueExpression());
        } catch (TeiidProcessingException e) {
            throw new ExpressionEvaluationException(e);
        }
        if (values != null) {
            return values.contains(leftValue);
        }
        vis.setUnused(true);
        // them in memory
        return true;
    } else if (criteria instanceof SubquerySetCriteria) {
        try {
            valueIter = evaluateSubquery((SubquerySetCriteria) criteria, tuple);
        } catch (TeiidProcessingException e) {
            throw new ExpressionEvaluationException(e);
        }
    } else {
        // $NON-NLS-1$
        throw new AssertionError("unknown set criteria type");
    }
    while (valueIter.hasNext()) {
        if (leftValue == null) {
            return null;
        }
        Object possibleValue = valueIter.next();
        Object value = null;
        if (possibleValue instanceof Expression) {
            try {
                value = evaluate((Expression) possibleValue, tuple);
            } catch (ExpressionEvaluationException e) {
                throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30323, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30323, possibleValue));
            }
        } else {
            value = possibleValue;
        }
        if (value != null) {
            if (Constant.COMPARATOR.compare(leftValue, value) == 0) {
                return Boolean.valueOf(!criteria.isNegated());
            }
        // else try next value
        } else {
            result = null;
        }
    }
    if (result == null) {
        return null;
    }
    return Boolean.valueOf(criteria.isNegated());
}
Also used : ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) Set(java.util.Set) ValueIterator(org.teiid.query.sql.util.ValueIterator) VariableContext(org.teiid.query.sql.util.VariableContext) TeiidProcessingException(org.teiid.core.TeiidProcessingException) ValueIteratorSource(org.teiid.query.sql.util.ValueIteratorSource) ExceptionExpression(org.teiid.query.sql.proc.ExceptionExpression) LanguageObject(org.teiid.query.sql.LanguageObject)

Aggregations

Set (java.util.Set)1 ExpressionEvaluationException (org.teiid.api.exception.query.ExpressionEvaluationException)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 LanguageObject (org.teiid.query.sql.LanguageObject)1 ExceptionExpression (org.teiid.query.sql.proc.ExceptionExpression)1 ValueIterator (org.teiid.query.sql.util.ValueIterator)1 ValueIteratorSource (org.teiid.query.sql.util.ValueIteratorSource)1 VariableContext (org.teiid.query.sql.util.VariableContext)1