User-Defined Functions

This tutorial shows how to add user-defined functions (UDF) to PGX and how to use them in PGQL and Green-Marl. The examples will focus on Java and JavaScript UDFs.

Using UDFs in PGX

After configuring the UDFs, they can now be used in PGX and the next two sections will show how they can be utilized in PGQL and Green-Marl (not available in all releases).

First we start PGX in local mode:

1session = pypgx.get_session(session_name="my-session")

Next, let us load a graph into memory that we can use in the subsequent examples.

1connections_graph = session.read_graph_with_properties(
2    "examples/graphs/connections.csv.json"
3)

UDFs in PGQL

PGQL can call UDFs directly using the namespace and function_name. Note that the namespace is required, otherwise the function will be interpreted as a built-in function. UDFs can be used in any part of the PGQL query, e.g. the following query utilizes them in the SELECT clause.

1result_set = connections_graph.query_pgql(
2    """
3    SELECT
4        DISTINCT udfTutorial.format(v.name, v.country),
5        udfTutorial.toHexString(e.weight)
6    FROM MATCH (v) -[e]-> (u)
7    """
8)