/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * 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.
 * </p>
 */

package io.shardingsphere.core.executor.sql.execute.result;

import io.shardingsphere.core.merger.QueryResult;
import lombok.SneakyThrows;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * Distinct query result.
 *
 * @author panjuan
 */
public final class DistinctQueryResult implements QueryResult {

    private final Collection<QueryResult> queryResults;

    private final Map<Integer, Set<Object>> columnIndexAndDistinctValusesMap;

    public DistinctQueryResult(final Collection<QueryResult> queryResults) {
        this.queryResults = queryResults;
        
    }
    
    @SneakyThrows
    private Map<Integer, Collection<Object>> getColumnIndexAndDistinctValusesMap(final Collection<QueryResult> queryResults) {
        Map<Integer, Collection<Object>> result = new HashMap<>(queryResults.iterator().next().getColumnCount());
        
    }

    @Override
    public boolean next() {
        if (resultData.hasNext()) {
            currentRow = resultData.next();
            return true;
        }
        currentRow = null;
        return false;
    }

    @Override
    public Object getValue(final int columnIndex, final Class<?> type) {
        return currentRow.get(columnIndex - 1);
    }

    @Override
    public Object getValue(final String columnLabel, final Class<?> type) {
        return currentRow.get(getIndexByColumnLabel(columnLabel));
    }

    @Override
    public Object getCalendarValue(final int columnIndex, final Class<?> type, final Calendar calendar) {
        return currentRow.get(columnIndex - 1);
    }

    @Override
    public Object getCalendarValue(final String columnLabel, final Class<?> type, final Calendar calendar) {
        return currentRow.get(getIndexByColumnLabel(columnLabel));
    }

    @Override
    public InputStream getInputStream(final int columnIndex, final String type) {
        return (InputStream) currentRow.get(columnIndex - 1);
    }

    @Override
    public InputStream getInputStream(final String columnLabel, final String type) {
        return (InputStream) currentRow.get(getIndexByColumnLabel(columnLabel));
    }

    @Override
    public boolean wasNull() {
        return null == currentRow;
    }

    @Override
    public int getColumnCount() {
        return columnLabelAndIndexMap.size();
    }

    @Override
    public String getColumnLabel(final int columnIndex) throws SQLException {
        for (Entry<String, Integer> entry : columnLabelAndIndexMap.entries()) {
            if (columnIndex == entry.getValue()) {
                return entry.getKey();
            }
        }
        throw new SQLException("Column index out of range", "9999");
    }
}

@@ -674,16 +674,62 @@
 esult;%0A%0A
+import com.google.common.collect.SetMultimap;%0A
 import i
@@ -798,17 +798,16 @@
 Throws;%0A
-%0A
 import j
@@ -929,16 +929,22 @@
 va.util.
+Linked
 HashMap;
@@ -940,24 +940,56 @@
 kedHashMap;%0A
+import java.util.LinkedHashSet;%0A
 import java.
@@ -1429,24 +1429,109 @@
 ts;%0A        
+columnIndexAndDistinctValusesMap = getColumnIndexAndDistinctValusesMap(queryResults);
 %0A    %7D%0A    %0A
@@ -1569,34 +1569,27 @@
 ap%3CInteger, 
-Collection
+Set
 %3CObject%3E%3E ge
@@ -1689,26 +1689,509 @@
 nteger, 
-Collection
+Set%3CObject%3E%3E result = createIntegerSetMap(queryResults);%0A        for (QueryResult each : queryResults) %7B%0A            while (each.next()) %7B%0A                for (int i = 1; i %3C= result.size(); i++) %7B%0A                    result.get(i).add(each.getValue(i, Object.class));%0A                %7D%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A    %0A    private Map%3CInteger, Set%3CObject%3E%3E createIntegerSetMap(final Collection%3CQueryResult%3E queryResults) throws SQLException %7B%0A        Map%3CInteger, Set
 %3CObject%3E
@@ -2205,16 +2205,22 @@
 t = new 
+Linked
 HashMap%3C
@@ -2279,23 +2279,186 @@
         
+for (int i = 1; i %3C= queryResults.iterator().next().getColumnCount(); i++) %7B%0A            result.put(i, new LinkedHashSet%3C%3E());%0A        %7D%0A        return result;
 %0A    %7D%0A
+    
 %0A    @Ov

add getColumnIndexAndDistinctValusesMap()