Search in sources :

Example 1 with TypeSignatureProvider

use of com.facebook.presto.sql.analyzer.TypeSignatureProvider in project presto by prestodb.

the class FunctionRegistry method doGetSpecializedFunctionKey.

private SpecializedFunctionKey doGetSpecializedFunctionKey(Signature signature) {
    Iterable<SqlFunction> candidates = functions.get(QualifiedName.of(signature.getName()));
    // search for exact match
    Type returnType = typeManager.getType(signature.getReturnType());
    List<TypeSignatureProvider> argumentTypeSignatureProviders = fromTypeSignatures(signature.getArgumentTypes());
    for (SqlFunction candidate : candidates) {
        Optional<BoundVariables> boundVariables = new SignatureBinder(typeManager, candidate.getSignature(), false).bindVariables(argumentTypeSignatureProviders, returnType);
        if (boundVariables.isPresent()) {
            return new SpecializedFunctionKey(candidate, boundVariables.get(), argumentTypeSignatureProviders.size());
        }
    }
    // TODO: hack because there could be "type only" coercions (which aren't necessarily included as implicit casts),
    // so do a second pass allowing "type only" coercions
    List<Type> argumentTypes = resolveTypes(signature.getArgumentTypes(), typeManager);
    for (SqlFunction candidate : candidates) {
        SignatureBinder binder = new SignatureBinder(typeManager, candidate.getSignature(), true);
        Optional<BoundVariables> boundVariables = binder.bindVariables(argumentTypeSignatureProviders, returnType);
        if (!boundVariables.isPresent()) {
            continue;
        }
        Signature boundSignature = applyBoundVariables(candidate.getSignature(), boundVariables.get(), argumentTypes.size());
        if (!typeManager.isTypeOnlyCoercion(typeManager.getType(boundSignature.getReturnType()), returnType)) {
            continue;
        }
        boolean nonTypeOnlyCoercion = false;
        for (int i = 0; i < argumentTypes.size(); i++) {
            Type expectedType = typeManager.getType(boundSignature.getArgumentTypes().get(i));
            if (!typeManager.isTypeOnlyCoercion(argumentTypes.get(i), expectedType)) {
                nonTypeOnlyCoercion = true;
                break;
            }
        }
        if (nonTypeOnlyCoercion) {
            continue;
        }
        return new SpecializedFunctionKey(candidate, boundVariables.get(), argumentTypes.size());
    }
    // TODO: this is a hack and should be removed
    if (signature.getName().startsWith(MAGIC_LITERAL_FUNCTION_PREFIX)) {
        List<TypeSignature> parameterTypes = signature.getArgumentTypes();
        // extract type from function name
        String typeName = signature.getName().substring(MAGIC_LITERAL_FUNCTION_PREFIX.length());
        // lookup the type
        Type type = typeManager.getType(parseTypeSignature(typeName));
        requireNonNull(type, format("Type %s not registered", typeName));
        // verify we have one parameter of the proper type
        checkArgument(parameterTypes.size() == 1, "Expected one argument to literal function, but got %s", parameterTypes);
        Type parameterType = typeManager.getType(parameterTypes.get(0));
        requireNonNull(parameterType, format("Type %s not found", parameterTypes.get(0)));
        return new SpecializedFunctionKey(magicLiteralFunction, BoundVariables.builder().setTypeVariable("T", parameterType).setTypeVariable("R", type).build(), 1);
    }
    throw new PrestoException(FUNCTION_IMPLEMENTATION_MISSING, format("%s not found", signature));
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) OperatorType(com.facebook.presto.spi.function.OperatorType) Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.spi.type.TypeSignature) SignatureBinder.applyBoundVariables(com.facebook.presto.metadata.SignatureBinder.applyBoundVariables) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.spi.type.TypeSignature)

Example 2 with TypeSignatureProvider

use of com.facebook.presto.sql.analyzer.TypeSignatureProvider in project presto by prestodb.

the class FunctionRegistry method resolveFunction.

public Signature resolveFunction(QualifiedName name, List<TypeSignatureProvider> parameterTypes) {
    Collection<SqlFunction> allCandidates = functions.get(name);
    List<SqlFunction> exactCandidates = allCandidates.stream().filter(function -> function.getSignature().getTypeVariableConstraints().isEmpty()).collect(Collectors.toList());
    Optional<Signature> match = matchFunctionExact(exactCandidates, parameterTypes);
    if (match.isPresent()) {
        return match.get();
    }
    List<SqlFunction> genericCandidates = allCandidates.stream().filter(function -> !function.getSignature().getTypeVariableConstraints().isEmpty()).collect(Collectors.toList());
    match = matchFunctionExact(genericCandidates, parameterTypes);
    if (match.isPresent()) {
        return match.get();
    }
    match = matchFunctionWithCoercion(allCandidates, parameterTypes);
    if (match.isPresent()) {
        return match.get();
    }
    List<String> expectedParameters = new ArrayList<>();
    for (SqlFunction function : allCandidates) {
        expectedParameters.add(format("%s(%s) %s", name, Joiner.on(", ").join(function.getSignature().getArgumentTypes()), Joiner.on(", ").join(function.getSignature().getTypeVariableConstraints())));
    }
    String parameters = Joiner.on(", ").join(parameterTypes);
    String message = format("Function %s not registered", name);
    if (!expectedParameters.isEmpty()) {
        String expected = Joiner.on(", ").join(expectedParameters);
        message = format("Unexpected parameters (%s) for function %s. Expected: %s", parameters, name, expected);
    }
    if (name.getSuffix().startsWith(MAGIC_LITERAL_FUNCTION_PREFIX)) {
        // extract type from function name
        String typeName = name.getSuffix().substring(MAGIC_LITERAL_FUNCTION_PREFIX.length());
        // lookup the type
        Type type = typeManager.getType(parseTypeSignature(typeName));
        requireNonNull(type, format("Type %s not registered", typeName));
        // verify we have one parameter of the proper type
        checkArgument(parameterTypes.size() == 1, "Expected one argument to literal function, but got %s", parameterTypes);
        Type parameterType = typeManager.getType(parameterTypes.get(0).getTypeSignature());
        requireNonNull(parameterType, format("Type %s not found", parameterTypes.get(0)));
        return getMagicLiteralFunctionSignature(type);
    }
    throw new PrestoException(FUNCTION_NOT_FOUND, message);
}
Also used : INTEGER_TO_DECIMAL_CAST(com.facebook.presto.type.DecimalCasts.INTEGER_TO_DECIMAL_CAST) ApproximateDoublePercentileArrayAggregations(com.facebook.presto.operator.aggregation.ApproximateDoublePercentileArrayAggregations) DECIMAL_TO_SMALLINT_SATURATED_FLOOR_CAST(com.facebook.presto.type.DecimalSaturatedFloorCasts.DECIMAL_TO_SMALLINT_SATURATED_FLOOR_CAST) MapEqualOperator(com.facebook.presto.operator.scalar.MapEqualOperator) DECIMAL_SUBTRACT_OPERATOR(com.facebook.presto.type.DecimalOperators.DECIMAL_SUBTRACT_OPERATOR) TimeOperators(com.facebook.presto.type.TimeOperators) JsonFunctions(com.facebook.presto.operator.scalar.JsonFunctions) NTileFunction(com.facebook.presto.operator.window.NTileFunction) TimestampWithTimeZoneOperators(com.facebook.presto.type.TimestampWithTimeZoneOperators) ArrayContains(com.facebook.presto.operator.scalar.ArrayContains) DECIMAL_LESS_THAN_OR_EQUAL_OPERATOR(com.facebook.presto.type.DecimalInequalityOperators.DECIMAL_LESS_THAN_OR_EQUAL_OPERATOR) BOOLEAN(com.facebook.presto.spi.type.BooleanType.BOOLEAN) AverageAggregations(com.facebook.presto.operator.aggregation.AverageAggregations) Map(java.util.Map) MathFunctions(com.facebook.presto.operator.scalar.MathFunctions) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) MAP_FILTER_FUNCTION(com.facebook.presto.operator.scalar.MapFilterFunction.MAP_FILTER_FUNCTION) CONCAT(com.facebook.presto.operator.scalar.ConcatFunction.CONCAT) SignatureBinder.applyBoundVariables(com.facebook.presto.metadata.SignatureBinder.applyBoundVariables) HyperLogLogOperators(com.facebook.presto.type.HyperLogLogOperators) WINDOW(com.facebook.presto.metadata.FunctionKind.WINDOW) InternalAggregationFunction(com.facebook.presto.operator.aggregation.InternalAggregationFunction) MAP_ELEMENT_AT(com.facebook.presto.operator.scalar.MapElementAtFunction.MAP_ELEMENT_AT) OperatorType(com.facebook.presto.spi.function.OperatorType) ArrayFunctions(com.facebook.presto.operator.scalar.ArrayFunctions) PercentRankFunction(com.facebook.presto.operator.window.PercentRankFunction) DECIMAL_MULTIPLY_OPERATOR(com.facebook.presto.type.DecimalOperators.DECIMAL_MULTIPLY_OPERATOR) Joiner(com.google.common.base.Joiner) MethodHandle(java.lang.invoke.MethodHandle) LagFunction(com.facebook.presto.operator.window.LagFunction) ApproximateCountDistinctAggregations(com.facebook.presto.operator.aggregation.ApproximateCountDistinctAggregations) ColorFunctions(com.facebook.presto.operator.scalar.ColorFunctions) DECIMAL_SUM_AGGREGATION(com.facebook.presto.operator.aggregation.DecimalSumAggregation.DECIMAL_SUM_AGGREGATION) VarbinaryOperators(com.facebook.presto.type.VarbinaryOperators) MAP_UNION(com.facebook.presto.operator.aggregation.MapUnionAggregation.MAP_UNION) ARRAY_TO_ARRAY_CAST(com.facebook.presto.operator.scalar.ArrayToArrayCast.ARRAY_TO_ARRAY_CAST) TimestampOperators(com.facebook.presto.type.TimestampOperators) ArrayPositionFunction(com.facebook.presto.operator.scalar.ArrayPositionFunction) CHECKSUM_AGGREGATION(com.facebook.presto.operator.aggregation.ChecksumAggregationFunction.CHECKSUM_AGGREGATION) DecimalOperators(com.facebook.presto.type.DecimalOperators) Type(com.facebook.presto.spi.type.Type) ARRAY_FLATTEN_FUNCTION(com.facebook.presto.operator.scalar.ArrayFlattenFunction.ARRAY_FLATTEN_FUNCTION) TRY_CAST(com.facebook.presto.operator.scalar.TryCastFunction.TRY_CAST) ArrayEqualOperator(com.facebook.presto.operator.scalar.ArrayEqualOperator) JoniRegexpFunctions(com.facebook.presto.operator.scalar.JoniRegexpFunctions) BitwiseAndAggregation(com.facebook.presto.operator.aggregation.BitwiseAndAggregation) ELEMENT_TO_ARRAY_CONCAT_FUNCTION(com.facebook.presto.operator.scalar.ElementToArrayConcatFunction.ELEMENT_TO_ARRAY_CONCAT_FUNCTION) AMBIGUOUS_FUNCTION_CALL(com.facebook.presto.spi.StandardErrorCode.AMBIGUOUS_FUNCTION_CALL) DECIMAL_GREATER_THAN_OPERATOR(com.facebook.presto.type.DecimalInequalityOperators.DECIMAL_GREATER_THAN_OPERATOR) ApproximateRealPercentileAggregations(com.facebook.presto.operator.aggregation.ApproximateRealPercentileAggregations) DECIMAL_TO_BIGINT_SATURATED_FLOOR_CAST(com.facebook.presto.type.DecimalSaturatedFloorCasts.DECIMAL_TO_BIGINT_SATURATED_FLOOR_CAST) BIGINT_TO_DECIMAL_CAST(com.facebook.presto.type.DecimalCasts.BIGINT_TO_DECIMAL_CAST) Throwables(com.google.common.base.Throwables) MAP_AGG(com.facebook.presto.operator.aggregation.MapAggregationFunction.MAP_AGG) GREATEST(com.facebook.presto.operator.scalar.Greatest.GREATEST) IDENTITY_CAST(com.facebook.presto.operator.scalar.IdentityCast.IDENTITY_CAST) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) ColorOperators(com.facebook.presto.type.ColorOperators) HISTOGRAM(com.facebook.presto.operator.aggregation.Histogram.HISTOGRAM) ArrayExceptFunction(com.facebook.presto.operator.scalar.ArrayExceptFunction) ARRAY_TO_ELEMENT_CONCAT_FUNCTION(com.facebook.presto.operator.scalar.ArrayToElementConcatFunction.ARRAY_TO_ELEMENT_CONCAT_FUNCTION) TINYINT_TO_DECIMAL_SATURATED_FLOOR_CAST(com.facebook.presto.type.DecimalSaturatedFloorCasts.TINYINT_TO_DECIMAL_SATURATED_FLOOR_CAST) DoubleCovarianceAggregation(com.facebook.presto.operator.aggregation.DoubleCovarianceAggregation) CountIfAggregation(com.facebook.presto.operator.aggregation.CountIfAggregation) JSON_TO_DECIMAL_CAST(com.facebook.presto.type.DecimalCasts.JSON_TO_DECIMAL_CAST) VarcharType(com.facebook.presto.spi.type.VarcharType) DECIMAL_DISTINCT_FROM_OPERATOR(com.facebook.presto.type.DecimalInequalityOperators.DECIMAL_DISTINCT_FROM_OPERATOR) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) DoubleRegressionAggregation(com.facebook.presto.operator.aggregation.DoubleRegressionAggregation) MAX_BY(com.facebook.presto.operator.aggregation.MaxBy.MAX_BY) DoubleSumAggregation(com.facebook.presto.operator.aggregation.DoubleSumAggregation) DECIMAL_NOT_EQUAL_OPERATOR(com.facebook.presto.type.DecimalInequalityOperators.DECIMAL_NOT_EQUAL_OPERATOR) IntegerOperators(com.facebook.presto.type.IntegerOperators) ArrayReverseFunction(com.facebook.presto.operator.scalar.ArrayReverseFunction) LastValueFunction(com.facebook.presto.operator.window.LastValueFunction) Block(com.facebook.presto.spi.block.Block) MAP_HASH_CODE(com.facebook.presto.operator.scalar.MapHashCodeOperator.MAP_HASH_CODE) RealGeometricMeanAggregations(com.facebook.presto.operator.aggregation.RealGeometricMeanAggregations) FUNCTION_NOT_FOUND(com.facebook.presto.spi.StandardErrorCode.FUNCTION_NOT_FOUND) ARBITRARY_AGGREGATION(com.facebook.presto.operator.aggregation.ArbitraryAggregationFunction.ARBITRARY_AGGREGATION) JSON_TO_ARRAY(com.facebook.presto.operator.scalar.JsonToArrayCast.JSON_TO_ARRAY) ARRAY_CONCAT_FUNCTION(com.facebook.presto.operator.scalar.ArrayConcatFunction.ARRAY_CONCAT_FUNCTION) Re2JCastToRegexpFunction.castCharToRe2JRegexp(com.facebook.presto.operator.scalar.Re2JCastToRegexpFunction.castCharToRe2JRegexp) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) BooleanAndAggregation(com.facebook.presto.operator.aggregation.BooleanAndAggregation) DateTimeOperators(com.facebook.presto.type.DateTimeOperators) MIN_BY(com.facebook.presto.operator.aggregation.MinBy.MIN_BY) ARRAY_TO_JSON(com.facebook.presto.operator.scalar.ArrayToJsonCast.ARRAY_TO_JSON) UNKNOWN(com.facebook.presto.type.UnknownType.UNKNOWN) RankFunction(com.facebook.presto.operator.window.RankFunction) MAP_TRANSFORM_VALUE_FUNCTION(com.facebook.presto.operator.scalar.MapTransformValueFunction.MAP_TRANSFORM_VALUE_FUNCTION) ArrayDistinctFunction(com.facebook.presto.operator.scalar.ArrayDistinctFunction) DECIMAL_MOD_FUNCTION(com.facebook.presto.operator.scalar.MathFunctions.DECIMAL_MOD_FUNCTION) NthValueFunction(com.facebook.presto.operator.window.NthValueFunction) MAX_BY_N_AGGREGATION(com.facebook.presto.operator.aggregation.MaxByNAggregationFunction.MAX_BY_N_AGGREGATION) ImmutableSet(com.google.common.collect.ImmutableSet) DECIMAL_LESS_THAN_OPERATOR(com.facebook.presto.type.DecimalInequalityOperators.DECIMAL_LESS_THAN_OPERATOR) Collection(java.util.Collection) CountAggregation(com.facebook.presto.operator.aggregation.CountAggregation) Collectors(java.util.stream.Collectors) ARRAY_REDUCE_FUNCTION(com.facebook.presto.operator.scalar.ArrayReduceFunction.ARRAY_REDUCE_FUNCTION) CacheLoader(com.google.common.cache.CacheLoader) DECIMAL_TO_BIGINT_CAST(com.facebook.presto.type.DecimalCasts.DECIMAL_TO_BIGINT_CAST) Re2JCastToRegexpFunction.castVarcharToRe2JRegexp(com.facebook.presto.operator.scalar.Re2JCastToRegexpFunction.castVarcharToRe2JRegexp) DECIMAL_TO_DECIMAL_CAST(com.facebook.presto.type.DecimalToDecimalCasts.DECIMAL_TO_DECIMAL_CAST) WindowFunctionSupplier(com.facebook.presto.operator.window.WindowFunctionSupplier) AggregateWindowFunction.supplier(com.facebook.presto.operator.window.AggregateWindowFunction.supplier) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) DECIMAL_ADD_OPERATOR(com.facebook.presto.type.DecimalOperators.DECIMAL_ADD_OPERATOR) DoubleHistogramAggregation(com.facebook.presto.operator.aggregation.DoubleHistogramAggregation) ROW_HASH_CODE(com.facebook.presto.operator.scalar.RowHashCodeOperator.ROW_HASH_CODE) JoniRegexpCasts(com.facebook.presto.operator.scalar.JoniRegexpCasts) UnknownOperators(com.facebook.presto.type.UnknownOperators) ArrayDistinctFromOperator(com.facebook.presto.operator.scalar.ArrayDistinctFromOperator) BooleanOperators(com.facebook.presto.type.BooleanOperators) BlockEncodingSerde(com.facebook.presto.spi.block.BlockEncodingSerde) Re2JRegexpFunctions(com.facebook.presto.operator.scalar.Re2JRegexpFunctions) ImmutableList(com.google.common.collect.ImmutableList) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) INTEGER_TO_DECIMAL_SATURATED_FLOOR_CAST(com.facebook.presto.type.DecimalSaturatedFloorCasts.INTEGER_TO_DECIMAL_SATURATED_FLOOR_CAST) ArrayLessThanOperator(com.facebook.presto.operator.scalar.ArrayLessThanOperator) JsonOperators(com.facebook.presto.operator.scalar.JsonOperators) VARBINARY(com.facebook.presto.spi.type.VarbinaryType.VARBINARY) ArrayGreaterThanOrEqualOperator(com.facebook.presto.operator.scalar.ArrayGreaterThanOrEqualOperator) MAX_N_AGGREGATION(com.facebook.presto.operator.aggregation.MaxNAggregationFunction.MAX_N_AGGREGATION) DECIMAL_TO_INTEGER_SATURATED_FLOOR_CAST(com.facebook.presto.type.DecimalSaturatedFloorCasts.DECIMAL_TO_INTEGER_SATURATED_FLOOR_CAST) RealCorrelationAggregation(com.facebook.presto.operator.aggregation.RealCorrelationAggregation) ArraySortFunction(com.facebook.presto.operator.scalar.ArraySortFunction) ArraySliceFunction(com.facebook.presto.operator.scalar.ArraySliceFunction) MAP_TO_JSON(com.facebook.presto.operator.scalar.MapToJsonCast.MAP_TO_JSON) HyperLogLogFunctions(com.facebook.presto.operator.scalar.HyperLogLogFunctions) RowNumberFunction(com.facebook.presto.operator.window.RowNumberFunction) JSON_TO_MAP(com.facebook.presto.operator.scalar.JsonToMapCast.JSON_TO_MAP) MAP_TRANSFORM_KEY_FUNCTION(com.facebook.presto.operator.scalar.MapTransformKeyFunction.MAP_TRANSFORM_KEY_FUNCTION) Ordering(com.google.common.collect.Ordering) ARRAY_SUBSCRIPT(com.facebook.presto.operator.scalar.ArraySubscriptOperator.ARRAY_SUBSCRIPT) BitwiseOrAggregation(com.facebook.presto.operator.aggregation.BitwiseOrAggregation) DECIMAL_TO_JSON_CAST(com.facebook.presto.type.DecimalCasts.DECIMAL_TO_JSON_CAST) EmptyMapConstructor(com.facebook.presto.operator.scalar.EmptyMapConstructor) ARRAY_JOIN(com.facebook.presto.operator.scalar.ArrayJoin.ARRAY_JOIN) DECIMAL_TO_VARCHAR_CAST(com.facebook.presto.type.DecimalCasts.DECIMAL_TO_VARCHAR_CAST) Arrays(java.util.Arrays) DECIMAL_TO_BOOLEAN_CAST(com.facebook.presto.type.DecimalCasts.DECIMAL_TO_BOOLEAN_CAST) LoadingCache(com.google.common.cache.LoadingCache) TypeManager(com.facebook.presto.spi.type.TypeManager) MapKeys(com.facebook.presto.operator.scalar.MapKeys) BIGINT_TO_DECIMAL_SATURATED_FLOOR_CAST(com.facebook.presto.type.DecimalSaturatedFloorCasts.BIGINT_TO_DECIMAL_SATURATED_FLOOR_CAST) AGGREGATE(com.facebook.presto.metadata.FunctionKind.AGGREGATE) REAL_TO_DECIMAL_SATURATED_FLOOR_CAST(com.facebook.presto.type.DecimalSaturatedFloorCasts.REAL_TO_DECIMAL_SATURATED_FLOOR_CAST) ROW_NOT_EQUAL(com.facebook.presto.operator.scalar.RowNotEqualOperator.ROW_NOT_EQUAL) BIGINT(com.facebook.presto.spi.type.BigintType.BIGINT) SmallintOperators(com.facebook.presto.type.SmallintOperators) ApproximateRealPercentileArrayAggregations(com.facebook.presto.operator.aggregation.ApproximateRealPercentileArrayAggregations) SqlWindowFunction(com.facebook.presto.operator.window.SqlWindowFunction) CharOperators(com.facebook.presto.type.CharOperators) MAP_CONSTRUCTOR(com.facebook.presto.operator.scalar.MapConstructor.MAP_CONSTRUCTOR) ArrayAggregationFunction(com.facebook.presto.operator.aggregation.ArrayAggregationFunction) MapCardinalityFunction(com.facebook.presto.operator.scalar.MapCardinalityFunction) LongSumAggregation(com.facebook.presto.operator.aggregation.LongSumAggregation) MapNotEqualOperator(com.facebook.presto.operator.scalar.MapNotEqualOperator) ROW_GREATER_THAN_OR_EQUAL(com.facebook.presto.operator.scalar.RowGreaterThanOrEqualOperator.ROW_GREATER_THAN_OR_EQUAL) LikeFunctions(com.facebook.presto.type.LikeFunctions) MethodHandles(java.lang.invoke.MethodHandles) TypeOfFunction(com.facebook.presto.operator.scalar.TypeOfFunction) Set(java.util.Set) RealSumAggregation(com.facebook.presto.operator.aggregation.RealSumAggregation) ThreadSafe(javax.annotation.concurrent.ThreadSafe) DECIMAL_TO_DOUBLE_CAST(com.facebook.presto.type.DecimalCasts.DECIMAL_TO_DOUBLE_CAST) ArrayGreaterThanOperator(com.facebook.presto.operator.scalar.ArrayGreaterThanOperator) FeaturesConfig(com.facebook.presto.sql.analyzer.FeaturesConfig) MIN_AGGREGATION(com.facebook.presto.operator.aggregation.MinAggregationFunction.MIN_AGGREGATION) LEAST(com.facebook.presto.operator.scalar.Least.LEAST) DECIMAL_TO_INTEGER_CAST(com.facebook.presto.type.DecimalCasts.DECIMAL_TO_INTEGER_CAST) ApproximateLongPercentileArrayAggregations(com.facebook.presto.operator.aggregation.ApproximateLongPercentileArrayAggregations) DECIMAL_TO_TINYINT_CAST(com.facebook.presto.type.DecimalCasts.DECIMAL_TO_TINYINT_CAST) MergeHyperLogLogAggregation(com.facebook.presto.operator.aggregation.MergeHyperLogLogAggregation) IntervalYearMonthOperators(com.facebook.presto.type.IntervalYearMonthOperators) FUNCTION_IMPLEMENTATION_MISSING(com.facebook.presto.spi.StandardErrorCode.FUNCTION_IMPLEMENTATION_MISSING) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ArrayElementAtFunction(com.facebook.presto.operator.scalar.ArrayElementAtFunction) ROW_EQUAL(com.facebook.presto.operator.scalar.RowEqualOperator.ROW_EQUAL) BitwiseFunctions(com.facebook.presto.operator.scalar.BitwiseFunctions) RealOperators(com.facebook.presto.type.RealOperators) VarcharOperators(com.facebook.presto.type.VarcharOperators) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) SMALLINT_TO_DECIMAL_SATURATED_FLOOR_CAST(com.facebook.presto.type.DecimalSaturatedFloorCasts.SMALLINT_TO_DECIMAL_SATURATED_FLOOR_CAST) Iterables(com.google.common.collect.Iterables) DOUBLE(com.facebook.presto.spi.type.DoubleType.DOUBLE) Slice(io.airlift.slice.Slice) ApproximateSetAggregation(com.facebook.presto.operator.aggregation.ApproximateSetAggregation) DoubleOperators(com.facebook.presto.type.DoubleOperators) MAX_AGGREGATION(com.facebook.presto.operator.aggregation.MaxAggregationFunction.MAX_AGGREGATION) DECIMAL_MODULUS_OPERATOR(com.facebook.presto.type.DecimalOperators.DECIMAL_MODULUS_OPERATOR) TypeSignatureProvider.fromTypes(com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes) DECIMAL_AVERAGE_AGGREGATION(com.facebook.presto.operator.aggregation.DecimalAverageAggregation.DECIMAL_AVERAGE_AGGREGATION) Multimaps(com.google.common.collect.Multimaps) ArrayList(java.util.ArrayList) MIN_N_AGGREGATION(com.facebook.presto.operator.aggregation.MinNAggregationFunction.MIN_N_AGGREGATION) REAL_TO_DECIMAL_CAST(com.facebook.presto.type.DecimalCasts.REAL_TO_DECIMAL_CAST) RealAverageAggregation(com.facebook.presto.operator.aggregation.RealAverageAggregation) ArrayNotEqualOperator(com.facebook.presto.operator.scalar.ArrayNotEqualOperator) TimeWithTimeZoneOperators(com.facebook.presto.type.TimeWithTimeZoneOperators) DECIMAL_EQUAL_OPERATOR(com.facebook.presto.type.DecimalInequalityOperators.DECIMAL_EQUAL_OPERATOR) DECIMAL_DIVIDE_OPERATOR(com.facebook.presto.type.DecimalOperators.DECIMAL_DIVIDE_OPERATOR) ApproximateDoublePercentileAggregations(com.facebook.presto.operator.aggregation.ApproximateDoublePercentileAggregations) MIN_BY_N_AGGREGATION(com.facebook.presto.operator.aggregation.MinByNAggregationFunction.MIN_BY_N_AGGREGATION) DECIMAL_GREATER_THAN_OR_EQUAL_OPERATOR(com.facebook.presto.type.DecimalInequalityOperators.DECIMAL_GREATER_THAN_OR_EQUAL_OPERATOR) CAST_FROM_UNKNOWN(com.facebook.presto.operator.scalar.CastFromUnknownOperator.CAST_FROM_UNKNOWN) DenseRankFunction(com.facebook.presto.operator.window.DenseRankFunction) DECIMAL_TO_DECIMAL_SATURATED_FLOOR_CAST(com.facebook.presto.type.DecimalSaturatedFloorCasts.DECIMAL_TO_DECIMAL_SATURATED_FLOOR_CAST) ArrayMaxFunction(com.facebook.presto.operator.scalar.ArrayMaxFunction) RealCovarianceAggregation(com.facebook.presto.operator.aggregation.RealCovarianceAggregation) ArrayLessThanOrEqualOperator(com.facebook.presto.operator.scalar.ArrayLessThanOrEqualOperator) BlockSerdeUtil(com.facebook.presto.block.BlockSerdeUtil) ArrayUnionFunction(com.facebook.presto.operator.scalar.ArrayUnionFunction) CumulativeDistributionFunction(com.facebook.presto.operator.window.CumulativeDistributionFunction) ARRAY_TRANSFORM_FUNCTION(com.facebook.presto.operator.scalar.ArrayTransformFunction.ARRAY_TRANSFORM_FUNCTION) SequenceFunction(com.facebook.presto.operator.scalar.SequenceFunction) RealRegressionAggregation(com.facebook.presto.operator.aggregation.RealRegressionAggregation) FailureFunction(com.facebook.presto.operator.scalar.FailureFunction) MapSubscriptOperator(com.facebook.presto.operator.scalar.MapSubscriptOperator) MULTIMAP_AGG(com.facebook.presto.operator.aggregation.MultimapAggregationFunction.MULTIMAP_AGG) BigintOperators(com.facebook.presto.type.BigintOperators) ARRAY_CONSTRUCTOR(com.facebook.presto.operator.scalar.ArrayConstructor.ARRAY_CONSTRUCTOR) ArraysOverlapFunction(com.facebook.presto.operator.scalar.ArraysOverlapFunction) ROW_GREATER_THAN(com.facebook.presto.operator.scalar.RowGreaterThanOperator.ROW_GREATER_THAN) GeometricMeanAggregations(com.facebook.presto.operator.aggregation.GeometricMeanAggregations) RealHistogramAggregation(com.facebook.presto.operator.aggregation.RealHistogramAggregation) BOOLEAN_TO_DECIMAL_CAST(com.facebook.presto.type.DecimalCasts.BOOLEAN_TO_DECIMAL_CAST) ImmutableCollectors.toImmutableSet(com.facebook.presto.util.ImmutableCollectors.toImmutableSet) DECIMAL_BETWEEN_OPERATOR(com.facebook.presto.type.DecimalInequalityOperators.DECIMAL_BETWEEN_OPERATOR) DateTimeFunctions(com.facebook.presto.operator.scalar.DateTimeFunctions) BooleanOrAggregation(com.facebook.presto.operator.aggregation.BooleanOrAggregation) DOUBLE_TO_DECIMAL_SATURATED_FLOOR_CAST(com.facebook.presto.type.DecimalSaturatedFloorCasts.DOUBLE_TO_DECIMAL_SATURATED_FLOOR_CAST) CharacterStringCasts(com.facebook.presto.operator.scalar.CharacterStringCasts) ZIP_FUNCTIONS(com.facebook.presto.operator.scalar.ZipFunction.ZIP_FUNCTIONS) IntervalDayTimeOperators(com.facebook.presto.type.IntervalDayTimeOperators) ApproximateLongPercentileAggregations(com.facebook.presto.operator.aggregation.ApproximateLongPercentileAggregations) MapDistinctFromOperator(com.facebook.presto.operator.scalar.MapDistinctFromOperator) FirstValueFunction(com.facebook.presto.operator.window.FirstValueFunction) String.format(java.lang.String.format) TypeUtils.resolveTypes(com.facebook.presto.type.TypeUtils.resolveTypes) Preconditions.checkState(com.google.common.base.Preconditions.checkState) TINYINT_TO_DECIMAL_CAST(com.facebook.presto.type.DecimalCasts.TINYINT_TO_DECIMAL_CAST) List(java.util.List) ArrayShuffleFunction(com.facebook.presto.operator.scalar.ArrayShuffleFunction) ArrayIntersectFunction(com.facebook.presto.operator.scalar.ArrayIntersectFunction) ROW_TO_JSON(com.facebook.presto.operator.scalar.RowToJsonCast.ROW_TO_JSON) TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) Optional(java.util.Optional) MapToMapCast(com.facebook.presto.operator.scalar.MapToMapCast) CacheBuilder(com.google.common.cache.CacheBuilder) TypeSignature(com.facebook.presto.spi.type.TypeSignature) DOUBLE_TO_DECIMAL_CAST(com.facebook.presto.type.DecimalCasts.DOUBLE_TO_DECIMAL_CAST) SMALLINT_TO_DECIMAL_CAST(com.facebook.presto.type.DecimalCasts.SMALLINT_TO_DECIMAL_CAST) ScalarFunctionImplementation(com.facebook.presto.operator.scalar.ScalarFunctionImplementation) DateOperators(com.facebook.presto.type.DateOperators) PrestoException(com.facebook.presto.spi.PrestoException) Multimap(com.google.common.collect.Multimap) ZIP_WITH_FUNCTION(com.facebook.presto.operator.scalar.ZipWithFunction.ZIP_WITH_FUNCTION) StringFunctions(com.facebook.presto.operator.scalar.StringFunctions) DECIMAL_TO_TINYINT_SATURATED_FLOOR_CAST(com.facebook.presto.type.DecimalSaturatedFloorCasts.DECIMAL_TO_TINYINT_SATURATED_FLOOR_CAST) SCALAR(com.facebook.presto.metadata.FunctionKind.SCALAR) ROW_TO_ROW_CAST(com.facebook.presto.operator.scalar.RowToRowCast.ROW_TO_ROW_CAST) CombineHashFunction(com.facebook.presto.operator.scalar.CombineHashFunction) Objects.requireNonNull(java.util.Objects.requireNonNull) TypeSignatureProvider.fromTypeSignatures(com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypeSignatures) COUNT_COLUMN(com.facebook.presto.operator.aggregation.CountColumn.COUNT_COLUMN) LeadFunction(com.facebook.presto.operator.window.LeadFunction) DoubleCorrelationAggregation(com.facebook.presto.operator.aggregation.DoubleCorrelationAggregation) DECIMAL_TO_REAL_CAST(com.facebook.presto.type.DecimalCasts.DECIMAL_TO_REAL_CAST) ArrayFilterFunction(com.facebook.presto.operator.scalar.ArrayFilterFunction) MapValues(com.facebook.presto.operator.scalar.MapValues) TinyintOperators(com.facebook.presto.type.TinyintOperators) Signature.internalOperator(com.facebook.presto.metadata.Signature.internalOperator) ArrayCardinalityFunction(com.facebook.presto.operator.scalar.ArrayCardinalityFunction) VarbinaryFunctions(com.facebook.presto.operator.scalar.VarbinaryFunctions) UrlFunctions(com.facebook.presto.operator.scalar.UrlFunctions) ARRAY_JOIN_WITH_NULL_REPLACEMENT(com.facebook.presto.operator.scalar.ArrayJoin.ARRAY_JOIN_WITH_NULL_REPLACEMENT) DECIMAL_TO_SMALLINT_CAST(com.facebook.presto.type.DecimalCasts.DECIMAL_TO_SMALLINT_CAST) VARCHAR_TO_DECIMAL_CAST(com.facebook.presto.type.DecimalCasts.VARCHAR_TO_DECIMAL_CAST) Primitives(com.google.common.primitives.Primitives) VarianceAggregation(com.facebook.presto.operator.aggregation.VarianceAggregation) ArrayRemoveFunction(com.facebook.presto.operator.scalar.ArrayRemoveFunction) MAP_CONCAT_FUNCTION(com.facebook.presto.operator.scalar.MapConcatFunction.MAP_CONCAT_FUNCTION) ROW_LESS_THAN_OR_EQUAL(com.facebook.presto.operator.scalar.RowLessThanOrEqualOperator.ROW_LESS_THAN_OR_EQUAL) ArrayHashCodeOperator(com.facebook.presto.operator.scalar.ArrayHashCodeOperator) ArrayMinFunction(com.facebook.presto.operator.scalar.ArrayMinFunction) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ROW_DISTINCT_FROM(com.facebook.presto.operator.scalar.RowDistinctFromOperator.ROW_DISTINCT_FROM) ROW_LESS_THAN(com.facebook.presto.operator.scalar.RowLessThanOperator.ROW_LESS_THAN) OperatorType(com.facebook.presto.spi.function.OperatorType) Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.spi.type.TypeSignature) ArrayList(java.util.ArrayList) PrestoException(com.facebook.presto.spi.PrestoException)

Example 3 with TypeSignatureProvider

use of com.facebook.presto.sql.analyzer.TypeSignatureProvider in project presto by prestodb.

the class BuiltInTypeAndFunctionNamespaceManager method doGetSpecializedFunctionKey.

private SpecializedFunctionKey doGetSpecializedFunctionKey(Signature signature) {
    Iterable<SqlFunction> candidates = getFunctions(null, signature.getName());
    // search for exact match
    Type returnType = functionAndTypeManager.getType(signature.getReturnType());
    List<TypeSignatureProvider> argumentTypeSignatureProviders = fromTypeSignatures(signature.getArgumentTypes());
    for (SqlFunction candidate : candidates) {
        Optional<BoundVariables> boundVariables = new SignatureBinder(functionAndTypeManager, candidate.getSignature(), false).bindVariables(argumentTypeSignatureProviders, returnType);
        if (boundVariables.isPresent()) {
            return new SpecializedFunctionKey(candidate, boundVariables.get(), argumentTypeSignatureProviders.size());
        }
    }
    // TODO: hack because there could be "type only" coercions (which aren't necessarily included as implicit casts),
    // so do a second pass allowing "type only" coercions
    List<Type> argumentTypes = resolveTypes(signature.getArgumentTypes(), functionAndTypeManager);
    for (SqlFunction candidate : candidates) {
        SignatureBinder binder = new SignatureBinder(functionAndTypeManager, candidate.getSignature(), true);
        Optional<BoundVariables> boundVariables = binder.bindVariables(argumentTypeSignatureProviders, returnType);
        if (!boundVariables.isPresent()) {
            continue;
        }
        Signature boundSignature = applyBoundVariables(candidate.getSignature(), boundVariables.get(), argumentTypes.size());
        if (!functionAndTypeManager.isTypeOnlyCoercion(functionAndTypeManager.getType(boundSignature.getReturnType()), returnType)) {
            continue;
        }
        boolean nonTypeOnlyCoercion = false;
        for (int i = 0; i < argumentTypes.size(); i++) {
            Type expectedType = functionAndTypeManager.getType(boundSignature.getArgumentTypes().get(i));
            if (!functionAndTypeManager.isTypeOnlyCoercion(argumentTypes.get(i), expectedType)) {
                nonTypeOnlyCoercion = true;
                break;
            }
        }
        if (nonTypeOnlyCoercion) {
            continue;
        }
        return new SpecializedFunctionKey(candidate, boundVariables.get(), argumentTypes.size());
    }
    // TODO: this is a hack and should be removed
    if (signature.getNameSuffix().startsWith(MAGIC_LITERAL_FUNCTION_PREFIX)) {
        List<TypeSignature> parameterTypes = signature.getArgumentTypes();
        // extract type from function name
        String typeName = signature.getNameSuffix().substring(MAGIC_LITERAL_FUNCTION_PREFIX.length());
        // lookup the type
        Type type = functionAndTypeManager.getType(parseTypeSignature(typeName));
        // verify we have one parameter of the proper type
        checkArgument(parameterTypes.size() == 1, "Expected one argument to literal function, but got %s", parameterTypes);
        Type parameterType = functionAndTypeManager.getType(parameterTypes.get(0));
        requireNonNull(parameterType, format("Type %s not found", parameterTypes.get(0)));
        return new SpecializedFunctionKey(magicLiteralFunction, BoundVariables.builder().setTypeVariable("T", parameterType).setTypeVariable("R", type).build(), 1);
    }
    throw new PrestoException(FUNCTION_IMPLEMENTATION_MISSING, format("%s not found", signature));
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) 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) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.common.type.TypeSignature) SignatureBinder.applyBoundVariables(com.facebook.presto.metadata.SignatureBinder.applyBoundVariables) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) TypeSignature(com.facebook.presto.common.type.TypeSignature) SqlFunction(com.facebook.presto.spi.function.SqlFunction)

Example 4 with TypeSignatureProvider

use of com.facebook.presto.sql.analyzer.TypeSignatureProvider in project presto by prestodb.

the class FunctionAndTypeManager method lookupFunction.

/**
 * Lookup up a function with name and fully bound types. This can only be used for builtin functions. {@link #resolveFunction(Optional, Optional, QualifiedObjectName, List)}
 * should be used for dynamically registered functions.
 *
 * @throws PrestoException if function could not be found
 */
public FunctionHandle lookupFunction(String name, List<TypeSignatureProvider> parameterTypes) {
    QualifiedObjectName functionName = qualifyObjectName(QualifiedName.of(name));
    if (parameterTypes.stream().noneMatch(TypeSignatureProvider::hasDependency)) {
        return lookupCachedFunction(functionName, parameterTypes);
    }
    Collection<? extends SqlFunction> candidates = builtInTypeAndFunctionNamespaceManager.getFunctions(Optional.empty(), functionName);
    Optional<Signature> match = functionSignatureMatcher.match(candidates, parameterTypes, false);
    if (!match.isPresent()) {
        throw new PrestoException(FUNCTION_NOT_FOUND, constructFunctionNotFoundErrorMessage(functionName, parameterTypes, candidates));
    }
    return builtInTypeAndFunctionNamespaceManager.getFunctionHandle(Optional.empty(), match.get());
}
Also used : TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) TypeSignature(com.facebook.presto.common.type.TypeSignature) LiteralEncoder.getMagicLiteralFunctionSignature(com.facebook.presto.sql.planner.LiteralEncoder.getMagicLiteralFunctionSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) PrestoException(com.facebook.presto.spi.PrestoException) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName)

Example 5 with TypeSignatureProvider

use of com.facebook.presto.sql.analyzer.TypeSignatureProvider in project presto by prestodb.

the class TestSignatureBinder method testFunction.

@Test
public void testFunction() {
    Signature simple = functionSignature().returnType(parseTypeSignature("boolean")).argumentTypes(parseTypeSignature("function(integer,integer)")).build();
    assertThat(simple).boundTo("integer").fails();
    assertThat(simple).boundTo("function(integer,integer)").succeeds();
    // TODO: Support coercion of return type of lambda
    assertThat(simple).boundTo("function(integer,smallint)").withCoercion().fails();
    assertThat(simple).boundTo("function(integer,bigint)").withCoercion().fails();
    Signature applyTwice = functionSignature().returnType(parseTypeSignature("V")).argumentTypes(parseTypeSignature("T"), parseTypeSignature("function(T,U)"), parseTypeSignature("function(U,V)")).typeVariableConstraints(typeVariable("T"), typeVariable("U"), typeVariable("V")).build();
    assertThat(applyTwice).boundTo("integer", "integer", "integer").fails();
    assertThat(applyTwice).boundTo("integer", "function(integer,varchar)", "function(varchar,double)").produces(BoundVariables.builder().setTypeVariable("T", INTEGER).setTypeVariable("U", VARCHAR).setTypeVariable("V", DOUBLE).build());
    assertThat(applyTwice).boundTo("integer", new TypeSignatureProvider(functionArgumentTypes -> TypeSignature.parseTypeSignature("function(integer,varchar)")), new TypeSignatureProvider(functionArgumentTypes -> TypeSignature.parseTypeSignature("function(varchar,double)"))).produces(BoundVariables.builder().setTypeVariable("T", INTEGER).setTypeVariable("U", VARCHAR).setTypeVariable("V", DOUBLE).build());
    assertThat(applyTwice).boundTo(// pass function argument to non-function position of a function
    new TypeSignatureProvider(functionArgumentTypes -> TypeSignature.parseTypeSignature("function(integer,varchar)")), new TypeSignatureProvider(functionArgumentTypes -> TypeSignature.parseTypeSignature("function(integer,varchar)")), new TypeSignatureProvider(functionArgumentTypes -> TypeSignature.parseTypeSignature("function(varchar,double)"))).fails();
    assertThat(applyTwice).boundTo(new TypeSignatureProvider(functionArgumentTypes -> TypeSignature.parseTypeSignature("function(integer,varchar)")), // pass non-function argument to function position of a function
    "integer", new TypeSignatureProvider(functionArgumentTypes -> TypeSignature.parseTypeSignature("function(varchar,double)"))).fails();
    Signature flatMap = functionSignature().returnType(parseTypeSignature("array(T)")).argumentTypes(parseTypeSignature("array(T)"), parseTypeSignature("function(T, array(T))")).typeVariableConstraints(typeVariable("T")).build();
    assertThat(flatMap).boundTo("array(integer)", "function(integer, array(integer))").produces(BoundVariables.builder().setTypeVariable("T", INTEGER).build());
    Signature varargApply = functionSignature().returnType(parseTypeSignature("T")).argumentTypes(parseTypeSignature("T"), parseTypeSignature("function(T, T)")).typeVariableConstraints(typeVariable("T")).setVariableArity(true).build();
    assertThat(varargApply).boundTo("integer", "function(integer, integer)", "function(integer, integer)", "function(integer, integer)").produces(BoundVariables.builder().setTypeVariable("T", INTEGER).build());
    assertThat(varargApply).boundTo("integer", "function(integer, integer)", "function(integer, double)", "function(double, double)").fails();
    Signature loop = functionSignature().returnType(parseTypeSignature("T")).argumentTypes(parseTypeSignature("T"), parseTypeSignature("function(T, T)")).typeVariableConstraints(typeVariable("T")).build();
    assertThat(loop).boundTo("integer", new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, BIGINT).getTypeSignature())).fails();
    assertThat(loop).boundTo("integer", new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, BIGINT).getTypeSignature())).withCoercion().produces(BoundVariables.builder().setTypeVariable("T", BIGINT).build());
    // TODO: Support coercion of return type of lambda
    assertThat(loop).withCoercion().boundTo("integer", new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, SMALLINT).getTypeSignature())).fails();
    // TODO: Support coercion of return type of lambda
    // Without coercion support for return type of lambda, the return type of lambda must be `varchar(x)` to avoid need for coercions.
    Signature varcharApply = functionSignature().returnType(parseTypeSignature("varchar")).argumentTypes(parseTypeSignature("varchar"), parseTypeSignature("function(varchar, varchar(x))", ImmutableSet.of("x"))).build();
    assertThat(varcharApply).withCoercion().boundTo("varchar(10)", new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, createVarcharType(1)).getTypeSignature())).succeeds();
    Signature sortByKey = functionSignature().returnType(parseTypeSignature("array(T)")).argumentTypes(parseTypeSignature("array(T)"), parseTypeSignature("function(T,E)")).typeVariableConstraints(typeVariable("T"), orderableTypeParameter("E")).build();
    assertThat(sortByKey).boundTo("array(integer)", new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, VARCHAR).getTypeSignature())).produces(BoundVariables.builder().setTypeVariable("T", INTEGER).setTypeVariable("E", VARCHAR).build());
}
Also used : TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) StandardTypes(com.facebook.presto.common.type.StandardTypes) DecimalType(com.facebook.presto.common.type.DecimalType) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) TypeSignatureProvider.fromTypes(com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes) TypeSignature(com.facebook.presto.common.type.TypeSignature) Signature.typeVariable(com.facebook.presto.spi.function.Signature.typeVariable) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) Signature.orderableTypeParameter(com.facebook.presto.spi.function.Signature.orderableTypeParameter) Assert.assertFalse(org.testng.Assert.assertFalse) Type(com.facebook.presto.common.type.Type) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) Signature.comparableTypeParameter(com.facebook.presto.spi.function.Signature.comparableTypeParameter) ImmutableSet(com.google.common.collect.ImmutableSet) TypeVariableConstraint(com.facebook.presto.spi.function.TypeVariableConstraint) ImmutableMap(com.google.common.collect.ImmutableMap) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) Assert.fail(org.testng.Assert.fail) Set(java.util.Set) Assert.assertNotNull(org.testng.Assert.assertNotNull) FunctionType(com.facebook.presto.common.type.FunctionType) SCALAR(com.facebook.presto.spi.function.FunctionKind.SCALAR) String.format(java.lang.String.format) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) SMALLINT(com.facebook.presto.common.type.SmallintType.SMALLINT) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) Signature(com.facebook.presto.spi.function.Signature) TypeSignatureProvider(com.facebook.presto.sql.analyzer.TypeSignatureProvider) Optional(java.util.Optional) Assert.assertTrue(org.testng.Assert.assertTrue) Signature.withVariadicBound(com.facebook.presto.spi.function.Signature.withVariadicBound) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) FunctionType(com.facebook.presto.common.type.FunctionType) Test(org.testng.annotations.Test)

Aggregations

TypeSignatureProvider (com.facebook.presto.sql.analyzer.TypeSignatureProvider)10 TypeSignature (com.facebook.presto.common.type.TypeSignature)5 PrestoException (com.facebook.presto.spi.PrestoException)5 Type (com.facebook.presto.common.type.Type)4 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)4 Signature (com.facebook.presto.spi.function.Signature)4 OperatorType (com.facebook.presto.common.function.OperatorType)3 SignatureBinder.applyBoundVariables (com.facebook.presto.metadata.SignatureBinder.applyBoundVariables)3 ImmutableList (com.google.common.collect.ImmutableList)3 FunctionType (com.facebook.presto.common.type.FunctionType)2 ParametricType (com.facebook.presto.common.type.ParametricType)2 UserDefinedType (com.facebook.presto.common.type.UserDefinedType)2 FunctionAndTypeManager.createTestFunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager)2 OperatorType (com.facebook.presto.spi.function.OperatorType)2 TypeVariableConstraint (com.facebook.presto.spi.function.TypeVariableConstraint)2 Type (com.facebook.presto.spi.type.Type)2 TypeSignature (com.facebook.presto.spi.type.TypeSignature)2 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)2 VarcharType (com.facebook.presto.spi.type.VarcharType)2 TypeSignatureProvider.fromTypes (com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes)2