Search in sources :

Example 1 with AvaticaType

use of org.apache.calcite.avatica.ColumnMetaData.AvaticaType in project drill by axbaretto.

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 2 with AvaticaType

use of org.apache.calcite.avatica.ColumnMetaData.AvaticaType in project drill by apache.

the class DrillColumnMetaDataList method updateColumnMetaData.

public void updateColumnMetaData(String catalogName, String schemaName, String tableName, BatchSchema schema, List<Class<?>> getObjectClasses) {
    final List<ColumnMetaData> newColumns = new ArrayList<>(schema.getFieldCount());
    for (int colOffset = 0; colOffset < schema.getFieldCount(); colOffset++) {
        final MaterializedField field = schema.getColumn(colOffset);
        Class<?> objectClass = getObjectClasses.get(colOffset);
        final String columnName = field.getName();
        final MajorType rpcDataType = field.getType();
        final AvaticaType bundledSqlDataType = getAvaticaType(rpcDataType);
        final String columnClassName = objectClass.getName();
        final int nullability;
        switch(field.getDataMode()) {
            case OPTIONAL:
                nullability = ResultSetMetaData.columnNullable;
                break;
            case REQUIRED:
                nullability = ResultSetMetaData.columnNoNulls;
                break;
            // Should REPEATED still map to columnNoNulls? or to columnNullable?
            case REPEATED:
                nullability = ResultSetMetaData.columnNoNulls;
                break;
            default:
                throw new AssertionError("Unexpected new DataMode value '" + field.getDataMode().name() + "'");
        }
        final boolean isSigned = Types.isJdbcSignedType(rpcDataType);
        // TODO(DRILL-3355):  TODO(DRILL-3356):  When string lengths, precisions,
        // interval kinds, etc., are available from RPC-level data, implement:
        // - precision for ResultSetMetadata.getPrecision(...) (like
        // getColumns()'s COLUMN_SIZE)
        // - scale for getScale(...), and
        // - and displaySize for getColumnDisplaySize(...).
        final int precision = Types.getPrecision(rpcDataType);
        final int scale = Types.getScale(rpcDataType);
        final int displaySize = Types.getJdbcDisplaySize(rpcDataType);
        ColumnMetaData col = new ColumnMetaData(// (zero-based ordinal (for Java arrays/lists).)
        colOffset, false, /* autoIncrement */
        false, /* caseSensitive */
        true, /* searchable */
        false, /* currency */
        nullability, isSigned, displaySize, columnName, /* label */
        columnName, /* columnName */
        schemaName, precision, scale, tableName, catalogName, bundledSqlDataType, true, /* readOnly */
        false, /* writable */
        false, /* definitelyWritable */
        columnClassName);
        newColumns.add(col);
    }
    columns = newColumns;
}
Also used : MajorType(org.apache.drill.common.types.TypeProtos.MajorType) AvaticaType(org.apache.calcite.avatica.ColumnMetaData.AvaticaType) ArrayList(java.util.ArrayList) MaterializedField(org.apache.drill.exec.record.MaterializedField) ColumnMetaData(org.apache.calcite.avatica.ColumnMetaData)

Example 3 with AvaticaType

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

the class AvaticaConnection method createArrayOf.

public Array createArrayOf(String typeName, Object[] elements) throws SQLException {
    checkOpen();
    @SuppressWarnings("unchecked") List<Object> elementList = (List<Object>) AvaticaUtils.primitiveList(elements);
    SqlType type;
    try {
        type = SqlType.valueOf(typeName);
    } catch (IllegalArgumentException e) {
        throw new SQLException("Could not find JDBC type for '" + typeName + "'");
    }
    AvaticaType avaticaType = null;
    switch(type) {
        case ARRAY:
            // TODO: Nested ARRAYs
            throw HELPER.createException("Cannot create an ARRAY of ARRAY's");
        case STRUCT:
            // TODO: ARRAYs of STRUCTs
            throw HELPER.createException("Cannot create an ARRAY of STRUCT's");
        default:
            // This is an ARRAY, we need to use Objects, not primitives (nullable).
            avaticaType = ColumnMetaData.scalar(type.id, typeName, Rep.nonPrimitiveRepOf(type));
    }
    ArrayFactoryImpl arrayFactory = new ArrayFactoryImpl(getTimeZone());
    return arrayFactory.createArray(avaticaType, elementList);
}
Also used : ArrayFactoryImpl(org.apache.calcite.avatica.util.ArrayFactoryImpl) SQLException(java.sql.SQLException) AvaticaType(org.apache.calcite.avatica.ColumnMetaData.AvaticaType) List(java.util.List)

Example 4 with AvaticaType

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

the class TypedValue method serialToJdbc.

/**
 * Converts the given value from serial form to JDBC form.
 *
 * @param type The type of the value
 * @param value The value
 * @param calendar A calendar instance
 * @return The JDBC representation of the value.
 */
private static Object serialToJdbc(ColumnMetaData.Rep type, ColumnMetaData.Rep componentRep, Object value, Calendar calendar) {
    switch(type) {
        case BYTE_STRING:
            return ByteString.ofBase64((String) value).getBytes();
        case JAVA_UTIL_DATE:
            return new java.util.Date(adjust((Number) value, calendar));
        case JAVA_SQL_DATE:
            return new java.sql.Date(adjust(((Number) value).longValue() * DateTimeUtils.MILLIS_PER_DAY, calendar));
        case JAVA_SQL_TIME:
            return new java.sql.Time(adjust((Number) value, calendar));
        case JAVA_SQL_TIMESTAMP:
            return new java.sql.Timestamp(adjust((Number) value, calendar));
        case ARRAY:
            if (null == value) {
                return null;
            }
            final List<?> list = (List<?>) value;
            final List<Object> copy = new ArrayList<>(list.size());
            // Copy the list from the serial representation to a JDBC representation
            for (Object o : list) {
                if (null == o) {
                    copy.add(null);
                } else if (o instanceof TypedValue) {
                    // Protobuf can maintain the TypedValue hierarchy to simplify things
                    copy.add(((TypedValue) o).toJdbc(calendar));
                } else {
                    // We can't get the above recursion with the JSON serialization
                    copy.add(serialToJdbc(componentRep, null, o, calendar));
                }
            }
            if (componentRep == null && list.size() > 0) {
                componentRep = ((TypedValue) list.get(0)).type;
                if (componentRep == null) {
                    throw new RuntimeException("ComponentRep of element must not be null for ARRAYs");
                }
            }
            AvaticaType elementType = new AvaticaType(componentRep.typeId, componentRep.name(), componentRep);
            return new ArrayFactoryImpl(calendar.getTimeZone()).createArray(elementType, copy);
        default:
            return serialToLocal(type, value);
    }
}
Also used : AvaticaType(org.apache.calcite.avatica.ColumnMetaData.AvaticaType) ArrayList(java.util.ArrayList) Time(java.sql.Time) ByteString(org.apache.calcite.avatica.util.ByteString) Timestamp(java.sql.Timestamp) Date(java.util.Date) ArrayFactoryImpl(org.apache.calcite.avatica.util.ArrayFactoryImpl) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with AvaticaType

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

the class ArrayTypeTest method testCreateArrayOf.

@Test
public void testCreateArrayOf() throws Exception {
    try (Connection conn = DriverManager.getConnection(url)) {
        final String componentName = SqlType.INTEGER.name();
        Array a1 = conn.createArrayOf(componentName, new Object[] { 1, 2, 3, 4, 5 });
        Array a2 = conn.createArrayOf(componentName, new Object[] { 2, 3, 4, 5, 6 });
        Array a3 = conn.createArrayOf(componentName, new Object[] { 3, 4, 5, 6, 7 });
        AvaticaType arrayType = ColumnMetaData.array(ColumnMetaData.scalar(Types.INTEGER, componentName, Rep.INTEGER), "NUMBERS", Rep.ARRAY);
        writeAndReadArrays(conn, "CREATE_ARRAY_OF_INTEGERS", componentName, arrayType, Arrays.asList(a1, a2, a3), PRIMITIVE_LIST_VALIDATOR);
    }
}
Also used : Array(java.sql.Array) AvaticaType(org.apache.calcite.avatica.ColumnMetaData.AvaticaType) Connection(java.sql.Connection) Test(org.junit.Test)

Aggregations

AvaticaType (org.apache.calcite.avatica.ColumnMetaData.AvaticaType)8 ArrayList (java.util.ArrayList)6 ColumnMetaData (org.apache.calcite.avatica.ColumnMetaData)5 Array (java.sql.Array)2 List (java.util.List)2 ArrayFactoryImpl (org.apache.calcite.avatica.util.ArrayFactoryImpl)2 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)2 ResultColumnMetadata (org.apache.drill.exec.proto.UserProtos.ResultColumnMetadata)2 MaterializedField (org.apache.drill.exec.record.MaterializedField)2 Connection (java.sql.Connection)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 Time (java.sql.Time)1 Timestamp (java.sql.Timestamp)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 ArrayType (org.apache.calcite.avatica.ColumnMetaData.ArrayType)1 SqlType (org.apache.calcite.avatica.SqlType)1 ByteString (org.apache.calcite.avatica.util.ByteString)1 Test (org.junit.Test)1