Search in sources :

Example 6 with ArrayType

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

the class ArrayImplTest method listOfStructs.

/**
 * The same test as arrayOfStructs(), except we use List instead of ArrayImpl.
 */
@Test
public void listOfStructs() throws Exception {
    ColumnMetaData intMetaData = MetaImpl.columnMetaData("MY_INT", 1, int.class, false);
    ColumnMetaData stringMetaData = MetaImpl.columnMetaData("MY_STRING", 2, String.class, true);
    StructType structType = ColumnMetaData.struct(Arrays.asList(intMetaData, stringMetaData));
    Struct struct1 = new StructImpl(Arrays.asList(1, "one"));
    Struct struct2 = new StructImpl(Arrays.asList(2, "two"));
    Struct struct3 = new StructImpl(Arrays.asList(3));
    Struct struct4 = new StructImpl(Arrays.asList(4, "four"));
    ArrayType arrayType = ColumnMetaData.array(structType, "OBJECT", Rep.STRUCT);
    ColumnMetaData arrayMetaData = MetaImpl.columnMetaData("MY_ARRAY", 1, arrayType, false);
    ArrayImpl.Factory factory = new ArrayFactoryImpl(Unsafe.localCalendar().getTimeZone());
    List<Object> list1 = Arrays.asList(struct1, struct2);
    List<Object> list2 = Arrays.asList(struct3, struct4);
    List<List<Object>> rows = Arrays.asList(Arrays.asList(list1), Arrays.asList(list2));
    try (Cursor cursor = new ListIteratorCursor(rows.iterator())) {
        List<Accessor> accessors = cursor.createAccessors(Arrays.asList(arrayMetaData), Unsafe.localCalendar(), factory);
        assertEquals(1, accessors.size());
        Accessor accessor = accessors.get(0);
        assertTrue(cursor.next());
        Array actualArray = accessor.getArray();
        Object[] arrayData = (Object[]) actualArray.getArray();
        assertEquals(2, arrayData.length);
        Struct actualStruct = (Struct) arrayData[0];
        Object[] o = actualStruct.getAttributes();
        assertEquals(2, o.length);
        assertEquals(1, o[0]);
        assertEquals("one", o[1]);
        actualStruct = (Struct) arrayData[1];
        o = actualStruct.getAttributes();
        assertEquals(2, o.length);
        assertEquals(2, o[0]);
        assertEquals("two", o[1]);
        assertTrue(cursor.next());
        actualArray = accessor.getArray();
        arrayData = (Object[]) actualArray.getArray();
        assertEquals(2, arrayData.length);
        actualStruct = (Struct) arrayData[0];
        o = actualStruct.getAttributes();
        assertEquals(1, o.length);
        assertEquals(3, o[0]);
        actualStruct = (Struct) arrayData[1];
        o = actualStruct.getAttributes();
        assertEquals(2, o.length);
        assertEquals(4, o[0]);
        assertEquals("four", o[1]);
    }
}
Also used : StructType(org.apache.calcite.avatica.ColumnMetaData.StructType) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) Struct(java.sql.Struct) ArrayType(org.apache.calcite.avatica.ColumnMetaData.ArrayType) Array(java.sql.Array) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) Test(org.junit.Test)

Example 7 with ArrayType

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

the class ArrayImplTest method arraysOfJavaObjects.

/**
 * Test case for when a column is an array of plain Java objects.
 * This is a common use case when data come from a dynamic schema source.
 */
@Test
public void arraysOfJavaObjects() throws Exception {
    final ColumnMetaData.Rep rep = ColumnMetaData.Rep.of(PlainJavaObject.class);
    ColumnMetaData.AvaticaType objectAvaticaType = ColumnMetaData.scalar(Types.OTHER, "OTHER", rep);
    String arrayTypeName = "JavaType(" + PlainJavaObject.class.toString() + ") ARRAY";
    ArrayType arrayType = ColumnMetaData.array(objectAvaticaType, arrayTypeName, rep);
    ColumnMetaData javaObjectArrayMetaData = MetaImpl.columnMetaData("PLAIN_JAVA_OBJECT_ARRAY", 1, arrayType, true);
    PlainJavaObject pjo1 = new PlainJavaObject(1, "one");
    PlainJavaObject pjo2 = new PlainJavaObject(2, "two");
    PlainJavaObject pjo3 = new PlainJavaObject(3, "three");
    PlainJavaObject pjo4 = new PlainJavaObject(4, "four");
    ArrayImpl.Factory factory = new ArrayFactoryImpl(Unsafe.localCalendar().getTimeZone());
    Array array1 = factory.createArray(objectAvaticaType, Arrays.asList(pjo1, pjo2));
    Array array2 = factory.createArray(objectAvaticaType, Arrays.asList(pjo3, pjo4));
    List<List<Object>> rows = Arrays.asList(Arrays.asList(array1), Arrays.asList(array2));
    try (Cursor cursor = new ListIteratorCursor(rows.iterator())) {
        List<Accessor> accessors = cursor.createAccessors(Arrays.asList(javaObjectArrayMetaData), Unsafe.localCalendar(), factory);
        assertEquals(1, accessors.size());
        Accessor accessor = accessors.get(0);
        assertTrue(cursor.next());
        Array actualArray = accessor.getArray();
        Object[] arrayData = (Object[]) actualArray.getArray();
        assertEquals(2, arrayData.length);
        assertEquals(pjo1, arrayData[0]);
        assertEquals(pjo2, arrayData[1]);
        assertTrue(cursor.next());
        actualArray = accessor.getArray();
        arrayData = (Object[]) actualArray.getArray();
        assertEquals(2, arrayData.length);
        assertEquals(pjo3, arrayData[0]);
        assertEquals(pjo4, arrayData[1]);
    }
}
Also used : Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) ArrayType(org.apache.calcite.avatica.ColumnMetaData.ArrayType) Array(java.sql.Array) Rep(org.apache.calcite.avatica.ColumnMetaData.Rep) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) Test(org.junit.Test)

Example 8 with ArrayType

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

the class ArrayImplTest method arraysOfStructs.

@Test
public void arraysOfStructs() throws Exception {
    // Define the struct type we're creating
    ColumnMetaData intMetaData = MetaImpl.columnMetaData("MY_INT", 1, int.class, false);
    ColumnMetaData stringMetaData = MetaImpl.columnMetaData("MY_STRING", 2, String.class, true);
    StructType structType = ColumnMetaData.struct(Arrays.asList(intMetaData, stringMetaData));
    // Create some structs
    Struct struct1 = new StructImpl(Arrays.<Object>asList(1, "one"));
    Struct struct2 = new StructImpl(Arrays.<Object>asList(2, "two"));
    Struct struct3 = new StructImpl(Arrays.<Object>asList(3));
    Struct struct4 = new StructImpl(Arrays.<Object>asList(4, "four"));
    ArrayType arrayType = ColumnMetaData.array(structType, "OBJECT", Rep.STRUCT);
    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(structType, Arrays.<Object>asList(struct1, struct2));
    Array array2 = factory.createArray(structType, Arrays.<Object>asList(struct3, struct4));
    List<List<Object>> rows = Arrays.asList(Collections.<Object>singletonList(array1), Collections.<Object>singletonList(array2));
    // 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();
        // Avoiding explicit use of the getResultSet() method for now..
        Object[] arrayData = (Object[]) actualArray.getArray();
        assertEquals(2, arrayData.length);
        Struct actualStruct = (Struct) arrayData[0];
        Object[] o = actualStruct.getAttributes();
        assertEquals(2, o.length);
        assertEquals(1, o[0]);
        assertEquals("one", o[1]);
        actualStruct = (Struct) arrayData[1];
        o = actualStruct.getAttributes();
        assertEquals(2, o.length);
        assertEquals(2, o[0]);
        assertEquals("two", o[1]);
        assertTrue(cursor.next());
        actualArray = accessor.getArray();
        arrayData = (Object[]) actualArray.getArray();
        assertEquals(2, arrayData.length);
        actualStruct = (Struct) arrayData[0];
        o = actualStruct.getAttributes();
        assertEquals(1, o.length);
        assertEquals(3, o[0]);
        actualStruct = (Struct) arrayData[1];
        o = actualStruct.getAttributes();
        assertEquals(2, o.length);
        assertEquals(4, o[0]);
        assertEquals("four", o[1]);
    }
}
Also used : StructType(org.apache.calcite.avatica.ColumnMetaData.StructType) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) Struct(java.sql.Struct) ArrayType(org.apache.calcite.avatica.ColumnMetaData.ArrayType) Array(java.sql.Array) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) Test(org.junit.Test)

Aggregations

ColumnMetaData (org.apache.calcite.avatica.ColumnMetaData)8 ArrayType (org.apache.calcite.avatica.ColumnMetaData.ArrayType)8 List (java.util.List)7 Accessor (org.apache.calcite.avatica.util.Cursor.Accessor)7 Array (java.sql.Array)6 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 Struct (java.sql.Struct)2 Rep (org.apache.calcite.avatica.ColumnMetaData.Rep)2 StructType (org.apache.calcite.avatica.ColumnMetaData.StructType)2 ArrayAccessor (org.apache.calcite.avatica.util.AbstractCursor.ArrayAccessor)2 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Calendar (java.util.Calendar)1 HashSet (java.util.HashSet)1 AvaticaType (org.apache.calcite.avatica.ColumnMetaData.AvaticaType)1 ScalarType (org.apache.calcite.avatica.ColumnMetaData.ScalarType)1 SqlType (org.apache.calcite.avatica.SqlType)1 ArrayImpl (org.apache.calcite.avatica.util.ArrayImpl)1 ListIteratorCursor (org.apache.calcite.avatica.util.ListIteratorCursor)1