Aggregation of Facet with type datetime

Hi,

currently i am working on an Uni Project, where we have to measure the response time from different Querys on different graph databases. My group chose Dgraph.
Since i am struggling with a certain query and can’t get it to run without errors, i thought i ask more experienced people in this forum.

The Query is the following:

{
	var(func: has(f_relationships)){
		f_relationships @facets(d as date){ 
                     dateInt as math(since(d))
                }
        }
      
        data(func: uid(dateVar)){
		avgDate : avg(val(dateInt))
        }
}

The facet date has the type datetime and the goal is to create a query which computes the average of every date on every edge.

While testing i always come back to the same error.

Repeated id with non int/float value for facet var encountered.

The Documentation of Dgraph on aggregation with facets seems to use integer in their example and since my facet has the type datetime it seems to have problems with it, but i could be interpreting this error completly wrong.

I hope there is someone, who can help me :slight_smile: .

Hi @Wockel,

That’s right, the average function is only available for ints and floats, hence the error. See https://dgraph.io/docs/query-language/aggregation.

Hi @matthewmcneely,

okay but i am using the since function before the average function and if i am understanding the since function correctly its converts the datetime attribute into a new float attribute.

The error disappears after i change the has(f_relationships) into eq(f_id,1), which results in one Node thats returned.

@Wockel Hmm, I don’t see dateVar being set anywhere in your opening block.

There still maybe be an issue with the dateInt being declared at that level. Can you respond with the relevant parts of your schema and a some example predicate values? If so, I’ll try to reproduce and figure out a work-around.

Hi @matthewmcneely,

oh i forgot to change the dateVar to dateInt before i posted it here, so the “correct” query would be

{
	var(func: has(f_relationships)){
		f_relationships @facets(d as date){ 
                     dateInt as math(since(d))
                }
        }
      
        data(func: uid(dateInt)){
		avgDate : avg(val(dateInt))
        }
}

I used the Facets and Aggregation Dgraph Docs as reference.

The relevant part of the schema would be:

<f_relationships>: [uid] @reverse .
<f_id>: int @index(int) 

some example Mutation so that you can understand the data better:

_:n1 <f_id> "1" .
_:n1 <f_relationships> _:n2 (date="2016-08-07") .
_:n1 <f_relationships> _:n3 (date="2016-08-05") .
_:n2 <f_id> "2" .
_:n2 <f_relationships> _:n1 (date="2016-08-07") .
_:n3 <f_id> "3" .
_:n3 <f_relationships> _:n1 (date="2016-08-05") .

If you could find a way, to create a work around without changing the way the data is modelt, that would be very helpful :slight_smile: .

Thanks for that. I looked in the codebase and it seems even tho you’ve math'd the result of since, the query builder still thinks that the variable is not an int or float.

@MichelDiz Do you have any ideas here? I see from an issue in Github you were working with something similar to this a while back. The same issue exists in 21.12.

Can you point me it? This issue feels different. I think only Aman or Harshil would be able to understand the case.

I have my theories. I think as facets don’t have a type, it’s a pure string. This can influence the parser for datetime(just a guess). Perhaps there needed to be a conversion from 2016-08-07 to 2016-08-07T00:00:00Z? And have the type determined in the map.

This use case might be a feature request for facets.

I believe that the easiest way to resolve this is to convert this data into intermediate nodes. Since facet isn’t first class citizen.