Search in sources :

Example 1 with ColumnMetaData

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

the class DruidStatement method createColumnMetaData.

public static List<ColumnMetaData> createColumnMetaData(final RelDataType rowType) {
    final List<ColumnMetaData> columns = new ArrayList<>();
    List<RelDataTypeField> fieldList = rowType.getFieldList();
    for (int i = 0; i < fieldList.size(); i++) {
        RelDataTypeField field = fieldList.get(i);
        final ColumnMetaData.Rep rep = QueryMaker.rep(field.getType().getSqlTypeName());
        final ColumnMetaData.ScalarType columnType = ColumnMetaData.scalar(field.getType().getSqlTypeName().getJdbcOrdinal(), field.getType().getSqlTypeName().getName(), rep);
        columns.add(new ColumnMetaData(// ordinal
        i, // auto increment
        false, // case sensitive
        true, // searchable
        false, // currency
        false, field.getType().isNullable() ? DatabaseMetaData.columnNullable : // nullable
        DatabaseMetaData.columnNoNulls, // signed
        true, // display size
        field.getType().getPrecision(), // label
        field.getName(), // column name
        null, // schema name
        null, // precision
        field.getType().getPrecision(), // scale
        field.getType().getScale(), // table name
        null, // catalog name
        null, // avatica type
        columnType, // read only
        true, // writable
        false, // definitely writable
        false, // column class name
        columnType.columnClassName()));
    }
    return columns;
}
Also used : RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) ArrayList(java.util.ArrayList) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Example 2 with ColumnMetaData

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

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

the class StructImplTest method structAccessor.

@Test
public void structAccessor() 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", "ignored"));
    ColumnMetaData structMetaData = MetaImpl.columnMetaData("MY_STRUCT", 1, structType, false);
    List<List<Object>> rows = Arrays.asList(Collections.<Object>singletonList(struct1), Collections.<Object>singletonList(struct2), Collections.<Object>singletonList(struct3), Collections.<Object>singletonList(struct4));
    // Create four rows, each with one (struct) column
    try (Cursor cursor = new ListIteratorCursor(rows.iterator())) {
        List<Accessor> accessors = cursor.createAccessors(Collections.singletonList(structMetaData), Unsafe.localCalendar(), null);
        assertEquals(1, accessors.size());
        Accessor accessor = accessors.get(0);
        assertTrue(cursor.next());
        Struct s = accessor.getObject(Struct.class);
        Object[] structData = s.getAttributes();
        assertEquals(2, structData.length);
        assertEquals(1, structData[0]);
        assertEquals("one", structData[1]);
        assertTrue(cursor.next());
        s = accessor.getObject(Struct.class);
        structData = s.getAttributes();
        assertEquals(2, structData.length);
        assertEquals(2, structData[0]);
        assertEquals("two", structData[1]);
        assertTrue(cursor.next());
        s = accessor.getObject(Struct.class);
        structData = s.getAttributes();
        assertEquals(1, structData.length);
        assertEquals(3, structData[0]);
        assertTrue(cursor.next());
        s = accessor.getObject(Struct.class);
        structData = s.getAttributes();
        assertEquals(3, structData.length);
        assertEquals(4, structData[0]);
        assertEquals("four", structData[1]);
        // We didn't provide metadata, but we still expect to see it.
        assertEquals("ignored", structData[2]);
    }
}
Also used : StructType(org.apache.calcite.avatica.ColumnMetaData.StructType) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor) Struct(java.sql.Struct) Test(org.junit.Test)

Example 4 with ColumnMetaData

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

the class AbstractCursor method createAccessor.

protected Accessor createAccessor(ColumnMetaData columnMetaData, Getter getter, Calendar localCalendar, ArrayImpl.Factory factory) {
    switch(columnMetaData.type.rep) {
        case NUMBER:
            switch(columnMetaData.type.id) {
                case Types.TINYINT:
                case Types.SMALLINT:
                case Types.INTEGER:
                case Types.BIGINT:
                case Types.REAL:
                case Types.FLOAT:
                case Types.DOUBLE:
                case Types.NUMERIC:
                case Types.DECIMAL:
                    return new NumberAccessor(getter, columnMetaData.scale);
            }
    }
    switch(columnMetaData.type.id) {
        case Types.TINYINT:
            return new ByteAccessor(getter);
        case Types.SMALLINT:
            return new ShortAccessor(getter);
        case Types.INTEGER:
            return new IntAccessor(getter);
        case Types.BIGINT:
            return new LongAccessor(getter);
        case Types.BOOLEAN:
            return new BooleanAccessor(getter);
        case Types.REAL:
            return new FloatAccessor(getter);
        case Types.FLOAT:
        case Types.DOUBLE:
            return new DoubleAccessor(getter);
        case Types.DECIMAL:
            return new BigDecimalAccessor(getter);
        case Types.CHAR:
            switch(columnMetaData.type.rep) {
                case PRIMITIVE_CHAR:
                case CHARACTER:
                    return new StringFromCharAccessor(getter, columnMetaData.displaySize);
                default:
                    return new FixedStringAccessor(getter, columnMetaData.displaySize);
            }
        case Types.VARCHAR:
            return new StringAccessor(getter);
        case Types.BINARY:
        case Types.VARBINARY:
            switch(columnMetaData.type.rep) {
                case STRING:
                    return new BinaryFromStringAccessor(getter);
                default:
                    return new BinaryAccessor(getter);
            }
        case Types.DATE:
            switch(columnMetaData.type.rep) {
                case PRIMITIVE_INT:
                case INTEGER:
                case NUMBER:
                    return new DateFromNumberAccessor(getter, localCalendar);
                case JAVA_SQL_DATE:
                    return new DateAccessor(getter);
                default:
                    throw new AssertionError("bad " + columnMetaData.type.rep);
            }
        case Types.TIME:
            switch(columnMetaData.type.rep) {
                case PRIMITIVE_INT:
                case INTEGER:
                case NUMBER:
                    return new TimeFromNumberAccessor(getter, localCalendar);
                case JAVA_SQL_TIME:
                    return new TimeAccessor(getter);
                default:
                    throw new AssertionError("bad " + columnMetaData.type.rep);
            }
        case Types.TIMESTAMP:
            switch(columnMetaData.type.rep) {
                case PRIMITIVE_LONG:
                case LONG:
                case NUMBER:
                    return new TimestampFromNumberAccessor(getter, localCalendar);
                case JAVA_SQL_TIMESTAMP:
                    return new TimestampAccessor(getter);
                case JAVA_UTIL_DATE:
                    return new TimestampFromUtilDateAccessor(getter, localCalendar);
                default:
                    throw new AssertionError("bad " + columnMetaData.type.rep);
            }
        case Types.ARRAY:
            final ColumnMetaData.ArrayType arrayType = (ColumnMetaData.ArrayType) columnMetaData.type;
            final SlotGetter componentGetter = new SlotGetter();
            final Accessor componentAccessor = createAccessor(ColumnMetaData.dummy(arrayType.getComponent(), true), componentGetter, localCalendar, factory);
            return new ArrayAccessor(getter, arrayType.getComponent(), componentAccessor, componentGetter, factory);
        case Types.STRUCT:
            switch(columnMetaData.type.rep) {
                case OBJECT:
                    final ColumnMetaData.StructType structType = (ColumnMetaData.StructType) columnMetaData.type;
                    List<Accessor> accessors = new ArrayList<>();
                    for (ColumnMetaData column : structType.columns) {
                        final Getter fieldGetter = structType.columns.size() == 1 ? getter : new StructGetter(getter, column);
                        accessors.add(createAccessor(column, fieldGetter, localCalendar, factory));
                    }
                    return new StructAccessor(getter, accessors);
                default:
                    throw new AssertionError("bad " + columnMetaData.type.rep);
            }
        case Types.JAVA_OBJECT:
        case // e.g. map
        Types.OTHER:
            if (columnMetaData.type.getName().startsWith("INTERVAL_")) {
                int end = columnMetaData.type.getName().indexOf("(");
                if (end < 0) {
                    end = columnMetaData.type.getName().length();
                }
                TimeUnitRange range = TimeUnitRange.valueOf(columnMetaData.type.getName().substring("INTERVAL_".length(), end));
                if (range.monthly()) {
                    return new IntervalYearMonthAccessor(getter, range);
                } else {
                    return new IntervalDayTimeAccessor(getter, range, columnMetaData.scale);
                }
            }
            return new ObjectAccessor(getter);
        default:
            throw new RuntimeException("unknown type " + columnMetaData.type.id);
    }
}
Also used : ArrayList(java.util.ArrayList) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Example 5 with ColumnMetaData

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

the class ArrayFactoryImpl method createArray.

@Override
public Array createArray(AvaticaType elementType, Iterable<Object> elements) {
    final ArrayType array = ColumnMetaData.array(elementType, elementType.name, Rep.ARRAY);
    final List<ColumnMetaData> types = Collections.singletonList(ColumnMetaData.dummy(array, true));
    // Avoid creating a new List if we already have a List
    List<Object> elementList;
    if (elements instanceof List) {
        elementList = (List<Object>) elements;
    } else {
        elementList = new ArrayList<>();
        for (Object element : elements) {
            elementList.add(element);
        }
    }
    try (ListIteratorCursor cursor = new ListIteratorCursor(createRowForArrayData(elementList))) {
        List<Accessor> accessor = cursor.createAccessors(types, Unsafe.localCalendar(), this);
        assert 1 == accessor.size();
        return new ArrayImpl(elementList, (ArrayAccessor) accessor.get(0));
    }
}
Also used : ArrayType(org.apache.calcite.avatica.ColumnMetaData.ArrayType) ArrayList(java.util.ArrayList) List(java.util.List) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData) ArrayAccessor(org.apache.calcite.avatica.util.AbstractCursor.ArrayAccessor) Accessor(org.apache.calcite.avatica.util.Cursor.Accessor)

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