Search in sources :

Example 51 with SymbolMap

use of org.teiid.query.sql.util.SymbolMap in project teiid by teiid.

the class RuleSubstituteExpressions method execute.

@Override
public PlanNode execute(PlanNode plan, QueryMetadataInterface metadata, CapabilitiesFinder capabilitiesFinder, RuleStack rules, AnalysisRecord analysisRecord, CommandContext context) throws QueryPlannerException, QueryMetadataException, TeiidComponentException {
    boolean substitued = false;
    for (PlanNode accessNode : NodeEditor.findAllNodes(plan, NodeConstants.Types.ACCESS)) {
        if (accessNode.getParent() == null) {
            continue;
        }
        for (GroupSymbol gs : accessNode.getGroups()) {
            Map<Expression, Integer> fbis = metadata.getFunctionBasedExpressions(gs.getMetadataID());
            if (fbis == null) {
                continue;
            }
            substitued = true;
            Map<Expression, ElementSymbol> replacements = new HashMap<Expression, ElementSymbol>();
            GroupSymbol gsOrig = new GroupSymbol(gs.getNonCorrelationName());
            gsOrig.setMetadataID(gs.getMetadataID());
            List<ElementSymbol> curremtElems = ResolverUtil.resolveElementsInGroup(gs, metadata);
            List<ElementSymbol> origElems = ResolverUtil.resolveElementsInGroup(gsOrig, metadata);
            boolean nameMatches = gs.getNonCorrelationName().equals(gs.getName());
            SymbolMap map = SymbolMap.createSymbolMap(origElems, curremtElems);
            for (Map.Entry<Expression, Integer> entry : fbis.entrySet()) {
                Expression ex = entry.getKey();
                if (!nameMatches) {
                    ex = (Expression) ex.clone();
                    ExpressionMappingVisitor.mapExpressions(ex, map.asMap());
                }
                replacements.put(ex, curremtElems.get(entry.getValue() - 1));
            }
            FrameUtil.convertFrame(accessNode.getParent(), gs, null, replacements, metadata);
        }
    }
    if (substitued) {
        rules.push(RuleConstants.PUSH_SELECT_CRITERIA);
    }
    return plan;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) HashMap(java.util.HashMap) SymbolMap(org.teiid.query.sql.util.SymbolMap) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) Expression(org.teiid.query.sql.symbol.Expression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) HashMap(java.util.HashMap) SymbolMap(org.teiid.query.sql.util.SymbolMap) Map(java.util.Map)

Example 52 with SymbolMap

use of org.teiid.query.sql.util.SymbolMap in project teiid by teiid.

the class QueryRewriter method rewriteInherentDelete.

private Command rewriteInherentDelete(Delete delete, UpdateInfo info) throws QueryMetadataException, TeiidComponentException, QueryResolverException, TeiidProcessingException {
    UpdateMapping mapping = info.getDeleteTarget();
    if (info.isSimple()) {
        Collection<ElementSymbol> elements = getAllElementsUsed(delete, delete.getGroup());
        UpdateMapping fullMapping = info.findUpdateMapping(elements, false);
        if (fullMapping != null) {
            delete.setGroup(mapping.getGroup().clone());
            // TODO: properly handle correlated references
            DeepPostOrderNavigator.doVisit(delete, new ExpressionMappingVisitor(mapping.getUpdatableViewSymbols(), true));
            delete.setUpdateInfo(ProcedureContainerResolver.getUpdateInfo(delete.getGroup(), metadata, Command.TYPE_DELETE, true));
            if (info.getViewDefinition().getCriteria() != null) {
                delete.setCriteria(Criteria.combineCriteria(delete.getCriteria(), (Criteria) info.getViewDefinition().getCriteria().clone()));
            }
            return rewriteDelete(delete);
        }
    }
    Query query = (Query) info.getViewDefinition().clone();
    query.setOrderBy(null);
    SymbolMap expressionMapping = SymbolMap.createSymbolMap(delete.getGroup(), query.getProjectedSymbols(), metadata);
    query.setSelect(new Select());
    ExpressionMappingVisitor emv = new ExpressionMappingVisitor(expressionMapping.asMap(), true);
    Criteria crit = delete.getCriteria();
    if (crit != null) {
        PostOrderNavigator.doVisit(crit, emv);
        query.setCriteria(Criteria.combineCriteria(query.getCriteria(), crit));
    }
    GroupSymbol group = mapping.getGroup();
    String correlationName = mapping.getCorrelatedName().getName();
    return createDeleteProcedure(delete, query, group, correlationName);
}
Also used : UpdateMapping(org.teiid.query.validator.UpdateValidator.UpdateMapping) SymbolMap(org.teiid.query.sql.util.SymbolMap) RuleMergeCriteria(org.teiid.query.optimizer.relational.rules.RuleMergeCriteria) ExpressionMappingVisitor(org.teiid.query.sql.visitor.ExpressionMappingVisitor)

Example 53 with SymbolMap

use of org.teiid.query.sql.util.SymbolMap in project teiid by teiid.

the class QueryRewriter method rewriteInherentUpdate.

private Command rewriteInherentUpdate(Update update, UpdateInfo info) throws QueryValidatorException, QueryMetadataException, TeiidComponentException, QueryResolverException, TeiidProcessingException {
    UpdateMapping mapping = info.findUpdateMapping(update.getChangeList().getClauseMap().keySet(), false);
    if (mapping == null) {
        throw new QueryValidatorException(QueryPlugin.Event.TEIID30376, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30376, update.getChangeList().getClauseMap().keySet()));
    }
    Map<ElementSymbol, ElementSymbol> symbolMap = mapping.getUpdatableViewSymbols();
    if (info.isSimple()) {
        Collection<ElementSymbol> elements = getAllElementsUsed(update, update.getGroup());
        UpdateMapping fullMapping = info.findUpdateMapping(elements, false);
        if (fullMapping != null) {
            update.setGroup(mapping.getGroup().clone());
            for (SetClause clause : update.getChangeList().getClauses()) {
                clause.setSymbol(symbolMap.get(clause.getSymbol()));
            }
            // TODO: properly handle correlated references
            DeepPostOrderNavigator.doVisit(update, new ExpressionMappingVisitor(symbolMap, true));
            if (info.getViewDefinition().getCriteria() != null) {
                update.setCriteria(Criteria.combineCriteria(update.getCriteria(), (Criteria) info.getViewDefinition().getCriteria().clone()));
            }
            // resolve
            update.setUpdateInfo(ProcedureContainerResolver.getUpdateInfo(update.getGroup(), metadata, Command.TYPE_UPDATE, true));
            return rewriteUpdate(update);
        }
    }
    Query query = (Query) info.getViewDefinition().clone();
    query.setOrderBy(null);
    SymbolMap expressionMapping = SymbolMap.createSymbolMap(update.getGroup(), query.getProjectedSymbols(), metadata);
    SetClauseList setClauseList = (SetClauseList) update.getChangeList().clone();
    GroupSymbol varGroup = getVarGroup(update);
    ArrayList<Expression> selectSymbols = mapChangeList(setClauseList, symbolMap, varGroup);
    query.setSelect(new Select(selectSymbols));
    ExpressionMappingVisitor emv = new ExpressionMappingVisitor(expressionMapping.asMap(), true);
    PostOrderNavigator.doVisit(query.getSelect(), emv);
    Criteria crit = update.getCriteria();
    if (crit != null) {
        PostOrderNavigator.doVisit(crit, emv);
        query.setCriteria(Criteria.combineCriteria(query.getCriteria(), crit));
    }
    GroupSymbol group = mapping.getGroup();
    String correlationName = mapping.getCorrelatedName().getName();
    return createUpdateProcedure(update, query, group, correlationName, setClauseList, varGroup, null);
}
Also used : UpdateMapping(org.teiid.query.validator.UpdateValidator.UpdateMapping) SymbolMap(org.teiid.query.sql.util.SymbolMap) RuleMergeCriteria(org.teiid.query.optimizer.relational.rules.RuleMergeCriteria) ExpressionMappingVisitor(org.teiid.query.sql.visitor.ExpressionMappingVisitor) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException)

Example 54 with SymbolMap

use of org.teiid.query.sql.util.SymbolMap in project teiid by teiid.

the class QueryRewriter method determineCorrelatedReferences.

private void determineCorrelatedReferences(List<GroupSymbol> groups, PlannedResult plannedResult) {
    if (plannedResult.query.getCorrelatedReferences() == null) {
        // create the correlated refs if they exist
        // there is a little bit of a design problem here that null usually means no refs.
        ArrayList<Reference> correlatedReferences = new ArrayList<Reference>();
        CorrelatedReferenceCollectorVisitor.collectReferences(plannedResult.query, groups, correlatedReferences, metadata);
        if (!correlatedReferences.isEmpty()) {
            SymbolMap map = new SymbolMap();
            for (Reference reference : correlatedReferences) {
                map.addMapping(reference.getExpression(), reference.getExpression());
            }
            plannedResult.query.setCorrelatedReferences(map);
        }
    }
}
Also used : SymbolMap(org.teiid.query.sql.util.SymbolMap)

Example 55 with SymbolMap

use of org.teiid.query.sql.util.SymbolMap in project teiid by teiid.

the class TestFrameUtil method testJoinGroups1.

@Test
public void testJoinGroups1() throws Exception {
    PlanNode joinNode = getExamplePlan();
    PlanNode projectNode = NodeFactory.getNewNode(NodeConstants.Types.PROJECT);
    // $NON-NLS-1$
    ElementSymbol e1 = new ElementSymbol("e1");
    e1.setGroupSymbol(getGroup(3));
    projectNode.setProperty(Info.PROJECT_COLS, Arrays.asList(e1));
    projectNode.addFirstChild(joinNode);
    projectNode.addGroup(getGroup(3));
    PlanNode sourceNode = NodeFactory.getNewNode(NodeConstants.Types.SOURCE);
    sourceNode.addFirstChild(projectNode);
    GroupSymbol four = getGroup(4);
    sourceNode.addGroup(four);
    // $NON-NLS-1$
    ElementSymbol e2 = new ElementSymbol("e2");
    e2.setGroupSymbol(four);
    SymbolMap sm = SymbolMap.createSymbolMap(Arrays.asList(e2), Arrays.asList(e1));
    sourceNode.setProperty(Info.SYMBOL_MAP, sm);
    PlanNode projectNode1 = NodeFactory.getNewNode(NodeConstants.Types.PROJECT);
    projectNode1.addFirstChild(sourceNode);
    projectNode1.addGroup(four);
    projectNode1.setProperty(Info.PROJECT_COLS, Arrays.asList(e2));
    // replace source 3 with groups 5, 6
    SymbolMap replacement = SymbolMap.createSymbolMap(Arrays.asList(e1), Arrays.asList(new Constant(null)));
    FrameUtil.convertFrame(NodeEditor.findNodePreOrder(joinNode, NodeConstants.Types.SOURCE), getGroup(3), new HashSet<GroupSymbol>(Arrays.asList(getGroup(5), getGroup(6))), replacement.asMap(), null);
    // even though this is a cross join it should still retain its groups
    assertEquals(4, joinNode.getGroups().size());
    assertEquals(0, NodeEditor.findNodePreOrder(joinNode, NodeConstants.Types.SELECT).getGroups().size());
    assertEquals(1, projectNode1.getGroups().size());
    assertEquals(0, projectNode.getGroups().size());
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) Constant(org.teiid.query.sql.symbol.Constant) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) SymbolMap(org.teiid.query.sql.util.SymbolMap) Test(org.junit.Test)

Aggregations

SymbolMap (org.teiid.query.sql.util.SymbolMap)56 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)37 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)28 Expression (org.teiid.query.sql.symbol.Expression)28 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)21 ArrayList (java.util.ArrayList)16 List (java.util.List)15 Criteria (org.teiid.query.sql.lang.Criteria)10 LinkedList (java.util.LinkedList)8 Map (java.util.Map)8 HashMap (java.util.HashMap)6 Constant (org.teiid.query.sql.symbol.Constant)6 LinkedHashSet (java.util.LinkedHashSet)5 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)5 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)5 OrderBy (org.teiid.query.sql.lang.OrderBy)5 HashSet (java.util.HashSet)4 ExpressionMappingVisitor (org.teiid.query.sql.visitor.ExpressionMappingVisitor)4 LinkedHashMap (java.util.LinkedHashMap)3 Set (java.util.Set)3