1

When I execute these queries on apache ageDB

SELECT * FROM cypher('sample_graph', $$
    CREATE (:opt_match_v {prop: 'value1'})-[:opt_match_e]->(:opt_match_v {prop: 'value2'}),
           (:opt_match_v {prop: 'value3'})-[:opt_match_e]->(:opt_match_v {prop: 'value4'})
$$) AS (r agtype);
SELECT * FROM cypher('sample_graph', $$
    MATCH p=(n)-[]->(m)
    RETURN n.name AS custom_node
    ORDER BY custom_node
$$) AS (r agtype);

this query is logically and syntactically correct but I get this error.

ERROR: could not find rte for r

cybersam
  • 63,203
  • 6
  • 53
  • 76
Abdul
  • 69
  • 5

11 Answers11

0

First of all there is a syntax error. You use prop field in the properties but in your query you use name which doesn't exist. Here is a way to fix your query :

SELECT * FROM cypher('sample_graph', $$
    MATCH p=(n)-[]->(m) 
    WITH n.prop 
    AS custom_node
    RETURN custom_node
    ORDER BY custom_node   
$$) AS (r agtype);

You need to use WITH...AS after the matching clause to set the alias for n.prop and then return that value correctly ordered.

0

First of all, ORDER BY is supposed to precede RETURN. Secondly, 'name' is not specified in properties hence the error.

The following statement should work:

SELECT * FROM cypher('sample_graph', $$
MATCH p=(n)-[]->(m)
WITH n.name AS custom_node
ORDER BY custom_node
RETURN custom_node
$$) as (r agtype);
Tito
  • 289
  • 8
0

You can not use AS with RETURN clause.

Try something like this:

RETURN n.name
ORDER BY n.name

OR

WITH n.name AS custom_node
RETURN custom_node
ORDER BY custom_node
Mohayu Din
  • 433
  • 9
0

The error is because you are using AS with the RETURN clause. You cannot use AS with RETURN.

Documentation of RETURN CLAUSE

Marcos Silva
  • 115
  • 5
0

Try this one :

SELECT * FROM cypher('sample_graph', $$
    CREATE (:opt_match_v {prop: 'value1'})-[:opt_match_e]->(:opt_match_v {prop: 'value2'}),
           (:opt_match_v {prop: 'value3'})-[:opt_match_e]->(:opt_match_v {prop: 'value4'})
$$) AS r;

SELECT * FROM cypher('sample_graph', $$
    MATCH p=(n)-[]->(m)
    RETURN n.name AS custom_node
    ORDER BY custom_node
$$) AS r;
0

I ran the commands in AGE and ran through the following steps: -

Creating my data like this:

SELECT * FROM cypher('test', $$
CREATE (:opt_match_v {prop: 'value1'})-[:opt_match_e]->(:opt_match_v {prop: 'value2'}),
(:opt_match_v {prop: 'value3'})-[:opt_match_e]->(:opt_match_v {prop: 'value4'})
$$) AS (r agtype);

This command works perfectly fine:

SELECT * FROM cypher('test', $$
    MATCH p=(n)-[]->(m)
    RETURN n.prop AS custom_node ORDER BY n.prop
$$) AS (r agtype);
    r     
----------
 "value1"
 "value3"
(2 rows)

Hence, we can see, order by being after return is not really an issue. Using as with return is not an issue either.

However, you were correct, this command does not work.

test=# SELECT * FROM cypher('test', $$
    MATCH p=(n)-[]->(m)
    RETURN n.prop AS custom_node ORDER BY custom_node
$$) AS (r agtype);
2023-05-26 11:52:08.197 CEST [30849] ERROR:  could not find rte for custom_node at character 99
2023-05-26 11:52:08.197 CEST [30849] STATEMENT:  SELECT * FROM cypher('test', $$
        MATCH p=(n)-[]->(m)
        RETURN n.prop AS custom_node ORDER BY custom_node
    $$) AS (r agtype);
ERROR:  could not find rte for custom_node
LINE 3:     RETURN n.prop AS custom_node ORDER BY custom_node

Therefore, solution would be to use this query: -

test=# SELECT * FROM cypher('test', $$
    MATCH p=(n)-[]->(m)
    WITH n.prop as custom_node RETURN custom_node ORDER BY custom_node
$$) AS (r agtype);
    r     
----------
 "value1"
 "value3"
(2 rows)
0

The issue is with your returning variable. You are trying to get the relationships between the nodes and trying to fetch the name variable of the vertexes but that name variable is not in the vertex.

So, what can be done here is you have to add the name property to the vertexes and after that, if you use that query then it will work fine.

Else you have to sort the query based on the prop value. You can use the prop property to sort it in ascending or descending order.

Ascending Order

MATCH p=(n)-[]->(m)
RETURN n.prop AS custom_node
ORDER BY custom_node

Descending Order

MATCH p=(n)-[]->(m)
RETURN n.prop AS custom_node
ORDER BY custom_node DESC
0

You have to use the WITH clause to sort of select the part of the query you would like to order using ORDER BY, so ORDER BY is a sub-clause following the WITH clause, And finally your RETURN statement should come last. So your updated query would be;

SELECT * FROM cypher('sample_graph', $$
    MATCH p=(n)-[]->(m)
    WITH n.name AS custom_node
    ORDER BY n.name
    RETURN custom_node
$$) AS (r agtype);

To learn more about the WITH clause, you can read more here https://age.apache.org/age-manual/master/clauses/with.html

0

The error is because 'name' is not there in the properties. Try using this:

SELECT *
FROM cypher('sample_graph', $$
MATCH p=(n)-[]->(m)
WITH n.prop
RETURN custom_node
ORDER BY custom_node
$$) AS r;

So what happens is that the paths 'p' between nodes 'n' and 'm' in the 'sample_graph' is retrieved. The 'WITH' clause is used to select the prop property of node 'n'. Then, the 'RETURN' clause is used to return the 'custom_node.' The 'ORDER BY' clause is applied to sort the result in ascending order based on custom_node.

0

You should use the WITH clause when you want to alias in this scenario.. You should do WITH n.name AS custom_node check out the docs for more info.

Peter
  • 43
  • 4
0

ORDER BY should come after the WITH clause and before RETURN. Also, the name property isn't specified in the query. Corrected query is:

SELECT * FROM cypher('sample_graph', $$
    MATCH p=(n)-[]->(m)
    WITH n.name AS custom_node
    ORDER BY custom_node
    RETURN custom_node
$$) AS r;

WITH clause creates an alias and ORDER BY sorts the results based on this alias, and finally the RETURN statement displays the sorted values.