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.expression;
 
 import java.util.List;
 import java.util.Map;
 
 
 
     // This value points to the current LogicalRelationalOperator we are working on
     protected LogicalRelationalOperator currentOp;
     
             Map<OperatorPhysicalOperatormapthrows FrontendException {
         this(plannew DependencyOrderWalker(plan), opphyPlanmap);
     }
     
     public ExpToPhyTranslationVisitor(OperatorPlan planPlanWalker walkerLogicalRelationalOperator opPhysicalPlan phyPlan
             Map<OperatorPhysicalOperatormapthrows FrontendException {
         super(planwalker);
          = op;
          = map;
          = phyPlan;
          = new LinkedList<PhysicalPlan>();
     }
     
     protected Map<OperatorPhysicalOperatorlogToPhyMap;
 
     protected Deque<PhysicalPlancurrentPlans;
 
    protected PhysicalPlan currentPlan;
    protected NodeIdGenerator nodeGen = NodeIdGenerator.getGenerator();
    protected PigContext pc;
    
    public void setPigContext(PigContext pc) {
        this. = pc;
    }
    public PhysicalPlan getPhysicalPlan() {
        return ;
    }
    
    private void attachBinaryComparisonOperatorBinaryExpression op
            BinaryComparisonOperator exprOp ) throws FrontendException {
        // We dont have aliases in ExpressionOperators
        // exprOp.setAlias(op.getAlias());
        
        
        exprOp.setOperandType(op.getLhs().getType());
        exprOp.setLhs((ExpressionOperator.get(op.getLhs()));
        exprOp.setRhs((ExpressionOperator.get(op.getRhs()));
        OperatorPlan oPlan = op.getPlan();
        .add(exprOp);
        .put(opexprOp);
        List<Operatorsuccessors = oPlan.getSuccessors(op);
        if (successors == null) {
            return;
        }
        for (Operator lo : successors) {
            PhysicalOperator from = .get(lo);
            try {
                .connect(fromexprOp);
            } catch (PlanException e) {
                int errCode = 2015;
                String msg = "Invalid physical operators in the physical plan" ;
                throw new LogicalToPhysicalTranslatorException(msgerrCode.e);
            }
        }
    }
    
    private void attachBinaryExpressionOperatorBinaryExpression op
            BinaryExpressionOperator exprOp ) throws FrontendException {
        // We dont have aliases in ExpressionOperators
        // exprOp.setAlias(op.getAlias());
        
        
        exprOp.setResultType(op.getLhs().getType());
        exprOp.setLhs((ExpressionOperator.get(op.getLhs()));
        exprOp.setRhs((ExpressionOperator.get(op.getRhs()));
        OperatorPlan oPlan = op.getPlan();
        .add(exprOp);
        .put(opexprOp);
        List<Operatorsuccessors = oPlan.getSuccessors(op);
        if (successors == null) {
            return;
        }
        for (Operator lo : successors) {
            PhysicalOperator from = .get(lo);
            try {
                .connect(fromexprOp);
            } catch (PlanException e) {
                int errCode = 2015;
                String msg = "Invalid physical operators in the physical plan" ;
                throw new LogicalToPhysicalTranslatorException(msgerrCode.e);
            }
        }
    }
    @Override
    public void visitAndExpression op ) throws FrontendException {
        
//        System.err.println("Entering And");
        
        attachBinaryComparisonOperator(opexprOp);
    }
    
    @Override
    public void visitOrExpression op ) throws FrontendException {
        
//        System.err.println("Entering Or");
        
        attachBinaryComparisonOperator(opexprOp);
    }
    
    @Override
    public void visitEqualExpression op ) throws FrontendException {
        
        BinaryComparisonOperator exprOp = new EqualToExpr(new OperatorKey(
                .getNextNodeId()));
        
        attachBinaryComparisonOperator(opexprOp);
    }
    
    @Override
    public void visitNotEqualExpression op ) throws FrontendException {
        
        BinaryComparisonOperator exprOp = new NotEqualToExpr(new OperatorKey(
                .getNextNodeId()));
        
        attachBinaryComparisonOperator(opexprOp);
    }
    
    @Override
    public void visitGreaterThanExpression op ) throws FrontendException {
        
        BinaryComparisonOperator exprOp = new GreaterThanExpr(new OperatorKey(
                .getNextNodeId()));
        
        attachBinaryComparisonOperator(opexprOp);
    }
    
    @Override
    public void visitGreaterThanEqualExpression op ) throws FrontendException {
        
        BinaryComparisonOperator exprOp = new GTOrEqualToExpr(new OperatorKey(
                .getNextNodeId()));
        
        attachBinaryComparisonOperator(opexprOp);
    }
    
    @Override
    public void visitLessThanExpression op ) throws FrontendException {
        
        BinaryComparisonOperator exprOp = new LessThanExpr(new OperatorKey(
                .getNextNodeId()));
        
        attachBinaryComparisonOperator(opexprOp);
    }
    
    
    @Override
    public void visitLessThanEqualExpression op ) throws FrontendException {
        
        BinaryComparisonOperator exprOp = new LTOrEqualToExpr(new OperatorKey(
                .getNextNodeId()));
        
        attachBinaryComparisonOperator(opexprOp);
    }
    
    @Override
    public void visit(ProjectExpression opthrows FrontendException {
        POProject exprOp;
       
        if(op.getAttachedRelationalOp() instanceof LOGenerate && op.getPlan().getSuccessors(op)==null &&
            !(op.findReferent() instanceof LOInnerLoad)) {
            exprOp = new PORelationToExprProject(new OperatorKey(
                .getNextNodeId()));
         } else {
            exprOp = new POProject(new OperatorKey(
                .getNextNodeId()));
        }
        
        if (op.getFieldSchema()==null && op.isRangeOrStarProject())
            exprOp.setResultType(.);
        else
            exprOp.setResultType(op.getType());
        if(op.isProjectStar()){
            exprOp.setStar(op.isProjectStar());
        }
        else if(op.isRangeProject()){
            if(op.getEndCol() != -1){
                //all other project-range should have been expanded by
                // project-star expander
                throw new AssertionError("project range that is not a " +
                "project-to-end seen in translation to physical plan!");
            }
            exprOp.setProjectToEnd(op.getStartCol());
        }else {
            exprOp.setColumn(op.getColNum());
        }
        // TODO implement this
//        exprOp.setOverloaded(op.getOverloaded());
        .put(opexprOp);
        .add(exprOp);        
    }
    
    @Override
    public void visitMapLookupExpression op ) throws FrontendException {
        ExpressionOperator physOp = new POMapLookUp(new OperatorKey(,
                .getNextNodeId()));
        ((POMapLookUp)physOp).setLookUpKey(op.getLookupKey() );
        physOp.setResultType(op.getType());
        physOp.addOriginalLocation(op.getFieldSchema().op.getLocation());
        .add(physOp);
        .put(opphysOp);
                .getMap());
        try {
            .connect(fromphysOp);
        } catch (PlanException e) {
            int errCode = 2015;
            String msg = "Invalid physical operators in the physical plan" ;
            throw new LogicalToPhysicalTranslatorException(msgerrCode.e);
        }
    }
    
    @Override
        
//        System.err.println("Entering Constant");
                .getNextNodeId()));
        // We dont have aliases in ExpressionOperators
        // ce.setAlias(op.getAlias());
        ce.setValue(op.getValue());
        ce.setResultType(op.getType());
        //this operator doesn't have any predecessors
        .add(ce);
        .put(opce);
//        System.err.println("Exiting Constant");
    }
    
    @Override
    public void visitCastExpression op ) throws FrontendException {
        POCast pCast = new POCast(new OperatorKey(
                .getNextNodeId()));
//        physOp.setAlias(op.getAlias());
        .add(pCast);
        .put(oppCast);
                .getExpression());
        pCast.setResultType(op.getType());
        FuncSpec lfSpec = op.getFuncSpec();
        if(null != lfSpec) {
            try {
                pCast.setFuncSpec(lfSpec);
            } catch (IOException e) {
                int errCode = 1053;
                String msg = "Cannot resolve load function to use for casting" +
                        " from " + DataType.findTypeName(op.getExpression().
                                getType()) + " to " + DataType.findTypeName(op.getType());
                throw new LogicalToPhysicalTranslatorException(msgerrCode.e);
            }
        }
        try {
            .connect(frompCast);
        } catch (PlanException e) {
            int errCode = 2015;
            String msg = "Invalid physical operators in the physical plan" ;
            throw new LogicalToPhysicalTranslatorException(msgerrCode.e);
        }
    }
    
    @Override
    public void visitNotExpression op ) throws FrontendException {
        
        PONot pNot = new PONot(new OperatorKey(
                .getNextNodeId()));
//        physOp.setAlias(op.getAlias());
        .add(pNot);
        .put(oppNot);
                .getExpression());
        pNot.setExpr(from);
        pNot.setResultType(op.getType());
        pNot.setOperandType(op.getType());
        try {
            .connect(frompNot);
        } catch (PlanException e) {
            int errCode = 2015;
            String msg = "Invalid physical operators in the physical plan" ;
            throw new LogicalToPhysicalTranslatorException(msgerrCode.e);
        }
    }
    
    @Override
    public void visitIsNullExpression op ) throws FrontendException {
        POIsNull pIsNull = new POIsNull(new OperatorKey(
                .getNextNodeId()));
//        physOp.setAlias(op.getAlias());
        .add(pIsNull);
        .put(oppIsNull);
                .getExpression());
        pIsNull.setExpr(from);
        pIsNull.setResultType(op.getType());
        pIsNull.setOperandType(op.getExpression().getType());
        try {
            .connect(frompIsNull);
        } catch (PlanException e) {
            int errCode = 2015;
            String msg = "Invalid physical operators in the physical plan" ;
            throw new LogicalToPhysicalTranslatorException(msgerrCode.e);
        }
    }
    @Override
    public void visitNegativeExpression op ) throws FrontendException {
        PONegative pNegative = new PONegative(new OperatorKey(
                .getNextNodeId()));
//        physOp.setAlias(op.getAlias());
        .add(pNegative);
        .put(oppNegative);
                .getExpression());
        pNegative.setExpr(from);
        pNegative.setResultType(op.getType());        
        try {
            .connect(frompNegative);
        } catch (PlanException e) {
            int errCode = 2015;
            String msg = "Invalid physical operators in the physical plan" ;
            throw new LogicalToPhysicalTranslatorException(msgerrCode.e);
        }
    }
    
    @Override
    public void visitAddExpression op ) throws FrontendException {        
        BinaryExpressionOperator exprOp = new Add(new OperatorKey(.getNextNodeId()));        
        
        attachBinaryExpressionOperator(opexprOp);
    }
    
    @Override
    public void visitRegexExpression op ) throws FrontendException {        
        BinaryExpressionOperator exprOp = new PORegexp(new OperatorKey(.getNextNodeId()));        
        
        attachBinaryExpressionOperator(opexprOp);
        
        List<Operatorsuccessors = op.getPlan().getSuccessors(op);
        if (successors.get(1) instanceof org.apache.pig.newplan.logical.expression.ConstantExpression) {
            ((PORegexp)exprOp).setConstExpr(true);
        }
    }
    
    @Override
    public void visitSubtractExpression op ) throws FrontendException {        
        BinaryExpressionOperator exprOp = new Subtract(new OperatorKey(.getNextNodeId()));        
        
        attachBinaryExpressionOperator(opexprOp);
    }
    
    @Override
    public void visitMultiplyExpression op ) throws FrontendException {        
        BinaryExpressionOperator exprOp = new Multiply(new OperatorKey(.getNextNodeId()));        
        
        attachBinaryExpressionOperator(opexprOp);
    }
    
    @Override
    public void visitDivideExpression op ) throws FrontendException {        
        BinaryExpressionOperator exprOp = new Divide(new OperatorKey(.getNextNodeId()));        
        
        attachBinaryExpressionOperator(opexprOp);
    }
    
    @Override
    public void visitModExpression op ) throws FrontendException {        
        BinaryExpressionOperator exprOp = new Mod(new OperatorKey(.getNextNodeId()));        
        
        attachBinaryExpressionOperator(opexprOp);
    }
    
    @Override
    public void visitBinCondExpression op ) throws FrontendException {
        
        POBinCond exprOp = new POBinCondnew OperatorKey(,
                .getNextNodeId()) );
        
        exprOp.setResultType(op.getType());
        exprOp.setCond((ExpressionOperator.get(op.getCondition()));
        exprOp.setLhs((ExpressionOperator.get(op.getLhs()));
        exprOp.setRhs((ExpressionOperator.get(op.getRhs()));
        OperatorPlan oPlan = op.getPlan();
        .add(exprOp);
        .put(opexprOp);
        List<Operatorsuccessors = oPlan.getSuccessors(op);
        if (successors == null) {
            return;
        }
        for (Operator lo : successors) {
            PhysicalOperator from = .get(lo);
            try {
                .connect(fromexprOp);
            } catch (PlanException e) {
                int errCode = 2015;
                String msg = "Invalid physical operators in the physical plan" ;
                throw new LogicalToPhysicalTranslatorException(msgerrCode.e);
            }
        }
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public void visitUserFuncExpression op ) throws FrontendException {       
        Object f = PigContext.instantiateFuncFromSpec(op.getFuncSpec());
        PhysicalOperator p;
        if (f instanceof EvalFunc) {
            p = new POUserFunc(new OperatorKey(
                    .getNextNodeId()), -1,
                    nullop.getFuncSpec(), (EvalFuncf);
            ((POUserFunc)p).setSignature(op.getSignature());
            List<StringcacheFiles = ((EvalFunc)f).getCacheFiles();
            if (cacheFiles != null) {
                ((POUserFunc)p).setCacheFiles(cacheFiles.toArray(new String[cacheFiles.size()]));
            }
        } else {
            p = new POUserComparisonFunc(new OperatorKey(
                    .getNextNodeId()), -1,
                    nullop.getFuncSpec(), (ComparisonFuncf);
        }
        p.setResultType(op.getType());
        .add(p);
        List<LogicalExpressionfromList = op.getArguments();
        if(fromList!=null){
            for (LogicalExpression inputOperator : fromList) {
                PhysicalOperator from = .get(inputOperator);
                try {
                    .connect(fromp);
                } catch (PlanException e) {
                    int errCode = 2015;
                    String msg = "Invalid physical operators in the physical plan" ;
                    throw new LogicalToPhysicalTranslatorException(msgerrCode.e);
                }
            }
        }
        .put(opp);
        
        //We need to track all the scalars
        ifop instanceof ScalarExpression ) {
            Operator refOp = ((ScalarExpression)op).getImplicitReferencedOperator();
            ((POUserFunc)p).setReferencedOperator.getrefOp ) );
        }
    }
    
    @Override
    public void visitDereferenceExpression op ) throws FrontendException {
        POProject exprOp = new POProject(new OperatorKey(
                .getNextNodeId()));
        exprOp.setResultType(op.getType());
        exprOp.setColumns((ArrayList<Integer>)op.getBagColumns());        
        exprOp.setStar(false);
        .put(opexprOp);
        .add(exprOp);
        
        PhysicalOperator from = .getop.getReferredExpression() );
        
        iffrom != null ) {
            .connect(fromexprOp);
        }
    }
New to GrepCode? Check out our FAQ X