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