diff --git a/engine/src/main/java/com/arcadedb/query/sql/parser/DefineFunctionStatement.java b/engine/src/main/java/com/arcadedb/query/sql/parser/DefineFunctionStatement.java index c4972253c9..2df42ff3e8 100644 --- a/engine/src/main/java/com/arcadedb/query/sql/parser/DefineFunctionStatement.java +++ b/engine/src/main/java/com/arcadedb/query/sql/parser/DefineFunctionStatement.java @@ -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()) { @@ -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 diff --git a/engine/src/test/java/com/arcadedb/function/sql/SQLDefinedSQLFunctionTest.java b/engine/src/test/java/com/arcadedb/function/sql/SQLDefinedSQLFunctionTest.java index 2d1d3481aa..c2ef4227ae 100644 --- a/engine/src/test/java/com/arcadedb/function/sql/SQLDefinedSQLFunctionTest.java +++ b/engine/src/test/java/com/arcadedb/function/sql/SQLDefinedSQLFunctionTest.java @@ -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(); @@ -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);