DEV Community

Cover image for The use of Predicate Functions in Apache AGE
Nnaemeka Daniel John
Nnaemeka Daniel John

Posted on

The use of Predicate Functions in Apache AGE

Apache AGE, the graph extension for PostgreSQL, has revolutionized the way we work with graph data. With its rich feature set and powerful querying capabilities, AGE opens up a world of possibilities for developers and data analysts. One of the standout features of AGE is its support for predicate functions, which allows for advanced graph traversal and filtering. In this blog post, we will explore the concept of predicate functions in Apache AGE and discover how they can supercharge your graph querying experience.


Predicate Functions

In graph databases, predicate functions are essential tools for filtering and selecting specific nodes or edges based on certain criteria. They act as Boolean functions that evaluate the properties of graph elements and determine their inclusion or exclusion in query results. They are most commonly used to filter out subgraphs in the WHERE part of a query. Apache AGE takes this concept to the next level by integrating predicate functions seamlessly into its querying framework.


Using Predicate Functions

Given a graph with nodes;

QUERY

demo=# SELECT * FROM cypher('demo', $$ MATCH (u) RETURN u $$)
demo-# as (an agtype);
                                                     an
-------------------------------------------------------------------------------------------------------------
 {"id": 2251799813685249, "label": "worker", "properties": {"city": "New York", "name": "Pete"}}::vertex
 {"id": 2251799813685250, "label": "worker", "properties": {"city": "Toronto", "name": "Mike"}}::vertex
 {"id": 2251799813685251, "label": "worker", "properties": {"city": "Vancouver", "name": "Clint"}}::vertex
 {"id": 2251799813685252, "label": "worker", "properties": {"city": "Dallas", "name": "Jane"}}::vertex
 {"id": 2251799813685253, "label": "worker", "properties": {"city": "San Francisco", "name": "Tom"}}::vertex
(5 rows)
Enter fullscreen mode Exit fullscreen mode

demo=# SELECT *
demo-# FROM cypher('demo', $$
demo$# MATCH (p)
demo$# WHERE p.name = 'Mike'
demo$# RETURN p.name, p.city
demo$# $$) as (name agtype, city agtype);
  name  |   city
--------+-----------
 "Mike" | "Toronto"
(1 rows)
Enter fullscreen mode Exit fullscreen mode

In this example, the WHERE clause uses a predicate function to filter the users based on the name property. AGE evaluates the predicate function for each node and includes only those that satisfy the condition.


The Exists(Property)

The function exists() evaluates to true if the specified property is present in the node, relationship, or map. It should be noted that this functionality differs from the EXISTS clause in queries.

QUERY

demo=# SELECT *
demo-# FROM cypher('demo', $$
demo$# MATCH (p)
demo$# WHERE exists(p.city)
demo$# RETURN p.name, p.city
demo$# $$) as (name agtype, city agtype);
  name   |      city
---------+-----------------
 "Pete"  | "New York"
 "Mike"  | "Toronto"
 "Clint" | "Vancouver"
 "Jane"  | "Dallas"
 "Tom"   | "San Francisco"
 "Hugh"  | "Houston"
(6 rows)
Enter fullscreen mode Exit fullscreen mode

So in our query WHERE exists(p.city) evaluates to true, because p matches properties with city in our graph, and those cities are returned along with the names attached to them in our output.


Conclusion

Predicate functions in Apache AGE are a game-changer when it comes to graph querying and filtering. They empower developers and data analysts to extract meaningful insights from complex graph data by providing a flexible and expressive way to filter and traverse nodes and edges. By mastering the art of predicate functions, you can unlock the full potential of Apache AGE and harness its graph processing capabilities to drive your data-driven applications to new heights.


References

Top comments (0)