Search in sources :

Example 6 with Frame

use of org.apache.calcite.avatica.Meta.Frame 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 7 with Frame

use of org.apache.calcite.avatica.Meta.Frame in project calcite-avatica by apache.

the class FrameTest method testDeprecatedValueAttributeForArrays.

@Test
public void testDeprecatedValueAttributeForArrays() {
    // Create a row with schema: [VARCHAR, ARRAY]
    List<Object> rows = Collections.<Object>singletonList(new Object[] { "string", Arrays.asList(1, 2, 3) });
    Meta.Frame frame = Meta.Frame.create(0, true, rows);
    // Convert it to a protobuf
    Common.Frame protoFrame = frame.toProto();
    assertEquals(1, protoFrame.getRowsCount());
    // Get that row we created
    Common.Row protoRow = protoFrame.getRows(0);
    // One row has many columns
    List<Common.ColumnValue> protoColumns = protoRow.getValueList();
    // We should have two columns
    assertEquals(2, protoColumns.size());
    // Fetch the ARRAY column
    Common.ColumnValue protoColumn = protoColumns.get(1);
    // We should have the 3 ARRAY elements in the array_values attribute as well as the deprecated
    // values attribute.
    List<Common.TypedValue> deprecatedValues = protoColumn.getValueList();
    assertEquals(3, deprecatedValues.size());
    assertTrue("Column 2 should have an array_value", protoColumns.get(1).getHasArrayValue());
    List<Common.TypedValue> arrayValues = protoColumns.get(1).getArrayValueList();
    assertEquals(arrayValues, deprecatedValues);
}
Also used : ColumnValue(org.apache.calcite.avatica.proto.Common.ColumnValue) ColumnValue(org.apache.calcite.avatica.proto.Common.ColumnValue) Frame(org.apache.calcite.avatica.Meta.Frame) Common(org.apache.calcite.avatica.proto.Common) TypedValue(org.apache.calcite.avatica.proto.Common.TypedValue) Test(org.junit.Test)

Example 8 with Frame

use of org.apache.calcite.avatica.Meta.Frame in project calcite-avatica by apache.

the class FrameTest method testMultipleRows.

@Test
public void testMultipleRows() {
    ArrayList<Object> rows = new ArrayList<>();
    rows.add(new Object[] { "string", Integer.MAX_VALUE, new Date().getTime() });
    rows.add(new Object[] { "gnirts", 0, Long.MIN_VALUE });
    rows.add(new Object[] { "", null, Long.MAX_VALUE });
    Frame singleRow = new Frame(0, true, rows);
    serializeAndTestEquality(singleRow);
}
Also used : Frame(org.apache.calcite.avatica.Meta.Frame) ArrayList(java.util.ArrayList) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Frame (org.apache.calcite.avatica.Meta.Frame)8 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 Common (org.apache.calcite.avatica.proto.Common)4 ColumnValue (org.apache.calcite.avatica.proto.Common.ColumnValue)4 Date (java.util.Date)3 List (java.util.List)3 TypedValue (org.apache.calcite.avatica.proto.Common.TypedValue)3 Meta (org.apache.calcite.avatica.Meta)2 FetchResponse (org.apache.calcite.avatica.remote.Service.FetchResponse)2 RpcMetadataResponse (org.apache.calcite.avatica.remote.Service.RpcMetadataResponse)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)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 ScalarType (org.apache.calcite.avatica.ColumnMetaData.ScalarType)1 ConnectionPropertiesImpl (org.apache.calcite.avatica.ConnectionPropertiesImpl)1