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]);
}
}
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;
}
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);
}
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++;
}
}
}
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));
}
}
Aggregations