Skip to content

Commit 54e0017

Browse files
committed
[AGE agtype_util.c] Fix issue #870 regarding orderability and added regression tests
Fixed issue #870 Odd behavior in context of orderability of different agtypes. Implemented the solution suggested here: #870 (comment) Clearance given here: #870 (comment)
1 parent e22c5ac commit 54e0017

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

regress/expected/agtype.out

+19-1
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ SELECT agtype_in('{"bool":true}') < agtype_in('{"bool":true, "null": null}');
15231523
(1 row)
15241524

15251525
-- Comparisons between types
1526-
-- Object < List < String < Boolean < Integer = Float = Numeric < Null
1526+
-- Path < Edge < Vertex < Object < List < String < Boolean < Integer = Float = Numeric < Null
15271527
SELECT agtype_in('1') < agtype_in('null');
15281528
?column?
15291529
----------
@@ -1590,6 +1590,24 @@ SELECT agtype_in('{"bool":true, "integer":1}') < agtype_in('{"bool":true, "integ
15901590
t
15911591
(1 row)
15921592

1593+
SELECT agtype_in('{"id":0, "label": "v", "properties":{"i":0}}::vertex') < agtype_in('{"bool":true, "i":0}');
1594+
?column?
1595+
----------
1596+
t
1597+
(1 row)
1598+
1599+
SELECT agtype_in('{"id":2, "start_id":0, "end_id":1, "label": "e", "properties":{"i":0}}::edge') < agtype_in('{"id":0, "label": "v", "properties":{"i":0}}::vertex');
1600+
?column?
1601+
----------
1602+
t
1603+
(1 row)
1604+
1605+
SELECT agtype_in('[{"id": 0, "label": "v", "properties": {"i": 0}}::vertex, {"id": 2, "start_id": 0, "end_id": 1, "label": "e", "properties": {"i": 0}}::edge, {"id": 1, "label": "v", "properties": {"i": 0}}::vertex]::path') < agtype_in('{"id":2, "start_id":0, "end_id":1, "label": "e", "properties":{"i":0}}::edge');
1606+
?column?
1607+
----------
1608+
t
1609+
(1 row)
1610+
15931611
SELECT agtype_in('1::numeric') < agtype_in('null');
15941612
?column?
15951613
----------

regress/sql/agtype.sql

+4-1
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ SELECT agtype_in('{"bool":true, "null": null}') = agtype_in('{"null":null, "bool
403403
SELECT agtype_in('{"bool":true}') < agtype_in('{"bool":true, "null": null}');
404404

405405
-- Comparisons between types
406-
-- Object < List < String < Boolean < Integer = Float = Numeric < Null
406+
-- Path < Edge < Vertex < Object < List < String < Boolean < Integer = Float = Numeric < Null
407407
SELECT agtype_in('1') < agtype_in('null');
408408
SELECT agtype_in('NaN') < agtype_in('null');
409409
SELECT agtype_in('Infinity') < agtype_in('null');
@@ -415,6 +415,9 @@ SELECT agtype_in('[1,3,5,7,9,11]') < agtype_in('"string"');
415415
SELECT agtype_in('{"bool":true, "integer":1}') < agtype_in('[1,3,5,7,9,11]');
416416
SELECT agtype_in('[1, "string"]') < agtype_in('[1, 1]');
417417
SELECT agtype_in('{"bool":true, "integer":1}') < agtype_in('{"bool":true, "integer":null}');
418+
SELECT agtype_in('{"id":0, "label": "v", "properties":{"i":0}}::vertex') < agtype_in('{"bool":true, "i":0}');
419+
SELECT agtype_in('{"id":2, "start_id":0, "end_id":1, "label": "e", "properties":{"i":0}}::edge') < agtype_in('{"id":0, "label": "v", "properties":{"i":0}}::vertex');
420+
SELECT agtype_in('[{"id": 0, "label": "v", "properties": {"i": 0}}::vertex, {"id": 2, "start_id": 0, "end_id": 1, "label": "e", "properties": {"i": 0}}::edge, {"id": 1, "label": "v", "properties": {"i": 0}}::vertex]::path') < agtype_in('{"id":2, "start_id":0, "end_id":1, "label": "e", "properties":{"i":0}}::edge');
418421
SELECT agtype_in('1::numeric') < agtype_in('null');
419422
SELECT agtype_in('true') < agtype_in('1::numeric');
420423

src/backend/utils/adt/agtype_util.c

+41-7
Original file line numberDiff line numberDiff line change
@@ -197,20 +197,42 @@ uint32 get_agtype_length(const agtype_container *agtc, int index)
197197
*/
198198
static int get_type_sort_priority(enum agtype_value_type type)
199199
{
200-
if (type == AGTV_OBJECT)
200+
if (type == AGTV_PATH)
201+
{
201202
return 0;
202-
if (type == AGTV_VERTEX)
203+
}
204+
if (type == AGTV_EDGE)
205+
{
203206
return 1;
204-
if (type == AGTV_ARRAY)
207+
}
208+
if (type == AGTV_VERTEX)
209+
{
205210
return 2;
206-
if (type == AGTV_STRING)
211+
}
212+
if (type == AGTV_OBJECT)
213+
{
207214
return 3;
208-
if (type == AGTV_BOOL)
215+
}
216+
if (type == AGTV_ARRAY)
217+
{
209218
return 4;
210-
if (type == AGTV_NUMERIC || type == AGTV_INTEGER || type == AGTV_FLOAT)
219+
}
220+
if (type == AGTV_STRING)
221+
{
211222
return 5;
212-
if (type == AGTV_NULL)
223+
}
224+
if (type == AGTV_BOOL)
225+
{
213226
return 6;
227+
}
228+
if (type == AGTV_NUMERIC || type == AGTV_INTEGER || type == AGTV_FLOAT)
229+
{
230+
return 7;
231+
}
232+
if (type == AGTV_NULL)
233+
{
234+
return 8;
235+
}
214236
return -1;
215237
}
216238

@@ -356,6 +378,18 @@ int compare_agtype_containers_orderability(agtype_container *a,
356378
break;
357379
}
358380

381+
/* Correction step because AGTV_ARRAY might be there just because of the container type */
382+
/* Case 1: left side is assigned to an array, right is an object */
383+
if(va.type == AGTV_ARRAY && vb.type == AGTV_OBJECT)
384+
{
385+
ra = agtype_iterator_next(&ita, &va, false);
386+
}
387+
/* Case 2: left side is an object, right side is assigned to an array */
388+
else if(va.type == AGTV_OBJECT && vb.type == AGTV_ARRAY)
389+
{
390+
rb = agtype_iterator_next(&itb, &vb, false);
391+
}
392+
359393
Assert(va.type != vb.type);
360394
Assert(va.type != AGTV_BINARY);
361395
Assert(vb.type != AGTV_BINARY);

0 commit comments

Comments
 (0)