use of org.apache.calcite.avatica.remote.Service.FetchRequest in project calcite-avatica by apache.
the class ProtobufHandlerTest method testFetch.
@Test
public void testFetch() throws Exception {
final String connectionId = "cnxn1";
final int statementId = 30;
final long offset = 10;
final int fetchMaxRowCount = 100;
final List<Common.TypedValue> values = new ArrayList<>();
values.add(Common.TypedValue.newBuilder().setType(Common.Rep.BOOLEAN).setBoolValue(true).build());
values.add(Common.TypedValue.newBuilder().setType(Common.Rep.STRING).setStringValue("my_string").build());
Requests.FetchRequest protoRequest = Requests.FetchRequest.newBuilder().setConnectionId(connectionId).setStatementId(statementId).setOffset(offset).setFetchMaxRowCount(fetchMaxRowCount).build();
byte[] serializedRequest = protoRequest.toByteArray();
FetchRequest request = new FetchRequest().deserialize(protoRequest);
List<Object> frameRows = new ArrayList<>();
frameRows.add(new Object[] { true, "my_string" });
Meta.Frame frame = Frame.create(0, true, frameRows);
RpcMetadataResponse metadata = new RpcMetadataResponse("localhost:8765");
FetchResponse response = new FetchResponse(frame, false, false, metadata);
when(translation.parseRequest(serializedRequest)).thenReturn(request);
when(service.apply(request)).thenReturn(response);
when(translation.serializeResponse(response)).thenReturn(response.serialize().toByteArray());
HandlerResponse<byte[]> handlerResponse = handler.apply(serializedRequest);
byte[] serializedResponse = handlerResponse.getResponse();
assertEquals(200, handlerResponse.getStatusCode());
Responses.FetchResponse protoResponse = Responses.FetchResponse.parseFrom(serializedResponse);
Common.Frame protoFrame = protoResponse.getFrame();
assertEquals(frame.offset, protoFrame.getOffset());
assertEquals(frame.done, protoFrame.getDone());
List<Common.Row> rows = protoFrame.getRowsList();
assertEquals(1, rows.size());
Common.Row row = rows.get(0);
List<Common.ColumnValue> columnValues = row.getValueList();
assertEquals(2, columnValues.size());
Iterator<Common.ColumnValue> iter = columnValues.iterator();
assertTrue(iter.hasNext());
Common.ColumnValue column = iter.next();
assertTrue("The Column should have contained a scalar: " + column, column.hasField(ColumnValue.getDescriptor().findFieldByNumber(ColumnValue.SCALAR_VALUE_FIELD_NUMBER)));
Common.TypedValue value = column.getScalarValue();
assertEquals(Common.Rep.BOOLEAN, value.getType());
assertEquals(true, value.getBoolValue());
assertTrue(iter.hasNext());
column = iter.next();
assertTrue("The Column should have contained a scalar: " + column, column.hasField(ColumnValue.getDescriptor().findFieldByNumber(ColumnValue.SCALAR_VALUE_FIELD_NUMBER)));
value = column.getScalarValue();
assertEquals(Common.Rep.STRING, value.getType());
assertEquals("my_string", value.getStringValue());
}
use of org.apache.calcite.avatica.remote.Service.FetchRequest 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;
}
Aggregations