Search in sources :

Example 6 with ScalarType

use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.

the class ArrayTypeTest method varbinaryArrays.

@Test
public void varbinaryArrays() throws Exception {
    try (Connection conn = DriverManager.getConnection(url)) {
        ScalarType component = ColumnMetaData.scalar(Types.VARBINARY, "VARBINARY", Rep.BYTE_STRING);
        // [ Array(binary, binary, binary), Array(binary, binary, binary), ...]
        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++) {
                elements.add((i + "_" + j).getBytes(UTF_8));
            }
            arrays.add(createArray("VARBINARY", component, elements));
        }
        writeAndReadArrays(conn, "binary_arrays", "VARBINARY", component, arrays, BYTE_ARRAY_ARRAY_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 7 with ScalarType

use of org.apache.calcite.avatica.ColumnMetaData.ScalarType 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 8 with ScalarType

use of org.apache.calcite.avatica.ColumnMetaData.ScalarType 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 9 with ScalarType

use of org.apache.calcite.avatica.ColumnMetaData.ScalarType 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 10 with ScalarType

use of org.apache.calcite.avatica.ColumnMetaData.ScalarType 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)

Aggregations

ScalarType (org.apache.calcite.avatica.ColumnMetaData.ScalarType)18 Array (java.sql.Array)16 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)15 Connection (java.sql.Connection)13 Random (java.util.Random)7 List (java.util.List)4 SQLException (java.sql.SQLException)3 Calendar (java.util.Calendar)3 ColumnMetaData (org.apache.calcite.avatica.ColumnMetaData)3 Time (java.sql.Time)2 Meta (org.apache.calcite.avatica.Meta)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Date (java.sql.Date)1 ResultSet (java.sql.ResultSet)1 Timestamp (java.sql.Timestamp)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1