Search in sources :

Example 1 with ExternalFunctionInfo

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

the class TestMySqlRegisterRemoteUdf method testDefaults.

@Test
public void testDefaults() {
    MysqlExternalFunctionHub externalFunctionHub = new MysqlExternalFunctionHub(new BaseJdbcConfig());
    Set<ExternalFunctionInfo> externalFunctionInfo = externalFunctionHub.getExternalFunctions();
    assertTrue(externalFunctionInfo.size() > 0);
    List<ExternalFunctionInfo> functionList = externalFunctionInfo.stream().filter(x -> x.getFunctionName().get().equals("lower")).collect(Collectors.toList());
    assertEquals(functionList.size(), 1);
    ExternalFunctionInfo functionInfo = functionList.get(0);
    assertEquals(functionInfo.getInputArgs().get(0), StandardTypes.VARCHAR);
    assertEquals(functionInfo.getReturnType().get(), StandardTypes.VARCHAR);
    assertTrue(functionInfo.isDeterministic());
    assertFalse(functionInfo.isCalledOnNullInput());
}
Also used : List(java.util.List) StandardTypes(io.prestosql.spi.type.StandardTypes) MysqlExternalFunctionHub(io.prestosql.plugin.mysql.optimization.function.MysqlExternalFunctionHub) BaseJdbcConfig(io.prestosql.plugin.jdbc.BaseJdbcConfig) Set(java.util.Set) Assert.assertEquals(org.testng.Assert.assertEquals) Assert.assertTrue(org.testng.Assert.assertTrue) Test(org.testng.annotations.Test) ExternalFunctionInfo(io.prestosql.spi.function.ExternalFunctionInfo) Collectors(java.util.stream.Collectors) Assert.assertFalse(org.testng.Assert.assertFalse) MysqlExternalFunctionHub(io.prestosql.plugin.mysql.optimization.function.MysqlExternalFunctionHub) ExternalFunctionInfo(io.prestosql.spi.function.ExternalFunctionInfo) BaseJdbcConfig(io.prestosql.plugin.jdbc.BaseJdbcConfig) Test(org.testng.annotations.Test)

Example 2 with ExternalFunctionInfo

use of io.prestosql.spi.function.ExternalFunctionInfo 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 3 with ExternalFunctionInfo

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

the class FunctionExtractor method extractExternalFunctions.

public static Set<SqlInvokedFunction> extractExternalFunctions(ExternalFunctionHub externalFunctionHub) {
    RoutineCharacteristics.Language language = externalFunctionHub.getExternalFunctionLanguage();
    Optional<CatalogSchemaName> catalogSchemaName = externalFunctionHub.getExternalFunctionCatalogSchemaName();
    if (!catalogSchemaName.isPresent()) {
        return Collections.emptySet();
    }
    ImmutableSet.Builder<SqlInvokedFunction> builder = new ImmutableSet.Builder<>();
    for (ExternalFunctionInfo externalFunctionInfo : externalFunctionHub.getExternalFunctions()) {
        Optional<SqlInvokedFunction> sqlInvokedFunctionOptional = ExternalFunctionsParser.parseExternalFunction(externalFunctionInfo, catalogSchemaName.get(), language);
        sqlInvokedFunctionOptional.ifPresent(builder::add);
    }
    return builder.build();
}
Also used : RoutineCharacteristics(io.prestosql.spi.function.RoutineCharacteristics) ImmutableSet(com.google.common.collect.ImmutableSet) CatalogSchemaName(io.prestosql.spi.connector.CatalogSchemaName) SqlInvokedFunction(io.prestosql.spi.function.SqlInvokedFunction) ExternalFunctionInfo(io.prestosql.spi.function.ExternalFunctionInfo)

Example 4 with ExternalFunctionInfo

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

the class TestMySqlRegisterRemoteUdf method testMultiParamsFunctions.

// test func defination with more than one params
@Test
public void testMultiParamsFunctions() {
    MysqlExternalFunctionHub externalFunctionHub = new MysqlExternalFunctionHub(new BaseJdbcConfig());
    Set<ExternalFunctionInfo> externalFunctionInfo = externalFunctionHub.getExternalFunctions();
    assertTrue(externalFunctionInfo.size() > 0);
    List<ExternalFunctionInfo> functionList = externalFunctionInfo.stream().filter(x -> x.getFunctionName().get().equals("abs")).collect(Collectors.toList());
    assertEquals(functionList.size(), 2);
}
Also used : List(java.util.List) StandardTypes(io.prestosql.spi.type.StandardTypes) MysqlExternalFunctionHub(io.prestosql.plugin.mysql.optimization.function.MysqlExternalFunctionHub) BaseJdbcConfig(io.prestosql.plugin.jdbc.BaseJdbcConfig) Set(java.util.Set) Assert.assertEquals(org.testng.Assert.assertEquals) Assert.assertTrue(org.testng.Assert.assertTrue) Test(org.testng.annotations.Test) ExternalFunctionInfo(io.prestosql.spi.function.ExternalFunctionInfo) Collectors(java.util.stream.Collectors) Assert.assertFalse(org.testng.Assert.assertFalse) MysqlExternalFunctionHub(io.prestosql.plugin.mysql.optimization.function.MysqlExternalFunctionHub) ExternalFunctionInfo(io.prestosql.spi.function.ExternalFunctionInfo) BaseJdbcConfig(io.prestosql.plugin.jdbc.BaseJdbcConfig) Test(org.testng.annotations.Test)

Example 5 with ExternalFunctionInfo

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

the class TestMySqlRegisterRemoteUdf method testOverloadFunctions.

// test over load function register
@Test
public void testOverloadFunctions() {
    MysqlExternalFunctionHub externalFunctionHub = new MysqlExternalFunctionHub(new BaseJdbcConfig());
    Set<ExternalFunctionInfo> externalFunctionInfo = externalFunctionHub.getExternalFunctions();
    assertTrue(externalFunctionInfo.size() > 0);
    List<ExternalFunctionInfo> functionList = externalFunctionInfo.stream().filter(x -> x.getFunctionName().get().equals("timestamp")).collect(Collectors.toList());
    assertEquals(functionList.size(), 1);
    ExternalFunctionInfo functionInfo = functionList.get(0);
    List<String> inputArgs = functionInfo.getInputArgs();
    assertEquals(inputArgs.size(), 2);
    assertEquals(inputArgs.get(0), StandardTypes.TIMESTAMP);
    assertEquals(inputArgs.get(1), StandardTypes.TIME);
    assertEquals(functionInfo.getReturnType().get(), StandardTypes.TIMESTAMP);
    assertTrue(functionInfo.isDeterministic());
    assertFalse(functionInfo.isCalledOnNullInput());
}
Also used : List(java.util.List) StandardTypes(io.prestosql.spi.type.StandardTypes) MysqlExternalFunctionHub(io.prestosql.plugin.mysql.optimization.function.MysqlExternalFunctionHub) BaseJdbcConfig(io.prestosql.plugin.jdbc.BaseJdbcConfig) Set(java.util.Set) Assert.assertEquals(org.testng.Assert.assertEquals) Assert.assertTrue(org.testng.Assert.assertTrue) Test(org.testng.annotations.Test) ExternalFunctionInfo(io.prestosql.spi.function.ExternalFunctionInfo) Collectors(java.util.stream.Collectors) Assert.assertFalse(org.testng.Assert.assertFalse) MysqlExternalFunctionHub(io.prestosql.plugin.mysql.optimization.function.MysqlExternalFunctionHub) ExternalFunctionInfo(io.prestosql.spi.function.ExternalFunctionInfo) BaseJdbcConfig(io.prestosql.plugin.jdbc.BaseJdbcConfig) Test(org.testng.annotations.Test)

Aggregations

ExternalFunctionInfo (io.prestosql.spi.function.ExternalFunctionInfo)5 StandardTypes (io.prestosql.spi.type.StandardTypes)4 List (java.util.List)4 Set (java.util.Set)4 BaseJdbcConfig (io.prestosql.plugin.jdbc.BaseJdbcConfig)3 MysqlExternalFunctionHub (io.prestosql.plugin.mysql.optimization.function.MysqlExternalFunctionHub)3 Collectors (java.util.stream.Collectors)3 Assert.assertEquals (org.testng.Assert.assertEquals)3 Assert.assertFalse (org.testng.Assert.assertFalse)3 Assert.assertTrue (org.testng.Assert.assertTrue)3 Test (org.testng.annotations.Test)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 CatalogSchemaName (io.prestosql.spi.connector.CatalogSchemaName)2 RoutineCharacteristics (io.prestosql.spi.function.RoutineCharacteristics)2 SqlInvokedFunction (io.prestosql.spi.function.SqlInvokedFunction)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 QualifiedObjectName (io.prestosql.spi.connector.QualifiedObjectName)1 Parameter (io.prestosql.spi.function.Parameter)1