skip to Main Content

How can I create a custom function in apache-age,
similar to the function we can create using PostgreSQL statement;

CREATE FUNCTION f2(INTEGER) returns INTEGER AS 'SELECT $1' LANGUAGE SQL;

I need to create a function just like the above function created in Postgresql. Input of function should be integer and output should also be integer.

2

Answers


  1. In Apache-AGE you can create the custom function in same way as in PostgreSQL.

    For this you need to use "CREATE FUNCTION" clause. A simple example of function which accepts INTEGER and returns INTEGER is as:

    CREATE FUNCTION find_square(i INTEGER) RETURNS INTEGER AS $$
    SELECT i * i;
    $$ LANGUAGE SQL;
    

    This function is accepting INTERGER and is returning its square.
    All of your function calculation goes after "SELECT" statement. For Example, if you want to subtract i after finding its square then you will modify it as:

    CREATE FUNCTION find_square_subtract_i(i INTEGER) RETURNS INTEGER AS $$
    SELECT (i * i) - i;
    $$ LANGUAGE SQL;
    
    Login or Signup to reply.
  2. Basically, there are two methods to create custom function in Apache AGE. One is using python or using SQL. For simple functions, SQL method will be used and for complex functions python method will be used.

    SQL method:

    CREATE FUNCTION numb_add(a INTEGER, b INTEGER) RETURNS INTEGER AS
    $$SELECT a + b;
    $$ LANGUAGE SQL;
    

    Function calling:

    SELECT numb_add(5, 8); //output will be 13
    

    Python method:

    CREATE FUNCTION f2(arg INTEGER) RETURNS INTEGER AS $$return arg
    $$ LANGUAGE plpython3u;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search