use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.
the class BigQueryResultPageSource method writeBlock.
private void writeBlock(BlockBuilder output, Type type, Object value) {
if (type instanceof ArrayType && value instanceof List<?>) {
BlockBuilder builder = output.beginBlockEntry();
for (Object element : (List<?>) value) {
appendTo(type.getTypeParameters().get(0), element, builder);
}
output.closeEntry();
return;
}
if (type instanceof RowType && value instanceof GenericRecord) {
GenericRecord record = (GenericRecord) value;
BlockBuilder builder = output.beginBlockEntry();
List<String> fieldNames = new ArrayList<>();
for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
}
checkState(fieldNames.size() == type.getTypeParameters().size(), "fieldName doesn't match with type size : %s", type);
for (int index = 0; index < type.getTypeParameters().size(); index++) {
appendTo(type.getTypeParameters().get(index), record.get(fieldNames.get(index)), builder);
}
output.closeEntry();
return;
}
throw new TrinoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Block: " + type.getTypeSignature());
}
use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.
the class DeltaHiveTypeTranslator method translate.
// Copy from HiveTypeTranslator with a custom mapping for TimestampWithTimeZone
public static TypeInfo translate(Type type) {
requireNonNull(type, "type is null");
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;
if (varcharType.isUnbounded()) {
return HIVE_STRING.getTypeInfo();
}
if (varcharType.getBoundedLength() <= HiveVarchar.MAX_VARCHAR_LENGTH) {
return getVarcharTypeInfo(varcharType.getBoundedLength());
}
throw new TrinoException(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 TrinoException(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 (type instanceof TimestampWithTimeZoneType) {
verify(((TimestampWithTimeZoneType) type).getPrecision() == 3, "Unsupported type: %s", type);
return HIVE_TIMESTAMP.getTypeInfo();
}
if (type instanceof TimestampType) {
verify(((TimestampType) type).getPrecision() == 3, "Unsupported type: %s", 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();
if (namedTypeSignature.getName().isEmpty()) {
throw new TrinoException(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(DeltaHiveTypeTranslator::translate).collect(toImmutableList()));
}
throw new TrinoException(NOT_SUPPORTED, format("Unsupported Delta Lake type: %s", type));
}
use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.
the class RowToJsonCast method specialize.
@Override
protected ScalarFunctionImplementation specialize(BoundSignature boundSignature) {
Type type = boundSignature.getArgumentType(0);
checkCondition(canCastToJson(type), INVALID_CAST_ARGUMENT, "Cannot cast %s to JSON", type);
List<Type> fieldTypes = type.getTypeParameters();
List<JsonGeneratorWriter> fieldWriters = new ArrayList<>(fieldTypes.size());
MethodHandle methodHandle;
if (legacyRowToJson) {
for (Type fieldType : fieldTypes) {
fieldWriters.add(createJsonGeneratorWriter(fieldType, true));
}
methodHandle = LEGACY_METHOD_HANDLE.bindTo(fieldWriters);
} else {
List<TypeSignatureParameter> typeSignatureParameters = type.getTypeSignature().getParameters();
List<String> fieldNames = new ArrayList<>(fieldTypes.size());
for (int i = 0; i < fieldTypes.size(); i++) {
fieldNames.add(typeSignatureParameters.get(i).getNamedTypeSignature().getName().orElse(""));
fieldWriters.add(createJsonGeneratorWriter(fieldTypes.get(i), false));
}
methodHandle = METHOD_HANDLE.bindTo(fieldNames).bindTo(fieldWriters);
}
return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, ImmutableList.of(NEVER_NULL), methodHandle);
}
use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.
the class OrcTester method rowType.
private static Type rowType(Type... fieldTypes) {
ImmutableList.Builder<TypeSignatureParameter> typeSignatureParameters = ImmutableList.builder();
for (int i = 0; i < fieldTypes.length; i++) {
String filedName = "field_" + i;
Type fieldType = fieldTypes[i];
typeSignatureParameters.add(TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(filedName)), fieldType.getTypeSignature())));
}
return TESTING_TYPE_MANAGER.getParameterizedType(StandardTypes.ROW, typeSignatureParameters.build());
}
use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.
the class SignatureBinder method extractBoundVariables.
private static void extractBoundVariables(BoundSignature boundSignature, Signature declaredSignature, Map<String, TypeVariableConstraint> typeVariableConstraints, BoundVariables bindings, Type actualType, TypeSignature declaredTypeSignature) {
// type without nested type parameters
if (declaredTypeSignature.getParameters().isEmpty()) {
String typeVariable = declaredTypeSignature.getBase();
TypeVariableConstraint typeVariableConstraint = typeVariableConstraints.get(typeVariable);
if (typeVariableConstraint == null) {
return;
}
if (bindings.containsTypeVariable(typeVariable)) {
Type existingTypeBinding = bindings.getTypeVariable(typeVariable);
verifyBoundSignature(actualType.equals(existingTypeBinding), boundSignature, declaredSignature);
} else {
bindings.setTypeVariable(typeVariable, actualType);
}
return;
}
verifyBoundSignature(declaredTypeSignature.getBase().equalsIgnoreCase(actualType.getTypeSignature().getBase()), boundSignature, declaredSignature);
// type with nested literal parameters
if (isTypeWithLiteralParameters(declaredTypeSignature)) {
for (int i = 0; i < declaredTypeSignature.getParameters().size(); i++) {
TypeSignatureParameter typeSignatureParameter = declaredTypeSignature.getParameters().get(i);
Long actualLongBinding = actualType.getTypeSignature().getParameters().get(i).getLongLiteral();
if (typeSignatureParameter.getKind() == ParameterKind.VARIABLE) {
if (bindings.containsLongVariable(typeSignatureParameter.getVariable())) {
Long existingLongBinding = bindings.getLongVariable(typeSignatureParameter.getVariable());
verifyBoundSignature(actualLongBinding.equals(existingLongBinding), boundSignature, declaredSignature);
} else {
bindings.setLongVariable(typeSignatureParameter.getVariable(), actualLongBinding);
}
} else {
verify(typeSignatureParameter.getKind() == ParameterKind.LONG);
verifyBoundSignature(actualLongBinding.equals(typeSignatureParameter.getLongLiteral()), boundSignature, declaredSignature);
}
}
return;
}
// type with nested type parameters
List<Type> actualTypeParameters = actualType.getTypeParameters();
// unknown types are assumed to have unknown nested types
if (UNKNOWN.equals(actualType)) {
actualTypeParameters = Collections.nCopies(declaredTypeSignature.getParameters().size(), UNKNOWN);
}
verifyBoundSignature(declaredTypeSignature.getParameters().size() == actualTypeParameters.size(), boundSignature, declaredSignature);
for (int i = 0; i < declaredTypeSignature.getParameters().size(); i++) {
TypeSignatureParameter typeSignatureParameter = declaredTypeSignature.getParameters().get(i);
TypeSignature typeSignature = typeSignatureParameter.getTypeSignatureOrNamedTypeSignature().orElseThrow(() -> new UnsupportedOperationException("Types with both type parameters and literal parameters at the same time are not supported"));
Type actualTypeParameter = actualTypeParameters.get(i);
extractBoundVariables(boundSignature, declaredSignature, typeVariableConstraints, bindings, actualTypeParameter, typeSignature);
}
}
Aggregations