Search in sources :

Example 6 with IsNullCriteria

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

the class TestIsNullCriteria method testEquals2.

public void testEquals2() {
    // $NON-NLS-1$
    IsNullCriteria c1 = example("abc", false);
    IsNullCriteria c2 = (IsNullCriteria) c1.clone();
    // $NON-NLS-1$ //$NON-NLS-2$
    assertTrue("Equivalent is null criteria don't compare as equal: " + c1 + ", " + c2, c1.equals(c2));
}
Also used : IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria)

Example 7 with IsNullCriteria

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

the class TestAccessNode method testOpen_Defect16059.

@Test
public void testOpen_Defect16059() throws Exception {
    // $NON-NLS-1$
    Query query = (Query) TestResolver.helpResolve("SELECT e1, e2 FROM pm1.g1 WHERE e2 = 5 AND ? IS NULL", RealMetadataFactory.example1Cached());
    IsNullCriteria nullCrit = (IsNullCriteria) ((CompoundCriteria) query.getCriteria()).getCriteria(1);
    nullCrit.setExpression(new Constant(null));
    // $NON-NLS-1$
    helpTestOpen(query, "SELECT e1, e2 FROM pm1.g1 WHERE e2 = 5", true);
}
Also used : Query(org.teiid.query.sql.lang.Query) Constant(org.teiid.query.sql.symbol.Constant) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria) Test(org.junit.Test)

Example 8 with IsNullCriteria

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

the class TestFrameUtil method getExamplePlan.

/**
 * <pre>
 * Join(groups=[3, 2, 1])
 *   Null(groups=[1])
 *   Select(groups=[2])
 *     Join(groups=[3, 2])
 *       Source(groups=[3])
 *       Access(groups=[2])
 * </pre>
 */
public static PlanNode getExamplePlan() {
    PlanNode joinNode = NodeFactory.getNewNode(NodeConstants.Types.JOIN);
    joinNode.setProperty(NodeConstants.Info.JOIN_TYPE, JoinType.JOIN_CROSS);
    joinNode.addGroup(getGroup(1));
    joinNode.addGroup(getGroup(2));
    joinNode.addGroup(getGroup(3));
    PlanNode nullNode = NodeFactory.getNewNode(NodeConstants.Types.NULL);
    nullNode.addGroup(getGroup(1));
    joinNode.addFirstChild(nullNode);
    PlanNode childCriteria = NodeFactory.getNewNode(NodeConstants.Types.SELECT);
    childCriteria.setProperty(Info.SELECT_CRITERIA, new IsNullCriteria(new Constant(1)));
    childCriteria.addGroup(getGroup(2));
    joinNode.addLastChild(childCriteria);
    PlanNode childJoinNode = NodeFactory.getNewNode(NodeConstants.Types.JOIN);
    childJoinNode.setProperty(NodeConstants.Info.JOIN_TYPE, JoinType.JOIN_CROSS);
    childJoinNode.addGroup(getGroup(2));
    childJoinNode.addGroup(getGroup(3));
    childCriteria.addFirstChild(childJoinNode);
    PlanNode accessNode = NodeFactory.getNewNode(NodeConstants.Types.ACCESS);
    accessNode.addGroup(getGroup(2));
    childJoinNode.addFirstChild(accessNode);
    PlanNode sourceNode = NodeFactory.getNewNode(NodeConstants.Types.SOURCE);
    sourceNode.addGroup(getGroup(3));
    childJoinNode.addFirstChild(sourceNode);
    return joinNode;
}
Also used : PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) Constant(org.teiid.query.sql.symbol.Constant) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria)

Example 9 with IsNullCriteria

use of org.teiid.query.sql.lang.IsNullCriteria 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 10 with IsNullCriteria

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

the class RuleCopyCriteria method copyCriteria.

/**
 * Given a criteria and a map of elements to values try to create a new single group criteria
 *
 * If the new criteria does not have exactly one group or already exists in the combined criteria,
 * it will not be added.
 *
 * @param crit
 * @param tgtMap
 * @param joinCriteria
 * @param combinedCriteria
 * @return number of remaining groups if the copy was successful
 */
private Integer copyCriteria(Criteria crit, Map<Expression, Expression> tgtMap, List<Criteria> joinCriteria, Set<Criteria> combinedCriteria, boolean checkForGroupReduction, QueryMetadataInterface metadata, boolean underAccess) {
    int startGroups = GroupsUsedByElementsVisitor.getGroups(crit).size();
    Criteria tgtCrit = (Criteria) crit.clone();
    try {
        tgtCrit = FrameUtil.convertCriteria(tgtCrit, tgtMap, metadata, true);
    } catch (QueryPlannerException err) {
        // $NON-NLS-1$
        LogManager.logDetail(LogConstants.CTX_QUERY_PLANNER, err, "Could not remap target criteria in RuleCopyCriteria");
        return null;
    }
    if (tgtCrit instanceof IsNullCriteria && ((IsNullCriteria) tgtCrit).isNegated()) {
        return null;
    }
    int endGroups = GroupsUsedByElementsVisitor.getGroups(tgtCrit).size();
    if (checkForGroupReduction) {
        if (endGroups >= startGroups) {
            return null;
        }
    } else if (endGroups > startGroups) {
        return null;
    }
    boolean isNew = combinedCriteria.add(tgtCrit);
    if (underAccess) {
        if (!isNew || checkForGroupReduction || endGroups > 1) {
            return null;
        }
        if (!COPY_ALL) {
            boolean use = false;
            Collection<ElementSymbol> cols = ElementCollectorVisitor.getElements(tgtCrit, true);
            // use only if it could be used to further rewrite predicates
            for (Criteria existing : combinedCriteria) {
                if (existing.equals(tgtCrit)) {
                    continue;
                }
                Collection<ElementSymbol> elements = ElementCollectorVisitor.getElements(existing, true);
                if (GroupsUsedByElementsVisitor.getGroups(elements).size() > 1) {
                    continue;
                }
                if (elements.containsAll(cols)) {
                    use = true;
                    break;
                }
            }
            if (!use) {
                return null;
            }
        }
    }
    // if this is unique or it a duplicate but reduced a current join conjunct, return true
    if (isNew) {
        joinCriteria.add(tgtCrit);
        if (tgtCrit instanceof CompareCriteria) {
            CompareCriteria cc = (CompareCriteria) tgtCrit;
            if (!EvaluatableVisitor.willBecomeConstant(cc.getRightExpression()) && !EvaluatableVisitor.willBecomeConstant(cc.getRightExpression())) {
                ((CompareCriteria) tgtCrit).setOptional(true);
            }
        }
        return endGroups;
    } else if (checkForGroupReduction && endGroups < 2) {
        return endGroups;
    }
    return null;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria) Criteria(org.teiid.query.sql.lang.Criteria) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) QueryPlannerException(org.teiid.api.exception.query.QueryPlannerException) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) IsNullCriteria(org.teiid.query.sql.lang.IsNullCriteria)

Aggregations

IsNullCriteria (org.teiid.query.sql.lang.IsNullCriteria)22 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)6 Criteria (org.teiid.query.sql.lang.Criteria)5 Constant (org.teiid.query.sql.symbol.Constant)5 Test (org.junit.Test)4 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)4 Expression (org.teiid.query.sql.symbol.Expression)4 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)3 Collection (java.util.Collection)2 Query (org.teiid.query.sql.lang.Query)2 SetCriteria (org.teiid.query.sql.lang.SetCriteria)2 Reference (org.teiid.query.sql.symbol.Reference)2 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)1 ArrayImpl (org.teiid.core.types.ArrayImpl)1 LanguageObject (org.teiid.query.sql.LanguageObject)1 LanguageVisitor (org.teiid.query.sql.LanguageVisitor)1 CompoundCriteria (org.teiid.query.sql.lang.CompoundCriteria)1 DependentSetCriteria (org.teiid.query.sql.lang.DependentSetCriteria)1