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();
}
}
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();
}
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));
}
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();
}
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));
}
Aggregations