use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.
the class ArrayIT method testArrayWithCast.
@Test
public void testArrayWithCast() 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 bigint 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");
Long[] s = new Long[] { 1l, 2l };
Array array = conn.createArrayOf("BIGINT", 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 DOUBLE []) FROM t");
assertTrue(rs.next());
Double[] d = new Double[] { 1.0, 2.0 };
array = conn.createArrayOf("DOUBLE", d);
PhoenixArray arr = (PhoenixArray) rs.getArray(1);
assertEquals(array, arr);
assertEquals("[1.0, 2.0]", rs.getString(1));
conn.close();
}
use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.
the class ArrayIT method testWithOutOfRangeIndex.
@Test
public void testWithOutOfRangeIndex() 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[100] 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());
PhoenixArray resultArray = (PhoenixArray) rs.getArray(1);
assertNull(resultArray);
} finally {
conn.close();
}
}
use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.
the class ExpressionCompiler method visitLeave.
@Override
public Expression visitLeave(ExistsParseNode node, List<Expression> l) throws SQLException {
LiteralExpression child = (LiteralExpression) l.get(0);
PhoenixArray array = (PhoenixArray) child.getValue();
return LiteralExpression.newConstant(array.getDimensions() > 0 ^ node.isNegate(), PBoolean.INSTANCE);
}
use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.
the class UnnestArrayPlanTest method toTuples.
private List<Tuple> toTuples(PArrayDataType arrayType, List<Object[]> arrays) {
List<Tuple> tuples = Lists.newArrayListWithExpectedSize(arrays.size());
PDataType baseType = PDataType.fromTypeId(arrayType.getSqlType() - PDataType.ARRAY_TYPE_BASE);
for (Object[] array : arrays) {
PhoenixArray pArray = new PhoenixArray(baseType, array);
byte[] bytes = arrayType.toBytes(pArray);
tuples.add(new SingleKeyValueTuple(KeyValueUtil.newKeyValue(bytes, 0, bytes.length, bytes, 0, 0, bytes, 0, 0, 0, bytes, 0, 0)));
}
return tuples;
}
use of org.apache.phoenix.schema.types.PhoenixArray in project phoenix by apache.
the class PDataTypeForArraysTest method testForVarCharArrayForOddNumber.
@Test
public void testForVarCharArrayForOddNumber() {
String[] strArr = new String[3];
strArr[0] = "abx";
strArr[1] = "ereref";
strArr[2] = "random";
PhoenixArray arr = PArrayDataType.instantiatePhoenixArray(PVarchar.INSTANCE, strArr);
byte[] bytes = PVarcharArray.INSTANCE.toBytes(arr);
PhoenixArray resultArr = (PhoenixArray) PVarcharArray.INSTANCE.toObject(bytes, 0, bytes.length);
assertEquals(arr, resultArr);
}
Aggregations