Search in sources :

Example 21 with QueryResolverException

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

the class ResolverVisitor method setDesiredType.

private void setDesiredType(Expression obj, Class<?> type, LanguageObject surrounding) throws QueryResolverException {
    ResolverUtil.setDesiredType(obj, type, surrounding);
    // second pass resolving for functions
    if (!(obj instanceof Function)) {
        return;
    }
    if (unresolvedFunctions != null) {
        Function f = (Function) obj;
        if (f.getFunctionDescriptor() != null) {
            return;
        }
        unresolvedFunctions.remove(obj);
        obj.acceptVisitor(this);
        QueryResolverException e = unresolvedFunctions.get(obj);
        if (e != null) {
            throw e;
        }
    }
}
Also used : QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 22 with QueryResolverException

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

the class ResolverVisitor method resolveSearchedCaseExpression.

void resolveSearchedCaseExpression(SearchedCaseExpression obj) throws QueryResolverException {
    // If already resolved, do nothing
    if (obj.getType() != null) {
        return;
    }
    final int whenCount = obj.getWhenCount();
    // 1. Call recursively to resolve any contained CASE expressions
    Class<?> thenType = null;
    // Get the WHEN and THEN types, and get a candidate type for each (for the next step)
    for (int i = 0; i < whenCount; i++) {
        if (thenType == null) {
            thenType = obj.getThenExpression(i).getType();
        }
    }
    Expression elseExpr = obj.getElseExpression();
    if (elseExpr != null) {
        if (thenType == null) {
            thenType = elseExpr.getType();
        }
    }
    // Invariant: All the expressions contained in the obj are resolved (except References)
    // 2. Attempt to set the target types of all contained expressions,
    // and collect their type names for the next step
    ArrayList<String> thenTypeNames = new ArrayList<String>(whenCount + 1);
    Expression then = null;
    // Set the types of the WHEN and THEN parts
    for (int i = 0; i < whenCount; i++) {
        then = obj.getThenExpression(i);
        setDesiredType(then, thenType, obj);
        thenTypeNames.add(DataTypeManager.getDataTypeName(then.getType()));
    }
    // Set the type of the else expression
    if (elseExpr != null) {
        setDesiredType(elseExpr, thenType, obj);
        thenTypeNames.add(DataTypeManager.getDataTypeName(elseExpr.getType()));
    }
    // Invariants: all the expressions' types are non-null
    // 3. Perform implicit type conversions
    String thenTypeName = ResolverUtil.getCommonRuntimeType(thenTypeNames.toArray(new String[thenTypeNames.size()]));
    if (thenTypeName == null) {
        // $NON-NLS-1$
        throw new QueryResolverException(QueryPlugin.Event.TEIID30079, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30079, "THEN/ELSE", obj));
    }
    ArrayList<Expression> thens = new ArrayList<Expression>(whenCount);
    for (int i = 0; i < whenCount; i++) {
        thens.add(ResolverUtil.convertExpression(obj.getThenExpression(i), thenTypeName, metadata));
    }
    obj.setWhen(obj.getWhen(), thens);
    if (elseExpr != null) {
        obj.setElseExpression(ResolverUtil.convertExpression(elseExpr, thenTypeName, metadata));
    }
    // Set this CASE expression's type to the common THEN type, and we're done.
    obj.setType(DataTypeManager.getDataTypeClass(thenTypeName));
}
Also used : ExceptionExpression(org.teiid.query.sql.proc.ExceptionExpression) ArrayList(java.util.ArrayList) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 23 with QueryResolverException

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

the class ResolverVisitor method resolveQuantifiedCompareArray.

private void resolveQuantifiedCompareArray(SubqueryCompareCriteria obj) throws QueryResolverException, AssertionError {
    Class<?> expressionType = obj.getArrayExpression().getType();
    if (expressionType == null || !expressionType.isArray()) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID31175, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31175, new Object[] { obj.getArrayExpression(), expressionType }));
    }
    Class<?> rightType = expressionType.getComponentType();
    Expression leftExpression = obj.getLeftExpression();
    setDesiredType(leftExpression, rightType, obj);
    if (leftExpression.getType().equals(rightType)) {
        return;
    }
    // Try to apply an implicit conversion from one side to the other
    String leftTypeName = DataTypeManager.getDataTypeName(leftExpression.getType());
    String rightTypeName = DataTypeManager.getDataTypeName(rightType);
    if (leftExpression.getType() == DataTypeManager.DefaultDataClasses.NULL) {
        obj.setLeftExpression(ResolverUtil.convertExpression(leftExpression, rightTypeName, metadata));
        return;
    }
    boolean leftChar = isCharacter(leftExpression, true);
    boolean rightChar = isCharacter(rightType, true);
    // Special cases when left expression is a constant
    if (leftExpression instanceof Constant && (!rightChar || leftChar)) {
        // Auto-convert constant string on left to expected type on right
        try {
            obj.setLeftExpression(ResolverUtil.convertExpression(leftExpression, leftTypeName, rightTypeName, metadata));
            return;
        } catch (QueryResolverException qre) {
            if (leftChar && !metadata.widenComparisonToString()) {
                throw qre;
            }
        }
    }
    // Try to apply a conversion generically
    if ((rightChar ^ leftChar) && !metadata.widenComparisonToString()) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID31172, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31172, obj));
    }
    if (ResolverUtil.canImplicitlyConvert(leftTypeName, rightTypeName)) {
        obj.setLeftExpression(ResolverUtil.convertExpression(leftExpression, leftTypeName, rightTypeName, metadata));
        return;
    }
    throw new QueryResolverException(QueryPlugin.Event.TEIID30072, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30072, new Object[] { leftTypeName, rightTypeName, obj }));
}
Also used : ExceptionExpression(org.teiid.query.sql.proc.ExceptionExpression) LanguageObject(org.teiid.query.sql.LanguageObject) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 24 with QueryResolverException

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

the class ResolverVisitor method resolveLanguageObject.

public static void resolveLanguageObject(LanguageObject obj, Collection<GroupSymbol> groups, GroupContext externalContext, QueryMetadataInterface metadata) throws TeiidComponentException, QueryResolverException {
    if (obj == null) {
        return;
    }
    // Resolve elements, deal with errors
    final ResolverVisitor elementsVisitor = new ResolverVisitor(metadata, groups, externalContext);
    // special handling for is distinct - we must resolve as both element and group
    // until we generalize the notion of a scalar group reference as an "elementsymbol"
    PostOrderNavigator nav = new PostOrderNavigator(elementsVisitor) {

        @Override
        public void visit(IsDistinctCriteria obj) {
            obj.setLeftRowValue(resolveAsGroup(obj.getLeftRowValue()));
            obj.setRightRowValue(resolveAsGroup(obj.getRightRowValue()));
            super.visit(obj);
        }

        private LanguageObject resolveAsGroup(LanguageObject rowValue) {
            if (rowValue instanceof ElementSymbol) {
                ElementSymbol es = (ElementSymbol) rowValue;
                if (es.getMetadataID() == null) {
                    try {
                        elementsVisitor.resolveElementSymbol(es);
                    } catch (QueryResolverException | TeiidComponentException e) {
                        GroupSymbol gs = new GroupSymbol(es.getName());
                        try {
                            ResolverUtil.resolveGroup(gs, metadata);
                            rowValue = gs;
                        } catch (QueryResolverException | TeiidComponentException e1) {
                        }
                    }
                }
            }
            return rowValue;
        }
    };
    obj.acceptVisitor(nav);
    elementsVisitor.throwException(true);
}
Also used : PostOrderNavigator(org.teiid.query.sql.navigator.PostOrderNavigator) TeiidComponentException(org.teiid.core.TeiidComponentException) LanguageObject(org.teiid.query.sql.LanguageObject) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 25 with QueryResolverException

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

the class SetQuery method getTypedProjectedSymbols.

public static List<Expression> getTypedProjectedSymbols(List<? extends Expression> acutal, List<Class<?>> projectedTypes, QueryMetadataInterface metadata) {
    List<Expression> newProject = new ArrayList<Expression>();
    for (int i = 0; i < acutal.size(); i++) {
        Expression originalSymbol = acutal.get(i);
        Expression symbol = originalSymbol;
        Class<?> type = projectedTypes.get(i);
        if (symbol.getType() != type) {
            symbol = SymbolMap.getExpression(originalSymbol);
            try {
                symbol = ResolverUtil.convertExpression(symbol, DataTypeManager.getDataTypeName(type), metadata);
            } catch (QueryResolverException err) {
                throw new TeiidRuntimeException(QueryPlugin.Event.TEIID30447, err);
            }
            if (originalSymbol instanceof Symbol) {
                symbol = new AliasSymbol(Symbol.getShortName(originalSymbol), symbol);
            }
        }
        newProject.add(symbol);
    }
    return newProject;
}
Also used : AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Expression(org.teiid.query.sql.symbol.Expression) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Symbol(org.teiid.query.sql.symbol.Symbol) ArrayList(java.util.ArrayList) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) 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