Vars can be assigned only when grouped by UID attribute

hello everyone
I have the same error

Vars can be assigned only when grouped by UID attribute while executing this query

{
 var(func: has(evt_uniq_visitor)) @groupby(evt_uniq_visitor){
     data as  count(uid)
  }
  res(func:uid(data)){
  evt_uniq_visitor  
  }
  
  
}

but I remove res function work very well also I tested the same query in another database it works I don’t know why doesn’t work here and what this error mean

1 Like

Is it like this?

{
  xx(func: has(evt_uniq_visitor)) @groupby(evt_uniq_visitor){
     count(uid)
  }
}

This is correct because you do not use var.

evt_uniq_visitor is an attribute of a vertex, not an edge, so var cannot be used when grouping by it.
This is a limitation of dql, no way.

1 Like

but I execute the same query in another database it works for me
also, I wanna get data for each evt_uniq_visitor that’s why I want to get them in var

just ran into this issue. frustrating.

edit: looks like a fix(?) for this has been merged into master: feat(DQL): @groupby on scalar fields and count duplicate by minhaj-shakeel · Pull Request #7746 · dgraph-io/dgraph · GitHub

I have created a cherry-pick for this - it may come in a next release.

2 Likes

Update. A big problem. This feature depends in the Roaring Bitmaps support. That means it will take longer. Unless we edit the code of the cherry-pick if we choose to not support Roaring Bitmaps.

2 Likes

The following query works as expected:

me(func: type(Product)) @groupby(Product.size) {
  count(uid)
}

However, when trying to define a variable, I get the error: “Vars can be assigned only when grouped by UID attribute.”
The query that causes the error:

var(func: type(Product)) @groupby(Product.size) {
  size as count(uid)
}
me(func: uid(size)) {
  uid
  val(size)
}

uid(variable) returns the unique identifier of nodes.

When you write uid(size) which node do you expect to get?
it’s a rhetorical question to explain why it does not make sense:

In fact there are no nodes in the graph representing what you count.
The @groupby(Product.size) can give you an array and each item in the array will have a Product.size value and a count value, but those items are not nodes in the graph and have no uid.

It makes no sense for Dgraph to provide uids of the @groupby result, unless the groupby is done on nodes (which have uids).

That explains the error message you see and why you cannot use variable in that case.

If you need the flexibility of considering your Product.size as nodes then you can design the graph schema so that Product.size is the relation predicate to a ProductSize node with a value.

Doing so you “materialize” sizes values with nodes and you can aggregate along those nodes and obtain a variable (variables in Dgraph are maps uid → values).