DEV Community

Abdullah Bin Omer Zia
Abdullah Bin Omer Zia

Posted on

User Defined Functions in Apache AGE

User-defined functions in Apache AGE are defined using the CREATE FUNCTION statement. The syntax for the CREATE FUNCTION statement is as follows:

CREATE FUNCTION function_name(argument_1 data_type, argument_2 data_type, ...)
RETURNS return_data_type AS $$
-- function body
LANGUAGE language;
Enter fullscreen mode Exit fullscreen mode

The function_name is the name of the function, argument_1, argument_2, ... are the arguments to the function, return_data_type is the data type of the return value, and language is the language in which the function is written.

The function body is a block of code that is executed when the function is called. The function body can contain any valid SQL statements.

The language parameter can be one of the following:
plpgsql: The function is written in PL/pgSQL.
c: The function is written in C.
java: The function is written in Java.

Once a user-defined function has been created, it can be called from a Cypher query using the following syntax:

age_function_name(argument_1, argument_2, ...)
Enter fullscreen mode Exit fullscreen mode


The following Cypher query creates a function called my_function that takes two integers as arguments and returns the product of those integers:

CREATE FUNCTION my_function(a integer, b integer)
RETURNS integer AS $$
RETURN a * b;
LANGUAGE plpgsql;
Enter fullscreen mode Exit fullscreen mode

The following Cypher query calls the my_function function and returns the result:

RETURN age_my_function(10, 20);
Enter fullscreen mode Exit fullscreen mode

User-defined functions can be used to perform a variety of tasks, such as:

  • Filtering data
  • Calculating values
  • Formatting data
  • Converting data between different formats

User-defined functions can be a powerful tool for extending the functionality of Apache AGE.

Top comments (0)