/*
 * Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
 * license agreements.  See the NOTICE file distributed with this work for
 * additional information regarding copyright ownership.  Crate licenses
 * this file to you 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.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial agreement.
 */

package io.crate.analyze;

import com.google.common.collect.Iterables;
import io.crate.sql.ExpressionFormatter;
import io.crate.sql.tree.ArrayComparisonExpression;
import io.crate.sql.tree.Expression;
import io.crate.sql.tree.QualifiedNameReference;
import io.crate.sql.tree.SubscriptExpression;

import java.util.ArrayList;
import java.util.List;

public class OutputNameFormatter {

    private final static InnerOutputNameFormatter INSTANCE = new InnerOutputNameFormatter();

    public static String format(Expression expression) {
        return INSTANCE.process(expression, null);
    }

    private static class InnerOutputNameFormatter extends ExpressionFormatter.Formatter {
        @Override
        protected String visitQualifiedNameReference(QualifiedNameReference node, Void context) {

            List<String> parts = new ArrayList<>();
            for (String part : node.getName().getParts()) {
                parts.add(part);
            }
            return Iterables.getLast(parts);
        }

        @Override
        protected String visitSubscriptExpression(SubscriptExpression node, Void context) {
            return process(node.name(), null) + '[' + process(node.index(), null) + ']';
        }

        @Override
        public String visitArrayComparisonExpression(ArrayComparisonExpression node, Void context) {
            return process(node.getLeft(), null) + ' ' +
                   node.getType().getValue() + ' ' +
                   node.quantifier().name() + '(' +
                   process(node.getRight(), null) + ')';
        }
    }
}

@@ -1047,52 +1047,8 @@
 e;%0A%0A
-import com.google.common.collect.Iterables;%0A
 impo
@@ -1286,21 +1286,16 @@
 va.util.
-Array
 List;%0Aim
@@ -1309,20 +1309,38 @@
 va.util.
-List
+NoSuchElementException
 ;%0A%0Apubli
@@ -1785,17 +1785,16 @@
 text) %7B%0A
-%0A
         
@@ -1823,116 +1823,180 @@
  = n
-ew ArrayList%3C%3E();%0A            for (String part : node.getName().getParts()) %7B%0A                parts.add(part
+ode.getName().getParts();%0A            if (parts.isEmpty()) %7B%0A                throw new NoSuchElementException(%22Parts of QualifiedNameReference are empty: %22 + node.getName()
 );%0A 
@@ -2031,31 +2031,34 @@
 urn 
-Iterables.getLast(parts
+parts.get(parts.size() - 1
 );%0A 

Remove unnecessary list copy in OutputNameFormatter