

package io.crate.integrationtests;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService;
import org.junit.Test;

import static io.crate.protocols.postgres.PGErrorStatus.INTERNAL_ERROR;
import static io.crate.testing.Asserts.assertThrows;
import static io.crate.testing.SQLErrorMatcher.isSQLError;
import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
import static org.hamcrest.Matchers.is;

public class GroupByAggregateBreakerTest extends SQLTransportIntegrationTest {

    @Override
    protected Settings nodeSettings(int nodeOrdinal) {
        return Settings.builder()
            .put(super.nodeSettings(nodeOrdinal))
            .put(HierarchyCircuitBreakerService.QUERY_CIRCUIT_BREAKER_LIMIT_SETTING.getKey(), "256b")
            .build();
    }

    @Test
    public void selectGroupByWithBreaking() throws Exception {
        assertThrows(() -> execute("select region, count(*) from sys.summits group by 1"),
                     isSQLError(is("[query] Data too large, data for [collect: 0] would be [280/280b], " +
                                   "which is larger than the limit of [256/256b]"),
                                INTERNAL_ERROR,
                                INTERNAL_SERVER_ERROR,
                                5000));
    }
}
