kclay / rethink-scala

Scala Driver for RethinkDB
Other
100 stars 24 forks source link

Grouping and couting data #48

Open gstraymond opened 8 years ago

gstraymond commented 8 years ago

Hi,

I try using group() and count() on a table like : https://www.rethinkdb.com/api/java/group/

r.table("matches").group(
    match -> r.array(match.g("date").year(), match.g("date").month())
).count().run(conn);
[
    {
        "group": [2014, 2],
        "values": 2
    },
    {
        "group": [2014, 3],
        "values": 2
    },
    ...
]

The goal is to simply group data by a field and count grouped occurences. In my code, this looks like :

def getLeaderboard: Future[Double] = {
    r.table("stats").group("userId").count().run
}

First problem, to make code compile to function must return a Future[Double], I would expect something like Future[Map[String, Double]] or Future[Seq[(String, Double)]]. Then, when I try to run the code and get this in return (this is logic since the return type is not correct) :

RethinkRuntimeError: Can not deserialize instance of java.lang.Double out of START_OBJECT token at 
[Source: {"t":1,"r":[{"$reql_type$":"GROUPED_DATA","data":[["foo",143]]}]}; line: 1, column: 13] 
(through reference chain: com.rethinkscala.net.JsonResponse["r"]->com.fasterxml.jackson.module.scala.deser.BuilderWrapper[0])]

I think the request is good, since in the error I can see the data grouped and counted.

BTW, I'm using : "com.rethinkscala" %% "core" % "0.4.8-SNAPSHOT"

Any help appreciated !

kclay commented 8 years ago

Group and ungroup have always been a problem to support due to the ability to group by different data types. I'll take another look into this to see if I can at least provide some casting methods so the user can instruct the driver of the correct type when it fails to pick the correct type.