@@ -0,0 +1,3896 @@
+/*%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.relational;%0A%0Aimport com.facebook.presto.spi.relation.CallExpression;%0Aimport com.facebook.presto.spi.relation.ConstantExpression;%0Aimport com.facebook.presto.spi.relation.InputReferenceExpression;%0Aimport com.facebook.presto.spi.relation.LambdaDefinitionExpression;%0Aimport com.facebook.presto.spi.relation.RowExpression;%0Aimport com.facebook.presto.spi.relation.RowExpressionVisitor;%0Aimport com.facebook.presto.spi.relation.SpecialFormExpression;%0Aimport com.facebook.presto.spi.relation.VariableReferenceExpression;%0Aimport com.facebook.presto.sql.planner.Symbol;%0Aimport com.google.common.collect.ImmutableList;%0A%0Aimport java.util.Map;%0A%0Aimport static com.facebook.presto.sql.relational.Expressions.call;%0Aimport static com.facebook.presto.sql.relational.Expressions.field;%0A%0Apublic final class SymbolToChannelTranslator%0A%7B%0A    private SymbolToChannelTranslator() %7B%7D%0A%0A    /**%0A     *%0A     * Given an %7B@param expression%7D and a %7B@param layout%7D, translate the symbols in the expression to the corresponding channel.%0A     */%0A    public static RowExpression translate(RowExpression expression, Map%3CSymbol, Integer%3E layout)%0A    %7B%0A        return expression.accept(new Visitor(), layout);%0A    %7D%0A%0A    private static class Visitor%0A            implements RowExpressionVisitor%3CRowExpression, Map%3CSymbol, Integer%3E%3E%0A    %7B%0A        @Override%0A        public RowExpression visitInputReference(InputReferenceExpression input, Map%3CSymbol, Integer%3E layout)%0A        %7B%0A            throw new UnsupportedOperationException(%22encountered already-translated symbols%22);%0A        %7D%0A%0A        @Override%0A        public RowExpression visitCall(CallExpression call, Map%3CSymbol, Integer%3E layout)%0A        %7B%0A            ImmutableList.Builder%3CRowExpression%3E arguments = ImmutableList.builder();%0A            call.getArguments().forEach(argument -%3E arguments.add(argument.accept(this, layout)));%0A            return call(call.getDisplayName(), call.getFunctionHandle(), call.getType(), arguments.build());%0A        %7D%0A%0A        @Override%0A        public RowExpression visitConstant(ConstantExpression literal, Map%3CSymbol, Integer%3E layout)%0A        %7B%0A            return literal;%0A        %7D%0A%0A        @Override%0A        public RowExpression visitLambda(LambdaDefinitionExpression lambda, Map%3CSymbol, Integer%3E layout)%0A        %7B%0A            return new LambdaDefinitionExpression(lambda.getArgumentTypes(), lambda.getArguments(), lambda.getBody().accept(this, layout));%0A        %7D%0A%0A        @Override%0A        public RowExpression visitVariableReference(VariableReferenceExpression reference, Map%3CSymbol, Integer%3E layout)%0A        %7B%0A            Symbol symbol = new Symbol(reference.getName());%0A            if (layout.containsKey(symbol)) %7B%0A                return field(layout.get(symbol), reference.getType());%0A            %7D%0A            // this is possible only for lambda%0A            return reference;%0A        %7D%0A%0A        @Override%0A        public RowExpression visitSpecialForm(SpecialFormExpression specialForm, Map%3CSymbol, Integer%3E layout)%0A        %7B%0A            ImmutableList.Builder%3CRowExpression%3E arguments = ImmutableList.builder();%0A            specialForm.getArguments().forEach(argument -%3E arguments.add(argument.accept(this, layout)));%0A            return new SpecialFormExpression(specialForm.getForm(), specialForm.getType(), arguments.build());%0A        %7D%0A    %7D%0A%7D%0A

Introduce SymbolToChannelTranslator