Search in sources :

Example 1 with Rep

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

the class TypedValue method fromProto.

/**
 * Constructs a {@link TypedValue} from the protocol buffer representation.
 *
 * @param proto The protobuf Typedvalue
 * @return A {@link TypedValue} instance
 */
public static TypedValue fromProto(Common.TypedValue proto) {
    ColumnMetaData.Rep rep = ColumnMetaData.Rep.fromProto(proto.getType());
    ColumnMetaData.Rep componentRep = ColumnMetaData.Rep.fromProto(proto.getComponentType());
    Object value = getSerialFromProto(proto);
    return new TypedValue(rep, componentRep, value);
}
Also used : Rep(org.apache.calcite.avatica.ColumnMetaData.Rep) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Example 2 with Rep

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

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

Aggregations

ColumnMetaData (org.apache.calcite.avatica.ColumnMetaData)3 Rep (org.apache.calcite.avatica.ColumnMetaData.Rep)3 Array (java.sql.Array)2 List (java.util.List)2 ArrayType (org.apache.calcite.avatica.ColumnMetaData.ArrayType)2 Accessor (org.apache.calcite.avatica.util.Cursor.Accessor)2 Test (org.junit.Test)2