Search in sources :

Example 31 with TempMetadataStore

use of org.teiid.query.metadata.TempMetadataStore in project teiid by teiid.

the class ProcedureContainerResolver method findChildCommandMetadata.

/**
 * Set the appropriate "external" metadata for the given command
 * @param inferProcedureResultSetColumns
 * @throws QueryResolverException
 */
public static void findChildCommandMetadata(Command currentCommand, GroupSymbol container, int type, QueryMetadataInterface metadata, boolean inferProcedureResultSetColumns) throws QueryMetadataException, TeiidComponentException, QueryResolverException {
    // find the childMetadata using a clean metadata store
    TempMetadataStore childMetadata = new TempMetadataStore();
    TempMetadataAdapter tma = new TempMetadataAdapter(metadata, childMetadata);
    GroupContext externalGroups = new GroupContext();
    if (currentCommand instanceof TriggerAction) {
        TriggerAction ta = (TriggerAction) currentCommand;
        ta.setView(container);
        // TODO: it seems easier to just inline the handling here rather than have each of the resolvers check for trigger actions
        List<ElementSymbol> viewElements = ResolverUtil.resolveElementsInGroup(ta.getView(), metadata);
        if (type == Command.TYPE_UPDATE || type == Command.TYPE_INSERT) {
            ProcedureContainerResolver.addChanging(tma.getMetadataStore(), externalGroups, viewElements);
            ProcedureContainerResolver.addScalarGroup(SQLConstants.Reserved.NEW, tma.getMetadataStore(), externalGroups, viewElements, false);
            if (type == Command.TYPE_INSERT) {
                List<ElementSymbol> key = InsertResolver.getAutoIncrementKey(ta.getView().getMetadataID(), viewElements, metadata);
                if (key != null) {
                    ProcedureContainerResolver.addScalarGroup(SQLConstants.NonReserved.KEY, tma.getMetadataStore(), externalGroups, key, true);
                }
            }
        }
        if (type == Command.TYPE_UPDATE || type == Command.TYPE_DELETE) {
            ProcedureContainerResolver.addScalarGroup(SQLConstants.Reserved.OLD, tma.getMetadataStore(), externalGroups, viewElements, false);
        }
    } else if (currentCommand instanceof CreateProcedureCommand) {
        CreateProcedureCommand cupc = (CreateProcedureCommand) currentCommand;
        cupc.setVirtualGroup(container);
        if (type == Command.TYPE_STORED_PROCEDURE) {
            StoredProcedureInfo info = metadata.getStoredProcedureInfoForProcedure(container.getName());
            // Create temporary metadata that defines a group based on either the stored proc
            // name or the stored query name - this will be used later during planning
            String procName = info.getProcedureCallableName();
            // Look through parameters to find input elements - these become child metadata
            List<ElementSymbol> tempElements = new ArrayList<ElementSymbol>(info.getParameters().size());
            boolean[] updatable = new boolean[info.getParameters().size()];
            int i = 0;
            List<ElementSymbol> rsColumns = Collections.emptyList();
            for (SPParameter param : info.getParameters()) {
                if (param.getParameterType() != ParameterInfo.RESULT_SET) {
                    ElementSymbol symbol = param.getParameterSymbol();
                    tempElements.add(symbol);
                    updatable[i++] = param.getParameterType() != ParameterInfo.IN;
                    if (param.getParameterType() == ParameterInfo.RETURN_VALUE) {
                        cupc.setReturnVariable(symbol);
                    }
                } else {
                    rsColumns = param.getResultSetColumns();
                }
            }
            if (inferProcedureResultSetColumns) {
                rsColumns = null;
            }
            GroupSymbol gs = ProcedureContainerResolver.addScalarGroup(procName, childMetadata, externalGroups, tempElements, updatable);
            if (cupc.getReturnVariable() != null) {
                ResolverVisitor.resolveLanguageObject(cupc.getReturnVariable(), Arrays.asList(gs), metadata);
            }
            cupc.setResultSetColumns(rsColumns);
            // the relational planner will override this with the appropriate value
            cupc.setProjectedSymbols(rsColumns);
        } else {
            cupc.setUpdateType(type);
        }
    }
    QueryResolver.setChildMetadata(currentCommand, childMetadata, externalGroups);
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) TriggerAction(org.teiid.query.sql.proc.TriggerAction) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) SPParameter(org.teiid.query.sql.lang.SPParameter) StoredProcedureInfo(org.teiid.query.metadata.StoredProcedureInfo) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) ArrayList(java.util.ArrayList) List(java.util.List) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) GroupContext(org.teiid.query.sql.lang.GroupContext)

Example 32 with TempMetadataStore

use of org.teiid.query.metadata.TempMetadataStore 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 33 with TempMetadataStore

use of org.teiid.query.metadata.TempMetadataStore in project teiid by teiid.

the class QueryResolver method setChildMetadata.

public static void setChildMetadata(Command subCommand, Command parent) {
    TempMetadataStore childMetadata = parent.getTemporaryMetadata();
    GroupContext parentContext = parent.getExternalGroupContexts();
    setChildMetadata(subCommand, childMetadata, parentContext);
}
Also used : TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) GroupContext(org.teiid.query.sql.lang.GroupContext)

Example 34 with TempMetadataStore

use of org.teiid.query.metadata.TempMetadataStore 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 35 with TempMetadataStore

use of org.teiid.query.metadata.TempMetadataStore in project teiid by teiid.

the class QueryResolver method setChildMetadata.

public static void setChildMetadata(Command subCommand, TempMetadataStore parentTempMetadata, GroupContext parentContext) {
    TempMetadataStore tempMetadata = subCommand.getTemporaryMetadata();
    if (tempMetadata == null) {
        subCommand.setTemporaryMetadata(parentTempMetadata.clone());
    } else {
        tempMetadata.getData().putAll(parentTempMetadata.getData());
    }
    subCommand.setExternalGroupContexts(parentContext);
}
Also used : TempMetadataStore(org.teiid.query.metadata.TempMetadataStore)

Aggregations

TempMetadataStore (org.teiid.query.metadata.TempMetadataStore)52 TempMetadataAdapter (org.teiid.query.metadata.TempMetadataAdapter)45 Test (org.junit.Test)33 ProcessorPlan (org.teiid.query.processor.ProcessorPlan)32 Ignore (org.junit.Ignore)21 TempMetadataID (org.teiid.query.metadata.TempMetadataID)6 CommandContext (org.teiid.query.util.CommandContext)6 List (java.util.List)5 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)5 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)5 Command (org.teiid.query.sql.lang.Command)5 GroupContext (org.teiid.query.sql.lang.GroupContext)5 Expression (org.teiid.query.sql.symbol.Expression)5 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)5 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)4 ArrayList (java.util.ArrayList)3 TeiidProcessingException (org.teiid.core.TeiidProcessingException)3 AnalysisRecord (org.teiid.query.analysis.AnalysisRecord)3 CreateProcedureCommand (org.teiid.query.sql.proc.CreateProcedureCommand)3 LinkedList (java.util.LinkedList)2