Search in sources :

Example 56 with PhoenixArray

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

the class ArrayPrependFunctionIT method testArrayPrependFunctionNulls1.

@Test
public void testArrayPrependFunctionNulls1() throws Exception {
    Connection conn = DriverManager.getConnection(getUrl());
    String[] s = new String[] { null, null, "1", "2" };
    String tableName = generateUniqueName();
    initTableWithVarArray(conn, tableName, "VARCHAR", s, null);
    String[] s2 = new String[] { null, null, null, "1", "2" };
    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 57 with PhoenixArray

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

the class ArrayIT method testArrayWithCastForVarLengthArr.

@Test
public void testArrayWithCastForVarLengthArr() 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 PRIMARY KEY, a VARCHAR(5) ARRAY)");
    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[] { "1", "2" };
    PhoenixArray array = (PhoenixArray) conn.createArrayOf("VARCHAR", s);
    stmt.setArray(2, 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 CAST(a AS CHAR ARRAY) FROM t");
    assertTrue(rs.next());
    PhoenixArray arr = (PhoenixArray) rs.getArray(1);
    String[] array2 = (String[]) array.getArray();
    String[] array3 = (String[]) arr.getArray();
    assertEquals(array2[0], array3[0]);
    assertEquals(array2[1], array3[1]);
    assertEquals("['1', '2']", rs.getString(1));
    conn.close();
}
Also used : 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 58 with PhoenixArray

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

the class PhoenixListObjectInspector method getList.

@Override
public List<?> getList(Object data) {
    if (data == null) {
        return null;
    }
    PhoenixArray array = (PhoenixArray) data;
    int valueLength = array.getDimensions();
    List<Object> valueList = Lists.newArrayListWithExpectedSize(valueLength);
    for (int i = 0; i < valueLength; i++) {
        valueList.add(array.getElement(i));
    }
    return valueList;
}
Also used : PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray)

Example 59 with PhoenixArray

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

the class ArrayIT method testSelectArrayUsingUpsertLikeSyntax.

@Test
public void testSelectArrayUsingUpsertLikeSyntax() 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_double_array FROM TABLE_WITH_ARRAY WHERE a_double_array = CAST(ARRAY [ 25.343, 36.763, 37.56,386.63] AS DOUBLE 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());
        Double[] doubleArr = new Double[4];
        doubleArr[0] = 25.343;
        doubleArr[1] = 36.763;
        doubleArr[2] = 37.56;
        doubleArr[3] = 386.63;
        Array array = conn.createArrayOf("DOUBLE", doubleArr);
        PhoenixArray resultArray = (PhoenixArray) rs.getArray(1);
        assertEquals(resultArray, array);
        assertEquals("[25.343, 36.763, 37.56, 386.63]", 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 60 with PhoenixArray

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

the class ArrayIT method testScanByArrayValue.

@Test
public void testScanByArrayValue() 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_double_array, /* comment ok? */ b_string, a_float FROM table_with_array WHERE ?=organization_id and ?=a_float";
    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);
    analyzeTable(conn, TABLE_WITH_ARRAY);
    try {
        PreparedStatement statement = conn.prepareStatement(query);
        statement.setString(1, tenantId);
        statement.setFloat(2, 0.01f);
        ResultSet rs = statement.executeQuery();
        assertTrue(rs.next());
        // Need to support primitive
        Double[] doubleArr = new Double[4];
        doubleArr[0] = 25.343;
        doubleArr[1] = 36.763;
        doubleArr[2] = 37.56;
        doubleArr[3] = 386.63;
        Array array = conn.createArrayOf("DOUBLE", doubleArr);
        PhoenixArray resultArray = (PhoenixArray) rs.getArray(1);
        assertEquals(resultArray, array);
        assertEquals("[25.343, 36.763, 37.56, 386.63]", rs.getString(1));
        assertEquals(rs.getString("B_string"), B_VALUE);
        assertTrue(Floats.compare(rs.getFloat(3), 0.01f) == 0);
        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)

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