Search in sources :

Example 11 with ColumnMetaData

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);
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) NotNull(javax.validation.constraints.NotNull)

Example 12 with ColumnMetaData

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);
}
Also used : ResultColumnMetadata(org.apache.drill.exec.proto.UserProtos.ResultColumnMetadata) AvaticaType(org.apache.calcite.avatica.ColumnMetaData.AvaticaType) ArrayList(java.util.ArrayList) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Example 13 with ColumnMetaData

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;
}
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)

Example 14 with ColumnMetaData

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

Example 15 with ColumnMetaData

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]);
    }
}
Also used : StructType(org.apache.calcite.avatica.ColumnMetaData.StructType) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) Struct(java.sql.Struct) ArrayType(org.apache.calcite.avatica.ColumnMetaData.ArrayType) Array(java.sql.Array) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) Test(org.junit.Test)

Aggregations

ColumnMetaData (org.apache.calcite.avatica.ColumnMetaData)16 ArrayList (java.util.ArrayList)11 List (java.util.List)7 Accessor (org.apache.calcite.avatica.util.Cursor.Accessor)6 ArrayType (org.apache.calcite.avatica.ColumnMetaData.ArrayType)5 Array (java.sql.Array)3 AvaticaType (org.apache.calcite.avatica.ColumnMetaData.AvaticaType)3 ScalarType (org.apache.calcite.avatica.ColumnMetaData.ScalarType)3 Meta (org.apache.calcite.avatica.Meta)3 Struct (java.sql.Struct)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 AvaticaParameter (org.apache.calcite.avatica.AvaticaParameter)2 StructType (org.apache.calcite.avatica.ColumnMetaData.StructType)2 ConnectionPropertiesImpl (org.apache.calcite.avatica.ConnectionPropertiesImpl)2 Signature (org.apache.calcite.avatica.Meta.Signature)2 SqlType (org.apache.calcite.avatica.SqlType)2 ArrayAccessor (org.apache.calcite.avatica.util.AbstractCursor.ArrayAccessor)2 Test (org.junit.Test)2 IOException (java.io.IOException)1