Skip to content

Commit

Permalink
fix: fixed calling custom functions without parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
lvca committed Nov 8, 2023
1 parent 573677c commit 6ed87a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,22 @@ public ResultSet executeSimple(final CommandContext context) {
break;

default:
throw new CommandSQLParsingException("Error on function creation: language '" + language.getStringValue() + "' not supported");
throw new CommandSQLParsingException(
"Error on function creation: language '" + language.getStringValue() + "' not supported");
}

database.getSchema().registerFunctionLibrary(fLib);
} else
fLib = database.getSchema().getFunctionLibrary(libraryName.getStringValue());

// CONVERT PARAMETERS
final String[] parameterArray = new String[parameters.size()];
for (int i = 0; i < parameters.size(); i++)
parameterArray[i] = parameters.get(i).getStringValue();
final String[] parameterArray;
if (parameters != null) {
// CONVERT PARAMETERS
parameterArray = new String[parameters.size()];
for (int i = 0; i < parameters.size(); i++)
parameterArray[i] = parameters.get(i).getStringValue();
} else
parameterArray = new String[] {};

final FunctionDefinition f;
switch (language.getStringValue()) {
Expand All @@ -69,13 +74,15 @@ public ResultSet executeSimple(final CommandContext context) {
break;

default:
throw new CommandSQLParsingException("Error on function creation: language '" + language.getStringValue() + "' not supported");
throw new CommandSQLParsingException(
"Error on function creation: language '" + language.getStringValue() + "' not supported");
}

fLib.registerFunction(f);

return new InternalResultSet().add(new ResultInternal().setProperty("operation", "create function").setProperty("libraryName", libraryName.getStringValue())
.setProperty("functionName", functionName.getStringValue()));
return new InternalResultSet().add(
new ResultInternal().setProperty("operation", "create function").setProperty("libraryName", libraryName.getStringValue())
.setProperty("functionName", functionName.getStringValue()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ public void testEmbeddedFunction() {
}

@Test
public void testCallFromSQL() {
public void testCallFromSQLWithParams() {
registerFunctions();
final ResultSet result = database.command("sql", "select `math.sum`(?,?) as result", 3, 5);
Assertions.assertEquals(8, (Integer) result.next().getProperty("result"));
}

@Test
public void testCallFromSQLNoParams() {
registerFunctions();
final ResultSet result = database.command("sql", "select `math.hello`() as result");
Assertions.assertEquals("hello", result.next().getProperty("result"));
}

@Test
public void testReuseSameQueryEngine() {
registerFunctions();
Expand Down Expand Up @@ -62,6 +69,7 @@ public void testRedefineFunction() {
private void registerFunctions() {
database.command("sql", "define function math.sum \"select :a + :b;\" parameters [a,b] language sql");
database.command("sql", "define function util.sum \"select :a + :b;\" parameters [a,b] language sql");
database.command("sql", "define function math.hello \"select 'hello'\" language sql");

final FunctionLibraryDefinition flib = database.getSchema().getFunctionLibrary("math");
Assertions.assertNotNull(flib);
Expand Down

0 comments on commit 6ed87a3

Please sign in to comment.