Search in sources :

Example 11 with ExpressionMappingVisitor

use of org.teiid.query.sql.visitor.ExpressionMappingVisitor in project teiid by teiid.

the class QueryResolver method resolveWithBindingMetadata.

/**
 * Bindings are a poor mans input parameters.  They are represented in legacy metadata
 * by ElementSymbols and placed positionally into the command or by alias symbols
 * and matched by names.  After resolving bindings will be replaced with their
 * referenced symbols (input names will not be used) and those symbols will
 * be marked as external references.
 */
public static TempMetadataStore resolveWithBindingMetadata(Command currentCommand, QueryMetadataInterface metadata, QueryNode queryNode, boolean replaceBindings) throws TeiidComponentException, QueryResolverException {
    Map<ElementSymbol, ElementSymbol> symbolMap = null;
    if (queryNode.getBindings() != null && queryNode.getBindings().size() > 0) {
        symbolMap = new HashMap<ElementSymbol, ElementSymbol>();
        // Create ElementSymbols for each InputParameter
        final List<ElementSymbol> elements = new ArrayList<ElementSymbol>(queryNode.getBindings().size());
        boolean positional = true;
        for (Expression ses : parseBindings(queryNode)) {
            String name = Symbol.getShortName(ses);
            if (ses instanceof AliasSymbol) {
                ses = ((AliasSymbol) ses).getSymbol();
                positional = false;
            }
            ElementSymbol elementSymbol = (ElementSymbol) ses;
            ResolverVisitor.resolveLanguageObject(elementSymbol, metadata);
            elementSymbol.setIsExternalReference(true);
            if (!positional) {
                symbolMap.put(new ElementSymbol("INPUT" + Symbol.SEPARATOR + name), elementSymbol.clone());
                symbolMap.put(new ElementSymbol(BINDING_GROUP + Symbol.SEPARATOR + name), elementSymbol.clone());
                elementSymbol.setShortName(name);
            }
            elements.add(elementSymbol);
        }
        if (positional) {
            ExpressionMappingVisitor emv = new ExpressionMappingVisitor(null) {

                @Override
                public Expression replaceExpression(Expression element) {
                    if (!(element instanceof Reference)) {
                        return element;
                    }
                    Reference ref = (Reference) element;
                    if (!ref.isPositional()) {
                        return ref;
                    }
                    return elements.get(ref.getIndex()).clone();
                }
            };
            DeepPostOrderNavigator.doVisit(currentCommand, emv);
        } else {
            TempMetadataStore rootExternalStore = new TempMetadataStore();
            GroupContext externalGroups = new GroupContext();
            // $NON-NLS-1$
            ProcedureContainerResolver.addScalarGroup("INPUT", rootExternalStore, externalGroups, elements);
            ProcedureContainerResolver.addScalarGroup(BINDING_GROUP, rootExternalStore, externalGroups, elements);
            QueryResolver.setChildMetadata(currentCommand, rootExternalStore, externalGroups);
        }
    }
    TempMetadataStore result = resolveCommand(currentCommand, metadata, false);
    if (replaceBindings && symbolMap != null && !symbolMap.isEmpty()) {
        ExpressionMappingVisitor emv = new ExpressionMappingVisitor(symbolMap);
        DeepPostOrderNavigator.doVisit(currentCommand, emv);
    }
    return result;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Reference(org.teiid.query.sql.symbol.Reference) ArrayList(java.util.ArrayList) ExpressionMappingVisitor(org.teiid.query.sql.visitor.ExpressionMappingVisitor) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Expression(org.teiid.query.sql.symbol.Expression) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) GroupContext(org.teiid.query.sql.lang.GroupContext)

Example 12 with ExpressionMappingVisitor

use of org.teiid.query.sql.visitor.ExpressionMappingVisitor 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 13 with ExpressionMappingVisitor

use of org.teiid.query.sql.visitor.ExpressionMappingVisitor in project teiid by teiid.

the class CommandBuilder method getCommand.

public org.teiid.language.Command getCommand(String queryString, boolean generateAliases, boolean supportsGroupAlias) {
    Command command = null;
    try {
        command = QueryParser.getQueryParser().parseCommand(queryString);
        QueryResolver.resolveCommand(command, metadata);
        command = QueryRewriter.rewrite(command, metadata, null);
        expandAllSymbol(command);
        if (generateAliases) {
            command = (Command) command.clone();
            command.acceptVisitor(new AliasGenerator(supportsGroupAlias));
        }
        // the language bridge doesn't expect References
        ValueIteratorProviderCollectorVisitor v = new ValueIteratorProviderCollectorVisitor();
        v.setCollectLateral(true);
        PreOrderNavigator.doVisit(command, v);
        for (SubqueryContainer<?> container : v.getValueIteratorProviders()) {
            ExpressionMappingVisitor visitor = new ExpressionMappingVisitor(null) {

                @Override
                public Expression replaceExpression(Expression element) {
                    if (element instanceof Reference) {
                        return ((Reference) element).getExpression();
                    }
                    return element;
                }
            };
            DeepPostOrderNavigator.doVisit(command, visitor);
        }
        return languageBridgeFactory.translate(command);
    } catch (TeiidException e) {
        throw new TeiidRuntimeException(e);
    }
}
Also used : Command(org.teiid.query.sql.lang.Command) AliasGenerator(org.teiid.query.optimizer.relational.AliasGenerator) Expression(org.teiid.query.sql.symbol.Expression) Reference(org.teiid.query.sql.symbol.Reference) ValueIteratorProviderCollectorVisitor(org.teiid.query.sql.visitor.ValueIteratorProviderCollectorVisitor) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) ExpressionMappingVisitor(org.teiid.query.sql.visitor.ExpressionMappingVisitor) TeiidException(org.teiid.core.TeiidException)

Aggregations

ExpressionMappingVisitor (org.teiid.query.sql.visitor.ExpressionMappingVisitor)13 Expression (org.teiid.query.sql.symbol.Expression)6 SymbolMap (org.teiid.query.sql.util.SymbolMap)6 ArrayList (java.util.ArrayList)4 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 DataPolicy (org.teiid.adminapi.DataPolicy)2 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)2 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)2 RuleMergeCriteria (org.teiid.query.optimizer.relational.rules.RuleMergeCriteria)2 LanguageObject (org.teiid.query.sql.LanguageObject)2 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)2 Reference (org.teiid.query.sql.symbol.Reference)2 UpdateMapping (org.teiid.query.validator.UpdateValidator.UpdateMapping)2 List (java.util.List)1 DataPolicyMetadata (org.teiid.adminapi.impl.DataPolicyMetadata)1 PermissionMetaData (org.teiid.adminapi.impl.DataPolicyMetadata.PermissionMetaData)1 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)1 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)1