/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

package org.apache.shardingsphere.core.rewrite.token.generator;

import com.google.common.base.Optional;
import org.apache.shardingsphere.core.parse.sql.segment.dml.SelectItemsSegment;
import org.apache.shardingsphere.core.parse.sql.segment.dml.item.AggregationDistinctSelectItemSegment;
import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement;
import org.apache.shardingsphere.core.rewrite.token.pojo.SelectItemPrefixToken;
import org.apache.shardingsphere.core.rule.ShardingRule;

import java.util.List;

/**
 * Select item prefix token generator.
 *
 * @author panjuan
 */
public final class SelectItemPrefixTokenGenerator implements OptionalSQLTokenGenerator<ShardingRule> {
    
    @Override
    public Optional<SelectItemPrefixToken> generateSQLToken(final SQLStatement sqlStatement, final List<Object> parameters, final ShardingRule shardingRule) {
        if (!(sqlStatement instanceof SelectStatement)) {
            return Optional.absent();
        }
        Optional<AggregationDistinctSelectItemSegment> distinctSelectItemSegment = sqlStatement.findSQLSegment(AggregationDistinctSelectItemSegment.class);
        Optional<SelectItemsSegment> selectItemsSegment = sqlStatement.findSQLSegment(SelectItemsSegment.class);
        if (distinctSelectItemSegment.isPresent() && selectItemsSegment.isPresent()) {
            return Optional.of(new SelectItemPrefixToken(selectItemsSegment.get().getStartIndex()));
        }
        return Optional.absent();
    }
}

@@ -1870,35 +1870,16 @@
 ptional%3C
-AggregationDistinct
 SelectIt
@@ -1876,24 +1876,25 @@
 l%3CSelectItem
+s
 Segment%3E dis
@@ -1890,25 +1890,17 @@
 egment%3E 
-distinctS
+s
 electIte
@@ -1896,24 +1896,25 @@
 %3E selectItem
+s
 Segment = sq
@@ -1939,35 +1939,16 @@
 Segment(
-AggregationDistinct
 SelectIt
@@ -1941,32 +1941,33 @@
 gment(SelectItem
+s
 Segment.class);%0A
@@ -1970,34 +1970,29 @@
 s);%0A        
-Optional%3CS
+if (s
 electItemsSe
@@ -2000,99 +2000,45 @@
 ment
-%3E selectItemsSegment = sqlStatement.findSQLSegment(SelectItemsSegment.class);%0A        if (d
+.isPresent() && isContainAggregationD
 isti
@@ -2057,32 +2057,17 @@
 mSegment
-.isPresent() && 
+(
 selectIt
@@ -2081,20 +2081,15 @@
 ent.
-isPresen
+ge
 t())
+)
  %7B%0A 
@@ -2238,10 +2238,247 @@
 ;%0A    %7D%0A
+    %0A    private boolean isContainAggregationDistinctSelectItemSegment(final SelectItemsSegment selectItemsSegment) %7B%0A        return !selectItemsSegment.findSelectItemSegments(AggregationDistinctSelectItemSegment.class).isEmpty();%0A    %7D%0A
 %7D%0A

is Contain AggregationDistinctSelectItemSegment