Package org.apache.pig.impl.plan.optimizer

Examples of org.apache.pig.impl.plan.optimizer.OptimizerException


        // Look to see if this is a non-split node with two outputs.  If so
        // it matches.
        if((nodes == null) || (nodes.size() <= 0)) {
            int errCode = 2052;
            String msg = "Internal error. Cannot retrieve operator from null or empty list.";
            throw new OptimizerException(msg, errCode, PigException.BUG);
        }
        try {
            LogicalOperator op = nodes.get(0);
            List<LogicalOperator> succs = mPlan.getSuccessors(op);
            if (succs == null || succs.size() < 2) return false;
            if (op instanceof LOSplit) return false;
            if (op instanceof LOStore) return false;
            return true;
        } catch (Exception e) {
            int errCode = 2048;
            String msg = "Error while performing checks to introduce split operators.";
            throw new OptimizerException(msg, errCode, PigException.BUG, e);
        }
    }
View Full Code Here


    public void transform(List<LogicalOperator> nodes)
            throws OptimizerException {
        if((nodes == null) || (nodes.size() <= 0)) {
            int errCode = 2052;
            String msg = "Internal error. Cannot retrieve operator from null or empty list.";
            throw new OptimizerException(msg, errCode, PigException.BUG);
        }
        try {
            // Insert a split and its corresponding SplitOutput nodes into the plan
            // between node 0 and 1 / 2.
            String scope = nodes.get(0).getOperatorKey().scope;
            NodeIdGenerator idGen = NodeIdGenerator.getGenerator();
            LOSplit splitOp = new LOSplit(mPlan, new OperatorKey(scope,
                    idGen.getNextNodeId(scope)), new ArrayList<LogicalOperator>());
            splitOp.setAlias(nodes.get(0).getAlias());
            mPlan.add(splitOp);
           
            // Find all the successors and connect appropriately with split
            // and splitoutput operators.  Keep our own copy
            // of the list, as we're changing the graph by doing these calls
            // and that will change the list of predecessors.
            List<LogicalOperator> succs =
                new ArrayList<LogicalOperator>(mPlan.getSuccessors(nodes.get(0)));
            int index = -1;
            // For two successors of nodes.get(0) here is a pictorial
            // representation of the change required:
            // BEFORE:
            // Succ1  Succ2
            //  \       /
            //  nodes.get(0)
           
            //  SHOULD BECOME:
           
            // AFTER:
            // Succ1          Succ2
            //   |              |
            // SplitOutput SplitOutput
            //      \       /
            //        Split
            //          |
            //        nodes.get(0)
           
            // Here is how this will be accomplished.
            // First (the same) Split Operator will be "inserted between" nodes.get(0)
            // and all its successors. The "insertBetween" API is used which makes sure
            // the ordering of operators in the graph is preserved. So we get the following:
            // Succ1        Succ2
            //    |          |
            //   Split     Split
            //      \      / 
            //      nodes.get(0)
           
            // Then all but the first connection between nodes.get(0) and the Split
            // Operator are removed using "disconnect" - so we get the following:
            // Succ1          Succ2
            //      \       /
            //        Split
            //          |
            //        nodes.get(0)
           
            // Now a new SplitOutputOperator is "inserted between" the Split operator
            // and the successors. So we get:
            // Succ1          Succ2
            //   |              |
            // SplitOutput SplitOutput
            //      \       /
            //        Split
            //          |
            //        nodes.get(0)
           
           
            for (LogicalOperator succ : succs) {
                mPlan.insertBetween(nodes.get(0), splitOp, succ);
            }
           
            for(int i = 1; i < succs.size(); i++) {
                mPlan.disconnect(nodes.get(0), splitOp);
            }

            for (LogicalOperator succ : succs) {
                LogicalPlan condPlan = new LogicalPlan();
                LOConst cnst = new LOConst(mPlan, new OperatorKey(scope,
                        idGen.getNextNodeId(scope)), Boolean.valueOf(true));
                cnst.setType(DataType.BOOLEAN);
                condPlan.add(cnst);
                LOSplitOutput splitOutput = new LOSplitOutput(mPlan,
                        new OperatorKey(scope, idGen.getNextNodeId(scope)), ++index, condPlan);
                splitOp.addOutput(splitOutput);
                mPlan.add(splitOutput);
                mPlan.insertBetween(splitOp, splitOutput, succ);
                splitOutput.setAlias(splitOp.getAlias());
            }
           
        } catch (Exception e) {
            int errCode = 2047;
            String msg = "Internal error. Unable to introduce split operators.";
            throw new OptimizerException(msg, errCode, PigException.BUG, e);
        }
    }
View Full Code Here

        } catch (OptimizerException oe) {
            throw oe;
        } catch (Exception e) {
            int errCode = 2152;
            String msg = "Internal error while trying to check if foreach with flatten can be pushed down.";
            throw new OptimizerException(msg, errCode, PigException.BUG, e);
        }
    }
View Full Code Here

    private LogicalOperator getOperator(List<LogicalOperator> nodes)
            throws FrontendException {
        if ((nodes == null) || (nodes.size() <= 0)) {
            int errCode = 2052;
            String msg = "Internal error. Cannot retrieve operator from null or empty list.";
            throw new OptimizerException(msg, errCode, PigException.BUG);
        }

        LogicalOperator lo = nodes.get(0);
        if (lo == null || !(lo instanceof LOForEach)) {
            // we should never be called with any other operator class name
            int errCode = 2005;
            String msg = "Expected " + LOForEach.class.getSimpleName()
                    + ", got "
                    + (lo == null ? lo : lo.getClass().getSimpleName());
            throw new OptimizerException(msg, errCode, PigException.INPUT);
        } else {
            return lo;
        }

    }
View Full Code Here

                // in the new foreach operator
               
                if(mFlattenedColumnReMap == null) {
                    int errCode = 2153;
                    String msg = "Internal error. The mapping for the flattened columns is empty";
                    throw new OptimizerException(msg, errCode, PigException.BUG);
                }
               
                // set flatten to false for all columns in the mapping
               
                ArrayList<Boolean> flattenList = (ArrayList<Boolean>)foreach.getFlatten();               
                for(Integer key: mFlattenedColumnReMap.keySet()) {
                    flattenList.set(key, false);
                }
               
                // rebuild schemas of the foreach and the successor after the foreach modification
                foreach.regenerateSchema();
                successor.regenerateSchema();
               
                Schema successorSchema = successor.getSchema();
               
                if(successorSchema == null) {
                    int errCode = 2154;
                    String msg = "Internal error. Schema of successor cannot be null for pushing down foreach with flatten.";
                    throw new OptimizerException(msg, errCode, PigException.BUG);
                }
               
                flattenList = new ArrayList<Boolean>();
               
                ArrayList<LogicalPlan> foreachInnerPlans = new ArrayList<LogicalPlan>();
               
                for(int i = 0; i < successorSchema.size(); ++i) {
                    LogicalPlan innerPlan = new LogicalPlan();
                    LOProject project = new LOProject(innerPlan, OperatorKey
                            .genOpKey(foreach.getOperatorKey().scope),
                            successor, i);
                    innerPlan.add(project);
                    foreachInnerPlans.add(innerPlan);
                    flattenList.add(false);
                }
               
                // set the flattened remapped column to true
                for(Integer key: mFlattenedColumnReMap.keySet()) {
                    Integer value = mFlattenedColumnReMap.get(key);
                    flattenList.set(value, true);
                }           
               
               
                LOForEach newForeach = new LOForEach(mPlan, OperatorKey
                        .genOpKey(foreach.getOperatorKey().scope), foreachInnerPlans,
                        flattenList);
               
                // add the new foreach to the plan
                mPlan.add(newForeach);
               
                // insert the new foreach between the successor and the successor's successor
                mPlan.insertBetween(successor, newForeach, mPlan.getSuccessors(successor).get(0));            
            }
        } catch (OptimizerException oe) {
            throw oe;
        } catch (Exception e) {
            int errCode = 2155;
            String msg = "Internal error while pushing foreach with flatten down.";
            throw new OptimizerException(msg, errCode, PigException.BUG, e);
        }
    }
View Full Code Here

    @Override
    public boolean check(List<LogicalOperator> nodes) throws OptimizerException {
        if((nodes == null) || (nodes.size() <= 0)) {
            int errCode = 2052;
            String msg = "Internal error. Cannot retrieve operator from null or empty list.";
            throw new OptimizerException(msg, errCode, PigException.BUG);
        }
       
        try {
            LogicalOperator lo = nodes.get(0);
            if (lo == null || !(lo instanceof LOLimit)) {
                int errCode = 2005;
                String msg = "Expected " + LOLimit.class.getSimpleName()
                        + ", got "
                        + (lo == null ? lo : lo.getClass().getSimpleName());
                throw new OptimizerException(msg, errCode, PigException.BUG);
            }
            List<LogicalOperator> predecessors = mPlan.getPredecessors(lo);
            if (predecessors.size()!=1) {
                int errCode = 2008;
                String msg = "Limit cannot have more than one input. Found " + predecessors.size() + " inputs.";
                throw new OptimizerException(msg, errCode, PigException.BUG);
            }
            LogicalOperator predecessor = predecessors.get(0);
           
            // Limit cannot be pushed up
            if (predecessor instanceof LOCogroup || predecessor instanceof LOFilter ||
                    predecessor instanceof LOLoad || predecessor instanceof LOSplit ||
                    predecessor instanceof LODistinct || predecessor instanceof LOJoin ||
                    predecessor instanceof LOStream || predecessor instanceof LONative)
            {
                return false;
            }
            // Limit cannot be pushed in front of ForEach if it has a flatten
            if (predecessor instanceof LOForEach)
            {
                LOForEach loForEach = (LOForEach)predecessor;
                List<Boolean> mFlatten = loForEach.getFlatten();
                boolean hasFlatten = false;
                for (Boolean b:mFlatten)
                    if (b.equals(true)) hasFlatten = true;
               
                if (hasFlatten) {
                    return false;
                }
            }
        } catch (Exception e) {
            int errCode = 2049;
            String msg = "Error while performing checks to optimize limit operator.";
            throw new OptimizerException(msg, errCode, PigException.BUG);
        }

        return true;
    }
View Full Code Here

    @Override
    public void transform(List<LogicalOperator> nodes) throws OptimizerException {       
        if((nodes == null) || (nodes.size() <= 0)) {
            int errCode = 2052;
            String msg = "Internal error. Cannot retrieve operator from null or empty list.";
            throw new OptimizerException(msg, errCode, PigException.BUG);
        }
        try {
            LogicalOperator lo = nodes.get(0);
            if (lo == null || !(lo instanceof LOLimit)) {
                int errCode = 2005;
                String msg = "Expected " + LOLimit.class.getSimpleName() + ", got " + (lo == null ? lo : lo.getClass().getSimpleName());
                throw new OptimizerException(msg, errCode, PigException.BUG);
            }

            LOLimit limit = (LOLimit)lo;
           
            processNode(limit);
        } catch (OptimizerException oe) {
            throw oe;
        } catch (Exception e) {
            int errCode = 2050;
            String msg = "Internal error. Unable to optimize limit operator.";
            throw new OptimizerException(msg, errCode, PigException.BUG);
        }
    }
View Full Code Here

      try {
            List<LogicalOperator> predecessors = mPlan.getPredecessors(limit);
            if (predecessors.size()!=1) {
              int errCode = 2008;
              String msg = "Limit cannot have more than one input. Found " + predecessors.size() + " inputs.";
                throw new OptimizerException(msg, errCode, PigException.BUG);
            }
            LogicalOperator predecessor = predecessors.get(0);
           
            // Limit cannot be pushed up
            if (predecessor instanceof LOCogroup || predecessor instanceof LOFilter ||
                predecessor instanceof LOLoad || predecessor instanceof LOSplit ||
                predecessor instanceof LODistinct || predecessor instanceof LOJoin ||
                predecessor instanceof LOStream || predecessor instanceof LONative)
            {
              return;
            }
            // Limit can be pushed in front of ForEach if it does not have a flatten
            else if (predecessor instanceof LOForEach)
            {
              LOForEach loForEach = (LOForEach)predecessor;
              List<Boolean> mFlatten = loForEach.getFlatten();
              boolean hasFlatten = false;
              for (Boolean b:mFlatten)
                if (b.equals(true)) hasFlatten = true;
             
              // We can safely move LOLimit up
              if (!hasFlatten)
              {
                // Get operator before LOFilter
                LogicalOperator prepredecessor = mPlan.getPredecessors(predecessor).get(0);
                if (prepredecessor!=null)
                {
                    try {
                      mPlan.removeAndReconnect(limit);
                      insertBetween(prepredecessor, limit, predecessor, null);
                     
                    } catch (Exception e) {
                        int errCode = 2009;
                        String msg = "Can not move LOLimit up";
                      throw new OptimizerException(msg, errCode, PigException.BUG, e);
                    }
                }
                else
                {
                    int errCode = 2010;
                    String msg = "LOForEach should have one input";
                  throw new OptimizerException(msg, errCode, PigException.BUG);
                }
                    // we can move LOLimit even further, recursively optimize LOLimit
                    processNode(limit);
              }
            }
            // Limit can be duplicated, and the new instance pushed in front of an operator for the following operators
            // (that is, if you have X->limit, you can transform that to limit->X->limit):
            else if (predecessor instanceof LOCross || predecessor instanceof LOUnion)
            {
              LOLimit newLimit = null;
              List<LogicalOperator> nodesToProcess = new ArrayList<LogicalOperator>();
              for (LogicalOperator prepredecessor:mPlan.getPredecessors(predecessor))
                nodesToProcess.add(prepredecessor);
              for (LogicalOperator prepredecessor:nodesToProcess)
              {
                try {
                  newLimit = limit.duplicate();
                  insertBetween(prepredecessor, newLimit, predecessor, null);
                } catch (Exception e) {
                    int errCode = 2011;
                    String msg = "Can not insert LOLimit clone";
                  throw new OptimizerException(msg, errCode, PigException.BUG, e);
                }
                // we can move the new LOLimit even further, recursively optimize LOLimit
                processNode(newLimit);
              }
            }
            // Limit can be merged into LOSort, result a "limited sort"
            else if (predecessor instanceof LOSort)
            {
                if(mode == ExecType.LOCAL) {
                    //We don't need this optimisation to happen in the local mode.
                    //so we do nothing here.
                } else {
                    LOSort sort = (LOSort)predecessor;
                    if (sort.getLimit()==-1)
                        sort.setLimit(limit.getLimit());
                    else
                        sort.setLimit(sort.getLimit()<limit.getLimit()?sort.getLimit():limit.getLimit());
                    try {
                        mPlan.removeAndReconnect(limit);
                    } catch (Exception e) {
                        int errCode = 2012;
                        String msg = "Can not remove LOLimit after LOSort";
                        throw new OptimizerException(msg, errCode, PigException.BUG, e);
                    }
                }
            }
            // Limit is merged into another LOLimit
            else if (predecessor instanceof LOLimit)
            {
              LOLimit beforeLimit = (LOLimit)predecessor;
              beforeLimit.setLimit(beforeLimit.getLimit()<limit.getLimit()?beforeLimit.getLimit():limit.getLimit());
              try {
                mPlan.removeAndReconnect(limit);
              } catch (Exception e) {
                  int errCode = 2012;
                  String msg = "Can not remove LOLimit after LOLimit";
                throw new OptimizerException(msg, errCode, PigException.BUG, e);
              }
            }
            // Limit and OrderBy (LOSort) can be separated by split
            else if (predecessor instanceof LOSplitOutput) {              
                if(mode == ExecType.LOCAL) {
                    //We don't need this optimisation to happen in the local mode.
                    //so we do nothing here.
                } else {
                    List<LogicalOperator> grandparants = mPlan
                            .getPredecessors(predecessor);
                    // After insertion of splitters, any node in the plan can
                    // have at most one predecessor
                    if (grandparants != null && grandparants.size() != 0
                            && grandparants.get(0) instanceof LOSplit) {                       
                        List<LogicalOperator> greatGrandparants = mPlan
                                .getPredecessors(grandparants.get(0));
                        if (greatGrandparants != null
                                && greatGrandparants.size() != 0
                                && greatGrandparants.get(0) instanceof LOSort) {                          
                            LOSort sort = (LOSort)greatGrandparants.get(0);
                            LOSort newSort = new LOSort(
                                    sort.getPlan(),
                                    new OperatorKey(
                                            sort.getOperatorKey().scope,
                                            NodeIdGenerator
                                                    .getGenerator()
                                                    .getNextNodeId(
                                                            sort.getOperatorKey().scope)),
                                    sort.getSortColPlans(),
                                    sort.getAscendingCols(),
                                    sort.getUserFunc());
                                                 
                            newSort.setLimit(limit.getLimit());
                            try {
                                mPlan.replace(limit, newSort);
                            } catch (PlanException e) {
                                int errCode = 2012;
                                String msg = "Can not replace LOLimit with LOSort after splitter";
                                throw new OptimizerException(msg, errCode, PigException.BUG, e);
                            }
                        }
                    }
                }
            }
            else {
                int errCode = 2013;
                String msg = "Moving LOLimit in front of " + predecessor.getClass().getSimpleName() + " is not implemented";
              throw new OptimizerException(msg, errCode, PigException.BUG);
            }
      } catch (OptimizerException oe) {
          throw oe;
        }
    }
View Full Code Here

        } catch(OptimizerException oe) {
            throw oe;
        } catch (Exception e) {
            int errCode = 2004;
            String msg = "Internal error while trying to check if type casts are needed";
            throw new OptimizerException(msg, errCode, PigException.BUG, e);
        }
    }
View Full Code Here

   
    private LogicalOperator getOperator(List<LogicalOperator> nodes) throws FrontendException {
        if((nodes == null) || (nodes.size() <= 0)) {
            int errCode = 2052;
            String msg = "Internal error. Cannot retrieve operator from null or empty list.";
            throw new OptimizerException(msg, errCode, PigException.BUG);
        }
       
        LogicalOperator lo = nodes.get(0);
        if(LOLoad.class.getName().equals(operatorClassName)) {
            if (lo == null || !(lo instanceof LOLoad)) {
                int errCode = 2005;
                String msg = "Expected " + LOLoad.class.getSimpleName()
                        + ", got "
                        + (lo == null ? lo : lo.getClass().getSimpleName());
                throw new OptimizerException(msg, errCode, PigException.BUG);
            }
   
            return lo;
        } else if(LOStream.class.getName().equals(operatorClassName)){
            if (lo == null || !(lo instanceof LOStream)) {
                int errCode = 2005;
                String msg = "Expected " + LOStream.class.getSimpleName()
                        + ", got "
                        + (lo == null ? lo : lo.getClass().getSimpleName());
                throw new OptimizerException(msg, errCode, PigException.BUG);
            }
   
            return lo;
        } else {
            // we should never be called with any other operator class name
            int errCode = 1034;
            String msg = "TypeCastInserter invoked with an invalid operator class name:" + operatorClassName;
            throw new OptimizerException(msg, errCode, PigException.INPUT);
        }
  
    }
View Full Code Here

TOP

Related Classes of org.apache.pig.impl.plan.optimizer.OptimizerException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.