Search in sources :

Example 1 with StructType

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

the class StructImplTest method structAccessor.

@Test
public void structAccessor() 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", "ignored"));
    ColumnMetaData structMetaData = MetaImpl.columnMetaData("MY_STRUCT", 1, structType, false);
    List<List<Object>> rows = Arrays.asList(Collections.<Object>singletonList(struct1), Collections.<Object>singletonList(struct2), Collections.<Object>singletonList(struct3), Collections.<Object>singletonList(struct4));
    // Create four rows, each with one (struct) column
    try (Cursor cursor = new ListIteratorCursor(rows.iterator())) {
        List<Accessor> accessors = cursor.createAccessors(Collections.singletonList(structMetaData), Unsafe.localCalendar(), null);
        assertEquals(1, accessors.size());
        Accessor accessor = accessors.get(0);
        assertTrue(cursor.next());
        Struct s = accessor.getObject(Struct.class);
        Object[] structData = s.getAttributes();
        assertEquals(2, structData.length);
        assertEquals(1, structData[0]);
        assertEquals("one", structData[1]);
        assertTrue(cursor.next());
        s = accessor.getObject(Struct.class);
        structData = s.getAttributes();
        assertEquals(2, structData.length);
        assertEquals(2, structData[0]);
        assertEquals("two", structData[1]);
        assertTrue(cursor.next());
        s = accessor.getObject(Struct.class);
        structData = s.getAttributes();
        assertEquals(1, structData.length);
        assertEquals(3, structData[0]);
        assertTrue(cursor.next());
        s = accessor.getObject(Struct.class);
        structData = s.getAttributes();
        assertEquals(3, structData.length);
        assertEquals(4, structData[0]);
        assertEquals("four", structData[1]);
        // We didn't provide metadata, but we still expect to see it.
        assertEquals("ignored", structData[2]);
    }
}
Also used : StructType(org.apache.calcite.avatica.ColumnMetaData.StructType) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) Struct(java.sql.Struct) Test(org.junit.Test)

Example 2 with StructType

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

Struct (java.sql.Struct)2 List (java.util.List)2 ColumnMetaData (org.apache.calcite.avatica.ColumnMetaData)2 StructType (org.apache.calcite.avatica.ColumnMetaData.StructType)2 Accessor (org.apache.calcite.avatica.util.Cursor.Accessor)2 Test (org.junit.Test)2 Array (java.sql.Array)1 ArrayType (org.apache.calcite.avatica.ColumnMetaData.ArrayType)1