Search in sources :

Example 1 with Statement

use of org.teiid.query.sql.proc.Statement in project teiid by teiid.

the class TriggerActionPlanner method rewritePlan.

/**
 * look for the simple case of a mapping to a single insert statement trigger action - and reconstruct the plan as a single insert
 * TODO: need internal primitives for delete/update batching in a loop for delete/update cases
 */
private ProcessorPlan rewritePlan(TriggerAction ta, IDGenerator idGenerator, QueryMetadataInterface metadata, CapabilitiesFinder capFinder, AnalysisRecord analysisRecord, CommandContext context, QueryCommand query, Map<ElementSymbol, Expression> mapping, Insert insert) throws QueryMetadataException, QueryResolverException, TeiidComponentException, QueryPlannerException {
    if (ta.getBlock().getStatements().size() != 1) {
        return null;
    }
    Statement s = ta.getBlock().getStatements().get(0);
    if (!(s instanceof CommandStatement)) {
        return null;
    }
    CommandStatement cs = (CommandStatement) s;
    if (!(cs.getCommand() instanceof Insert)) {
        return null;
    }
    Insert mapped = (Insert) cs.getCommand();
    if (mapped.getQueryExpression() != null) {
        return null;
    }
    if (insert.getQueryExpression() != null) {
        // use a unique inline view name to make the final remapping easier
        GroupSymbol group = new GroupSymbol("X");
        Collection<GroupSymbol> groups = GroupCollectorVisitor.getGroups(query, true);
        for (int i = 0; groups.contains(group); i++) {
            group.setName("X_" + i);
        }
        List<Expression> projectedSymbols = query.getProjectedSymbols();
        Query queryExpression = QueryRewriter.createInlineViewQuery(group, query, metadata, projectedSymbols);
        List<Expression> viewSymbols = new ArrayList<Expression>(queryExpression.getSelect().getSymbols());
        // switch to the values
        queryExpression.getSelect().clearSymbols();
        List<Expression> values = mapped.getValues();
        queryExpression.getSelect().addSymbols(values);
        values.clear();
        // update the mapping to the view symbols
        for (int i = 0; i < projectedSymbols.size(); i++) {
            ElementSymbol es = insert.getVariables().get(i);
            mapping.put(new ElementSymbol(es.getShortName(), new GroupSymbol(SQLConstants.Reserved.NEW)), SymbolMap.getExpression(viewSymbols.get(i)));
        }
        // map to the query form - changes references back to element form
        SymbolMap queryMapping = new SymbolMap();
        queryMapping.asUpdatableMap().putAll(mapping);
        ExpressionMappingVisitor visitor = new RuleMergeCriteria.ReferenceReplacementVisitor(queryMapping);
        DeepPostOrderNavigator.doVisit(queryExpression.getSelect(), visitor);
        // now we can return a plan based off a single insert statement
        mapped.setQueryExpression(queryExpression);
        return QueryOptimizer.optimizePlan(mapped, metadata, idGenerator, capFinder, analysisRecord, context);
    }
    List<Expression> values = mapped.getValues();
    SymbolMap queryMapping = new SymbolMap();
    queryMapping.asUpdatableMap().putAll(mapping);
    ExpressionMappingVisitor visitor = new RuleMergeCriteria.ReferenceReplacementVisitor(queryMapping);
    Select select = new Select();
    select.addSymbols(values);
    DeepPostOrderNavigator.doVisit(select, visitor);
    values.clear();
    for (Expression ex : select.getSymbols()) {
        try {
            values.add(QueryRewriter.rewriteExpression(SymbolMap.getExpression(ex), context, metadata));
        } catch (TeiidProcessingException e) {
            throw new QueryPlannerException(e);
        }
    }
    return QueryOptimizer.optimizePlan(mapped, metadata, idGenerator, capFinder, analysisRecord, context);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) CommandStatement(org.teiid.query.sql.proc.CommandStatement) Statement(org.teiid.query.sql.proc.Statement) ArrayList(java.util.ArrayList) SymbolMap(org.teiid.query.sql.util.SymbolMap) ExpressionMappingVisitor(org.teiid.query.sql.visitor.ExpressionMappingVisitor) TeiidProcessingException(org.teiid.core.TeiidProcessingException) CommandStatement(org.teiid.query.sql.proc.CommandStatement) Expression(org.teiid.query.sql.symbol.Expression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryPlannerException(org.teiid.api.exception.query.QueryPlannerException)

Aggregations

ArrayList (java.util.ArrayList)1 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 CommandStatement (org.teiid.query.sql.proc.CommandStatement)1 Statement (org.teiid.query.sql.proc.Statement)1 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)1 Expression (org.teiid.query.sql.symbol.Expression)1 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)1 SymbolMap (org.teiid.query.sql.util.SymbolMap)1 ExpressionMappingVisitor (org.teiid.query.sql.visitor.ExpressionMappingVisitor)1