Search in sources :

Example 1 with ScalarType

use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.

the class ProtobufTranslationImplTest method getResponses.

/**
 * Generates a collection of Responses whose serialization will be tested.
 */
private static List<Response> getResponses() {
    final RpcMetadataResponse rpcMetadata = new RpcMetadataResponse("localhost:8765");
    LinkedList<Response> responses = new LinkedList<>();
    // Nested classes (Signature, ColumnMetaData, CursorFactory, etc) are implicitly getting tested)
    // Stub out the metadata for a row
    ScalarType arrayComponentType = ColumnMetaData.scalar(Types.INTEGER, "integer", Rep.INTEGER);
    ColumnMetaData arrayColumnMetaData = getArrayColumnMetaData(arrayComponentType, 2, "counts");
    List<ColumnMetaData> columns = Arrays.asList(MetaImpl.columnMetaData("str", 0, String.class, true), MetaImpl.columnMetaData("count", 1, Integer.class, true), arrayColumnMetaData);
    List<AvaticaParameter> params = Arrays.asList(new AvaticaParameter(false, 10, 0, Types.VARCHAR, "VARCHAR", String.class.getName(), "str"));
    Meta.CursorFactory cursorFactory = Meta.CursorFactory.create(Style.LIST, Object.class, Arrays.asList("str", "count", "counts"));
    // The row values
    List<Object> rows = new ArrayList<>();
    rows.add(new Object[] { "str_value1", 50, Arrays.asList(1, 2, 3) });
    rows.add(new Object[] { "str_value2", 100, Arrays.asList(1) });
    // Create the signature and frame using the metadata and values
    Signature signature = Signature.create(columns, "sql", params, cursorFactory, Meta.StatementType.SELECT);
    Frame frame = Frame.create(Integer.MAX_VALUE, true, rows);
    // And then create a ResultSetResponse
    ResultSetResponse results1 = new ResultSetResponse("connectionId", Integer.MAX_VALUE, true, signature, frame, Long.MAX_VALUE, rpcMetadata);
    responses.add(results1);
    responses.add(new CloseStatementResponse(rpcMetadata));
    ConnectionPropertiesImpl connProps = new ConnectionPropertiesImpl(false, true, Integer.MAX_VALUE, "catalog", "schema");
    responses.add(new ConnectionSyncResponse(connProps, rpcMetadata));
    responses.add(new OpenConnectionResponse(rpcMetadata));
    responses.add(new CloseConnectionResponse(rpcMetadata));
    responses.add(new CreateStatementResponse("connectionId", Integer.MAX_VALUE, rpcMetadata));
    Map<Meta.DatabaseProperty, Object> propertyMap = new HashMap<>();
    for (Meta.DatabaseProperty prop : Meta.DatabaseProperty.values()) {
        propertyMap.put(prop, prop.defaultValue);
    }
    responses.add(new DatabasePropertyResponse(propertyMap, rpcMetadata));
    responses.add(new ExecuteResponse(Arrays.asList(results1, results1, results1), false, rpcMetadata));
    responses.add(new FetchResponse(frame, false, false, rpcMetadata));
    responses.add(new FetchResponse(frame, true, true, rpcMetadata));
    responses.add(new FetchResponse(frame, false, true, rpcMetadata));
    responses.add(new PrepareResponse(new Meta.StatementHandle("connectionId", Integer.MAX_VALUE, signature), rpcMetadata));
    StringWriter sw = new StringWriter();
    new Exception().printStackTrace(new PrintWriter(sw));
    responses.add(new ErrorResponse(Collections.singletonList(sw.toString()), "Test Error Message", ErrorResponse.UNKNOWN_ERROR_CODE, ErrorResponse.UNKNOWN_SQL_STATE, AvaticaSeverity.WARNING, rpcMetadata));
    // No more results, statement not missing
    responses.add(new SyncResultsResponse(false, false, rpcMetadata));
    // Missing statement, no results
    responses.add(new SyncResultsResponse(false, true, rpcMetadata));
    // More results, no missing statement
    responses.add(new SyncResultsResponse(true, false, rpcMetadata));
    // Some tests to make sure ErrorResponse doesn't fail.
    responses.add(new ErrorResponse((List<String>) null, null, 0, null, null, null));
    responses.add(new ErrorResponse(Arrays.asList("stacktrace1", "stacktrace2"), null, 0, null, null, null));
    responses.add(new CommitResponse());
    responses.add(new RollbackResponse());
    long[] updateCounts = new long[] { 1, 0, 1, 1 };
    responses.add(new ExecuteBatchResponse("connectionId", 12345, updateCounts, false, rpcMetadata));
    return responses;
}
Also used : Meta(org.apache.calcite.avatica.Meta) Frame(org.apache.calcite.avatica.Meta.Frame) HashMap(java.util.HashMap) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) ArrayList(java.util.ArrayList) DatabasePropertyResponse(org.apache.calcite.avatica.remote.Service.DatabasePropertyResponse) RpcMetadataResponse(org.apache.calcite.avatica.remote.Service.RpcMetadataResponse) ConnectionPropertiesImpl(org.apache.calcite.avatica.ConnectionPropertiesImpl) RollbackResponse(org.apache.calcite.avatica.remote.Service.RollbackResponse) AvaticaParameter(org.apache.calcite.avatica.AvaticaParameter) CloseConnectionResponse(org.apache.calcite.avatica.remote.Service.CloseConnectionResponse) CloseStatementResponse(org.apache.calcite.avatica.remote.Service.CloseStatementResponse) StringWriter(java.io.StringWriter) ExecuteBatchResponse(org.apache.calcite.avatica.remote.Service.ExecuteBatchResponse) ResultSetResponse(org.apache.calcite.avatica.remote.Service.ResultSetResponse) OpenConnectionResponse(org.apache.calcite.avatica.remote.Service.OpenConnectionResponse) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) PrintWriter(java.io.PrintWriter) CreateStatementResponse(org.apache.calcite.avatica.remote.Service.CreateStatementResponse) SyncResultsResponse(org.apache.calcite.avatica.remote.Service.SyncResultsResponse) PrepareResponse(org.apache.calcite.avatica.remote.Service.PrepareResponse) ExecuteResponse(org.apache.calcite.avatica.remote.Service.ExecuteResponse) CommitResponse(org.apache.calcite.avatica.remote.Service.CommitResponse) FetchResponse(org.apache.calcite.avatica.remote.Service.FetchResponse) LinkedList(java.util.LinkedList) IOException(java.io.IOException) ErrorResponse(org.apache.calcite.avatica.remote.Service.ErrorResponse) CloseConnectionResponse(org.apache.calcite.avatica.remote.Service.CloseConnectionResponse) PrepareResponse(org.apache.calcite.avatica.remote.Service.PrepareResponse) FetchResponse(org.apache.calcite.avatica.remote.Service.FetchResponse) RpcMetadataResponse(org.apache.calcite.avatica.remote.Service.RpcMetadataResponse) ExecuteBatchResponse(org.apache.calcite.avatica.remote.Service.ExecuteBatchResponse) DatabasePropertyResponse(org.apache.calcite.avatica.remote.Service.DatabasePropertyResponse) OpenConnectionResponse(org.apache.calcite.avatica.remote.Service.OpenConnectionResponse) RollbackResponse(org.apache.calcite.avatica.remote.Service.RollbackResponse) CloseStatementResponse(org.apache.calcite.avatica.remote.Service.CloseStatementResponse) Response(org.apache.calcite.avatica.remote.Service.Response) CreateStatementResponse(org.apache.calcite.avatica.remote.Service.CreateStatementResponse) ErrorResponse(org.apache.calcite.avatica.remote.Service.ErrorResponse) ConnectionSyncResponse(org.apache.calcite.avatica.remote.Service.ConnectionSyncResponse) ResultSetResponse(org.apache.calcite.avatica.remote.Service.ResultSetResponse) ExecuteResponse(org.apache.calcite.avatica.remote.Service.ExecuteResponse) SyncResultsResponse(org.apache.calcite.avatica.remote.Service.SyncResultsResponse) CommitResponse(org.apache.calcite.avatica.remote.Service.CommitResponse) Signature(org.apache.calcite.avatica.Meta.Signature) ConnectionSyncResponse(org.apache.calcite.avatica.remote.Service.ConnectionSyncResponse)

Example 2 with ScalarType

use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.

the class ArrayImplTest method testArrayWithOffsets.

@Test
public void testArrayWithOffsets() throws Exception {
    // Define the struct type we're creating
    ScalarType intType = ColumnMetaData.scalar(Types.INTEGER, "INTEGER", Rep.INTEGER);
    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 array3 = factory.createArray(intType, Arrays.<Object>asList(4, 5, 6));
    Object[] data = (Object[]) array1.getArray(2, 1);
    assertEquals(1, data.length);
    assertEquals(2, data[0]);
    data = (Object[]) array3.getArray(1, 1);
    assertEquals(1, data.length);
    assertEquals(4, data[0]);
    data = (Object[]) array3.getArray(2, 2);
    assertEquals(2, data.length);
    assertEquals(5, data[0]);
    assertEquals(6, data[1]);
    data = (Object[]) array3.getArray(1, 3);
    assertEquals(3, data.length);
    assertEquals(4, data[0]);
    assertEquals(5, data[1]);
    assertEquals(6, data[2]);
}
Also used : Array(java.sql.Array) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) Test(org.junit.Test)

Example 3 with ScalarType

use of org.apache.calcite.avatica.ColumnMetaData.ScalarType 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++;
        }
    }
}
Also used : ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) List(java.util.List) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) Test(org.junit.Test)

Example 4 with ScalarType

use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.

the class ArrayTypeTest method dateArrays.

@Test
public void dateArrays() throws Exception {
    try (Connection conn = DriverManager.getConnection(url)) {
        final long now = System.currentTimeMillis();
        ScalarType component = ColumnMetaData.scalar(Types.DATE, "DATE", Rep.JAVA_SQL_DATE);
        List<Array> arrays = new ArrayList<>();
        // Construct the data
        for (int i = 0; i < 5; i++) {
            List<Date> elements = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                elements.add(new Date(now + i + j));
            }
            arrays.add(createArray("DATE", component, elements));
        }
        writeAndReadArrays(conn, "date_arrays", "DATE", component, arrays, (expected, actual) -> {
            Object[] expectedDates = (Object[]) expected.getArray();
            Object[] actualDates = (Object[]) actual.getArray();
            assertEquals(expectedDates.length, actualDates.length);
            final Calendar cal = Unsafe.localCalendar();
            for (int i = 0; i < expectedDates.length; i++) {
                cal.setTime((Date) expectedDates[i]);
                int expectedDayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
                int expectedMonth = cal.get(Calendar.MONTH);
                int expectedYear = cal.get(Calendar.YEAR);
                cal.setTime((Date) actualDates[i]);
                assertEquals(expectedDayOfMonth, cal.get(Calendar.DAY_OF_MONTH));
                assertEquals(expectedMonth, cal.get(Calendar.MONTH));
                assertEquals(expectedYear, cal.get(Calendar.YEAR));
            }
        });
        // Ensure an array with a null element can be written/read
        Array arrayWithNull = createArray("DATE", component, Arrays.asList((Time) null));
        writeAndReadArrays(conn, "date_array_with_null", "DATE", component, Collections.singletonList(arrayWithNull), (expected, actual) -> {
            Object[] expectedArray = (Object[]) expected.getArray();
            Object[] actualArray = (Object[]) actual.getArray();
            assertEquals(1, expectedArray.length);
            assertEquals(expectedArray.length, actualArray.length);
            assertEquals(expectedArray[0], actualArray[0]);
        });
    }
}
Also used : Calendar(java.util.Calendar) Connection(java.sql.Connection) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) ArrayList(java.util.ArrayList) Time(java.sql.Time) Date(java.sql.Date) Array(java.sql.Array) Test(org.junit.Test)

Example 5 with ScalarType

use of org.apache.calcite.avatica.ColumnMetaData.ScalarType in project calcite-avatica by apache.

the class ArrayTypeTest method stringArrays.

@Test
public void stringArrays() throws Exception {
    try (Connection conn = DriverManager.getConnection(url)) {
        ScalarType component = ColumnMetaData.scalar(Types.VARCHAR, "VARCHAR", Rep.STRING);
        List<Array> arrays = new ArrayList<>();
        // Construct the data
        for (int i = 0; i < 5; i++) {
            List<String> elements = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                elements.add(i + "_" + j);
            }
            arrays.add(createArray("VARCHAR", component, elements));
        }
        // Verify read/write
        writeAndReadArrays(conn, "string_arrays", "VARCHAR", component, arrays, PRIMITIVE_LIST_VALIDATOR);
    }
}
Also used : Array(java.sql.Array) Connection(java.sql.Connection) ScalarType(org.apache.calcite.avatica.ColumnMetaData.ScalarType) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

ScalarType (org.apache.calcite.avatica.ColumnMetaData.ScalarType)25 Test (org.junit.Test)23 Array (java.sql.Array)19 ArrayList (java.util.ArrayList)18 Connection (java.sql.Connection)16 List (java.util.List)9 Random (java.util.Random)9 ColumnMetaData (org.apache.calcite.avatica.ColumnMetaData)8 Accessor (org.apache.calcite.avatica.util.Cursor.Accessor)5 Calendar (java.util.Calendar)4 ResultSet (java.sql.ResultSet)3 Time (java.sql.Time)3 Date (java.sql.Date)2 PreparedStatement (java.sql.PreparedStatement)2 Statement (java.sql.Statement)2 Timestamp (java.sql.Timestamp)2 ArrayType (org.apache.calcite.avatica.ColumnMetaData.ArrayType)2 Meta (org.apache.calcite.avatica.Meta)2 ArrayImpl (org.apache.calcite.avatica.util.ArrayImpl)2 IOException (java.io.IOException)1