Skip to content

Commit

Permalink
Fix issue apache#1302 - crash on NULL input to UNWIND
Browse files Browse the repository at this point in the history
- Added a check for NULL input in age_unnest.
- Added regression tests.
  • Loading branch information
MuhammadTahaNaveed committed Oct 26, 2023
1 parent ae058ef commit bfc5d7c
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
69 changes: 69 additions & 0 deletions regress/expected/cypher_unwind.out
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,65 @@ $$) as (i agtype);
{"id": 281474976710659, "label": "", "properties": {"a": [7, 8, 9], "name": "node3", "type": "vertex"}}::vertex
(3 rows)

--
-- Issue 1302
--
SELECT create_graph('issue_1302');
NOTICE: graph "issue_1302" has been created
create_graph
--------------

(1 row)

SELECT * FROM cypher('cypher_unwind', $$
CREATE (agtype {name: 'node1', a: [1, 2, 3]}),
(m {name: 'node2', a: [4, 5, 6]}),
(o {name: 'node3', a: [7, 8, 9]}),
(n)-[:KNOWS]->(m),
(m)-[:KNOWS]->(o)
$$) as (i agtype);
i
---
(0 rows)

SELECT * FROM cypher('cypher_unwind', $$
MATCH (n)
WITH n.a AS a
UNWIND a AS i
RETURN *
$$) as (i agtype, j agtype);
i | j
-----------+---
[1, 2, 3] | 1
[1, 2, 3] | 2
[1, 2, 3] | 3
[4, 5, 6] | 4
[4, 5, 6] | 5
[4, 5, 6] | 6
[7, 8, 9] | 7
[7, 8, 9] | 8
[7, 8, 9] | 9
[1, 2, 3] | 1
[1, 2, 3] | 2
[1, 2, 3] | 3
[4, 5, 6] | 4
[4, 5, 6] | 5
[4, 5, 6] | 6
[7, 8, 9] | 7
[7, 8, 9] | 8
[7, 8, 9] | 9
|
(19 rows)

SELECT * FROM cypher('cypher_unwind', $$
UNWIND NULL as i
RETURN i
$$) as (i agtype);
i
---

(1 row)

--
-- Clean up
--
Expand All @@ -214,3 +273,13 @@ NOTICE: graph "cypher_unwind" has been dropped

(1 row)

SELECT drop_graph('issue_1302', true);
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to table issue_1302._ag_label_vertex
drop cascades to table issue_1302._ag_label_edge
NOTICE: graph "issue_1302" has been dropped
drop_graph
------------

(1 row)

29 changes: 28 additions & 1 deletion regress/sql/cypher_unwind.sql
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,35 @@ SELECT * FROM cypher('cypher_unwind', $$
RETURN n
$$) as (i agtype);

--
-- Issue 1302
--

SELECT create_graph('issue_1302');

SELECT * FROM cypher('cypher_unwind', $$
CREATE (agtype {name: 'node1', a: [1, 2, 3]}),
(m {name: 'node2', a: [4, 5, 6]}),
(o {name: 'node3', a: [7, 8, 9]}),
(n)-[:KNOWS]->(m),
(m)-[:KNOWS]->(o)
$$) as (i agtype);

SELECT * FROM cypher('cypher_unwind', $$
MATCH (n)
WITH n.a AS a
UNWIND a AS i
RETURN *
$$) as (i agtype, j agtype);

SELECT * FROM cypher('cypher_unwind', $$
UNWIND NULL as i
RETURN i
$$) as (i agtype);

--
-- Clean up
--

SELECT drop_graph('cypher_unwind', true);
SELECT drop_graph('cypher_unwind', true);
SELECT drop_graph('issue_1302', true);
9 changes: 8 additions & 1 deletion src/backend/utils/adt/agtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -10949,7 +10949,7 @@ PG_FUNCTION_INFO_V1(age_unnest);
*/
Datum age_unnest(PG_FUNCTION_ARGS)
{
agtype *agtype_arg = AG_GET_ARG_AGTYPE_P(0);
agtype *agtype_arg = NULL;
ReturnSetInfo *rsi;
Tuplestorestate *tuple_store;
TupleDesc tupdesc;
Expand All @@ -10960,6 +10960,13 @@ Datum age_unnest(PG_FUNCTION_ARGS)
agtype_value v;
agtype_iterator_token r;

// check for null
if (PG_ARGISNULL(0))
{
PG_RETURN_NULL();
}

agtype_arg = AG_GET_ARG_AGTYPE_P(0);
if (!AGT_ROOT_IS_ARRAY(agtype_arg))
{
ereport(ERROR,
Expand Down

0 comments on commit bfc5d7c

Please sign in to comment.