Skip to content

Commit

Permalink
Fix issue 1955 - List comprehension in WHERE clause (#2094)
Browse files Browse the repository at this point in the history
- Fixed issue when MATCH clause variable is referenced inside list
  comprehension in WHERE clause or in property constraint.
- Added regression tests.
  • Loading branch information
MuhammadTahaNaveed authored Sep 9, 2024
1 parent c3d658d commit 0f0d9be
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
47 changes: 47 additions & 0 deletions regress/expected/list_comprehension.out
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,53 @@ SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH *, [i in [1,2,3]] a
[1, 2, 3]
(1 row)

-- Issue 1955 - variable reference in list comprehension
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WHERE u.list=[i IN u.list] RETURN u $$) AS (result agtype);
result
---------------------------------------------------------------------------------------------------------------------------------------------------------------
{"id": 281474976710658, "label": "", "properties": {"list": [1, 3, 5, 7, 9, 11, 13]}}::vertex
{"id": 281474976710660, "label": "", "properties": {"list": [12, 14, 16, 18, 20, 22, 24]}}::vertex
{"id": 281474976710662, "label": "", "properties": {"list": [25.0, 49.0, 81.0, 121.0, 169.0]}}::vertex
{"id": 281474976710657, "label": "", "properties": {"a": [], "b": [0, 1, 2, 3, 4, 5], "c": [0, 2, 4, 6, 8, 10, 12], "list": [0, 2, 4, 6, 8, 10, 12]}}::vertex
{"id": 281474976710663, "label": "", "properties": {"list": [1, 2, 3]}}::vertex
{"id": 281474976710659, "label": "", "properties": {"list": []}}::vertex
{"id": 281474976710661, "label": "", "properties": {"list": [6, 8, 10, 12]}}::vertex
{"id": 844424930131969, "label": "csm_match", "properties": {"list": ["abc", "def", "ghi"]}}::vertex
(8 rows)

SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WHERE u.list=[i IN u.list WHERE i>0] RETURN u $$) AS (result agtype);
result
--------------------------------------------------------------------------------------------------------
{"id": 281474976710658, "label": "", "properties": {"list": [1, 3, 5, 7, 9, 11, 13]}}::vertex
{"id": 281474976710660, "label": "", "properties": {"list": [12, 14, 16, 18, 20, 22, 24]}}::vertex
{"id": 281474976710662, "label": "", "properties": {"list": [25.0, 49.0, 81.0, 121.0, 169.0]}}::vertex
{"id": 281474976710663, "label": "", "properties": {"list": [1, 2, 3]}}::vertex
{"id": 281474976710661, "label": "", "properties": {"list": [6, 8, 10, 12]}}::vertex
(5 rows)

SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WHERE size([e in u.list where e starts with "a"])>0 RETURN u $$) AS (result agtype);
result
------------------------------------------------------------------------------------------------------
{"id": 844424930131969, "label": "csm_match", "properties": {"list": ["abc", "def", "ghi"]}}::vertex
(1 row)

SELECT * FROM cypher('list_comprehension', $$ MATCH (u ={list:[i IN u.list | i+1]}) RETURN u $$) AS (result agtype);
result
--------------------------------------------------------------------------
{"id": 281474976710659, "label": "", "properties": {"list": []}}::vertex
(1 row)

SELECT * FROM cypher('list_comprehension', $$ MATCH (u ={list:[i IN u.list WHERE i>0]}) RETURN u$$) AS (result agtype);
result
--------------------------------------------------------------------------------------------------------
{"id": 281474976710658, "label": "", "properties": {"list": [1, 3, 5, 7, 9, 11, 13]}}::vertex
{"id": 281474976710660, "label": "", "properties": {"list": [12, 14, 16, 18, 20, 22, 24]}}::vertex
{"id": 281474976710662, "label": "", "properties": {"list": [25.0, 49.0, 81.0, 121.0, 169.0]}}::vertex
{"id": 281474976710663, "label": "", "properties": {"list": [1, 2, 3]}}::vertex
{"id": 281474976710661, "label": "", "properties": {"list": [6, 8, 10, 12]}}::vertex
(5 rows)

-- Clean up
SELECT * FROM drop_graph('list_comprehension', true);
NOTICE: drop cascades to 4 other objects
DETAIL: drop cascades to table list_comprehension._ag_label_vertex
Expand Down
8 changes: 8 additions & 0 deletions regress/sql/list_comprehension.sql
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,12 @@ SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WHERE u.list=[i I
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WITH *, [i in [1,2,3]] as list RETURN list LIMIT 1 $$) AS (result agtype);
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH *, [i in [1,2,3]] as list WITH * RETURN list LIMIT 1 $$) AS (result agtype);

-- Issue 1955 - variable reference in list comprehension
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WHERE u.list=[i IN u.list] RETURN u $$) AS (result agtype);
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WHERE u.list=[i IN u.list WHERE i>0] RETURN u $$) AS (result agtype);
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WHERE size([e in u.list where e starts with "a"])>0 RETURN u $$) AS (result agtype);
SELECT * FROM cypher('list_comprehension', $$ MATCH (u ={list:[i IN u.list | i+1]}) RETURN u $$) AS (result agtype);
SELECT * FROM cypher('list_comprehension', $$ MATCH (u ={list:[i IN u.list WHERE i>0]}) RETURN u$$) AS (result agtype);

-- Clean up
SELECT * FROM drop_graph('list_comprehension', true);
3 changes: 2 additions & 1 deletion src/backend/parser/cypher_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,8 @@ static Node *transform_column_ref_for_indirection(cypher_parsestate *cpstate,
}

/* find the properties column of the NSI and return a var for it */
node = scanNSItemForColumn(pstate, pnsi, 0, "properties", cr->location);
node = scanNSItemForColumn(pstate, pnsi, levels_up, "properties",
cr->location);

/*
* Error out if we couldn't find it.
Expand Down

0 comments on commit 0f0d9be

Please sign in to comment.