/*
 * 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.iterative.rule;

import com.facebook.presto.sql.planner.PlanNodeIdAllocator;
import com.facebook.presto.sql.planner.Symbol;
import com.facebook.presto.sql.planner.plan.JoinNode;
import com.facebook.presto.sql.planner.plan.PlanNode;

import java.util.Optional;
import java.util.Set;

import static com.facebook.presto.sql.planner.iterative.rule.Util.restrictChildOutputs;
import static com.facebook.presto.sql.planner.plan.Patterns.join;

/**
 * Cross joins don't support output symbol selection, so push the project-off through the node.
 */
public class PruneCrossJoinColumns
        extends ProjectOffPushDownRule<JoinNode>
{
    public PruneCrossJoinColumns()
    {
        super(join().matching(JoinNode::isCrossJoin));
    }

    @Override
    protected Optional<PlanNode> pushDownProjectOff(PlanNodeIdAllocator idAllocator, JoinNode joinNode, Set<Symbol> referencedOutputs)
    {
        return restrictChildOutputs(idAllocator, joinNode, referencedOutputs, referencedOutputs);
    }
}

@@ -826,16 +826,64 @@
 lanNode;
+%0Aimport com.google.common.collect.ImmutableList;
 %0A%0Aimport
@@ -996,29 +996,24 @@
 til.restrict
-Child
 Outputs;%0Aimp
@@ -1536,28 +1536,152 @@
     
-return restrictChild
+Optional%3CPlanNode%3E newLeft = restrictOutputs(idAllocator, joinNode.getLeft(), referencedOutputs);%0A        Optional%3CPlanNode%3E newRight = restrict
 Outp
@@ -1705,16 +1705,27 @@
 joinNode
+.getRight()
 , refere
@@ -1735,35 +1735,886 @@
 dOutputs
-, referencedOutputs
+);%0A%0A        if (!newLeft.isPresent() && !newRight.isPresent()) %7B%0A            return Optional.empty();%0A        %7D%0A%0A        ImmutableList.Builder%3CSymbol%3E outputSymbolBuilder = ImmutableList.builder();%0A        outputSymbolBuilder.addAll(newLeft.orElse(joinNode.getLeft()).getOutputSymbols());%0A        outputSymbolBuilder.addAll(newRight.orElse(joinNode.getRight()).getOutputSymbols());%0A%0A        return Optional.of(new JoinNode(%0A                idAllocator.getNextId(),%0A                joinNode.getType(),%0A                newLeft.orElse(joinNode.getLeft()),%0A                newRight.orElse(joinNode.getRight()),%0A                joinNode.getCriteria(),%0A                outputSymbolBuilder.build(),%0A                joinNode.getFilter(),%0A                joinNode.getLeftHashSymbol(),%0A                joinNode.getRightHashSymbol(),%0A                joinNode.getDistributionType())
 );%0A    %7D

Return a JoinNode instead of replaceChildren in PruneCrossJoinColumns