This is an automated email from the ASF dual-hosted git repository.
jgemignani pushed a commit to branch PG14
in repository https://gitbox.apache.org/repos/asf/age.git
The following commit(s) were added to refs/heads/PG14 by this push:
new 85a587f6 Fix error using list comprehension with WITH * (#1838) (#1856)
85a587f6 is described below
commit 85a587f638f1b01be01209d6f0d78f137ae33c9d
Author: Muhammad Taha Naveed <[email protected]>
AuthorDate: Thu May 9 21:42:10 2024 +0500
Fix error using list comprehension with WITH * (#1838) (#1856)
- 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.
---
regress/expected/list_comprehension.out | 37 +++++++++++++++++++++++++++++++++
regress/sql/list_comprehension.sql | 8 +++++++
src/backend/parser/cypher_clause.c | 37 +++++++++++++++++++++++++++++++++
3 files changed, 82 insertions(+)
diff --git a/regress/expected/list_comprehension.out
b/regress/expected/list_comprehension.out
index d6d69096..131c7cbe 100644
--- a/regress/expected/list_comprehension.out
+++ b/regress/expected/list_comprehension.out
@@ -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
diff --git a/regress/sql/list_comprehension.sql
b/regress/sql/list_comprehension.sql
index 6a33b649..cef92a22 100644
--- a/regress/sql/list_comprehension.sql
+++ b/regress/sql/list_comprehension.sql
@@ -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);
\ No newline at end of file
diff --git a/src/backend/parser/cypher_clause.c
b/src/backend/parser/cypher_clause.c
index 3e4c52c0..8256287f 100644
--- a/src/backend/parser/cypher_clause.c
+++ b/src/backend/parser/cypher_clause.c
@@ -2401,15 +2401,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,