Search in sources :

Example 36 with QueryMetadataException

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

the class ResolverVisitor method internalResolveElementSymbol.

private boolean internalResolveElementSymbol(ElementSymbol elementSymbol, String groupContext, String shortCanonicalName, String expectedGroupContext) throws TeiidComponentException, QueryResolverException {
    boolean isExternal = false;
    boolean groupMatched = false;
    GroupContext root = null;
    if (groups != null || externalContext != null) {
        if (groups != null) {
            root = new GroupContext(externalContext, groups);
        }
        if (root == null) {
            isExternal = true;
            root = externalContext;
        }
    } else {
        try {
            LinkedList<GroupSymbol> matchedGroups = new LinkedList<GroupSymbol>();
            if (groupContext != null) {
                // assume that this is fully qualified
                Object groupID = this.metadata.getGroupID(groupContext);
                // No groups specified, so any group is valid
                GroupSymbol groupSymbol = new GroupSymbol(groupContext);
                groupSymbol.setMetadataID(groupID);
                matchedGroups.add(groupSymbol);
            }
            root = new GroupContext(null, matchedGroups);
        } catch (QueryMetadataException e) {
        // ignore
        }
    }
    matches.clear();
    groupMatches.clear();
    while (root != null) {
        Collection<GroupSymbol> matchedGroups = ResolverUtil.findMatchingGroups(groupContext, root.getGroups(), metadata);
        if (matchedGroups != null && !matchedGroups.isEmpty()) {
            groupMatched = true;
            resolveAgainstGroups(shortCanonicalName, matchedGroups);
            if (matches.size() > 1) {
                throw handleUnresolvedElement(elementSymbol, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31117, elementSymbol, groupMatches));
            }
            if (matches.size() == 1) {
                break;
            }
        }
        root = root.getParent();
        isExternal = true;
    }
    if (matches.isEmpty()) {
        if (groupMatched) {
            throw handleUnresolvedElement(elementSymbol, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31118, elementSymbol));
        }
        throw handleUnresolvedElement(elementSymbol, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31119, elementSymbol));
    }
    // copy the match information
    ElementSymbol resolvedSymbol = matches.get(0);
    GroupSymbol resolvedGroup = groupMatches.get(0);
    String oldName = elementSymbol.getOutputName();
    if (expectedGroupContext != null && !ResolverUtil.nameMatchesGroup(expectedGroupContext, resolvedGroup.getName())) {
        return false;
    }
    elementSymbol.setIsExternalReference(isExternal);
    elementSymbol.setType(resolvedSymbol.getType());
    elementSymbol.setMetadataID(resolvedSymbol.getMetadataID());
    elementSymbol.setGroupSymbol(resolvedGroup);
    elementSymbol.setShortName(resolvedSymbol.getShortName());
    if (metadata.useOutputName()) {
        elementSymbol.setOutputName(oldName);
    }
    return true;
}
Also used : LanguageObject(org.teiid.query.sql.LanguageObject) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) LinkedList(java.util.LinkedList)

Example 37 with QueryMetadataException

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

the class QueryResolver method resolveCommand.

public static TempMetadataStore resolveCommand(Command currentCommand, QueryMetadataInterface metadata, boolean resolveNullLiterals) throws QueryResolverException, TeiidComponentException {
    // $NON-NLS-1$
    LogManager.logTrace(org.teiid.logging.LogConstants.CTX_QUERY_RESOLVER, new Object[] { "Resolving command", currentCommand });
    TempMetadataAdapter resolverMetadata = null;
    try {
        TempMetadataStore discoveredMetadata = currentCommand.getTemporaryMetadata();
        if (discoveredMetadata == null) {
            discoveredMetadata = new TempMetadataStore();
            currentCommand.setTemporaryMetadata(discoveredMetadata);
        }
        resolverMetadata = new TempMetadataAdapter(metadata, discoveredMetadata);
        // Resolve external groups for command
        Collection<GroupSymbol> externalGroups = currentCommand.getAllExternalGroups();
        for (GroupSymbol extGroup : externalGroups) {
            Object metadataID = extGroup.getMetadataID();
            // TODO: this is mainly for XML resolving since it sends external groups in unresolved
            if (metadataID == null || (!(extGroup.getMetadataID() instanceof TempMetadataID) && discoveredMetadata.getTempGroupID(extGroup.getName()) != null)) {
                boolean missing = metadataID == null;
                metadataID = resolverMetadata.getGroupID(extGroup.getName());
                if (missing) {
                    extGroup.setMetadataID(metadataID);
                } else {
                    // we shouldn't modify the existing, just add a shadow group
                    GroupSymbol gs = extGroup.clone();
                    gs.setMetadataID(metadataID);
                    currentCommand.getExternalGroupContexts().addGroup(gs);
                }
            }
        }
        CommandResolver resolver = chooseResolver(currentCommand, resolverMetadata);
        // Resolve this command
        resolver.resolveCommand(currentCommand, resolverMetadata, resolveNullLiterals);
    } catch (QueryMetadataException e) {
        throw new QueryResolverException(e);
    }
    // Flag that this command has been resolved.
    currentCommand.setIsResolved(true);
    return resolverMetadata.getMetadataStore();
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) TempMetadataID(org.teiid.query.metadata.TempMetadataID) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 38 with QueryMetadataException

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

the class SimpleQueryResolver method resolveCommand.

/**
 * @see org.teiid.query.resolver.CommandResolver#resolveCommand(org.teiid.query.sql.lang.Command, org.teiid.query.metadata.TempMetadataAdapter, boolean)
 */
public void resolveCommand(Command command, TempMetadataAdapter metadata, boolean resolveNullLiterals) throws QueryMetadataException, QueryResolverException, TeiidComponentException {
    Query query = (Query) command;
    resolveWith(metadata, query);
    try {
        QueryResolverVisitor qrv = new QueryResolverVisitor(query, metadata);
        qrv.visit(query);
        ResolverVisitor visitor = (ResolverVisitor) qrv.getVisitor();
        visitor.throwException(true);
        if (visitor.hasUserDefinedAggregate()) {
            ExpressionMappingVisitor emv = new ExpressionMappingVisitor(null) {

                public Expression replaceExpression(Expression element) {
                    if (element instanceof Function && !(element instanceof AggregateSymbol) && ((Function) element).isAggregate()) {
                        Function f = (Function) element;
                        AggregateSymbol as = new AggregateSymbol(f.getName(), false, f.getArgs(), null);
                        as.setType(f.getType());
                        as.setFunctionDescriptor(f.getFunctionDescriptor());
                        return as;
                    }
                    return element;
                }
            };
            PreOrPostOrderNavigator.doVisit(query, emv, PreOrPostOrderNavigator.POST_ORDER);
        }
    } catch (TeiidRuntimeException e) {
        if (e.getCause() instanceof QueryMetadataException) {
            throw (QueryMetadataException) e.getCause();
        }
        if (e.getCause() instanceof QueryResolverException) {
            throw (QueryResolverException) e.getCause();
        }
        if (e.getCause() instanceof TeiidComponentException) {
            throw (TeiidComponentException) e.getCause();
        }
        throw e;
    }
    if (query.getLimit() != null) {
        ResolverUtil.resolveLimit(query.getLimit());
    }
    if (query.getOrderBy() != null) {
        ResolverUtil.resolveOrderBy(query.getOrderBy(), query, metadata);
    }
    List<Expression> symbols = query.getSelect().getProjectedSymbols();
    if (query.getInto() != null) {
        GroupSymbol symbol = query.getInto().getGroup();
        ResolverUtil.resolveImplicitTempGroup(metadata, symbol, symbols);
    } else if (resolveNullLiterals) {
        ResolverUtil.resolveNullLiterals(symbols);
    }
}
Also used : TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) ExpressionMappingVisitor(org.teiid.query.sql.visitor.ExpressionMappingVisitor) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) ResolverVisitor(org.teiid.query.resolver.util.ResolverVisitor) TeiidComponentException(org.teiid.core.TeiidComponentException)

Example 39 with QueryMetadataException

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

the class TestCriteriaCapabilityValidatorVisitor method helpTestVisitor.

public void helpTestVisitor(String sql, Object modelID, TransformationMetadata metadata, CapabilitiesFinder capFinder, boolean isValid, boolean expectException) throws Exception {
    try {
        Criteria criteria = QueryParser.getQueryParser().parseCriteria(sql);
        QueryResolver.resolveCriteria(criteria, metadata);
        // $NON-NLS-1$
        assertEquals("Got incorrect isValid flag", isValid, CriteriaCapabilityValidatorVisitor.canPushLanguageObject(criteria, modelID, metadata, capFinder, null));
    } catch (QueryMetadataException e) {
        if (!expectException) {
            throw new RuntimeException(e);
        }
    }
}
Also used : Criteria(org.teiid.query.sql.lang.Criteria) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException)

Aggregations

QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)39 TeiidComponentException (org.teiid.core.TeiidComponentException)18 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)12 LanguageObject (org.teiid.query.sql.LanguageObject)7 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)6 TempMetadataID (org.teiid.query.metadata.TempMetadataID)6 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)5 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)5 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 VDBMetaData (org.teiid.adminapi.impl.VDBMetaData)3 TeiidException (org.teiid.core.TeiidException)3 TeiidProcessingException (org.teiid.core.TeiidProcessingException)3 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)3 ProcessorPlan (org.teiid.query.processor.ProcessorPlan)3 ObjectTable (org.teiid.query.sql.lang.ObjectTable)3 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)3 Expression (org.teiid.query.sql.symbol.Expression)3 Collection (java.util.Collection)2