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);
}
}
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);
}
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();
}
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());
}
Aggregations