Search in sources :

Example 1 with ValueIterator

use of org.teiid.query.sql.util.ValueIterator 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)

Example 2 with ValueIterator

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

the class Evaluator method evaluate.

private Object evaluate(ScalarSubquery scalarSubquery, List<?> tuple) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
    Object result = null;
    ValueIterator valueIter;
    try {
        valueIter = evaluateSubquery(scalarSubquery, tuple);
    } catch (TeiidProcessingException e) {
        throw new ExpressionEvaluationException(e);
    }
    if (valueIter.hasNext()) {
        result = valueIter.next();
        if (valueIter.hasNext()) {
            // more than one result value - this is an exception case
            throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30345, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30345, scalarSubquery.getCommand()));
        }
    }
    return result;
}
Also used : ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) ValueIterator(org.teiid.query.sql.util.ValueIterator) LanguageObject(org.teiid.query.sql.LanguageObject) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 3 with ValueIterator

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

the class Evaluator method evaluate.

private Boolean evaluate(SubqueryCompareCriteria criteria, List<?> tuple) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
    // Evaluate expression
    Object leftValue = null;
    try {
        leftValue = evaluate(criteria.getLeftExpression(), tuple);
    } catch (ExpressionEvaluationException e) {
        throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30323, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30323, criteria));
    }
    // Need to be careful to initialize this variable carefully for the case
    // where valueIterator has no values, and the block below is not entered.
    // If there are no rows, and ALL is the predicate quantifier, the result
    // should be true.  If SOME is the predicate quantifier, or no quantifier
    // is used, the result should be false.
    Boolean result = Boolean.FALSE;
    if (criteria.getPredicateQuantifier() == SubqueryCompareCriteria.ALL) {
        result = Boolean.TRUE;
    }
    ValueIterator valueIter;
    if (criteria.getCommand() != null) {
        try {
            valueIter = evaluateSubquery(criteria, tuple);
        } catch (TeiidProcessingException e) {
            throw new ExpressionEvaluationException(e);
        }
    } else {
        Object array = evaluate(criteria.getArrayExpression(), tuple);
        final Object[] vals;
        if (array instanceof Object[]) {
            vals = (Object[]) array;
        } else {
            ArrayImpl arrayImpl = (ArrayImpl) array;
            vals = arrayImpl.getValues();
        }
        valueIter = new ValueIterator() {

            int index = 0;

            @Override
            public void reset() {
                index = 0;
            }

            @Override
            public boolean hasNext() {
                return index < vals.length;
            }

            @Override
            public Object next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                return vals[index++];
            }
        };
    }
    while (valueIter.hasNext()) {
        Object value = valueIter.next();
        // Shortcut if null
        if (leftValue == null) {
            return null;
        }
        if (value != null) {
            Boolean comp = compare(criteria.getOperator(), leftValue, value);
            switch(criteria.getPredicateQuantifier()) {
                case SubqueryCompareCriteria.ALL:
                    if (Boolean.FALSE.equals(comp)) {
                        return Boolean.FALSE;
                    }
                    break;
                case SubqueryCompareCriteria.SOME:
                    if (Boolean.TRUE.equals(comp)) {
                        return Boolean.TRUE;
                    }
                    break;
                default:
                    throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30326, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30326, criteria.getPredicateQuantifier()));
            }
        } else {
            // value is null
            switch(criteria.getPredicateQuantifier()) {
                case SubqueryCompareCriteria.ALL:
                    if (Boolean.TRUE.equals(result)) {
                        result = null;
                    }
                    break;
                case SubqueryCompareCriteria.SOME:
                    if (Boolean.FALSE.equals(result)) {
                        result = null;
                    }
                    break;
                default:
                    throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30326, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30326, criteria.getPredicateQuantifier()));
            }
        }
    }
    return result;
}
Also used : ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) ValueIterator(org.teiid.query.sql.util.ValueIterator) LanguageObject(org.teiid.query.sql.LanguageObject) NoSuchElementException(java.util.NoSuchElementException) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Aggregations

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