use of org.apache.calcite.avatica.ColumnMetaData in project drill by apache.
the class DrillMetaImpl method drillFieldMetaData.
// Overriding fieldMetaData as Calcite version create ColumnMetaData with invalid offset
protected static ColumnMetaData.StructType drillFieldMetaData(Class<?> clazz) {
final List<ColumnMetaData> list = new ArrayList<>();
for (Field field : clazz.getFields()) {
if (Modifier.isPublic(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())) {
NotNull notNull = field.getAnnotation(NotNull.class);
boolean notNullable = (notNull != null || field.getType().isPrimitive());
list.add(drillColumnMetaData(AvaticaUtils.camelToUpper(field.getName()), list.size(), field.getType(), notNullable));
}
}
return ColumnMetaData.struct(list);
}
use of org.apache.calcite.avatica.ColumnMetaData in project drill by apache.
the class DrillColumnMetaDataList method updateColumnMetaData.
/**
* Update the metadata with given metadata received from server.
* @param metadata
*/
public void updateColumnMetaData(List<ResultColumnMetadata> metadata) {
final List<ColumnMetaData> newColumns = new ArrayList<>(metadata.size());
int offset = 0;
for (ResultColumnMetadata m : metadata) {
final AvaticaType bundledSqlDataType = getAvaticaType(m.getDataType());
newColumns.add(new ColumnMetaData(offset, m.getAutoIncrement(), m.getCaseSensitivity(), m.getSearchability() != ColumnSearchability.NONE, m.getIsCurrency(), m.getIsNullable() ? ResultSetMetaData.columnNullable : ResultSetMetaData.columnNoNulls, m.getSigned(), m.getDisplaySize(), m.getLabel(), m.getColumnName(), m.getSchemaName(), m.getPrecision(), m.getScale(), m.getTableName(), m.getCatalogName(), bundledSqlDataType, m.getUpdatability() == ColumnUpdatability.READ_ONLY, m.getUpdatability() == ColumnUpdatability.WRITABLE, m.getUpdatability() == ColumnUpdatability.WRITABLE, m.getClassName()));
offset++;
}
columns = ImmutableList.copyOf(newColumns);
}
use of org.apache.calcite.avatica.ColumnMetaData in project calcite-avatica by apache.
the class ProtobufTranslationImplTest method getRequests.
/**
* Generates a collection of Requests whose serialization will be tested.
*/
private static List<Request> getRequests() {
LinkedList<Request> requests = new LinkedList<>();
requests.add(new CatalogsRequest());
requests.add(new DatabasePropertyRequest());
requests.add(new SchemasRequest("connectionId", "catalog", "schemaPattern"));
requests.add(new TablesRequest("connectionId", "catalog", "schemaPattern", "tableNamePattern", Arrays.asList("STRING", "BOOLEAN", "INT")));
requests.add(new TableTypesRequest());
requests.add(new ColumnsRequest("connectionId", "catalog", "schemaPattern", "tableNamePattern", "columnNamePattern"));
requests.add(new TypeInfoRequest());
requests.add(new PrepareAndExecuteRequest("connectionId", Integer.MAX_VALUE, "sql", Long.MAX_VALUE));
requests.add(new PrepareRequest("connectionId", "sql", Long.MAX_VALUE));
List<TypedValue> paramValues = Arrays.asList(TypedValue.create(Rep.BOOLEAN.name(), Boolean.TRUE), TypedValue.create(Rep.STRING.name(), "string"));
FetchRequest fetchRequest = new FetchRequest("connectionId", Integer.MAX_VALUE, Long.MAX_VALUE, Integer.MAX_VALUE);
requests.add(fetchRequest);
requests.add(new CreateStatementRequest("connectionId"));
requests.add(new CloseStatementRequest("connectionId", Integer.MAX_VALUE));
Map<String, String> info = new HashMap<>();
info.put("param1", "value1");
info.put("param2", "value2");
requests.add(new OpenConnectionRequest("connectionId", info));
requests.add(new CloseConnectionRequest("connectionId"));
requests.add(new ConnectionSyncRequest("connectionId", new ConnectionPropertiesImpl(Boolean.FALSE, Boolean.FALSE, Integer.MAX_VALUE, "catalog", "schema")));
requests.add(new SyncResultsRequest("connectionId", 12345, getSqlQueryState(), 150));
requests.add(new SyncResultsRequest("connectionId2", 54321, getMetadataQueryState1(), 0));
requests.add(new SyncResultsRequest("connectionId3", 5, getMetadataQueryState2(), 10));
requests.add(new CommitRequest("connectionId"));
requests.add(new RollbackRequest("connectionId"));
// ExecuteBatchRequest omitted because of the special protobuf conversion it does
List<String> commands = Arrays.asList("command1", "command2", "command3");
requests.add(new PrepareAndExecuteBatchRequest("connectionId", 12345, commands));
List<ColumnMetaData> columns = Collections.emptyList();
List<AvaticaParameter> params = Collections.emptyList();
Meta.CursorFactory cursorFactory = Meta.CursorFactory.create(Style.LIST, Object.class, Collections.<String>emptyList());
Signature signature = Signature.create(columns, "sql", params, cursorFactory, Meta.StatementType.SELECT);
Meta.StatementHandle handle = new Meta.StatementHandle("1234", 1, signature);
requests.add(new ExecuteRequest(handle, Arrays.<TypedValue>asList((TypedValue) null), 10));
requests.add(new ExecuteRequest(handle, Arrays.asList(TypedValue.EXPLICIT_NULL), 10));
return requests;
}
use of org.apache.calcite.avatica.ColumnMetaData 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());
}
}
use of org.apache.calcite.avatica.ColumnMetaData 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]);
}
}
Aggregations