Search in sources :

Example 11 with ScalarType

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

the class ArrayImplTest method resultSetFromArray.

@Test
public void resultSetFromArray() throws Exception {
    // Define the struct type we're creating
    ScalarType intType = ColumnMetaData.scalar(Types.INTEGER, "INTEGER", Rep.INTEGER);
    ArrayType arrayType = ColumnMetaData.array(intType, "INTEGER", Rep.INTEGER);
    ColumnMetaData arrayMetaData = MetaImpl.columnMetaData("MY_ARRAY", 1, arrayType, false);
    ArrayImpl.Factory factory = new ArrayFactoryImpl(Unsafe.localCalendar().getTimeZone());
    // Create some arrays from the structs
    Array array1 = factory.createArray(intType, Arrays.<Object>asList(1, 2));
    Array array2 = factory.createArray(intType, Arrays.<Object>asList(3));
    Array array3 = factory.createArray(intType, Arrays.<Object>asList(4, 5, 6));
    List<List<Object>> rows = Arrays.asList(Collections.<Object>singletonList(array1), Collections.<Object>singletonList(array2), Collections.<Object>singletonList(array3));
    // Create two rows, each with one (array) column
    try (Cursor cursor = new ListIteratorCursor(rows.iterator())) {
        List<Accessor> accessors = cursor.createAccessors(Collections.singletonList(arrayMetaData), Unsafe.localCalendar(), factory);
        assertEquals(1, accessors.size());
        Accessor accessor = accessors.get(0);
        assertTrue(cursor.next());
        Array actualArray = accessor.getArray();
        // An Array's result set has one row per array element.
        // Each row has two columns. Column 1 is the array offset (1-based), Column 2 is the value.
        ResultSet actualArrayResultSet = actualArray.getResultSet();
        assertEquals(2, actualArrayResultSet.getMetaData().getColumnCount());
        assertTrue(actualArrayResultSet.next());
        // Order is Avatica implementation specific
        assertEquals(1, actualArrayResultSet.getInt(1));
        assertEquals(1, actualArrayResultSet.getInt(2));
        assertTrue(actualArrayResultSet.next());
        assertEquals(2, actualArrayResultSet.getInt(1));
        assertEquals(2, actualArrayResultSet.getInt(2));
        assertFalse(actualArrayResultSet.next());
        assertTrue(cursor.next());
        actualArray = accessor.getArray();
        actualArrayResultSet = actualArray.getResultSet();
        assertEquals(2, actualArrayResultSet.getMetaData().getColumnCount());
        assertTrue(actualArrayResultSet.next());
        assertEquals(1, actualArrayResultSet.getInt(1));
        assertEquals(3, actualArrayResultSet.getInt(2));
        assertFalse(actualArrayResultSet.next());
        assertTrue(cursor.next());
        actualArray = accessor.getArray();
        actualArrayResultSet = actualArray.getResultSet();
        assertEquals(2, actualArrayResultSet.getMetaData().getColumnCount());
        assertTrue(actualArrayResultSet.next());
        assertEquals(1, actualArrayResultSet.getInt(1));
        assertEquals(4, actualArrayResultSet.getInt(2));
        assertTrue(actualArrayResultSet.next());
        assertEquals(2, actualArrayResultSet.getInt(1));
        assertEquals(5, actualArrayResultSet.getInt(2));
        assertTrue(actualArrayResultSet.next());
        assertEquals(3, actualArrayResultSet.getInt(1));
        assertEquals(6, actualArrayResultSet.getInt(2));
        assertFalse(actualArrayResultSet.next());
        assertFalse(cursor.next());
    }
}
Also used : ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) ArrayType(org.apache.calcite.avatica.ColumnMetaData.ArrayType) Array(java.sql.Array) ResultSet(java.sql.ResultSet) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) Test(org.junit.Test)

Example 12 with ScalarType

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

the class TypedValueTest method testArrays.

@Test
public void testArrays() {
    List<Object> serialObj = Arrays.<Object>asList(1, 2, 3, 4);
    ArrayImpl.Factory factory = new ArrayFactoryImpl(Unsafe.localCalendar().getTimeZone());
    ScalarType scalarType = ColumnMetaData.scalar(Types.INTEGER, "INTEGER", Rep.INTEGER);
    Array a1 = factory.createArray(scalarType, serialObj);
    TypedValue tv1 = TypedValue.ofJdbc(Rep.ARRAY, a1, Unsafe.localCalendar());
    Object jdbcObj = tv1.toJdbc(Unsafe.localCalendar());
    assertTrue("The JDBC object is an " + jdbcObj.getClass(), jdbcObj instanceof Array);
    Object localObj = tv1.toLocal();
    assertTrue("The local object is an " + localObj.getClass(), localObj instanceof List);
    Common.TypedValue protoTv1 = tv1.toProto();
    assertEquals(serialObj.size(), protoTv1.getArrayValueCount());
    TypedValue tv1Copy = TypedValue.fromProto(protoTv1);
    Object jdbcObjCopy = tv1Copy.toJdbc(Unsafe.localCalendar());
    assertTrue("The JDBC object is an " + jdbcObjCopy.getClass(), jdbcObjCopy instanceof Array);
    Object localObjCopy = tv1Copy.toLocal();
    assertTrue("The local object is an " + localObjCopy.getClass(), localObjCopy instanceof List);
}
Also used : Array(java.sql.Array) ArrayFactoryImpl(org.apache.calcite.avatica.util.ArrayFactoryImpl) ArrayImpl(org.apache.calcite.avatica.util.ArrayImpl) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) List(java.util.List) Common(org.apache.calcite.avatica.proto.Common) Test(org.junit.Test)

Example 13 with ScalarType

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

the class ArrayFactoryImpl method create.

@Override
public ResultSet create(AvaticaType elementType, Iterable<Object> elements) {
    // The ColumnMetaData for offset "1" in the ResultSet for an Array.
    ScalarType arrayOffsetType = ColumnMetaData.scalar(Types.INTEGER, "INTEGER", Rep.PRIMITIVE_INT);
    // Two columns (types) in the ResultSet we will create
    List<ColumnMetaData> types = Arrays.asList(ColumnMetaData.dummy(arrayOffsetType, false), ColumnMetaData.dummy(elementType, true));
    List<List<Object>> rows = createResultSetRowsForArrayData(elements);
    // `(List<Object>) rows` is a compile error.
    @SuppressWarnings({ "unchecked", "rawtypes" }) List<Object> untypedRows = (List<Object>) ((List) rows);
    try (ListIteratorCursor cursor = new ListIteratorCursor(rows.iterator())) {
        final String sql = "MOCKED";
        QueryState state = new QueryState(sql);
        Meta.Signature signature = new Meta.Signature(types, sql, Collections.<AvaticaParameter>emptyList(), Collections.<String, Object>emptyMap(), Meta.CursorFactory.LIST, Meta.StatementType.SELECT);
        AvaticaResultSetMetaData resultSetMetaData = new AvaticaResultSetMetaData(null, sql, signature);
        Meta.Frame frame = new Meta.Frame(0, true, untypedRows);
        AvaticaResultSet resultSet = new AvaticaResultSet(null, state, signature, resultSetMetaData, timeZone, frame);
        resultSet.execute2(cursor, types);
        return resultSet;
    }
}
Also used : Meta(org.apache.calcite.avatica.Meta) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) QueryState(org.apache.calcite.avatica.QueryState) AvaticaResultSet(org.apache.calcite.avatica.AvaticaResultSet) AvaticaResultSetMetaData(org.apache.calcite.avatica.AvaticaResultSetMetaData) ArrayList(java.util.ArrayList) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Example 14 with ScalarType

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

the class ArrayTypeTest method longArrays.

@Test
public void longArrays() 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 < 5; i++) {
            List<Long> elements = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                elements.add(r.nextLong());
            }
            arrays.add(createArray("BIGINT", component, elements));
        }
        // Verify read/write
        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 15 with ScalarType

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

the class ArrayTypeTest method timeArrays.

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

            @Override
            public void validate(Array expected, Array actual) throws SQLException {
                Object[] expectedTimes = (Object[]) expected.getArray();
                Object[] actualTimes = (Object[]) actual.getArray();
                assertEquals(expectedTimes.length, actualTimes.length);
                final Calendar cal = Unsafe.localCalendar();
                for (int i = 0; i < expectedTimes.length; i++) {
                    cal.setTime((Time) expectedTimes[i]);
                    int expectedHour = cal.get(Calendar.HOUR_OF_DAY);
                    int expectedMinute = cal.get(Calendar.MINUTE);
                    int expectedSecond = cal.get(Calendar.SECOND);
                    cal.setTime((Time) actualTimes[i]);
                    assertEquals(expectedHour, cal.get(Calendar.HOUR_OF_DAY));
                    assertEquals(expectedMinute, cal.get(Calendar.MINUTE));
                    assertEquals(expectedSecond, cal.get(Calendar.SECOND));
                }
            }
        });
        // Ensure an array with a null element can be written/read
        Array arrayWithNull = createArray("TIME", component, Arrays.asList((Time) null));
        writeAndReadArrays(conn, "time_array_with_null", "TIME", 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) Time(java.sql.Time) SQLException(java.sql.SQLException) Array(java.sql.Array) 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