/*
 * 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;

import com.facebook.presto.sql.tree.DefaultTraversalVisitor;
import com.facebook.presto.sql.tree.Expression;
import com.facebook.presto.sql.tree.Node;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import javax.annotation.Nullable;

import java.util.List;
import java.util.Set;

/**
 * Extracts and returns the set of all expression subtrees within an Expression, including Expression itself
 */
public final class SubExpressionExtractor
{
    private SubExpressionExtractor() {}

    public static Set<Expression> extract(Expression expression)
    {
        final ImmutableSet.Builder<Expression> builder = ImmutableSet.builder();
        extract(builder, expression);
        return builder.build();
    }

    private static void extract(final ImmutableCollection.Builder<Expression> builder, Expression expression)
    {
        new DefaultTraversalVisitor<Void, Void>()
        {
            @Override
            public Void process(Node node, @Nullable Void context)
            {
                Expression expression = (Expression) node;
                builder.add(expression);

                return super.process(node, context);
            }
        }.process(expression, null);
    }
}

@@ -637,223 +637,64 @@
 ree.
-DefaultTraversalVisitor;%0Aimport com.facebook.presto.sql.tree.Expression;%0Aimport com.facebook.presto.sql.tree.Node;%0Aimport com.google.common.collect.ImmutableCollection;%0Aimport com.google.common.collect.ImmutableList
+Expression;%0Aimport com.facebook.presto.sql.util.AstUtils
 ;%0Aim
@@ -743,66 +743,8 @@
 t;%0A%0A
-import javax.annotation.Nullable;%0A%0Aimport java.util.List;%0A
 impo
@@ -1047,106 +1047,33 @@
     
-final ImmutableSet.Builder%3CExpression%3E builder = ImmutableSet.builder();%0A        extract(builder, 
+return AstUtils.preOrder(
 expr
@@ -1079,17 +1079,16 @@
 ression)
-;
 %0A       
@@ -1092,203 +1092,43 @@
     
-return builder.build();%0A    %7D%0A%0A    private static void extract(final ImmutableCollection.Builder%3CExpression%3E builder, Expression expression)%0A    %7B%0A        new DefaultTraversalVisitor%3CVoid, Void%3E(
+        .map(Expression.class::cast
 )%0A  
@@ -1129,34 +1129,32 @@
 st)%0A        
-%7B%0A
         
     @Overrid
@@ -1145,305 +1145,46 @@
     
-    @Override%0A            public Void process(Node node, @Nullable Void context)%0A            %7B%0A                Expression expression = (Expression) node;%0A                builder.add(expression);%0A%0A                return super.process(node, context);%0A            %7D%0A        %7D.process(expression, null
+.collect(ImmutableSet.toImmutableSet()
 );%0A 

Use AstUtils in SubExpressionExtractor