Search in sources :

Example 16 with ScalarType

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

the class ArrayTypeTest method shortArraysWithNull.

@Test
public void shortArraysWithNull() throws Exception {
    final Random r = new Random();
    try (Connection conn = DriverManager.getConnection(url)) {
        ScalarType component = ColumnMetaData.scalar(Types.SMALLINT, "SMALLINT", Rep.SHORT);
        List<Array> arrays = new ArrayList<>();
        // Construct the data
        for (int i = 0; i < 5; i++) {
            List<Short> elements = new ArrayList<>();
            for (int j = 0; j < 4; j++) {
                short value = (short) r.nextInt(Short.MAX_VALUE);
                // 50% of the time, negate the value
                if (0 == r.nextInt(2)) {
                    value *= -1;
                }
                elements.add(Short.valueOf(value));
            }
            elements.add(null);
            arrays.add(createArray("SMALLINT", component, elements));
        }
        // Verify read/write
        writeAndReadArrays(conn, "short_arrays", "SMALLINT", 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 17 with ScalarType

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

the class ArrayTypeTest method doubleArrays.

@Test
public void doubleArrays() throws Exception {
    final Random r = new Random();
    try (Connection conn = DriverManager.getConnection(url)) {
        ScalarType component = ColumnMetaData.scalar(Types.DOUBLE, "DOUBLE", Rep.DOUBLE);
        List<Array> arrays = new ArrayList<>();
        // Construct the data
        for (int i = 0; i < 3; i++) {
            List<Double> elements = new ArrayList<>();
            for (int j = 0; j < 7; j++) {
                double element = r.nextDouble();
                if (r.nextBoolean()) {
                    element *= -1;
                }
                elements.add(element);
            }
            arrays.add(createArray("DOUBLE", component, elements));
        }
        writeAndReadArrays(conn, "float_arrays", "DOUBLE", 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 18 with ScalarType

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

the class ArrayTypeTest method booleanArrays.

@Test
public void booleanArrays() throws Exception {
    final Random r = new Random();
    try (Connection conn = DriverManager.getConnection(url)) {
        ScalarType component = ColumnMetaData.scalar(Types.BOOLEAN, "BOOLEAN", Rep.BOOLEAN);
        List<Array> arrays = new ArrayList<>();
        // Construct the data
        for (int i = 0; i < 5; i++) {
            List<Boolean> elements = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                switch(r.nextInt(3)) {
                    case 0:
                        elements.add(Boolean.FALSE);
                        break;
                    case 1:
                        elements.add(Boolean.TRUE);
                        break;
                    case 2:
                        elements.add(null);
                        break;
                    default:
                        fail();
                }
            }
            arrays.add(createArray("BOOLEAN", component, elements));
        }
        // Verify we can read and write the data
        writeAndReadArrays(conn, "boolean_arrays", "BOOLEAN", 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