Skip to content

Commit

Permalink
[AGE agtype_util.c] Fix issue #870 regarding orderability and added r…
Browse files Browse the repository at this point in the history
…egression 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)
  • Loading branch information
CapnSpek committed Jun 26, 2023
1 parent e22c5ac commit 4d5f9b2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
20 changes: 19 additions & 1 deletion regress/expected/agtype.out
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,7 @@ SELECT agtype_in('{"bool":true}') < agtype_in('{"bool":true, "null": null}');
(1 row)

-- Comparisons between types
-- Object < List < String < Boolean < Integer = Float = Numeric < Null
-- Path < Edge < Vertex < Object < List < String < Boolean < Integer = Float = Numeric < Null
SELECT agtype_in('1') < agtype_in('null');
?column?
----------
Expand Down Expand Up @@ -1590,6 +1590,24 @@ SELECT agtype_in('{"bool":true, "integer":1}') < agtype_in('{"bool":true, "integ
t
(1 row)

SELECT agtype_in('{"id":0, "label": "v", "properties":{"i":0}}::vertex') < agtype_in('{"bool":true, "i":0}');
?column?
----------
t
(1 row)

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');
?column?
----------
t
(1 row)

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');
?column?
----------
t
(1 row)

SELECT agtype_in('1::numeric') < agtype_in('null');
?column?
----------
Expand Down
5 changes: 4 additions & 1 deletion regress/sql/agtype.sql
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ SELECT agtype_in('{"bool":true, "null": null}') = agtype_in('{"null":null, "bool
SELECT agtype_in('{"bool":true}') < agtype_in('{"bool":true, "null": null}');

-- Comparisons between types
-- Object < List < String < Boolean < Integer = Float = Numeric < Null
-- Path < Edge < Vertex < Object < List < String < Boolean < Integer = Float = Numeric < Null
SELECT agtype_in('1') < agtype_in('null');
SELECT agtype_in('NaN') < agtype_in('null');
SELECT agtype_in('Infinity') < agtype_in('null');
Expand All @@ -415,6 +415,9 @@ SELECT agtype_in('[1,3,5,7,9,11]') < agtype_in('"string"');
SELECT agtype_in('{"bool":true, "integer":1}') < agtype_in('[1,3,5,7,9,11]');
SELECT agtype_in('[1, "string"]') < agtype_in('[1, 1]');
SELECT agtype_in('{"bool":true, "integer":1}') < agtype_in('{"bool":true, "integer":null}');
SELECT agtype_in('{"id":0, "label": "v", "properties":{"i":0}}::vertex') < agtype_in('{"bool":true, "i":0}');
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');
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');
SELECT agtype_in('1::numeric') < agtype_in('null');
SELECT agtype_in('true') < agtype_in('1::numeric');

Expand Down
28 changes: 21 additions & 7 deletions src/backend/utils/adt/agtype_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,24 @@ uint32 get_agtype_length(const agtype_container *agtc, int index)
*/
static int get_type_sort_priority(enum agtype_value_type type)
{
if (type == AGTV_OBJECT)
if (type == AGTV_PATH)
return 0;
if (type == AGTV_VERTEX)
if (type == AGTV_EDGE)
return 1;
if (type == AGTV_ARRAY)
if (type == AGTV_VERTEX)
return 2;
if (type == AGTV_STRING)
if (type == AGTV_OBJECT)
return 3;
if (type == AGTV_BOOL)
if (type == AGTV_ARRAY)
return 4;
if (type == AGTV_NUMERIC || type == AGTV_INTEGER || type == AGTV_FLOAT)
if (type == AGTV_STRING)
return 5;
if (type == AGTV_NULL)
if (type == AGTV_BOOL)
return 6;
if (type == AGTV_NUMERIC || type == AGTV_INTEGER || type == AGTV_FLOAT)
return 7;
if (type == AGTV_NULL)
return 8;
return -1;
}

Expand Down Expand Up @@ -356,6 +360,16 @@ int compare_agtype_containers_orderability(agtype_container *a,
break;
}

/*Correction step because AGTV_ARRAY might be there just because of the container type*/
if(va.type == AGTV_ARRAY && vb.type == AGTV_OBJECT)
{
ra = agtype_iterator_next(&ita, &va, false);
}
else if(va.type == AGTV_OBJECT && vb.type == AGTV_ARRAY)
{
rb = agtype_iterator_next(&itb, &vb, false);
}

Assert(va.type != vb.type);
Assert(va.type != AGTV_BINARY);
Assert(vb.type != AGTV_BINARY);
Expand Down

0 comments on commit 4d5f9b2

Please sign in to comment.