Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed 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 com.facebook.presto.sql.planner.optimizations;
 
 
 import java.util.Map;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public class CanonicalizeExpressions
         extends PlanOptimizer
 {
     public static Expression canonicalizeExpression(Expression expression)
     {
         return ExpressionTreeRewriter.rewriteWith(new CanonicalizeExpressionRewriter(), expression);
     }
 
     @Override
     public PlanNode optimize(PlanNode planSession sessionMap<SymbolTypetypesSymbolAllocator symbolAllocatorPlanNodeIdAllocator idAllocator)
     {
         checkNotNull(plan"plan is null");
         checkNotNull(session"session is null");
         checkNotNull(types"types is null");
         checkNotNull(symbolAllocator"symbolAllocator is null");
         checkNotNull(idAllocator"idAllocator is null");
 
         return PlanRewriter.rewriteWith(new Rewriter(), plan);
     }
 
     private static class Rewriter
             extends PlanRewriter<Void>
     {
         @Override
         public PlanNode visitProject(ProjectNode nodeRewriteContext<Voidcontext)
         {
             PlanNode source = context.rewrite(node.getSource());
             Map<SymbolExpressionassignments = ImmutableMap.copyOf(Maps.transformValues(node.getAssignments(), CanonicalizeExpressions::canonicalizeExpression));
             return new ProjectNode(node.getId(), sourceassignments);
         }
 
         @Override
         public PlanNode visitFilter(FilterNode nodeRewriteContext<Voidcontext)
         {
             PlanNode source = context.rewrite(node.getSource());
             Expression canonicalized = canonicalizeExpression(node.getPredicate());
             if (canonicalized.equals(.)) {
                 return source;
             }
             return new FilterNode(node.getId(), sourcecanonicalized);
         }
 
         @Override
         public PlanNode visitTableScan(TableScanNode nodeRewriteContext<Voidcontext)
         {
             Expression originalConstraint = null;
             if (node.getOriginalConstraint() != null) {
                 originalConstraint = canonicalizeExpression(node.getOriginalConstraint());
             }
             return new TableScanNode(node.getId(), node.getTable(), node.getOutputSymbols(), node.getAssignments(), originalConstraintnode.getGeneratedPartitions());
         }
    }
    private static class CanonicalizeExpressionRewriter
            extends ExpressionRewriter<Void>
    {
        @Override
        public Expression rewriteIsNotNullPredicate(IsNotNullPredicate nodeVoid contextExpressionTreeRewriter<VoidtreeRewriter)
        {
            Expression value = treeRewriter.rewrite(node.getValue(), context);
            return new NotExpression(new IsNullPredicate(value));
        }
        @Override
        public Expression rewriteIfExpression(IfExpression nodeVoid contextExpressionTreeRewriter<VoidtreeRewriter)
        {
            Expression condition = treeRewriter.rewrite(node.getCondition(), context);
            Expression trueValue = treeRewriter.rewrite(node.getTrueValue(), context);
            Optional<ExpressionfalseValue = node.getFalseValue()
                    .map((value) -> treeRewriter.rewrite(valuecontext));
            return new SearchedCaseExpression(ImmutableList.of(new WhenClause(conditiontrueValue)), falseValue);
        }
        @Override
        public Expression rewriteCurrentTime(CurrentTime nodeVoid contextExpressionTreeRewriter<VoidtreeRewriter)
        {
            if (node.getPrecision() != null) {
                throw new UnsupportedOperationException("not yet implemented: non-default precision");
            }
            switch (node.getType()) {
                case :
                    return new FunctionCall(new QualifiedName("current_date"), ImmutableList.<Expression>of());
                case :
                    return new FunctionCall(new QualifiedName("current_time"), ImmutableList.<Expression>of());
                case :
                    return new FunctionCall(new QualifiedName("localtime"), ImmutableList.<Expression>of());
                case :
                    return new FunctionCall(new QualifiedName("current_timestamp"), ImmutableList.<Expression>of());
                case :
                    return new FunctionCall(new QualifiedName("localtimestamp"), ImmutableList.<Expression>of());
                default:
                    throw new UnsupportedOperationException("not yet implemented: " + node.getType());
            }
        }
        @Override
        public Expression rewriteExtract(Extract nodeVoid contextExpressionTreeRewriter<VoidtreeRewriter)
        {
            Expression value = treeRewriter.rewrite(node.getExpression(), context);
            switch (node.getField()) {
                case :
                    return new FunctionCall(new QualifiedName("year"), ImmutableList.of(value));
                case :
                    return new FunctionCall(new QualifiedName("quarter"), ImmutableList.of(value));
                case :
                    return new FunctionCall(new QualifiedName("month"), ImmutableList.of(value));
                case :
                    return new FunctionCall(new QualifiedName("week"), ImmutableList.of(value));
                case :
                case :
                    return new FunctionCall(new QualifiedName("day"), ImmutableList.of(value));
                case :
                case :
                    return new FunctionCall(new QualifiedName("day_of_week"), ImmutableList.of(value));
                case :
                case :
                    return new FunctionCall(new QualifiedName("day_of_year"), ImmutableList.of(value));
                case :
                case :
                    return new FunctionCall(new QualifiedName("year_of_week"), ImmutableList.of(value));
                case :
                    return new FunctionCall(new QualifiedName("hour"), ImmutableList.of(value));
                case :
                    return new FunctionCall(new QualifiedName("minute"), ImmutableList.of(value));
                case :
                    return new FunctionCall(new QualifiedName("second"), ImmutableList.of(value));
                case :
                    return new FunctionCall(new QualifiedName("timezone_minute"), ImmutableList.of(value));
                case :
                    return new FunctionCall(new QualifiedName("timezone_hour"), ImmutableList.of(value));
            }
            throw new UnsupportedOperationException("not yet implemented: " + node.getField());
        }
    }
New to GrepCode? Check out our FAQ X