Search in sources :

Example 61 with PhoenixArray

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();
}
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 62 with PhoenixArray

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();
    }
}
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 63 with PhoenixArray

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);
}
Also used : LiteralExpression(org.apache.phoenix.expression.LiteralExpression) PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray)

Example 64 with PhoenixArray

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;
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple) Tuple(org.apache.phoenix.schema.tuple.Tuple) SingleKeyValueTuple(org.apache.phoenix.schema.tuple.SingleKeyValueTuple)

Example 65 with PhoenixArray

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