use of io.trino.spi.type.MapType in project trino by trinodb.
the class OrcTester method preprocessWriteValueHive.
private static Object preprocessWriteValueHive(Type type, Object value) {
if (value == null) {
return null;
}
if (type.equals(BOOLEAN)) {
return value;
}
if (type.equals(TINYINT)) {
return ((Number) value).byteValue();
}
if (type.equals(SMALLINT)) {
return ((Number) value).shortValue();
}
if (type.equals(INTEGER)) {
return ((Number) value).intValue();
}
if (type.equals(BIGINT)) {
return ((Number) value).longValue();
}
if (type.equals(REAL)) {
return ((Number) value).floatValue();
}
if (type.equals(DOUBLE)) {
return ((Number) value).doubleValue();
}
if (type instanceof VarcharType) {
return value;
}
if (type instanceof CharType) {
return new HiveChar((String) value, ((CharType) type).getLength());
}
if (type.equals(VARBINARY)) {
return ((SqlVarbinary) value).getBytes();
}
if (type.equals(DATE)) {
return Date.ofEpochDay(((SqlDate) value).getDays());
}
if (type.equals(TIMESTAMP_MILLIS) || type.equals(TIMESTAMP_MICROS) || type.equals(TIMESTAMP_NANOS)) {
LocalDateTime dateTime = ((SqlTimestamp) value).toLocalDateTime();
return Timestamp.ofEpochSecond(dateTime.toEpochSecond(ZoneOffset.UTC), dateTime.getNano());
}
if (type.equals(TIMESTAMP_TZ_MILLIS) || type.equals(TIMESTAMP_TZ_MICROS) || type.equals(TIMESTAMP_TZ_NANOS)) {
SqlTimestampWithTimeZone timestamp = (SqlTimestampWithTimeZone) value;
int nanosOfMilli = roundDiv(timestamp.getPicosOfMilli(), PICOSECONDS_PER_NANOSECOND);
return Timestamp.ofEpochMilli(timestamp.getEpochMillis(), nanosOfMilli);
}
if (type instanceof DecimalType) {
return HiveDecimal.create(((SqlDecimal) value).toBigDecimal());
}
if (type instanceof ArrayType) {
Type elementType = type.getTypeParameters().get(0);
return ((List<?>) value).stream().map(element -> preprocessWriteValueHive(elementType, element)).collect(toList());
}
if (type instanceof MapType) {
Type keyType = type.getTypeParameters().get(0);
Type valueType = type.getTypeParameters().get(1);
Map<Object, Object> newMap = new HashMap<>();
for (Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
newMap.put(preprocessWriteValueHive(keyType, entry.getKey()), preprocessWriteValueHive(valueType, entry.getValue()));
}
return newMap;
}
if (type instanceof RowType) {
List<?> fieldValues = (List<?>) value;
List<Type> fieldTypes = type.getTypeParameters();
List<Object> newStruct = new ArrayList<>();
for (int fieldId = 0; fieldId < fieldValues.size(); fieldId++) {
newStruct.add(preprocessWriteValueHive(fieldTypes.get(fieldId), fieldValues.get(fieldId)));
}
return newStruct;
}
throw new IllegalArgumentException("unsupported type: " + type);
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class OrcTester method testMapRoundTrip.
private void testMapRoundTrip(Type type, List<?> readValues) throws Exception {
Type mapType = mapType(type, type);
// maps cannot have a null key, so select a value to use for the map key when the value is null
Object readNullKeyValue = Iterables.getLast(readValues);
// values in simple map
testRoundTripType(mapType, readValues.stream().map(value -> toHiveMap(value, readNullKeyValue)).collect(toList()));
if (structuralNullTestsEnabled) {
// values and nulls in simple map
testRoundTripType(mapType, insertNullEvery(5, readValues).stream().map(value -> toHiveMap(value, readNullKeyValue)).collect(toList()));
// all null values in simple map
testRoundTripType(mapType, readValues.stream().map(value -> toHiveMap(null, readNullKeyValue)).collect(toList()));
}
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class CheckpointWriter method writeStringMap.
private void writeStringMap(BlockBuilder blockBuilder, RowType type, int fieldId, String fieldName, @Nullable Map<String, String> values) {
RowType.Field field = validateAndGetField(type, fieldId, fieldName);
checkArgument(field.getType() instanceof MapType, "Expected field %s/%s to by of MapType but got %s", fieldId, fieldName, field.getType());
if (values == null) {
blockBuilder.appendNull();
return;
}
MapType mapType = (MapType) field.getType();
BlockBuilder mapBuilder = blockBuilder.beginBlockEntry();
for (Map.Entry<String, String> entry : values.entrySet()) {
mapType.getKeyType().writeSlice(mapBuilder, utf8Slice(entry.getKey()));
if (entry.getValue() == null) {
mapBuilder.appendNull();
} else {
mapType.getKeyType().writeSlice(mapBuilder, utf8Slice(entry.getValue()));
}
}
blockBuilder.closeEntry();
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class DeltaLakeSchemaSupport method validateStructuralType.
private static void validateStructuralType(Optional<Type> rootType, Type type) {
if (type instanceof ArrayType) {
validateType(rootType, ((ArrayType) type).getElementType());
}
if (type instanceof MapType) {
MapType mapType = (MapType) type;
validateType(rootType, mapType.getKeyType());
validateType(rootType, mapType.getValueType());
}
if (type instanceof RowType) {
RowType rowType = (RowType) type;
rowType.getFields().forEach(field -> validateType(rootType, field.getType()));
}
}
use of io.trino.spi.type.MapType in project trino by trinodb.
the class IcebergPageSourceProvider method getOrcReadType.
private static Type getOrcReadType(Type columnType, TypeManager typeManager) {
if (columnType == UUID) {
// TODO: Validate that the OrcColumn attribute ICEBERG_BINARY_TYPE is equal to "UUID"
return VARBINARY;
}
if (columnType instanceof ArrayType) {
return new ArrayType(getOrcReadType(((ArrayType) columnType).getElementType(), typeManager));
}
if (columnType instanceof MapType) {
MapType mapType = (MapType) columnType;
Type keyType = getOrcReadType(mapType.getKeyType(), typeManager);
Type valueType = getOrcReadType(mapType.getValueType(), typeManager);
return new MapType(keyType, valueType, typeManager.getTypeOperators());
}
if (columnType instanceof RowType) {
return RowType.from(((RowType) columnType).getFields().stream().map(field -> new RowType.Field(field.getName(), getOrcReadType(field.getType(), typeManager))).collect(toImmutableList()));
}
return columnType;
}
Aggregations