Search in sources :

Example 11 with SqlInvokedFunction

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

the class TestSqlInvokedFunctionNamespaceManager method testCaching.

@Test
public void testCaching() {
    InMemoryFunctionNamespaceManager functionNamespaceManager = createFunctionNamespaceManager();
    functionNamespaceManager.createFunction(FUNCTION_POWER_TOWER_DOUBLE, false);
    // fetchFunctionsDirect does not produce the same function reference
    SqlInvokedFunction function1 = getOnlyElement(functionNamespaceManager.fetchFunctionsDirect(POWER_TOWER));
    SqlInvokedFunction function2 = getOnlyElement(functionNamespaceManager.fetchFunctionsDirect(POWER_TOWER));
    assertEquals(function1, function2);
    assertNotSame(function1, function2);
    // fetchFunctionMetadataDirect does not produce the same metadata reference
    FunctionMetadata metadata1 = functionNamespaceManager.fetchFunctionMetadataDirect(function1.getRequiredFunctionHandle());
    FunctionMetadata metadata2 = functionNamespaceManager.fetchFunctionMetadataDirect(function2.getRequiredFunctionHandle());
    assertEquals(metadata1, metadata2);
    assertNotSame(metadata1, metadata2);
    // getFunctionMetadata produces the same metadata reference
    metadata1 = functionNamespaceManager.getFunctionMetadata(function1.getRequiredFunctionHandle());
    metadata2 = functionNamespaceManager.getFunctionMetadata(function2.getRequiredFunctionHandle());
    assertSame(metadata1, metadata2);
    // getFunctions produces the same function collection reference
    functionNamespaceManager.createFunction(FUNCTION_POWER_TOWER_INT, false);
    FunctionNamespaceTransactionHandle transaction1 = functionNamespaceManager.beginTransaction();
    FunctionNamespaceTransactionHandle transaction2 = functionNamespaceManager.beginTransaction();
    Collection<SqlInvokedFunction> functions1 = functionNamespaceManager.getFunctions(Optional.of(transaction1), POWER_TOWER);
    Collection<SqlInvokedFunction> functions2 = functionNamespaceManager.getFunctions(Optional.of(transaction2), POWER_TOWER);
    assertEquals(functions1.size(), 2);
    assertSame(functions1, functions2);
}
Also used : FunctionNamespaceTransactionHandle(com.facebook.presto.spi.function.FunctionNamespaceTransactionHandle) FunctionMetadata(com.facebook.presto.spi.function.FunctionMetadata) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) InMemoryFunctionNamespaceManager(com.facebook.presto.functionNamespace.testing.InMemoryFunctionNamespaceManager) Test(org.testng.annotations.Test)

Example 12 with SqlInvokedFunction

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

the class InMemoryFunctionNamespaceManager method createFunction.

@Override
public synchronized void createFunction(SqlInvokedFunction function, boolean replace) {
    checkFunctionLanguageSupported(function);
    SqlFunctionId functionId = function.getFunctionId();
    if (!replace && latestFunctions.containsKey(function.getFunctionId())) {
        throw new PrestoException(GENERIC_USER_ERROR, format("Function '%s' already exists", functionId.getId()));
    }
    SqlInvokedFunction replacedFunction = latestFunctions.get(functionId);
    long version = 1;
    if (replacedFunction != null) {
        version = parseLong(replacedFunction.getRequiredVersion()) + 1;
    }
    latestFunctions.put(functionId, function.withVersion(String.valueOf(version)));
}
Also used : SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) PrestoException(com.facebook.presto.spi.PrestoException)

Example 13 with SqlInvokedFunction

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

the class SqlInvokedFunctionRowMapper method map.

@Override
public SqlInvokedFunction map(ResultSet rs, StatementContext ctx) throws SQLException {
    String catalog = rs.getString("catalog_name");
    String schema = rs.getString("schema_name");
    String functionName = rs.getString("function_name");
    List<Parameter> parameters = PARAMETERS_CODEC.fromJson(rs.getString("parameters"));
    String returnType = rs.getString("return_type");
    String description = rs.getString("description");
    RoutineCharacteristics routineCharacteristics = ROUTINE_CHARACTERISTICS_CODEC.fromJson(rs.getString("routine_characteristics"));
    String body = rs.getString("body");
    String version = String.valueOf(rs.getLong("version"));
    return new SqlInvokedFunction(QualifiedObjectName.valueOf(catalog, schema, functionName), parameters, parseTypeSignature(returnType), description, routineCharacteristics, body, withVersion(version));
}
Also used : RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) Parameter(com.facebook.presto.spi.function.Parameter)

Example 14 with SqlInvokedFunction

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

the class SqlInvokedScalarFromAnnotationsParser method createSqlInvokedFunctions.

private static List<SqlInvokedFunction> createSqlInvokedFunctions(Method method, Optional<SqlInvokedScalarFunction> header, Optional<String> description) {
    SqlInvokedScalarFunction functionHeader = header.orElse(method.getAnnotation(SqlInvokedScalarFunction.class));
    String functionDescription = description.orElse(method.isAnnotationPresent(Description.class) ? method.getAnnotation(Description.class).value() : "");
    TypeSignature returnType = parseTypeSignature(method.getAnnotation(SqlType.class).value());
    // Parameter
    checkCondition(!method.isAnnotationPresent(SqlParameter.class) || !method.isAnnotationPresent(SqlParameters.class), FUNCTION_IMPLEMENTATION_ERROR, "Function-defining method [%s] is annotated with both @SqlParameter and @SqlParameters", method);
    List<Parameter> parameters;
    if (method.isAnnotationPresent(SqlParameter.class)) {
        parameters = ImmutableList.of(getParameterFromAnnotation(method.getAnnotation(SqlParameter.class)));
    } else if (method.isAnnotationPresent(SqlParameters.class)) {
        parameters = stream(method.getAnnotation(SqlParameters.class).value()).map(SqlInvokedScalarFromAnnotationsParser::getParameterFromAnnotation).collect(toImmutableList());
    } else {
        parameters = ImmutableList.of();
    }
    // Routine characteristics
    RoutineCharacteristics routineCharacteristics = RoutineCharacteristics.builder().setLanguage(RoutineCharacteristics.Language.SQL).setDeterminism(functionHeader.deterministic() ? DETERMINISTIC : NOT_DETERMINISTIC).setNullCallClause(functionHeader.calledOnNullInput() ? CALLED_ON_NULL_INPUT : RETURNS_NULL_ON_NULL_INPUT).build();
    String body;
    try {
        body = (String) method.invoke(null);
    } catch (ReflectiveOperationException e) {
        throw new PrestoException(FUNCTION_IMPLEMENTATION_ERROR, format("Failed to get function body for method [%s]", method), e);
    }
    return Stream.concat(Stream.of(functionHeader.value()), stream(functionHeader.alias())).map(name -> new SqlInvokedFunction(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, name), parameters, returnType, functionDescription, routineCharacteristics, body, notVersioned())).collect(toImmutableList());
}
Also used : Parameter(com.facebook.presto.spi.function.Parameter) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) PrestoException(com.facebook.presto.spi.PrestoException) TypeSignature(com.facebook.presto.common.type.TypeSignature) SqlParameters(com.facebook.presto.spi.function.SqlParameters) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) CodegenScalarFunction(com.facebook.presto.spi.function.CodegenScalarFunction) DEFAULT_NAMESPACE(com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) ImmutableList(com.google.common.collect.ImmutableList) Description(com.facebook.presto.spi.function.Description) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) Method(java.lang.reflect.Method) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) SqlInvokedScalarFunction(com.facebook.presto.spi.function.SqlInvokedScalarFunction) FUNCTION_IMPLEMENTATION_ERROR(com.facebook.presto.spi.StandardErrorCode.FUNCTION_IMPLEMENTATION_ERROR) CALLED_ON_NULL_INPUT(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.CALLED_ON_NULL_INPUT) Failures.checkCondition(com.facebook.presto.util.Failures.checkCondition) FunctionsParserHelper.findPublicStaticMethods(com.facebook.presto.operator.annotations.FunctionsParserHelper.findPublicStaticMethods) SqlParameter(com.facebook.presto.spi.function.SqlParameter) ImmutableSet(com.google.common.collect.ImmutableSet) NOT_DETERMINISTIC(com.facebook.presto.spi.function.RoutineCharacteristics.Determinism.NOT_DETERMINISTIC) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DETERMINISTIC(com.facebook.presto.spi.function.RoutineCharacteristics.Determinism.DETERMINISTIC) Set(java.util.Set) String.format(java.lang.String.format) List(java.util.List) Stream(java.util.stream.Stream) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) RETURNS_NULL_ON_NULL_INPUT(com.facebook.presto.spi.function.RoutineCharacteristics.NullCallClause.RETURNS_NULL_ON_NULL_INPUT) FunctionVersion.notVersioned(com.facebook.presto.spi.function.FunctionVersion.notVersioned) Optional(java.util.Optional) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Arrays.stream(java.util.Arrays.stream) SqlType(com.facebook.presto.spi.function.SqlType) Description(com.facebook.presto.spi.function.Description) SqlParameter(com.facebook.presto.spi.function.SqlParameter) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) PrestoException(com.facebook.presto.spi.PrestoException) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) SqlParameters(com.facebook.presto.spi.function.SqlParameters) RoutineCharacteristics(com.facebook.presto.spi.function.RoutineCharacteristics) SqlInvokedScalarFunction(com.facebook.presto.spi.function.SqlInvokedScalarFunction) Parameter(com.facebook.presto.spi.function.Parameter) SqlParameter(com.facebook.presto.spi.function.SqlParameter)

Example 15 with SqlInvokedFunction

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

the class CreateFunctionTask method execute.

@Override
public ListenableFuture<?> execute(CreateFunction statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.empty(), parameters, warningCollector);
    Analysis analysis = analyzer.analyze(statement);
    if (analysis.getFunctionHandles().values().stream().anyMatch(SqlFunctionHandle.class::isInstance)) {
        throw new PrestoException(NOT_SUPPORTED, "Invoking a dynamically registered function in SQL function body is not supported");
    }
    SqlInvokedFunction function = createSqlInvokedFunction(statement, metadata, analysis);
    if (statement.isTemporary()) {
        addSessionFunction(session, new SqlFunctionId(function.getSignature().getName(), function.getSignature().getArgumentTypes()), function);
    } else {
        metadata.getFunctionAndTypeManager().createFunction(function, statement.isReplace());
    }
    return immediateFuture(null);
}
Also used : SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) Analysis(com.facebook.presto.sql.analyzer.Analysis) SqlInvokedFunction(com.facebook.presto.spi.function.SqlInvokedFunction) SqlFunctionHandle(com.facebook.presto.spi.function.SqlFunctionHandle) PrestoException(com.facebook.presto.spi.PrestoException) Analyzer(com.facebook.presto.sql.analyzer.Analyzer)

Aggregations

SqlInvokedFunction (com.facebook.presto.spi.function.SqlInvokedFunction)22 SqlFunctionId (com.facebook.presto.spi.function.SqlFunctionId)12 PrestoException (com.facebook.presto.spi.PrestoException)10 List (java.util.List)8 Map (java.util.Map)8 Parameter (com.facebook.presto.spi.function.Parameter)7 ImmutableList (com.google.common.collect.ImmutableList)7 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)7 Optional (java.util.Optional)7 PRIVATE (com.facebook.presto.bytecode.Access.PRIVATE)4 PUBLIC (com.facebook.presto.bytecode.Access.PUBLIC)4 Access.a (com.facebook.presto.bytecode.Access.a)4 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)4 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)4 CallSiteBinder (com.facebook.presto.bytecode.CallSiteBinder)4 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)4 FieldDefinition (com.facebook.presto.bytecode.FieldDefinition)4 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)4 Parameter (com.facebook.presto.bytecode.Parameter)4 Parameter.arg (com.facebook.presto.bytecode.Parameter.arg)4