Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error using list comprehension with WITH * #1858

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 - 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);
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
Loading