use of com.facebook.presto.spi.type.NamedTypeSignature in project presto by prestodb.
the class QueryResults method fixValue.
/**
* Force values coming from Jackson to have the expected object type.
*/
private static Object fixValue(TypeSignature signature, Object value) {
if (value == null) {
return null;
}
if (signature.getBase().equals(ARRAY)) {
List<Object> fixedValue = new ArrayList<>();
for (Object object : List.class.cast(value)) {
fixedValue.add(fixValue(signature.getTypeParametersAsTypeSignatures().get(0), object));
}
return fixedValue;
}
if (signature.getBase().equals(MAP)) {
TypeSignature keySignature = signature.getTypeParametersAsTypeSignatures().get(0);
TypeSignature valueSignature = signature.getTypeParametersAsTypeSignatures().get(1);
Map<Object, Object> fixedValue = new HashMap<>();
for (Map.Entry<?, ?> entry : (Set<Map.Entry<?, ?>>) Map.class.cast(value).entrySet()) {
fixedValue.put(fixValue(keySignature, entry.getKey()), fixValue(valueSignature, entry.getValue()));
}
return fixedValue;
}
if (signature.getBase().equals(ROW)) {
Map<String, Object> fixedValue = new LinkedHashMap<>();
List<Object> listValue = List.class.cast(value);
checkArgument(listValue.size() == signature.getParameters().size(), "Mismatched data values and row type");
for (int i = 0; i < listValue.size(); i++) {
TypeSignatureParameter parameter = signature.getParameters().get(i);
checkArgument(parameter.getKind() == ParameterKind.NAMED_TYPE, "Unexpected parameter [%s] for row type", parameter);
NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
String key = namedTypeSignature.getName();
fixedValue.put(key, fixValue(namedTypeSignature.getTypeSignature(), listValue.get(i)));
}
return fixedValue;
}
switch(signature.getBase()) {
case BIGINT:
if (value instanceof String) {
return Long.parseLong((String) value);
}
return ((Number) value).longValue();
case INTEGER:
if (value instanceof String) {
return Integer.parseInt((String) value);
}
return ((Number) value).intValue();
case SMALLINT:
if (value instanceof String) {
return Short.parseShort((String) value);
}
return ((Number) value).shortValue();
case TINYINT:
if (value instanceof String) {
return Byte.parseByte((String) value);
}
return ((Number) value).byteValue();
case DOUBLE:
if (value instanceof String) {
return Double.parseDouble((String) value);
}
return ((Number) value).doubleValue();
case REAL:
if (value instanceof String) {
return Float.parseFloat((String) value);
}
return ((Number) value).floatValue();
case BOOLEAN:
if (value instanceof String) {
return Boolean.parseBoolean((String) value);
}
return Boolean.class.cast(value);
case VARCHAR:
case JSON:
case TIME:
case TIME_WITH_TIME_ZONE:
case TIMESTAMP:
case TIMESTAMP_WITH_TIME_ZONE:
case DATE:
case INTERVAL_YEAR_TO_MONTH:
case INTERVAL_DAY_TO_SECOND:
case DECIMAL:
case CHAR:
return String.class.cast(value);
default:
// as a plain text and everything else is base64 encoded binary
if (value instanceof String) {
return Base64.getDecoder().decode((String) value);
}
return value;
}
}
use of com.facebook.presto.spi.type.NamedTypeSignature in project presto by prestodb.
the class ParquetReader method readStruct.
private Block readStruct(Type type, List<String> path, IntList elementOffsets) throws IOException {
List<TypeSignatureParameter> parameters = type.getTypeSignature().getParameters();
Block[] blocks = new Block[parameters.size()];
for (int i = 0; i < parameters.size(); i++) {
NamedTypeSignature namedTypeSignature = parameters.get(i).getNamedTypeSignature();
Type fieldType = typeManager.getType(namedTypeSignature.getTypeSignature());
String name = namedTypeSignature.getName();
blocks[i] = readBlock(name, fieldType, path, new IntArrayList());
}
InterleavedBlock interleavedBlock = new InterleavedBlock(blocks);
int blockSize = blocks[0].getPositionCount();
int[] offsets = new int[blockSize + 1];
for (int i = 1; i < offsets.length; i++) {
elementOffsets.add(parameters.size());
offsets[i] = i * parameters.size();
}
return new ArrayBlock(blockSize, new boolean[blockSize], offsets, interleavedBlock);
}
use of com.facebook.presto.spi.type.NamedTypeSignature in project presto by prestodb.
the class HiveTypeTranslator method translate.
@Override
public TypeInfo translate(Type type) {
if (BOOLEAN.equals(type)) {
return HIVE_BOOLEAN.getTypeInfo();
}
if (BIGINT.equals(type)) {
return HIVE_LONG.getTypeInfo();
}
if (INTEGER.equals(type)) {
return HIVE_INT.getTypeInfo();
}
if (SMALLINT.equals(type)) {
return HIVE_SHORT.getTypeInfo();
}
if (TINYINT.equals(type)) {
return HIVE_BYTE.getTypeInfo();
}
if (REAL.equals(type)) {
return HIVE_FLOAT.getTypeInfo();
}
if (DOUBLE.equals(type)) {
return HIVE_DOUBLE.getTypeInfo();
}
if (type instanceof VarcharType) {
VarcharType varcharType = (VarcharType) type;
int varcharLength = varcharType.getLength();
if (varcharLength <= HiveVarchar.MAX_VARCHAR_LENGTH) {
return getVarcharTypeInfo(varcharLength);
} else if (varcharLength == VarcharType.UNBOUNDED_LENGTH) {
return HIVE_STRING.getTypeInfo();
} else {
throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported VARCHAR types: VARCHAR(<=%d), VARCHAR.", type, HiveVarchar.MAX_VARCHAR_LENGTH));
}
}
if (type instanceof CharType) {
CharType charType = (CharType) type;
int charLength = charType.getLength();
if (charLength <= HiveChar.MAX_CHAR_LENGTH) {
return getCharTypeInfo(charLength);
}
throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported CHAR types: CHAR(<=%d).", type, HiveChar.MAX_CHAR_LENGTH));
}
if (VARBINARY.equals(type)) {
return HIVE_BINARY.getTypeInfo();
}
if (DATE.equals(type)) {
return HIVE_DATE.getTypeInfo();
}
if (TIMESTAMP.equals(type)) {
return HIVE_TIMESTAMP.getTypeInfo();
}
if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
return new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale());
}
if (isArrayType(type)) {
TypeInfo elementType = translate(type.getTypeParameters().get(0));
return getListTypeInfo(elementType);
}
if (isMapType(type)) {
TypeInfo keyType = translate(type.getTypeParameters().get(0));
TypeInfo valueType = translate(type.getTypeParameters().get(1));
return getMapTypeInfo(keyType, valueType);
}
if (isRowType(type)) {
ImmutableList.Builder<String> fieldNames = ImmutableList.builder();
for (TypeSignatureParameter parameter : type.getTypeSignature().getParameters()) {
if (!parameter.isNamedTypeSignature()) {
throw new IllegalArgumentException(format("Expected all parameters to be named type, but got %s", parameter));
}
NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
fieldNames.add(namedTypeSignature.getName());
}
return getStructTypeInfo(fieldNames.build(), type.getTypeParameters().stream().map(this::translate).collect(toList()));
}
throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
Aggregations