Search in sources :

Example 11 with Accessor

use of org.apache.calcite.avatica.util.Cursor.Accessor in project calcite-avatica by apache.

the class ArrayImplTest method resultSetFromFloatArray.

@Test
public void resultSetFromFloatArray() throws Exception {
    ScalarType floatType = ColumnMetaData.scalar(Types.FLOAT, "FLOAT", Rep.PRIMITIVE_DOUBLE);
    ColumnMetaData arrayMetadata = createArrayMetaData(floatType);
    List<List<Object>> rowsValues = Arrays.asList(Arrays.asList(1.123d, 0.123456789012d), Arrays.asList(4.134555d, 54444d, 66.12345d));
    try (Cursor cursor = CursorTestUtils.createArrayImplBasedCursor(rowsValues, floatType, ARRAY_FACTORY)) {
        Cursor.Accessor accessor = createArrayAccessor(cursor, arrayMetadata);
        int rowid = 0;
        while (cursor.next()) {
            List<Object> expectedArray = rowsValues.get(rowid);
            Assert.assertThat(accessor, isArrayAccessorResult(expectedArray, Double.class));
            rowid++;
        }
    }
}
Also used : ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) List(java.util.List) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) Test(org.junit.Test)

Example 12 with Accessor

use of org.apache.calcite.avatica.util.Cursor.Accessor in project calcite-avatica by apache.

the class ArrayImplTest method listOfJavaObjects.

/**
 * Test case for when a column is a list of plain Java objects.
 */
@Test
public void listOfJavaObjects() throws Exception {
    final ColumnMetaData.Rep rep = ColumnMetaData.Rep.of(PlainJavaObject.class);
    final ColumnMetaData.Rep rep2 = 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, rep2);
    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());
    List<Object> list1 = Arrays.asList(pjo1, pjo2);
    List<Object> list2 = Arrays.asList(pjo3, pjo4);
    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(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 13 with Accessor

use of org.apache.calcite.avatica.util.Cursor.Accessor 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 14 with Accessor

use of org.apache.calcite.avatica.util.Cursor.Accessor 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 15 with Accessor

use of org.apache.calcite.avatica.util.Cursor.Accessor 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

Accessor (org.apache.calcite.avatica.util.Cursor.Accessor)16 List (java.util.List)13 ColumnMetaData (org.apache.calcite.avatica.ColumnMetaData)13 Test (org.junit.Test)11 ArrayType (org.apache.calcite.avatica.ColumnMetaData.ArrayType)7 Array (java.sql.Array)5 ArrayList (java.util.ArrayList)5 ScalarType (org.apache.calcite.avatica.ColumnMetaData.ScalarType)5 Struct (java.sql.Struct)4 StructType (org.apache.calcite.avatica.ColumnMetaData.StructType)3 Rep (org.apache.calcite.avatica.ColumnMetaData.Rep)2 ArrayAccessor (org.apache.calcite.avatica.util.AbstractCursor.ArrayAccessor)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Calendar (java.util.Calendar)1 ArrayImpl (org.apache.calcite.avatica.util.ArrayImpl)1 Cursor (org.apache.calcite.avatica.util.Cursor)1 ListIteratorCursor (org.apache.calcite.avatica.util.ListIteratorCursor)1