/*
 * 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.base.Optional;
import io.shardingsphere.core.constant.AggregationType;
import lombok.RequiredArgsConstructor;

import java.util.Collection;

/**
 * Aggregation distinct query metadata.
 *
 * @author panjuan
 */
public final class AggregationDistinctQueryMetaData {
    
    private Collection<AggregationDistinctColumnMetaData> columnMetaDatas;
    
    @RequiredArgsConstructor 
    final class AggregationDistinctColumnMetaData {
        
        private final int columnIndex;
        
        private final String columnLabel;
        
        private final AggregationType aggregationType;
        
        private final Optional<Integer> derivedCountIndex;
        
        private final Optional<Integer> derivedSumIndex;
    }
}

@@ -770,16 +770,198 @@
 onType;%0A
+import io.shardingsphere.core.parsing.parser.context.selectitem.AggregationDistinctSelectItem;%0Aimport io.shardingsphere.core.parsing.parser.context.selectitem.AggregationSelectItem;%0A
 import l
@@ -1020,16 +1020,68 @@
 lection;
+%0Aimport java.util.LinkedList;%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -1215,16 +1215,22 @@
 private 
+final 
 Collecti
@@ -1285,24 +1285,1098 @@
 Datas;%0A    %0A
+    public AggregationDistinctQueryMetaData(final Collection%3CAggregationDistinctSelectItem%3E aggregationDistinctSelectItems) %7B%0A        columnMetaDatas = new LinkedList%3C%3E();%0A        for (AggregationDistinctSelectItem each : aggregationDistinctSelectItems) %7B%0A            columnMetaDatas.add(getAggregationDistinctColumnMetaData(each));%0A        %7D%0A    %7D%0A    %0A    private AggregationDistinctColumnMetaData getAggregationDistinctColumnMetaData(final AggregationDistinctSelectItem selectItem) %7B%0A        List%3CAggregationSelectItem%3E derivedSelectItems = selectItem.getDerivedAggregationSelectItems();%0A        if (derivedSelectItems.isEmpty()) %7B%0A            return new AggregationDistinctColumnMetaData(selectItem.getIndex(), selectItem.getColumnLabel(), selectItem.getType(), Optional.%3CInteger%3Eabsent(), Optional.%3CInteger%3Eabsent());%0A        %7D%0A        return new AggregationDistinctColumnMetaData(selectItem.getIndex(), selectItem.getColumnLabel(), selectItem.getType(), Optional.of(derivedSelectItems.get(0).getIndex()), Optional.of(derivedSelectItems.get(1).getIndex()));%0A    %7D%0A    %0A
     @Require

add getAggregationDistinctColumnMetaData()