Search in sources :

Example 1 with FetchRequest

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());
}
Also used : Meta(org.apache.calcite.avatica.Meta) ArrayList(java.util.ArrayList) RpcMetadataResponse(org.apache.calcite.avatica.remote.Service.RpcMetadataResponse) Requests(org.apache.calcite.avatica.proto.Requests) FetchRequest(org.apache.calcite.avatica.remote.Service.FetchRequest) ColumnValue(org.apache.calcite.avatica.proto.Common.ColumnValue) FetchResponse(org.apache.calcite.avatica.remote.Service.FetchResponse) Frame(org.apache.calcite.avatica.Meta.Frame) Responses(org.apache.calcite.avatica.proto.Responses) Common(org.apache.calcite.avatica.proto.Common) ColumnValue(org.apache.calcite.avatica.proto.Common.ColumnValue) Test(org.junit.Test)

Example 2 with FetchRequest

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;
}
Also used : PrepareAndExecuteBatchRequest(org.apache.calcite.avatica.remote.Service.PrepareAndExecuteBatchRequest) Meta(org.apache.calcite.avatica.Meta) HashMap(java.util.HashMap) DatabasePropertyRequest(org.apache.calcite.avatica.remote.Service.DatabasePropertyRequest) SchemasRequest(org.apache.calcite.avatica.remote.Service.SchemasRequest) ConnectionPropertiesImpl(org.apache.calcite.avatica.ConnectionPropertiesImpl) OpenConnectionRequest(org.apache.calcite.avatica.remote.Service.OpenConnectionRequest) RollbackRequest(org.apache.calcite.avatica.remote.Service.RollbackRequest) SyncResultsRequest(org.apache.calcite.avatica.remote.Service.SyncResultsRequest) AvaticaParameter(org.apache.calcite.avatica.AvaticaParameter) TablesRequest(org.apache.calcite.avatica.remote.Service.TablesRequest) TableTypesRequest(org.apache.calcite.avatica.remote.Service.TableTypesRequest) ExecuteRequest(org.apache.calcite.avatica.remote.Service.ExecuteRequest) PrepareAndExecuteRequest(org.apache.calcite.avatica.remote.Service.PrepareAndExecuteRequest) FetchRequest(org.apache.calcite.avatica.remote.Service.FetchRequest) PrepareAndExecuteRequest(org.apache.calcite.avatica.remote.Service.PrepareAndExecuteRequest) CloseStatementRequest(org.apache.calcite.avatica.remote.Service.CloseStatementRequest) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) PrepareRequest(org.apache.calcite.avatica.remote.Service.PrepareRequest) CommitRequest(org.apache.calcite.avatica.remote.Service.CommitRequest) ExecuteRequest(org.apache.calcite.avatica.remote.Service.ExecuteRequest) TablesRequest(org.apache.calcite.avatica.remote.Service.TablesRequest) CloseStatementRequest(org.apache.calcite.avatica.remote.Service.CloseStatementRequest) FetchRequest(org.apache.calcite.avatica.remote.Service.FetchRequest) PrepareRequest(org.apache.calcite.avatica.remote.Service.PrepareRequest) CreateStatementRequest(org.apache.calcite.avatica.remote.Service.CreateStatementRequest) CloseConnectionRequest(org.apache.calcite.avatica.remote.Service.CloseConnectionRequest) CatalogsRequest(org.apache.calcite.avatica.remote.Service.CatalogsRequest) SyncResultsRequest(org.apache.calcite.avatica.remote.Service.SyncResultsRequest) Request(org.apache.calcite.avatica.remote.Service.Request) ConnectionSyncRequest(org.apache.calcite.avatica.remote.Service.ConnectionSyncRequest) SchemasRequest(org.apache.calcite.avatica.remote.Service.SchemasRequest) TableTypesRequest(org.apache.calcite.avatica.remote.Service.TableTypesRequest) DatabasePropertyRequest(org.apache.calcite.avatica.remote.Service.DatabasePropertyRequest) PrepareAndExecuteBatchRequest(org.apache.calcite.avatica.remote.Service.PrepareAndExecuteBatchRequest) TypeInfoRequest(org.apache.calcite.avatica.remote.Service.TypeInfoRequest) CommitRequest(org.apache.calcite.avatica.remote.Service.CommitRequest) PrepareAndExecuteRequest(org.apache.calcite.avatica.remote.Service.PrepareAndExecuteRequest) OpenConnectionRequest(org.apache.calcite.avatica.remote.Service.OpenConnectionRequest) ColumnsRequest(org.apache.calcite.avatica.remote.Service.ColumnsRequest) RollbackRequest(org.apache.calcite.avatica.remote.Service.RollbackRequest) LinkedList(java.util.LinkedList) CloseConnectionRequest(org.apache.calcite.avatica.remote.Service.CloseConnectionRequest) TypeInfoRequest(org.apache.calcite.avatica.remote.Service.TypeInfoRequest) CreateStatementRequest(org.apache.calcite.avatica.remote.Service.CreateStatementRequest) Signature(org.apache.calcite.avatica.Meta.Signature) ConnectionSyncRequest(org.apache.calcite.avatica.remote.Service.ConnectionSyncRequest) CatalogsRequest(org.apache.calcite.avatica.remote.Service.CatalogsRequest) ColumnsRequest(org.apache.calcite.avatica.remote.Service.ColumnsRequest)

Aggregations

Meta (org.apache.calcite.avatica.Meta)2 FetchRequest (org.apache.calcite.avatica.remote.Service.FetchRequest)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 AvaticaParameter (org.apache.calcite.avatica.AvaticaParameter)1 ColumnMetaData (org.apache.calcite.avatica.ColumnMetaData)1 ConnectionPropertiesImpl (org.apache.calcite.avatica.ConnectionPropertiesImpl)1 Frame (org.apache.calcite.avatica.Meta.Frame)1 Signature (org.apache.calcite.avatica.Meta.Signature)1 Common (org.apache.calcite.avatica.proto.Common)1 ColumnValue (org.apache.calcite.avatica.proto.Common.ColumnValue)1 Requests (org.apache.calcite.avatica.proto.Requests)1 Responses (org.apache.calcite.avatica.proto.Responses)1 CatalogsRequest (org.apache.calcite.avatica.remote.Service.CatalogsRequest)1 CloseConnectionRequest (org.apache.calcite.avatica.remote.Service.CloseConnectionRequest)1 CloseStatementRequest (org.apache.calcite.avatica.remote.Service.CloseStatementRequest)1 ColumnsRequest (org.apache.calcite.avatica.remote.Service.ColumnsRequest)1 CommitRequest (org.apache.calcite.avatica.remote.Service.CommitRequest)1 ConnectionSyncRequest (org.apache.calcite.avatica.remote.Service.ConnectionSyncRequest)1