Search in sources :

Example 6 with SetCriteria

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

the class TestDependentCriteriaProcessor method testSetCriteria.

@Test
public void testSetCriteria() throws Exception {
    DependentAccessNode dan = new DependentAccessNode(0);
    // $NON-NLS-1$
    SetCriteria sc = new SetCriteria(new ElementSymbol("e1"), Arrays.asList(new Constant(1), new Constant(2)));
    sc.setAllConstants(true);
    DependentCriteriaProcessor dcp = new DependentCriteriaProcessor(1, -1, dan, sc);
    Criteria result = dcp.prepareCriteria();
    // $NON-NLS-1$
    assertEquals(new CompareCriteria(new ElementSymbol("e1"), CompareCriteria.EQ, new Constant(1)), result);
    assertTrue(dcp.hasNextCommand());
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Constant(org.teiid.query.sql.symbol.Constant) SetCriteria(org.teiid.query.sql.lang.SetCriteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) SetCriteria(org.teiid.query.sql.lang.SetCriteria) Criteria(org.teiid.query.sql.lang.Criteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) Test(org.junit.Test)

Example 7 with SetCriteria

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

the class RulePushLargeIn method execute.

@Override
public PlanNode execute(PlanNode plan, QueryMetadataInterface metadata, CapabilitiesFinder capabilitiesFinder, RuleStack rules, AnalysisRecord analysisRecord, CommandContext context) throws QueryPlannerException, QueryMetadataException, TeiidComponentException {
    for (PlanNode critNode : NodeEditor.findAllNodes(plan, NodeConstants.Types.SELECT, NodeConstants.Types.ACCESS)) {
        if (critNode.hasBooleanProperty(NodeConstants.Info.IS_HAVING) || critNode.hasBooleanProperty(NodeConstants.Info.IS_PHANTOM)) {
            continue;
        }
        Criteria crit = (Criteria) critNode.getProperty(NodeConstants.Info.SELECT_CRITERIA);
        if (!(crit instanceof SetCriteria)) {
            continue;
        }
        SetCriteria setCriteria = (SetCriteria) crit;
        if (setCriteria.isNegated() || !setCriteria.isAllConstants()) {
            continue;
        }
        // we need to be directly over an access node
        PlanNode childAccess = critNode.getFirstChild();
        accessLoop: while (true) {
            switch(childAccess.getType()) {
                case NodeConstants.Types.ACCESS:
                    break accessLoop;
                case NodeConstants.Types.SELECT:
                    break;
                default:
                    break accessLoop;
            }
            childAccess = childAccess.getFirstChild();
        }
        if (childAccess.getType() != NodeConstants.Types.ACCESS) {
            continue;
        }
        // use a dummy value to test if we can raise
        critNode.setProperty(NodeConstants.Info.SELECT_CRITERIA, new SetCriteria(setCriteria.getExpression(), Collections.EMPTY_LIST));
        boolean canRaise = RuleRaiseAccess.canRaiseOverSelect(childAccess, metadata, capabilitiesFinder, critNode, analysisRecord);
        critNode.setProperty(NodeConstants.Info.SELECT_CRITERIA, crit);
        if (!canRaise) {
            continue;
        }
        // push the crit node and mark as dependent set
        critNode.getParent().replaceChild(critNode, critNode.getFirstChild());
        childAccess.addAsParent(critNode);
        RuleRaiseAccess.performRaise(plan, childAccess, critNode);
        childAccess.setProperty(NodeConstants.Info.IS_DEPENDENT_SET, true);
        childAccess.setProperty(NodeConstants.Info.EST_CARDINALITY, null);
    }
    return plan;
}
Also used : PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) SetCriteria(org.teiid.query.sql.lang.SetCriteria) Criteria(org.teiid.query.sql.lang.Criteria) SetCriteria(org.teiid.query.sql.lang.SetCriteria)

Example 8 with SetCriteria

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

the class PartitionAnalyzer method extractPartitionInfo.

private static Map<ElementSymbol, Set<Constant>> extractPartitionInfo(Query query, List<ElementSymbol> projectedSymbols) {
    List<Expression> projected = query.getSelect().getProjectedSymbols();
    List<Criteria> crits = Criteria.separateCriteriaByAnd(query.getCriteria());
    Map<Expression, Set<Constant>> inMap = new HashMap<Expression, Set<Constant>>();
    for (Criteria criteria : crits) {
        if (criteria instanceof CompareCriteria) {
            CompareCriteria cc = (CompareCriteria) criteria;
            if (cc.getOperator() != CompareCriteria.EQ) {
                continue;
            }
            if (cc.getLeftExpression() instanceof Constant) {
                inMap.put(cc.getRightExpression(), new HashSet<Constant>(Arrays.asList((Constant) cc.getLeftExpression())));
            } else if (cc.getRightExpression() instanceof Constant) {
                inMap.put(cc.getLeftExpression(), new HashSet<Constant>(Arrays.asList((Constant) cc.getRightExpression())));
            }
            continue;
        }
        if (!(criteria instanceof SetCriteria)) {
            continue;
        }
        SetCriteria sc = (SetCriteria) criteria;
        HashSet<Constant> values = new HashSet<Constant>();
        boolean allConstants = true;
        for (Expression exp : (Collection<Expression>) sc.getValues()) {
            if (exp instanceof Constant) {
                values.add((Constant) exp);
            } else {
                allConstants = false;
                break;
            }
        }
        if (allConstants) {
            inMap.put(sc.getExpression(), values);
        }
    }
    Map<ElementSymbol, Set<Constant>> result = new HashMap<ElementSymbol, Set<Constant>>();
    for (int i = 0; i < projected.size(); i++) {
        Expression ex = SymbolMap.getExpression(projected.get(i));
        if (DataTypeManager.isNonComparable(DataTypeManager.getDataTypeName(ex.getType()))) {
            continue;
        }
        if (ex instanceof Constant) {
            result.put(projectedSymbols.get(i), Collections.singleton((Constant) ex));
        } else {
            Set<Constant> values = inMap.get(ex);
            if (values != null) {
                result.put(projectedSymbols.get(i), values);
            }
        }
    }
    return result;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Constant(org.teiid.query.sql.symbol.Constant) Criteria(org.teiid.query.sql.lang.Criteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) SetCriteria(org.teiid.query.sql.lang.SetCriteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) Expression(org.teiid.query.sql.symbol.Expression) SetCriteria(org.teiid.query.sql.lang.SetCriteria)

Example 9 with SetCriteria

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

the class RulePlanProcedures method findInputNodes.

private void findInputNodes(final HashSet<ElementSymbol> inputs, PlanNode critNode, final List<Criteria> conjuncts, final Set<ElementSymbol> params) {
    while (critNode.getType() == NodeConstants.Types.SELECT) {
        final PlanNode currentNode = critNode;
        final Criteria crit = (Criteria) currentNode.getProperty(NodeConstants.Info.SELECT_CRITERIA);
        critNode = currentNode.getParent();
        if (!currentNode.getGroups().isEmpty()) {
            continue;
        }
        LanguageVisitor visitor = new LanguageVisitor() {

            public void visit(CompareCriteria compCrit) {
                if (compCrit.getOperator() == CompareCriteria.EQ && checkForInput(compCrit.getLeftExpression()) && !checkForAnyInput(compCrit.getRightExpression())) {
                    addInputNode((Reference) compCrit.getLeftExpression());
                }
            }

            private void addInputNode(Reference param) {
                params.add(param.getExpression());
                conjuncts.add(crit);
                NodeEditor.removeChildNode(currentNode.getParent(), currentNode);
            }

            public void visit(IsNullCriteria isNull) {
                if (!isNull.isNegated() && checkForInput(isNull.getExpression())) {
                    addInputNode((Reference) isNull.getExpression());
                }
            }

            public void visit(SetCriteria obj) {
                if (!obj.isNegated() && checkForInput(obj.getExpression()) && !checkForAnyInput(obj.getValues())) {
                    addInputNode((Reference) obj.getExpression());
                }
            }

            public void visit(DependentSetCriteria obj) {
                if (obj.isNegated()) {
                    // just a sanity check
                    return;
                }
                if (obj.hasMultipleAttributes()) {
                    for (AttributeComparison comp : obj.getAttributes()) {
                        if (!checkForInput(comp.dep)) {
                            return;
                        }
                    }
                    for (AttributeComparison comp : obj.getAttributes()) {
                        params.add(((Reference) comp.dep).getExpression());
                    }
                    conjuncts.add(crit);
                    NodeEditor.removeChildNode(currentNode.getParent(), currentNode);
                } else if (checkForInput(obj.getExpression())) {
                    addInputNode((Reference) obj.getExpression());
                }
            }

            boolean checkForInput(Expression expr) {
                if (!(expr instanceof Reference)) {
                    return false;
                }
                // if the expr is a function containing a reference should give a warning
                Reference ref = (Reference) expr;
                return inputs.contains(ref.getExpression());
            }

            boolean checkForAnyInput(LanguageObject expr) {
                for (Reference ref : ReferenceCollectorVisitor.getReferences(expr)) {
                    if (checkForInput(ref)) {
                        return true;
                    }
                }
                return false;
            }

            boolean checkForAnyInput(Collection<Expression> expressions) {
                for (Expression expr : expressions) {
                    if (checkForAnyInput(expr)) {
                        return true;
                    }
                }
                return false;
            }
        };
        for (Criteria conjunct : Criteria.separateCriteriaByAnd(crit)) {
            conjunct.acceptVisitor(visitor);
        }
    }
}
Also used : LanguageVisitor(org.teiid.query.sql.LanguageVisitor) DependentSetCriteria(org.teiid.query.sql.lang.DependentSetCriteria) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) Expression(org.teiid.query.sql.symbol.Expression) Reference(org.teiid.query.sql.symbol.Reference) DependentSetCriteria(org.teiid.query.sql.lang.DependentSetCriteria) SetCriteria(org.teiid.query.sql.lang.SetCriteria) Collection(java.util.Collection) DependentSetCriteria(org.teiid.query.sql.lang.DependentSetCriteria) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria) Criteria(org.teiid.query.sql.lang.Criteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) SetCriteria(org.teiid.query.sql.lang.SetCriteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) AttributeComparison(org.teiid.query.sql.lang.DependentSetCriteria.AttributeComparison) LanguageObject(org.teiid.query.sql.LanguageObject) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria)

Example 10 with SetCriteria

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

the class BaseIndexInfo method processCriteria.

private void processCriteria(Criteria condition, boolean primary) {
    List<Criteria> crits = Criteria.separateCriteriaByAnd(condition);
    if (!primary) {
        for (Iterator<Criteria> critIter = crits.iterator(); critIter.hasNext(); ) {
            Criteria criteria = critIter.next();
            if (table.getColumnMap().keySet().containsAll(ElementCollectorVisitor.getElements(criteria, false))) {
                if (coveredCriteria == null) {
                    coveredCriteria = new CompoundCriteria();
                }
                coveredCriteria.addCriteria(criteria);
            } else {
                covering = false;
                if (nonCoveredCriteria == null) {
                    nonCoveredCriteria = new CompoundCriteria();
                }
                nonCoveredCriteria.addCriteria(criteria);
                critIter.remove();
            }
        }
    }
    for (int i = 0; i < table.getPkLength(); i++) {
        for (Iterator<Criteria> critIter = crits.iterator(); critIter.hasNext(); ) {
            Criteria criteria = critIter.next();
            if (criteria instanceof CompareCriteria) {
                CompareCriteria cc = (CompareCriteria) criteria;
                Object matchResult = table.matchesPkColumn(i, cc.getLeftExpression());
                if (Boolean.FALSE.equals(matchResult)) {
                    continue;
                }
                if (cc.getOperator() != CompareCriteria.EQ && !table.supportsOrdering(i, cc.getLeftExpression())) {
                    critIter.remove();
                    continue;
                }
                this.addCondition(i, matchResult, (Constant) cc.getRightExpression(), cc.getOperator());
                critIter.remove();
            } else if (criteria instanceof IsNullCriteria) {
                IsNullCriteria inc = (IsNullCriteria) criteria;
                Object matchResult = table.matchesPkColumn(i, inc.getExpression());
                if (Boolean.FALSE.equals(matchResult)) {
                    continue;
                }
                this.addCondition(i, matchResult, new Constant(null), CompareCriteria.EQ);
                critIter.remove();
            } else if (criteria instanceof MatchCriteria) {
                MatchCriteria matchCriteria = (MatchCriteria) criteria;
                Object matchResult = table.matchesPkColumn(i, matchCriteria.getLeftExpression());
                if (Boolean.FALSE.equals(matchResult)) {
                    continue;
                }
                Constant value = (Constant) matchCriteria.getRightExpression();
                String pattern = (String) value.getValue();
                boolean escaped = false;
                char escapeChar = matchCriteria.getEscapeChar();
                if (matchCriteria.getMode() == MatchMode.REGEX) {
                    escapeChar = '\\';
                }
                StringBuilder prefix = new StringBuilder();
                if (pattern.length() > 0 && matchCriteria.getMode() == MatchMode.REGEX && pattern.charAt(0) != '^') {
                    // make the assumption that we require an anchor
                    continue;
                }
                for (int j = matchCriteria.getMode() == MatchMode.REGEX ? 1 : 0; j < pattern.length(); j++) {
                    char character = pattern.charAt(j);
                    if (character == escapeChar && character != MatchCriteria.NULL_ESCAPE_CHAR) {
                        if (escaped) {
                            prefix.append(character);
                            escaped = false;
                        } else {
                            escaped = true;
                        }
                        continue;
                    }
                    if (!escaped) {
                        if (matchCriteria.getMode() == MatchMode.LIKE) {
                            if (character == MatchCriteria.WILDCARD_CHAR || character == MatchCriteria.MATCH_CHAR) {
                                break;
                            }
                        } else {
                            int index = Arrays.binarySearch(Evaluator.REGEX_RESERVED, character);
                            if (index >= 0 && pattern.length() > 0) {
                                getRegexPrefix(pattern, escapeChar, prefix, j, character);
                                break;
                            }
                        }
                    } else {
                        escaped = false;
                    }
                    prefix.append(character);
                }
                if (prefix.length() > 0) {
                    this.addCondition(i, matchResult, new Constant(prefix.toString()), CompareCriteria.GE);
                    if (matchCriteria.getLeftExpression() instanceof Function && table.supportsOrdering(i, matchCriteria.getLeftExpression())) {
                        // this comparison needs to be aware of case
                        this.addCondition(i, matchResult, new Constant(prefix.substring(0, prefix.length() - 1) + (char) (Character.toLowerCase(prefix.charAt(prefix.length() - 1)) + 1)), CompareCriteria.LE);
                    } else {
                        this.addCondition(i, matchResult, new Constant(prefix.substring(0, prefix.length() - 1) + (char) (prefix.charAt(prefix.length() - 1) + 1)), CompareCriteria.LE);
                    }
                } else {
                    critIter.remove();
                }
            } else if (criteria instanceof SetCriteria) {
                SetCriteria setCriteria = (SetCriteria) criteria;
                if (setCriteria.isNegated()) {
                    continue;
                }
                Object matchResult = table.matchesPkColumn(i, setCriteria.getExpression());
                if (Boolean.FALSE.equals(matchResult)) {
                    continue;
                }
                Collection<Constant> values = (Collection<Constant>) setCriteria.getValues();
                this.addSet(i, matchResult, values);
                critIter.remove();
            }
        }
    }
}
Also used : MatchCriteria(org.teiid.query.sql.lang.MatchCriteria) Constant(org.teiid.query.sql.symbol.Constant) CompoundCriteria(org.teiid.query.sql.lang.CompoundCriteria) Criteria(org.teiid.query.sql.lang.Criteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) SetCriteria(org.teiid.query.sql.lang.SetCriteria) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria) MatchCriteria(org.teiid.query.sql.lang.MatchCriteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) Function(org.teiid.query.sql.symbol.Function) CompoundCriteria(org.teiid.query.sql.lang.CompoundCriteria) SetCriteria(org.teiid.query.sql.lang.SetCriteria) Collection(java.util.Collection) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria)

Aggregations

SetCriteria (org.teiid.query.sql.lang.SetCriteria)12 Criteria (org.teiid.query.sql.lang.Criteria)8 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)7 Test (org.junit.Test)6 Constant (org.teiid.query.sql.symbol.Constant)6 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)4 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 AndOr (org.teiid.language.AndOr)2 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)2 CompoundCriteria (org.teiid.query.sql.lang.CompoundCriteria)2 DependentSetCriteria (org.teiid.query.sql.lang.DependentSetCriteria)2 IsNullCriteria (org.teiid.query.sql.lang.IsNullCriteria)2 Expression (org.teiid.query.sql.symbol.Expression)2 Reference (org.teiid.query.sql.symbol.Reference)2 HashMap (java.util.HashMap)1 Expression (org.teiid.language.Expression)1 In (org.teiid.language.In)1 LanguageObject (org.teiid.query.sql.LanguageObject)1 LanguageVisitor (org.teiid.query.sql.LanguageVisitor)1