/**
 * 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.drill;

import org.apache.drill.common.exceptions.UserException;
import org.apache.drill.exec.planner.physical.PlannerSettings;
import org.junit.Ignore;
import org.junit.Test;

public class TestBugFixes extends BaseTestQuery {
  private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TestBugFixes.class);

  @Test
  public void leak1() throws Exception {
    String select = "select count(*) \n" +
        "    from cp.`tpch/part.parquet` p1, cp.`tpch/part.parquet` p2 \n" +
        "    where p1.p_name = p2.p_name \n" +
        "  and p1.p_mfgr = p2.p_mfgr";
    test(select);
  }

  @Ignore
  @Test
  public void failingSmoke() throws Exception {
    String select = "select count(*) \n" +
        "  from (select l.l_orderkey as x, c.c_custkey as y \n" +
        "  from cp.`tpch/lineitem.parquet` l \n" +
        "    left outer join cp.`tpch/customer.parquet` c \n" +
        "      on l.l_orderkey = c.c_custkey) as foo\n" +
        "  where x < 10000";
    test(select);
  }

  @Test
  public void testSysDrillbits() throws Exception {
    test("select * from sys.drillbits");
  }

  @Test
  public void testVersionTable() throws Exception {
    test("select * from sys.version");
  }

  @Test
  public void DRILL883() throws Exception {
    test("select n1.n_regionkey from cp.`tpch/nation.parquet` n1, (select n_nationkey from cp.`tpch/nation.parquet`) as n2 where n1.n_nationkey = n2.n_nationkey");
  }

  @Test
  public void DRILL1061() throws Exception {
    String query = "select foo.mycol.x as COMPLEX_COL from (select convert_from('{ x : [1,2], y : 100 }', 'JSON') as mycol from cp.`tpch/nation.parquet`) as foo(mycol) limit 1";
    test(query);
  }

  @Test
  public void DRILL1126() throws Exception {
    try {
      test(String.format("alter session set `%s` = true", PlannerSettings.ENABLE_DECIMAL_DATA_TYPE_KEY));
      String query = "select sum(cast(employee_id as decimal(38, 18))), avg(cast(employee_id as decimal(38, 18))) from cp.`employee.json` group by (department_id)";
      test(query);
    } finally {
      test(String.format("alter session set `%s` = false", PlannerSettings.ENABLE_DECIMAL_DATA_TYPE_KEY));
    }
  }


  @Test (expected = UserException.class)
  // Should be "Failure while parsing sql. Node [rel#26:Subset#6.LOGICAL.ANY([]).[]] could not be implemented;".
  // Drill will hit CanNotPlan, until we add code fix to transform the local LHS filter in left outer join properly.
  public void testDRILL1337_LocalLeftFilterLeftOutJoin() throws Exception {
    try {
      test("select count(*) from cp.`tpch/nation.parquet` n left outer join cp.`tpch/region.parquet` r on n.n_regionkey = r.r_regionkey and n.n_nationkey > 10;");
    } catch (UserException e) {
      logger.info("***** Test resulted in expected failure: " + e.getMessage());
      throw e;
    }
  }

  @Test
  public void testDRILL1337_LocalRightFilterLeftOutJoin() throws Exception {
    test("select * from cp.`tpch/nation.parquet` n left outer join cp.`tpch/region.parquet` r on n.n_regionkey = r.r_regionkey and r.r_name not like '%ASIA' order by r.r_name;");
  }

  @Test
  public void testDRILL2361_ColumnAliasWithDots() throws Exception {
    testBuilder()
      .sqlQuery("select count(*) as `test.alias` from cp.`employee.json`")
      .unOrdered()
      .baselineColumns("`test.alias`")
      .baselineValues(1155L)
      .build().run();
  }

  @Test
  public void testDRILL2361_SortColumnAliasWithDots() throws Exception {
    testBuilder()
            .sqlQuery("select o_custkey as `x.y.z` from cp.`tpch/orders.parquet` where o_orderkey < 5 order by `x.y.z`")
            .unOrdered()
            .baselineColumns("`x.y.z`")
            .baselineValues(370)
            .baselineValues(781)
            .baselineValues(1234)
            .baselineValues(1369)
            .build().run();
  }

  @Test
  public void testDRILL2361_JoinColumnAliasWithDots() throws Exception {
    testBuilder()
            .sqlQuery("select count(*) from (select o_custkey as `x.y` from cp.`tpch/orders.parquet`) o inner join cp.`tpch/customer.parquet` c on o.`x.y` = c.c_custkey")
            .unOrdered()
            .baselineColumns("EXPR$0")
            .baselineValues(15000L)
            .build().run();
  }

  @Test
  public void testDRILL3150_NonExistentStringFilter() throws Exception {
    testBuilder()
            .sqlQuery("select * from cp.`employee.json` where `blah` = 'test'")
            .expectsEmptyResultSet()
            .build().run();
  }

  @Test
  public void testDRILL3150_NonExistentBooleanFilter() throws Exception {
    testBuilder()
          .sqlQuery("select * from cp.`employee.json` where `blah` = false")
          .expectsEmptyResultSet()
          .build().run();
  }

  @Test
  public void testDRILL3150_NonExistentDoubleFilter() throws Exception {
      testBuilder()
              .sqlQuery("select * from cp.`employee.json` where `blah` = 102.54")
              .expectsEmptyResultSet()
              .build().run();
  }

    @Test
    public void testDRILL3150_InvalidCast() throws Exception {
        errorMsgTestHelper("select * from cp.`employee.json` where `blah` = CAST('test' AS INT)", "NumberFormatException");
    }

}

@@ -3929,1147 +3929,8 @@
  %7D%0A%0A
-  @Test%0A  public void testDRILL2361_ColumnAliasWithDots() throws Exception %7B%0A    testBuilder()%0A      .sqlQuery(%22select count(*) as %60test.alias%60 from cp.%60employee.json%60%22)%0A      .unOrdered()%0A      .baselineColumns(%22%60test.alias%60%22)%0A      .baselineValues(1155L)%0A      .build().run();%0A  %7D%0A%0A  @Test%0A  public void testDRILL2361_SortColumnAliasWithDots() throws Exception %7B%0A    testBuilder()%0A            .sqlQuery(%22select o_custkey as %60x.y.z%60 from cp.%60tpch/orders.parquet%60 where o_orderkey %3C 5 order by %60x.y.z%60%22)%0A            .unOrdered()%0A            .baselineColumns(%22%60x.y.z%60%22)%0A            .baselineValues(370)%0A            .baselineValues(781)%0A            .baselineValues(1234)%0A            .baselineValues(1369)%0A            .build().run();%0A  %7D%0A%0A  @Test%0A  public void testDRILL2361_JoinColumnAliasWithDots() throws Exception %7B%0A    testBuilder()%0A            .sqlQuery(%22select count(*) from (select o_custkey as %60x.y%60 from cp.%60tpch/orders.parquet%60) o inner join cp.%60tpch/customer.parquet%60 c on o.%60x.y%60 = c.c_custkey%22)%0A            .unOrdered()%0A            .baselineColumns(%22EXPR$0%22)%0A            .baselineValues(15000L)%0A            .build().run();%0A  %7D%0A%0A
   @T

Remove invalid tests from branch error