Skip to content

Commit

Permalink
Fix error using list comprehension with WITH *
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
MuhammadTahaNaveed committed May 6, 2024
1 parent d612d43 commit 2edef10
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
37 changes: 37 additions & 0 deletions regress/expected/list_comprehension.out
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,43 @@ SELECT * FROM cypher('list_comprehension', $$ RETURN [1 IN range(0, 10, 2) | 1]
ERROR: Syntax error at or near IN
LINE 1: SELECT * FROM cypher('list_comprehension', $$ RETURN [1 IN r...
^
-- Issue #
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH *, [i in [1,2,3]] as list RETURN list LIMIT 1 $$) AS (result agtype);
result
-----------
[1, 2, 3]
(1 row)

SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH *, [i in [1,2,3]] as list RETURN [i in list] LIMIT 1 $$) AS (result agtype);
result
-----------
[1, 2, 3]
(1 row)

SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WHERE u.list=[i IN range(0,12,2)] RETURN u $$) AS (result agtype);
result
---------------------------------------------------------------------------------------------------------------------------------------------------------------
{"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
(1 row)

SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WHERE u.list=[i IN u.list] RETURN u LIMIT 1 $$) AS (result agtype);
result
-----------------------------------------------------------------------------------------------
{"id": 281474976710658, "label": "", "properties": {"list": [1, 3, 5, 7, 9, 11, 13]}}::vertex
(1 row)

SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WITH *, [i in [1,2,3]] as list RETURN list LIMIT 1 $$) AS (result agtype);
result
-----------
[1, 2, 3]
(1 row)

SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH *, [i in [1,2,3]] as list WITH * RETURN list LIMIT 1 $$) AS (result agtype);
result
-----------
[1, 2, 3]
(1 row)

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 @@ -149,4 +149,12 @@ SELECT * FROM cypher('list_comprehension', $$ RETURN [i IN range(0, 10, 2) WHERE
SELECT * FROM cypher('list_comprehension', $$ RETURN [1 IN range(0, 10, 2) WHERE 2>5] $$) AS (result agtype);
SELECT * FROM cypher('list_comprehension', $$ RETURN [1 IN range(0, 10, 2) | 1] $$) AS (result agtype);

-- Issue - error using list comprehension with WITH *
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) 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 RETURN [i in list] LIMIT 1 $$) AS (result agtype);
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WHERE u.list=[i IN range(0,12,2)] RETURN u $$) AS (result agtype);
SELECT * FROM cypher('list_comprehension', $$ MATCH (u) WITH * WHERE u.list=[i IN u.list] RETURN u LIMIT 1 $$) AS (result agtype);
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);

SELECT * FROM drop_graph('list_comprehension', true);
37 changes: 37 additions & 0 deletions src/backend/parser/cypher_clause.c
Original file line number Diff line number Diff line change
Expand Up @@ -2409,15 +2409,52 @@ static Query *transform_cypher_clause_with_where(cypher_parsestate *cpstate,
{
List *groupClause = NIL;
ListCell *li;
bool has_a_star;

has_a_star = false;
query->jointree = makeFromExpr(pstate->p_joinlist, NULL);
query->havingQual = where_qual;

foreach (li, ((cypher_return *)self)->items)
{
ResTarget *item = lfirst(li);
ColumnRef *cref;

/*
* We need to handle the case where the item is a A_star. In this
* case we will need to build group by using targetList.
*/
if (IsA(item->val, ColumnRef))
{
cref = (ColumnRef *)item->val;
if (IsA(linitial(cref->fields), A_Star))
{
has_a_star = true;
continue;
}
}

groupClause = lappend(groupClause, item->val);
}

/*
* If there is A_star flag, build the group by clause
* using the targetList.
*/
if (has_a_star)
{
ListCell *lc;
foreach (lc, query->targetList)
{
TargetEntry *te = lfirst(lc);
ColumnRef *cref = makeNode(ColumnRef);

cref->fields = list_make1(makeString(te->resname));
cref->location = exprLocation((Node *)te->expr);

groupClause = lappend(groupClause, cref);
}
}
query->groupClause = transform_group_clause(cpstate, groupClause,
&query->groupingSets,
&query->targetList,
Expand Down

0 comments on commit 2edef10

Please sign in to comment.