Skip to content

Commit 5e08a2f

Browse files
Fix error using list comprehension with WITH * (#1838)
- Added a check for the case where the list comprehension is used with `WITH *`. Handled this case by adding entries in targetlist to the group clause. - Added regression tests.
1 parent db0a442 commit 5e08a2f

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

regress/expected/list_comprehension.out

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,43 @@ SELECT * FROM cypher('list_comprehension', $$ RETURN [1 IN range(0, 10, 2) | 1]
580580
ERROR: Syntax error at or near IN
581581
LINE 1: SELECT * FROM cypher('list_comprehension', $$ RETURN [1 IN r...
582582
^
583+
-- Issue - error using list comprehension with WITH *
584+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH *, [i in [1,2,3]] as list RETURN list LIMIT 1 $$) AS (result agtype);
585+
result
586+
-----------
587+
[1, 2, 3]
588+
(1 row)
589+
590+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH *, [i in [1,2,3]] as list RETURN [i in list] LIMIT 1 $$) AS (result agtype);
591+
result
592+
-----------
593+
[1, 2, 3]
594+
(1 row)
595+
596+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WHERE u.list=[i IN range(0,12,2)] RETURN u $$) AS (result agtype);
597+
result
598+
---------------------------------------------------------------------------------------------------------------------------------------------------------------
599+
{"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
600+
(1 row)
601+
602+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WHERE u.list=[i IN u.list] RETURN u LIMIT 1 $$) AS (result agtype);
603+
result
604+
-----------------------------------------------------------------------------------------------
605+
{"id": 281474976710658, "label": "", "properties": {"list": [1, 3, 5, 7, 9, 11, 13]}}::vertex
606+
(1 row)
607+
608+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WITH *, [i in [1,2,3]] as list RETURN list LIMIT 1 $$) AS (result agtype);
609+
result
610+
-----------
611+
[1, 2, 3]
612+
(1 row)
613+
614+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH *, [i in [1,2,3]] as list WITH * RETURN list LIMIT 1 $$) AS (result agtype);
615+
result
616+
-----------
617+
[1, 2, 3]
618+
(1 row)
619+
583620
SELECT * FROM drop_graph('list_comprehension', true);
584621
NOTICE: drop cascades to 4 other objects
585622
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
@@ -149,4 +149,12 @@ SELECT * FROM cypher('list_comprehension', $$ RETURN [i IN range(0, 10, 2) WHERE
149149
SELECT * FROM cypher('list_comprehension', $$ RETURN [1 IN range(0, 10, 2) WHERE 2>5] $$) AS (result agtype);
150150
SELECT * FROM cypher('list_comprehension', $$ RETURN [1 IN range(0, 10, 2) | 1] $$) AS (result agtype);
151151

152+
-- Issue - error using list comprehension with WITH *
153+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH *, [i in [1,2,3]] as list RETURN list LIMIT 1 $$) AS (result agtype);
154+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH *, [i in [1,2,3]] as list RETURN [i in list] LIMIT 1 $$) AS (result agtype);
155+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WHERE u.list=[i IN range(0,12,2)] RETURN u $$) AS (result agtype);
156+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WHERE u.list=[i IN u.list] RETURN u LIMIT 1 $$) AS (result agtype);
157+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WITH *, [i in [1,2,3]] as list RETURN list LIMIT 1 $$) AS (result agtype);
158+
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH *, [i in [1,2,3]] as list WITH * RETURN list LIMIT 1 $$) AS (result agtype);
159+
152160
SELECT * FROM drop_graph('list_comprehension', true);

src/backend/parser/cypher_clause.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,15 +2409,52 @@ static Query *transform_cypher_clause_with_where(cypher_parsestate *cpstate,
24092409
{
24102410
List *groupClause = NIL;
24112411
ListCell *li;
2412+
bool has_a_star;
2413+
2414+
has_a_star = false;
24122415
query->jointree = makeFromExpr(pstate->p_joinlist, NULL);
24132416
query->havingQual = where_qual;
24142417

24152418
foreach (li, ((cypher_return *)self)->items)
24162419
{
24172420
ResTarget *item = lfirst(li);
2421+
ColumnRef *cref;
2422+
2423+
/*
2424+
* We need to handle the case where the item is a A_star. In this
2425+
* case we will need to build group by using targetList.
2426+
*/
2427+
if (IsA(item->val, ColumnRef))
2428+
{
2429+
cref = (ColumnRef *)item->val;
2430+
if (IsA(linitial(cref->fields), A_Star))
2431+
{
2432+
has_a_star = true;
2433+
continue;
2434+
}
2435+
}
24182436

24192437
groupClause = lappend(groupClause, item->val);
24202438
}
2439+
2440+
/*
2441+
* If there is A_star flag, build the group by clause
2442+
* using the targetList.
2443+
*/
2444+
if (has_a_star)
2445+
{
2446+
ListCell *lc;
2447+
foreach (lc, query->targetList)
2448+
{
2449+
TargetEntry *te = lfirst(lc);
2450+
ColumnRef *cref = makeNode(ColumnRef);
2451+
2452+
cref->fields = list_make1(makeString(te->resname));
2453+
cref->location = exprLocation((Node *)te->expr);
2454+
2455+
groupClause = lappend(groupClause, cref);
2456+
}
2457+
}
24212458
query->groupClause = transform_group_clause(cpstate, groupClause,
24222459
&query->groupingSets,
24232460
&query->targetList,

0 commit comments

Comments
 (0)