package com.facebook.presto.metadata;

import com.facebook.presto.ingest.ImportSchemaUtil;
import com.facebook.presto.spi.ImportClient;
import com.facebook.presto.spi.SchemaField;
import com.facebook.presto.split.ImportClientFactory;
import com.facebook.presto.tuple.TupleInfo;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.inject.Inject;

import java.util.List;
import java.util.concurrent.Callable;

import static com.facebook.presto.metadata.MetadataUtil.checkTableName;
import static com.facebook.presto.util.RetryDriver.runWithRetryUnchecked;
import static com.google.common.base.Preconditions.checkNotNull;

public class ImportMetadata
        extends AbstractMetadata
{
    private final ImportClientFactory importClientFactory;

    @Inject
    public ImportMetadata(ImportClientFactory importClientFactory)
    {
        this.importClientFactory = checkNotNull(importClientFactory, "importClientFactory is null");
    }

    @Override
    public TableMetadata getTable(String catalogName, final String schemaName, final String tableName)
    {
        checkTableName(catalogName, schemaName, tableName);

        final ImportClient client = importClientFactory.getClient(catalogName);
        List<SchemaField> tableSchema = runWithRetryUnchecked(new Callable<List<SchemaField>>()
        {
            @Override
            public List<SchemaField> call()
                    throws Exception
            {
                return client.getTableSchema(schemaName, tableName);
            }
        });

        ImportTableHandle importTableHandle = new ImportTableHandle(catalogName, schemaName, tableName);

        List<ColumnMetadata> columns = convertToMetadata(catalogName, tableSchema);

        return new TableMetadata(catalogName, schemaName, tableName, columns, importTableHandle);
    }

    private List<ColumnMetadata> convertToMetadata(final String sourceName, List<SchemaField> schemaFields)
    {
        return Lists.transform(schemaFields, new Function<SchemaField, ColumnMetadata>()
        {
            @Override
            public ColumnMetadata apply(SchemaField schemaField)
            {
                TupleInfo.Type type = ImportSchemaUtil.getTupleType(schemaField.getPrimitiveType());
                ImportColumnHandle columnHandle = new ImportColumnHandle(sourceName, schemaField.getFieldName(), type);
                return new ColumnMetadata(schemaField.getFieldName(), type, columnHandle);
            }
        });
    }
}

@@ -311,16 +311,64 @@
 nction;%0A
+import com.google.common.collect.ImmutableList;%0A
 import c
@@ -399,16 +399,16 @@
 .Lists;%0A
-
 import c
@@ -495,16 +495,90 @@
 lable;%0A%0A
+import static com.facebook.presto.metadata.MetadataUtil.checkCatalogName;%0A
 import s
@@ -1867,24 +1867,24 @@
 leSchema);%0A%0A
-
         retu
@@ -1972,24 +1972,1383 @@
 le);%0A    %7D%0A%0A
+    @Override%0A    public List%3CQualifiedTableName%3E listTables(String catalogName)%0A    %7B%0A        checkCatalogName(catalogName);%0A%0A        ImportClient client = importClientFactory.getClient(catalogName);%0A%0A        ImmutableList.Builder%3CQualifiedTableName%3E list = ImmutableList.builder();%0A%0A        List%3CString%3E schemas = getDatabaseNames(client);%0A        for (String schema : schemas) %7B%0A            List%3CString%3E tables = getTableNames(client, schema);%0A            for (String table : tables) %7B%0A                list.add(new QualifiedTableName(catalogName, schema, table));%0A            %7D%0A        %7D%0A%0A        return list.build();%0A    %7D%0A%0A    private static List%3CString%3E getTableNames(final ImportClient client, final String database)%0A    %7B%0A        return runWithRetryUnchecked(new Callable%3CList%3CString%3E%3E()%0A        %7B%0A            @Override%0A            public List%3CString%3E call()%0A                    throws Exception%0A            %7B%0A                return client.getTableNames(database);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private static List%3CString%3E getDatabaseNames(final ImportClient client)%0A    %7B%0A        return runWithRetryUnchecked(new Callable%3CList%3CString%3E%3E()%0A        %7B%0A            @Override%0A            public List%3CString%3E call()%0A                    throws Exception%0A            %7B%0A                return client.getDatabaseNames();%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     private 

Support listing tables for import data sources