use of com.facebook.presto.common.type.TypeParameter in project presto by prestodb.
the class BuiltInTypeAndFunctionNamespaceManager method instantiateParametricType.
private Type instantiateParametricType(TypeSignature signature) {
List<TypeParameter> parameters = new ArrayList<>();
for (TypeSignatureParameter parameter : signature.getParameters()) {
TypeParameter typeParameter = TypeParameter.of(parameter, functionAndTypeManager);
parameters.add(typeParameter);
}
ParametricType parametricType = parametricTypes.get(signature.getBase().toLowerCase(Locale.ENGLISH));
if (parametricType == null) {
throw new IllegalArgumentException("Unknown type " + signature);
}
if (parametricType instanceof MapParametricType) {
return ((MapParametricType) parametricType).createType(functionAndTypeManager, parameters);
}
Type instantiatedType = parametricType.createType(parameters);
// checkState(instantiatedType.equalsSignature(signature), "Instantiated parametric type name (%s) does not match expected name (%s)", instantiatedType, signature);
return instantiatedType;
}
use of com.facebook.presto.common.type.TypeParameter in project presto by prestodb.
the class MapParametricType method createType.
public Type createType(FunctionAndTypeManager functionAndTypeManager, List<TypeParameter> parameters) {
checkArgument(parameters.size() == 2, "Expected two parameters, got %s", parameters);
TypeParameter firstParameter = parameters.get(0);
TypeParameter secondParameter = parameters.get(1);
checkArgument(firstParameter.getKind() == ParameterKind.TYPE && secondParameter.getKind() == ParameterKind.TYPE, "Expected key and type to be types, got %s", parameters);
Type keyType = firstParameter.getType();
Type valueType = secondParameter.getType();
MethodHandle keyNativeEquals = functionAndTypeManager.getJavaScalarFunctionImplementation(functionAndTypeManager.resolveOperator(OperatorType.EQUAL, fromTypes(keyType, keyType))).getMethodHandle();
MethodHandle keyBlockEquals = compose(keyNativeEquals, nativeValueGetter(keyType), nativeValueGetter(keyType));
MethodHandle keyNativeHashCode = functionAndTypeManager.getJavaScalarFunctionImplementation(functionAndTypeManager.resolveOperator(OperatorType.HASH_CODE, fromTypes(keyType))).getMethodHandle();
MethodHandle keyBlockHashCode = compose(keyNativeHashCode, nativeValueGetter(keyType));
return new MapType(keyType, valueType, keyBlockEquals, keyBlockHashCode);
}
use of com.facebook.presto.common.type.TypeParameter in project presto by prestodb.
the class RowParametricType method createType.
@Override
public Type createType(List<TypeParameter> parameters) {
checkArgument(!parameters.isEmpty(), "Row type must have at least one parameter");
checkArgument(parameters.stream().allMatch(parameter -> parameter.getKind() == ParameterKind.NAMED_TYPE), "Expected only named types as a parameters, got %s", parameters);
List<TypeSignatureParameter> typeSignatureParameters = parameters.stream().map(TypeParameter::getNamedType).map(parameter -> TypeSignatureParameter.of(new NamedTypeSignature(parameter.getName(), parameter.getType().getTypeSignature()))).collect(toList());
List<RowType.Field> fields = parameters.stream().map(TypeParameter::getNamedType).map(parameter -> new RowType.Field(parameter.getName().map(RowFieldName::getName), parameter.getType())).collect(toList());
return RowType.createWithTypeSignature(new TypeSignature(StandardTypes.ROW, typeSignatureParameters), fields);
}
use of com.facebook.presto.common.type.TypeParameter in project presto by prestodb.
the class TestRowParametricType method testTypeSignatureRoundTrip.
@Test
public void testTypeSignatureRoundTrip() {
FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
TypeSignature typeSignature = new TypeSignature(ROW, TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName("col1", false)), new TypeSignature(BIGINT))), TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName("col2", true)), new TypeSignature(DOUBLE))));
List<TypeParameter> parameters = typeSignature.getParameters().stream().map(parameter -> TypeParameter.of(parameter, functionAndTypeManager)).collect(Collectors.toList());
Type rowType = RowParametricType.ROW.createType(parameters);
assertEquals(rowType.getTypeSignature(), typeSignature);
}
use of com.facebook.presto.common.type.TypeParameter in project presto by prestodb.
the class VarcharParametricType method createType.
@Override
public Type createType(List<TypeParameter> parameters) {
if (parameters.isEmpty()) {
return createUnboundedVarcharType();
}
if (parameters.size() != 1) {
throw new IllegalArgumentException("Expected exactly one parameter for VARCHAR");
}
TypeParameter parameter = parameters.get(0);
if (!parameter.isLongLiteral()) {
throw new IllegalArgumentException("VARCHAR length must be a number");
}
long length = parameter.getLongLiteral();
if (length == VarcharType.UNBOUNDED_LENGTH) {
return VarcharType.createUnboundedVarcharType();
}
if (length < 0 || length > VarcharType.MAX_LENGTH) {
throw new IllegalArgumentException("Invalid VARCHAR length " + length);
}
return VarcharType.createVarcharType((int) length);
}
Aggregations