Search in sources :

Example 6 with SqlScalarFunction

use of com.facebook.presto.metadata.SqlScalarFunction in project presto by prestodb.

the class TestAnnotationEngineForScalars method testComplexParametricScalarParse.

@Test
public void testComplexParametricScalarParse() {
    Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "with_exact_scalar"), FunctionKind.SCALAR, ImmutableList.of(), ImmutableList.of(), BOOLEAN.getTypeSignature(), ImmutableList.of(parseTypeSignature("array(varchar(x))", ImmutableSet.of("x"))), false);
    Signature exactSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "with_exact_scalar"), FunctionKind.SCALAR, ImmutableList.of(), ImmutableList.of(), BOOLEAN.getTypeSignature(), ImmutableList.of(parseTypeSignature("array(varchar(17))")), false);
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(ComplexParametricScalarFunction.class);
    assertEquals(functions.size(), 1);
    ParametricScalar scalar = (ParametricScalar) functions.get(0);
    assertImplementationCount(scalar.getImplementations(), 1, 0, 1);
    assertEquals(getOnlyElement(scalar.getImplementations().getExactImplementations().keySet()), exactSignature);
    assertEquals(scalar.getSignature(), expectedSignature);
    assertTrue(scalar.isDeterministic());
    assertEquals(scalar.getVisibility(), PUBLIC);
    assertEquals(scalar.getDescription(), "Parametric scalar with exact and generic implementations");
}
Also used : TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) ParametricScalar(com.facebook.presto.operator.scalar.ParametricScalar) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Test(org.testng.annotations.Test)

Example 7 with SqlScalarFunction

use of com.facebook.presto.metadata.SqlScalarFunction in project presto by prestodb.

the class TestAnnotationEngineForScalars method testBetaScalarParse.

@Test
public void testBetaScalarParse() {
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(BetaScalarFunction.class);
    assertEquals(functions.size(), 1);
    ParametricScalar scalar = (ParametricScalar) functions.get(0);
    assertTrue(scalar.isDeterministic());
    assertEquals(scalar.getVisibility(), EXPERIMENTAL);
}
Also used : ParametricScalar(com.facebook.presto.operator.scalar.ParametricScalar) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Test(org.testng.annotations.Test)

Example 8 with SqlScalarFunction

use of com.facebook.presto.metadata.SqlScalarFunction in project presto by prestodb.

the class TestAnnotationEngineForScalars method testConstructorInjectionScalarParse.

@Test
public void testConstructorInjectionScalarParse() {
    Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "parametric_scalar_inject_constructor"), FunctionKind.SCALAR, ImmutableList.of(typeVariable("T")), ImmutableList.of(), BIGINT.getTypeSignature(), ImmutableList.of(parseTypeSignature("array(T)")), false);
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(ConstructorInjectionScalarFunction.class);
    assertEquals(functions.size(), 1);
    ParametricScalar scalar = (ParametricScalar) functions.get(0);
    assertImplementationCount(scalar, 2, 0, 1);
    List<ParametricScalarImplementationChoice> parametricScalarImplementationChoices = scalar.getImplementations().getGenericImplementations().get(0).getChoices();
    assertEquals(parametricScalarImplementationChoices.size(), 1);
    List<ImplementationDependency> dependencies = parametricScalarImplementationChoices.get(0).getDependencies();
    assertEquals(dependencies.size(), 0);
    List<ImplementationDependency> constructorDependencies = parametricScalarImplementationChoices.get(0).getConstructorDependencies();
    assertEquals(constructorDependencies.size(), 1);
    assertTrue(constructorDependencies.get(0) instanceof TypeImplementationDependency);
    assertEquals(scalar.getSignature(), expectedSignature);
    assertTrue(scalar.isDeterministic());
    assertEquals(scalar.getVisibility(), PUBLIC);
    assertEquals(scalar.getDescription(), "Parametric scalar with type injected though constructor");
}
Also used : TypeImplementationDependency(com.facebook.presto.operator.annotations.TypeImplementationDependency) ImplementationDependency(com.facebook.presto.operator.annotations.ImplementationDependency) LiteralImplementationDependency(com.facebook.presto.operator.annotations.LiteralImplementationDependency) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) ParametricScalarImplementationChoice(com.facebook.presto.operator.scalar.annotations.ParametricScalarImplementation.ParametricScalarImplementationChoice) ParametricScalar(com.facebook.presto.operator.scalar.ParametricScalar) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) TypeImplementationDependency(com.facebook.presto.operator.annotations.TypeImplementationDependency) Test(org.testng.annotations.Test)

Example 9 with SqlScalarFunction

use of com.facebook.presto.metadata.SqlScalarFunction in project presto by prestodb.

the class DecimalOperators method decimalDivideOperator.

private static SqlScalarFunction decimalDivideOperator() {
    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"));
    // we extend target precision by b_scale. This is upper bound on how much division result will grow.
    // pessimistic case is a / 0.0000001
    // if scale of divisor is greater than scale of dividend we extend scale further as we
    // want result scale to be maximum of scales of divisor and dividend.
    Signature signature = SignatureBuilder.builder().kind(SCALAR).operatorType(DIVIDE).longVariableConstraints(longVariableExpression("r_precision", "min(38, a_precision + b_scale + max(b_scale - a_scale, 0))"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
    return SqlScalarFunction.builder(DecimalOperators.class, DIVIDE).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("divideShortShortShort", "divideShortLongShort", "divideLongShortShort", "divideShortShortLong", "divideLongLongLong", "divideShortLongLong", "divideLongShortLong").withExtraParameters(DecimalOperators::divideRescaleFactor))).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 10 with SqlScalarFunction

use of com.facebook.presto.metadata.SqlScalarFunction in project presto by prestodb.

the class DecimalOperators method decimalSubtractOperator.

private static SqlScalarFunction decimalSubtractOperator() {
    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(SUBTRACT).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, SUBTRACT).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("subtractShortShortShort").withExtraParameters(DecimalOperators::calculateShortRescaleParameters)).implementation(methodsGroup -> methodsGroup.methods("subtractShortShortLong", "subtractLongLongLong", "subtractShortLongLong", "subtractLongShortLong").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)

Aggregations

SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)21 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)16 Signature (com.facebook.presto.spi.function.Signature)16 Test (org.testng.annotations.Test)15 ParametricScalar (com.facebook.presto.operator.scalar.ParametricScalar)14 ImmutableList (com.google.common.collect.ImmutableList)7 PrestoException (com.facebook.presto.spi.PrestoException)6 SqlType (com.facebook.presto.spi.function.SqlType)6 List (java.util.List)6 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)5 StandardTypes (com.facebook.presto.common.type.StandardTypes)5 TypeSignature (com.facebook.presto.common.type.TypeSignature)5 ADD (com.facebook.presto.common.function.OperatorType.ADD)4 DIVIDE (com.facebook.presto.common.function.OperatorType.DIVIDE)4 HASH_CODE (com.facebook.presto.common.function.OperatorType.HASH_CODE)4 INDETERMINATE (com.facebook.presto.common.function.OperatorType.INDETERMINATE)4 MODULUS (com.facebook.presto.common.function.OperatorType.MODULUS)4 MULTIPLY (com.facebook.presto.common.function.OperatorType.MULTIPLY)4 NEGATION (com.facebook.presto.common.function.OperatorType.NEGATION)4 SUBTRACT (com.facebook.presto.common.function.OperatorType.SUBTRACT)4