use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class OrcTester method testMapRoundTrip.
private void testMapRoundTrip(Type type, List<?> readValues) throws Exception {
Type mapType = mapType(type, type);
// maps can not 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 com.facebook.presto.common.type.MapType in project presto by prestodb.
the class SubfieldExtractor method toRowExpression.
public RowExpression toRowExpression(Subfield subfield, Type columnType) {
List<Subfield.PathElement> path = subfield.getPath();
ImmutableList.Builder<Type> types = ImmutableList.builder();
types.add(columnType);
Type type = columnType;
for (int i = 0; i < path.size(); i++) {
if (type instanceof RowType) {
type = getFieldType((RowType) type, ((Subfield.NestedField) path.get(i)).getName());
types.add(type);
} else if (type instanceof ArrayType) {
type = ((ArrayType) type).getElementType();
types.add(type);
} else if (type instanceof MapType) {
type = ((MapType) type).getValueType();
types.add(type);
} else {
verify(false, "Unexpected type: " + type);
}
}
return toRowExpression(subfield, types.build());
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class SubfieldExtractor method toRowExpression.
private RowExpression toRowExpression(Subfield subfield, List<Type> types) {
List<Subfield.PathElement> path = subfield.getPath();
if (path.isEmpty()) {
return new VariableReferenceExpression(Optional.empty(), subfield.getRootName(), types.get(0));
}
RowExpression base = toRowExpression(new Subfield(subfield.getRootName(), path.subList(0, path.size() - 1)), types.subList(0, types.size() - 1));
Type baseType = types.get(types.size() - 2);
Subfield.PathElement pathElement = path.get(path.size() - 1);
if (pathElement instanceof Subfield.LongSubscript) {
Type indexType = baseType instanceof MapType ? ((MapType) baseType).getKeyType() : BIGINT;
FunctionHandle functionHandle = functionResolution.subscriptFunction(baseType, indexType);
ConstantExpression index = new ConstantExpression(base.getSourceLocation(), ((Subfield.LongSubscript) pathElement).getIndex(), indexType);
return new CallExpression(base.getSourceLocation(), SUBSCRIPT.name(), functionHandle, types.get(types.size() - 1), ImmutableList.of(base, index));
}
if (pathElement instanceof Subfield.StringSubscript) {
Type indexType = ((MapType) baseType).getKeyType();
FunctionHandle functionHandle = functionResolution.subscriptFunction(baseType, indexType);
ConstantExpression index = new ConstantExpression(base.getSourceLocation(), Slices.utf8Slice(((Subfield.StringSubscript) pathElement).getIndex()), indexType);
return new CallExpression(base.getSourceLocation(), SUBSCRIPT.name(), functionHandle, types.get(types.size() - 1), ImmutableList.of(base, index));
}
if (pathElement instanceof Subfield.NestedField) {
Subfield.NestedField nestedField = (Subfield.NestedField) pathElement;
return new SpecialFormExpression(base.getSourceLocation(), DEREFERENCE, types.get(types.size() - 1), base, new ConstantExpression(base.getSourceLocation(), getFieldIndex((RowType) baseType, nestedField.getName()), INTEGER));
}
verify(false, "Unexpected path element: " + pathElement);
return null;
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class ParquetTester method writeValue.
private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
if (value == null) {
blockBuilder.appendNull();
} else {
if (BOOLEAN.equals(type)) {
type.writeBoolean(blockBuilder, (Boolean) value);
} else if (TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type)) {
type.writeLong(blockBuilder, ((Number) value).longValue());
} else if (Decimals.isShortDecimal(type)) {
type.writeLong(blockBuilder, ((SqlDecimal) value).getUnscaledValue().longValue());
} else if (Decimals.isLongDecimal(type)) {
if (Decimals.overflows(((SqlDecimal) value).getUnscaledValue(), MAX_PRECISION_INT64)) {
type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).toBigDecimal().unscaledValue()));
} else {
type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).getUnscaledValue().longValue()));
}
} else if (DOUBLE.equals(type)) {
type.writeDouble(blockBuilder, ((Number) value).doubleValue());
} else if (REAL.equals(type)) {
float floatValue = ((Number) value).floatValue();
type.writeLong(blockBuilder, Float.floatToIntBits(floatValue));
} else if (type instanceof VarcharType) {
Slice slice = truncateToLength(utf8Slice((String) value), type);
type.writeSlice(blockBuilder, slice);
} else if (type instanceof CharType) {
Slice slice = truncateToLengthAndTrimSpaces(utf8Slice((String) value), type);
type.writeSlice(blockBuilder, slice);
} else if (VARBINARY.equals(type)) {
type.writeSlice(blockBuilder, Slices.wrappedBuffer(((SqlVarbinary) value).getBytes()));
} else if (DATE.equals(type)) {
long days = ((SqlDate) value).getDays();
type.writeLong(blockBuilder, days);
} else if (TIMESTAMP.equals(type)) {
long millis = ((SqlTimestamp) value).getMillisUtc();
type.writeLong(blockBuilder, millis);
} else {
if (type instanceof ArrayType) {
List<?> array = (List<?>) value;
Type elementType = type.getTypeParameters().get(0);
BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
for (Object elementValue : array) {
writeValue(elementType, arrayBlockBuilder, elementValue);
}
blockBuilder.closeEntry();
} else if (type instanceof MapType) {
Map<?, ?> map = (Map<?, ?>) value;
Type keyType = type.getTypeParameters().get(0);
Type valueType = type.getTypeParameters().get(1);
BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
for (Map.Entry<?, ?> entry : map.entrySet()) {
writeValue(keyType, mapBlockBuilder, entry.getKey());
writeValue(valueType, mapBlockBuilder, entry.getValue());
}
blockBuilder.closeEntry();
} else if (type instanceof RowType) {
List<?> array = (List<?>) value;
List<Type> fieldTypes = type.getTypeParameters();
BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
for (int fieldId = 0; fieldId < fieldTypes.size(); fieldId++) {
Type fieldType = fieldTypes.get(fieldId);
writeValue(fieldType, rowBlockBuilder, array.get(fieldId));
}
blockBuilder.closeEntry();
} else {
throw new IllegalArgumentException("Unsupported type " + type);
}
}
}
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class AbstractTestType method getNonNullValueForType.
/**
* @return a non-null value, represented in native container type
*/
private static Object getNonNullValueForType(Type type) {
if (type.getJavaType() == boolean.class) {
return true;
}
if (type.getJavaType() == long.class) {
return 1L;
}
if (type.getJavaType() == double.class) {
return 1.0;
}
if (type.getJavaType() == Slice.class) {
return Slices.utf8Slice("_");
}
if (type instanceof ArrayType) {
ArrayType arrayType = (ArrayType) type;
Type elementType = arrayType.getElementType();
Object elementNonNullValue = getNonNullValueForType(elementType);
return arrayBlockOf(elementType, elementNonNullValue);
}
if (type instanceof MapType) {
MapType mapType = (MapType) type;
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
Object keyNonNullValue = getNonNullValueForType(keyType);
Object valueNonNullValue = getNonNullValueForType(valueType);
Map map = ImmutableMap.of(keyNonNullValue, valueNonNullValue);
return mapBlockOf(keyType, valueType, map);
}
if (type instanceof RowType) {
RowType rowType = (RowType) type;
List<Type> elementTypes = rowType.getTypeParameters();
Object[] elementNonNullValues = elementTypes.stream().map(AbstractTestType::getNonNullValueForType).toArray(Object[]::new);
return toRow(elementTypes, elementNonNullValues);
}
throw new IllegalStateException("Unsupported Java type " + type.getJavaType() + " (for type " + type + ")");
}
Aggregations