/*-
 * #%L
 * JSQLParser library
 * %%
 * Copyright (C) 2004 - 2019 JSQLParser
 * %%
 * Dual licensed under GNU LGPL 2.1 or Apache License 2.0
 * #L%
 */
package net.sf.jsqlparser.statement.select;

import net.sf.jsqlparser.JSQLParserException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;

public class ClickHouseTest {

    @Test
    public void testGlobalJoin() throws JSQLParserException {
        String sql =
                "SELECT a.*,b.* from lineorder_all as a  global left join supplier_all as b on a.LOLINENUMBER=b.SSUPPKEY";
        assertSqlCanBeParsedAndDeparsed(sql, true);
    }

    @Test
    public void testFunctionWithAttributesIssue1742() throws JSQLParserException {
        String sql = "SELECT f1(arguments).f2.f3 from dual";
        assertSqlCanBeParsedAndDeparsed(sql, true);

        sql = "SELECT f1(arguments).f2(arguments).f3.f4 from dual";
        assertSqlCanBeParsedAndDeparsed(sql, true);

        sql = "SELECT schemaName.f1(arguments).f2(arguments).f3.f4 from dual";
        assertSqlCanBeParsedAndDeparsed(sql, true);
    }

    @Test
    public void testSelectUsingFinal() throws JSQLParserException {
        String sqlStr = "SELECT column FROM table_name AS tn FINAL";
        assertSqlCanBeParsedAndDeparsed(sqlStr, true);

        // check that FINAL is reserved keyword and won't be read as an Alias
        sqlStr = "SELECT column FROM table_name FINAL";
        PlainSelect select = (PlainSelect) assertSqlCanBeParsedAndDeparsed(sqlStr, true);

        Assertions.assertTrue(select.isUsingFinal());
        Assertions.assertFalse(select.withUsingFinal(false).toString().contains("FINAL"));
    }

    @Test
    public void testLimitBy() throws JSQLParserException {
        String sqlStr = "SELECT * FROM limit_by ORDER BY id, val LIMIT 1, 2 BY id";
        assertSqlCanBeParsedAndDeparsed(sqlStr, true);

        sqlStr = "SELECT\n"
                + "    domainWithoutWWW(URL) AS domain,\n"
                + "    domainWithoutWWW(REFERRER_URL) AS referrer,\n"
                + "    device_type,\n"
                + "    count() cnt\n"
                + "FROM hits\n"
                + "GROUP BY domain, referrer, device_type\n"
                + "ORDER BY cnt DESC\n"
                + "LIMIT 5 BY domain, device_type\n"
                + "LIMIT 100";
        assertSqlCanBeParsedAndDeparsed(sqlStr, true);
    }
}
