[GitHub] Group by nested fields

Moved from GitHub dgraph/1562

Posted by raitraidma:

It would be great to be able to group by nested fields.

For example, when data is structured like that:

mutation {
  schema {
    name: string @index(hash) .
    geoPoint: geo .
  }

  set {
    _:company1 <name> "Company A" .
    _:company1 <pickupFrom> _:company1_pickup_from .
    _:company1 <deliverTo> _:company1_deliver_to .
    _:company1_pickup_from <geoPoint> "{'type':'Point','coordinates':[0.0,0.0]}" .
    _:company1_deliver_to <geoPoint> "{'type':'Point','coordinates':[10.0,11.0]}" .

    _:company2 <name> "Company A" .
    _:company2 <pickupFrom> _:company2_pickup_from .
    _:company2 <deliverTo> _:company2_deliver_to .
    _:company2_pickup_from <geoPoint> "{'type':'Point','coordinates':[0.0,0.0]}" .
    _:company2_deliver_to <geoPoint> "{'type':'Point','coordinates':[10.0,11.0]}" .

    _:company3 <name> "Company A" .
    _:company3 <pickupFrom> _:company3_pickup_from .
    _:company3 <deliverTo> _:company3_deliver_to .
    _:company3_pickup_from <geoPoint> "{'type':'Point','coordinates':[0.0,0.0]}" .
    _:company3_deliver_to <geoPoint> "{'type':'Point','coordinates':[13.0,14.0]}" .
  }
}

Query might be something like that:

{
  q(func: eq(name, "Company A")) @groupby(<pickupFrom>.<geoPoint>,  <deliverTo>.<geoPoint>) {
    count(_uid_)
  }
}

Expected result something like that:

[
    {
        pickupFrom: {'type':'Point','coordinates':[0.0,0.0]},
        deliverTo: {'type':'Point','coordinates':[10.0,11.0]},
        count: 2
    },
    {
        pickupFrom: {'type':'Point','coordinates':[0.0,0.0]},
        deliverTo: {'type':'Point','coordinates':[13.0,14.0]},
        count: 1
    }
]

campoy commented :

In order to simplify the problem statement lets’ forget about geo features for now.

A different example that I hope shows what you’re requesting is the following.

Given the data in the mutation below:

{
	"set": [
    {
      "name": "Bob",
      "school": {
        "uid": "_:mit",
        "name": "MIT"
      }
    },
    {
      "name": "Alice",
      "school": {
        "uid": "_:mit",
        "name": "MIT"
      }
    },
    {
      "name": "Charlie",
      "school": {
        "uid": "_:standford",
        "name": "Stanford"
      }
    }    
  ]
}

You’d like to be able to group by school name directly. Something similar to this:

{
	q(func: has(school)) @groupby(<school>.<name>) {
  	count(uid)
  }
}

Which is, indeed, not supported.

I’ve been trying to implement this query in other ways using vars etc, but I did not succeed.

Maybe @danielmai can help with this so we can identify whether it’s necessary to build a new feature?

AugustHell commented :

+1 from me.
I think the groupby would need a rework as it does not do the thing that groupby does in eg sql.
Would be very nice, to have a filter with “having” then too :slight_smile:

{
	q(func: has(school)) @groupby(<school>.<name>) @having(eq(<school>.<name>,"MIT")) {
  	count(uid)
  }
}