/*
 * 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 com.facebook.presto.sql.planner.plan.AggregationNode;
import com.facebook.presto.sql.planner.plan.EnforceSingleRowNode;
import com.facebook.presto.sql.planner.plan.ExchangeNode;
import com.facebook.presto.sql.planner.plan.FilterNode;
import com.facebook.presto.sql.planner.plan.PlanNode;
import com.facebook.presto.sql.planner.plan.PlanVisitor;
import com.facebook.presto.sql.planner.plan.ProjectNode;
import com.facebook.presto.sql.planner.plan.ValuesNode;
import com.google.common.collect.ImmutableList;

import static com.google.common.collect.Iterables.getOnlyElement;

public final class ScalarQueryUtil
{
    private ScalarQueryUtil() {}

    public static boolean isScalar(PlanNode node)
    {
        return node.accept(new IsScalarPlanVisitor(), null);
    }

    private static final class IsScalarPlanVisitor
            extends PlanVisitor<Void, Boolean>
    {
        @Override
        protected Boolean visitPlan(PlanNode node, Void context)
        {
            return false;
        }

        @Override
        public Boolean visitEnforceSingleRow(EnforceSingleRowNode node, Void context)
        {
            return true;
        }

        @Override
        public Boolean visitAggregation(AggregationNode node, Void context)
        {
            return node.getGroupingSets().equals(ImmutableList.of(ImmutableList.of()));
        }

        @Override
        public Boolean visitExchange(ExchangeNode node, Void context)
        {
            return (node.getSources().size() == 1) &&
                    getOnlyElement(node.getSources()).accept(this, null);
        }

        @Override
        public Boolean visitProject(ProjectNode node, Void context)
        {
            return node.getSource().accept(this, null);
        }

        @Override
        public Boolean visitFilter(FilterNode node, Void context)
        {
            return node.getSource().accept(this, null);
        }

        public Boolean visitValues(ValuesNode node, Void context)
        {
            return node.getRows().size() == 1;
        }
    }
}

@@ -611,16 +611,138 @@
 tions;%0A%0A
+import com.facebook.presto.sql.planner.iterative.GroupReference;%0Aimport com.facebook.presto.sql.planner.iterative.Lookup;%0A
 import c
@@ -1247,16 +1247,107 @@
 eList;%0A%0A
+import java.util.Optional;%0A%0Aimport static com.google.common.base.Preconditions.checkState;%0A
 import s
@@ -1403,16 +1403,64 @@
 Element;
+%0Aimport static java.util.Objects.requireNonNull;
 %0A%0Apublic
@@ -1572,16 +1572,31 @@
 ode node
+, Lookup lookup
 )%0A    %7B%0A
@@ -1650,221 +1650,797 @@
 tor(
-), null);%0A    %7D%0A%0A    private static final class IsScalarPlanVisitor%0A            extends PlanVisitor%3CVoid, Boolean%3E%0A    %7B%0A        @Override%0A        protected Boolean visitPlan(PlanNode node, Void context)%0A        %7B
+Optional.ofNullable(lookup)), null);%0A    %7D%0A%0A    public static boolean isScalar(PlanNode node)%0A    %7B%0A        return isScalar(node, null);%0A    %7D%0A%0A    private static final class IsScalarPlanVisitor%0A            extends PlanVisitor%3CVoid, Boolean%3E%0A    %7B%0A        private final Optional%3CLookup%3E lookup;%0A%0A        public IsScalarPlanVisitor(Optional%3CLookup%3E lookup)%0A        %7B%0A            this.lookup = requireNonNull(lookup, %22lookup is null%22);%0A        %7D%0A%0A        @Override%0A        protected Boolean visitPlan(PlanNode node, Void context)%0A        %7B%0A            if (node instanceof GroupReference) %7B%0A                checkState(lookup.isPresent(), %22Lookup has to be provided to test plans with GroupReference nodes%22);%0A                return lookup.get().resolve(node).accept(this, null);%0A            %7D%0A
 %0A   

Add Lookup resolution to the ScalarQueryUtil.isScalar