Skip to content

Commit 0f0d9be

Browse files
Fix issue 1955 - List comprehension in WHERE clause (apache#2094)
- Fixed issue when MATCH clause variable is referenced inside list comprehension in WHERE clause or in property constraint. - Added regression tests.
1 parent c3d658d commit 0f0d9be

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

regress/expected/list_comprehension.out

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,53 @@ SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH *, [i in [1,2,3]] a
617617
[1, 2, 3]
618618
(1 row)
619619

620+
-- Issue 1955 - variable reference in list comprehension
621+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WHERE u.list=[i IN u.list] RETURN u $$) AS (result agtype);
622+
result
623+
---------------------------------------------------------------------------------------------------------------------------------------------------------------
624+
{"id": 281474976710658, "label": "", "properties": {"list": [1, 3, 5, 7, 9, 11, 13]}}::vertex
625+
{"id": 281474976710660, "label": "", "properties": {"list": [12, 14, 16, 18, 20, 22, 24]}}::vertex
626+
{"id": 281474976710662, "label": "", "properties": {"list": [25.0, 49.0, 81.0, 121.0, 169.0]}}::vertex
627+
{"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
628+
{"id": 281474976710663, "label": "", "properties": {"list": [1, 2, 3]}}::vertex
629+
{"id": 281474976710659, "label": "", "properties": {"list": []}}::vertex
630+
{"id": 281474976710661, "label": "", "properties": {"list": [6, 8, 10, 12]}}::vertex
631+
{"id": 844424930131969, "label": "csm_match", "properties": {"list": ["abc", "def", "ghi"]}}::vertex
632+
(8 rows)
633+
634+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WHERE u.list=[i IN u.list WHERE i>0] RETURN u $$) AS (result agtype);
635+
result
636+
--------------------------------------------------------------------------------------------------------
637+
{"id": 281474976710658, "label": "", "properties": {"list": [1, 3, 5, 7, 9, 11, 13]}}::vertex
638+
{"id": 281474976710660, "label": "", "properties": {"list": [12, 14, 16, 18, 20, 22, 24]}}::vertex
639+
{"id": 281474976710662, "label": "", "properties": {"list": [25.0, 49.0, 81.0, 121.0, 169.0]}}::vertex
640+
{"id": 281474976710663, "label": "", "properties": {"list": [1, 2, 3]}}::vertex
641+
{"id": 281474976710661, "label": "", "properties": {"list": [6, 8, 10, 12]}}::vertex
642+
(5 rows)
643+
644+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WHERE size([e in u.list where e starts with "a"])>0 RETURN u $$) AS (result agtype);
645+
result
646+
------------------------------------------------------------------------------------------------------
647+
{"id": 844424930131969, "label": "csm_match", "properties": {"list": ["abc", "def", "ghi"]}}::vertex
648+
(1 row)
649+
650+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u ={list:[i IN u.list | i+1]}) RETURN u $$) AS (result agtype);
651+
result
652+
--------------------------------------------------------------------------
653+
{"id": 281474976710659, "label": "", "properties": {"list": []}}::vertex
654+
(1 row)
655+
656+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u ={list:[i IN u.list WHERE i>0]}) RETURN u$$) AS (result agtype);
657+
result
658+
--------------------------------------------------------------------------------------------------------
659+
{"id": 281474976710658, "label": "", "properties": {"list": [1, 3, 5, 7, 9, 11, 13]}}::vertex
660+
{"id": 281474976710660, "label": "", "properties": {"list": [12, 14, 16, 18, 20, 22, 24]}}::vertex
661+
{"id": 281474976710662, "label": "", "properties": {"list": [25.0, 49.0, 81.0, 121.0, 169.0]}}::vertex
662+
{"id": 281474976710663, "label": "", "properties": {"list": [1, 2, 3]}}::vertex
663+
{"id": 281474976710661, "label": "", "properties": {"list": [6, 8, 10, 12]}}::vertex
664+
(5 rows)
665+
666+
-- Clean up
620667
SELECT * FROM drop_graph('list_comprehension', true);
621668
NOTICE: drop cascades to 4 other objects
622669
DETAIL: drop cascades to table list_comprehension._ag_label_vertex

regress/sql/list_comprehension.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,12 @@ SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WHERE u.list=[i I
157157
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WITH *, [i in [1,2,3]] as list RETURN list LIMIT 1 $$) AS (result agtype);
158158
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH *, [i in [1,2,3]] as list WITH * RETURN list LIMIT 1 $$) AS (result agtype);
159159

160+
-- Issue 1955 - variable reference in list comprehension
161+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WHERE u.list=[i IN u.list] RETURN u $$) AS (result agtype);
162+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WHERE u.list=[i IN u.list WHERE i>0] RETURN u $$) AS (result agtype);
163+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WHERE size([e in u.list where e starts with "a"])>0 RETURN u $$) AS (result agtype);
164+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u ={list:[i IN u.list | i+1]}) RETURN u $$) AS (result agtype);
165+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u ={list:[i IN u.list WHERE i>0]}) RETURN u$$) AS (result agtype);
166+
167+
-- Clean up
160168
SELECT * FROM drop_graph('list_comprehension', true);

src/backend/parser/cypher_expr.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,8 @@ static Node *transform_column_ref_for_indirection(cypher_parsestate *cpstate,
13091309
}
13101310

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

13141315
/*
13151316
* Error out if we couldn't find it.

0 commit comments

Comments
 (0)