Search in sources :

Example 1 with Parameter

use of io.prestosql.spi.function.Parameter in project hetu-core by openlookeng.

the class FunctionNamespaceTestCommon method testGetFunction.

/**
 * test get function
 */
public void testGetFunction(AbstractSqlInvokedFunctionNamespaceManager functionNamespaceManager) {
    QualifiedObjectName powerTower = QualifiedObjectName.valueOf(new CatalogSchemaName(this.catalogName, funcNsSchemaName), functionName);
    List<Parameter> parameters = ImmutableList.of(new Parameter("x", parseTypeSignature(DOUBLE)));
    List<Parameter> parameters1 = ImmutableList.of(new Parameter("y", parseTypeSignature(INTEGER)), new Parameter("x", parseTypeSignature(DOUBLE)));
    String funcDesc = "power tower test";
    String funcBody = "RETURN pow(x, x)";
    Map<String, String> funcProperties = ImmutableMap.of("executor", "mysql");
    SqlInvokedFunction function = new SqlInvokedFunction(powerTower, parameters, parseTypeSignature(DOUBLE), funcDesc, RoutineCharacteristics.builder().setDeterminism(DETERMINISTIC).setLanguage(JDBC).build(), funcBody, funcProperties, Optional.empty());
    SqlInvokedFunction function1 = new SqlInvokedFunction(powerTower, parameters1, parseTypeSignature(DOUBLE), funcDesc, RoutineCharacteristics.builder().setDeterminism(DETERMINISTIC).setLanguage(JDBC).build(), funcBody, funcProperties, Optional.empty());
    try {
        functionNamespaceManager.createFunction(function, false);
        functionNamespaceManager.createFunction(function1, false);
    } catch (PrestoException e) {
        fail(format("Create function(id=%s) failed.", function.getFunctionId().toString()));
    }
    if ((functionNamespaceManager instanceof InMemoryFunctionNamespaceManager)) {
        // trans to InMemoryFunctionNamespaceManager obj for sub classes method test
        InMemoryFunctionNamespaceManager manager = (InMemoryFunctionNamespaceManager) functionNamespaceManager;
        List<SqlInvokedFunction> storedFunc = manager.listFunctions();
        assertTrue(storedFunc.size() == 2);
        storedFunc = manager.fetchFunctionsDirect(powerTower);
        assertTrue(storedFunc.size() == 2);
        Optional<SqlInvokedFunction> singleFunc = manager.fetchFunctionsDirect(powerTower, parameters.stream().map(x -> x.getType()).collect(Collectors.toList()));
        assertTrue(singleFunc.isPresent());
        FunctionMetadata funcMeta = manager.fetchFunctionMetadataDirect(singleFunc.get().getFunctionHandle().get());
        assertEquals(funcMeta.getName(), powerTower);
    }
}
Also used : FunctionMetadata(io.prestosql.spi.function.FunctionMetadata) CatalogSchemaName(io.prestosql.spi.connector.CatalogSchemaName) SqlInvokedFunction(io.prestosql.spi.function.SqlInvokedFunction) Parameter(io.prestosql.spi.function.Parameter) PrestoException(io.prestosql.spi.PrestoException) InMemoryFunctionNamespaceManager(io.hetu.core.plugin.functionnamespace.memory.InMemoryFunctionNamespaceManager) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName)

Example 2 with Parameter

use of io.prestosql.spi.function.Parameter in project hetu-core by openlookeng.

the class TestInMemoryManager method testCreateAndGetFunction.

/**
 * test get function
 */
@Test
public void testCreateAndGetFunction() {
    QualifiedObjectName powerTower = QualifiedObjectName.valueOf(new CatalogSchemaName(this.catalogName, funcNsSchemaName), functionName);
    List<Parameter> parameters = ImmutableList.of(new Parameter("x", parseTypeSignature(DOUBLE)));
    List<Parameter> parameters1 = ImmutableList.of(new Parameter("y", parseTypeSignature(INTEGER)), new Parameter("x", parseTypeSignature(DOUBLE)));
    testProc.createFunction(functionNamespaceManager, functionName, parameters);
    testProc.createFunction(functionNamespaceManager, functionName, parameters1);
    List<SqlInvokedFunction> storedFunc = functionNamespaceManager.listFunctions();
    assertTrue(storedFunc.size() == 2);
    storedFunc = functionNamespaceManager.fetchFunctionsDirect(powerTower);
    assertTrue(storedFunc.size() == 2);
    Optional<SqlInvokedFunction> singleFunc = functionNamespaceManager.fetchFunctionsDirect(powerTower, parameters.stream().map(x -> x.getType()).collect(Collectors.toList()));
    assertTrue(singleFunc.isPresent());
    FunctionMetadata funcMeta = functionNamespaceManager.fetchFunctionMetadataDirect(singleFunc.get().getFunctionHandle().get());
    assertEquals(funcMeta.getName(), powerTower);
}
Also used : FunctionMetadata(io.prestosql.spi.function.FunctionMetadata) CatalogSchemaName(io.prestosql.spi.connector.CatalogSchemaName) SqlInvokedFunction(io.prestosql.spi.function.SqlInvokedFunction) Parameter(io.prestosql.spi.function.Parameter) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) Test(org.testng.annotations.Test)

Example 3 with Parameter

use of io.prestosql.spi.function.Parameter in project hetu-core by openlookeng.

the class ExternalFunctionsParser method parseExternalFunction.

public static Optional<SqlInvokedFunction> parseExternalFunction(ExternalFunctionInfo externalFunctionInfo, CatalogSchemaName catalogSchemaName, RoutineCharacteristics.Language language) {
    Optional<String> functionName = externalFunctionInfo.getFunctionName();
    Optional<String> description = externalFunctionInfo.getDescription();
    List<String> inputArgs = externalFunctionInfo.getInputArgs();
    Optional<String> returnType = externalFunctionInfo.getReturnType();
    boolean deterministic = externalFunctionInfo.isDeterministic();
    boolean calledOnNullInput = externalFunctionInfo.isCalledOnNullInput();
    if (functionName.isPresent() && returnType.isPresent()) {
        QualifiedObjectName qualifiedObjectName = new QualifiedObjectName(catalogSchemaName.getCatalogName(), catalogSchemaName.getSchemaName(), functionName.get());
        List<Parameter> parameters = inputArgs.stream().map(str -> {
            checkState(SUPPORTED_TYPE.contains(str), format("external function do not supported type: %s", str));
            if (str.equals(StandardTypes.DECIMAL)) {
                return new Parameter(getRandomString((inputArgs.size() / ALPHABET.length() + 1), ALPHABET), parseTypeSignature(str + "(p, s)", ImmutableSet.of("p", "s")));
            } else if (str.equals(StandardTypes.CHAR) || str.equals(StandardTypes.VARCHAR)) {
                return new Parameter(getRandomString((inputArgs.size() / ALPHABET.length() + 1), ALPHABET), parseTypeSignature(str + "(x)", ImmutableSet.of("x")));
            } else {
                return new Parameter(getRandomString((inputArgs.size() / ALPHABET.length() + 1), ALPHABET), parseTypeSignature(str));
            }
        }).collect(toImmutableList());
        TypeSignature reType = parseTypeSignature(returnType.get());
        String deter = deterministic ? "DETERMINISTIC" : "NOT_DETERMINISTIC";
        String nullCallClause = calledOnNullInput ? "CALLED_ON_NULL_INPUT" : "RETURNS_NULL_ON_NULL_INPUT";
        RoutineCharacteristics routineCharacteristics = RoutineCharacteristics.builder().setLanguage(new RoutineCharacteristics.Language(language.getLanguage())).setDeterminism(RoutineCharacteristics.Determinism.valueOf(deter)).setNullCallClause(RoutineCharacteristics.NullCallClause.valueOf(nullCallClause)).build();
        SqlInvokedFunction sqlInvokedFunction = new SqlInvokedFunction(qualifiedObjectName, parameters, reType, description.orElse(""), routineCharacteristics, EXTERNAL_FUNCTION_BODY, ImmutableMap.of(), Optional.empty());
        return Optional.of(sqlInvokedFunction);
    }
    return Optional.empty();
}
Also used : RoutineCharacteristics(io.prestosql.spi.function.RoutineCharacteristics) ImmutableSet(com.google.common.collect.ImmutableSet) StandardTypes(io.prestosql.spi.type.StandardTypes) ImmutableMap(com.google.common.collect.ImmutableMap) Parameter(io.prestosql.spi.function.Parameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) ExternalFunctionInfo(io.prestosql.spi.function.ExternalFunctionInfo) SqlInvokedFunction(io.prestosql.spi.function.SqlInvokedFunction) String.format(java.lang.String.format) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) Preconditions.checkState(com.google.common.base.Preconditions.checkState) SecureRandom(java.security.SecureRandom) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) List(java.util.List) Optional(java.util.Optional) TypeSignature(io.prestosql.spi.type.TypeSignature) CatalogSchemaName(io.prestosql.spi.connector.CatalogSchemaName) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) RoutineCharacteristics(io.prestosql.spi.function.RoutineCharacteristics) SqlInvokedFunction(io.prestosql.spi.function.SqlInvokedFunction) Parameter(io.prestosql.spi.function.Parameter) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName)

Example 4 with Parameter

use of io.prestosql.spi.function.Parameter in project hetu-core by openlookeng.

the class InMemoryFunctionNamespaceManager method fetchFunctionMetadataDirect.

@Override
public FunctionMetadata fetchFunctionMetadataDirect(SqlFunctionHandle functionHandle) {
    List<SqlInvokedFunction> sqlInvokedFunctions = fetchFunctionsDirect(functionHandle.getFunctionId().getFunctionName());
    List<SqlInvokedFunction> listMatchedWithoutBound = sqlInvokedFunctions.stream().filter(function -> isSqlFunctionIdEqualsWithoutBound(functionHandle.getFunctionId(), function.getFunctionId())).collect(Collectors.toList());
    List<SqlInvokedFunction> listMatchedWithBound = sqlInvokedFunctions.stream().filter(function -> function.getRequiredFunctionHandle().equals(functionHandle)).collect(Collectors.toList());
    if ((listMatchedWithoutBound.size() != 0) && (listMatchedWithBound.size() != listMatchedWithoutBound.size())) {
        SqlInvokedFunction sqlInvokedFunctionElect = listMatchedWithoutBound.get(0);
        return new FunctionMetadata(sqlInvokedFunctionElect.getSignature().getName(), functionHandle.getFunctionId().getArgumentTypes(), sqlInvokedFunctionElect.getParameters().stream().map(Parameter::getName).collect(toImmutableList()), sqlInvokedFunctionElect.getSignature().getReturnType(), SCALAR, sqlInvokedFunctionElect.getRoutineCharacteristics().getLanguage(), getFunctionImplementationType(sqlInvokedFunctionElect), sqlInvokedFunctionElect.isDeterministic(), sqlInvokedFunctionElect.isCalledOnNullInput());
    }
    return fetchFunctionsDirect(functionHandle.getFunctionId().getFunctionName()).stream().filter(function -> function.getRequiredFunctionHandle().equals(functionHandle)).map(this::sqlInvokedFunctionToMetadata).collect(onlyElement());
}
Also used : Parameter(io.prestosql.spi.function.Parameter) SCALAR(io.prestosql.spi.function.FunctionKind.SCALAR) SqlFunctionId(io.prestosql.spi.function.SqlFunctionId) SqlInvokedFunction(io.prestosql.spi.function.SqlInvokedFunction) MoreCollectors.onlyElement(com.google.common.collect.MoreCollectors.onlyElement) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) SqlInvokedFunctionNamespaceManagerConfig(io.hetu.core.plugin.functionnamespace.SqlInvokedFunctionNamespaceManagerConfig) SqlFunctionHandle(io.prestosql.spi.function.SqlFunctionHandle) Map(java.util.Map) FunctionMetadata(io.prestosql.spi.function.FunctionMetadata) ONLY_VERSION(io.hetu.core.plugin.functionnamespace.FunctionNameSpaceConstants.ONLY_VERSION) AbstractSqlInvokedFunctionNamespaceManager(io.hetu.core.plugin.functionnamespace.AbstractSqlInvokedFunctionNamespaceManager) PrestoException(io.prestosql.spi.PrestoException) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ThreadSafe(javax.annotation.concurrent.ThreadSafe) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) List(java.util.List) GENERIC_USER_ERROR(io.prestosql.spi.StandardErrorCode.GENERIC_USER_ERROR) ScalarFunctionImplementation(io.prestosql.spi.function.ScalarFunctionImplementation) ServingCatalog(io.hetu.core.plugin.functionnamespace.ServingCatalog) Optional(java.util.Optional) TypeSignature(io.prestosql.spi.type.TypeSignature) FunctionMetadata(io.prestosql.spi.function.FunctionMetadata) SqlInvokedFunction(io.prestosql.spi.function.SqlInvokedFunction) Parameter(io.prestosql.spi.function.Parameter)

Aggregations

QualifiedObjectName (io.prestosql.spi.connector.QualifiedObjectName)4 Parameter (io.prestosql.spi.function.Parameter)4 SqlInvokedFunction (io.prestosql.spi.function.SqlInvokedFunction)4 CatalogSchemaName (io.prestosql.spi.connector.CatalogSchemaName)3 FunctionMetadata (io.prestosql.spi.function.FunctionMetadata)3 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 PrestoException (io.prestosql.spi.PrestoException)2 TypeSignature (io.prestosql.spi.type.TypeSignature)2 String.format (java.lang.String.format)2 List (java.util.List)2 Optional (java.util.Optional)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 MoreCollectors.onlyElement (com.google.common.collect.MoreCollectors.onlyElement)1 AbstractSqlInvokedFunctionNamespaceManager (io.hetu.core.plugin.functionnamespace.AbstractSqlInvokedFunctionNamespaceManager)1 ONLY_VERSION (io.hetu.core.plugin.functionnamespace.FunctionNameSpaceConstants.ONLY_VERSION)1 ServingCatalog (io.hetu.core.plugin.functionnamespace.ServingCatalog)1 SqlInvokedFunctionNamespaceManagerConfig (io.hetu.core.plugin.functionnamespace.SqlInvokedFunctionNamespaceManagerConfig)1 InMemoryFunctionNamespaceManager (io.hetu.core.plugin.functionnamespace.memory.InMemoryFunctionNamespaceManager)1