Search in sources :

Example 36 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class TestArraySqlFunctions method testArrayFrequencyBigint.

@Test
public void testArrayFrequencyBigint() {
    FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
    MapType type = new MapType(BIGINT, INTEGER, methodHandle(TestRowType.class, "throwUnsupportedOperation"), methodHandle(TestRowType.class, "throwUnsupportedOperation"));
    TypeSignature typeSignature = TypeSignature.parseTypeSignature(type.getDisplayName());
    assertFunction("array_frequency(cast(null as array(bigint)))", functionAndTypeManager.getType(typeSignature), null);
    assertFunction("array_frequency(cast(array[] as array(bigint)))", functionAndTypeManager.getType(typeSignature), ImmutableMap.of());
    assertFunction("array_frequency(array[cast(null as bigint), cast(null as bigint), cast(null as bigint)])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of());
    assertFunction("array_frequency(array[cast(null as bigint), bigint '1'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(1L, 1));
    assertFunction("array_frequency(array[cast(null as bigint), bigint '1', bigint '3', cast(null as bigint), bigint '1', bigint '3', cast(null as bigint)])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(1L, 2, 3L, 2));
    assertFunction("array_frequency(array[bigint '1', bigint '1', bigint '2', bigint '2', bigint '3', bigint '1', bigint '3', bigint '2'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(1L, 3, 2L, 3, 3L, 2));
    assertFunction("array_frequency(array[bigint '45'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(45L, 1));
    assertFunction("array_frequency(array[bigint '-45'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(-45L, 1));
    assertFunction("array_frequency(array[bigint '1', bigint '3', bigint '1', bigint '3'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(1L, 2, 3L, 2));
    assertFunction("array_frequency(array[bigint '3', bigint '1', bigint '3',bigint '1'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(1L, 2, 3L, 2));
    assertFunction("array_frequency(array[bigint '4',bigint '3',bigint '3',bigint '2',bigint '2',bigint '2',bigint '1',bigint '1',bigint '1',bigint '1'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(1L, 4, 2L, 3, 3L, 2, 4L, 1));
    assertFunction("array_frequency(array[bigint '3', bigint '3', bigint '2', bigint '2', bigint '5', bigint '5', bigint '1', bigint '1'])", functionAndTypeManager.getType(typeSignature), ImmutableMap.of(1L, 2, 2L, 2, 3L, 2, 5L, 2));
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) TestRowType(com.facebook.presto.common.type.TestRowType) MapType(com.facebook.presto.common.type.MapType) Test(org.testng.annotations.Test)

Example 37 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class TypeConverter method toPrestoType.

public static Type toPrestoType(org.apache.iceberg.types.Type type, TypeManager typeManager) {
    switch(type.typeId()) {
        case BOOLEAN:
            return BooleanType.BOOLEAN;
        case BINARY:
        case FIXED:
            return VarbinaryType.VARBINARY;
        case DATE:
            return DateType.DATE;
        case DECIMAL:
            Types.DecimalType decimalType = (Types.DecimalType) type;
            return DecimalType.createDecimalType(decimalType.precision(), decimalType.scale());
        case DOUBLE:
            return DoubleType.DOUBLE;
        case LONG:
            return BigintType.BIGINT;
        case FLOAT:
            return RealType.REAL;
        case INTEGER:
            return IntegerType.INTEGER;
        case TIME:
            return TimeType.TIME;
        case TIMESTAMP:
            return TimestampType.TIMESTAMP;
        case STRING:
            return VarcharType.createUnboundedVarcharType();
        case LIST:
            Types.ListType listType = (Types.ListType) type;
            return new ArrayType(toPrestoType(listType.elementType(), typeManager));
        case MAP:
            Types.MapType mapType = (Types.MapType) type;
            TypeSignature keyType = toPrestoType(mapType.keyType(), typeManager).getTypeSignature();
            TypeSignature valueType = toPrestoType(mapType.valueType(), typeManager).getTypeSignature();
            return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType), TypeSignatureParameter.of(valueType)));
        case STRUCT:
            List<Types.NestedField> fields = ((Types.StructType) type).fields();
            return RowType.from(fields.stream().map(field -> new RowType.Field(Optional.of(field.name()), toPrestoType(field.type(), typeManager))).collect(toImmutableList()));
        default:
            throw new UnsupportedOperationException(format("Cannot convert from Iceberg type '%s' (%s) to Presto type", type, type.typeId()));
    }
}
Also used : Types(org.apache.iceberg.types.Types) StandardTypes(com.facebook.presto.common.type.StandardTypes) MetastoreUtil.isRowType(com.facebook.presto.hive.metastore.MetastoreUtil.isRowType) RowType(com.facebook.presto.common.type.RowType) MapType(com.facebook.presto.common.type.MapType) MetastoreUtil.isMapType(com.facebook.presto.hive.metastore.MetastoreUtil.isMapType) ArrayType(com.facebook.presto.common.type.ArrayType) MetastoreUtil.isArrayType(com.facebook.presto.hive.metastore.MetastoreUtil.isArrayType) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) DecimalType(com.facebook.presto.common.type.DecimalType)

Example 38 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class SignatureBinder method applyBoundVariables.

public static Signature applyBoundVariables(Signature signature, BoundVariables boundVariables, int arity) {
    List<TypeSignature> argumentSignatures;
    if (signature.isVariableArity()) {
        argumentSignatures = expandVarargFormalTypeSignature(signature.getArgumentTypes(), arity);
    } else {
        checkArgument(signature.getArgumentTypes().size() == arity);
        argumentSignatures = signature.getArgumentTypes();
    }
    List<TypeSignature> boundArgumentSignatures = applyBoundVariables(argumentSignatures, boundVariables);
    TypeSignature boundReturnTypeSignature = applyBoundVariables(signature.getReturnType(), boundVariables);
    return new Signature(signature.getName(), signature.getKind(), ImmutableList.of(), ImmutableList.of(), boundReturnTypeSignature, boundArgumentSignatures, false);
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) Signature(com.facebook.presto.spi.function.Signature)

Example 39 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class SignatureBinder method expandVarargFormalTypeSignature.

private static List<TypeSignature> expandVarargFormalTypeSignature(List<TypeSignature> formalTypeSignatures, int actualArity) {
    int variableArityArgumentsCount = actualArity - formalTypeSignatures.size() + 1;
    if (variableArityArgumentsCount == 0) {
        return formalTypeSignatures.subList(0, formalTypeSignatures.size() - 1);
    }
    if (variableArityArgumentsCount == 1) {
        return formalTypeSignatures;
    }
    checkArgument(variableArityArgumentsCount > 1 && !formalTypeSignatures.isEmpty());
    ImmutableList.Builder<TypeSignature> builder = ImmutableList.builder();
    builder.addAll(formalTypeSignatures);
    TypeSignature lastTypeSignature = formalTypeSignatures.get(formalTypeSignatures.size() - 1);
    for (int i = 1; i < variableArityArgumentsCount; i++) {
        builder.add(lastTypeSignature);
    }
    return builder.build();
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) LongVariableConstraint(com.facebook.presto.spi.function.LongVariableConstraint) TypeVariableConstraint(com.facebook.presto.spi.function.TypeVariableConstraint)

Example 40 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class ConcatFunction method generateConcat.

private static Class<?> generateConcat(TypeSignature type, int arity) {
    checkCondition(arity <= 254, NOT_SUPPORTED, "Too many arguments for string concatenation");
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(type.getBase() + "_concat" + arity + "ScalarFunction"), type(Object.class));
    // Generate constructor
    definition.declareDefaultConstructor(a(PRIVATE));
    // Generate concat()
    List<Parameter> parameters = IntStream.range(0, arity).mapToObj(i -> arg("arg" + i, Slice.class)).collect(toImmutableList());
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "concat", type(Slice.class), parameters);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable result = scope.declareVariable(Slice.class, "result");
    body.append(invokeStatic(ConcatFunction.class, "concat", Slice.class, BytecodeExpressions.newArray(ParameterizedType.type(Slice[].class), parameters))).retObject();
    return defineClass(definition, Object.class, ImmutableMap.of(), new DynamicClassLoader(ConcatFunction.class.getClassLoader()));
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) IntStream(java.util.stream.IntStream) MethodHandle(java.lang.invoke.MethodHandle) FunctionKind(com.facebook.presto.spi.function.FunctionKind) Slice(io.airlift.slice.Slice) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) BytecodeExpressions(com.facebook.presto.bytecode.expression.BytecodeExpressions) PRIVATE(com.facebook.presto.bytecode.Access.PRIVATE) PrestoException(com.facebook.presto.spi.PrestoException) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) TypeSignature(com.facebook.presto.common.type.TypeSignature) Access.a(com.facebook.presto.bytecode.Access.a) DEFAULT_NAMESPACE(com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE) ImmutableList(com.google.common.collect.ImmutableList) ParameterizedType.type(com.facebook.presto.bytecode.ParameterizedType.type) Reflection.methodHandle(com.facebook.presto.util.Reflection.methodHandle) Slices(io.airlift.slice.Slices) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) INVALID_FUNCTION_ARGUMENT(com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) CompilerUtils.makeClassName(com.facebook.presto.util.CompilerUtils.makeClassName) RETURN_NULL_ON_NULL(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.RETURN_NULL_ON_NULL) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) Failures.checkCondition(com.facebook.presto.util.Failures.checkCondition) PUBLIC(com.facebook.presto.bytecode.Access.PUBLIC) Variable(com.facebook.presto.bytecode.Variable) CompilerUtils.defineClass(com.facebook.presto.util.CompilerUtils.defineClass) BoundVariables(com.facebook.presto.metadata.BoundVariables) Parameter(com.facebook.presto.bytecode.Parameter) ImmutableMap(com.google.common.collect.ImmutableMap) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Collections.nCopies(java.util.Collections.nCopies) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) VARBINARY(com.facebook.presto.common.type.VarbinaryType.VARBINARY) DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) SqlFunctionVisibility(com.facebook.presto.spi.function.SqlFunctionVisibility) ArgumentProperty.valueTypeArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty.valueTypeArgumentProperty) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) List(java.util.List) FINAL(com.facebook.presto.bytecode.Access.FINAL) STATIC(com.facebook.presto.bytecode.Access.STATIC) Scope(com.facebook.presto.bytecode.Scope) NOT_SUPPORTED(com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED) Signature(com.facebook.presto.spi.function.Signature) BytecodeExpressions.invokeStatic(com.facebook.presto.bytecode.expression.BytecodeExpressions.invokeStatic) Parameter.arg(com.facebook.presto.bytecode.Parameter.arg) ParameterizedType(com.facebook.presto.bytecode.ParameterizedType) DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) Variable(com.facebook.presto.bytecode.Variable) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Slice(io.airlift.slice.Slice) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Parameter(com.facebook.presto.bytecode.Parameter) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition)

Aggregations

TypeSignature (com.facebook.presto.common.type.TypeSignature)60 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)25 Signature (com.facebook.presto.spi.function.Signature)20 NamedTypeSignature (com.facebook.presto.common.type.NamedTypeSignature)19 ImmutableList (com.google.common.collect.ImmutableList)19 List (java.util.List)17 PrestoException (com.facebook.presto.spi.PrestoException)15 Type (com.facebook.presto.common.type.Type)14 Test (org.testng.annotations.Test)14 TypeSignatureParameter (com.facebook.presto.common.type.TypeSignatureParameter)13 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)13 Objects.requireNonNull (java.util.Objects.requireNonNull)12 RowFieldName (com.facebook.presto.common.type.RowFieldName)10 StandardTypes (com.facebook.presto.common.type.StandardTypes)9 Optional (java.util.Optional)9 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)8 DecimalType (com.facebook.presto.common.type.DecimalType)8 ImmutableSet (com.google.common.collect.ImmutableSet)8 RowType (com.facebook.presto.common.type.RowType)7 ArrayType (com.facebook.presto.common.type.ArrayType)6