Search in sources :

Example 6 with Accessor

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

the class ArrayTypeTest method createArray.

/**
 * Creates a JDBC {@link Array} from a list of values.
 *
 * @param typeName the SQL type name of the elements in the array
 * @param componentType The Avatica type for the array elements
 * @param arrayValues The array elements
 * @return An Array instance for the given component and values
 */
@SuppressWarnings("unchecked")
private <T> Array createArray(String typeName, AvaticaType componentType, List<T> arrayValues) {
    // Make a "row" with one "column" (which is really a list)
    final List<Object> oneRow = Collections.singletonList((Object) arrayValues);
    // Make an iterator over this one "row"
    final Iterator<List<Object>> rowIterator = Collections.singletonList(oneRow).iterator();
    ArrayType array = ColumnMetaData.array(componentType, typeName, Rep.ARRAY);
    try (ListIteratorCursor cursor = new ListIteratorCursor(rowIterator)) {
        List<ColumnMetaData> types = Collections.singletonList(ColumnMetaData.dummy(array, true));
        Calendar calendar = Unsafe.localCalendar();
        List<Accessor> accessors = cursor.createAccessors(types, calendar, null);
        assertFalse("Expected at least one accessor, found " + accessors.size(), accessors.isEmpty());
        ArrayAccessor arrayAccessor = (ArrayAccessor) accessors.get(0);
        return new ArrayImpl((List<Object>) arrayValues, arrayAccessor);
    }
}
Also used : Calendar(java.util.Calendar) ArrayImpl(org.apache.calcite.avatica.util.ArrayImpl) ArrayAccessor(org.apache.calcite.avatica.util.AbstractCursor.ArrayAccessor) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) ArrayAccessor(org.apache.calcite.avatica.util.AbstractCursor.ArrayAccessor) ArrayType(org.apache.calcite.avatica.ColumnMetaData.ArrayType) ListIteratorCursor(org.apache.calcite.avatica.util.ListIteratorCursor) ArrayList(java.util.ArrayList) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Example 7 with Accessor

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

the class ArrayImplTest method resultSetFromArray.

@Test
public void resultSetFromArray() throws Exception {
    // Define the struct type we're creating
    ScalarType intType = ColumnMetaData.scalar(Types.INTEGER, "INTEGER", Rep.INTEGER);
    ArrayType arrayType = ColumnMetaData.array(intType, "INTEGER", Rep.INTEGER);
    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(intType, Arrays.<Object>asList(1, 2));
    Array array2 = factory.createArray(intType, Arrays.<Object>asList(3));
    Array array3 = factory.createArray(intType, Arrays.<Object>asList(4, 5, 6));
    List<List<Object>> rows = Arrays.asList(Collections.<Object>singletonList(array1), Collections.<Object>singletonList(array2), Collections.<Object>singletonList(array3));
    // 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();
        // An Array's result set has one row per array element.
        // Each row has two columns. Column 1 is the array offset (1-based), Column 2 is the value.
        ResultSet actualArrayResultSet = actualArray.getResultSet();
        assertEquals(2, actualArrayResultSet.getMetaData().getColumnCount());
        assertTrue(actualArrayResultSet.next());
        // Order is Avatica implementation specific
        assertEquals(1, actualArrayResultSet.getInt(1));
        assertEquals(1, actualArrayResultSet.getInt(2));
        assertTrue(actualArrayResultSet.next());
        assertEquals(2, actualArrayResultSet.getInt(1));
        assertEquals(2, actualArrayResultSet.getInt(2));
        assertFalse(actualArrayResultSet.next());
        assertTrue(cursor.next());
        actualArray = accessor.getArray();
        actualArrayResultSet = actualArray.getResultSet();
        assertEquals(2, actualArrayResultSet.getMetaData().getColumnCount());
        assertTrue(actualArrayResultSet.next());
        assertEquals(1, actualArrayResultSet.getInt(1));
        assertEquals(3, actualArrayResultSet.getInt(2));
        assertFalse(actualArrayResultSet.next());
        assertTrue(cursor.next());
        actualArray = accessor.getArray();
        actualArrayResultSet = actualArray.getResultSet();
        assertEquals(2, actualArrayResultSet.getMetaData().getColumnCount());
        assertTrue(actualArrayResultSet.next());
        assertEquals(1, actualArrayResultSet.getInt(1));
        assertEquals(4, actualArrayResultSet.getInt(2));
        assertTrue(actualArrayResultSet.next());
        assertEquals(2, actualArrayResultSet.getInt(1));
        assertEquals(5, actualArrayResultSet.getInt(2));
        assertTrue(actualArrayResultSet.next());
        assertEquals(3, actualArrayResultSet.getInt(1));
        assertEquals(6, actualArrayResultSet.getInt(2));
        assertFalse(actualArrayResultSet.next());
        assertFalse(cursor.next());
    }
}
Also used : ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) ArrayType(org.apache.calcite.avatica.ColumnMetaData.ArrayType) Array(java.sql.Array) ResultSet(java.sql.ResultSet) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) Test(org.junit.Test)

Example 8 with Accessor

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

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);
        // Getting handle to elapsed timer for timeout purposes
        this.elapsedTimer = drillCursor.getElapsedTimer();
        // Setting this to ensure future calls to change timeouts for an active Statement doesn't affect ResultSet
        this.queryTimeoutInMilliseconds = drillCursor.getTimeoutInMilliseconds();
        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 9 with Accessor

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

the class ArrayImplTest method resultSetFromRealArray.

@Test
public void resultSetFromRealArray() throws Exception {
    ScalarType realType = ColumnMetaData.scalar(Types.REAL, "REAL", Rep.FLOAT);
    ColumnMetaData arrayMetadata = createArrayMetaData(realType);
    List<List<Object>> rowsValues = Arrays.asList(Arrays.asList(1.123f, 0.2f), Arrays.asList(4.1f, 5f, 66.12345f));
    try (Cursor cursor = CursorTestUtils.createArrayImplBasedCursor(rowsValues, realType, 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, Float.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 10 with Accessor

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

the class ArrayImplTest method resultSetFromDoubleArray.

@Test
public void resultSetFromDoubleArray() throws Exception {
    ScalarType doubleType = ColumnMetaData.scalar(Types.DOUBLE, "DOUBLE", Rep.PRIMITIVE_DOUBLE);
    ColumnMetaData arrayMetadata = createArrayMetaData(doubleType);
    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, doubleType, 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)

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