Search in sources :

Example 21 with CompareCriteria

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

the class RulePushAggregates method addEmptyFilter.

private void addEmptyFilter(Collection<AggregateSymbol> aggregates, PlanNode stageGroup, QueryMetadataInterface metadata, CapabilitiesFinder capFinder, Object modelId) throws QueryMetadataException, TeiidComponentException {
    PlanNode selectNode = NodeFactory.getNewNode(NodeConstants.Types.SELECT);
    AggregateSymbol count = new AggregateSymbol(NonReserved.COUNT, false, null);
    // consider the count aggregate for the push down call below
    aggregates.add(count);
    Criteria crit = new CompareCriteria(count, CompareCriteria.GT, new Constant(new Integer(0)));
    selectNode.setProperty(NodeConstants.Info.SELECT_CRITERIA, crit);
    selectNode.setProperty(NodeConstants.Info.IS_HAVING, Boolean.TRUE);
    stageGroup.addAsParent(selectNode);
}
Also used : PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria) Criteria(org.teiid.query.sql.lang.Criteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria)

Example 22 with CompareCriteria

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

the class JoinRegion method initializeJoinInformation.

/**
 *  Initializes information on the joinRegion about dependency information, etc.
 *
 *  TODO: assumptions are made here about how dependent criteria must look that are a little restrictive
 */
public void initializeJoinInformation() {
    critieriaToSourceMap = new HashMap<PlanNode, Set<PlanNode>>();
    LinkedList<PlanNode> crits = new LinkedList<PlanNode>(criteriaNodes);
    crits.addAll(dependentCritieraNodes);
    LinkedHashMap<PlanNode, PlanNode> source = new LinkedHashMap<PlanNode, PlanNode>(joinSourceNodes);
    source.putAll(dependentJoinSourceNodes);
    for (PlanNode critNode : crits) {
        for (GroupSymbol group : critNode.getGroups()) {
            for (PlanNode node : source.keySet()) {
                if (node.getGroups().contains(group)) {
                    Set<PlanNode> sources = critieriaToSourceMap.get(critNode);
                    if (sources == null) {
                        sources = new HashSet<PlanNode>();
                        critieriaToSourceMap.put(critNode, sources);
                    }
                    sources.add(node);
                    break;
                }
            }
        }
    }
    if (unsatisfiedAccessPatterns.isEmpty()) {
        return;
    }
    Map<GroupSymbol, PlanNode> dependentGroupToSourceMap = new HashMap<GroupSymbol, PlanNode>();
    for (PlanNode node : dependentJoinSourceNodes.keySet()) {
        for (GroupSymbol symbol : node.getGroups()) {
            dependentGroupToSourceMap.put(symbol, node);
        }
    }
    for (Iterator<PlanNode> i = getCriteriaNodes().iterator(); i.hasNext(); ) {
        PlanNode node = i.next();
        for (GroupSymbol symbol : node.getGroups()) {
            if (dependentGroupToSourceMap.containsKey(symbol)) {
                i.remove();
                dependentCritieraNodes.add(node);
                break;
            }
        }
    }
    dependentCriteriaElements = new HashMap<ElementSymbol, Set<Collection<GroupSymbol>>>();
    for (PlanNode critNode : dependentCritieraNodes) {
        Criteria crit = (Criteria) critNode.getProperty(NodeConstants.Info.SELECT_CRITERIA);
        if (!(crit instanceof CompareCriteria)) {
            continue;
        }
        CompareCriteria compCrit = (CompareCriteria) crit;
        if (compCrit.getOperator() != CompareCriteria.EQ) {
            continue;
        }
        CompareCriteria compareCriteria = (CompareCriteria) crit;
        // this may be a proper dependent join criteria
        Collection<ElementSymbol>[] critElements = new Collection[2];
        critElements[0] = ElementCollectorVisitor.getElements(compareCriteria.getLeftExpression(), true);
        if (critElements[0].isEmpty()) {
            continue;
        }
        critElements[1] = ElementCollectorVisitor.getElements(compareCriteria.getRightExpression(), true);
        if (critElements[1].isEmpty()) {
            continue;
        }
        for (int expr = 0; expr < critElements.length; expr++) {
            // simplifying assumption that there will be a single element on the dependent side
            if (critElements[expr].size() != 1) {
                continue;
            }
            ElementSymbol elem = critElements[expr].iterator().next();
            if (!dependentGroupToSourceMap.containsKey(elem.getGroupSymbol())) {
                continue;
            }
            // this is also a simplifying assumption.  don't consider criteria that can't be pushed
            if (containsFunctionsThatCannotBePushed(expr == 0 ? compareCriteria.getRightExpression() : compareCriteria.getLeftExpression())) {
                continue;
            }
            Set<Collection<GroupSymbol>> independentGroups = dependentCriteriaElements.get(elem);
            if (independentGroups == null) {
                independentGroups = new HashSet<Collection<GroupSymbol>>();
                dependentCriteriaElements.put(elem, independentGroups);
            }
            // set the other side as independent elements
            independentGroups.add(GroupsUsedByElementsVisitor.getGroups(critElements[(expr + 1) % 2]));
        }
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) CompoundCriteria(org.teiid.query.sql.lang.CompoundCriteria) Criteria(org.teiid.query.sql.lang.Criteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol)

Example 23 with CompareCriteria

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

the class RulePushLimit method getMinValue.

/**
 * @param limitNode
 * @param child
 * @throws TeiidComponentException
 * @throws BlockedException
 * @throws ExpressionEvaluationException
 */
static Expression getMinValue(Expression expr1, Expression expr2) {
    if (expr1 == null) {
        return expr2;
    }
    if (expr2 == null) {
        return expr1;
    }
    Criteria crit = new CompareCriteria(expr1, CompareCriteria.LT, expr2);
    SearchedCaseExpression sce = new SearchedCaseExpression(Arrays.asList(new Object[] { crit }), Arrays.asList(new Object[] { expr1 }));
    sce.setElseExpression(expr2);
    sce.setType(expr1.getType());
    return evaluateIfPossible(sce);
}
Also used : SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) Criteria(org.teiid.query.sql.lang.Criteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria)

Example 24 with CompareCriteria

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

the class RulePushNonJoinCriteria method pushCriteria.

/**
 * True if the criteria is pushed.
 *
 * It's possible to push to the inner side of the join if the new criteria node
 * originates there
 *
 * @param joinNode
 * @param tgtCrit
 * @param metadata
 * @return
 */
private boolean pushCriteria(PlanNode joinNode, Criteria tgtCrit, Iterator iter, QueryMetadataInterface metadata) {
    PlanNode newCritNode = RelationalPlanner.createSelectNode(tgtCrit, false);
    Set<GroupSymbol> groups = newCritNode.getGroups();
    PlanNode[] innerJoinNodes = JoinUtil.getInnerSideJoinNodes(joinNode);
    boolean pushed = false;
    for (int i = 0; i < innerJoinNodes.length; i++) {
        if (FrameUtil.findOriginatingNode(innerJoinNodes[i], groups) != null) {
            if (pushed) {
                // create a new copy since the old one has been used
                newCritNode = RelationalPlanner.createSelectNode(tgtCrit, false);
            }
            innerJoinNodes[i].addAsParent(newCritNode);
            pushed = true;
        }
    }
    if (pushed) {
        iter.remove();
    } else if (firstRun && tgtCrit instanceof CompareCriteria) {
        CompareCriteria crit = (CompareCriteria) tgtCrit;
        Expression leftExpr = crit.getLeftExpression();
        Expression rightExpr = crit.getRightExpression();
        for (int i = 0; i < innerJoinNodes.length; i++) {
            PlanNode node = FrameUtil.findJoinSourceNode(innerJoinNodes[i]);
            boolean outer = false;
            for (PlanNode child : NodeEditor.findAllNodes(node, NodeConstants.Types.JOIN)) {
                if (((JoinType) child.getProperty(Info.JOIN_TYPE)).isOuter()) {
                    outer = true;
                    break;
                }
            }
            if (!outer) {
                continue;
            }
            Set<GroupSymbol> leftExprGroups = GroupsUsedByElementsVisitor.getGroups(leftExpr);
            Set<GroupSymbol> rightExprGroups = GroupsUsedByElementsVisitor.getGroups(rightExpr);
            ArrayList<ElementSymbol> notNull = new ArrayList<ElementSymbol>(2);
            if (node.getGroups().containsAll(leftExprGroups)) {
                collectNotNull(leftExpr, notNull);
            } else if (node.getGroups().containsAll(rightExprGroups)) {
                collectNotNull(rightExpr, notNull);
            }
            if (!notNull.isEmpty()) {
                pushed = true;
                for (ElementSymbol es : notNull) {
                    IsNullCriteria inc = new IsNullCriteria(es);
                    inc.setNegated(true);
                    PlanNode notNullCrit = RelationalPlanner.createSelectNode(inc, false);
                    notNullCrit.setProperty(NodeConstants.Info.IS_TEMPORARY, true);
                    innerJoinNodes[i].addAsParent(notNullCrit);
                }
            }
        }
    }
    return pushed;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Set(java.util.Set) ArrayList(java.util.ArrayList) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) Expression(org.teiid.query.sql.symbol.Expression) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria)

Example 25 with CompareCriteria

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

the class RulePushSelectCriteria method markDependent.

private void markDependent(PlanNode critNode, PlanNode accessNode, QueryMetadataInterface metadata, CapabilitiesFinder capFinder) throws QueryMetadataException, TeiidComponentException {
    // once a dependent crit node is pushed, don't bother pushing it further into the command
    // dependent access node will use this as an assumption for where dependent sets can appear in the command
    critNode.setProperty(NodeConstants.Info.IS_PUSHED, Boolean.TRUE);
    if (createdNodes != null) {
        // this is during a planning run and should not cause additional side-effects
        return;
    }
    accessNode.setProperty(NodeConstants.Info.IS_DEPENDENT_SET, Boolean.TRUE);
    Criteria crit = (Criteria) critNode.getProperty(NodeConstants.Info.SELECT_CRITERIA);
    if (isMultiAttributeDependentSet(crit)) {
        // split the criteria as needed
        List<DependentSetCriteria> crits = splitDependentSetCriteria((DependentSetCriteria) crit, CapabilitiesUtil.supports(Capability.ARRAY_TYPE, RuleRaiseAccess.getModelIDFromAccess(accessNode, metadata), metadata, capFinder), metadata);
        critNode.setProperty(NodeConstants.Info.SELECT_CRITERIA, new CompoundCriteria(crits));
    }
    Collection<ElementSymbol> elements = null;
    for (PlanNode joinNode : NodeEditor.findAllNodes(accessNode, NodeConstants.Types.JOIN, NodeConstants.Types.SOURCE)) {
        List<Criteria> joinCriteria = (List<Criteria>) joinNode.getProperty(Info.JOIN_CRITERIA);
        if (joinCriteria == null) {
            continue;
        }
        for (Criteria joinPredicate : joinCriteria) {
            if (!(joinPredicate instanceof CompareCriteria)) {
                continue;
            }
            CompareCriteria cc = (CompareCriteria) joinPredicate;
            if (!cc.isOptional()) {
                continue;
            }
            if (elements == null) {
                elements = ElementCollectorVisitor.getElements((LanguageObject) critNode.getProperty(Info.SELECT_CRITERIA), true);
            }
            if (!Collections.disjoint(elements, ElementCollectorVisitor.getElements(cc, false))) {
                cc.setOptional(false);
            }
        }
    }
}
Also used : DependentSetCriteria(org.teiid.query.sql.lang.DependentSetCriteria) ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) CompoundCriteria(org.teiid.query.sql.lang.CompoundCriteria) 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) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) LanguageObject(org.teiid.query.sql.LanguageObject)

Aggregations

CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)52 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)25 Constant (org.teiid.query.sql.symbol.Constant)24 Criteria (org.teiid.query.sql.lang.Criteria)21 ArrayList (java.util.ArrayList)15 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)11 Test (org.junit.Test)10 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)9 CompoundCriteria (org.teiid.query.sql.lang.CompoundCriteria)9 Expression (org.teiid.query.sql.symbol.Expression)9 IsNullCriteria (org.teiid.query.sql.lang.IsNullCriteria)8 List (java.util.List)7 HashMap (java.util.HashMap)6 Query (org.teiid.query.sql.lang.Query)6 SetCriteria (org.teiid.query.sql.lang.SetCriteria)6 From (org.teiid.query.sql.lang.From)5 Select (org.teiid.query.sql.lang.Select)5 DependentSetCriteria (org.teiid.query.sql.lang.DependentSetCriteria)4 Collection (java.util.Collection)3 JoinType (org.teiid.query.sql.lang.JoinType)3