Search in sources :

Example 41 with QueryResolverException

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

the class ResolverVisitor method visit.

@Override
public void visit(Array array) {
    try {
        if (array.getComponentType() != null) {
            String type = DataTypeManager.getDataTypeName(array.getComponentType());
            for (int i = 0; i < array.getExpressions().size(); i++) {
                Expression expr = array.getExpressions().get(i);
                setDesiredType(expr, array.getComponentType(), array);
                if (array.getComponentType() != DefaultDataClasses.OBJECT) {
                    array.getExpressions().set(i, ResolverUtil.convertExpression(expr, type, metadata));
                }
            }
        } else {
            resolveComponentType(array);
        }
    } catch (QueryResolverException e) {
        handleException(e);
    }
}
Also used : ExceptionExpression(org.teiid.query.sql.proc.ExceptionExpression) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 42 with QueryResolverException

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

the class ResolverVisitor method checkException.

public static void checkException(Expression obj) throws QueryResolverException {
    if (obj == null || obj instanceof ExceptionExpression) {
        return;
    }
    if (obj instanceof ElementSymbol) {
        ElementSymbol es = (ElementSymbol) obj;
        if (!(es.getMetadataID() instanceof TempMetadataID)) {
            throw new QueryResolverException(QueryPlugin.Event.TEIID31120, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31120, obj));
        }
        TempMetadataID tid = (TempMetadataID) es.getMetadataID();
        if (tid.getType() != Exception.class) {
            throw new QueryResolverException(QueryPlugin.Event.TEIID31120, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31120, obj));
        }
    } else if (obj instanceof Constant) {
        Constant c = (Constant) obj;
        if (!(c.getValue() instanceof Exception)) {
            throw new QueryResolverException(QueryPlugin.Event.TEIID31120, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31120, obj));
        }
    } else {
        throw new QueryResolverException(QueryPlugin.Event.TEIID31120, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31120, obj));
    }
}
Also used : ExceptionExpression(org.teiid.query.sql.proc.ExceptionExpression) TempMetadataID(org.teiid.query.metadata.TempMetadataID) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) TeiidComponentException(org.teiid.core.TeiidComponentException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) InvalidFunctionException(org.teiid.api.exception.query.InvalidFunctionException)

Example 43 with QueryResolverException

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

the class ResolverVisitor method resolveMatchCriteriaExpression.

/**
 * Checks one side of a LIKE Criteria; implicitly converts to a String or CLOB if necessary.
 * @param mcrit the Match Criteria
 * @param expr either left or right expression
 * @return either 'expr' itself, or a new implicit type conversion wrapping expr
 * @throws QueryResolverException if no implicit type conversion is available
 */
Expression resolveMatchCriteriaExpression(MatchCriteria mcrit, Expression expr) throws QueryResolverException {
    // Check left expression == string or CLOB
    String type = DataTypeManager.getDataTypeName(expr.getType());
    Expression result = expr;
    if (type != null) {
        if (!isCharacter(expr, false)) {
            if (ResolverUtil.canImplicitlyConvert(type, DataTypeManager.DefaultDataTypes.STRING)) {
                result = ResolverUtil.convertExpression(expr, type, DataTypeManager.DefaultDataTypes.STRING, metadata);
            } else if (ResolverUtil.canImplicitlyConvert(type, DataTypeManager.DefaultDataTypes.CLOB)) {
                result = ResolverUtil.convertExpression(expr, type, DataTypeManager.DefaultDataTypes.CLOB, metadata);
            } else {
                throw new QueryResolverException(QueryPlugin.Event.TEIID30074, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30074, mcrit));
            }
        }
    }
    return result;
}
Also used : ExceptionExpression(org.teiid.query.sql.proc.ExceptionExpression) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 44 with QueryResolverException

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

the class ResolverVisitor method resolveSetCriteria.

void resolveSetCriteria(SetCriteria scrit) throws QueryResolverException {
    // Check that each of the values are the same type as expression
    Class<?> exprType = scrit.getExpression().getType();
    if (exprType == null) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID30075, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30075, scrit.getExpression()));
    }
    // check if all types are the same, or if there is a common type
    boolean same = true;
    Iterator valIter = scrit.getValues().iterator();
    String[] types = new String[scrit.getValues().size()];
    int i = 0;
    Class<?> type = null;
    while (valIter.hasNext()) {
        Expression value = (Expression) valIter.next();
        if (value.getType() != exprType) {
            same = false;
        }
        types[i++] = DataTypeManager.getDataTypeName(value.getType());
        type = value.getType();
    }
    if (same && type == exprType) {
        return;
    }
    if (!same) {
        String commonType = ResolverUtil.getCommonRuntimeType(types);
        if (commonType != null) {
            type = DataTypeManager.getDataTypeClass(commonType);
        } else {
            type = null;
        }
    }
    String exprTypeName = DataTypeManager.getDataTypeName(exprType);
    boolean attemptConvert = !isCharacter(exprType, true) || metadata.widenComparisonToString();
    List<Expression> newVals = new ArrayList<Expression>(scrit.getValues().size());
    if (scrit.getExpression().getType() != DataTypeManager.DefaultDataClasses.NULL) {
        valIter = scrit.getValues().iterator();
        while (valIter.hasNext()) {
            Expression value = (Expression) valIter.next();
            setDesiredType(value, exprType, scrit);
            if (value.getType() != exprType) {
                String valTypeName = DataTypeManager.getDataTypeName(value.getType());
                // Apply cast and replace current value
                if (attemptConvert || isCharacter(value.getType(), true)) {
                    try {
                        newVals.add(ResolverUtil.convertExpression(value, valTypeName, exprTypeName, metadata));
                    } catch (QueryResolverException e) {
                        if (value instanceof Constant && isCharacter(value, true) && !metadata.widenComparisonToString()) {
                            throw e;
                        }
                        if (type == null) {
                            type = value.getType();
                        }
                        break;
                    }
                }
            } else {
                newVals.add(value);
            }
        }
        if (newVals.size() == scrit.getValues().size()) {
            scrit.setValues(newVals);
            return;
        }
    }
    // set is the same and the convert can be placed on the left side
    if (type == null) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID30077, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30077, scrit));
    }
    // Is there a possible conversion from left to right?
    String setTypeName = DataTypeManager.getDataTypeName(type);
    if (!isCharacter(type, true) || metadata.widenComparisonToString() || scrit.getExpression().getType() == DataTypeManager.DefaultDataClasses.NULL) {
        scrit.setExpression(ResolverUtil.convertExpression(scrit.getExpression(), exprTypeName, setTypeName, metadata));
    } else if (type != scrit.getExpression().getType()) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID31172, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31172, scrit));
    }
    boolean exprChar = isCharacter(scrit.getExpression(), true);
    newVals.clear();
    valIter = scrit.getValues().iterator();
    while (valIter.hasNext()) {
        Expression value = (Expression) valIter.next();
        if (value.getType() == null) {
            throw new QueryResolverException(QueryPlugin.Event.TEIID30075, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30075, value));
        } else if (value.getType() != type) {
            if (!metadata.widenComparisonToString() && exprChar ^ isCharacter(value, true)) {
                throw new QueryResolverException(QueryPlugin.Event.TEIID31172, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31172, scrit));
            }
            value = ResolverUtil.convertExpression(value, setTypeName, metadata);
        }
        newVals.add(value);
    }
    scrit.setValues(newVals);
}
Also used : ExceptionExpression(org.teiid.query.sql.proc.ExceptionExpression) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 45 with QueryResolverException

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

the class ResolverVisitor method visit.

@Override
public void visit(AggregateSymbol obj) {
    if (obj.getCondition() != null) {
        try {
            obj.setCondition(ResolverUtil.convertExpression(obj.getCondition(), DataTypeManager.DefaultDataTypes.BOOLEAN, metadata));
        } catch (QueryResolverException e) {
            handleException(e);
        }
    }
    switch(obj.getAggregateFunction()) {
        case USER_DEFINED:
            visit((Function) obj);
            break;
        case LEAD:
        case LAG:
            if (obj.getArgs().length > 1) {
                // second arg must be an integer
                try {
                    obj.getArgs()[1] = ResolverUtil.convertExpression(obj.getArg(1), DataTypeManager.DefaultDataTypes.INTEGER, metadata);
                } catch (QueryResolverException e) {
                    handleException(e);
                }
                // first and third args must match type
                if (obj.getArgs().length > 2) {
                    try {
                        if (obj.getArgs()[0].getType() == DataTypeManager.DefaultDataClasses.NULL) {
                            obj.getArgs()[0] = ResolverUtil.convertExpression(obj.getArg(0), DataTypeManager.getDataTypeName(obj.getArg(2).getType()), metadata);
                        } else {
                            obj.getArgs()[2] = ResolverUtil.convertExpression(obj.getArg(2), DataTypeManager.getDataTypeName(obj.getArg(0).getType()), metadata);
                        }
                    } catch (QueryResolverException e) {
                        handleException(e);
                    }
                }
            }
            break;
        case STRING_AGG:
            try {
                if (obj.getArgs().length != 2) {
                    throw new QueryResolverException(QueryPlugin.Event.TEIID31140, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31140, obj));
                }
                if (obj.getType() == null) {
                    Expression arg = obj.getArg(0);
                    Expression arg1 = obj.getArg(1);
                    Class<?> type = null;
                    if (isBinary(arg)) {
                        setDesiredType(arg1, DataTypeManager.DefaultDataClasses.BLOB, obj);
                        if (isBinary(arg1)) {
                            type = DataTypeManager.DefaultDataClasses.BLOB;
                        }
                    } else if (isCharacter(arg, false)) {
                        setDesiredType(arg1, DataTypeManager.DefaultDataClasses.CLOB, obj);
                        if (isCharacter(arg1, false)) {
                            type = DataTypeManager.DefaultDataClasses.CLOB;
                        }
                    } else if (arg.getType() == null) {
                        if (isBinary(arg1)) {
                            setDesiredType(arg, DataTypeManager.DefaultDataClasses.BLOB, obj);
                            if (isBinary(arg)) {
                                type = DataTypeManager.DefaultDataClasses.BLOB;
                            }
                        } else if (isCharacter(arg1, false)) {
                            setDesiredType(arg, DataTypeManager.DefaultDataClasses.CLOB, obj);
                            if (isCharacter(arg, false)) {
                                type = DataTypeManager.DefaultDataClasses.CLOB;
                            }
                        }
                    }
                    if (type == null) {
                        throw new QueryResolverException(QueryPlugin.Event.TEIID31141, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31141, obj));
                    }
                    obj.setType(type);
                }
            } catch (QueryResolverException e) {
                handleException(e);
            }
            break;
    }
}
Also used : ExceptionExpression(org.teiid.query.sql.proc.ExceptionExpression) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Aggregations

QueryResolverException (org.teiid.api.exception.query.QueryResolverException)62 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)13 TempMetadataID (org.teiid.query.metadata.TempMetadataID)11 ExceptionExpression (org.teiid.query.sql.proc.ExceptionExpression)11 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)10 TeiidComponentException (org.teiid.core.TeiidComponentException)10 Expression (org.teiid.query.sql.symbol.Expression)10 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)8 CreateProcedureCommand (org.teiid.query.sql.proc.CreateProcedureCommand)7 Command (org.teiid.query.sql.lang.Command)6 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)6 QueryParserException (org.teiid.api.exception.query.QueryParserException)5 TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)5 LanguageObject (org.teiid.query.sql.LanguageObject)5 List (java.util.List)4 UnresolvedSymbolDescription (org.teiid.api.exception.query.UnresolvedSymbolDescription)4 TempMetadataAdapter (org.teiid.query.metadata.TempMetadataAdapter)4 StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)4 HashSet (java.util.HashSet)3