Search in sources :

Example 1 with SqlScalarFunction

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

the class DecimalCasts method castFunctionFromDecimalTo.

private static SqlScalarFunction castFunctionFromDecimalTo(TypeSignature to, String... methodNames) {
    Signature signature = Signature.builder().kind(SCALAR).operatorType(CAST).argumentTypes(parseTypeSignature("decimal(precision,scale)", ImmutableSet.of("precision", "scale"))).returnType(to).build();
    return SqlScalarFunction.builder(DecimalCasts.class).signature(signature).implementation(b -> b.methods(methodNames).withExtraParameters((context) -> {
        long precision = context.getLiteral("precision");
        long scale = context.getLiteral("scale");
        Number tenToScale;
        if (isShortDecimal(context.getParameterTypes().get(0))) {
            tenToScale = longTenToNth(intScale(scale));
        } else {
            tenToScale = bigIntegerTenToNth(intScale(scale));
        }
        return ImmutableList.of(precision, scale, tenToScale);
    })).build();
}
Also used : CAST(com.facebook.presto.spi.function.OperatorType.CAST) Decimals.encodeUnscaledValue(com.facebook.presto.spi.type.Decimals.encodeUnscaledValue) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLongUnsafe(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLongUnsafe) BIGINT(com.facebook.presto.spi.type.BigintType.BIGINT) BigDecimal(java.math.BigDecimal) Float.parseFloat(java.lang.Float.parseFloat) DecimalType(com.facebook.presto.spi.type.DecimalType) BOOLEAN(com.facebook.presto.spi.type.BooleanType.BOOLEAN) Slices(io.airlift.slice.Slices) Decimals.decodeUnscaledValue(com.facebook.presto.spi.type.Decimals.decodeUnscaledValue) BigInteger(java.math.BigInteger) StandardTypes(com.facebook.presto.spi.type.StandardTypes) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) Double.parseDouble(java.lang.Double.parseDouble) UnscaledDecimal128Arithmetic(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic) Decimals.isShortDecimal(com.facebook.presto.spi.type.Decimals.isShortDecimal) Decimals(com.facebook.presto.spi.type.Decimals) TINYINT(com.facebook.presto.spi.type.TinyintType.TINYINT) ImmutableSet(com.google.common.collect.ImmutableSet) UnscaledDecimal128Arithmetic.multiply(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.multiply) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) UnscaledDecimal128Arithmetic.compareAbsolute(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.compareAbsolute) INTEGER(com.facebook.presto.spi.type.IntegerType.INTEGER) ZERO(java.math.BigInteger.ZERO) Decimals.longTenToNth(com.facebook.presto.spi.type.Decimals.longTenToNth) TypeSignature(com.facebook.presto.spi.type.TypeSignature) UnscaledDecimal128Arithmetic.rescale(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.rescale) DOUBLE(com.facebook.presto.spi.type.DoubleType.DOUBLE) Slice(io.airlift.slice.Slice) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) SliceOutput(io.airlift.slice.SliceOutput) JSON(com.facebook.presto.type.JsonType.JSON) Shorts(com.google.common.primitives.Shorts) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong) PrestoException(com.facebook.presto.spi.PrestoException) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Float.floatToRawIntBits(java.lang.Float.floatToRawIntBits) SCALAR(com.facebook.presto.metadata.FunctionKind.SCALAR) JsonUtil.createJsonGenerator(com.facebook.presto.util.JsonUtil.createJsonGenerator) UnscaledDecimal128Arithmetic.overflows(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.overflows) ImmutableList(com.google.common.collect.ImmutableList) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) Math.toIntExact(java.lang.Math.toIntExact) Nullable(javax.annotation.Nullable) Failures.checkCondition(com.facebook.presto.util.Failures.checkCondition) Decimals.bigIntegerTenToNth(com.facebook.presto.spi.type.Decimals.bigIntegerTenToNth) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) HALF_UP(java.math.RoundingMode.HALF_UP) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Signature(com.facebook.presto.metadata.Signature) SignedBytes(com.google.common.primitives.SignedBytes) INVALID_CAST_ARGUMENT(com.facebook.presto.spi.StandardErrorCode.INVALID_CAST_ARGUMENT) IOException(java.io.IOException) SMALLINT(com.facebook.presto.spi.type.SmallintType.SMALLINT) JSON_FACTORY(com.facebook.presto.operator.scalar.JsonOperators.JSON_FACTORY) UnscaledDecimal128Arithmetic.unscaledDecimal(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimal) Math.multiplyExact(java.lang.Math.multiplyExact) REAL(com.facebook.presto.spi.type.RealType.REAL) SqlScalarFunctionBuilder(com.facebook.presto.metadata.SqlScalarFunctionBuilder) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) Decimals.overflows(com.facebook.presto.spi.type.Decimals.overflows) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)

Example 2 with SqlScalarFunction

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

the class TestAnnotationEngineForScalars method testMultiScalarParse.

@Test
public void testMultiScalarParse() {
    Signature expectedSignature1 = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "static_method_scalar_1"), FunctionKind.SCALAR, DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature()));
    Signature expectedSignature2 = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "static_method_scalar_2"), FunctionKind.SCALAR, BIGINT.getTypeSignature(), ImmutableList.of(BIGINT.getTypeSignature()));
    Signature expectedSignature3 = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "static_method_scalar_3"), FunctionKind.SCALAR, BIGINT.getTypeSignature(), ImmutableList.of(BIGINT.getTypeSignature()));
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinitions(MultiScalarFunction.class);
    assertEquals(functions.size(), 3);
    ParametricScalar scalar1 = (ParametricScalar) functions.stream().filter(signature -> signature.getSignature().equals(expectedSignature1)).collect(toImmutableList()).get(0);
    ParametricScalar scalar2 = (ParametricScalar) functions.stream().filter(signature -> signature.getSignature().equals(expectedSignature2)).collect(toImmutableList()).get(0);
    ParametricScalar scalar3 = (ParametricScalar) functions.stream().filter(signature -> signature.getSignature().equals(expectedSignature3)).collect(toImmutableList()).get(0);
    assertImplementationCount(scalar1, 1, 0, 0);
    assertImplementationCount(scalar2, 1, 0, 0);
    assertEquals(scalar1.getSignature(), expectedSignature1);
    assertTrue(scalar1.isDeterministic());
    assertEquals(scalar1.getVisibility(), PUBLIC);
    assertEquals(scalar1.getDescription(), "Simple scalar with single implementation based on method 1");
    assertEquals(scalar2.getSignature(), expectedSignature2);
    assertFalse(scalar2.isDeterministic());
    assertEquals(scalar2.getVisibility(), HIDDEN);
    assertEquals(scalar2.getDescription(), "Simple scalar with single implementation based on method 2");
    assertEquals(scalar3.getSignature(), expectedSignature3);
    assertFalse(scalar3.isDeterministic());
    assertEquals(scalar3.getVisibility(), EXPERIMENTAL);
    assertEquals(scalar3.getDescription(), "Simple scalar with single implementation based on method 3");
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) EXPERIMENTAL(com.facebook.presto.spi.function.SqlFunctionVisibility.EXPERIMENTAL) StandardTypes(com.facebook.presto.common.type.StandardTypes) TypeParameter(com.facebook.presto.spi.function.TypeParameter) FunctionKind(com.facebook.presto.spi.function.FunctionKind) Slice(io.airlift.slice.Slice) ParametricScalar(com.facebook.presto.operator.scalar.ParametricScalar) ParametricScalarImplementationChoice(com.facebook.presto.operator.scalar.annotations.ParametricScalarImplementation.ParametricScalarImplementationChoice) TypeImplementationDependency(com.facebook.presto.operator.annotations.TypeImplementationDependency) Assert.assertEquals(org.testng.Assert.assertEquals) Test(org.testng.annotations.Test) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) DEFAULT_NAMESPACE(com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE) Signature.typeVariable(com.facebook.presto.spi.function.Signature.typeVariable) ImmutableList(com.google.common.collect.ImmutableList) Description(com.facebook.presto.spi.function.Description) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) RETURN_NULL_ON_NULL(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.RETURN_NULL_ON_NULL) Assert.assertFalse(org.testng.Assert.assertFalse) Type(com.facebook.presto.common.type.Type) LiteralParameter(com.facebook.presto.type.LiteralParameter) BuiltInScalarFunctionImplementation(com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) ImmutableSet(com.google.common.collect.ImmutableSet) BoundVariables(com.facebook.presto.metadata.BoundVariables) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) HIDDEN(com.facebook.presto.spi.function.SqlFunctionVisibility.HIDDEN) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) USE_BOXED_TYPE(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.USE_BOXED_TYPE) USE_NULL_FLAG(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.USE_NULL_FLAG) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) ArgumentProperty.valueTypeArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty.valueTypeArgumentProperty) PUBLIC(com.facebook.presto.spi.function.SqlFunctionVisibility.PUBLIC) List(java.util.List) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) ScalarFromAnnotationsParser(com.facebook.presto.operator.scalar.annotations.ScalarFromAnnotationsParser) IsNull(com.facebook.presto.spi.function.IsNull) SqlNullable(com.facebook.presto.spi.function.SqlNullable) Signature(com.facebook.presto.spi.function.Signature) Assert.assertTrue(org.testng.Assert.assertTrue) Block(com.facebook.presto.common.block.Block) ImplementationDependency(com.facebook.presto.operator.annotations.ImplementationDependency) LiteralImplementationDependency(com.facebook.presto.operator.annotations.LiteralImplementationDependency) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType) 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 3 with SqlScalarFunction

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

the class TestAnnotationEngineForScalars method testPartiallyFixedTypeParameterParse.

@Test
public void testPartiallyFixedTypeParameterParse() {
    Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "partially_fixed_type_parameter_scalar_function"), FunctionKind.SCALAR, ImmutableList.of(typeVariable("T1"), typeVariable("T2")), ImmutableList.of(), BIGINT.getTypeSignature(), ImmutableList.of(BIGINT.getTypeSignature()), false);
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(PartiallyFixedTypeParameterScalarFunction.class);
    assertEquals(functions.size(), 1);
    ParametricScalar scalar = (ParametricScalar) functions.get(0);
    assertImplementationCount(scalar, 0, 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(), 1);
    assertEquals(scalar.getSignature(), expectedSignature);
    assertTrue(scalar.isDeterministic());
    assertEquals(scalar.getVisibility(), PUBLIC);
    assertEquals(scalar.getDescription(), "Parametric scalar that uses TypeParameter with partially fixed type");
}
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) Test(org.testng.annotations.Test)

Example 4 with SqlScalarFunction

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

the class TestAnnotationEngineForScalars method testSimpleInjectionScalarParse.

@Test
public void testSimpleInjectionScalarParse() {
    Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "parametric_scalar_inject"), FunctionKind.SCALAR, ImmutableList.of(), ImmutableList.of(), BIGINT.getTypeSignature(), ImmutableList.of(parseTypeSignature("varchar(x)", ImmutableSet.of("x"))), false);
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(SimpleInjectionScalarFunction.class);
    assertEquals(functions.size(), 1);
    ParametricScalar scalar = (ParametricScalar) functions.get(0);
    assertImplementationCount(scalar, 0, 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(), 1);
    assertTrue(dependencies.get(0) instanceof LiteralImplementationDependency);
    assertEquals(scalar.getSignature(), expectedSignature);
    assertTrue(scalar.isDeterministic());
    assertEquals(scalar.getVisibility(), PUBLIC);
    assertEquals(scalar.getDescription(), "Parametric scalar with literal injected");
}
Also used : TypeImplementationDependency(com.facebook.presto.operator.annotations.TypeImplementationDependency) ImplementationDependency(com.facebook.presto.operator.annotations.ImplementationDependency) LiteralImplementationDependency(com.facebook.presto.operator.annotations.LiteralImplementationDependency) 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) Test(org.testng.annotations.Test)

Example 5 with SqlScalarFunction

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

the class TestAnnotationEngineForScalars method testWithNullablePrimitiveArgScalarParse.

@Test
public void testWithNullablePrimitiveArgScalarParse() {
    Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "scalar_with_nullable"), FunctionKind.SCALAR, DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature(), DOUBLE.getTypeSignature()));
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(WithNullablePrimitiveArgScalarFunction.class);
    assertEquals(functions.size(), 1);
    ParametricScalar scalar = (ParametricScalar) functions.get(0);
    assertEquals(scalar.getSignature(), expectedSignature);
    assertTrue(scalar.isDeterministic());
    assertEquals(scalar.getVisibility(), PUBLIC);
    assertEquals(scalar.getDescription(), "Simple scalar with nullable primitive");
    BuiltInScalarFunctionImplementation specialized = scalar.specialize(BoundVariables.builder().build(), 2, FUNCTION_AND_TYPE_MANAGER);
    assertFalse(specialized.getInstanceFactory().isPresent());
    assertEquals(specialized.getArgumentProperty(0), valueTypeArgumentProperty(RETURN_NULL_ON_NULL));
    assertEquals(specialized.getArgumentProperty(1), valueTypeArgumentProperty(USE_NULL_FLAG));
}
Also used : BuiltInScalarFunctionImplementation(com.facebook.presto.operator.scalar.BuiltInScalarFunctionImplementation) 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)

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