Search in sources :

Example 41 with FunctionAndTypeManager

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

the class ArrayJoin method specializeArrayJoin.

private static BuiltInScalarFunctionImplementation specializeArrayJoin(Map<String, Type> types, FunctionAndTypeManager functionAndTypeManager, List<Boolean> nullableArguments, MethodHandle methodHandleStack, MethodHandle methodHandleProvidedBlock) {
    Type type = types.get("T");
    List<ArgumentProperty> argumentProperties = nullableArguments.stream().map(nullable -> nullable ? valueTypeArgumentProperty(USE_BOXED_TYPE) : valueTypeArgumentProperty(RETURN_NULL_ON_NULL)).collect(toImmutableList());
    if (type instanceof UnknownType) {
        return new BuiltInScalarFunctionImplementation(false, argumentProperties, methodHandleStack.bindTo(null), Optional.empty());
    } else {
        try {
            MethodHandle cast = functionAndTypeManager.getJavaScalarFunctionImplementation(functionAndTypeManager.lookupCast(CAST, type.getTypeSignature(), VARCHAR_TYPE_SIGNATURE)).getMethodHandle();
            MethodHandle getter;
            Class<?> elementType = type.getJavaType();
            if (elementType == boolean.class) {
                getter = GET_BOOLEAN;
            } else if (elementType == double.class) {
                getter = GET_DOUBLE;
            } else if (elementType == long.class) {
                getter = GET_LONG;
            } else if (elementType == Slice.class) {
                getter = GET_SLICE;
            } else {
                throw new UnsupportedOperationException("Unsupported type: " + elementType.getName());
            }
            // if the cast doesn't take a SqlFunctionProperties, create an adapter that drops the provided session
            if (cast.type().parameterArray()[0] != SqlFunctionProperties.class) {
                cast = MethodHandles.dropArguments(cast, 0, SqlFunctionProperties.class);
            }
            // Adapt a target cast that takes (SqlFunctionProperties, ?) to one that takes (Block, int, SqlFunctionProperties), which will be invoked by the implementation
            // The first two arguments (Block, int) are filtered through the element type's getXXX method to produce the underlying value that needs to be passed to
            // the cast.
            cast = MethodHandles.permuteArguments(cast, MethodType.methodType(Slice.class, cast.type().parameterArray()[1], cast.type().parameterArray()[0]), 1, 0);
            cast = MethodHandles.dropArguments(cast, 1, int.class);
            cast = MethodHandles.dropArguments(cast, 1, Block.class);
            cast = MethodHandles.foldArguments(cast, getter.bindTo(type));
            MethodHandle targetStack = MethodHandles.insertArguments(methodHandleStack, 0, cast);
            MethodHandle targetProvidedBlock = MethodHandles.insertArguments(methodHandleProvidedBlock, 0, cast);
            return new BuiltInScalarFunctionImplementation(ImmutableList.of(new ScalarFunctionImplementationChoice(false, argumentProperties, STACK, targetStack, Optional.empty()), new ScalarFunctionImplementationChoice(false, argumentProperties, PROVIDED_BLOCKBUILDER, targetProvidedBlock, Optional.empty())));
        } catch (PrestoException e) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Input type %s not supported", type), e);
        }
    }
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) STACK(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ReturnPlaceConvention.STACK) MethodHandle(java.lang.invoke.MethodHandle) StandardTypes(com.facebook.presto.common.type.StandardTypes) FunctionKind(com.facebook.presto.spi.function.FunctionKind) Slice(io.airlift.slice.Slice) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) GENERIC_INTERNAL_ERROR(com.facebook.presto.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR) PrestoException(com.facebook.presto.spi.PrestoException) TypeSignature(com.facebook.presto.common.type.TypeSignature) ArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty) DEFAULT_NAMESPACE(com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE) Signature.typeVariable(com.facebook.presto.spi.function.Signature.typeVariable) ImmutableList(com.google.common.collect.ImmutableList) Reflection.methodHandle(com.facebook.presto.util.Reflection.methodHandle) Map(java.util.Map) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) INVALID_FUNCTION_ARGUMENT(com.facebook.presto.spi.StandardErrorCode.INVALID_FUNCTION_ARGUMENT) UnknownType(com.facebook.presto.common.type.UnknownType) RETURN_NULL_ON_NULL(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.RETURN_NULL_ON_NULL) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) Type(com.facebook.presto.common.type.Type) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) BoundVariables(com.facebook.presto.metadata.BoundVariables) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) PROVIDED_BLOCKBUILDER(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ReturnPlaceConvention.PROVIDED_BLOCKBUILDER) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) MethodHandles(java.lang.invoke.MethodHandles) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) USE_BOXED_TYPE(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.USE_BOXED_TYPE) String.format(java.lang.String.format) SqlFunctionVisibility(com.facebook.presto.spi.function.SqlFunctionVisibility) ArgumentProperty.valueTypeArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty.valueTypeArgumentProperty) PUBLIC(com.facebook.presto.spi.function.SqlFunctionVisibility.PUBLIC) List(java.util.List) MethodType(java.lang.invoke.MethodType) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) Optional(java.util.Optional) Block(com.facebook.presto.common.block.Block) CAST(com.facebook.presto.metadata.CastType.CAST) ArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty) ArgumentProperty.valueTypeArgumentProperty(com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.ArgumentProperty.valueTypeArgumentProperty) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) PrestoException(com.facebook.presto.spi.PrestoException) UnknownType(com.facebook.presto.common.type.UnknownType) UnknownType(com.facebook.presto.common.type.UnknownType) Type(com.facebook.presto.common.type.Type) MethodType(java.lang.invoke.MethodType) Slice(io.airlift.slice.Slice) Block(com.facebook.presto.common.block.Block) MethodHandle(java.lang.invoke.MethodHandle)

Example 42 with FunctionAndTypeManager

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

the class TestFilterAndProjectOperator method test.

@Test
public void test() {
    List<Page> input = rowPagesBuilder(VARCHAR, BIGINT).addSequencePage(100, 0, 0).build();
    MetadataManager metadata = createTestMetadataManager();
    FunctionAndTypeManager functionAndTypeManager = metadata.getFunctionAndTypeManager();
    RowExpression filter = call(BETWEEN.name(), functionAndTypeManager.resolveOperator(BETWEEN, fromTypes(BIGINT, BIGINT, BIGINT)), BOOLEAN, field(1, BIGINT), constant(10L, BIGINT), constant(19L, BIGINT));
    RowExpression field0 = field(0, VARCHAR);
    RowExpression add5 = call(ADD.name(), functionAndTypeManager.resolveOperator(ADD, fromTypes(BIGINT, BIGINT)), BIGINT, field(1, BIGINT), constant(5L, BIGINT));
    ExpressionCompiler compiler = new ExpressionCompiler(metadata, new PageFunctionCompiler(metadata, 0));
    Supplier<PageProcessor> processor = compiler.compilePageProcessor(TEST_SESSION.getSqlFunctionProperties(), Optional.of(filter), ImmutableList.of(field0, add5));
    OperatorFactory operatorFactory = new FilterAndProjectOperator.FilterAndProjectOperatorFactory(0, new PlanNodeId("test"), processor, ImmutableList.of(VARCHAR, BIGINT), new DataSize(0, BYTE), 0);
    MaterializedResult expected = MaterializedResult.resultBuilder(driverContext.getSession(), VARCHAR, BIGINT).row("10", 15L).row("11", 16L).row("12", 17L).row("13", 18L).row("14", 19L).row("15", 20L).row("16", 21L).row("17", 22L).row("18", 23L).row("19", 24L).build();
    assertOperatorEquals(operatorFactory, driverContext, input, expected);
}
Also used : PageFunctionCompiler(com.facebook.presto.sql.gen.PageFunctionCompiler) RowExpression(com.facebook.presto.spi.relation.RowExpression) Page(com.facebook.presto.common.Page) PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) MetadataManager(com.facebook.presto.metadata.MetadataManager) MetadataManager.createTestMetadataManager(com.facebook.presto.metadata.MetadataManager.createTestMetadataManager) PageProcessor(com.facebook.presto.operator.project.PageProcessor) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) DataSize(io.airlift.units.DataSize) ExpressionCompiler(com.facebook.presto.sql.gen.ExpressionCompiler) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Test(org.testng.annotations.Test)

Example 43 with FunctionAndTypeManager

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

the class TestRowParametricType method testTypeSignatureRoundTrip.

@Test
public void testTypeSignatureRoundTrip() {
    FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
    TypeSignature typeSignature = new TypeSignature(ROW, TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName("col1", false)), new TypeSignature(BIGINT))), TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName("col2", true)), new TypeSignature(DOUBLE))));
    List<TypeParameter> parameters = typeSignature.getParameters().stream().map(parameter -> TypeParameter.of(parameter, functionAndTypeManager)).collect(Collectors.toList());
    Type rowType = RowParametricType.ROW.createType(parameters);
    assertEquals(rowType.getTypeSignature(), typeSignature);
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) Assert.assertEquals(com.facebook.presto.testing.assertions.Assert.assertEquals) Test(org.testng.annotations.Test) ROW(com.facebook.presto.common.type.StandardTypes.ROW) Collectors(java.util.stream.Collectors) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) TypeSignature(com.facebook.presto.common.type.TypeSignature) DOUBLE(com.facebook.presto.common.type.StandardTypes.DOUBLE) List(java.util.List) Optional(java.util.Optional) RowFieldName(com.facebook.presto.common.type.RowFieldName) TypeParameter(com.facebook.presto.common.type.TypeParameter) BIGINT(com.facebook.presto.common.type.StandardTypes.BIGINT) Type(com.facebook.presto.common.type.Type) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeParameter(com.facebook.presto.common.type.TypeParameter) Type(com.facebook.presto.common.type.Type) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) RowFieldName(com.facebook.presto.common.type.RowFieldName) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) Test(org.testng.annotations.Test)

Example 44 with FunctionAndTypeManager

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

the class TestFunctionType method testDisplayName.

@Test
public void testDisplayName() {
    FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
    Type function = functionAndTypeManager.getType(TypeSignature.parseTypeSignature("function<row(field double),bigint>"));
    assertEquals(function.getDisplayName(), "function<row(\"field\" double),bigint>");
}
Also used : Type(com.facebook.presto.common.type.Type) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) Test(org.testng.annotations.Test)

Example 45 with FunctionAndTypeManager

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

the class TestPlanRemoteProjections method setup.

@BeforeClass
public void setup() {
    tester = new RuleTester(ImmutableList.of(), ImmutableMap.of("remote_functions_enabled", "true"));
    FunctionAndTypeManager functionAndTypeManager = getFunctionAndTypeManager();
    functionAndTypeManager.addFunctionNamespace("unittest", new InMemoryFunctionNamespaceManager("unittest", new SqlFunctionExecutors(ImmutableMap.of(SQL, FunctionImplementationType.SQL, JAVA, THRIFT), new NoopSqlFunctionExecutor()), new SqlInvokedFunctionNamespaceManagerConfig().setSupportedFunctionLanguages("sql,java")));
    functionAndTypeManager.createFunction(FUNCTION_REMOTE_FOO_0, true);
    functionAndTypeManager.createFunction(FUNCTION_REMOTE_FOO_1, true);
    functionAndTypeManager.createFunction(FUNCTION_REMOTE_FOO_2, true);
    functionAndTypeManager.createFunction(FUNCTION_REMOTE_FOO_3, true);
}
Also used : FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) NoopSqlFunctionExecutor(com.facebook.presto.functionNamespace.execution.NoopSqlFunctionExecutor) RuleTester(com.facebook.presto.sql.planner.iterative.rule.test.RuleTester) SqlFunctionExecutors(com.facebook.presto.functionNamespace.execution.SqlFunctionExecutors) InMemoryFunctionNamespaceManager(com.facebook.presto.functionNamespace.testing.InMemoryFunctionNamespaceManager) SqlInvokedFunctionNamespaceManagerConfig(com.facebook.presto.functionNamespace.SqlInvokedFunctionNamespaceManagerConfig) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)62 Test (org.testng.annotations.Test)31 FunctionAndTypeManager.createTestFunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager)26 Type (com.facebook.presto.common.type.Type)24 RowExpression (com.facebook.presto.spi.relation.RowExpression)13 ImmutableList (com.google.common.collect.ImmutableList)13 FunctionHandle (com.facebook.presto.spi.function.FunctionHandle)12 CallExpression (com.facebook.presto.spi.relation.CallExpression)12 Block (com.facebook.presto.common.block.Block)11 ArrayType (com.facebook.presto.common.type.ArrayType)11 Optional (java.util.Optional)11 BeforeClass (org.testng.annotations.BeforeClass)11 Page (com.facebook.presto.common.Page)10 PlanNodeId (com.facebook.presto.spi.plan.PlanNodeId)10 List (java.util.List)10 DBI (org.skife.jdbi.v2.DBI)8 Bootstrap (com.facebook.airlift.bootstrap.Bootstrap)6 JsonBinder.jsonBinder (com.facebook.airlift.json.JsonBinder.jsonBinder)6 JsonCodecBinder.jsonCodecBinder (com.facebook.airlift.json.JsonCodecBinder.jsonCodecBinder)6 JsonModule (com.facebook.airlift.json.JsonModule)6