1.3.0 - aggregate functions do not work on embedded documents collection
Versions: -1.3.0 release -OrientDB 1.4.0-SNAPSHOT 20130227-0214
Hi, could you try the following sequence:
create class collection
insert into collection set data = [{"size": 0}, {"size": 0}, {"size": 30}]
select
data.size,
data.size(),
min(data.size),
max(data.size),
sum(data.size),
avg(data.size)
from collection
returned: { "result": [{ "@type": "d", "@rid": "#-2:0", "@version": 0, "data": [0, 30], "data2": 3, "sum": 30, "avg": 15 } ] }
expected: { "result": [{ "@type": "d", "@rid": "#-2:0", "@version": 0, "data": [0, 0, 30], "data2": 3, "min": 0, "max": 30 "sum": 30, "avg": 10 } ] }
It seems the aggregation is made on the set of values (not the list) .
I don't know if it's right the current behavior or not, but try this:
select list(data.size), data.size(), min(data.size), max(data.size), sum(data.size), avg(data.size)
from collection
Luca,
running the following query (added list function as you suggested) does not change anything:
select list(data.size), data.size(), min( list(data.size)), max( list(data.size)), sum( list(data.size)), avg(list(data.size)) from collection
The result is the same and it is actually inconsistent.
On the input we have collection of documents: [{"size": 0}, {"size": 0}, {"size": 30}] So the query's task is to aggregate the following collection of numbers [0, 0, 30]
And the result says: "data": [0, 30], -> 1 element form the collection missed "data2": 3, -> size of the collection =3 (OK) "sum": 30, -> OK "avg": 15 -> incorrect; should be =10 (30/3) min, max are not calculated at all -> incorrect
It is a bug from my point of view.
Regards Dan.
So the data is not present in database?
Luca,
I am sorry for inconvenience. I try to explain it more clearly.
If you prepare data: 1. create class collection insert into collection set data = [{"size": 0}, {"size": 0}, {"size": 30}, {"size": 50}, {"size": 50}]
and run this query: 2. select from collection
you get the the correct content in DB:
{"result":[{"@type":"d","@rid":"#11:0","@version":0,"@class":"collection", "data":[{"size":0},{"size":0},{"size":30},{"size":50},{"size":50}] }]}
- But if you try to calculate aggregates on "size" attribute from that collection [[{"size":0},{"size":0},{"size":30},{"size":50},{"size":50}] you get incorrect result.
select data.size as collection_content, data.size() as collection_size, min(data.size) as collection_min, max(data.size) as collection_max, sum(data.size) as collection_sum, avg(data.size) as collection_avg from collection
{"result":[{"@type":"d","@rid":"#-2:0","@version":0, "collection_content":[0,50,30]-> incorrect: should be [0,0, 30, 50, 50] as in the source data "collection_size":5,-> OK "collection_sum":80, -> incorrect: should be =130 "collection_avg":26} -> OK ]}
-if you try with [0, 0, 30, 50] {"result":[{"@type":"d","@rid":"#-2:0","@version":0, "collection_content":[0,50,30], -> incorrect: should be [0, 0, 30, 50] as in the source data "collection_size":4,-> OK: "collection_sum":80,-> OK: "collection_avg":26}-> incorrect; should be =20 (80/4) ]}
-if you try with [0,0,30] {"result":[{"@type":"d","@rid":"#-2:0","@version":0, "collection_content":[0,30], -> incorrect: should be [0,0, 30] as in the source data "collection_size":3, -> OK: "collection_sum":30, -> OK "collection_avg":15 -> incorrect; should be =10 (30/3) } ] }
You can note the following:
- min max aggregates have not been calculated.
- avg, sum result depends on input and sometimes is calculated incorrectly
Regards Dan
this is fixed in develop branch. I just added a test case.
Is this merged to 2.2 branch ?