@@ -0,0 +1,3460 @@
+/*%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0Apackage com.facebook.presto.sql.analyzer;%0A%0Aimport com.facebook.presto.sql.tree.DefaultExpressionTraversalVisitor;%0Aimport com.facebook.presto.sql.tree.DereferenceExpression;%0Aimport com.facebook.presto.sql.tree.Expression;%0Aimport com.facebook.presto.sql.tree.Identifier;%0Aimport com.facebook.presto.sql.tree.LambdaExpression;%0Aimport com.facebook.presto.sql.tree.Node;%0Aimport com.facebook.presto.sql.tree.QualifiedName;%0Aimport com.google.common.collect.ImmutableList;%0A%0Aimport java.util.List;%0Aimport java.util.Optional;%0A%0Aimport static java.util.Objects.requireNonNull;%0A%0A/**%0A * Extract expressions that are references to a given scope.%0A */%0Aclass ScopeReferenceExtractor%0A%7B%0A    private ScopeReferenceExtractor() %7B%7D%0A%0A    public static boolean hasReferencesToScope(Node node, Analysis analysis, Scope scope)%0A    %7B%0A        return !getReferencesToScope(node, analysis, scope).isEmpty();%0A    %7D%0A%0A    public static List%3CExpression%3E getReferencesToScope(Node node, Analysis analysis, Scope scope)%0A    %7B%0A        ImmutableList.Builder%3CExpression%3E builder = ImmutableList.builder();%0A        new Visitor(analysis, scope).process(node, builder);%0A        return builder.build();%0A    %7D%0A%0A    private static class Visitor%0A            extends DefaultExpressionTraversalVisitor%3CVoid, ImmutableList.Builder%3CExpression%3E%3E%0A    %7B%0A        private final Analysis analysis;%0A        private final Scope scope;%0A%0A        private Visitor(Analysis analysis, Scope scope)%0A        %7B%0A            this.analysis = requireNonNull(analysis, %22analysis is null%22);%0A            this.scope = requireNonNull(scope, %22scope is null%22);%0A        %7D%0A%0A        @Override%0A        protected Void visitIdentifier(Identifier node, ImmutableList.Builder%3CExpression%3E context)%0A        %7B%0A            if (!analysis.getLambdaArgumentReferences().containsKey(node) && isReferenceToScope(node, QualifiedName.of(node.getName()))) %7B%0A                context.add(node);%0A            %7D%0A            return null;%0A        %7D%0A%0A        @Override%0A        protected Void visitLambdaExpression(LambdaExpression node, ImmutableList.Builder%3CExpression%3E context)%0A        %7B%0A            return process(node.getBody(), context);%0A        %7D%0A%0A        @Override%0A        protected Void visitDereferenceExpression(DereferenceExpression node, ImmutableList.Builder%3CExpression%3E context)%0A        %7B%0A            if (analysis.getColumnReferences().contains(node) && isReferenceToScope(node, DereferenceExpression.getQualifiedName(node))) %7B%0A                context.add(node);%0A                return null;%0A            %7D%0A%0A            return super.visitDereferenceExpression(node, context);%0A        %7D%0A%0A        private boolean isReferenceToScope(Expression node, QualifiedName qualifiedName)%0A        %7B%0A            Optional%3CResolvedField%3E resolvedField = scope.tryResolveField(node, qualifiedName);%0A            return resolvedField.isPresent() && resolvedField.get().getScope().equals(scope);%0A        %7D%0A    %7D%0A%7D%0A

Add ScopeReferenceExtractor