use of com.facebook.presto.common.type.TypeWithName in project presto by prestodb.
the class HiveTypeTranslator method translate.
@Override
public TypeInfo translate(Type type, Optional<HiveType> defaultHiveType) {
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 EnumType<?>) {
return translate(((EnumType<?>) type).getValueType());
}
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 (type instanceof TypeWithName) {
return translate(((TypeWithName) type).getType());
}
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), defaultHiveType);
return getListTypeInfo(elementType);
}
if (isMapType(type)) {
TypeInfo keyType = translate(type.getTypeParameters().get(0), defaultHiveType);
TypeInfo valueType = translate(type.getTypeParameters().get(1), defaultHiveType);
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();
if (!namedTypeSignature.getName().isPresent()) {
throw new PrestoException(NOT_SUPPORTED, format("Anonymous row type is not supported in Hive. Please give each field a name: %s", type));
}
fieldNames.add(namedTypeSignature.getName().get());
}
return getStructTypeInfo(fieldNames.build(), type.getTypeParameters().stream().map(t -> translate(t, defaultHiveType)).collect(toList()));
}
return defaultHiveType.orElseThrow(() -> new PrestoException(NOT_SUPPORTED, format("No default Hive type provided for unsupported Hive type: %s", type))).getTypeInfo();
}
use of com.facebook.presto.common.type.TypeWithName in project presto by prestodb.
the class H2QueryRunner method rowMapper.
private static RowMapper<MaterializedRow> rowMapper(List<? extends Type> types) {
return new RowMapper<MaterializedRow>() {
private Object getValue(Type type, ResultSet resultSet, int position) throws SQLException {
if (BOOLEAN.equals(type)) {
boolean booleanValue = resultSet.getBoolean(position);
return resultSet.wasNull() ? null : booleanValue;
} else if (TINYINT.equals(type)) {
byte byteValue = resultSet.getByte(position);
return resultSet.wasNull() ? null : byteValue;
} else if (SMALLINT.equals(type)) {
short shortValue = resultSet.getShort(position);
return resultSet.wasNull() ? null : shortValue;
} else if (INTEGER.equals(type)) {
int intValue = resultSet.getInt(position);
return resultSet.wasNull() ? null : intValue;
} else if (BIGINT.equals(type)) {
long longValue = resultSet.getLong(position);
return resultSet.wasNull() ? null : longValue;
} else if (REAL.equals(type)) {
float floatValue = resultSet.getFloat(position);
return resultSet.wasNull() ? null : floatValue;
} else if (DOUBLE.equals(type)) {
double doubleValue = resultSet.getDouble(position);
return resultSet.wasNull() ? null : doubleValue;
} else if (isVarcharType(type)) {
String stringValue = resultSet.getString(position);
return resultSet.wasNull() ? null : stringValue;
} else if (isCharType(type)) {
String stringValue = resultSet.getString(position);
return resultSet.wasNull() ? null : padEnd(stringValue, ((CharType) type).getLength(), ' ');
} else if (VARBINARY.equals(type)) {
byte[] binary = resultSet.getBytes(position);
return resultSet.wasNull() ? null : binary;
} else if (DATE.equals(type)) {
// resultSet.getDate(i) doesn't work if JVM's zone skipped day being retrieved (e.g. 2011-12-30 and Pacific/Apia zone)
LocalDate dateValue = resultSet.getObject(position, LocalDate.class);
return resultSet.wasNull() ? null : dateValue;
} else if (TIME.equals(type)) {
// resultSet.getTime(i) doesn't work if JVM's zone had forward offset change during 1970-01-01 (e.g. America/Hermosillo zone)
LocalTime timeValue = resultSet.getObject(position, LocalTime.class);
return resultSet.wasNull() ? null : timeValue;
} else if (TIME_WITH_TIME_ZONE.equals(type)) {
throw new UnsupportedOperationException("H2 does not support TIME WITH TIME ZONE");
} else if (TIMESTAMP.equals(type)) {
// resultSet.getTimestamp(i) doesn't work if JVM's zone had forward offset at the date/time being retrieved
LocalDateTime timestampValue;
try {
timestampValue = resultSet.getObject(position, LocalDateTime.class);
} catch (SQLException first) {
// H2 cannot convert DATE to LocalDateTime in their JDBC driver (even though it can convert to java.sql.Timestamp), we need to do this manually
try {
timestampValue = Optional.ofNullable(resultSet.getObject(position, LocalDate.class)).map(LocalDate::atStartOfDay).orElse(null);
} catch (RuntimeException e) {
first.addSuppressed(e);
throw first;
}
}
return resultSet.wasNull() ? null : timestampValue;
} else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
// This means H2 is unsuitable for testing TIMESTAMP WITH TIME ZONE-bearing queries. Those need to be tested manually.
throw new UnsupportedOperationException();
} else if (UNKNOWN.equals(type)) {
Object objectValue = resultSet.getObject(position);
checkState(resultSet.wasNull(), "Expected a null value, but got %s", objectValue);
return null;
} else if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
BigDecimal decimalValue = resultSet.getBigDecimal(position);
return resultSet.wasNull() ? null : decimalValue.setScale(decimalType.getScale(), BigDecimal.ROUND_HALF_UP).round(new MathContext(decimalType.getPrecision()));
} else if (type instanceof ArrayType) {
Array array = resultSet.getArray(position);
return resultSet.wasNull() ? null : newArrayList(mapArrayValues(((ArrayType) type), (Object[]) array.getArray()));
} else if (type instanceof RowType) {
Array array = resultSet.getArray(position);
return resultSet.wasNull() ? null : newArrayList(mapRowValues((RowType) type, (Object[]) array.getArray()));
} else if (type instanceof TypeWithName) {
return getValue(((TypeWithName) type).getType(), resultSet, position);
} else {
throw new AssertionError("unhandled type: " + type);
}
}
@Override
public MaterializedRow map(ResultSet resultSet, StatementContext context) throws SQLException {
int count = resultSet.getMetaData().getColumnCount();
checkArgument(types.size() == count, "expected types count (%s) does not match actual column count (%s)", types.size(), count);
List<Object> row = new ArrayList<>(count);
for (int i = 1; i <= count; i++) {
row.add(getValue(types.get(i - 1), resultSet, i));
}
return new MaterializedRow(MaterializedResult.DEFAULT_PRECISION, row);
}
};
}
use of com.facebook.presto.common.type.TypeWithName in project presto by prestodb.
the class SignatureBinder method applyBoundVariables.
public static Type applyBoundVariables(FunctionAndTypeManager functionAndTypeManager, TypeSignature typeSignature, BoundVariables boundVariables) {
String baseType = typeSignature.getBase();
if (boundVariables.containsTypeVariable(baseType)) {
checkState(typeSignature.getParameters().isEmpty(), "Type parameters cannot have parameters");
Type type = boundVariables.getTypeVariable(baseType);
if (type instanceof TypeWithName) {
return ((TypeWithName) type).getType();
}
return type;
}
List<TypeSignatureParameter> parameters = typeSignature.getParameters().stream().map(typeSignatureParameter -> applyBoundVariables(typeSignatureParameter, boundVariables)).collect(toList());
return functionAndTypeManager.getParameterizedType(baseType, parameters);
}
use of com.facebook.presto.common.type.TypeWithName in project presto by prestodb.
the class TestingPrestoClient method convertToRowValue.
private static Object convertToRowValue(Type type, Object value) {
if (value == null) {
return null;
}
if (BOOLEAN.equals(type)) {
return value;
} else if (TINYINT.equals(type)) {
return ((Number) value).byteValue();
} else if (SMALLINT.equals(type)) {
return ((Number) value).shortValue();
} else if (INTEGER.equals(type)) {
return ((Number) value).intValue();
} else if (BIGINT.equals(type)) {
return ((Number) value).longValue();
} else if (DOUBLE.equals(type)) {
return ((Number) value).doubleValue();
} else if (REAL.equals(type)) {
return ((Number) value).floatValue();
} else if (type instanceof VarcharType) {
return value;
} else if (isCharType(type)) {
return value;
} else if (VARBINARY.equals(type)) {
return value;
} else if (DATE.equals(type)) {
return DateTimeFormatter.ISO_LOCAL_DATE.parse(((String) value), LocalDate::from);
} else if (TIME.equals(type)) {
return DateTimeFormatter.ISO_LOCAL_TIME.parse(((String) value), LocalTime::from);
} else if (TIME_WITH_TIME_ZONE.equals(type)) {
// Only zone-offset timezones are supported (TODO remove political timezones support for TIME WITH TIME ZONE)
try {
return timeWithUtcZoneFormat.parse(((String) value), LocalTime::from).atOffset(ZoneOffset.UTC);
} catch (DateTimeParseException e) {
return timeWithZoneOffsetFormat.parse(((String) value), OffsetTime::from);
}
} else if (TIMESTAMP.equals(type)) {
return SqlTimestamp.JSON_FORMATTER.parse((String) value, LocalDateTime::from);
} else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
return timestampWithTimeZoneFormat.parse((String) value, ZonedDateTime::from);
} else if (INTERVAL_DAY_TIME.equals(type)) {
return new SqlIntervalDayTime(IntervalDayTime.parseMillis(String.valueOf(value)));
} else if (INTERVAL_YEAR_MONTH.equals(type)) {
return new SqlIntervalYearMonth(IntervalYearMonth.parseMonths(String.valueOf(value)));
} else if (IPADDRESS.equals(type)) {
return value;
} else if (type instanceof ArrayType) {
return ((List<Object>) value).stream().map(element -> convertToRowValue(((ArrayType) type).getElementType(), element)).collect(toList());
} else if (type instanceof MapType) {
return ((Map<Object, Object>) value).entrySet().stream().collect(Collectors.toMap(e -> convertToRowValue(((MapType) type).getKeyType(), e.getKey()), e -> convertToRowValue(((MapType) type).getValueType(), e.getValue())));
} else if (type instanceof RowType) {
Map<String, Object> data = (Map<String, Object>) value;
RowType rowType = (RowType) type;
return rowType.getFields().stream().map(field -> convertToRowValue(field.getType(), data.get(field.getName().get()))).collect(toList());
} else if (type instanceof DecimalType) {
return new BigDecimal((String) value);
} else if (type instanceof JsonType) {
return value;
} else if (type instanceof VarcharEnumType) {
return value;
} else if (type instanceof BigintEnumType) {
return ((Number) value).longValue();
} else if (type instanceof TypeWithName) {
return convertToRowValue(((TypeWithName) type).getType(), value);
} else if (type.getTypeSignature().getBase().equals("ObjectId")) {
return value;
} else {
throw new AssertionError("unhandled type: " + type);
}
}
Aggregations