/*
 * 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 com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import io.shardingsphere.core.merger.QueryResult;
import lombok.RequiredArgsConstructor;

import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;

/**
 * Distinct query result.
 *
 * @author panjuan
 */
public final class DistinctQueryResult implements QueryResult {
    
    private final Collection<QueryResult> queryResults;
    
    public DistinctQueryResult(final Collection<QueryResult> queryResults) {
    
    }
    
    @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");
    }
}

@@ -685,269 +685,78 @@
 ort 
-com.google.common.collect.HashMultimap;%0Aimport com.google.common.collect.Multimap;%0Aimport io.shardingsphere.core.merger.QueryResult;%0Aimport lombok.RequiredArgsConstructor;%0A%0Aimport java.io.InputStream;%0Aimport java.sql.ResultSet;%0Aimport java.sql.ResultSetMetaData
+io.shardingsphere.core.merger.QueryResult;%0A%0Aimport java.io.InputStream
 ;%0Aim
@@ -787,36 +787,8 @@
 on;%0A
-import java.util.ArrayList;%0A
 impo
@@ -860,68 +860,11 @@
 til.
-Iterator;%0Aimport java.util.LinkedList;%0Aimport java.util.List
+Map
 ;%0Aim
@@ -1010,20 +1010,16 @@
 esult %7B%0A
-    
 %0A    pri
@@ -1067,20 +1067,102 @@
 esults;%0A
+%0A
     
+private final Map%3CInteger, Collection%3CObject%3E%3E columnIndexAndDistinctValusesMap;%0A
 %0A    pub
@@ -1235,27 +1235,39 @@
 ) %7B%0A    
+    queryResults
 %0A    %7D%0A
-    
 %0A    @Ov
@@ -1463,36 +1463,32 @@
 rn false;%0A    %7D%0A
-    
 %0A    @Override%0A 
@@ -1605,36 +1605,32 @@
 dex - 1);%0A    %7D%0A
-    
 %0A    @Override%0A 
@@ -1769,36 +1769,32 @@
 nLabel));%0A    %7D%0A
-    
 %0A    @Override%0A 
@@ -1944,36 +1944,32 @@
 dex - 1);%0A    %7D%0A
-    
 %0A    @Override%0A 
@@ -2141,36 +2141,32 @@
 nLabel));%0A    %7D%0A
-    
 %0A    @Override%0A 
@@ -2306,36 +2306,32 @@
 dex - 1);%0A    %7D%0A
-    
 %0A    @Override%0A 
@@ -2493,36 +2493,32 @@
 nLabel));%0A    %7D%0A
-    
 %0A    @Override%0A 
@@ -2580,36 +2580,32 @@
 rrentRow;%0A    %7D%0A
-    
 %0A    @Override%0A 
@@ -2689,20 +2689,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Ov

add columnIndexAndDistinctValusesMap