Skip to content

Commit 692cb02

Browse files
Fix issue #1302 - crash on NULL input to UNWIND (#1304)
- Added a check for NULL input in age_unnest. - Added regression tests.
1 parent ae058ef commit 692cb02

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
lines changed

regress/expected/cypher_unwind.out

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,65 @@ $$) as (i agtype);
200200
{"id": 281474976710659, "label": "", "properties": {"a": [7, 8, 9], "name": "node3", "type": "vertex"}}::vertex
201201
(3 rows)
202202

203+
--
204+
-- Issue 1302
205+
--
206+
SELECT create_graph('issue_1302');
207+
NOTICE: graph "issue_1302" has been created
208+
create_graph
209+
--------------
210+
211+
(1 row)
212+
213+
SELECT * FROM cypher('cypher_unwind', $$
214+
CREATE (agtype {name: 'node1', a: [1, 2, 3]}),
215+
(m {name: 'node2', a: [4, 5, 6]}),
216+
(o {name: 'node3', a: [7, 8, 9]}),
217+
(n)-[:KNOWS]->(m),
218+
(m)-[:KNOWS]->(o)
219+
$$) as (i agtype);
220+
i
221+
---
222+
(0 rows)
223+
224+
SELECT * FROM cypher('cypher_unwind', $$
225+
MATCH (n)
226+
WITH n.a AS a
227+
UNWIND a AS i
228+
RETURN *
229+
$$) as (i agtype, j agtype);
230+
i | j
231+
-----------+---
232+
[1, 2, 3] | 1
233+
[1, 2, 3] | 2
234+
[1, 2, 3] | 3
235+
[4, 5, 6] | 4
236+
[4, 5, 6] | 5
237+
[4, 5, 6] | 6
238+
[7, 8, 9] | 7
239+
[7, 8, 9] | 8
240+
[7, 8, 9] | 9
241+
[1, 2, 3] | 1
242+
[1, 2, 3] | 2
243+
[1, 2, 3] | 3
244+
[4, 5, 6] | 4
245+
[4, 5, 6] | 5
246+
[4, 5, 6] | 6
247+
[7, 8, 9] | 7
248+
[7, 8, 9] | 8
249+
[7, 8, 9] | 9
250+
|
251+
(19 rows)
252+
253+
SELECT * FROM cypher('cypher_unwind', $$
254+
UNWIND NULL as i
255+
RETURN i
256+
$$) as (i agtype);
257+
i
258+
---
259+
260+
(1 row)
261+
203262
--
204263
-- Clean up
205264
--
@@ -214,3 +273,13 @@ NOTICE: graph "cypher_unwind" has been dropped
214273

215274
(1 row)
216275

276+
SELECT drop_graph('issue_1302', true);
277+
NOTICE: drop cascades to 2 other objects
278+
DETAIL: drop cascades to table issue_1302._ag_label_vertex
279+
drop cascades to table issue_1302._ag_label_edge
280+
NOTICE: graph "issue_1302" has been dropped
281+
drop_graph
282+
------------
283+
284+
(1 row)
285+

regress/sql/cypher_unwind.sql

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,35 @@ SELECT * FROM cypher('cypher_unwind', $$
122122
RETURN n
123123
$$) as (i agtype);
124124

125+
--
126+
-- Issue 1302
127+
--
128+
129+
SELECT create_graph('issue_1302');
130+
131+
SELECT * FROM cypher('cypher_unwind', $$
132+
CREATE (agtype {name: 'node1', a: [1, 2, 3]}),
133+
(m {name: 'node2', a: [4, 5, 6]}),
134+
(o {name: 'node3', a: [7, 8, 9]}),
135+
(n)-[:KNOWS]->(m),
136+
(m)-[:KNOWS]->(o)
137+
$$) as (i agtype);
138+
139+
SELECT * FROM cypher('cypher_unwind', $$
140+
MATCH (n)
141+
WITH n.a AS a
142+
UNWIND a AS i
143+
RETURN *
144+
$$) as (i agtype, j agtype);
145+
146+
SELECT * FROM cypher('cypher_unwind', $$
147+
UNWIND NULL as i
148+
RETURN i
149+
$$) as (i agtype);
150+
125151
--
126152
-- Clean up
127153
--
128154

129-
SELECT drop_graph('cypher_unwind', true);
155+
SELECT drop_graph('cypher_unwind', true);
156+
SELECT drop_graph('issue_1302', true);

src/backend/utils/adt/agtype.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10949,7 +10949,7 @@ PG_FUNCTION_INFO_V1(age_unnest);
1094910949
*/
1095010950
Datum age_unnest(PG_FUNCTION_ARGS)
1095110951
{
10952-
agtype *agtype_arg = AG_GET_ARG_AGTYPE_P(0);
10952+
agtype *agtype_arg = NULL;
1095310953
ReturnSetInfo *rsi;
1095410954
Tuplestorestate *tuple_store;
1095510955
TupleDesc tupdesc;
@@ -10960,6 +10960,13 @@ Datum age_unnest(PG_FUNCTION_ARGS)
1096010960
agtype_value v;
1096110961
agtype_iterator_token r;
1096210962

10963+
// check for null
10964+
if (PG_ARGISNULL(0))
10965+
{
10966+
PG_RETURN_NULL();
10967+
}
10968+
10969+
agtype_arg = AG_GET_ARG_AGTYPE_P(0);
1096310970
if (!AGT_ROOT_IS_ARRAY(agtype_arg))
1096410971
{
1096510972
ereport(ERROR,

0 commit comments

Comments
 (0)