/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.xpack.sql.plan.logical.command;

import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.analysis.catalog.EsIndex;
import org.elasticsearch.xpack.sql.expression.Attribute;
import org.elasticsearch.xpack.sql.expression.RootFieldAttribute;
import org.elasticsearch.xpack.sql.session.RowSet;
import org.elasticsearch.xpack.sql.session.Rows;
import org.elasticsearch.xpack.sql.session.SqlSession;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.CompoundDataType;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.type.DataTypes;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;

import static java.util.Arrays.asList;

public class ShowColumns extends Command {

    private final String index;

    public ShowColumns(Location location, String index) {
        super(location);
        this.index = index;
    }

    public String index() {
        return index;
    }

    @Override
    public List<Attribute> output() {
        return asList(new RootFieldAttribute(location(), "column", DataTypes.KEYWORD), 
                      new RootFieldAttribute(location(), "type", DataTypes.KEYWORD));
    }

    @Override
    protected RowSet execute(SqlSession session) {
        List<List<?>> rows = new ArrayList<>();
        EsIndex fetched;
        try {
            fetched = session.getIndexSync(index);
        } catch (SqlIllegalArgumentException e) {
            throw new IllegalArgumentException(e);
        }
        if (fetched != null) {
            fillInRows(fetched.mapping(), null, rows);
        }
        return Rows.of(output(), rows);
    }

    private void fillInRows(Map<String, DataType> mapping, String prefix, List<List<?>> rows) {
        for (Entry<String, DataType> e : mapping.entrySet()) {
            DataType dt = e.getValue();
            String name = e.getKey();
            if (dt != null) {
                rows.add(asList(prefix != null ? prefix + "." + name : name, dt.sqlName()));
                if (dt instanceof CompoundDataType) {
                    String newPrefix = prefix != null ? prefix + "." + name : name;
                    fillInRows(((CompoundDataType) dt).properties(), newPrefix, rows);
                }
            }
        }
    }

    @Override
    public int hashCode() {
        return Objects.hash(index);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        
        ShowColumns other = (ShowColumns) obj;
        return Objects.equals(index, other.index);
    }
}
@@ -323,106 +323,85 @@
 rch.
-xpack.sql.SqlIllegalArgumentException;%0Aimport org.elasticsearch.xpack.sql.analysis.catalog.EsIndex
+action.ActionListener;%0Aimport org.elasticsearch.action.support.IndicesOptions
 ;%0Aim
@@ -1444,24 +1444,17 @@
 EYWORD),
- %0A      
+%0A
         
@@ -1555,23 +1555,18 @@
    p
-rotected RowSet
+ublic void
  exe
@@ -1592,84 +1592,194 @@
 sion
-) %7B%0A        List%3CList%3C?%3E%3E rows = new ArrayList%3C%3E();%0A        EsIndex fetched;
+, ActionListener%3CRowSet%3E listener) %7B%0A        session.getIndices(new String%5B%5D%7Bindex%7D, IndicesOptions.strictExpandOpenAndForbidClosed(), ActionListener.wrap(%0A                esIndices -%3E %7B
 %0A   
@@ -1787,14 +1787,9 @@
     
-try %7B%0A
+ 
     
@@ -1799,190 +1799,216 @@
     
- fetched = session.getIndexSync(index);%0A        %7D catch (SqlIllegalArgumentException e) %7B%0A            throw new IllegalArgumentException(e);%0A        %7D%0A        if (fetched != null) %7B%0A
+List%3CList%3C?%3E%3E rows = new ArrayList%3C%3E();%0A                    if (esIndices.isEmpty() == false) %7B%0A                        //TODO: we are using only the first index for now - add support for aliases%0A            
     
@@ -2030,15 +2030,24 @@
 ows(
-fetched
+esIndices.get(0)
 .map
@@ -2079,47 +2079,296 @@
     
-%7D%0A        return Rows.of(output(), rows
+            %7D%0A                    listener.onResponse(Rows.of(output(), rows));%0A                %7D,%0A                listener::onFailure%0A        ));%0A    %7D%0A%0A    @Override%0A    protected RowSet execute(SqlSession session) %7B%0A        throw new UnsupportedOperationException(%22No synchronous exec%22
 );%0A 
@@ -3204,32 +3204,24 @@
 ;%0A        %7D%0A
-        
 %0A        if 
@@ -3304,24 +3304,16 @@
       %7D%0A
-        
 %0A       

Make ShowColumns async (elastic/x-pack-elasticsearch#2638)