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;
 
 import java.util.List;
 import java.util.Set;
 
A NOT conversion visitor that will traverse the expression tree in a depth-first manner with post-order handling. A status of negativity for a NOT expression is recorded in the depth-first traversal before subtree traversal and reversed after traversing the subtree. All "reversible" expressions is replaced by its negated counterpart for negative negativity. Currently equality op, and its non-equality counter part, all range comparisons, logical AND and OR are reversible. Notably missing is the "is null" for lack of a "is not null" base expression; UDF and regex are not reversed too for the same sake.
 
     boolean not = false;
 
         super(plannew NotConversionWalker(plan));
     }
 
     public void flip() {
          = !;
     }
 
     private void reset(Operator newOpOperator oldOp)
                     throws FrontendException {
         List<Operatorp = .getPredecessors(oldOp);
         if (p != null) {
             Operator[] preds = p.toArray(new Operator[0]);
             for (Operator pred : preds) {
                 Pair<IntegerIntegerpos = .disconnect(predoldOp);
                 .connect(predpos.firstnewOppos.second);
             }
         }
         List<Operators = .getSuccessors(oldOp);
         if (s != null) {
             Operator[] sucs = s.toArray(new Operator[0]);
             for (Operator suc : sucs) {
                 .disconnect(oldOpsuc);
             }
         }
         .remove(oldOp);
     }
 
     private void insert(Operator beforeOperator after)
                     throws FrontendException {
         // assume after is already connected to before
         List<Operatorp = .getPredecessors(after);
         if (p != null) {
             Operator[] preds = p.toArray(new Operator[0]);
             for (Operator pred : preds) {
                 if (pred != before) {
                     Pair<IntegerIntegerpos = .disconnect(predafter);
                     .connect(predpos.firstbeforepos.second);
                 }
             }
         }
     }
 
     private void remove(Operator opthrows FrontendException {
         List<Operatorp = .getPredecessors(op);
         if (p != null) {
             Operator[] preds = p.toArray(new Operator[0]);
             for (Operator pred : preds) {
                 Pair<IntegerIntegerpos = .disconnect(predop);
                 List<Operators = .getSuccessors(op);
                 if (s != null) {
                    Operator[] sucs = s.toArray(new Operator[0]);
                    for (int i = 0;  i < sucs.lengthi++)
                        .connect(predpos.first+isucs[i], pos.second+i);
                }
            }
        }
        List<Operators = .getSuccessors(op);
        if (s != null) {
            Operator[] sucs = s.toArray(new Operator[0]);
            for (Operator suc : sucs) {
                .disconnect(opsuc);
            }
        }
        .remove(op);
    }
    @Override
    public void visit(NotExpression notthrows FrontendException {
        remove(not);
    }
    @Override
    public void visit(AndExpression andExprthrows FrontendException {
        if () {
            // Application of DeMorgan's Law to AND
            LogicalExpression newExp = new OrExpression(,
                            andExpr.getLhs(), andExpr.getRhs());
            reset(newExpandExpr);
        }
    }
    @Override
    public void visit(OrExpression orExprthrows FrontendException {
        if () {
            // Application of DeMorgan's Law to OR
            LogicalExpression newExp = new AndExpression(,
                            orExpr.getLhs(), orExpr.getRhs());
            reset(newExporExpr);
        }
    }
    @Override
    public void visit(EqualExpression equalthrows FrontendException {
        if () {
            // Application of DeMorgan's Law to OR
            LogicalExpression newExp = new NotEqualExpression(,
                            equal.getLhs(), equal.getRhs());
            reset(newExpequal);
        }
    }
    @Override
    public void visit(NotEqualExpression opthrows FrontendException {
        if () {
            // Application of DeMorgan's Law to OR
            LogicalExpression newExp = new EqualExpression(,
                            op.getLhs(), op.getRhs());
            reset(newExpop);
        }
    }
    @Override
    public void visit(IsNullExpression opthrows FrontendException {
        if () {
            LogicalExpression newExp = new NotExpression(op);
            insert(newExpop);
        }
    }
    @Override
    public void visit(RegexExpression opthrows FrontendException {
        if () {
            LogicalExpression newExp = new NotExpression(op);
            insert(newExpop);
        }
    }
    
    @Override
    public void visit(UserFuncExpression opthrows FrontendException {
        if () {
            LogicalExpression newExp = new NotExpression(op);
            insert(newExpop);
        }
    }
    
    @Override
    public void visit(GreaterThanExpression greaterThanExpression)
                    throws FrontendException {
        if () {
            reset(new LessThanEqualExpression(,
                            greaterThanExpression.getLhs(),
                            greaterThanExpression.getRhs()),
                            greaterThanExpression);
        }
    }
    @Override
    public void visit(
                    GreaterThanEqualExpression greaterThanEqualExpression)
                    throws FrontendException {
        if () {
            reset(new LessThanExpression(,
                            greaterThanEqualExpression.getLhs(),
                            greaterThanEqualExpression.getRhs()),
                            greaterThanEqualExpression);
        }
    }
    @Override
    public void visit(LessThanExpression lessThanExpression)
                    throws FrontendException {
        if () {
            reset(new GreaterThanEqualExpression(,
                            lessThanExpression.getLhs(),
                            lessThanExpression.getRhs()),
                            lessThanExpression);
        }
    }
    @Override
    public void visit(LessThanEqualExpression lessThanEqualExpression)
                    throws FrontendException {
        if () {
            reset(new GreaterThanExpression(,
                            lessThanEqualExpression.getLhs(),
                            lessThanEqualExpression.getRhs()),
                            lessThanEqualExpression);
        }
    }
    private static class NotConversionWalker extends PlanWalker {
    
        public NotConversionWalker(OperatorPlan plan) {
            super(plan);
        }
    
        @Override
        public PlanWalker spawnChildWalker(OperatorPlan plan) {
            return new NotConversionWalker(plan);
        }
    
        
Begin traversing the graph.

Parameters:
visitor Visitor this walker is being used by.
Throws:
org.apache.pig.impl.logicalLayer.FrontendException if an error is encountered while walking.
        @Override
        public void walk(PlanVisitor visitorthrows FrontendException {
            List<Operatorroots = .getSources();
            Set<Operatorseen = new HashSet<Operator>();
    
            depthFirst(nullrootsseenvisitor);
        }
    
        private void depthFirst(Operator node,
                        Collection<Operatorsuccessors,
                        Set<OperatorseenPlanVisitor visitor)
                        throws FrontendException {
            if (successors == nullreturn;
    
            Operator[] sucs = successors.toArray(new Operator[0]);
            for (Operator suc : sucs) {
                if (seen.add(suc)) {
                    if (suc instanceof NotExpression)
                        ((NOTConversionVisitorvisitor).flip();
                    if(suc instanceof AndExpression
                            || suc instanceof NotExpression
                            || suc instanceof OrExpression
                    ){
                        //visit successors of suc only if they are the boolean operators
                        // the NOT conversion should be propagated only for 
                        // their successors 
                        Collection<OperatornewSuccessors = Utils.mergeCollection(.getSuccessors(suc), .getSoftLinkSuccessors(suc));
                        depthFirst(sucnewSuccessorsseenvisitor);
                    }
                    suc.accept(visitor);
                    if (suc instanceof NotExpression)
                        ((NOTConversionVisitorvisitor).flip();
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X