/*
 * 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.parsing.integrate.asserts.item;

import com.google.common.base.Predicate;
import com.google.common.collect.Sets;
import io.shardingsphere.core.parsing.integrate.asserts.SQLStatementAssertMessage;
import io.shardingsphere.core.parsing.integrate.jaxb.item.ExpectedDistinctSelectItem;
import io.shardingsphere.core.parsing.parser.context.selectitem.DistinctSelectItem;
import io.shardingsphere.core.parsing.parser.context.selectitem.SelectItem;
import lombok.RequiredArgsConstructor;

import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

/**
 * Distinct select item assert.
 *
 * @author panjuan
 */
@RequiredArgsConstructor
final class DistinctSelectItemAssert {
    
    private final SQLStatementAssertMessage assertMessage;
    
    void assertDistinctSelectItems(final Set<SelectItem> actual, final ExpectedDistinctSelectItem expected) {
        assertDistinctSelectItem(getDistinctSelectItem(actual), expected);
    }
    
    private void assertDistinctSelectItem(final DistinctSelectItem actual, final ExpectedDistinctSelectItem expected) {
        assertThat(assertMessage.getFullAssertMessage("Distinct select item alias assertion error: "), actual.getAlias().orNull(), is(expected.getAlias()));
        assertThat(assertMessage.getFullAssertMessage("Distinct select item distinct column name size assertion error: "),
                actual.getDistinctColumnNames().size(), is(expected.getDistinctColumnNames().size()));
        int count = 0;
        for (String each : actual.getDistinctColumnNames()) {
            assertThat(assertMessage.getFullAssertMessage("Distinct select item distinct column name assertion error: "), each, is(expected.getDistinctColumnNames().get(count)));
            count++;
        }
    }
    
    private DistinctSelectItem getDistinctSelectItem(final Set<SelectItem> actual) {
        return (DistinctSelectItem) (Sets.filter(actual, new Predicate<SelectItem>() {
            @Override
            public boolean apply(final SelectItem input) {
                return input instanceof DistinctSelectItem;
            }
        }).iterator().next());
    }
}

@@ -677,16 +677,56 @@
 .item;%0A%0A
+import com.google.common.base.Optional;%0A
 import c
@@ -758,16 +758,16 @@
 dicate;%0A
-
 import c
@@ -1233,16 +1233,59 @@
 ers.is;%0A
+import static org.junit.Assert.assertNull;%0A
 import s
@@ -1637,61 +1637,325 @@
     
-assertDistinctSelectItem(getDistinctSelectItem(actual
+Optional%3CDistinctSelectItem%3E distinctSelectItem = getDistinctSelectItem(actual);%0A        if (distinctSelectItem.isPresent()) %7B%0A            assertDistinctSelectItem(distinctSelectItem.get(), expected);%0A        %7D else %7B%0A            assertNull(assertMessage.getFullAssertMessage(%22distinct select item should not exist: %22
 ), e
@@ -1960,24 +1960,34 @@
  expected);%0A
+        %7D%0A
     %7D%0A    %0A 
@@ -2806,16 +2806,25 @@
 private 
+Optional%3C
 Distinct
@@ -2833,16 +2833,17 @@
 lectItem
+%3E
  getDist
@@ -2901,37 +2901,40 @@
     
-return (DistinctSelectItem) (
+Set%3CSelectItem%3E distinctItems = 
 Sets
@@ -3130,24 +3130,24 @@
           %7D%0A
-
         %7D).i
@@ -3144,16 +3144,145 @@
       %7D)
+;%0A        return distinctItems.isEmpty() ? Optional.%3CDistinctSelectItem%3Eabsent() : Optional.of((DistinctSelectItem) distinctItems
 .iterato

change to getDistinctSelectItem