Package org.apache.pig.impl.logicalLayer

Examples of org.apache.pig.impl.logicalLayer.LogicalOperator


            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 {
View Full Code Here


    }

    @Override
    public void transform(List<LogicalOperator> nodes) throws OptimizerException {
        try {
            LogicalOperator lo = getOperator(nodes);
            Schema s = lo.getSchema();
            String scope = lo.getOperatorKey().scope;
            // For every field, build a logical plan.  If the field has a type
            // other than byte array, then the plan will be cast(project).  Else
            // it will just be project.
            ArrayList<LogicalPlan> genPlans = new ArrayList<LogicalPlan>(s.size());
            ArrayList<Boolean> flattens = new ArrayList<Boolean>(s.size());
            Map<String, Byte> typeChanges = new HashMap<String, Byte>();
            // if we are inserting casts in a load and if the loader
            // implements determineSchema(), insert casts only where necessary
            // Note that in this case, the data coming out of the loader is not
            // a BYTEARRAY but is whatever determineSchema() says it is.
            Schema determinedSchema = null;
            if(LOLoad.class.getName().equals(operatorClassName)) {
                determinedSchema = ((LOLoad)lo).getDeterminedSchema();
            }
            for (int i = 0; i < s.size(); i++) {
                LogicalPlan p = new LogicalPlan();
                genPlans.add(p);
                flattens.add(false);
                List<Integer> toProject = new ArrayList<Integer>(1);
                toProject.add(i);
                LOProject proj = new LOProject(p, OperatorKey.genOpKey(scope),
                    lo, toProject);
                p.add(proj);
                Schema.FieldSchema fs = s.getField(i);
                if (fs.type != DataType.BYTEARRAY) {
                    if(determinedSchema == null || (fs.type != determinedSchema.getField(i).type)) {
                            // Either no schema was determined by loader OR the type
                            // from the "determinedSchema" is different
                            // from the type specified - so we need to cast
                            LOCast cast = new LOCast(p,
                                        OperatorKey.genOpKey(scope), fs.type);
                            cast.setFieldSchema(fs);
                            p.add(cast);
                            p.connect(proj, cast);
                           
                            cast.setFieldSchema(fs.clone());
                            FuncSpec loadFuncSpec = null;
                            if(lo instanceof LOLoad) {
                                loadFuncSpec = ((LOLoad)lo).getInputFile().getFuncSpec();
                            } else if (lo instanceof LOStream) {
                                StreamingCommand command = ((LOStream)lo).getStreamingCommand();
                                HandleSpec streamOutputSpec = command.getOutputSpec();
                                loadFuncSpec = new FuncSpec(streamOutputSpec.getSpec());
                            } else {
                                int errCode = 2006;
                                String msg = "TypeCastInserter invoked with an invalid operator class name: " + lo.getClass().getSimpleName();
                                throw new OptimizerException(msg, errCode, PigException.BUG);
                            }
                            cast.setLoadFuncSpec(loadFuncSpec);
                            typeChanges.put(fs.canonicalName, fs.type);
                            if(determinedSchema == null) {
                                // Reset the loads field schema to byte array so that it
                                // will reflect reality.
                                fs.type = DataType.BYTEARRAY;
                            } else {
                                // Reset the type to what determinedSchema says it is
                                fs.type = determinedSchema.getField(i).type;
                            }
                        }
                }
            }

            // Build a foreach to insert after the load, giving it a cast for each
            // position that has a type other than byte array.
            LOForEach foreach = new LOForEach(mPlan,
                OperatorKey.genOpKey(scope), genPlans, flattens);
            foreach.setAlias(lo.getAlias());
            // Insert the foreach into the plan and patch up the plan.
            insertAfter(lo, foreach, null);

            rebuildSchemas();
View Full Code Here

            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)
View Full Code Here

            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;
           
View Full Code Here

            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)
            {
              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);
View Full Code Here

                List<LogicalOperator> listOp = lp.getSuccessors(op);
               
                if(null != listOp) {
                    Iterator<LogicalOperator> iter = listOp.iterator();
                    while(iter.hasNext()) {
                        LogicalOperator lop = iter.next();
                        System.err.println("Successor: " + lop.getClass().getName() + " object " + lop);
                    }
                }
            }
           
            assertTrue(lp != null);
View Full Code Here

            // return false
            if (predecessors.size() == 0 || predecessors.size() > 1) {
                return false;
            }
               
            LogicalOperator predecessor = predecessors.get(0);

            // if the predecessor is one of LOLoad/LOStore/LOStream/LOLimit
            // return false
            if (predecessor instanceof LOLoad || predecessor instanceof LOStore
                    || predecessor instanceof LOStream
                    || predecessor instanceof LOLimit) {
                return false;
            }
           
            // TODO
            // for now filters cannot be combined
            // remove this check when filters can be combined
            if (predecessor instanceof LOFilter)
                return false;

            // TODO
            // same rule as filters
            if (predecessor instanceof LOSplitOutput) {
                return false;
            }
            if (predecessor instanceof LOSplit) {
                return false;
            }

            UDFFinder udfFinder = new UDFFinder(filter.getComparisonPlan());
            udfFinder.visit();

            // if the filter's inner plan contains any UDF then return false
            if (udfFinder.foundAnyUDF()) {
                return false;
            }

            CastFinder castFinder = new CastFinder(filter.getComparisonPlan());
            castFinder.visit();

            // if the filter's inner plan contains any casts then return false
            if (castFinder.foundAnyCast()) {
                return false;
            }

            List<RequiredFields> filterRequiredFields = filter
                    .getRequiredFields();
            if (filterRequiredFields == null) {
                return false;
            }
            RequiredFields requiredField = filterRequiredFields.get(0);

            // the filter's conditions contain constant expression
            // return false
            if (requiredField.needNoFields()) {
                return false;
            }

            // if the predecessor is a multi-input operator then detailed
            // checks are required
            if (predecessor instanceof LOCross
                    || predecessor instanceof LOUnion
                    || predecessor instanceof LOCogroup
                    || predecessor instanceof LOJoin) {

                // check if the filter's required fields in conjunction with the
                // predecessor's projection map. If the filter needs more than
                // one input then the filter's expressions have to be split

                List<LogicalOperator> grandParents = mPlan
                        .getPredecessors(predecessor);

                // if the predecessor does not have predecessors return false
                if (grandParents == null || grandParents.size() == 0) {
                    return false;
                }
               
                // check if the predecessor is a group by
                if (grandParents.size() == 1) {
                    if (predecessor instanceof LOCogroup) {
                        mSwap = true;
                        return true;
                    } else {
                        // only a group by can have a single input
                        return false;
                    }
                }

                if (requiredField.needAllFields()) {
                    return false;
                }

                Pair<Boolean, Set<Integer>> mappingResult = isRequiredFieldMapped(requiredField, predecessor.getProjectionMap());
                boolean mapped = mappingResult.first;
                Set<Integer> grandParentIndexes = mappingResult.second;
                if (!mapped) {
                    return false;
                }
               
                // TODO
                // the filter's conditions requires more than one input of its
                // predecessor
                // when the filter's conditions are splittable return true
                if ((grandParentIndexes == null)
                        || (grandParentIndexes.size() == 0)
                        || (grandParentIndexes.size() > 1)) {
                    return false;
                }

                if (predecessor instanceof LOCogroup) {
                    // check for outer
                    if (isAnyOuter((LOCogroup) predecessor)) {
                        return false;
                    }
                }
               
                mPushBeforeInput = grandParentIndexes.iterator().next();
               
                if (predecessor instanceof LOJoin) {
                    boolean otherBranchContainOuter = false;
                    boolean sawInner = false;
                    for (int i=0;i<=mPlan.getSuccessors(predecessor).size();i++) {
                        // We do not push filter if any other branch is outer
                        // See PIG-1289
                        // Also in LOJoin, innerFlag==true indicate that branch is the outer join side
                        // which has the exact opposite semantics
                        // If all innerFlag is true, that implies a regular join
                        if (i!=mPushBeforeInput && ((LOJoin)predecessor).getInnerFlags()[i]) {
                            otherBranchContainOuter = true;
                        }
                        if (((LOJoin)predecessor).getInnerFlags()[i]==false) {
                            sawInner = true;
                        }
                    }
                    if (otherBranchContainOuter && sawInner) {
                        mPushBeforeInput = -1;
                        return false;
                    }
                }
               
                mPushBefore = true;
                return true;

            } 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;
                    }
                }

                // TODO
                // A better check is to examine each column in the filter's
                // required fields. If the column is the result of a flatten
                // then
                // return false else return true

                // for now if the foreach has a flatten then return false
                if (hasFlatten) {
                    return false;
                }

                Pair<Boolean, Set<Integer>> mappingResult = isRequiredFieldMapped(requiredField, predecessor.getProjectionMap());
                boolean mapped = mappingResult.first;
               
                // Check if it is a direct mapping, that is, project optionally followed by cast, so if project->project, it is not
                // considered as a mapping
                for (Pair<Integer, Integer> pair : requiredField.getFields())
View Full Code Here

            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 LOFilter)) {
            // we should never be called with any other operator class name
            int errCode = 2005;
            String msg = "Expected " + LOFilter.class.getSimpleName()
                    + ", got "
                    + (lo == null ? lo : lo.getClass().getSimpleName());
            throw new OptimizerException(msg, errCode, PigException.INPUT);
        } else {
            return lo;
        }
View Full Code Here

    @Override
    public void transform(List<LogicalOperator> nodes)
            throws OptimizerException {
        try {
            LOFilter filter = (LOFilter) getOperator(nodes);
            LogicalOperator predecessor = mPlan.getPredecessors(filter).get(0);
            if (mSwap) {
                mPlan.swap(predecessor, filter);
            } else if (mPushBefore) {
                if (mPushBeforeInput == -1) {
                    // something is wrong!
View Full Code Here

        if (projectFinder.getProjectSet()!=null && projectFinder.getProjectSet().size()==1)
        {
            LOProject project = projectFinder.getProjectSet().iterator().next();
            if (lp.getPredecessors(project)==null)
            {
                LogicalOperator pred = project;
                while (lp.getSuccessors(pred)!=null)
                {
                    if (lp.getSuccessors(pred).size()!=1)
                        return false;
                    if (!(lp.getSuccessors(pred).get(0) instanceof LOCast))
View Full Code Here

TOP

Related Classes of org.apache.pig.impl.logicalLayer.LogicalOperator

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.