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

import static org.apache.phoenix.util.TestUtil.B_VALUE;
import static org.apache.phoenix.util.TestUtil.C_VALUE;
import static org.apache.phoenix.util.TestUtil.E_VALUE;
import static org.apache.phoenix.util.TestUtil.ROW1;
import static org.apache.phoenix.util.TestUtil.ROW2;
import static org.apache.phoenix.util.TestUtil.ROW3;
import static org.apache.phoenix.util.TestUtil.ROW4;
import static org.apache.phoenix.util.TestUtil.ROW5;
import static org.apache.phoenix.util.TestUtil.ROW6;
import static org.apache.phoenix.util.TestUtil.ROW7;
import static org.apache.phoenix.util.TestUtil.ROW8;
import static org.apache.phoenix.util.TestUtil.ROW9;
import static org.apache.phoenix.util.TestUtil.TEST_PROPERTIES;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import org.apache.phoenix.util.PropertiesUtil;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runners.Parameterized.Parameters;

@Category(ParallelStatsDisabledTest.class)
public class InQueryIT extends BaseQueryIT {

  public InQueryIT(String indexDDL, boolean columnEncoded, boolean keepDeletedCells) {
    super(indexDDL, columnEncoded, keepDeletedCells);
  }

  @Parameters(name = "InQueryIT_{index}") // name is used by failsafe as file name in reports
  public static synchronized Collection<Object> data() {
    return BaseQueryIT.allIndexes();
  }

  @Test
  public void testInListSkipScan() throws Exception {
    String query = "SELECT entity_id, b_string FROM " + tableName
      + " WHERE organization_id=? and entity_id IN (?,?)";
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    try {
      PreparedStatement statement = conn.prepareStatement(query);
      statement.setString(1, tenantId);
      statement.setString(2, ROW2);
      statement.setString(3, ROW4);
      ResultSet rs = statement.executeQuery();
      Set<String> expectedvals = new HashSet<String>();
      expectedvals.add(ROW2 + "_" + C_VALUE);
      expectedvals.add(ROW4 + "_" + B_VALUE);
      Set<String> vals = new HashSet<String>();
      assertTrue(rs.next());
      vals.add(rs.getString(1) + "_" + rs.getString(2));
      assertTrue(rs.next());
      vals.add(rs.getString(1) + "_" + rs.getString(2));
      assertFalse(rs.next());
      assertEquals(expectedvals, vals);
    } finally {
      conn.close();
    }
  }

  @Test
  public void testDateInList() throws Exception {
    String query =
      "SELECT entity_id FROM " + tableName + " WHERE a_date IN (?,?) AND a_integer < 4";
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(url, props);
    try {
      PreparedStatement statement = conn.prepareStatement(query);
      statement.setDate(1, new Date(0));
      statement.setDate(2, date);
      ResultSet rs = statement.executeQuery();
      assertTrue(rs.next());
      assertEquals(ROW1, rs.getString(1));
      assertFalse(rs.next());
    } finally {
      conn.close();
    }
  }

  @Test
  public void testSimpleInListStatement() throws Exception {
    String query =
      "SELECT entity_id FROM " + tableName + " WHERE organization_id=? AND a_integer IN (2,4)";
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(url, props);
    try {
      PreparedStatement statement = conn.prepareStatement(query);
      statement.setString(1, tenantId);
      ResultSet rs = statement.executeQuery();
      assertValueEqualsResultSet(rs, Arrays.<Object> asList(ROW2, ROW4));
    } finally {
      conn.close();
    }
  }

  @Test
  public void testPartiallyQualifiedRVCInList() throws Exception {
    String query =
      "SELECT entity_id FROM " + tableName + " WHERE (a_integer,a_string) IN ((2,'a'),(5,'b'))";
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(url, props);
    try {
      PreparedStatement statement = conn.prepareStatement(query);
      ResultSet rs = statement.executeQuery();
      assertValueEqualsResultSet(rs, Arrays.<Object> asList(ROW2, ROW5));
    } finally {
      conn.close();
    }
  }

  @Test
  public void testFullyQualifiedRVCInList() throws Exception {
    String query = "SELECT entity_id FROM " + tableName
      + " WHERE (a_integer,a_string, organization_id,entity_id) IN ((2,'a',:1,:2),(5,'b',:1,:3))";
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(url, props);
    try {
      PreparedStatement statement = conn.prepareStatement(query);
      statement.setString(1, tenantId);
      statement.setString(2, ROW2);
      statement.setString(3, ROW5);
      ResultSet rs = statement.executeQuery();
      assertValueEqualsResultSet(rs, Arrays.<Object> asList(ROW2, ROW5));
    } finally {
      conn.close();
    }
  }

  @Test
  public void testOneInListStatement() throws Exception {
    String query =
      "SELECT entity_id FROM " + tableName + " WHERE organization_id=? AND b_string IN (?)";
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(url, props);
    try {
      PreparedStatement statement = conn.prepareStatement(query);
      statement.setString(1, tenantId);
      statement.setString(2, E_VALUE);
      ResultSet rs = statement.executeQuery();
      assertTrue(rs.next());
      assertEquals(ROW3, rs.getString(1));
      assertTrue(rs.next());
      assertEquals(ROW6, rs.getString(1));
      assertTrue(rs.next());
      assertEquals(ROW9, rs.getString(1));
      assertFalse(rs.next());
    } finally {
      conn.close();
    }
  }

  @Test
  public void testMixedTypeInListStatement() throws Exception {
    String query =
      "SELECT entity_id FROM " + tableName + " WHERE organization_id=? AND x_long IN (5, ?)";
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(url, props);
    try {
      PreparedStatement statement = conn.prepareStatement(query);
      statement.setString(1, tenantId);
      long l = Integer.MAX_VALUE + 1L;
      statement.setLong(2, l);
      ResultSet rs = statement.executeQuery();
      assertTrue(rs.next());
      assertEquals(ROW7, rs.getString(1));
      assertTrue(rs.next());
      assertEquals(ROW9, rs.getString(1));
      assertFalse(rs.next());
    } finally {
      conn.close();
    }
  }

  @Test
  public void testRowKeySingleIn() throws Exception {
    String query =
      "SELECT entity_id FROM " + tableName + " WHERE organization_id=? and entity_id IN (?,?,?)";
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    try {
      PreparedStatement statement = conn.prepareStatement(query);
      statement.setString(1, tenantId);
      statement.setString(2, ROW2);
      statement.setString(3, ROW6);
      statement.setString(4, ROW8);
      ResultSet rs = statement.executeQuery();
      assertTrue(rs.next());
      assertEquals(rs.getString(1), ROW2);
      assertTrue(rs.next());
      assertEquals(rs.getString(1), ROW6);
      assertTrue(rs.next());
      assertEquals(rs.getString(1), ROW8);
      assertFalse(rs.next());
    } finally {
      conn.close();
    }
  }

  @Test
  public void testRowKeyMultiIn() throws Exception {
    String query = "SELECT entity_id FROM " + tableName
      + " WHERE organization_id=? and entity_id IN (?,?,?) and a_string IN (?,?)";
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    try {
      PreparedStatement statement = conn.prepareStatement(query);
      statement.setString(1, tenantId);
      statement.setString(2, ROW2);
      statement.setString(3, ROW6);
      statement.setString(4, ROW9);
      statement.setString(5, B_VALUE);
      statement.setString(6, C_VALUE);
      ResultSet rs = statement.executeQuery();
      assertTrue(rs.next());
      assertEquals(rs.getString(1), ROW6);
      assertTrue(rs.next());
      assertEquals(rs.getString(1), ROW9);
      assertFalse(rs.next());
    } finally {
      conn.close();
    }
  }

}
