Search in sources :

Example 1 with WindowFunction

use of org.teiid.query.sql.symbol.WindowFunction in project teiid by teiid.

the class LanguageBridgeFactory method translate.

org.teiid.language.WindowFunction translate(WindowFunction windowFunction) {
    org.teiid.language.WindowFunction result = new org.teiid.language.WindowFunction();
    result.setFunction(translate(windowFunction.getFunction()));
    WindowSpecification ws = new WindowSpecification();
    ws.setOrderBy(translate(windowFunction.getWindowSpecification().getOrderBy(), false));
    List<Expression> partition = windowFunction.getWindowSpecification().getPartition();
    if (partition != null) {
        ArrayList<org.teiid.language.Expression> partitionList = translateExpressionList(partition);
        ws.setPartition(partitionList);
    }
    result.setWindowSpecification(ws);
    return result;
}
Also used : WindowFunction(org.teiid.query.sql.symbol.WindowFunction) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) Expression(org.teiid.query.sql.symbol.Expression) WindowSpecification(org.teiid.language.WindowSpecification) org.teiid.language(org.teiid.language)

Example 2 with WindowFunction

use of org.teiid.query.sql.symbol.WindowFunction in project teiid by teiid.

the class RuleAssignOutputElements method collectRequiredInputSymbols.

/**
 * Collect all required input symbols for a given node.  Input symbols
 * are any symbols that are required in the processing of this node,
 * for instance to create a new element symbol or sort on it, etc.
 * @param node Node to collect for
 * @param metadata
 * @param capFinder
 * @throws TeiidComponentException
 * @throws QueryMetadataException
 */
private List<Expression> collectRequiredInputSymbols(PlanNode node, QueryMetadataInterface metadata, CapabilitiesFinder capFinder) throws QueryMetadataException, TeiidComponentException {
    Set<Expression> requiredSymbols = new LinkedHashSet<Expression>();
    Set<Expression> createdSymbols = new HashSet<Expression>();
    List<Expression> outputCols = (List<Expression>) node.getProperty(NodeConstants.Info.OUTPUT_COLS);
    switch(node.getType()) {
        case NodeConstants.Types.PROJECT:
            {
                List<Expression> projectCols = (List<Expression>) node.getProperty(NodeConstants.Info.PROJECT_COLS);
                PlanNode accessParent = NodeEditor.findParent(node, NodeConstants.Types.ACCESS);
                PlanNode accessNode = null;
                if (accessParent == null) {
                    // find the direct access node
                    accessNode = NodeEditor.findNodePreOrder(node, NodeConstants.Types.ACCESS, NodeConstants.Types.SOURCE | NodeConstants.Types.JOIN | NodeConstants.Types.SET_OP | NodeConstants.Types.GROUP);
                }
                for (Expression ss : projectCols) {
                    if (ss instanceof AliasSymbol) {
                        createdSymbols.add(ss);
                        ss = ((AliasSymbol) ss).getSymbol();
                    }
                    if (ss instanceof WindowFunction || ss instanceof ExpressionSymbol) {
                        createdSymbols.add(ss);
                    }
                    if (!pushProjection(node, metadata, capFinder, requiredSymbols, accessParent, accessNode, ss)) {
                        ElementCollectorVisitor.getElements(ss, requiredSymbols);
                    }
                }
                break;
            }
        case NodeConstants.Types.SELECT:
            Criteria selectCriteria = (Criteria) node.getProperty(NodeConstants.Info.SELECT_CRITERIA);
            ElementCollectorVisitor.getElements(selectCriteria, requiredSymbols);
            break;
        case NodeConstants.Types.JOIN:
            List<Criteria> crits = (List) node.getProperty(NodeConstants.Info.JOIN_CRITERIA);
            if (crits != null) {
                for (Criteria joinCriteria : crits) {
                    ElementCollectorVisitor.getElements(joinCriteria, requiredSymbols);
                }
            }
            break;
        case NodeConstants.Types.GROUP:
            List<Expression> groupCols = (List<Expression>) node.getProperty(NodeConstants.Info.GROUP_COLS);
            PlanNode accessParent = NodeEditor.findParent(node, NodeConstants.Types.ACCESS);
            PlanNode accessNode = null;
            if (accessParent == null) {
                // find the direct access node
                accessNode = NodeEditor.findNodePreOrder(node.getFirstChild(), NodeConstants.Types.ACCESS, NodeConstants.Types.SOURCE | NodeConstants.Types.JOIN | NodeConstants.Types.SET_OP | NodeConstants.Types.GROUP);
            }
            if (groupCols != null) {
                for (Expression expression : groupCols) {
                    if (!pushProjection(node, metadata, capFinder, requiredSymbols, accessParent, accessNode, expression)) {
                        ElementCollectorVisitor.getElements(expression, requiredSymbols);
                    }
                }
            }
            SymbolMap symbolMap = (SymbolMap) node.getProperty(NodeConstants.Info.SYMBOL_MAP);
            Set<ElementSymbol> usedAggregates = new HashSet<ElementSymbol>();
            // Take credit for creating any aggregates that are needed above
            for (Expression outputSymbol : outputCols) {
                if (!(outputSymbol instanceof ElementSymbol)) {
                    continue;
                }
                createdSymbols.add(outputSymbol);
                Expression ex = symbolMap.getMappedExpression((ElementSymbol) outputSymbol);
                if (ex instanceof AggregateSymbol) {
                    AggregateSymbol agg = (AggregateSymbol) ex;
                    Expression[] aggExprs = agg.getArgs();
                    for (Expression expression : aggExprs) {
                        if (!pushProjection(node, metadata, capFinder, requiredSymbols, accessParent, accessNode, expression)) {
                            ElementCollectorVisitor.getElements(expression, requiredSymbols);
                        }
                    }
                    OrderBy orderBy = agg.getOrderBy();
                    if (orderBy != null) {
                        ElementCollectorVisitor.getElements(orderBy, requiredSymbols);
                    }
                    Expression condition = agg.getCondition();
                    if (condition != null) {
                        ElementCollectorVisitor.getElements(condition, requiredSymbols);
                    }
                    usedAggregates.add((ElementSymbol) outputSymbol);
                }
            }
            // update the aggs in the symbolmap
            for (Map.Entry<ElementSymbol, Expression> entry : new ArrayList<Map.Entry<ElementSymbol, Expression>>(symbolMap.asMap().entrySet())) {
                if (entry.getValue() instanceof AggregateSymbol && !usedAggregates.contains(entry.getKey())) {
                    symbolMap.asUpdatableMap().remove(entry.getKey());
                }
            }
            if (requiredSymbols.isEmpty() && usedAggregates.isEmpty()) {
                node.setProperty(Info.IS_OPTIONAL, true);
            }
            break;
    }
    // Gather elements from correlated subquery references;
    for (SymbolMap refs : node.getAllReferences()) {
        for (Expression expr : refs.asMap().values()) {
            ElementCollectorVisitor.getElements(expr, requiredSymbols);
        }
    }
    // Add any columns to required that are in this node's output but were not created here
    for (Expression currentOutputSymbol : outputCols) {
        if (!createdSymbols.contains(currentOutputSymbol) && (finalRun || node.getType() != NodeConstants.Types.PROJECT || currentOutputSymbol instanceof ElementSymbol)) {
            requiredSymbols.add(currentOutputSymbol);
        }
    }
    // TODO: this should depend upon whether the expressions are deterministic
    if (node.getType() == NodeConstants.Types.PROJECT) {
        Set<Expression> expressions = new HashSet<Expression>();
        for (Iterator<Expression> iterator = requiredSymbols.iterator(); iterator.hasNext(); ) {
            Expression ses = iterator.next();
            if (!expressions.add(SymbolMap.getExpression(ses))) {
                iterator.remove();
            }
        }
    }
    return new ArrayList<Expression>(requiredSymbols);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) OrderBy(org.teiid.query.sql.lang.OrderBy) AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) ArrayList(java.util.ArrayList) SymbolMap(org.teiid.query.sql.util.SymbolMap) ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol) Criteria(org.teiid.query.sql.lang.Criteria) WindowFunction(org.teiid.query.sql.symbol.WindowFunction) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) AliasSymbol(org.teiid.query.sql.symbol.AliasSymbol) Expression(org.teiid.query.sql.symbol.Expression) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) SymbolMap(org.teiid.query.sql.util.SymbolMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with WindowFunction

use of org.teiid.query.sql.symbol.WindowFunction in project teiid by teiid.

the class PlanToProcessConverter method convertNode.

protected RelationalNode convertNode(PlanNode node) throws TeiidComponentException, TeiidProcessingException {
    RelationalNode processNode = null;
    switch(node.getType()) {
        case NodeConstants.Types.PROJECT:
            GroupSymbol intoGroup = (GroupSymbol) node.getProperty(NodeConstants.Info.INTO_GROUP);
            if (intoGroup != null) {
                try {
                    Insert insert = (Insert) node.getFirstChild().getProperty(Info.VIRTUAL_COMMAND);
                    List<ElementSymbol> allIntoElements = insert.getVariables();
                    Object groupID = intoGroup.getMetadataID();
                    Object modelID = metadata.getModelID(groupID);
                    String modelName = metadata.getFullName(modelID);
                    if (metadata.isVirtualGroup(groupID) && !metadata.isTemporaryTable(groupID)) {
                        InsertPlanExecutionNode ipen = new InsertPlanExecutionNode(getID(), metadata);
                        ProcessorPlan plan = (ProcessorPlan) node.getFirstChild().getProperty(Info.PROCESSOR_PLAN);
                        Assertion.isNotNull(plan);
                        ipen.setProcessorPlan(plan);
                        ipen.setReferences(insert.getValues());
                        processNode = ipen;
                    } else {
                        ProjectIntoNode pinode = new ProjectIntoNode(getID());
                        pinode.setIntoGroup(intoGroup);
                        pinode.setIntoElements(allIntoElements);
                        pinode.setModelName(modelName);
                        pinode.setConstraint((Criteria) node.getProperty(Info.CONSTRAINT));
                        pinode.setSourceHint((SourceHint) node.getProperty(Info.SOURCE_HINT));
                        if (node.hasBooleanProperty(Info.UPSERT)) {
                            pinode.setUpsert(true);
                        }
                        processNode = pinode;
                        SourceCapabilities caps = capFinder.findCapabilities(modelName);
                        if (caps.supportsCapability(Capability.INSERT_WITH_ITERATOR)) {
                            pinode.setMode(org.teiid.query.processor.relational.ProjectIntoNode.Mode.ITERATOR);
                        } else if (caps.supportsCapability(Capability.BATCHED_UPDATES)) {
                            pinode.setMode(org.teiid.query.processor.relational.ProjectIntoNode.Mode.BATCH);
                        } else {
                            pinode.setMode(org.teiid.query.processor.relational.ProjectIntoNode.Mode.SINGLE);
                        }
                        pinode.setTransactionSupport((TransactionSupport) caps.getSourceProperty(Capability.TRANSACTION_SUPPORT));
                    }
                } catch (QueryMetadataException e) {
                    throw new TeiidComponentException(QueryPlugin.Event.TEIID30247, e);
                }
            } else {
                List<Expression> symbols = (List) node.getProperty(NodeConstants.Info.PROJECT_COLS);
                ProjectNode pnode = new ProjectNode(getID());
                pnode.setSelectSymbols(symbols);
                processNode = pnode;
                if (node.hasBooleanProperty(Info.HAS_WINDOW_FUNCTIONS)) {
                    WindowFunctionProjectNode wfpn = new WindowFunctionProjectNode(getID());
                    // with partial projection the window function may already be pushed, we'll check for that here
                    ArrayList<Expression> filtered = new ArrayList<Expression>();
                    List<Expression> childSymbols = (List) node.getFirstChild().getProperty(NodeConstants.Info.OUTPUT_COLS);
                    for (Expression ex : symbols) {
                        ex = SymbolMap.getExpression(ex);
                        if (childSymbols.contains(ex)) {
                            continue;
                        }
                        filtered.add(ex);
                    }
                    Set<WindowFunction> windowFunctions = RuleAssignOutputElements.getWindowFunctions(filtered);
                    if (!windowFunctions.isEmpty()) {
                        // TODO: check for selecting all window functions
                        List<Expression> outputElements = new ArrayList<Expression>(windowFunctions);
                        // collect the other projected expressions
                        for (Expression singleElementSymbol : (List<Expression>) node.getFirstChild().getProperty(Info.OUTPUT_COLS)) {
                            outputElements.add(singleElementSymbol);
                        }
                        wfpn.setElements(outputElements);
                        wfpn.init();
                        pnode.addChild(wfpn);
                        for (WindowFunction wf : windowFunctions) {
                            validateAggregateFunctionEvaluation(wf.getFunction());
                        }
                    }
                }
            }
            break;
        case NodeConstants.Types.JOIN:
            JoinType jtype = (JoinType) node.getProperty(NodeConstants.Info.JOIN_TYPE);
            JoinStrategyType stype = (JoinStrategyType) node.getProperty(NodeConstants.Info.JOIN_STRATEGY);
            JoinNode jnode = new JoinNode(getID());
            jnode.setJoinType(jtype);
            jnode.setLeftDistinct(node.hasBooleanProperty(NodeConstants.Info.IS_LEFT_DISTINCT));
            jnode.setRightDistinct(node.hasBooleanProperty(NodeConstants.Info.IS_RIGHT_DISTINCT));
            List joinCrits = (List) node.getProperty(NodeConstants.Info.JOIN_CRITERIA);
            String depValueSource = (String) node.getProperty(NodeConstants.Info.DEPENDENT_VALUE_SOURCE);
            SortOption leftSort = (SortOption) node.getProperty(NodeConstants.Info.SORT_LEFT);
            if (stype == JoinStrategyType.MERGE || stype == JoinStrategyType.ENHANCED_SORT) {
                MergeJoinStrategy mjStrategy = null;
                if (stype.equals(JoinStrategyType.ENHANCED_SORT)) {
                    EnhancedSortMergeJoinStrategy esmjStrategy = new EnhancedSortMergeJoinStrategy(leftSort, (SortOption) node.getProperty(NodeConstants.Info.SORT_RIGHT));
                    esmjStrategy.setSemiDep(node.hasBooleanProperty(Info.IS_SEMI_DEP));
                    mjStrategy = esmjStrategy;
                } else {
                    mjStrategy = new MergeJoinStrategy(leftSort, (SortOption) node.getProperty(NodeConstants.Info.SORT_RIGHT), false);
                }
                jnode.setJoinStrategy(mjStrategy);
                List leftExpressions = (List) node.getProperty(NodeConstants.Info.LEFT_EXPRESSIONS);
                List rightExpressions = (List) node.getProperty(NodeConstants.Info.RIGHT_EXPRESSIONS);
                jnode.setJoinExpressions(leftExpressions, rightExpressions);
                joinCrits = (List) node.getProperty(NodeConstants.Info.NON_EQUI_JOIN_CRITERIA);
            } else if (stype == JoinStrategyType.NESTED_TABLE) {
                NestedTableJoinStrategy ntjStrategy = new NestedTableJoinStrategy();
                jnode.setJoinStrategy(ntjStrategy);
                SymbolMap references = (SymbolMap) node.getProperty(Info.RIGHT_NESTED_REFERENCES);
                ntjStrategy.setRightMap(references);
            } else {
                NestedLoopJoinStrategy nljStrategy = new NestedLoopJoinStrategy();
                jnode.setJoinStrategy(nljStrategy);
            }
            Criteria joinCrit = Criteria.combineCriteria(joinCrits);
            jnode.setJoinCriteria(joinCrit);
            processNode = jnode;
            jnode.setDependentValueSource(depValueSource);
            break;
        case NodeConstants.Types.ACCESS:
            ProcessorPlan plan = (ProcessorPlan) node.getProperty(NodeConstants.Info.PROCESSOR_PLAN);
            if (plan != null) {
                PlanExecutionNode peNode = null;
                Criteria crit = (Criteria) node.getProperty(NodeConstants.Info.PROCEDURE_CRITERIA);
                if (crit != null) {
                    List references = (List) node.getProperty(NodeConstants.Info.PROCEDURE_INPUTS);
                    List defaults = (List) node.getProperty(NodeConstants.Info.PROCEDURE_DEFAULTS);
                    peNode = new DependentProcedureExecutionNode(getID(), crit, references, defaults);
                } else {
                    peNode = new PlanExecutionNode(getID());
                }
                peNode.setProcessorPlan(plan);
                processNode = peNode;
            } else {
                AccessNode aNode = null;
                Command command = (Command) node.getProperty(NodeConstants.Info.ATOMIC_REQUEST);
                Object modelID = node.getProperty(NodeConstants.Info.MODEL_ID);
                if (modelID != null) {
                    String fullName = metadata.getFullName(modelID);
                    if (!capFinder.isValid(fullName)) {
                        // TODO: we ideally want to handle the partial resutls case here differently
                        // by adding a null node / and a source warning
                        // for now it's just as easy to say that the user needs to take steps to
                        // return static capabilities
                        SourceCapabilities caps = capFinder.findCapabilities(fullName);
                        Exception cause = null;
                        if (caps != null) {
                            cause = (Exception) caps.getSourceProperty(Capability.INVALID_EXCEPTION);
                        }
                        throw new QueryPlannerException(QueryPlugin.Event.TEIID30498, cause, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30498, fullName));
                    }
                }
                EvaluatableVisitor ev = null;
                if (node.hasBooleanProperty(NodeConstants.Info.IS_DEPENDENT_SET)) {
                    if (command instanceof StoredProcedure) {
                        List references = (List) node.getProperty(NodeConstants.Info.PROCEDURE_INPUTS);
                        List defaults = (List) node.getProperty(NodeConstants.Info.PROCEDURE_DEFAULTS);
                        Criteria crit = (Criteria) node.getProperty(NodeConstants.Info.PROCEDURE_CRITERIA);
                        DependentProcedureAccessNode depAccessNode = new DependentProcedureAccessNode(getID(), crit, references, defaults);
                        processNode = depAccessNode;
                        aNode = depAccessNode;
                    } else {
                        // create dependent access node
                        DependentAccessNode depAccessNode = new DependentAccessNode(getID());
                        if (modelID != null) {
                            depAccessNode.setPushdown(CapabilitiesUtil.supports(Capability.DEPENDENT_JOIN, modelID, metadata, capFinder));
                            depAccessNode.setMaxSetSize(CapabilitiesUtil.getMaxInCriteriaSize(modelID, metadata, capFinder));
                            depAccessNode.setMaxPredicates(CapabilitiesUtil.getMaxDependentPredicates(modelID, metadata, capFinder));
                            depAccessNode.setUseBindings(CapabilitiesUtil.supports(Capability.DEPENDENT_JOIN_BINDINGS, modelID, metadata, capFinder));
                            // TODO: allow the translator to drive this property
                            // simplistic check of whether this query is complex to re-execute
                            Query query = (Query) command;
                            if (query.getGroupBy() != null || query.getFrom().getClauses().size() > 1 || !(query.getFrom().getClauses().get(0) instanceof UnaryFromClause) || query.getWith() != null) {
                                depAccessNode.setComplexQuery(true);
                            } else {
                                // check to see if there in an index on at least one of the dependent sets
                                Set<GroupSymbol> groups = new HashSet<GroupSymbol>(query.getFrom().getGroups());
                                boolean found = false;
                                for (Criteria crit : Criteria.separateCriteriaByAnd(query.getCriteria())) {
                                    if (crit instanceof DependentSetCriteria) {
                                        DependentSetCriteria dsc = (DependentSetCriteria) crit;
                                        if (NewCalculateCostUtil.getKeyUsed(ElementCollectorVisitor.getElements(dsc.getExpression(), true), groups, metadata, null) != null) {
                                            found = true;
                                            break;
                                        }
                                    }
                                }
                                if (!found) {
                                    depAccessNode.setComplexQuery(true);
                                }
                            }
                        }
                        processNode = depAccessNode;
                        aNode = depAccessNode;
                    }
                    aNode.setShouldEvaluateExpressions(true);
                } else {
                    // create access node
                    aNode = new AccessNode(getID());
                    processNode = aNode;
                }
                // -- special handling for system tables. currently they cannot perform projection
                try {
                    if (command instanceof Query) {
                        processNode = correctProjectionInternalTables(node, aNode);
                    }
                } catch (QueryMetadataException err) {
                    throw new TeiidComponentException(QueryPlugin.Event.TEIID30248, err);
                }
                setRoutingName(aNode, node, command);
                boolean shouldEval = false;
                if (command instanceof Insert) {
                    Insert insert = (Insert) command;
                    if (insert.getQueryExpression() != null) {
                        insert.setQueryExpression((QueryCommand) aliasCommand(aNode, insert.getQueryExpression(), modelID));
                    } else {
                        for (int i = 0; i < insert.getValues().size(); i++) {
                            Expression ex = (Expression) insert.getValues().get(i);
                            if (!CriteriaCapabilityValidatorVisitor.canPushLanguageObject(ex, modelID, metadata, capFinder, analysisRecord)) {
                                // replace with an expression symbol to let the rewriter know that it should be replaced
                                insert.getValues().set(i, new ExpressionSymbol("x", ex));
                                shouldEval = true;
                            }
                        }
                    }
                } else if (command instanceof QueryCommand) {
                    command = aliasCommand(aNode, command, modelID);
                }
                ev = EvaluatableVisitor.needsEvaluationVisitor(modelID, metadata, capFinder);
                if (!shouldEval && modelID != null) {
                    // do a capabilities sensitive check for needs eval
                    String modelName = metadata.getFullName(modelID);
                    SourceCapabilities caps = capFinder.findCapabilities(modelName);
                    final CriteriaCapabilityValidatorVisitor capVisitor = new CriteriaCapabilityValidatorVisitor(modelID, metadata, capFinder, caps);
                    capVisitor.setCheckEvaluation(false);
                    DeepPreOrderNavigator nav = new DeepPreOrderNavigator(ev) {

                        protected void visitNode(org.teiid.query.sql.LanguageObject obj) {
                            if (capVisitor.isValid() && obj instanceof Expression) {
                                obj.acceptVisitor(capVisitor);
                            }
                            super.visitNode(obj);
                        }
                    };
                    command.acceptVisitor(nav);
                    if (!capVisitor.isValid()) {
                        // there's a non-supported construct pushed, we should eval
                        ev.evaluationNotPossible(EvaluationLevel.PROCESSING);
                    }
                } else {
                    DeepPreOrderNavigator.doVisit(command, ev);
                }
                aNode.setShouldEvaluateExpressions(ev.requiresEvaluation(EvaluationLevel.PROCESSING) || shouldEval);
                aNode.setCommand(command);
                if (modelID != null) {
                    String fullName = metadata.getFullName(modelID);
                    SourceCapabilities caps = capFinder.findCapabilities(fullName);
                    aNode.setTransactionSupport((TransactionSupport) caps.getSourceProperty(Capability.TRANSACTION_SUPPORT));
                }
                Map<GroupSymbol, PlanNode> subPlans = (Map<GroupSymbol, PlanNode>) node.getProperty(Info.SUB_PLANS);
                // it makes more sense to allow the multisource affect to be elevated above just access nodes
                if (aNode.getModelId() != null && metadata.isMultiSource(aNode.getModelId())) {
                    VDBMetaData vdb = context.getVdb();
                    // forces a rewrite
                    aNode.setShouldEvaluateExpressions(true);
                    aNode.setElements((List) node.getProperty(NodeConstants.Info.OUTPUT_COLS));
                    if (node.hasBooleanProperty(Info.IS_MULTI_SOURCE)) {
                        Expression ex = rewriteMultiSourceCommand(aNode.getCommand());
                        aNode.setConnectorBindingExpression(ex);
                        aNode.setMultiSource(true);
                    } else {
                        String sourceName = (String) node.getProperty(Info.SOURCE_NAME);
                        aNode.setConnectorBindingExpression(new Constant(sourceName));
                    }
                } else if (subPlans == null) {
                    if (!aNode.isShouldEvaluate()) {
                        aNode.minimizeProject(command);
                    }
                    // check if valid to share this with other nodes
                    if (ev != null && ev.getDeterminismLevel().compareTo(Determinism.INSTRUCTION_DETERMINISTIC) >= 0 && command.areResultsCachable()) {
                        checkForSharedSourceCommand(aNode, node);
                    }
                }
                if (subPlans != null) {
                    QueryCommand qc = (QueryCommand) command;
                    if (qc.getWith() == null) {
                        qc.setWith(new ArrayList<WithQueryCommand>(subPlans.size()));
                    }
                    Map<GroupSymbol, RelationalPlan> plans = new LinkedHashMap<GroupSymbol, RelationalPlan>();
                    for (Map.Entry<GroupSymbol, PlanNode> entry : subPlans.entrySet()) {
                        RelationalPlan subPlan = convert(entry.getValue());
                        List<ElementSymbol> elems = ResolverUtil.resolveElementsInGroup(entry.getKey(), metadata);
                        subPlan.setOutputElements(elems);
                        plans.put(entry.getKey(), subPlan);
                        WithQueryCommand withQueryCommand = new WithQueryCommand(entry.getKey(), elems, null);
                        qc.getWith().add(withQueryCommand);
                    }
                    aNode.setSubPlans(plans);
                }
            }
            break;
        case NodeConstants.Types.SELECT:
            Criteria crit = (Criteria) node.getProperty(NodeConstants.Info.SELECT_CRITERIA);
            if (!node.hasCollectionProperty(Info.OUTPUT_COLS)) {
                // the late optimization to create a dependent join from a subquery introduces
                // criteria that have no output elements set
                // TODO that should be cleaner, but the logic currently expects to be run after join implementation
                // and rerunning assign output elements seems excessive
                node.setProperty(Info.OUTPUT_COLS, node.getFirstChild().getProperty(Info.OUTPUT_COLS));
            }
            SelectNode selnode = new SelectNode(getID());
            selnode.setCriteria(crit);
            // in case the parent was a source
            selnode.setProjectedExpressions((List<Expression>) node.getProperty(NodeConstants.Info.PROJECT_COLS));
            processNode = selnode;
            break;
        case NodeConstants.Types.SORT:
        case NodeConstants.Types.DUP_REMOVE:
            if (node.getType() == NodeConstants.Types.DUP_REMOVE) {
                processNode = new DupRemoveNode(getID());
            } else {
                SortNode sortNode = new SortNode(getID());
                OrderBy orderBy = (OrderBy) node.getProperty(NodeConstants.Info.SORT_ORDER);
                if (orderBy != null) {
                    sortNode.setSortElements(orderBy.getOrderByItems());
                }
                if (node.hasBooleanProperty(NodeConstants.Info.IS_DUP_REMOVAL)) {
                    sortNode.setMode(Mode.DUP_REMOVE_SORT);
                }
                processNode = sortNode;
            }
            break;
        case NodeConstants.Types.GROUP:
            GroupingNode gnode = new GroupingNode(getID());
            gnode.setRollup(node.hasBooleanProperty(Info.ROLLUP));
            SymbolMap groupingMap = (SymbolMap) node.getProperty(NodeConstants.Info.SYMBOL_MAP);
            gnode.setOutputMapping(groupingMap);
            gnode.setRemoveDuplicates(node.hasBooleanProperty(NodeConstants.Info.IS_DUP_REMOVAL));
            List<Expression> gCols = (List) node.getProperty(NodeConstants.Info.GROUP_COLS);
            OrderBy orderBy = (OrderBy) node.getProperty(Info.SORT_ORDER);
            if (orderBy == null) {
                if (gCols != null) {
                    LinkedHashSet<Expression> exprs = new LinkedHashSet<Expression>();
                    for (Expression ex : gCols) {
                        exprs.add(SymbolMap.getExpression(ex));
                    }
                    orderBy = new OrderBy(RuleChooseJoinStrategy.createExpressionSymbols(new ArrayList<Expression>(exprs)));
                }
            } else {
                HashSet<Expression> seen = new HashSet<Expression>();
                for (int i = 0; i < gCols.size(); i++) {
                    if (i < orderBy.getOrderByItems().size()) {
                        OrderByItem orderByItem = orderBy.getOrderByItems().get(i);
                        Expression ex = SymbolMap.getExpression(orderByItem.getSymbol());
                        if (!seen.add(ex)) {
                            continue;
                        }
                        if (ex instanceof ElementSymbol) {
                            ex = groupingMap.getMappedExpression((ElementSymbol) ex);
                            // $NON-NLS-1$
                            orderByItem.setSymbol(new ExpressionSymbol("expr", ex));
                        }
                    } else {
                        // $NON-NLS-1$
                        orderBy.addVariable(new ExpressionSymbol("expr", gCols.get(i)), OrderBy.ASC);
                    }
                }
            }
            if (orderBy != null) {
                gnode.setOrderBy(orderBy.getOrderByItems());
            }
            for (Expression ex : groupingMap != null ? groupingMap.getValues() : (List<Expression>) node.getFirstChild().getProperty(NodeConstants.Info.PROJECT_COLS)) {
                if (ex instanceof AggregateSymbol) {
                    validateAggregateFunctionEvaluation((AggregateSymbol) ex);
                }
            }
            processNode = gnode;
            break;
        case NodeConstants.Types.SOURCE:
            Object source = node.getProperty(NodeConstants.Info.TABLE_FUNCTION);
            if (source instanceof XMLTable) {
                XMLTable xt = (XMLTable) source;
                XMLTableNode xtn = new XMLTableNode(getID());
                // we handle the projection filtering once here rather than repeating the
                // path analysis on a per plan basis
                updateGroupName(node, xt);
                Map<Expression, Integer> elementMap = RelationalNode.createLookupMap(xt.getProjectedSymbols());
                List cols = (List) node.getProperty(NodeConstants.Info.OUTPUT_COLS);
                int[] projectionIndexes = RelationalNode.getProjectionIndexes(elementMap, cols);
                ArrayList<XMLColumn> filteredColumns = new ArrayList<XMLColumn>(projectionIndexes.length);
                for (int col : projectionIndexes) {
                    filteredColumns.add(xt.getColumns().get(col));
                }
                xt.getXQueryExpression().useDocumentProjection(filteredColumns, analysisRecord);
                xtn.setProjectedColumns(filteredColumns);
                xtn.setTable(xt);
                processNode = xtn;
                break;
            }
            if (source instanceof ObjectTable) {
                ObjectTable ot = (ObjectTable) source;
                ObjectTableNode otn = new ObjectTableNode(getID());
                // we handle the projection filtering once here rather than repeating the
                // path analysis on a per plan basis
                updateGroupName(node, ot);
                Map<Expression, Integer> elementMap = RelationalNode.createLookupMap(ot.getProjectedSymbols());
                List<Expression> cols = (List<Expression>) node.getProperty(NodeConstants.Info.OUTPUT_COLS);
                int[] projectionIndexes = RelationalNode.getProjectionIndexes(elementMap, cols);
                ArrayList<ObjectColumn> filteredColumns = new ArrayList<ObjectColumn>(projectionIndexes.length);
                for (int col : projectionIndexes) {
                    filteredColumns.add(ot.getColumns().get(col));
                }
                otn.setProjectedColumns(filteredColumns);
                otn.setTable(ot);
                processNode = otn;
                break;
            }
            if (source instanceof TextTable) {
                TextTableNode ttn = new TextTableNode(getID());
                TextTable tt = (TextTable) source;
                updateGroupName(node, tt);
                ttn.setTable(tt);
                processNode = ttn;
                break;
            }
            if (source instanceof ArrayTable) {
                ArrayTableNode atn = new ArrayTableNode(getID());
                ArrayTable at = (ArrayTable) source;
                updateGroupName(node, at);
                atn.setTable(at);
                processNode = atn;
                break;
            }
            SymbolMap symbolMap = (SymbolMap) node.getProperty(NodeConstants.Info.SYMBOL_MAP);
            if (symbolMap != null) {
                PlanNode child = node.getLastChild();
                if (child.getType() == NodeConstants.Types.PROJECT || child.getType() == NodeConstants.Types.SELECT) {
                    // update the project cols based upon the original output
                    child.setProperty(NodeConstants.Info.PROJECT_COLS, child.getProperty(NodeConstants.Info.OUTPUT_COLS));
                }
                if (child.getType() != NodeConstants.Types.SET_OP || child.getProperty(Info.SET_OPERATION) == Operation.UNION) {
                    child.setProperty(NodeConstants.Info.OUTPUT_COLS, node.getProperty(NodeConstants.Info.OUTPUT_COLS));
                } else {
                    // else we cannot directly update the child properties as the child will get converted to a join
                    // create a projection instead for initialization purposes, but won't impact performance
                    ProjectNode pNode = new ProjectNode(getID());
                    pNode.setSelectSymbols((List<? extends Expression>) child.getProperty(NodeConstants.Info.OUTPUT_COLS));
                    return prepareToAdd(node, pNode);
                }
            }
            return null;
        case NodeConstants.Types.SET_OP:
            Operation setOp = (Operation) node.getProperty(NodeConstants.Info.SET_OPERATION);
            boolean useAll = ((Boolean) node.getProperty(NodeConstants.Info.USE_ALL)).booleanValue();
            if (setOp == Operation.UNION) {
                RelationalNode unionAllNode = new UnionAllNode(getID());
                if (useAll) {
                    processNode = unionAllNode;
                } else {
                    boolean onlyDupRemoval = node.hasBooleanProperty(NodeConstants.Info.IS_DUP_REMOVAL);
                    if (onlyDupRemoval) {
                        processNode = new DupRemoveNode(getID());
                    } else {
                        SortNode sNode = new SortNode(getID());
                        sNode.setMode(Mode.DUP_REMOVE_SORT);
                        processNode = sNode;
                    }
                    unionAllNode.setElements((List) node.getProperty(NodeConstants.Info.OUTPUT_COLS));
                    processNode.addChild(unionAllNode);
                }
            } else {
                JoinNode joinAsSet = new JoinNode(getID());
                joinAsSet.setJoinStrategy(new MergeJoinStrategy(SortOption.SORT_DISTINCT, SortOption.SORT_DISTINCT, true));
                // If we push these sorts, we will have to enforce null order, since nulls are equal here
                List leftExpressions = (List) node.getFirstChild().getProperty(NodeConstants.Info.OUTPUT_COLS);
                List rightExpressions = (List) node.getLastChild().getProperty(NodeConstants.Info.OUTPUT_COLS);
                joinAsSet.setJoinType(setOp == Operation.EXCEPT ? JoinType.JOIN_ANTI_SEMI : JoinType.JOIN_SEMI);
                joinAsSet.setJoinExpressions(leftExpressions, rightExpressions);
                processNode = joinAsSet;
            }
            break;
        case NodeConstants.Types.TUPLE_LIMIT:
            Expression rowLimit = (Expression) node.getProperty(NodeConstants.Info.MAX_TUPLE_LIMIT);
            Expression offset = (Expression) node.getProperty(NodeConstants.Info.OFFSET_TUPLE_COUNT);
            LimitNode ln = new LimitNode(getID(), rowLimit, offset);
            ln.setImplicit(node.hasBooleanProperty(Info.IS_IMPLICIT_LIMIT));
            processNode = ln;
            break;
        case NodeConstants.Types.NULL:
            processNode = new NullNode(getID());
            break;
        default:
            throw new QueryPlannerException(QueryPlugin.Event.TEIID30250, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30250, NodeConstants.getNodeTypeString(node.getType())));
    }
    if (processNode != null) {
        processNode = prepareToAdd(node, processNode);
    }
    return processNode;
}
Also used : ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol) DeepPreOrderNavigator(org.teiid.query.sql.navigator.DeepPreOrderNavigator) XMLColumn(org.teiid.query.sql.lang.XMLTable.XMLColumn) VDBMetaData(org.teiid.adminapi.impl.VDBMetaData) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) TeiidComponentException(org.teiid.core.TeiidComponentException) QueryPlannerException(org.teiid.api.exception.query.QueryPlannerException) SymbolMap(org.teiid.query.sql.util.SymbolMap) ObjectColumn(org.teiid.query.sql.lang.ObjectTable.ObjectColumn) CriteriaCapabilityValidatorVisitor(org.teiid.query.optimizer.relational.rules.CriteriaCapabilityValidatorVisitor) JoinStrategyType(org.teiid.query.processor.relational.JoinNode.JoinStrategyType) EvaluatableVisitor(org.teiid.query.sql.visitor.EvaluatableVisitor) Expression(org.teiid.query.sql.symbol.Expression) SourceCapabilities(org.teiid.query.optimizer.capabilities.SourceCapabilities) WindowFunction(org.teiid.query.sql.symbol.WindowFunction) SortOption(org.teiid.query.processor.relational.MergeJoinStrategy.SortOption) SymbolMap(org.teiid.query.sql.util.SymbolMap) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) AggregateSymbol(org.teiid.query.sql.symbol.AggregateSymbol) Constant(org.teiid.query.sql.symbol.Constant) Operation(org.teiid.query.sql.lang.SetQuery.Operation) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) TeiidComponentException(org.teiid.core.TeiidComponentException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) QueryPlannerException(org.teiid.api.exception.query.QueryPlannerException) SpecificHint(org.teiid.query.sql.lang.SourceHint.SpecificHint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 4 with WindowFunction

use of org.teiid.query.sql.symbol.WindowFunction in project teiid by teiid.

the class RulePushSelectCriteria method createConvertedSelectNode.

private PlanNode createConvertedSelectNode(PlanNode critNode, GroupSymbol sourceGroup, PlanNode projectNode, SymbolMap symbolMap, QueryMetadataInterface metadata) throws QueryPlannerException {
    // If projectNode has children, then it is from a SELECT without a FROM and the criteria should not be pushed
    if (projectNode.getChildCount() == 0) {
        return null;
    }
    Criteria crit = (Criteria) critNode.getProperty(NodeConstants.Info.SELECT_CRITERIA);
    Collection<ElementSymbol> cols = ElementCollectorVisitor.getElements(crit, true);
    if (projectNode.hasBooleanProperty(Info.HAS_WINDOW_FUNCTIONS)) {
        // we can push iff the predicate is against partitioning columns in all projected window functions
        Set<WindowFunction> windowFunctions = RuleAssignOutputElements.getWindowFunctions((List<Expression>) projectNode.getProperty(Info.PROJECT_COLS));
        for (WindowFunction windowFunction : windowFunctions) {
            WindowSpecification spec = windowFunction.getWindowSpecification();
            if (spec.getPartition() == null) {
                return null;
            }
            for (ElementSymbol col : cols) {
                if (!spec.getPartition().contains(symbolMap.getMappedExpression(col))) {
                    return null;
                }
            }
        }
    }
    Boolean conversionResult = checkConversion(symbolMap, cols);
    if (conversionResult == Boolean.FALSE) {
        // not convertable
        return null;
    }
    if (!critNode.getSubqueryContainers().isEmpty() && checkConversion(symbolMap, critNode.getCorrelatedReferenceElements()) != null) {
        // not convertable, or has an aggregate for a correlated reference
        return null;
    }
    PlanNode copyNode = copyNode(critNode);
    if (conversionResult == Boolean.TRUE) {
        copyNode.setProperty(NodeConstants.Info.IS_HAVING, Boolean.TRUE);
    }
    FrameUtil.convertNode(copyNode, sourceGroup, null, symbolMap.asMap(), metadata, true);
    // any proc relational criteria that is not input criteria should stay above the source
    if (sourceGroup.isProcedure() && !copyNode.getGroups().isEmpty()) {
        if (this.createdNodes != null) {
            this.createdNodes.remove(this.createdNodes.size() - 1);
        }
        return null;
    }
    return copyNode;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) WindowFunction(org.teiid.query.sql.symbol.WindowFunction) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) Expression(org.teiid.query.sql.symbol.Expression) WindowSpecification(org.teiid.query.sql.symbol.WindowSpecification) CompoundCriteria(org.teiid.query.sql.lang.CompoundCriteria) DependentSetCriteria(org.teiid.query.sql.lang.DependentSetCriteria) Criteria(org.teiid.query.sql.lang.Criteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria)

Example 5 with WindowFunction

use of org.teiid.query.sql.symbol.WindowFunction in project teiid by teiid.

the class RuleRaiseAccess method raiseAccessNode.

/**
 * @return null if nothing changed, and a new plan root if something changed
 */
static PlanNode raiseAccessNode(PlanNode rootNode, PlanNode accessNode, QueryMetadataInterface metadata, CapabilitiesFinder capFinder, boolean afterJoinPlanning, AnalysisRecord record, CommandContext context) throws QueryPlannerException, QueryMetadataException, TeiidComponentException {
    PlanNode parentNode = accessNode.getParent();
    if (parentNode == null) {
        // Nothing to raise over
        return null;
    }
    Object modelID = getModelIDFromAccess(accessNode, metadata);
    if (modelID == null) {
        return null;
    }
    switch(parentNode.getType()) {
        case NodeConstants.Types.JOIN:
            {
                modelID = canRaiseOverJoin(modelID, parentNode, metadata, capFinder, afterJoinPlanning, record, context);
                if (modelID != null && checkConformedSubqueries(accessNode, parentNode, true)) {
                    raiseAccessOverJoin(parentNode, accessNode, modelID, capFinder, metadata, true);
                    return rootNode;
                }
                return null;
            }
        case NodeConstants.Types.PROJECT:
            {
                if (CapabilitiesUtil.supports(Capability.NO_PROJECTION, modelID, metadata, capFinder)) {
                    return null;
                }
                // Check that the PROJECT contains only functions that can be pushed
                List<Expression> projectCols = (List) parentNode.getProperty(NodeConstants.Info.PROJECT_COLS);
                for (int i = 0; i < projectCols.size(); i++) {
                    Expression symbol = projectCols.get(i);
                    if (!canPushSymbol(symbol, true, modelID, metadata, capFinder, record)) {
                        return null;
                    }
                }
                /*
                 * TODO: this creates an extraneous project node in many circumstances.
                 * However we don't actually support project in this case, so allowing it to be pushed
                 * causes problems with stored procedures and the assumptions made for proc/relational
                 * planning. 
                 */
                if (FrameUtil.isProcedure(parentNode)) {
                    return null;
                }
                PlanNode orderBy = NodeEditor.findParent(parentNode, NodeConstants.Types.SORT, NodeConstants.Types.SOURCE);
                if (orderBy != null && orderBy.hasBooleanProperty(Info.UNRELATED_SORT) && !canRaiseOverSort(accessNode, metadata, capFinder, orderBy, record, false, context)) {
                    // this project node logically has the responsibility of creating the sort keys
                    return null;
                }
                if (accessNode.hasBooleanProperty(Info.IS_MULTI_SOURCE)) {
                    List<WindowFunction> windowFunctions = new ArrayList<WindowFunction>(2);
                    for (Expression ex : projectCols) {
                        AggregateSymbolCollectorVisitor.getAggregates(ex, null, null, null, windowFunctions, null);
                        if (!windowFunctions.isEmpty()) {
                            return null;
                        }
                    }
                }
                return performRaise(rootNode, accessNode, parentNode);
            }
        case NodeConstants.Types.DUP_REMOVE:
            {
                // If model supports the support constant parameter, then move access node
                if (!CapabilitiesUtil.supportsSelectDistinct(modelID, metadata, capFinder)) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    parentNode.recordDebugAnnotation("distinct is not supported by source", modelID, "cannot push dupremove", record, metadata);
                    return null;
                }
                if (!supportsDistinct(metadata, parentNode, accessNode.hasBooleanProperty(Info.IS_MULTI_SOURCE))) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    parentNode.recordDebugAnnotation("not all columns are comparable at the source", modelID, "cannot push dupremove", record, metadata);
                    return null;
                }
                return performRaise(rootNode, accessNode, parentNode);
            }
        case NodeConstants.Types.SORT:
            {
                if (canRaiseOverSort(accessNode, metadata, capFinder, parentNode, record, false, context)) {
                    return performRaise(rootNode, accessNode, parentNode);
                }
                return null;
            }
        case NodeConstants.Types.GROUP:
            {
                Set<AggregateSymbol> aggregates = RulePushAggregates.collectAggregates(parentNode);
                if (canRaiseOverGroupBy(parentNode, accessNode, aggregates, metadata, capFinder, record, true)) {
                    accessNode.getGroups().clear();
                    accessNode.getGroups().addAll(parentNode.getGroups());
                    return performRaise(rootNode, accessNode, parentNode);
                }
                return null;
            }
        case NodeConstants.Types.SET_OP:
            if (!canRaiseOverSetQuery(parentNode, metadata, capFinder)) {
                return null;
            }
            String sourceName = null;
            boolean multiSource = false;
            for (PlanNode node : new ArrayList<PlanNode>(parentNode.getChildren())) {
                multiSource |= accessNode.hasBooleanProperty(Info.IS_MULTI_SOURCE);
                if (sourceName == null) {
                    sourceName = (String) accessNode.getProperty(Info.SOURCE_NAME);
                }
                if (node == accessNode) {
                    continue;
                }
                combineSourceHints(accessNode, node);
                combineConformedSources(accessNode, node);
                NodeEditor.removeChildNode(parentNode, node);
            }
            accessNode.getGroups().clear();
            if (multiSource) {
                accessNode.setProperty(Info.IS_MULTI_SOURCE, true);
            } else if (sourceName != null) {
                accessNode.setProperty(Info.SOURCE_NAME, sourceName);
            }
            return performRaise(rootNode, accessNode, parentNode);
        case NodeConstants.Types.SELECT:
            {
                if (parentNode.hasBooleanProperty(NodeConstants.Info.IS_DEPENDENT_SET)) {
                    return null;
                }
                if (canRaiseOverSelect(accessNode, metadata, capFinder, parentNode, record)) {
                    RulePushSelectCriteria.satisfyConditions(parentNode, accessNode, metadata);
                    return performRaise(rootNode, accessNode, parentNode);
                }
                // determine if we should push the select back up
                if (parentNode.getParent() == null) {
                    return null;
                }
                PlanNode selectRoot = parentNode;
                while (selectRoot.getParent() != null && selectRoot.getParent().getType() == NodeConstants.Types.SELECT) {
                    selectRoot = selectRoot.getParent();
                }
                if (selectRoot.getParent() == null || (selectRoot.getParent().getType() & (NodeConstants.Types.PROJECT | NodeConstants.Types.GROUP)) == selectRoot.getParent().getType()) {
                    return null;
                }
                PlanNode grandParent = selectRoot.getParent();
                boolean isLeft = false;
                isLeft = grandParent.getFirstChild() == selectRoot;
                if (grandParent.getType() == NodeConstants.Types.JOIN) {
                    JoinType jt = (JoinType) grandParent.getProperty(NodeConstants.Info.JOIN_TYPE);
                    if (jt == JoinType.JOIN_FULL_OUTER || (jt == JoinType.JOIN_LEFT_OUTER && !isLeft)) {
                        return null;
                    }
                }
                grandParent.removeChild(selectRoot);
                if (isLeft) {
                    grandParent.addFirstChild(accessNode);
                } else {
                    grandParent.addLastChild(accessNode);
                }
                PlanNode newParent = grandParent.getParent();
                // TODO: use costing or heuristics instead of always raising
                PlanNode newRoot = raiseAccessNode(rootNode, accessNode, metadata, capFinder, afterJoinPlanning, record, context);
                if (newRoot == null) {
                    // return the tree to its original state
                    parentNode.addFirstChild(accessNode);
                    if (isLeft) {
                        grandParent.addFirstChild(selectRoot);
                    } else {
                        grandParent.addLastChild(selectRoot);
                    }
                } else {
                    // attach the select nodes above the access node
                    accessNode = grandParent.getParent();
                    if (newParent != null) {
                        isLeft = newParent.getFirstChild() == accessNode;
                        if (isLeft) {
                            newParent.addFirstChild(selectRoot);
                        } else {
                            newParent.addLastChild(selectRoot);
                        }
                    } else {
                        newRoot = selectRoot;
                    }
                    parentNode.addFirstChild(accessNode);
                    return newRoot;
                }
                return null;
            }
        case NodeConstants.Types.SOURCE:
            {
                // if a source has access patterns that are unsatisfied, then the raise cannot occur
                if (parentNode.hasCollectionProperty(NodeConstants.Info.ACCESS_PATTERNS)) {
                    return null;
                }
                SymbolMap references = (SymbolMap) parentNode.getProperty(NodeConstants.Info.CORRELATED_REFERENCES);
                if (references != null) {
                    return null;
                }
                // raise only if there is no intervening project into
                PlanNode parentProject = NodeEditor.findParent(parentNode, NodeConstants.Types.PROJECT);
                GroupSymbol intoGroup = (GroupSymbol) parentProject.getProperty(NodeConstants.Info.INTO_GROUP);
                if (intoGroup != null && parentProject.getParent() == null) {
                    if (!parentProject.hasProperty(Info.CONSTRAINT) && CapabilitiesUtil.supports(Capability.INSERT_WITH_QUERYEXPRESSION, modelID, metadata, capFinder) && CapabilitiesUtil.isSameConnector(modelID, metadata.getModelID(intoGroup.getMetadataID()), metadata, capFinder)) {
                        rootNode = performRaise(rootNode, accessNode, parentNode);
                        return performRaise(rootNode, accessNode, parentProject);
                    }
                    return null;
                } else if (!CapabilitiesUtil.supportsInlineView(modelID, metadata, capFinder)) {
                    return null;
                }
                // is there another query that will be used with this source
                if (FrameUtil.getNonQueryCommand(accessNode) != null || FrameUtil.getNestedPlan(accessNode) != null) {
                    return null;
                }
                // switch to inline view and change the group on the access to that of the source
                parentNode.setProperty(NodeConstants.Info.INLINE_VIEW, Boolean.TRUE);
                accessNode.getGroups().clear();
                accessNode.addGroups(parentNode.getGroups());
                RulePlaceAccess.copyProperties(parentNode, accessNode);
                return performRaise(rootNode, accessNode, parentNode);
            }
        case NodeConstants.Types.TUPLE_LIMIT:
            {
                return RulePushLimit.raiseAccessOverLimit(rootNode, accessNode, metadata, capFinder, parentNode, record);
            }
        default:
            {
                return null;
            }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) SymbolMap(org.teiid.query.sql.util.SymbolMap) WindowFunction(org.teiid.query.sql.symbol.WindowFunction) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) Expression(org.teiid.query.sql.symbol.Expression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Aggregations

Expression (org.teiid.query.sql.symbol.Expression)7 WindowFunction (org.teiid.query.sql.symbol.WindowFunction)7 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)5 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)4 ArrayList (java.util.ArrayList)3 Criteria (org.teiid.query.sql.lang.Criteria)3 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)3 SymbolMap (org.teiid.query.sql.util.SymbolMap)3 HashSet (java.util.HashSet)2 List (java.util.List)2 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)2 TeiidProcessingException (org.teiid.core.TeiidProcessingException)2 AggregateSymbol (org.teiid.query.sql.symbol.AggregateSymbol)2 ExpressionSymbol (org.teiid.query.sql.symbol.ExpressionSymbol)2 WindowSpecification (org.teiid.query.sql.symbol.WindowSpecification)2 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1