Search in sources :

Example 21 with Array

use of java.sql.Array in project calcite-avatica by apache.

the class ArrayTypeTest method simpleArrayTest.

@Test
public void simpleArrayTest() throws Exception {
    try (Connection conn = DriverManager.getConnection(url)) {
        ScalarType varcharComponent = ColumnMetaData.scalar(Types.VARCHAR, "VARCHAR", Rep.STRING);
        List<Array> varcharArrays = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            List<String> value = Collections.singletonList(Integer.toString(i));
            varcharArrays.add(createArray("VARCHAR", varcharComponent, value));
        }
        writeAndReadArrays(conn, "varchar_arrays", "VARCHAR(30)", varcharComponent, varcharArrays, PRIMITIVE_LIST_VALIDATOR);
    }
}
Also used : Array(java.sql.Array) Connection(java.sql.Connection) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 22 with Array

use of java.sql.Array in project calcite-avatica by apache.

the class ArrayTypeTest method timestampArrays.

@Test
public void timestampArrays() throws Exception {
    try (Connection conn = DriverManager.getConnection(url)) {
        final long now = System.currentTimeMillis();
        ScalarType component = ColumnMetaData.scalar(Types.TIMESTAMP, "TIMESTAMP", Rep.JAVA_SQL_TIMESTAMP);
        List<Array> arrays = new ArrayList<>();
        // Construct the data
        for (int i = 0; i < 5; i++) {
            List<Timestamp> elements = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                elements.add(new Timestamp(now + i + j));
            }
            arrays.add(createArray("TIMESTAMP", component, elements));
        }
        writeAndReadArrays(conn, "timestamp_arrays", "TIMESTAMP", component, arrays, new Validator<Array>() {

            @Override
            public void validate(Array expected, Array actual) throws SQLException {
                Object[] expectedTimestamps = (Object[]) expected.getArray();
                Object[] actualTimestamps = (Object[]) actual.getArray();
                assertEquals(expectedTimestamps.length, actualTimestamps.length);
                final Calendar cal = Unsafe.localCalendar();
                for (int i = 0; i < expectedTimestamps.length; i++) {
                    cal.setTime((Timestamp) expectedTimestamps[i]);
                    int expectedDayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
                    int expectedMonth = cal.get(Calendar.MONTH);
                    int expectedYear = cal.get(Calendar.YEAR);
                    int expectedHour = cal.get(Calendar.HOUR_OF_DAY);
                    int expectedMinute = cal.get(Calendar.MINUTE);
                    int expectedSecond = cal.get(Calendar.SECOND);
                    int expectedMillisecond = cal.get(Calendar.MILLISECOND);
                    cal.setTime((Timestamp) actualTimestamps[i]);
                    assertEquals(expectedDayOfMonth, cal.get(Calendar.DAY_OF_MONTH));
                    assertEquals(expectedMonth, cal.get(Calendar.MONTH));
                    assertEquals(expectedYear, cal.get(Calendar.YEAR));
                    assertEquals(expectedHour, cal.get(Calendar.HOUR_OF_DAY));
                    assertEquals(expectedMinute, cal.get(Calendar.MINUTE));
                    assertEquals(expectedSecond, cal.get(Calendar.SECOND));
                    assertEquals(expectedMillisecond, cal.get(Calendar.MILLISECOND));
                }
            }
        });
        // Ensure an array with a null element can be written/read
        Array arrayWithNull = createArray("TIMESTAMP", component, Arrays.asList((Timestamp) null));
        writeAndReadArrays(conn, "timestamp_array_with_null", "TIMESTAMP", component, Collections.singletonList(arrayWithNull), new Validator<Array>() {

            @Override
            public void validate(Array expected, Array actual) throws Exception {
                Object[] expectedArray = (Object[]) expected.getArray();
                Object[] actualArray = (Object[]) actual.getArray();
                assertEquals(1, expectedArray.length);
                assertEquals(expectedArray.length, actualArray.length);
                assertEquals(expectedArray[0], actualArray[0]);
            }
        });
    }
}
Also used : SQLException(java.sql.SQLException) Calendar(java.util.Calendar) Connection(java.sql.Connection) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) ArrayList(java.util.ArrayList) Timestamp(java.sql.Timestamp) SQLException(java.sql.SQLException) Array(java.sql.Array) Test(org.junit.Test)

Example 23 with Array

use of java.sql.Array in project calcite-avatica by apache.

the class ArrayTypeTest method arraysOfByteArrays.

@Test
public void arraysOfByteArrays() throws Exception {
    final Random r = new Random();
    try (Connection conn = DriverManager.getConnection(url)) {
        ScalarType component = ColumnMetaData.scalar(Types.TINYINT, "TINYINT", Rep.BYTE);
        // [ Array([b, b, b]), Array([b, b, b]), ... ]
        List<Array> arrays = new ArrayList<>();
        // Construct the data
        for (int i = 0; i < 5; i++) {
            List<Byte> elements = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                byte value = (byte) r.nextInt(Byte.MAX_VALUE);
                // 50% of the time, negate the value
                if (0 == r.nextInt(2)) {
                    value *= -1;
                }
                elements.add(Byte.valueOf(value));
            }
            arrays.add(createArray("TINYINT", component, elements));
        }
        // Verify read/write
        writeAndReadArrays(conn, "byte_arrays", "TINYINT", component, arrays, BYTE_ARRAY_VALIDATOR);
    }
}
Also used : Array(java.sql.Array) Random(java.util.Random) Connection(java.sql.Connection) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 24 with Array

use of java.sql.Array in project calcite-avatica by apache.

the class ArrayTypeTest method bigintArrays.

@Test
public void bigintArrays() throws Exception {
    final Random r = new Random();
    try (Connection conn = DriverManager.getConnection(url)) {
        ScalarType component = ColumnMetaData.scalar(Types.BIGINT, "BIGINT", Rep.LONG);
        List<Array> arrays = new ArrayList<>();
        // Construct the data
        for (int i = 0; i < 3; i++) {
            List<Long> elements = new ArrayList<>();
            for (int j = 0; j < 7; j++) {
                long element = r.nextLong();
                if (r.nextBoolean()) {
                    element *= -1;
                }
                elements.add(element);
            }
            arrays.add(createArray("BIGINT", component, elements));
        }
        writeAndReadArrays(conn, "long_arrays", "BIGINT", component, arrays, PRIMITIVE_LIST_VALIDATOR);
    }
}
Also used : Array(java.sql.Array) Random(java.util.Random) Connection(java.sql.Connection) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 25 with Array

use of java.sql.Array in project calcite-avatica by apache.

the class RemoteMetaTest method testArrays.

@Test
public void testArrays() throws SQLException {
    ConnectionSpec.getDatabaseLock().lock();
    try (AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url);
        Statement stmt = conn.createStatement()) {
        ResultSet resultSet = stmt.executeQuery("select * from (values ('a', array['b', 'c']));");
        assertTrue(resultSet.next());
        assertEquals("a", resultSet.getString(1));
        Array arr = resultSet.getArray(2);
        assertTrue(arr instanceof ArrayImpl);
        Object[] values = (Object[]) ((ArrayImpl) arr).getArray();
        assertArrayEquals(new String[] { "b", "c" }, values);
    } finally {
        ConnectionSpec.getDatabaseLock().unlock();
    }
}
Also used : Array(java.sql.Array) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) PreparedStatement(java.sql.PreparedStatement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) Statement(java.sql.Statement) ArrayImpl(org.apache.calcite.avatica.util.ArrayImpl) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Aggregations

Array (java.sql.Array)124 Test (org.junit.Test)85 Connection (java.sql.Connection)74 ResultSet (java.sql.ResultSet)69 PreparedStatement (java.sql.PreparedStatement)46 Properties (java.util.Properties)29 PhoenixArray (org.apache.phoenix.schema.types.PhoenixArray)25 ArrayList (java.util.ArrayList)24 BaseTest (org.apache.phoenix.query.BaseTest)22 SQLException (java.sql.SQLException)19 ScalarType (org.apache.calcite.avatica.ColumnMetaData.ScalarType)16 Timestamp (java.sql.Timestamp)10 Date (java.sql.Date)8 Random (java.util.Random)7 Time (java.sql.Time)6 Statement (java.sql.Statement)5 Struct (java.sql.Struct)5 HashMap (java.util.HashMap)5 List (java.util.List)5 BigDecimal (java.math.BigDecimal)4