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;
 
a constant expression evaluation visitor that will evaluate the constant expressions. It works by traversing the expression tree in a bottom-up manner and evaluate all subexpressions that have all constant subexpressions. All results from constant children are pushed to a stack for the parent to digest for its own evaluation. Any non-constant expression will push a null to the stack and consequently will cause all of its ancestors not to be evaluated. For simplicity, only constant binary expressions and constant unary expressions are evaluated. More complex expressions are not evaluated at this moment. For UDF, this evaluation is not planned at all for fear of possible stateful consequences resulting from the original evaluations
 
 
         super(plannew ReverseDependencyOrderWalker(plan));
          = new LinkedList<ConstantExpression>();
     }
 
     @SuppressWarnings("unchecked")
     private void binaryExpressionConstPrune(LogicalExpression parent)
                     throws FrontendException {
         ConstantExpression rhs = .pop(), lhs = .pop();
         if (rhs == null || lhs == null) {
             .push(null);
         }
         else {
             ConstantExpression newExp = null;
             if (parent instanceof AndExpressionnewExp = new ConstantExpression(
                             ,
                             (Booleanrhs.getValue() && (Booleanlhs.getValue());
             else if (parent instanceof OrExpressionnewExp = new ConstantExpression(
                             ,
                             (Booleanrhs.getValue() || (Booleanlhs.getValue());
             else if (parent instanceof EqualExpressionnewExp = new ConstantExpression(
                             rhs.isEqual(lhs));
             else if (parent instanceof GreaterThanExpressionnewExp = new ConstantExpression(
                             ,
                             ((Comparablerhs.getValue()).compareTo((Comparablelhs.getValue()) > 0);
            else if (parent instanceof GreaterThanEqualExpressionnewExp = new ConstantExpression(
                            ,
                            ((Comparablerhs.getValue()).compareTo((Comparablelhs.getValue()) >= 0);
            else if (parent instanceof LessThanExpressionnewExp = new ConstantExpression(
                            ,
                            ((Comparablerhs.getValue()).compareTo((Comparablelhs.getValue()) < 0);
            else if (parent instanceof LessThanExpressionnewExp = new ConstantExpression(
                            ,
                            ((Comparablerhs.getValue()).compareTo((Comparablelhs.getValue()) <= 0);
            else if (parent instanceof AddExpression) {
                byte type = parent.getFieldSchema().;
                switch (type) {
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        (Integerlhs.getValue() + (Integerrhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        (Longlhs.getValue() + (Longrhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        (Floatlhs.getValue() + (Floatrhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        (Doublelhs.getValue() + (Doublerhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        ((BigIntegerlhs.getValue()).add((BigIntegerrhs.getValue()));
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        ((BigDecimallhs.getValue()).add((BigDecimalrhs.getValue()));
                        break;
                    default:
                        throw new FrontendException("Invalid type");
                }
            }
            else if (parent instanceof SubtractExpression) {
                byte type = parent.getFieldSchema().;
                switch (type) {
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        (Integerlhs.getValue() - (Integerrhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        (Longlhs.getValue() - (Longrhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        (Floatlhs.getValue() - (Floatrhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        (Doublelhs.getValue() - (Doublerhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        ((BigIntegerlhs.getValue()).subtract((BigIntegerrhs.getValue()));
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        ((BigDecimallhs.getValue()).subtract((BigDecimalrhs.getValue()));
                        break;
                    default:
                        throw new FrontendException("Invalid type");
                }
            }
            else if (parent instanceof MultiplyExpression) {
                byte type = parent.getFieldSchema().;
                switch (type) {
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        (Integerlhs.getValue() * (Integerrhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        (Longlhs.getValue() * (Longrhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        (Floatlhs.getValue() * (Floatrhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        (Doublelhs.getValue() * (Doublerhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        ((BigIntegerlhs.getValue()).multiply((BigIntegerrhs.getValue()));
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        ((BigDecimallhs.getValue()).multiply((BigDecimalrhs.getValue()));
                        break;
                    default:
                        throw new FrontendException("Invalid type");
                }
            }
            else if (parent instanceof ModExpression) {
                byte type = parent.getFieldSchema().;
                switch (type) {
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        (Integerlhs.getValue() % (Integerrhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        (Longlhs.getValue() % (Longrhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        ((BigIntegerlhs.getValue()).mod((BigIntegerrhs.getValue()));
                        break;
                    default:
                        throw new FrontendException("Invalid type");
                }
            }
            else if (parent instanceof DivideExpression) {
                byte type = parent.getFieldSchema().;
                switch (type) {
                    case .:
                        if ((Integerrhs.getValue() != 0)
                            newExp = new ConstantExpression(
                                            ,
                                            (Integerlhs.getValue() / (Integerrhs.getValue());
                        break;
                    case .:
                        if ((Longrhs.getValue() != 0)
                            newExp = new ConstantExpression(
                                            ,
                                            (Longlhs.getValue() / (Longrhs.getValue());
                        break;
                    case .:
                        if ((Floatrhs.getValue() != 0)
                            newExp = new ConstantExpression(
                                            ,
                                            (Floatlhs.getValue() / (Floatrhs.getValue());
                        break;
                    case .:
                        if ((Doublerhs.getValue() != 0)
                            newExp = new ConstantExpression(
                                            ,
                                            (Doublelhs.getValue() / (Doublerhs.getValue());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        ((BigIntegerlhs.getValue()).divide((BigIntegerrhs.getValue()));
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        ((BigDecimallhs.getValue()).divide((BigDecimalrhs.getValue()));
                        break;
                    default:
                        throw new FrontendException("Invalid type");
                }
            }
            else throw new FrontendException("Invalid instance type.");
            if (newExp != null) {
                .disconnect(parentrhs);
                .remove(rhs);
                .disconnect(parentlhs);
                .remove(lhs);
                .add(newExp);
                List<OperatorpredList = .getPredecessors(parent);
                if (predList != null) {
                    Operator[] preds = predList.toArray(new Operator[0]);
                    for (Object p : preds) {
                        Operator pred = (Operatorp;
                        Pair<IntegerIntegerpos = .disconnect(predparent);
                        .connect(predpos.firstnewExppos.second);
                    }
                }
                .remove(parent);
                .push(newExp);
            }
            else .push(null);
        }
    }
                    throws FrontendException {
        ConstantExpression operand = .pop();
        if (operand == null) {
            .push(null);
            return;
        }
        if (op instanceof CastExpression) {
            // no simplification on cast for now
            .push(null);
        }
        else {
            .remove(operand);
            .remove(op);
            ConstantExpression newExp;
            if (op instanceof NotExpressionnewExp = new ConstantExpression(
                            , !((Booleanoperand.getValue()));
            else if (op instanceof IsNullExpressionnewExp = new ConstantExpression(
                            operand.getValue() == null);
            else if (op instanceof NegativeExpression) {
                byte type = operand.getFieldSchema().;
                switch (type) {
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        -1 * ((Integeroperand.getValue()));
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        -1L * ((Longoperand.getValue()));
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        -1.0F * ((Floatoperand.getValue()));
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        -1.0D * ((Integeroperand.getValue()));
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        ((BigIntegeroperand.getValue()).negate());
                        break;
                    case .:
                        newExp = new ConstantExpression(
                                        ,
                                        ((BigDecimaloperand.getValue()).negate());
                        break;
                    default:
                        throw new FrontendException("Invalid type");
                }
            }
            else throw new FrontendException("Invalid instance type.");
            .add(newExp);
            .push(newExp);
        }
    }
    private void noConstPrune(LogicalExpression op)
                    throws FrontendException {
        // universal handler of an expression that can not or currently does not prune constant
        // sub expressions after pre-calculation
        List<Operatorsucs = op.getPlan().getSuccessors(op);
        int nSucs = (sucs == null ? 0 : sucs.size());
        for (int i = 0; i < nSucsi++)
            .pop();
        .push(null);
    }
    @Override
    public void visit(ConstantExpression constant)
                    throws FrontendException {
        .push(constant);
    }
    @Override
    public void visit(AndExpression andExprthrows FrontendException {
        binaryExpressionConstPrune(andExpr);
    }
    @Override
    public void visit(OrExpression orExprthrows FrontendException {
        binaryExpressionConstPrune(orExpr);
    }
    @Override
    public void visit(EqualExpression equalthrows FrontendException {
        binaryExpressionConstPrune(equal);
    }
    @Override
    public void visit(ProjectExpression project)
                    throws FrontendException {
        noConstPrune(project);
    }
    @Override
    public void visit(CastExpression castthrows FrontendException {
        unaryExpressionConstPrune(cast);
    }
    @Override
    public void visit(GreaterThanExpression greaterThanExpression)
                    throws FrontendException {
        binaryExpressionConstPrune(greaterThanExpression);
    }
    @Override
    public void visit(NotExpression opthrows FrontendException {
        unaryExpressionConstPrune(op);
    }
    @Override
    public void visit(IsNullExpression opthrows FrontendException {
        unaryExpressionConstPrune(op);
    }
    @Override
    public void visit(NegativeExpression opthrows FrontendException {
        unaryExpressionConstPrune(op);
    }
    @Override
    public void visit(AddExpression opthrows FrontendException {
        binaryExpressionConstPrune(op);
    }
    @Override
    public void visit(SubtractExpression opthrows FrontendException {
        binaryExpressionConstPrune(op);
    }
    @Override
    public void visit(ModExpression opthrows FrontendException {
        binaryExpressionConstPrune(op);
    }
    @Override
    public void visit(MultiplyExpression opthrows FrontendException {
        binaryExpressionConstPrune(op);
    }
    @Override
    public void visit(DivideExpression opthrows FrontendException {
        binaryExpressionConstPrune(op);
    }
    @Override
    public void visit(MapLookupExpression opthrows FrontendException {
        noConstPrune(op);
    }
    @Override
    public void visit(BinCondExpression opthrows FrontendException {
        noConstPrune(op);
    }
    @Override
    public void visit(UserFuncExpression opthrows FrontendException {
        noConstPrune(op);
    }
    @Override
    public void visit(DereferenceExpression bagDerefenceExpression)
                    throws FrontendException {
        noConstPrune(bagDerefenceExpression);
    }
    @Override
    public void visit(RegexExpression opthrows FrontendException {
        noConstPrune(op);
    }
New to GrepCode? Check out our FAQ X