0

If I have the following query

select * from cypher('agload_test_graph', $$
 match (n) with n where n.name='A' return n
$$) as (sp agtype)

then n.name='A' doesn't work.

but if I remove with clause, then it works:

select * from cypher('agload_test_graph', $$
 match (n) where n.name='A' return n
$$) as (sp agtype)

I tried the example query in age document.

SELECT *
FROM cypher('graph_name', $$
    MATCH (david {name: 'David'})-[]-(otherPerson)-[]->()
    WITH otherPerson, count(*) AS foaf
    WHERE foaf > 1RETURN otherPerson.name
    RETURN otherPerson.name
$$) as (name agtype);
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
M Jin
  • 11
  • 1

3 Answers3

0

The where clause after with is ignored. This is one of the issues in Apache Age which is opened on github. Issue Page

0

This was a bug in AGE which has been fixed in the most recent commit to AGE. Now your query should work as you expect.

  • I have pulled the recent commits to my local repository, still the query does not work as expected. – Zainab Saad Mar 03 '23 at 01:09
  • @ZainabSaad which query did you ran ? Can you paste it here. Thanks. – Muhammad Taha Naveed Mar 03 '23 at 04:10
  • I verified, it is working at my end. The example in age documentation is same as in the documenatation of neo4j https://neo4j.com/docs/cypher-manual/current/clauses/with/. I created the same usecase and ran the query as in age documentation. `SELECT * FROM cypher('graph_name', $$ MATCH (david {name: 'David'})-[]-(otherPerson)-[]->() WITH otherPerson, count(*) AS foaf WHERE foaf > 1 RETURN otherPerson.name $$) as (name agtype);` And the result is as expected. Andres is returned. – Muhammad Taha Naveed Mar 03 '23 at 04:27
  • See output [output](https://i.imgur.com/BYhLI36.png). Also see this [demo](https://i.imgur.com/kErlgOa.png). Please make sure to rebuild age extension after pulling, and stop, start the server again before querying to reflect new changes. – Muhammad Taha Naveed Mar 03 '23 at 06:14
  • I pulled the changes, then rebuilt AGE using make install, stopped and started the server, dropped the extension age then recreated and loaded it still the query doesn't give the desired output. Query is `SELECT * FROM cypher('graph', $$ MATCH (u) WITH u WHERE u.name = 'John' RETURN u $$) AS (result agtype);` But it returns all the vertices Where am I making the mistake? – Zainab Saad Mar 03 '23 at 09:08
  • @ZainabSaad That's odd. Can you please try to run this query and see if this works `SELECT * FROM cypher('graph',$$RETURN isEmpty([])$$) AS (b agtype);`. If this works, it would mean that you have age with latest commits because this function was added after the bug fix. – Muhammad Taha Naveed Mar 03 '23 at 10:11
  • It gives an error saying `ERROR: function ag_catalog.age_isempty(agtype) does not exist`. I am assuming what wrong step did i do in pulling the latest commits that caused it. Btw I am on the same database cluster as before pulling the commits. – Zainab Saad Mar 03 '23 at 10:20
  • Ok so this means age is not built again properly. You should try these steps again. - git pull - make PG_CONFIG=/path/to/bin/pg_config install - Stop and start the server again - Drop and Create extension again and then retry – Muhammad Taha Naveed Mar 03 '23 at 10:28
  • Again did it, still no success – Zainab Saad Mar 03 '23 at 10:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252271/discussion-between-muhammad-taha-naveed-and-zainab-saad). – Muhammad Taha Naveed Mar 03 '23 at 10:44
-1

The problem with your first query is that the WHERE clause is not applied to the n variable because the WHERE clause is used before the WITH clause.  By WITHclause, a new variable that is a subset of the previous variable is created in Cypher. Any filters added after the the WITH clause will be applied to this new variable rather than the original variable. By deleting the WITH clause from your second query, the where clause is being applied directly to the n variable, which is what you want.

The example query in the age document you provided is a legitimate Cypher query. It uses MATCH to find a node with the name "David", then it uses  WITH clause to create a new variable otherPerson, which is the set of nodes nodes connected to the "David" node, and then uses  count(*) function to count the number of nodes connected to the "David" node. After this it uses the WHERE clause to filter otherPerson nodes where the count of their connections is greater than 1 and return their names.