Search in sources :

Example 16 with FunctionExecutionException

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

the class MongoDBSelectVisitor method convertGeometryToJson.

private void convertGeometryToJson(BasicDBObjectBuilder builder, GeometryType object) throws TranslatorException {
    try {
        ClobType clob = GeometryUtils.geometryToGeoJson(object);
        ClobToStringTransform clob2str = new ClobToStringTransform();
        String geometry = (String) clob2str.transform(clob, String.class);
        builder.add("$geometry", geometry);
    } catch (FunctionExecutionException | TransformationException e) {
        throw new TranslatorException(e);
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) TransformationException(org.teiid.core.types.TransformationException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) ClobToStringTransform(org.teiid.core.types.basic.ClobToStringTransform) TranslatorException(org.teiid.translator.TranslatorException)

Example 17 with FunctionExecutionException

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

the class QueryRewriter method simplifyMathematicalCriteria.

/**
 * @param criteria
 * @return CompareCriteria
 */
private CompareCriteria simplifyMathematicalCriteria(CompareCriteria criteria) throws TeiidProcessingException {
    Expression leftExpr = criteria.getLeftExpression();
    Expression rightExpr = criteria.getRightExpression();
    // Identify all the pieces of this criteria
    Function function = (Function) leftExpr;
    String funcName = function.getName();
    Expression[] args = function.getArgs();
    Constant const1 = null;
    Expression expr = null;
    if (args[1] instanceof Constant) {
        const1 = (Constant) args[1];
        expr = args[0];
    } else {
        if (funcName.equals("+") || funcName.equals("*")) {
            // $NON-NLS-1$ //$NON-NLS-2$
            const1 = (Constant) args[0];
            expr = args[1];
        } else {
            // If we have "5 - x = 10" or "5 / x = 10", abort!
            return criteria;
        }
    }
    int operator = criteria.getOperator();
    // Determine opposite function
    String oppFunc = null;
    switch(funcName.charAt(0)) {
        // $NON-NLS-1$
        case '+':
            oppFunc = "-";
            break;
        // $NON-NLS-1$
        case '-':
            oppFunc = "+";
            break;
        // $NON-NLS-1$
        case '*':
            oppFunc = "/";
            break;
        // $NON-NLS-1$
        case '/':
            oppFunc = "*";
            break;
    }
    // Create a function of the two constants and evaluate it
    Expression combinedConst = null;
    FunctionLibrary funcLib = this.metadata.getFunctionLibrary();
    FunctionDescriptor descriptor = funcLib.findFunction(oppFunc, new Class[] { rightExpr.getType(), const1.getType() });
    if (descriptor == null) {
        // See defect 9380 - this can be caused by const2 being a null Constant, for example (? + 1) < null
        return criteria;
    }
    if (rightExpr instanceof Constant) {
        Constant const2 = (Constant) rightExpr;
        try {
            Object result = descriptor.invokeFunction(new Object[] { const2.getValue(), const1.getValue() }, null, this.context);
            combinedConst = new Constant(result, descriptor.getReturnType());
        } catch (FunctionExecutionException e) {
            throw new QueryValidatorException(QueryPlugin.Event.TEIID30373, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30373, e.getMessage()));
        } catch (BlockedException e) {
            throw new QueryValidatorException(QueryPlugin.Event.TEIID30373, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30373, e.getMessage()));
        }
    } else {
        Function conversion = new Function(descriptor.getName(), new Expression[] { rightExpr, const1 });
        conversion.setType(leftExpr.getType());
        conversion.setFunctionDescriptor(descriptor);
        combinedConst = conversion;
    }
    // Flip operator if necessary
    if (!(operator == CompareCriteria.EQ || operator == CompareCriteria.NE) && (oppFunc.equals("*") || oppFunc.equals("/"))) {
        // $NON-NLS-1$ //$NON-NLS-2$
        Object value = const1.getValue();
        if (value != null) {
            Class type = const1.getType();
            Comparable comparisonObject = null;
            if (type.equals(DataTypeManager.DefaultDataClasses.INTEGER)) {
                comparisonObject = INTEGER_ZERO;
            } else if (type.equals(DataTypeManager.DefaultDataClasses.DOUBLE)) {
                comparisonObject = DOUBLE_ZERO;
            } else if (type.equals(DataTypeManager.DefaultDataClasses.FLOAT)) {
                comparisonObject = FLOAT_ZERO;
            } else if (type.equals(DataTypeManager.DefaultDataClasses.LONG)) {
                comparisonObject = LONG_ZERO;
            } else if (type.equals(DataTypeManager.DefaultDataClasses.BIG_INTEGER)) {
                comparisonObject = BIG_INTEGER_ZERO;
            } else if (type.equals(DataTypeManager.DefaultDataClasses.BIG_DECIMAL)) {
                comparisonObject = BIG_DECIMAL_ZERO;
            } else if (type.equals(DataTypeManager.DefaultDataClasses.SHORT)) {
                comparisonObject = SHORT_ZERO;
            } else if (type.equals(DataTypeManager.DefaultDataClasses.BYTE)) {
                comparisonObject = BYTE_ZERO;
            } else {
                // Unknown type
                return criteria;
            }
            // then need to switch operator.
            if (comparisonObject.compareTo(value) > 0) {
                switch(operator) {
                    case CompareCriteria.LE:
                        operator = CompareCriteria.GE;
                        break;
                    case CompareCriteria.LT:
                        operator = CompareCriteria.GT;
                        break;
                    case CompareCriteria.GE:
                        operator = CompareCriteria.LE;
                        break;
                    case CompareCriteria.GT:
                        operator = CompareCriteria.LT;
                        break;
                }
            }
        }
    }
    criteria.setLeftExpression(expr);
    criteria.setRightExpression(combinedConst);
    criteria.setOperator(operator);
    // Return new simplified criteria
    return criteria;
}
Also used : FunctionLibrary(org.teiid.query.function.FunctionLibrary) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) BlockedException(org.teiid.common.buffer.BlockedException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) LanguageObject(org.teiid.query.sql.LanguageObject)

Example 18 with FunctionExecutionException

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

the class XQueryEvaluator method evaluate.

public static Object evaluate(XMLType value, XMLCast expression, CommandContext context) throws ExpressionEvaluationException {
    Configuration config = new Configuration();
    Type t = value.getType();
    try {
        Item i = null;
        switch(t) {
            case CONTENT:
            // content could map to an array value, but we aren't handling that case here yet - only in xmltable
            case COMMENT:
            case PI:
                throw new FunctionExecutionException();
            case TEXT:
                i = new StringValue(value.getString());
                break;
            case UNKNOWN:
            case DOCUMENT:
            case ELEMENT:
                StreamSource ss = value.getSource(StreamSource.class);
                try {
                    i = config.buildDocument(ss);
                } finally {
                    if (ss.getInputStream() != null) {
                        ss.getInputStream().close();
                    }
                    if (ss.getReader() != null) {
                        ss.getReader().close();
                    }
                }
                break;
            default:
                // $NON-NLS-1$
                throw new AssertionError("Unknown xml value type " + t);
        }
        return XMLTableNode.getValue(expression.getType(), i, config, context);
    } catch (IOException e) {
        throw new FunctionExecutionException(e);
    } catch (ValidationException e) {
        throw new FunctionExecutionException(e);
    } catch (TransformationException e) {
        throw new FunctionExecutionException(e);
    } catch (XPathException e) {
        throw new FunctionExecutionException(e);
    } catch (SQLException e) {
        throw new FunctionExecutionException(e);
    }
}
Also used : Item(net.sf.saxon.om.Item) BinaryType(org.teiid.core.types.BinaryType) XMLType(org.teiid.core.types.XMLType) DocType(nu.xom.DocType) Type(org.teiid.core.types.XMLType.Type) TransformationException(org.teiid.core.types.TransformationException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) ValidationException(net.sf.saxon.type.ValidationException) Configuration(net.sf.saxon.Configuration) XPathException(net.sf.saxon.trans.XPathException) SQLException(java.sql.SQLException) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) StringValue(net.sf.saxon.value.StringValue)

Example 19 with FunctionExecutionException

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

the class Evaluator method evaluateXMLForest.

private Object evaluateXMLForest(List<?> tuple, XMLForest function) throws ExpressionEvaluationException, BlockedException, TeiidComponentException, FunctionExecutionException {
    List<DerivedColumn> args = function.getArgs();
    Evaluator.NameValuePair<Object>[] nameValuePairs = getNameValuePairs(tuple, args, true, true);
    try {
        return XMLSystemFunctions.xmlForest(context, namespaces(function.getNamespaces()), nameValuePairs);
    } catch (TeiidProcessingException e) {
        throw new FunctionExecutionException(e);
    }
}
Also used : FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 20 with FunctionExecutionException

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

the class FunctionDescriptor method invokeFunction.

/**
 * Invoke the function described in the function descriptor, using the
 * values provided.  Return the result of the function.
 * @param values Values that should match 1-to-1 with the types described in the
 * function descriptor
 * @param context
 * @param functionTarget TODO
 * @param fd Function descriptor describing the name and types of the arguments
 * @return Result of invoking the function
 */
public Object invokeFunction(Object[] values, CommandContext context, Object functionTarget) throws FunctionExecutionException, BlockedException {
    if (!isNullDependent()) {
        for (int i = requiresContext ? 1 : 0; i < values.length; i++) {
            if (values[i] == null) {
                return null;
            }
        }
    }
    checkMethod();
    // Invoke the method and return the result
    try {
        if (hasWrappedArgs) {
            for (int i = 0; i < values.length; i++) {
                Object val = values[i];
                if (val != null && types[i] == DataTypeManager.DefaultDataClasses.VARBINARY) {
                    values[i] = ((BinaryType) val).getBytesDirect();
                }
            }
        }
        if (method.isVarArgs()) {
            if (calledWithVarArgArrayParam) {
                ArrayImpl av = (ArrayImpl) values[values.length - 1];
                if (av != null) {
                    Object[] vals = av.getValues();
                    values[values.length - 1] = vals;
                    if (hasWrappedArgs && types[types.length - 1] == DataTypeManager.DefaultDataClasses.VARBINARY) {
                        vals = Arrays.copyOf(vals, vals.length);
                        for (int i = 0; i < vals.length; i++) {
                            if (vals[i] != null) {
                                vals[i] = ((BinaryType) vals[i]).getBytesDirect();
                            }
                        }
                        values[values.length - 1] = vals;
                    }
                    Class<?> arrayType = invocationMethod.getParameterTypes()[types.length - 1];
                    if (arrayType.getComponentType() != Object.class && vals.getClass() != arrayType) {
                        Object varArgs = Array.newInstance(arrayType.getComponentType(), vals.length);
                        for (int i = 0; i < vals.length; i++) {
                            Array.set(varArgs, i, vals[i]);
                        }
                        values[values.length - 1] = varArgs;
                    }
                }
            } else {
                int i = invocationMethod.getParameterTypes().length;
                Object[] newValues = Arrays.copyOf(values, i);
                Object varArgs = null;
                if (invocationMethod.getParameterTypes()[i - 1].getComponentType() != Object.class) {
                    int varArgCount = values.length - i + 1;
                    varArgs = Array.newInstance(invocationMethod.getParameterTypes()[i - 1].getComponentType(), varArgCount);
                    for (int j = 0; j < varArgCount; j++) {
                        Array.set(varArgs, j, values[i - 1 + j]);
                    }
                } else {
                    varArgs = Arrays.copyOfRange(values, i - 1, values.length);
                }
                newValues[i - 1] = varArgs;
                values = newValues;
            }
        }
        Object result = null;
        ClassLoader originalCL = Thread.currentThread().getContextClassLoader();
        try {
            if (this.classLoader != null) {
                Thread.currentThread().setContextClassLoader(this.classLoader);
            }
            result = invocationMethod.invoke(functionTarget, values);
        } finally {
            Thread.currentThread().setContextClassLoader(originalCL);
        }
        if (context != null && getDeterministic().ordinal() <= Determinism.USER_DETERMINISTIC.ordinal()) {
            context.setDeterminismLevel(getDeterministic());
        }
        return importValue(result, getReturnType());
    } catch (ArithmeticException e) {
        throw new FunctionExecutionException(QueryPlugin.Event.TEIID30384, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30384, getFullName()));
    } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof BlockedException) {
            throw (BlockedException) e.getTargetException();
        }
        throw new FunctionExecutionException(QueryPlugin.Event.TEIID30384, e.getTargetException(), QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30384, getFullName()));
    } catch (IllegalAccessException e) {
        throw new FunctionExecutionException(QueryPlugin.Event.TEIID30385, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30385, method.toString()));
    } catch (TransformationException e) {
        throw new FunctionExecutionException(e);
    }
}
Also used : TransformationException(org.teiid.core.types.TransformationException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) ArrayImpl(org.teiid.core.types.ArrayImpl) BlockedException(org.teiid.common.buffer.BlockedException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

FunctionExecutionException (org.teiid.api.exception.query.FunctionExecutionException)31 IOException (java.io.IOException)9 SQLException (java.sql.SQLException)9 ParseException (com.vividsolutions.jts.io.ParseException)5 TeiidProcessingException (org.teiid.core.TeiidProcessingException)5 LanguageObject (org.teiid.query.sql.LanguageObject)4 ClobType (org.teiid.core.types.ClobType)3 TransformationException (org.teiid.core.types.TransformationException)3 WKBReader (com.vividsolutions.jts.io.WKBReader)2 WKTReader (com.vividsolutions.jts.io.WKTReader)2 StringReader (java.io.StringReader)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 XPathException (net.sf.saxon.trans.XPathException)2 BlockedException (org.teiid.common.buffer.BlockedException)2