Search in sources :

Example 1 with ExpressionEvaluationException

use of org.teiid.api.exception.query.ExpressionEvaluationException in project teiid by teiid.

the class Evaluator method evaluate.

private Boolean evaluate(MatchCriteria criteria, List<?> tuple) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
    boolean result = false;
    // Evaluate left expression
    Object value = null;
    try {
        value = evaluate(criteria.getLeftExpression(), tuple);
    } catch (ExpressionEvaluationException e) {
        // $NON-NLS-1$
        throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30312, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30312, "left", criteria));
    }
    // Shortcut if null
    if (value == null) {
        return null;
    }
    CharSequence leftValue = null;
    if (value instanceof CharSequence) {
        leftValue = (CharSequence) value;
    } else {
        try {
            leftValue = ((Sequencable) value).getCharSequence();
        } catch (SQLException err) {
            throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30316, err, err.getMessage());
        }
    }
    // Evaluate right expression
    String rightValue = null;
    try {
        rightValue = (String) evaluate(criteria.getRightExpression(), tuple);
    } catch (ExpressionEvaluationException e) {
        // $NON-NLS-1$
        throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30312, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30312, "right", criteria));
    }
    // Shortcut if null
    if (rightValue == null) {
        return null;
    }
    result = match(rightValue, criteria.getEscapeChar(), leftValue, criteria.getMode());
    return Boolean.valueOf(result ^ criteria.isNegated());
}
Also used : ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) SQLException(java.sql.SQLException) LanguageObject(org.teiid.query.sql.LanguageObject)

Example 2 with ExpressionEvaluationException

use of org.teiid.api.exception.query.ExpressionEvaluationException in project teiid by teiid.

the class Evaluator method evaluateXMLParse.

private Object evaluateXMLParse(List<?> tuple, final XMLParse xp) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
    Object value = internalEvaluate(xp.getExpression(), tuple);
    if (value == null) {
        return null;
    }
    XMLType.Type type = Type.DOCUMENT;
    SQLXMLImpl result = null;
    try {
        if (value instanceof String) {
            String string = (String) value;
            result = new SQLXMLImpl(string);
            result.setCharset(Streamable.CHARSET);
            if (!xp.isWellFormed()) {
                Reader r = new StringReader(string);
                type = validate(xp, r);
            }
        } else if (value instanceof BinaryType) {
            BinaryType string = (BinaryType) value;
            result = new SQLXMLImpl(string.getBytesDirect());
            result.setCharset(Streamable.CHARSET);
            if (!xp.isWellFormed()) {
                Reader r = result.getCharacterStream();
                type = validate(xp, r);
            }
        } else {
            InputStreamFactory isf = null;
            Streamable<?> s = (Streamable<?>) value;
            isf = getInputStreamFactory(s);
            result = new SQLXMLImpl(isf);
            if (!xp.isWellFormed()) {
                Reader r = result.getCharacterStream();
                type = validate(xp, r);
            }
        }
    } catch (TransformationException e) {
        throw new ExpressionEvaluationException(e);
    } catch (SQLException e) {
        throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30331, e, e.getMessage());
    }
    if (!xp.isDocument()) {
        type = Type.CONTENT;
    }
    XMLType xml = new XMLType(result);
    xml.setType(type);
    return xml;
}
Also used : ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) TeiidSQLException(org.teiid.jdbc.TeiidSQLException) SQLException(java.sql.SQLException) Reader(java.io.Reader) StringReader(java.io.StringReader) StringReader(java.io.StringReader) Type(org.teiid.core.types.XMLType.Type) LanguageObject(org.teiid.query.sql.LanguageObject)

Example 3 with ExpressionEvaluationException

use of org.teiid.api.exception.query.ExpressionEvaluationException in project teiid by teiid.

the class Evaluator method internalEvaluate.

protected Object internalEvaluate(Expression expression, List<?> tuple) throws ExpressionEvaluationException, BlockedException, TeiidComponentException {
    if (expression instanceof DerivedExpression) {
        if (elements != null) {
            // Try to evaluate by lookup in the elements map (may work for both ElementSymbol and ExpressionSymbol
            Integer index = (Integer) elements.get(expression);
            if (index != null) {
                return tuple.get(index.intValue());
            }
        }
        // Otherwise this should be an ExpressionSymbol and we just need to dive in and evaluate the expression itself
        if (expression instanceof ExpressionSymbol) {
            ExpressionSymbol exprSyb = (ExpressionSymbol) expression;
            Expression expr = exprSyb.getExpression();
            return internalEvaluate(expr, tuple);
        }
        return getContext(expression).getFromContext(expression);
    }
    if (expression instanceof Constant) {
        Constant c = (Constant) expression;
        if (c.isMultiValued()) {
            // $NON-NLS-1$
            throw new AssertionError("Multi-valued constant not allowed to be directly evaluated");
        }
        return c.getValue();
    } else if (expression instanceof Function) {
        return evaluate((Function) expression, tuple);
    } else if (expression instanceof CaseExpression) {
        return evaluate((CaseExpression) expression, tuple);
    } else if (expression instanceof SearchedCaseExpression) {
        return evaluate((SearchedCaseExpression) expression, tuple);
    } else if (expression instanceof Reference) {
        Reference ref = (Reference) expression;
        if (ref.isPositional() && ref.getExpression() == null) {
            return getContext(ref).getVariableContext().getGlobalValue(ref.getContextSymbol());
        }
        Object result = getContext(ref.getExpression()).getFromContext(ref.getExpression());
        if (ref.getConstraint() != null) {
            try {
                ref.getConstraint().validate(result);
            } catch (QueryValidatorException e) {
                throw new ExpressionEvaluationException(e);
            }
        }
        return result;
    } else if (expression instanceof Criteria) {
        return evaluate((Criteria) expression, tuple);
    } else if (expression instanceof ScalarSubquery) {
        return evaluate((ScalarSubquery) expression, tuple);
    } else if (expression instanceof Criteria) {
        return evaluate((Criteria) expression, tuple);
    } else if (expression instanceof TextLine) {
        return evaluateTextLine(tuple, (TextLine) expression);
    } else if (expression instanceof XMLElement) {
        return evaluateXMLElement(tuple, (XMLElement) expression);
    } else if (expression instanceof XMLForest) {
        return evaluateXMLForest(tuple, (XMLForest) expression);
    } else if (expression instanceof JSONObject) {
        return evaluateJSONObject(tuple, (JSONObject) expression, null);
    } else if (expression instanceof XMLSerialize) {
        return evaluateXMLSerialize(tuple, (XMLSerialize) expression);
    } else if (expression instanceof XMLQuery) {
        return evaluateXMLQuery(tuple, (XMLQuery) expression, false);
    } else if (expression instanceof QueryString) {
        return evaluateQueryString(tuple, (QueryString) expression);
    } else if (expression instanceof XMLParse) {
        return evaluateXMLParse(tuple, (XMLParse) expression);
    } else if (expression instanceof Array) {
        Array array = (Array) expression;
        List<Expression> exprs = array.getExpressions();
        Object[] result = (Object[]) java.lang.reflect.Array.newInstance(array.getComponentType(), exprs.size());
        for (int i = 0; i < exprs.size(); i++) {
            Object eval = internalEvaluate(exprs.get(i), tuple);
            if (eval instanceof ArrayImpl) {
                eval = ((ArrayImpl) eval).getValues();
            }
            result[i] = eval;
        }
        return new ArrayImpl(result);
    } else if (expression instanceof ExceptionExpression) {
        return evaluate(tuple, (ExceptionExpression) expression);
    } else if (expression instanceof XMLCast) {
        return evaluate(tuple, (XMLCast) expression);
    } else {
        throw new TeiidComponentException(QueryPlugin.Event.TEIID30329, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30329, expression.getClass().getName()));
    }
}
Also used : ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) ExceptionExpression(org.teiid.query.sql.proc.ExceptionExpression) ExceptionExpression(org.teiid.query.sql.proc.ExceptionExpression) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) LanguageObject(org.teiid.query.sql.LanguageObject) TeiidComponentException(org.teiid.core.TeiidComponentException)

Example 4 with ExpressionEvaluationException

use of org.teiid.api.exception.query.ExpressionEvaluationException in project teiid by teiid.

the class Evaluator method evaluateTextLine.

private Object evaluateTextLine(List<?> tuple, TextLine function) throws ExpressionEvaluationException, BlockedException, TeiidComponentException, FunctionExecutionException {
    List<DerivedColumn> args = function.getExpressions();
    Evaluator.NameValuePair<Object>[] nameValuePairs = getNameValuePairs(tuple, args, true, true);
    try {
        return new ArrayImpl(TextLine.evaluate(Arrays.asList(nameValuePairs), defaultExtractor, function));
    } catch (TransformationException e) {
        throw new ExpressionEvaluationException(e);
    } catch (TeiidProcessingException e) {
        throw new ExpressionEvaluationException(e);
    }
}
Also used : ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 5 with ExpressionEvaluationException

use of org.teiid.api.exception.query.ExpressionEvaluationException 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

ExpressionEvaluationException (org.teiid.api.exception.query.ExpressionEvaluationException)23 LanguageObject (org.teiid.query.sql.LanguageObject)11 TeiidComponentException (org.teiid.core.TeiidComponentException)7 TeiidProcessingException (org.teiid.core.TeiidProcessingException)7 ArrayList (java.util.ArrayList)5 SQLException (java.sql.SQLException)4 BlockedException (org.teiid.common.buffer.BlockedException)4 TeiidSQLException (org.teiid.jdbc.TeiidSQLException)4 List (java.util.List)3 NoSuchElementException (java.util.NoSuchElementException)3 Test (org.junit.Test)3 QueryValidatorException (org.teiid.api.exception.query.QueryValidatorException)3 ComponentNotFoundException (org.teiid.core.ComponentNotFoundException)3 Evaluator (org.teiid.query.eval.Evaluator)3 ExceptionExpression (org.teiid.query.sql.proc.ExceptionExpression)3 Expression (org.teiid.query.sql.symbol.Expression)3 ValueIterator (org.teiid.query.sql.util.ValueIterator)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2