Skip to content

Commit

Permalink
Fix issue #876 - column reference "_age_default_alias_0" is ambiguous (
Browse files Browse the repository at this point in the history
…#931)

- The problem was with get_next_default_alias function which was not
  generating unique aliases.
- Every clause transformed as a subquery has its own cpstate which is
  being freed after it is tranformed. The root cpstate is the one that
  has the default_alias_number initialized. So we need to reach the
  root cpstate to get the next correct default alias number.
- This change also fixed some previously incorrect results.
- Added additional regression tests.
  • Loading branch information
MuhammadTahaNaveed authored Jun 1, 2023
1 parent b7a28a1 commit 27d4375
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 24 deletions.
24 changes: 21 additions & 3 deletions regress/expected/cypher_match.out
Original file line number Diff line number Diff line change
Expand Up @@ -1931,7 +1931,9 @@ SELECT * FROM cypher('cypher_match', $$
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
{"id": 4785074604081153, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710661, "properties": {"years": 3, "relationship": "friends"}}::edge
{"id": 4785074604081153, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710661, "properties": {"years": 3, "relationship": "friends"}}::edge
(2 rows)
{"id": 4785074604081153, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710661, "properties": {"years": 3, "relationship": "friends"}}::edge
{"id": 4785074604081153, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710661, "properties": {"years": 3, "relationship": "friends"}}::edge
(4 rows)

SELECT * FROM cypher('cypher_match', $$
MATCH ()-[r {years:3, relationship: "friends"}]-() RETURN r $$) as (r agtype);
Expand All @@ -1947,7 +1949,9 @@ SELECT * FROM cypher('cypher_match', $$
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
{"id": 4785074604081153, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710661, "properties": {"years": 3, "relationship": "friends"}}::edge
{"id": 4785074604081153, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710661, "properties": {"years": 3, "relationship": "friends"}}::edge
(2 rows)
{"id": 4785074604081153, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710661, "properties": {"years": 3, "relationship": "friends"}}::edge
{"id": 4785074604081153, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710661, "properties": {"years": 3, "relationship": "friends"}}::edge
(4 rows)

--mismatch year #, should return nothing
SELECT * FROM cypher('cypher_match', $$
Expand All @@ -1962,7 +1966,9 @@ SELECT * FROM cypher('cypher_match', $$
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
{"id": 4785074604081154, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710659, "properties": {"years": 4, "relationship": "enemies"}}::edge
{"id": 4785074604081154, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710659, "properties": {"years": 4, "relationship": "enemies"}}::edge
(2 rows)
{"id": 4785074604081154, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710659, "properties": {"years": 4, "relationship": "enemies"}}::edge
{"id": 4785074604081154, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710659, "properties": {"years": 4, "relationship": "enemies"}}::edge
(4 rows)

SELECT * FROM cypher('cypher_match', $$
MATCH ()-[r {relationship:"enemies"}]-() MATCH ()-[r {relationship:"friends"}]-() RETURN r $$) as (r agtype);
Expand Down Expand Up @@ -2101,6 +2107,18 @@ SELECT * FROM cypher('cypher_match', $$ MATCH (_age_default_whatever) RETURN 0 $
ERROR: _age_default_ is only for internal use
LINE 1: SELECT * FROM cypher('cypher_match', $$ MATCH (_age_default_...
^
-- issue 876
SELECT * FROM cypher('cypher_match', $$ MATCH ({name: "Dave"}) MATCH ({name: "Dave"}) MATCH ({name: "Dave"}) RETURN 0 $$) as (a agtype);
a
---
0
(1 row)

SELECT * FROM cypher('cypher_match', $$MATCH ({n0:0}) MATCH ()-[]->() MATCH ({n1:0})-[]-() RETURN 0 AS n2$$) as (a agtype);
a
---
(0 rows)

--
-- self referencing property constraints (issue #898)
--
Expand Down
43 changes: 23 additions & 20 deletions regress/expected/cypher_merge.out
Original file line number Diff line number Diff line change
Expand Up @@ -496,17 +496,20 @@ SELECT * FROM cypher('cypher_merge', $$MATCH (n) DETACH DELETE n $$) AS (a agtyp

/*
* test 17:
* XXX: Incorrect Output. To FIX
*/
--test query
SELECT * FROM cypher('cypher_merge', $$CREATE (n) MERGE (n)-[:e]->() $$) AS (a agtype);
ERROR: end_id() argument must resolve to a scalar value
--validate created correctly
SELECT * FROM cypher('cypher_merge', $$MATCH p=()-[:e]->() RETURN p$$) AS (p agtype);
p
a
---
(0 rows)

--validate created correctly
SELECT * FROM cypher('cypher_merge', $$MATCH p=()-[:e]->() RETURN p$$) AS (p agtype);
p
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"id": 281474976710689, "label": "", "properties": {}}::vertex, {"id": 844424930131982, "label": "e", "end_id": 281474976710690, "start_id": 281474976710689, "properties": {}}::edge, {"id": 281474976710690, "label": "", "properties": {}}::vertex]::path
(1 row)

--clean up
SELECT * FROM cypher('cypher_merge', $$MATCH (n) DETACH DELETE n $$) AS (a agtype);
a
Expand All @@ -526,7 +529,7 @@ SELECT * FROM cypher('cypher_merge', $$CREATE (n {i : 1}) SET n.i = 2 MERGE ({i:
SELECT * FROM cypher('cypher_merge', $$MATCH (a) RETURN a$$) AS (a agtype);
a
----------------------------------------------------------------------
{"id": 281474976710690, "label": "", "properties": {"i": 2}}::vertex
{"id": 281474976710691, "label": "", "properties": {"i": 2}}::vertex
(1 row)

--clean up
Expand All @@ -548,7 +551,7 @@ SELECT * FROM cypher('cypher_merge', $$CREATE (n {i : 1}) SET n.i = 2 WITH n as
SELECT * FROM cypher('cypher_merge', $$MATCH (a) RETURN a$$) AS (a agtype);
a
----------------------------------------------------------------------
{"id": 281474976710691, "label": "", "properties": {"i": 2}}::vertex
{"id": 281474976710692, "label": "", "properties": {"i": 2}}::vertex
(1 row)

--clean up
Expand Down Expand Up @@ -576,7 +579,7 @@ SELECT * FROM cypher('cypher_merge', $$MATCH (n {i : 1}) SET n.i = 2 WITH n as a
SELECT * FROM cypher('cypher_merge', $$MATCH (a) RETURN a$$) AS (a agtype);
a
----------------------------------------------------------------------
{"id": 281474976710692, "label": "", "properties": {"i": 2}}::vertex
{"id": 281474976710693, "label": "", "properties": {"i": 2}}::vertex
(1 row)

--clean up
Expand All @@ -601,7 +604,7 @@ ERROR: vertex assigned to variable n was deleted
SELECT * FROM cypher('cypher_merge', $$MATCH (a) RETURN a$$) AS (a agtype);
a
----------------------------------------------------------------------
{"id": 281474976710693, "label": "", "properties": {"i": 1}}::vertex
{"id": 281474976710694, "label": "", "properties": {"i": 1}}::vertex
(1 row)

--clean up
Expand Down Expand Up @@ -679,7 +682,7 @@ SELECT * FROM cypher('cypher_merge', $$MERGE ()-[:e]-()$$) AS (a agtype);
SELECT * FROM cypher('cypher_merge', $$MATCH p=()-[]->() RETURN p$$) AS (a agtype);
a
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"id": 281474976710694, "label": "", "properties": {}}::vertex, {"id": 844424930131982, "label": "e", "end_id": 281474976710695, "start_id": 281474976710694, "properties": {}}::edge, {"id": 281474976710695, "label": "", "properties": {}}::vertex]::path
[{"id": 281474976710695, "label": "", "properties": {}}::vertex, {"id": 844424930131983, "label": "e", "end_id": 281474976710696, "start_id": 281474976710695, "properties": {}}::edge, {"id": 281474976710696, "label": "", "properties": {}}::vertex]::path
(1 row)

--clean up
Expand All @@ -694,14 +697,14 @@ SELECT * FROM cypher('cypher_merge', $$MATCH (n) DETACH DELETE n $$) AS (a agtyp
SELECT * FROM cypher('cypher_merge', $$MERGE (a) RETURN a$$) AS (a agtype);
a
----------------------------------------------------------------
{"id": 281474976710696, "label": "", "properties": {}}::vertex
{"id": 281474976710697, "label": "", "properties": {}}::vertex
(1 row)

--validate
SELECT * FROM cypher('cypher_merge', $$MATCH (a) RETURN a$$) AS (a agtype);
a
----------------------------------------------------------------
{"id": 281474976710696, "label": "", "properties": {}}::vertex
{"id": 281474976710697, "label": "", "properties": {}}::vertex
(1 row)

--clean up
Expand All @@ -716,14 +719,14 @@ SELECT * FROM cypher('cypher_merge', $$MATCH (n) DETACH DELETE n $$) AS (a agtyp
SELECT * FROM cypher('cypher_merge', $$MERGE p=()-[:e]-() RETURN p$$) AS (a agtype);
a
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"id": 281474976710697, "label": "", "properties": {}}::vertex, {"id": 844424930131983, "label": "e", "end_id": 281474976710698, "start_id": 281474976710697, "properties": {}}::edge, {"id": 281474976710698, "label": "", "properties": {}}::vertex]::path
[{"id": 281474976710698, "label": "", "properties": {}}::vertex, {"id": 844424930131984, "label": "e", "end_id": 281474976710699, "start_id": 281474976710698, "properties": {}}::edge, {"id": 281474976710699, "label": "", "properties": {}}::vertex]::path
(1 row)

--validate
SELECT * FROM cypher('cypher_merge', $$MATCH p=()-[]->() RETURN p$$) AS (a agtype);
a
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"id": 281474976710697, "label": "", "properties": {}}::vertex, {"id": 844424930131983, "label": "e", "end_id": 281474976710698, "start_id": 281474976710697, "properties": {}}::edge, {"id": 281474976710698, "label": "", "properties": {}}::vertex]::path
[{"id": 281474976710698, "label": "", "properties": {}}::vertex, {"id": 844424930131984, "label": "e", "end_id": 281474976710699, "start_id": 281474976710698, "properties": {}}::edge, {"id": 281474976710699, "label": "", "properties": {}}::vertex]::path
(1 row)

--clean up
Expand All @@ -738,14 +741,14 @@ SELECT * FROM cypher('cypher_merge', $$MATCH (n) DETACH DELETE n $$) AS (a agtyp
SELECT * FROM cypher('cypher_merge', $$MERGE (a)-[:e]-(b) RETURN a$$) AS (a agtype);
a
----------------------------------------------------------------
{"id": 281474976710699, "label": "", "properties": {}}::vertex
{"id": 281474976710700, "label": "", "properties": {}}::vertex
(1 row)

--validate
SELECT * FROM cypher('cypher_merge', $$MATCH p=()-[]->() RETURN p$$) AS (a agtype);
a
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"id": 281474976710699, "label": "", "properties": {}}::vertex, {"id": 844424930131984, "label": "e", "end_id": 281474976710700, "start_id": 281474976710699, "properties": {}}::edge, {"id": 281474976710700, "label": "", "properties": {}}::vertex]::path
[{"id": 281474976710700, "label": "", "properties": {}}::vertex, {"id": 844424930131985, "label": "e", "end_id": 281474976710701, "start_id": 281474976710700, "properties": {}}::edge, {"id": 281474976710701, "label": "", "properties": {}}::vertex]::path
(1 row)

--clean up
Expand All @@ -760,14 +763,14 @@ SELECT * FROM cypher('cypher_merge', $$MATCH (n) DETACH DELETE n $$) AS (a agtyp
SELECT * FROM cypher('cypher_merge', $$CREATE p=()-[:e]->() RETURN p$$) AS (a agtype);
a
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"id": 281474976710701, "label": "", "properties": {}}::vertex, {"id": 844424930131985, "label": "e", "end_id": 281474976710702, "start_id": 281474976710701, "properties": {}}::edge, {"id": 281474976710702, "label": "", "properties": {}}::vertex]::path
[{"id": 281474976710702, "label": "", "properties": {}}::vertex, {"id": 844424930131986, "label": "e", "end_id": 281474976710703, "start_id": 281474976710702, "properties": {}}::edge, {"id": 281474976710703, "label": "", "properties": {}}::vertex]::path
(1 row)

SELECT * FROM cypher('cypher_merge', $$MERGE p=()-[:e]-() RETURN p$$) AS (a agtype);
a
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"id": 281474976710701, "label": "", "properties": {}}::vertex, {"id": 844424930131985, "label": "e", "end_id": 281474976710702, "start_id": 281474976710701, "properties": {}}::edge, {"id": 281474976710702, "label": "", "properties": {}}::vertex]::path
[{"id": 281474976710702, "label": "", "properties": {}}::vertex, {"id": 844424930131985, "label": "e", "end_id": 281474976710702, "start_id": 281474976710701, "properties": {}}::edge, {"id": 281474976710701, "label": "", "properties": {}}::vertex]::path
[{"id": 281474976710702, "label": "", "properties": {}}::vertex, {"id": 844424930131986, "label": "e", "end_id": 281474976710703, "start_id": 281474976710702, "properties": {}}::edge, {"id": 281474976710703, "label": "", "properties": {}}::vertex]::path
[{"id": 281474976710703, "label": "", "properties": {}}::vertex, {"id": 844424930131986, "label": "e", "end_id": 281474976710703, "start_id": 281474976710702, "properties": {}}::edge, {"id": 281474976710702, "label": "", "properties": {}}::vertex]::path
(2 rows)

--clean up
Expand Down Expand Up @@ -812,7 +815,7 @@ ERROR: Existing variable m cannot be NULL in MERGE clause
SELECT * FROM cypher('cypher_merge', $$MATCH (n) RETURN n$$) AS (a agtype);
a
----------------------------------------------------------------
{"id": 281474976710703, "label": "", "properties": {}}::vertex
{"id": 281474976710704, "label": "", "properties": {}}::vertex
(1 row)

--
Expand Down
4 changes: 4 additions & 0 deletions regress/sql/cypher_match.sql
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,10 @@ SELECT * FROM cypher('cypher_match', $$ MATCH (_age_default_) RETURN _age_defaul
SELECT * FROM cypher('cypher_match', $$ MATCH (_age_default_a) RETURN _age_default_a $$) as (a agtype);
SELECT * FROM cypher('cypher_match', $$ MATCH (_age_default_whatever) RETURN 0 $$) as (a agtype);

-- issue 876
SELECT * FROM cypher('cypher_match', $$ MATCH ({name: "Dave"}) MATCH ({name: "Dave"}) MATCH ({name: "Dave"}) RETURN 0 $$) as (a agtype);
SELECT * FROM cypher('cypher_match', $$MATCH ({n0:0}) MATCH ()-[]->() MATCH ({n1:0})-[]-() RETURN 0 AS n2$$) as (a agtype);

--
-- self referencing property constraints (issue #898)
--
Expand Down
1 change: 0 additions & 1 deletion regress/sql/cypher_merge.sql
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ SELECT * FROM cypher('cypher_merge', $$MATCH (n) DETACH DELETE n $$) AS (a agtyp

/*
* test 17:
* XXX: Incorrect Output. To FIX
*/

--test query
Expand Down
13 changes: 13 additions & 0 deletions src/backend/parser/cypher_parse_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,22 @@ RangeTblEntry *find_rte(cypher_parsestate *cpstate, char *varname)
*/
char *get_next_default_alias(cypher_parsestate *cpstate)
{
ParseState *pstate = (ParseState *)cpstate;
cypher_parsestate *parent_cpstate = (cypher_parsestate *)pstate->parentParseState;
char *alias_name;
int nlen = 0;

/*
* Every clause transformed as a subquery has its own cpstate which is being
* freed after it is tranformed. The root cpstate is the one that has the
* default alias number initialized. So we need to reach the root cpstate to
* get the next correct default alias number.
*/
if (parent_cpstate)
{
return get_next_default_alias(parent_cpstate);
}

/* get the length of the combinded string */
nlen = snprintf(NULL, 0, "%s%d", AGE_DEFAULT_ALIAS_PREFIX,
cpstate->default_alias_num);
Expand Down

0 comments on commit 27d4375

Please sign in to comment.