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