Search in sources :

Example 61 with Signature

use of com.facebook.presto.spi.function.Signature in project presto by prestodb.

the class DecimalOperators method decimalAddOperator.

private static SqlScalarFunction decimalAddOperator() {
    TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
    TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
    TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "r_scale"));
    Signature signature = SignatureBuilder.builder().kind(SCALAR).operatorType(ADD).longVariableConstraints(longVariableExpression("r_precision", "min(38, max(a_precision - a_scale, b_precision - b_scale) + max(a_scale, b_scale) + 1)"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
    return SqlScalarFunction.builder(DecimalOperators.class, ADD).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("addShortShortShort").withExtraParameters(DecimalOperators::calculateShortRescaleParameters)).implementation(methodsGroup -> methodsGroup.methods("addShortShortLong", "addLongLongLong", "addShortLongLong", "addLongShortLong").withExtraParameters(DecimalOperators::calculateLongRescaleParameters))).build();
}
Also used : SUBTRACT(com.facebook.presto.common.function.OperatorType.SUBTRACT) INDETERMINATE(com.facebook.presto.common.function.OperatorType.INDETERMINATE) UnscaledDecimal128Arithmetic.throwIfOverflows(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.throwIfOverflows) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong) Math.abs(java.lang.Math.abs) TypeSignature(com.facebook.presto.common.type.TypeSignature) ADD(com.facebook.presto.common.function.OperatorType.ADD) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) DIVIDE(com.facebook.presto.common.function.OperatorType.DIVIDE) BigInteger(java.math.BigInteger) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) Long.signum(java.lang.Long.signum) HASH_CODE(com.facebook.presto.common.function.OperatorType.HASH_CODE) ImmutableSet(com.google.common.collect.ImmutableSet) UnscaledDecimal128Arithmetic.remainder(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.remainder) NEGATION(com.facebook.presto.common.function.OperatorType.NEGATION) UnscaledDecimal128Arithmetic.rescale(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.rescale) SCALAR(com.facebook.presto.spi.function.FunctionKind.SCALAR) List(java.util.List) UnscaledDecimal128Arithmetic(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic) PolymorphicScalarFunctionBuilder(com.facebook.presto.metadata.PolymorphicScalarFunctionBuilder) SqlType(com.facebook.presto.spi.function.SqlType) StandardTypes(com.facebook.presto.common.type.StandardTypes) DecimalType(com.facebook.presto.common.type.DecimalType) Slice(io.airlift.slice.Slice) DIVISION_BY_ZERO(com.facebook.presto.spi.StandardErrorCode.DIVISION_BY_ZERO) Decimals.encodeUnscaledValue(com.facebook.presto.common.type.Decimals.encodeUnscaledValue) MULTIPLY(com.facebook.presto.common.function.OperatorType.MULTIPLY) PrestoException(com.facebook.presto.spi.PrestoException) UnscaledDecimal128Arithmetic.isZero(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.isZero) ImmutableList(com.google.common.collect.ImmutableList) UnscaledDecimal128Arithmetic.divideRoundUp(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.divideRoundUp) UnscaledDecimal128Arithmetic.unscaledDecimal(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.unscaledDecimal) Objects.requireNonNull(java.util.Objects.requireNonNull) Math.toIntExact(java.lang.Math.toIntExact) Decimals.longTenToNth(com.facebook.presto.common.type.Decimals.longTenToNth) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Signature.longVariableExpression(com.facebook.presto.spi.function.Signature.longVariableExpression) XxHash64(io.airlift.slice.XxHash64) Decimals(com.facebook.presto.common.type.Decimals) SpecializeContext(com.facebook.presto.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext) Integer.max(java.lang.Integer.max) NUMERIC_VALUE_OUT_OF_RANGE(com.facebook.presto.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) IsNull(com.facebook.presto.spi.function.IsNull) Signature(com.facebook.presto.spi.function.Signature) MODULUS(com.facebook.presto.common.function.OperatorType.MODULUS) SignatureBuilder(com.facebook.presto.metadata.SignatureBuilder) XX_HASH_64(com.facebook.presto.common.function.OperatorType.XX_HASH_64) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature)

Example 62 with Signature

use of com.facebook.presto.spi.function.Signature in project presto by prestodb.

the class DecimalOperators method decimalMultiplyOperator.

private static SqlScalarFunction decimalMultiplyOperator() {
    TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
    TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
    TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "r_scale"));
    Signature signature = SignatureBuilder.builder().kind(SCALAR).operatorType(MULTIPLY).longVariableConstraints(longVariableExpression("r_precision", "min(38, a_precision + b_precision)"), longVariableExpression("r_scale", "a_scale + b_scale")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
    return SqlScalarFunction.builder(DecimalOperators.class, MULTIPLY).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("multiplyShortShortShort", "multiplyShortShortLong", "multiplyLongLongLong", "multiplyShortLongLong", "multiplyLongShortLong"))).build();
}
Also used : SUBTRACT(com.facebook.presto.common.function.OperatorType.SUBTRACT) INDETERMINATE(com.facebook.presto.common.function.OperatorType.INDETERMINATE) UnscaledDecimal128Arithmetic.throwIfOverflows(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.throwIfOverflows) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong) Math.abs(java.lang.Math.abs) TypeSignature(com.facebook.presto.common.type.TypeSignature) ADD(com.facebook.presto.common.function.OperatorType.ADD) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) DIVIDE(com.facebook.presto.common.function.OperatorType.DIVIDE) BigInteger(java.math.BigInteger) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) Long.signum(java.lang.Long.signum) HASH_CODE(com.facebook.presto.common.function.OperatorType.HASH_CODE) ImmutableSet(com.google.common.collect.ImmutableSet) UnscaledDecimal128Arithmetic.remainder(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.remainder) NEGATION(com.facebook.presto.common.function.OperatorType.NEGATION) UnscaledDecimal128Arithmetic.rescale(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.rescale) SCALAR(com.facebook.presto.spi.function.FunctionKind.SCALAR) List(java.util.List) UnscaledDecimal128Arithmetic(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic) PolymorphicScalarFunctionBuilder(com.facebook.presto.metadata.PolymorphicScalarFunctionBuilder) SqlType(com.facebook.presto.spi.function.SqlType) StandardTypes(com.facebook.presto.common.type.StandardTypes) DecimalType(com.facebook.presto.common.type.DecimalType) Slice(io.airlift.slice.Slice) DIVISION_BY_ZERO(com.facebook.presto.spi.StandardErrorCode.DIVISION_BY_ZERO) Decimals.encodeUnscaledValue(com.facebook.presto.common.type.Decimals.encodeUnscaledValue) MULTIPLY(com.facebook.presto.common.function.OperatorType.MULTIPLY) PrestoException(com.facebook.presto.spi.PrestoException) UnscaledDecimal128Arithmetic.isZero(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.isZero) ImmutableList(com.google.common.collect.ImmutableList) UnscaledDecimal128Arithmetic.divideRoundUp(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.divideRoundUp) UnscaledDecimal128Arithmetic.unscaledDecimal(com.facebook.presto.common.type.UnscaledDecimal128Arithmetic.unscaledDecimal) Objects.requireNonNull(java.util.Objects.requireNonNull) Math.toIntExact(java.lang.Math.toIntExact) Decimals.longTenToNth(com.facebook.presto.common.type.Decimals.longTenToNth) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Signature.longVariableExpression(com.facebook.presto.spi.function.Signature.longVariableExpression) XxHash64(io.airlift.slice.XxHash64) Decimals(com.facebook.presto.common.type.Decimals) SpecializeContext(com.facebook.presto.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext) Integer.max(java.lang.Integer.max) NUMERIC_VALUE_OUT_OF_RANGE(com.facebook.presto.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) IsNull(com.facebook.presto.spi.function.IsNull) Signature(com.facebook.presto.spi.function.Signature) MODULUS(com.facebook.presto.common.function.OperatorType.MODULUS) SignatureBuilder(com.facebook.presto.metadata.SignatureBuilder) XX_HASH_64(com.facebook.presto.common.function.OperatorType.XX_HASH_64) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature)

Example 63 with Signature

use of com.facebook.presto.spi.function.Signature in project presto by prestodb.

the class TestFunctionAndTypeManager method testIdentityCast.

@Test
public void testIdentityCast() {
    FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
    FunctionHandle exactOperator = functionAndTypeManager.lookupCast(CastType.CAST, HYPER_LOG_LOG.getTypeSignature(), HYPER_LOG_LOG.getTypeSignature());
    assertEquals(exactOperator, new BuiltInFunctionHandle(new Signature(CAST.getFunctionName(), SCALAR, HYPER_LOG_LOG.getTypeSignature(), HYPER_LOG_LOG.getTypeSignature())));
}
Also used : FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) 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) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) Test(org.testng.annotations.Test)

Example 64 with Signature

use of com.facebook.presto.spi.function.Signature in project presto by prestodb.

the class TestPolymorphicScalarFunction method testSelectsMethodBasedOnReturnType.

@Test
public void testSelectsMethodBasedOnReturnType() throws Throwable {
    SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class).signature(SIGNATURE).deterministic(true).calledOnNullInput(false).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("varcharToVarcharCreateSliceWithExtraParameterLength")).implementation(methodsGroup -> methodsGroup.methods("varcharToBigintReturnExtraParameter").withExtraParameters(context -> ImmutableList.of(42)))).build();
    BuiltInScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, FUNCTION_AND_TYPE_MANAGER);
    assertEquals(functionImplementation.getMethodHandle().invoke(INPUT_SLICE), VARCHAR_TO_BIGINT_RETURN_VALUE);
}
Also used : StandardTypes(com.facebook.presto.common.type.StandardTypes) Slice(io.airlift.slice.Slice) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) IS_DISTINCT_FROM(com.facebook.presto.common.function.OperatorType.IS_DISTINCT_FROM) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) TypeSignature(com.facebook.presto.common.type.TypeSignature) MAX_SHORT_PRECISION(com.facebook.presto.common.type.Decimals.MAX_SHORT_PRECISION) ADD(com.facebook.presto.common.function.OperatorType.ADD) ImmutableList(com.google.common.collect.ImmutableList) Arrays.asList(java.util.Arrays.asList) Slices(io.airlift.slice.Slices) Math.toIntExact(java.lang.Math.toIntExact) VARCHAR_TO_VARCHAR_RETURN_VALUE(com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_VARCHAR_RETURN_VALUE) Assert.assertFalse(org.testng.Assert.assertFalse) BOOLEAN(com.facebook.presto.common.type.StandardTypes.BOOLEAN) BuiltInScalarFunctionImplementation(com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation) ImmutableSet(com.google.common.collect.ImmutableSet) Signature.comparableWithVariadicBound(com.facebook.presto.spi.function.Signature.comparableWithVariadicBound) ImmutableMap(com.google.common.collect.ImmutableMap) LongArrayBlock(com.facebook.presto.common.block.LongArrayBlock) BLOCK_AND_POSITION(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.BLOCK_AND_POSITION) USE_NULL_FLAG(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.USE_NULL_FLAG) SCALAR(com.facebook.presto.spi.function.FunctionKind.SCALAR) VARCHAR(com.facebook.presto.common.type.StandardTypes.VARCHAR) ArgumentProperty.valueTypeArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty.valueTypeArgumentProperty) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) VARCHAR_TO_BIGINT_RETURN_VALUE(com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_BIGINT_RETURN_VALUE) Signature(com.facebook.presto.spi.function.Signature) Optional(java.util.Optional) Assert.assertTrue(org.testng.Assert.assertTrue) Block(com.facebook.presto.common.block.Block) Collections(java.util.Collections) BuiltInScalarFunctionImplementation(com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation) Test(org.testng.annotations.Test)

Example 65 with Signature

use of com.facebook.presto.spi.function.Signature in project presto by prestodb.

the class TestPolymorphicScalarFunction method testTypeParameters.

@Test
public void testTypeParameters() throws Throwable {
    Signature signature = SignatureBuilder.builder().name("foo").kind(SCALAR).typeVariableConstraints(comparableWithVariadicBound("V", VARCHAR)).returnType(parseTypeSignature("V")).argumentTypes(parseTypeSignature("V")).build();
    SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class).signature(signature).deterministic(true).calledOnNullInput(false).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("varcharToVarchar"))).build();
    BuiltInScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, FUNCTION_AND_TYPE_MANAGER);
    Slice slice = (Slice) functionImplementation.getMethodHandle().invoke(INPUT_SLICE);
    assertEquals(slice, VARCHAR_TO_VARCHAR_RETURN_VALUE);
}
Also used : StandardTypes(com.facebook.presto.common.type.StandardTypes) Slice(io.airlift.slice.Slice) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) IS_DISTINCT_FROM(com.facebook.presto.common.function.OperatorType.IS_DISTINCT_FROM) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) TypeSignature(com.facebook.presto.common.type.TypeSignature) MAX_SHORT_PRECISION(com.facebook.presto.common.type.Decimals.MAX_SHORT_PRECISION) ADD(com.facebook.presto.common.function.OperatorType.ADD) ImmutableList(com.google.common.collect.ImmutableList) Arrays.asList(java.util.Arrays.asList) Slices(io.airlift.slice.Slices) Math.toIntExact(java.lang.Math.toIntExact) VARCHAR_TO_VARCHAR_RETURN_VALUE(com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_VARCHAR_RETURN_VALUE) Assert.assertFalse(org.testng.Assert.assertFalse) BOOLEAN(com.facebook.presto.common.type.StandardTypes.BOOLEAN) BuiltInScalarFunctionImplementation(com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation) ImmutableSet(com.google.common.collect.ImmutableSet) Signature.comparableWithVariadicBound(com.facebook.presto.spi.function.Signature.comparableWithVariadicBound) ImmutableMap(com.google.common.collect.ImmutableMap) LongArrayBlock(com.facebook.presto.common.block.LongArrayBlock) BLOCK_AND_POSITION(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.BLOCK_AND_POSITION) USE_NULL_FLAG(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.USE_NULL_FLAG) SCALAR(com.facebook.presto.spi.function.FunctionKind.SCALAR) VARCHAR(com.facebook.presto.common.type.StandardTypes.VARCHAR) ArgumentProperty.valueTypeArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty.valueTypeArgumentProperty) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) VARCHAR_TO_BIGINT_RETURN_VALUE(com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_BIGINT_RETURN_VALUE) Signature(com.facebook.presto.spi.function.Signature) Optional(java.util.Optional) Assert.assertTrue(org.testng.Assert.assertTrue) Block(com.facebook.presto.common.block.Block) Collections(java.util.Collections) BuiltInScalarFunctionImplementation(com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation) Slice(io.airlift.slice.Slice) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) Test(org.testng.annotations.Test)

Aggregations

Signature (com.facebook.presto.spi.function.Signature)90 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)79 TypeSignature (com.facebook.presto.common.type.TypeSignature)73 Test (org.testng.annotations.Test)64 SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)16 ParametricAggregation (com.facebook.presto.operator.aggregation.ParametricAggregation)14 ImmutableList (com.google.common.collect.ImmutableList)14 AggregationImplementation (com.facebook.presto.operator.aggregation.AggregationImplementation)13 AggregationMetadata (com.facebook.presto.operator.aggregation.AggregationMetadata)13 StandardTypes (com.facebook.presto.common.type.StandardTypes)12 InternalAggregationFunction (com.facebook.presto.operator.aggregation.InternalAggregationFunction)12 ParametricScalar (com.facebook.presto.operator.scalar.ParametricScalar)12 PrestoException (com.facebook.presto.spi.PrestoException)12 SCALAR (com.facebook.presto.spi.function.FunctionKind.SCALAR)12 ImmutableSet (com.google.common.collect.ImmutableSet)12 Slice (io.airlift.slice.Slice)12 BuiltInScalarFunctionImplementation (com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation)11 ADD (com.facebook.presto.common.function.OperatorType.ADD)10 FunctionAndTypeManager.createTestFunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager)10 Math.toIntExact (java.lang.Math.toIntExact)10