Search in sources :

Example 1 with TypeParameter

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;
}
Also used : TypeParameter(com.facebook.presto.common.type.TypeParameter) DecimalParametricType(com.facebook.presto.type.DecimalParametricType) UserDefinedType(com.facebook.presto.common.type.UserDefinedType) OperatorType.tryGetOperatorType(com.facebook.presto.common.function.OperatorType.tryGetOperatorType) VarcharParametricType(com.facebook.presto.type.VarcharParametricType) ParametricType(com.facebook.presto.common.type.ParametricType) CharParametricType(com.facebook.presto.type.CharParametricType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) MapParametricType(com.facebook.presto.type.MapParametricType) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ArrayList(java.util.ArrayList) MapParametricType(com.facebook.presto.type.MapParametricType) DecimalParametricType(com.facebook.presto.type.DecimalParametricType) VarcharParametricType(com.facebook.presto.type.VarcharParametricType) ParametricType(com.facebook.presto.common.type.ParametricType) CharParametricType(com.facebook.presto.type.CharParametricType) MapParametricType(com.facebook.presto.type.MapParametricType)

Example 2 with TypeParameter

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);
}
Also used : TypeParameter(com.facebook.presto.common.type.TypeParameter) MapType(com.facebook.presto.common.type.MapType) OperatorType(com.facebook.presto.common.function.OperatorType) ParametricType(com.facebook.presto.common.type.ParametricType) Type(com.facebook.presto.common.type.Type) MapType(com.facebook.presto.common.type.MapType) MethodHandle(java.lang.invoke.MethodHandle)

Example 3 with TypeParameter

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);
}
Also used : StandardTypes(com.facebook.presto.common.type.StandardTypes) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) TypeSignature(com.facebook.presto.common.type.TypeSignature) ParametricType(com.facebook.presto.common.type.ParametricType) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Collectors.toList(java.util.stream.Collectors.toList) ParameterKind(com.facebook.presto.common.type.ParameterKind) RowFieldName(com.facebook.presto.common.type.RowFieldName) TypeParameter(com.facebook.presto.common.type.TypeParameter) Type(com.facebook.presto.common.type.Type) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) RowType(com.facebook.presto.common.type.RowType) TypeParameter(com.facebook.presto.common.type.TypeParameter) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature)

Example 4 with TypeParameter

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);
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) Assert.assertEquals(com.facebook.presto.testing.assertions.Assert.assertEquals) Test(org.testng.annotations.Test) ROW(com.facebook.presto.common.type.StandardTypes.ROW) Collectors(java.util.stream.Collectors) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) TypeSignature(com.facebook.presto.common.type.TypeSignature) DOUBLE(com.facebook.presto.common.type.StandardTypes.DOUBLE) List(java.util.List) Optional(java.util.Optional) RowFieldName(com.facebook.presto.common.type.RowFieldName) TypeParameter(com.facebook.presto.common.type.TypeParameter) BIGINT(com.facebook.presto.common.type.StandardTypes.BIGINT) Type(com.facebook.presto.common.type.Type) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeParameter(com.facebook.presto.common.type.TypeParameter) Type(com.facebook.presto.common.type.Type) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) Test(org.testng.annotations.Test)

Example 5 with TypeParameter

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);
}
Also used : TypeParameter(com.facebook.presto.common.type.TypeParameter)

Aggregations

TypeParameter (com.facebook.presto.common.type.TypeParameter)5 Type (com.facebook.presto.common.type.Type)4 ParametricType (com.facebook.presto.common.type.ParametricType)3 TypeSignatureParameter (com.facebook.presto.common.type.TypeSignatureParameter)3 OperatorType (com.facebook.presto.common.function.OperatorType)2 NamedTypeSignature (com.facebook.presto.common.type.NamedTypeSignature)2 RowFieldName (com.facebook.presto.common.type.RowFieldName)2 TypeSignature (com.facebook.presto.common.type.TypeSignature)2 List (java.util.List)2 OperatorType.tryGetOperatorType (com.facebook.presto.common.function.OperatorType.tryGetOperatorType)1 MapType (com.facebook.presto.common.type.MapType)1 ParameterKind (com.facebook.presto.common.type.ParameterKind)1 RowType (com.facebook.presto.common.type.RowType)1 StandardTypes (com.facebook.presto.common.type.StandardTypes)1 BIGINT (com.facebook.presto.common.type.StandardTypes.BIGINT)1 DOUBLE (com.facebook.presto.common.type.StandardTypes.DOUBLE)1 ROW (com.facebook.presto.common.type.StandardTypes.ROW)1 UserDefinedType (com.facebook.presto.common.type.UserDefinedType)1 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)1 FunctionAndTypeManager.createTestFunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager)1