Search in sources :

Example 1 with Accessor

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

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

the class DrillResultSetImpl method execute.

////////////////////////////////////////
@Override
protected DrillResultSetImpl execute() throws SQLException {
    connection.getDriver().handler.onStatementExecute(statement, null);
    if (signature.cursorFactory != null) {
        // Avatica accessors have to be wrapped to match Drill behaviour regarding exception thrown
        super.execute();
        List<Accessor> wrappedAccessorList = new ArrayList<>(accessorList.size());
        for (Accessor accessor : accessorList) {
            wrappedAccessorList.add(new WrappedAccessor(accessor));
        }
        this.accessorList = wrappedAccessorList;
    } else {
        DrillCursor drillCursor = new DrillCursor(connection, statement, signature);
        super.execute2(drillCursor, this.signature.columns);
        // Read first (schema-only) batch to initialize result-set metadata from
        // (initial) schema before Statement.execute...(...) returns result set:
        drillCursor.loadInitialSchema();
    }
    return this;
}
Also used : ArrayList(java.util.ArrayList) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor)

Example 3 with Accessor

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

the class DrillResultSetImpl method getObject.

@Override
public Object getObject(int columnIndex) throws SQLException {
    throwIfClosed();
    final Cursor.Accessor accessor;
    try {
        accessor = accessorList.get(columnIndex - 1);
    } catch (IndexOutOfBoundsException e) {
        throw new SQLException("invalid column ordinal: " + columnIndex);
    }
    final ColumnMetaData metaData = columnMetaDataList.get(columnIndex - 1);
    // Drill returns a float (4bytes) for a SQL Float whereas Calcite would return a double (8bytes)
    int typeId = (metaData.type.id != Types.FLOAT) ? metaData.type.id : Types.REAL;
    return AvaticaSite.get(accessor, typeId, localCalendar);
}
Also used : SQLException(java.sql.SQLException) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) Cursor(org.apache.calcite.avatica.util.Cursor) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Example 4 with Accessor

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

the class ArrayImplTest method resultSetFromIntegerArray.

@Test
public void resultSetFromIntegerArray() throws Exception {
    ScalarType intType = ColumnMetaData.scalar(Types.INTEGER, "INTEGER", Rep.INTEGER);
    ColumnMetaData arrayMetadata = createArrayMetaData(intType);
    List<List<Object>> rowsValues = Arrays.asList(Arrays.asList(1, 2), Collections.singletonList(3), Arrays.asList(4, 5, 6));
    try (Cursor cursor = CursorTestUtils.createArrayImplBasedCursor(rowsValues, intType, 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, Integer.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 5 with Accessor

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

the class ArrayFactoryImpl method createArray.

@Override
public Array createArray(AvaticaType elementType, Iterable<Object> elements) {
    final ArrayType array = ColumnMetaData.array(elementType, elementType.name, Rep.ARRAY);
    final List<ColumnMetaData> types = Collections.singletonList(ColumnMetaData.dummy(array, true));
    // Avoid creating a new List if we already have a List
    List<Object> elementList;
    if (elements instanceof List) {
        elementList = (List<Object>) elements;
    } else {
        elementList = new ArrayList<>();
        for (Object element : elements) {
            elementList.add(element);
        }
    }
    try (ListIteratorCursor cursor = new ListIteratorCursor(createRowForArrayData(elementList))) {
        List<Accessor> accessor = cursor.createAccessors(types, Unsafe.localCalendar(), this);
        assert 1 == accessor.size();
        return new ArrayImpl(elementList, (ArrayAccessor) accessor.get(0));
    }
}
Also used : ArrayType(org.apache.calcite.avatica.ColumnMetaData.ArrayType) ArrayList(java.util.ArrayList) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) ArrayAccessor(org.apache.calcite.avatica.util.AbstractCursor.ArrayAccessor) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor)

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