Skip to content
Permalink
Browse files
bug-fix: chained union logic
fixed bug with chained unions that failed to remove duplicate values

the bug appeared when mixing agtypes and chaining multiple unions
  • Loading branch information
dehowef committed Feb 3, 2022
1 parent 9fdfe40 commit ed283c06b44b990957e3fa21e719eb9dc93e45f3
Showing 3 changed files with 39 additions and 8 deletions.
@@ -46,15 +46,15 @@ SELECT * FROM cypher('cypher_union', $$ MATCH (n) RETURN n UNION ALL MATCH (n) R
SELECT * FROM cypher('cypher_union', $$ MATCH (n) RETURN n UNION RETURN 1 $$) as (a agtype);
a
----------------------------------------------------------------
1
{"id": 281474976710657, "label": "", "properties": {}}::vertex
1
(2 rows)

SELECT * FROM cypher('cypher_union', $$ MATCH (n) RETURN n UNION RETURN NULL $$) as (a agtype);
a
----------------------------------------------------------------

{"id": 281474976710657, "label": "", "properties": {}}::vertex

(2 rows)

SELECT * FROM cypher('cypher_union', $$ RETURN [1,2,3] UNION RETURN 1 $$) as (a agtype);
@@ -137,10 +137,26 @@ SELECT * FROM cypher('cypher_union', $$MATCH (n) RETURN n UNION MATCH (n) return
(1 row)

/* scoping */
SELECT * FROM cypher('cypher_union', $$MATCH (n) RETURN n UNION ALL MATCH (m) RETURN n$$) AS (result agtype);
SELECT * FROM cypher('cypher_union', $$MATCH (n) RETURN n UNION ALL MATCH (m) RETURN n$$) AS (result agtype);
ERROR: could not find rte for n
LINE 2: ... $$MATCH (n) RETURN n UNION ALL MATCH (m) RETURN n$$) AS (r...
LINE 2: ..., $$MATCH (n) RETURN n UNION ALL MATCH (m) RETURN n$$) AS (r...
^
/*
*UNION and UNION ALL, type casting
*/
SELECT * FROM cypher('cypher_union', $$ RETURN 1.0::int UNION return 1::float UNION ALL RETURN 2.0::float $$) AS (result agtype);
result
--------
1
2.0
(2 rows)

SELECT * FROM cypher('cypher_union', $$ RETURN 1.0::float UNION return 1::int UNION RETURN 1::float $$) AS (result agtype);
result
--------
1.0
(1 row)

SELECT drop_graph('cypher_union', true);
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to table cypher_union._ag_label_vertex
@@ -65,6 +65,14 @@ SELECT * FROM cypher('cypher_union', $$MATCH (n) RETURN n UNION ALL MATCH (n) re
SELECT * FROM cypher('cypher_union', $$MATCH (n) RETURN n UNION MATCH (n) return n UNION ALL MATCH(n) RETURN n$$) AS (result agtype);

/* scoping */
SELECT * FROM cypher('cypher_union', $$MATCH (n) RETURN n UNION ALL MATCH (m) RETURN n$$) AS (result agtype);
SELECT * FROM cypher('cypher_union', $$MATCH (n) RETURN n UNION ALL MATCH (m) RETURN n$$) AS (result agtype);

/*
*UNION and UNION ALL, type casting
*/
SELECT * FROM cypher('cypher_union', $$ RETURN 1.0::int UNION return 1::float UNION ALL RETURN 2.0::float $$) AS (result agtype);

SELECT * FROM cypher('cypher_union', $$ RETURN 1.0::float UNION return 1::int UNION RETURN 1::float $$) AS (result agtype);


SELECT drop_graph('cypher_union', true);
@@ -921,17 +921,24 @@ transform_cypher_union_tree(cypher_parsestate *cpstate, cypher_clause *clause,
SortGroupClause *grpcl = makeNode(SortGroupClause);
Oid sortop;
Oid eqop;
bool hashable;
bool hashable = false;
ParseCallbackState pcbstate;

setup_parser_errposition_callback(&pcbstate, pstate,
bestlocation);

/* determine the eqop and optional sortop */
/*
* determine the eqop and optional sortop
*
* NOTE: for UNION, we set hashable to false and pass a NULL to
* isHashable in get_sort_group_operators to prevent a logic error
* where UNION fails to exclude duplicate results.
*
*/
get_sort_group_operators(rescoltype,
false, true, false,
&sortop, &eqop, NULL,
&hashable);
NULL);

cancel_parser_errposition_callback(&pcbstate);

0 comments on commit ed283c0

Please sign in to comment.