Search in sources :

Example 1 with PhoenixArray

use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.

the class ArrayIT method testScanWithNonFixedWidthArrayInSelectClause.

@Test
public void testScanWithNonFixedWidthArrayInSelectClause() throws Exception {
    long ts = nextTimestamp();
    String tenantId = getOrganizationId();
    createTableWithArray(getUrl(), getDefaultSplits(tenantId), null, ts - 2);
    initTablesWithArrays(tenantId, null, ts, false, getUrl());
    String query = "SELECT a_string_array FROM table_with_array";
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, // Execute at timestamp 2
    Long.toString(ts + 2));
    Connection conn = DriverManager.getConnection(getUrl(), props);
    try {
        PreparedStatement statement = conn.prepareStatement(query);
        ResultSet rs = statement.executeQuery();
        assertTrue(rs.next());
        String[] strArr = new String[4];
        strArr[0] = "ABC";
        strArr[1] = "CEDF";
        strArr[2] = "XYZWER";
        strArr[3] = "AB";
        Array array = conn.createArrayOf("VARCHAR", strArr);
        PhoenixArray resultArray = (PhoenixArray) rs.getArray(1);
        assertEquals(resultArray, array);
        assertEquals("['ABC', 'CEDF', 'XYZWER', 'AB']", rs.getString(1));
        assertFalse(rs.next());
    } finally {
        conn.close();
    }
}
Also used : Array(java.sql.Array) PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) BaseTest(org.apache.phoenix.query.BaseTest) Test(org.junit.Test)

Example 2 with PhoenixArray

use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.

the class ArrayIT method testArrayWithDescOrder.

@Test
public void testArrayWithDescOrder() throws Exception {
    Connection conn;
    PreparedStatement stmt;
    ResultSet rs;
    long ts = nextTimestamp();
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 10));
    conn = DriverManager.getConnection(getUrl(), props);
    conn.createStatement().execute("CREATE TABLE t ( k VARCHAR, a_string_array VARCHAR(100) ARRAY[4], b_string_array VARCHAR(100) ARRAY[4] \n" + " CONSTRAINT pk PRIMARY KEY (k, b_string_array DESC)) \n");
    conn.close();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 30));
    conn = DriverManager.getConnection(getUrl(), props);
    stmt = conn.prepareStatement("UPSERT INTO t VALUES(?,?,?)");
    stmt.setString(1, "a");
    String[] s = new String[] { "abc", "def", "ghi", "jkll", null, null, "xxx" };
    Array array = conn.createArrayOf("VARCHAR", s);
    stmt.setArray(2, array);
    s = new String[] { "abc", "def", "ghi", "jkll", null, null, null, "xxx" };
    array = conn.createArrayOf("VARCHAR", s);
    stmt.setArray(3, array);
    stmt.execute();
    conn.commit();
    conn.close();
    props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 40));
    conn = DriverManager.getConnection(getUrl(), props);
    rs = conn.createStatement().executeQuery("SELECT b_string_array FROM t");
    assertTrue(rs.next());
    PhoenixArray strArr = (PhoenixArray) rs.getArray(1);
    assertEquals(array, strArr);
    assertEquals("['abc', 'def', 'ghi', 'jkll', null, null, null, 'xxx']", rs.getString(1));
    conn.close();
}
Also used : Array(java.sql.Array) PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) BaseTest(org.apache.phoenix.query.BaseTest) Test(org.junit.Test)

Example 3 with PhoenixArray

use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.

the class ArrayPrependFunctionIT method testArrayPrependFunctionNulls3.

@Test
public void testArrayPrependFunctionNulls3() throws Exception {
    Connection conn = DriverManager.getConnection(getUrl());
    String[] s = new String[] { "176", null, "212" };
    String tableName = generateUniqueName();
    initTableWithVarArray(conn, tableName, "VARCHAR", s, null);
    String[] s2 = new String[] { null, "176", null, "212" };
    PhoenixArray array2 = (PhoenixArray) conn.createArrayOf("VARCHAR", s2);
    conn = DriverManager.getConnection(getUrl());
    ResultSet rs;
    rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(b,a) FROM " + tableName + " WHERE k = 'a'");
    assertTrue(rs.next());
    assertEquals(array2, rs.getArray(1));
}
Also used : PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) Test(org.junit.Test)

Example 4 with PhoenixArray

use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.

the class ArrayPrependFunctionIT method initTableWithVarArray.

private void initTableWithVarArray(Connection conn, String tableName, String type, Object[] objectArray, String value) throws SQLException {
    conn.createStatement().execute("CREATE TABLE " + tableName + " ( k VARCHAR PRIMARY KEY, a " + type + "[],b " + type + ")");
    conn.commit();
    PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + tableName + " VALUES(?,?," + value + ")");
    PhoenixArray array = (PhoenixArray) conn.createArrayOf(type, objectArray);
    stmt.setString(1, "a");
    stmt.setArray(2, array);
    stmt.execute();
    conn.commit();
}
Also used : PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray)

Example 5 with PhoenixArray

use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.

the class ArrayPrependFunctionIT method testArrayPrependFunctionNulls4.

@Test
public void testArrayPrependFunctionNulls4() throws Exception {
    Connection conn = DriverManager.getConnection(getUrl());
    String[] s = new String[] { "176", null, "212" };
    String tableName = generateUniqueName();
    initTableWithVarArray(conn, tableName, "VARCHAR", s, "'foo'");
    String[] s2 = new String[] { "foo", "176", null, "212" };
    PhoenixArray array2 = (PhoenixArray) conn.createArrayOf("VARCHAR", s2);
    conn = DriverManager.getConnection(getUrl());
    ResultSet rs;
    rs = conn.createStatement().executeQuery("SELECT ARRAY_PREPEND(b,a) FROM " + tableName + " WHERE k = 'a'");
    assertTrue(rs.next());
    assertEquals(array2, rs.getArray(1));
}
Also used : PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) Test(org.junit.Test)

Aggregations

PhoenixArray (org.apache.phoenix.schema.types.PhoenixArray)107 Test (org.junit.Test)97 Connection (java.sql.Connection)25 ResultSet (java.sql.ResultSet)25 PSmallint (org.apache.phoenix.schema.types.PSmallint)25 PTinyint (org.apache.phoenix.schema.types.PTinyint)25 PUnsignedSmallint (org.apache.phoenix.schema.types.PUnsignedSmallint)25 PUnsignedTinyint (org.apache.phoenix.schema.types.PUnsignedTinyint)25 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)23 PreparedStatement (java.sql.PreparedStatement)7 Properties (java.util.Properties)7 BaseTest (org.apache.phoenix.query.BaseTest)7 Array (java.sql.Array)6 PDouble (org.apache.phoenix.schema.types.PDouble)5 PLong (org.apache.phoenix.schema.types.PLong)4 PUnsignedLong (org.apache.phoenix.schema.types.PUnsignedLong)4 Timestamp (java.sql.Timestamp)3 PTimestamp (org.apache.phoenix.schema.types.PTimestamp)3 PUnsignedDouble (org.apache.phoenix.schema.types.PUnsignedDouble)3 PUnsignedTimestamp (org.apache.phoenix.schema.types.PUnsignedTimestamp)3