Search in sources :

Example 1 with SetQuery

use of org.teiid.query.sql.lang.SetQuery in project teiid by teiid.

the class InsertResolver method resolveProceduralCommand.

/**
 * Resolve an INSERT.  Need to resolve elements, constants, types, etc.
 * @see org.teiid.query.resolver.ProcedureContainerResolver#resolveProceduralCommand(org.teiid.query.sql.lang.Command, org.teiid.query.metadata.TempMetadataAdapter)
 */
public void resolveProceduralCommand(Command command, TempMetadataAdapter metadata) throws QueryMetadataException, QueryResolverException, TeiidComponentException {
    // Cast to known type
    Insert insert = (Insert) command;
    if (insert.getValues() != null) {
        QueryResolver.resolveSubqueries(command, metadata, null);
        // variables and values must be resolved separately to account for implicitly defined temp groups
        resolveList(insert.getValues(), metadata, insert.getExternalGroupContexts(), null);
    }
    boolean usingQuery = insert.getQueryExpression() != null;
    // resolve subquery if there
    if (usingQuery) {
        QueryResolver.setChildMetadata(insert.getQueryExpression(), command);
        QueryResolver.resolveCommand(insert.getQueryExpression(), metadata.getMetadata(), false);
    }
    Set<GroupSymbol> groups = new HashSet<GroupSymbol>();
    groups.add(insert.getGroup());
    // resolve any functions in the values
    List values = insert.getValues();
    if (usingQuery) {
        values = insert.getQueryExpression().getProjectedSymbols();
    }
    if (insert.getVariables().isEmpty()) {
        if (insert.getGroup().isResolved()) {
            List<ElementSymbol> variables = ResolverUtil.resolveElementsInGroup(insert.getGroup(), metadata);
            for (Iterator<ElementSymbol> i = variables.iterator(); i.hasNext(); ) {
                insert.addVariable(i.next().clone());
            }
        } else {
            for (int i = 0; i < values.size(); i++) {
                if (usingQuery) {
                    Expression ses = (Expression) values.get(i);
                    ElementSymbol es = new ElementSymbol(Symbol.getShortName(ses));
                    es.setType(ses.getType());
                    insert.addVariable(es);
                } else {
                    // $NON-NLS-1$
                    insert.addVariable(new ElementSymbol("expr" + i));
                }
            }
        }
    } else if (insert.getGroup().isResolved()) {
        resolveVariables(metadata, insert, groups);
    }
    resolveTypes(insert, metadata, values, usingQuery);
    if (usingQuery && insert.getQueryExpression() instanceof SetQuery) {
        // now that the first branch is set, we need to make sure that all branches conform
        QueryResolver.resolveCommand(insert.getQueryExpression(), metadata.getMetadata(), false);
        resolveTypes(insert, metadata, values, usingQuery);
    }
    if (!insert.getGroup().isResolved()) {
        // define the implicit temp group
        ResolverUtil.resolveImplicitTempGroup(metadata, insert.getGroup(), insert.getVariables());
        resolveVariables(metadata, insert, groups);
        // ensure that the types match
        resolveTypes(insert, metadata, values, usingQuery);
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) SetQuery(org.teiid.query.sql.lang.SetQuery) Expression(org.teiid.query.sql.symbol.Expression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) ArrayList(java.util.ArrayList) List(java.util.List) Insert(org.teiid.query.sql.lang.Insert) HashSet(java.util.HashSet)

Example 2 with SetQuery

use of org.teiid.query.sql.lang.SetQuery in project teiid by teiid.

the class SetQueryResolver method setProjectedTypes.

private void setProjectedTypes(SetQuery setQuery, List<Class<?>> firstProjectTypes, QueryMetadataInterface metadata) throws QueryResolverException {
    for (QueryCommand subCommand : setQuery.getQueryCommands()) {
        if (!(subCommand instanceof SetQuery)) {
            continue;
        }
        SetQuery child = (SetQuery) subCommand;
        List projectedSymbols = child.getProjectedSymbols();
        if (child.getOrderBy() != null) {
            for (int j = 0; j < projectedSymbols.size(); j++) {
                Expression ses = (Expression) projectedSymbols.get(j);
                Class<?> targetType = firstProjectTypes.get(j);
                if (ses.getType() != targetType && orderByContainsVariable(child.getOrderBy(), ses, j)) {
                    String sourceTypeName = DataTypeManager.getDataTypeName(ses.getType());
                    String targetTypeName = DataTypeManager.getDataTypeName(targetType);
                    throw new QueryResolverException(// $NON-NLS-1$
                    QueryPlugin.Util.getString(// $NON-NLS-1$
                    "UnionQueryResolver.type_conversion", new Object[] { ses, sourceTypeName, targetTypeName }));
                }
            }
        }
        child.setProjectedTypes(firstProjectTypes, metadata);
        setProjectedTypes(child, firstProjectTypes, metadata);
    }
}
Also used : SetQuery(org.teiid.query.sql.lang.SetQuery) Expression(org.teiid.query.sql.symbol.Expression) QueryCommand(org.teiid.query.sql.lang.QueryCommand) ArrayList(java.util.ArrayList) List(java.util.List) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 3 with SetQuery

use of org.teiid.query.sql.lang.SetQuery in project teiid by teiid.

the class RelationalPlan method toString.

public String toString() {
    StringBuilder sb = new StringBuilder();
    if (this.with != null) {
        sb.append(SQLConstants.Reserved.WITH);
        for (WithQueryCommand withCommand : this.with) {
            // $NON-NLS-1$
            sb.append("\n");
            sb.append(withCommand.getGroupSymbol());
            if (withCommand.isRecursive()) {
                // $NON-NLS-1$
                sb.append(" anchor\n").append(((SetQuery) withCommand.getCommand()).getLeftQuery().getProcessorPlan());
                // $NON-NLS-1$
                sb.append("recursive\n").append(((SetQuery) withCommand.getCommand()).getRightQuery().getProcessorPlan());
            } else {
                // $NON-NLS-1$
                sb.append("\n");
                sb.append(withCommand.getCommand().getProcessorPlan());
            }
        }
        // $NON-NLS-1$
        sb.append("body\n");
    }
    sb.append(this.root.toString());
    return sb.toString();
}
Also used : SetQuery(org.teiid.query.sql.lang.SetQuery) WithQueryCommand(org.teiid.query.sql.lang.WithQueryCommand)

Example 4 with SetQuery

use of org.teiid.query.sql.lang.SetQuery in project teiid by teiid.

the class RelationalPlan method requiresTransaction.

@Override
public Boolean requiresTransaction(boolean transactionalReads) {
    if (this.with != null) {
        for (WithQueryCommand withCommand : this.with) {
            if (withCommand.isRecursive()) {
                SetQuery setQuery = (SetQuery) withCommand.getCommand();
                Boolean leftRequires = setQuery.getLeftQuery().getProcessorPlan().requiresTransaction(transactionalReads);
                Boolean rightRequires = setQuery.getLeftQuery().getProcessorPlan().requiresTransaction(transactionalReads);
                if (!Boolean.FALSE.equals(leftRequires) || !Boolean.FALSE.equals(rightRequires)) {
                    return true;
                }
            } else {
                Boolean requires = withCommand.getCommand().getProcessorPlan().requiresTransaction(transactionalReads);
                if (!Boolean.FALSE.equals(requires)) {
                    return true;
                }
            }
        }
    }
    return requiresTransaction(transactionalReads, root);
}
Also used : SetQuery(org.teiid.query.sql.lang.SetQuery) WithQueryCommand(org.teiid.query.sql.lang.WithQueryCommand)

Example 5 with SetQuery

use of org.teiid.query.sql.lang.SetQuery in project teiid by teiid.

the class LanguageBridgeFactory method translate.

public org.teiid.language.Command translate(Command command) {
    try {
        if (command == null) {
            return null;
        }
        if (command instanceof Query) {
            Select result = translate((Query) command);
            result.setDependentValues(this.dependentSets);
            setProjected(result);
            return result;
        } else if (command instanceof SetQuery) {
            org.teiid.language.SetQuery result = translate((SetQuery) command);
            setProjected(result);
            return result;
        } else if (command instanceof Insert) {
            return translate((Insert) command);
        } else if (command instanceof Update) {
            return translate((Update) command);
        } else if (command instanceof Delete) {
            return translate((Delete) command);
        } else if (command instanceof StoredProcedure) {
            return translate((StoredProcedure) command);
        } else if (command instanceof BatchedUpdateCommand) {
            return translate((BatchedUpdateCommand) command);
        }
        // $NON-NLS-1$
        throw new AssertionError(command.getClass().getName() + " " + command);
    } finally {
        this.allValues.clear();
        this.dependentSets = null;
        this.valueIndex = 0;
    }
}
Also used : Delete(org.teiid.query.sql.lang.Delete) SetQuery(org.teiid.query.sql.lang.SetQuery) SetQuery(org.teiid.query.sql.lang.SetQuery) Select(org.teiid.language.Select) Insert(org.teiid.query.sql.lang.Insert) Update(org.teiid.query.sql.lang.Update)

Aggregations

SetQuery (org.teiid.query.sql.lang.SetQuery)24 Test (org.junit.Test)10 Query (org.teiid.query.sql.lang.Query)9 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)9 Select (org.teiid.query.sql.lang.Select)6 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)6 MultipleElementSymbol (org.teiid.query.sql.symbol.MultipleElementSymbol)6 From (org.teiid.query.sql.lang.From)5 WithQueryCommand (org.teiid.query.sql.lang.WithQueryCommand)5 ArrayList (java.util.ArrayList)4 Expression (org.teiid.query.sql.symbol.Expression)4 List (java.util.List)3 Constant (org.teiid.query.sql.symbol.Constant)3 Insert (org.teiid.query.sql.lang.Insert)2 Limit (org.teiid.query.sql.lang.Limit)2 QueryCommand (org.teiid.query.sql.lang.QueryCommand)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)1