Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.pig.newplan.logical.rules;
 
A utility class to generate a DNF plan from a base plan. Not that DNF plan can be generated only AFTER all NOTs are pushed below the OR/AND expression, namely, the NOTConversionVisitor should be applied before DNF can be generated.
 
     private OperatorPlan dnfPlan = null;
 
         super(plannew ReverseDependencyOrderWalker(plan));
          = new LinkedList<LogicalExpression>();
     }
 
     OperatorPlan getDNFPlan() {
         if ( == null = (.isEmpty() ?  : .pop().getPlan());
         return ;
     }
 
     @Override
     public void visit(AndExpression expthrows FrontendException {
         LogicalExpression rhsExp = ((exp.getRhs() instanceof AndExpression || exp.getRhs() instanceof OrExpression ? .pop() : exp.getRhs()));
         LogicalExpression lhsExp = ((exp.getLhs() instanceof AndExpression || exp.getLhs() instanceof OrExpression ? .pop() : exp.getLhs()));
         if (!(lhsExp instanceof AndExpression) && !(lhsExp instanceof DNFExpression) && !(lhsExp instanceof OrExpression) && !(rhsExp instanceof AndExpression) && !(rhsExp instanceof OrExpression) && !(rhsExp instanceof DNFExpression)) .push(exp);
         else {
             if ( == null = new DNFPlan();
             boolean isLhsAnd = lhsExp instanceof AndExpression || 
             (lhsExp instanceof DNFExpression && 
                 ((DNFExpressionlhsExp). == ..),
                 isRhsAnd = rhsExp instanceof AndExpression || (rhsExp instanceof DNFExpression && ((DNFExpressionrhsExp). == ..);
 
             LogicalExpression current;
             if (isLhsAnd && isRhsAnd) {
                 current = new DNFExpression("dnfAnd",
                                 ..);
                 ((DNFPlan).safeAdd(current);
                 .push(current);
                 addChildren(currentlhsExp);
                 addChildren(currentrhsExp);
             }
             else {
                 boolean isLhsOr = lhsExp instanceof OrExpression ||
                 (lhsExp instanceof DNFExpression && 
                     ((DNFExpressionlhsExp). == ..), 
                 isRhsOr = rhsExp instanceof OrExpression || 
                 (rhsExp instanceof DNFExpression && ((DNFExpressionrhsExp). == ..);
 
                 if (isLhsOr || isRhsOrcurrent = new DNFExpression(
                                 "dnfOr"..);
                 else current = new DNFExpression("dnfAnd",
                                 ..);
 
                 ((DNFPlan).safeAdd(current);
                 .push(current);
                 if (!isLhsOr && !isRhsOr) {
                     if (isLhsAndaddChildren(currentlhsExp);
                     else if (!isLhsOr) {
                         // lhs is a simple expression
                         ((DNFPlan).safeAdd(lhsExp);
                         .connect(currentlhsExp);
                     }
                     if (isRhsAndaddChildren(currentrhsExp);
                     else if (!isRhsOr) {
                         // rhs is a simple expression
                         ((DNFPlan).safeAdd(rhsExp);
                         .connect(currentrhsExp);
                     }
                 }
                else if (!isLhsOr) {
                    // rhs is OR
                    if (!isLhsAnd) {
                        // lhs is simple
                        mergeSimpleOr(currentlhsExprhsExptrue);
                    }
                    else {
                        // lhs is AND
                        mergeAndOr(currentlhsExprhsExptrue);
                    }
                }
                else if (!isRhsOr) {
                    // lhs is OR
                    if (!isRhsAnd) {
                        // rhs is simple
                        mergeSimpleOr(currentrhsExplhsExpfalse);
                    }
                    else {
                        // rhs is AND
                        mergeAndOr(currentrhsExplhsExpfalse);
                    }
                }
                else {
                    // both lhs and rhs are OR
                    Operator[] lhsChildren = lhsExp.getPlan().getSuccessors(
                                    lhsExp).toArray(new Operator[0]);
                    Operator[] rhsChildren = rhsExp.getPlan().getSuccessors(
                                    rhsExp).toArray(new Operator[0]);
                    boolean lhsDNF = lhsExp.getPlan() == rhsDNF = rhsExp.getPlan() == ;
                    int lsize = lhsChildren.lengthrsize = rhsChildren.length;
                    LogicalExpression[][] grandChildrenL = new LogicalExpression[lsize][];;
                    for (int i = 0; i < lsizei++) {
                        if (lhsChildren[iinstanceof AndExpression) {
                            grandChildrenL[i] = lhsChildren[i].getPlan().getSuccessors(
                              lhsChildren[i]).toArray(
                              new LogicalExpression[0]);
                        } else if (lhsChildren[iinstanceof DNFExpression) {
                            grandChildrenL[i] = .getSuccessors(
                              lhsChildren[i]).toArray(
                              new LogicalExpression[0]);
                        } else {
                            grandChildrenL[i] = new LogicalExpression[1];
                            grandChildrenL[i][0] = (LogicalExpressionlhsChildren[i];
                        }
                    }
                    LogicalExpression[][] grandChildrenR = new LogicalExpression[rsize][];;
                    for (int i = 0; i < rsizei++) {
                        if (rhsChildren[iinstanceof AndExpression) {
                            grandChildrenR[i] = rhsChildren[i].getPlan().getSuccessors(
                              rhsChildren[i]).toArray(
                              new LogicalExpression[0]);
                        } else if (rhsChildren[iinstanceof DNFExpression) {
                            grandChildrenR[i] = .getSuccessors(
                              rhsChildren[i]).toArray(
                              new LogicalExpression[0]);
                        } else {
                            grandChildrenR[i] = new LogicalExpression[1];
                            grandChildrenR[i][0] = (LogicalExpressionrhsChildren[i];
                        }
                    }
                    if (lhsDNF) {
                        removeDescendants(lhsExp);
                        .remove(lhsExp);
                    }
                    if (rhsDNF) {
                        removeDescendants(rhsExp);
                        .remove(rhsExp);
                    }
                    for (int i = 0; i < lsizei++)
                        for (LogicalExpression lgchild : grandChildrenL[i])
                            if (lgchild instanceof LogicalExpressionProxy)
                                ((LogicalExpressionProxylgchild).restoreSrc();
                    for (int i = 0; i < rsizei++)
                        for (LogicalExpression rgchild : grandChildrenR[i])
                            if (rgchild instanceof LogicalExpressionProxy)
                                ((LogicalExpressionProxyrgchild).restoreSrc();
                    for (int i = 0; i < lsizei++) {
                        for (int j = 0; j < rsizej++) {
                            LogicalExpression child = new DNFExpression(
                                            "dnfAnd",
                                            ..);
                            ((DNFPlan).safeAdd(child);
                            .connect(currentchild);
                            for (LogicalExpression lgchild : grandChildrenL[i]) {
                                LogicalExpressionProxy lhsClone;
                                if (lgchild instanceof LogicalExpressionProxy) {
                                    lhsClone = new LogicalExpressionProxy(
                                                    ,
                                                    ((LogicalExpressionProxylgchild).);
                                }
                                else {
                                    lhsClone = new LogicalExpressionProxy(
                                                    lgchild);
                                }
                                .add(lhsClone);
                                .connect(childlhsClone);
                            }
                            for (LogicalExpression rgchild : grandChildrenR[j]) {
                                LogicalExpressionProxy rhsClone;
                                if (rgchild instanceof LogicalExpressionProxy) {
                                    rhsClone = new LogicalExpressionProxy(
                                                    ,
                                                    ((LogicalExpressionProxyrgchild).);
                                }
                                else {
                                    rhsClone = new LogicalExpressionProxy(
                                                    ,
                                                    rgchild);
                                }
                                .add(rhsClone);
                                .connect(childrhsClone);
                            }
                        }
                    }
                }
            }
        }
    }
    private void removeDescendants(OperatorPlan planOperator op)
                    throws FrontendException {
        // remove recursively a operator and it descendants from the plan
        if (plan.getSuccessors(op) == nullreturn;
        Object[] children = plan.getSuccessors(op).toArray();
        if (children != null) {
            for (Object c : children) {
                Operator child = (Operatorc;
                removeDescendants(planchild);
                plan.disconnect(opchild);
                plan.remove(child);
            }
        }
    }
    private void mergeSimpleOr(LogicalExpression current,
                    LogicalExpression simpleLogicalExpression or,
                    boolean simpleFirstthrows FrontendException {
        Operator[] orChildren = or.getPlan().getSuccessors(or).toArray(
                        new Operator[0]);
        int size = orChildren.length;
        LogicalExpression[][] grandChildrenOr = new LogicalExpression[size][];;
        for (int i = 0; i < sizei++) {
            if (orChildren[iinstanceof DNFExpression)
              grandChildrenOr[i] = .getSuccessors(
                            orChildren[i]).toArray(
                            new LogicalExpression[0]);
            else if (orChildren[iinstanceof AndExpression)
              grandChildrenOr[i] = orChildren[i].getPlan().getSuccessors(
                  orChildren[i]).toArray(
                  new LogicalExpression[0]);
            else {
                grandChildrenOr[i] = new LogicalExpression[1];
                grandChildrenOr[i][0] = (LogicalExpressionorChildren[i];
            }
        }
        boolean simpleDNF = simple.getPlan() == orDNF = or.getPlan() == ;
        if (simpleDNF) {
            if (simple instanceof LogicalExpressionProxy)
                ((LogicalExpressionProxysimple).restoreSrc();
            .remove(simple);
        }
        if (orDNF) {
            removeDescendants(or);
            .remove(or);
        }
        for (int i = 0; i < sizei++) {
            LogicalExpression child = new DNFExpression("dnfAnd",
                            ..);
            ((DNFPlan).safeAdd(child);
            .connect(currentchild);
            LogicalExpressionProxy simpleClone;
            if (simple instanceof LogicalExpressionProxysimpleClone = new LogicalExpressionProxy(
                            ,
                            ((LogicalExpressionProxysimple).);
            else simpleClone = new LogicalExpressionProxy(simple);
            .add(simpleClone);
            if (simpleFirst.connect(childsimpleClone);
            for (Operator gchild : grandChildrenOr[i]) {
                LogicalExpression childClone;
                if (gchild instanceof LogicalExpressionProxychildClone = (LogicalExpressiongchild;
                else childClone = new LogicalExpressionProxy(,
                                (LogicalExpressiongchild);
                .add(childClone);
                .connect(childchildClone);
            }
            if (!simpleFirst.connect(childsimpleClone);
        }
    }
    private void mergeAndOr(LogicalExpression current,
                    LogicalExpression andLogicalExpression or,
                    boolean andFirstthrows FrontendException {
        Operator[] andChildren = and.getPlan().getSuccessors(and).toArray(
                        new Operator[0]);
        Operator[] orChildren = or.getPlan().getSuccessors(or).toArray(
                        new Operator[0]);
        int orSize = orChildren.length;
        int andSize = andChildren.length;
        boolean andDNF = and.getPlan() == orDNF = or.getPlan() == ;
        LogicalExpression[][] grandChildrenOr = new LogicalExpression[orSize][];;
        for (int i = 0; i < orSizei++) {
            if (orChildren[iinstanceof DNFExpression)
              grandChildrenOr[i] = .getSuccessors(
                            orChildren[i]).toArray(
                            new LogicalExpression[0]);
            else if (orChildren[iinstanceof AndExpression)
              grandChildrenOr[i] = orChildren[i].getPlan().getSuccessors(
                  orChildren[i]).toArray(
                      new LogicalExpression[0]);
            else {
                grandChildrenOr[i] = new LogicalExpression[1];
                grandChildrenOr[i][0] = (LogicalExpressionorChildren[i];
            }
        }
        for (Operator andChild : andChildren) {
            if (andChild instanceof LogicalExpressionProxy)
                ((LogicalExpressionProxyandChild).restoreSrc();
        }
        if (andDNF) {
            removeDescendants(and);
            .remove(and);
        }
        if (orDNF) {
            removeDescendants(or);
            .remove(or);
        }
        for (int i = 0; i < orSizei++) {
            LogicalExpression child = new DNFExpression("dnfAnd",
                            ..);
            ((DNFPlan).safeAdd(child);
            if (!andFirstfor (Operator gchild : grandChildrenOr[i]) {
                .connect(childgchild);
            }
            for (int j = 0; j < andSizej++) {
                LogicalExpressionProxy andChildClone;
                if (andChildren[jinstanceof LogicalExpressionProxyandChildClone = new LogicalExpressionProxy(
                                ,
                                ((LogicalExpressionProxyandChildren[j]).);
                else andChildClone = new LogicalExpressionProxy(
                                ,
                                (LogicalExpressionandChildren[j]);
                .connect(childandChildClone);
            }
            if (andFirstfor (Operator gchild : grandChildrenOr[i]) {
                .connect(childgchild);
            }
            .connect(currentchild);
        }
    }
    @Override
    public void visit(OrExpression expthrows FrontendException {
        LogicalExpression rhsExp = ((exp.getRhs() instanceof AndExpression || exp.getRhs() instanceof OrExpression ? .pop() : exp.getRhs()));
        LogicalExpression lhsExp = ((exp.getLhs() instanceof AndExpression || exp.getLhs() instanceof OrExpression ? .pop() : exp.getLhs()));
        if (!(lhsExp instanceof OrExpression) && 
            (!(lhsExp instanceof DNFExpression) || 
                ((DNFExpressionlhsExp). == ..) && !(rhsExp instanceof OrExpression) && (!(rhsExp instanceof DNFExpression) || ((DNFExpressionrhsExp). == ..)) .push(exp);
        else {
            if ( == null = new DNFPlan();
            LogicalExpression current = new DNFExpression("dnfOr",
                            ..);
            .push(current);
            ((DNFPlan).safeAdd(current);
            if (lhsExp instanceof OrExpression || (lhsExp instanceof DNFExpression && ((DNFExpressionlhsExp). == ..))
                addChildren(currentlhsExp);
            else
                .connect(currentlhsExp);
            if (rhsExp instanceof OrExpression || (rhsExp instanceof DNFExpression && ((DNFExpressionrhsExp). == ..))
                addChildren(currentrhsExp);
            else
                .connect(currentrhsExp);
        }
    }
    private void addChildren(LogicalExpression current,
                    LogicalExpression expthrows FrontendException {
        OperatorPlan childPlan = exp.getPlan();
        Operator[] children = childPlan.getSuccessors(exp).toArray(
                        new Operator[0]);
        int size = children.length;
        for (int i = 0; i < size; ++i) {
            ((DNFPlan).safeAdd(children[i]);
            .connect(currentchildren[i]);
        }
    }
New to GrepCode? Check out our FAQ X