RJKeevil
(Rob Keevil)
October 3, 2020, 12:41pm
1
Hi,
Im trying (and failing) to write a query that will add a summary link between two nodes, via a 3 hop traversal, and totalling the value of the facets on route. Take the following graph:
Which is fetched from the following query
{
trxs(func: type(Person)) @filter(gt(count(Account.transactedTo), 0)) {
uid
a: Person.hasAccount @filter(gt(count(Account.transactedTo), 0)) {
uid
b: Account.transactedTo @facets(amount) {
uid
~a:~Person.hasAccount {uid}
}
}
}
}
For all pairs of Person → Person with this pattern I would like to add a summary link, which sums the facet “amount”, in this case amount = 5.
The examples make it clear I need to use variables to acheive this. The examples group by the initial uid, but in my case I need to group by unique pairs of Person uids. Does anyone know how this can be done?
MichelDiz
(Michel Diz)
October 3, 2020, 2:29pm
2
RJKeevil
(Rob Keevil)
October 4, 2020, 9:05am
3
Hi @MichelDiz yes I tried to make these examples work before posting, I think the complexity of my query is that I need it for unique person pairs , i.e. Person A sent $5 to Person B via 2 transactions, and Person A sent $7 to Person C via 3 transactions etc. I dont see how i can group by Uid pairs currently. Edit: Missed this , should work. Ill test today and update the query for future reference.
MichelDiz
(Michel Diz)
October 4, 2020, 3:38pm
4
Give a sample of your dataset so I can analyze it and give you a proper answer.
Cheers.
1 Like
RJKeevil
(Rob Keevil)
October 4, 2020, 7:02pm
5
Hi,
Here is an example JSON for the scenario in my prev post. 0x167895f → 0x1678945 should total 5, 0x167895f → 0x1678946 should total 7
{
"uid": "0x167895f",
"a": [
{
"uid": "0x1678943",
"b": [
{
"uid": "0x167894a",
"~a": [
{
"uid": "0x1678945"
}
],
"b|amount": "2"
}
]
},
{
"uid": "0x167896d",
"b": [
{
"uid": "0x167896a",
"~a": [
{
"uid": "0x1678945"
}
],
"b|amount": "3"
}
]
},
{
"uid": "0x167896f",
"b": [
{
"uid": "0x168896a",
"~a": [
{
"uid": "0x1678946"
}
],
"b|amount": "1"
}
]
},
{
"uid": "0x167896b",
"b": [
{
"uid": "0x167897d",
"~a": [
{
"uid": "0x1678946"
}
],
"b|amount": "1"
}
]
},
{
"uid": "0x167896c",
"b": [
{
"uid": "0x167897e",
"~a": [
{
"uid": "0x1678946"
}
],
"b|amount": "5"
}
]
}
]
}
MichelDiz
(Michel Diz)
October 4, 2020, 8:03pm
6
So basically your issue is just like this one Aggregating facet variables
See my last comment there.
Also see this example
opened 10:27PM - 11 Oct 19 UTC
closed 04:52AM - 21 Jul 20 UTC
kind/bug
priority/P1
status/accepted
area/facets
## What version of Dgraph are you using?
master
### Have you tried reprodu… cing the issue with the latest release?
yes
### What is the hardware spec (RAM, OS)?
n/a
### Steps to reproduce the issue (command/config used to run Dgraph).
Given the dataset generated by this mutation:
```rdf
{
set {
_:a <name> "Anne" .
_:b <name> "Brian" .
_:jp <name> "Jurassic Park" .
_:ij <name> "Indiana Jones" .
_:a <rated> _:jp (rating=5) .
_:a <rated> _:ij (rating=2) .
_:b <rated> _:ij (rating=2) .
}
}
```
If you run the following request:
```graphql
{
q(func: has(rated)) {
name
rated @facets(r as rating)
partial_sum: sum(val(r))
}
sum() {
total_sum: sum(val(r))
}
}
```
### Expected behaviour and actual result.
I'd expect `partial_sum` to be 7 for Anne and 2 for Brian, then `total_sum` would be 9.
Instead, the result is as follows:
```json
{
"data": {
"q": [
{
"name": "Anne",
"rated": [
{
"rated|rating": 5
},
{
"rated|rating": 2
}
],
"partial_sum": 9
},
{
"name": "Brian",
"rated": [
{
"rated|rating": 2
}
],
"partial_sum": 4
}
],
"sum": [
{
"total_sum": 9
}
]
}
}
```
I have a theory about why we're getting these weird numbers.
Variables attach values to uid, but in this case that's not the right behavior, as the value of the variable should not be attached to the UID of the person nor the movie, but rather the combination of both linked by the predicate.
You can see the weird artifact by querying by this value on all of the nodes.
```graphql
{
var(func: has(rated)) {
rated @facets(r as rating)
}
sum(func: has(name)) {
name
val(r)
}
}
```
returns
```json
{
"data": {
"sum": [
{
"name": "Jurassic Park",
"val(r)": 5
},
{
"name": "Indiana Jones",
"val(r)": 4
},
{
"name": "Anne"
},
{
"name": "Brian"
}
]
}
}
```
This proves that the variable `r` has been attached to the movie UIDs by adding all of the values in the facets pointing to them.
Once we understand this, it makes sense that the sum of the ratings for Anne is 9 instead of 7, as it's the sum of the ratings for the two movies. Same goes for the ratings for Brian being 4 instead of 2.
Fixing this might be complicated, as it might imply making variables work as a map from <uid, uid> to value rather than <uid> to value.