/*
 * 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 io.prestosql.testing.datatype;

import com.google.common.base.Joiner;
import io.prestosql.testing.sql.SqlExecutor;
import io.prestosql.testing.sql.TestTable;

import java.util.List;
import java.util.stream.Stream;

import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;

public class CreateAsSelectDataSetup
        implements DataSetup
{
    private final SqlExecutor sqlExecutor;
    private final String tableNamePrefix;

    public CreateAsSelectDataSetup(SqlExecutor sqlExecutor, String tableNamePrefix)
    {
        this.sqlExecutor = sqlExecutor;
        this.tableNamePrefix = tableNamePrefix;
    }

    @Override
    public TestTable setupTestTable(List<ColumnSetup> inputs)
    {
        List<String> columnValues = inputs.stream()
                .map(this::literalInExplicitCast)
                .collect(toList());
        Stream<String> columnValuesWithNames = range(0, columnValues.size())
                .mapToObj(i -> format("%s col_%d", columnValues.get(i), i));
        String selectBody = Joiner.on(",\n").join(columnValuesWithNames.iterator());
        return new TestTable(sqlExecutor, tableNamePrefix, "AS SELECT " + selectBody);
    }

    private String literalInExplicitCast(ColumnSetup input)
    {
        return format(
                "CAST(%s AS %s)",
                input.getInputLiteral(),
                input.getDeclaredType().orElseThrow(() -> new IllegalArgumentException("declared type not set")));
    }
}

@@ -782,47 +782,8 @@
 m;%0A%0A
-import static java.lang.String.format;%0A
 impo
@@ -1377,36 +1377,21 @@
 p(this::
-literalInExplicitCas
+forma
 t)%0A     
@@ -1529,16 +1529,23 @@
 bj(i -%3E 
+String.
 format(%22
@@ -1784,28 +1784,13 @@
 ing 
-literalInExplicitCas
+forma
 t(Co
@@ -1820,23 +1820,133 @@
         
-return 
+if (input.getDeclaredType().isEmpty()) %7B%0A            return input.getInputLiteral();%0A        %7D%0A        return String.
 format(%0A
@@ -2064,79 +2064,12 @@
 e().
-orElseThrow(() -%3E new IllegalArgumentException(%22declared type not set%22)
+get(
 ));%0A

Support implicit types in CreateAsSelectDataSetup