Search in sources :

Example 36 with CompareCriteria

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

the class DependentProcedureCriteriaProcessor method prepareNextCommand.

protected boolean prepareNextCommand(VariableContext context) throws BlockedException, TeiidComponentException, TeiidProcessingException {
    if (this.critInProgress == null) {
        critInProgress = prepareCriteria();
    }
    for (int j = 0; j < inputReferences.size(); j++) {
        Reference ref = (Reference) inputReferences.get(j);
        context.remove(ref.getExpression());
    }
    if (critInProgress == QueryRewriter.FALSE_CRITERIA) {
        critInProgress = null;
        consumedCriteria();
        return false;
    }
    boolean validRow = true;
    for (Iterator<Criteria> i = Criteria.separateCriteriaByAnd(critInProgress).iterator(); i.hasNext() && validRow; ) {
        Criteria crit = i.next();
        Object value = null;
        boolean nullAllowed = false;
        Reference parameter = null;
        if (crit instanceof IsNullCriteria) {
            parameter = (Reference) ((IsNullCriteria) crit).getExpression();
            nullAllowed = true;
        } else if (crit instanceof CompareCriteria) {
            CompareCriteria compare = (CompareCriteria) crit;
            value = compare.getRightExpression();
            if (compare.getLeftExpression() instanceof Array) {
                Array array = (Array) compare.getLeftExpression();
                if (value instanceof Expression) {
                    value = eval.evaluate((Expression) value, null);
                }
                if (value == null) {
                    validRow = false;
                    break;
                }
                ArrayImpl valueArray = (ArrayImpl) value;
                for (int j = 0; j < array.getExpressions().size(); j++) {
                    validRow = setParam(context, valueArray.getValues()[j], nullAllowed, (Reference) array.getExpressions().get(j));
                    if (!validRow) {
                        break;
                    }
                }
                continue;
            }
            parameter = (Reference) compare.getLeftExpression();
        } else {
            // $NON-NLS-1$
            Assertion.failed("Unknown predicate type");
        }
        validRow = setParam(context, value, nullAllowed, parameter);
    }
    critInProgress = null;
    consumedCriteria();
    if (!validRow) {
        return false;
    }
    for (int j = 0; j < inputReferences.size(); j++) {
        Object defaultValue = inputDefaults.get(j);
        Reference ref = (Reference) inputReferences.get(j);
        if (defaultValue != null && !context.containsVariable(ref.getExpression())) {
            context.setValue(ref.getExpression(), defaultValue);
        }
    }
    return true;
}
Also used : Array(org.teiid.query.sql.symbol.Array) Expression(org.teiid.query.sql.symbol.Expression) Reference(org.teiid.query.sql.symbol.Reference) ArrayImpl(org.teiid.core.types.ArrayImpl) Criteria(org.teiid.query.sql.lang.Criteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria)

Example 37 with CompareCriteria

use of org.teiid.query.sql.lang.CompareCriteria 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)

Example 38 with CompareCriteria

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

the class TestUpdateImpl method helpExample.

public static org.teiid.query.sql.lang.Update helpExample() {
    // $NON-NLS-1$
    GroupSymbol group = TestGroupImpl.helpExample("vm1.g1");
    org.teiid.query.sql.lang.Update result = new org.teiid.query.sql.lang.Update();
    result.setGroup(group);
    result.addChange(TestElementImpl.helpExample("vm1.g1", "e1"), new Constant(new Integer(1)));
    result.addChange(TestElementImpl.helpExample("vm1.g1", "e2"), new Constant(new Integer(1)));
    result.addChange(TestElementImpl.helpExample("vm1.g1", "e3"), new Constant(new Integer(1)));
    result.addChange(TestElementImpl.helpExample("vm1.g1", "e4"), new Constant(new Integer(1)));
    result.setCriteria(new CompareCriteria(new Constant(new Integer(1)), CompareCriteria.EQ, new Constant(new Integer(1))));
    return result;
}
Also used : Constant(org.teiid.query.sql.symbol.Constant) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) Update(org.teiid.language.Update) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria)

Example 39 with CompareCriteria

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

the class TestJoinImpl method helpExample.

public static JoinPredicate helpExample(JoinType type) {
    // $NON-NLS-1$ //$NON-NLS-2$
    ElementSymbol e1 = TestElementImpl.helpExample("vm1.g1", "e1");
    // $NON-NLS-1$ //$NON-NLS-2$
    ElementSymbol e2 = TestElementImpl.helpExample("vm1.g2", "e1");
    ArrayList criteria = new ArrayList();
    criteria.add(new CompareCriteria(e1, CompareCriteria.EQ, e2));
    return new JoinPredicate(new UnaryFromClause(e1.getGroupSymbol()), new UnaryFromClause(e2.getGroupSymbol()), type, criteria);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) UnaryFromClause(org.teiid.query.sql.lang.UnaryFromClause) JoinPredicate(org.teiid.query.sql.lang.JoinPredicate) ArrayList(java.util.ArrayList) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria)

Example 40 with CompareCriteria

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

the class TestDependentCriteriaProcessor method testEvaluatedSetCriteria.

@Test
public void testEvaluatedSetCriteria() throws Exception {
    DependentAccessNode dan = new DependentAccessNode(0);
    CommandContext cc = new CommandContext();
    dan.setContext(cc);
    List<Reference> references = Arrays.asList(new Reference(1), new Reference(2));
    for (Reference reference : references) {
        cc.getVariableContext().setGlobalValue(reference.getContextSymbol(), 1);
    }
    // $NON-NLS-1$
    SetCriteria sc = new SetCriteria(new ElementSymbol("e1"), references);
    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);
    assertFalse(dcp.hasNextCommand());
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) CommandContext(org.teiid.query.util.CommandContext) Reference(org.teiid.query.sql.symbol.Reference) 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)

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