You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Implement support for multiple aggregations after r.group #1725
I was thinking of split having semantics where the only legal operations on the value in the variable are reductions (sum etc. count as reductions for this). This constraint will let us have an efficient implementation because we won't have to deal with cases like table.do{|t| t.map{|row| row.merge({count: t.count})}. Handling that case correctly would require loading the entire table into memory, or else using different values for t both times it's referenced. By contrast, if we have a term split and only allow reductions on the bound "stream", we only have to load a single batch into memory at once to feed all the reductions.
This is in subsequent, though, so let's avoid getting too deep into it right now.
I was thinking of split having semantics where the only legal operations on the value in the variable are reductions (sum etc. count as reductions for this).
I think it would be much more intuitive to implement a command that combines reductions instead:
@mlucy it seems like you're still going to have to do some fairly heavy duty analysis to make sure that your arguments to split don't reference themselves. And I think that if we're going to do that it's not a huge step farther to just let people bind streams to variables and statically check that they're not evaluating things recursively. It also gets a lot better. For example with this avg becomes almost unnecessary. You can simply write:
table["pts"].do({|s| s.sum() / s.count()})
I'm not advocating we get rid of avg but having it be this flexible seems really powerful. I actually talked to @AtnNn about a scheme like this he had come up with and I believe actually wrote some code for in the haskell driver. I think we should definitely take a look at how nice we can get this.
@jdoliner -- that example with avg would work with the split semantics I'm proposing as well.
The analysis is actually pretty straightforward; the variable x bound by split:
Can only have terminals called on it (we already have a list of these for when we push work to the shards).
Can't be referenced from inside a nested function.
If we enforce those two constraints, we can do something efficient without too much work.
I think that supporting full-blown streams in variables in a way that we know will be efficient would be a lot more work, but I'm not necessarily opposed to it if there's an easy way to do it.
It takes any function and splits it into map, reduce and finalizer functions. When the function contains multiple aggregations, say (\row -> f row / g row), it turns it into a map of (\row -> [f_map row, g_map row]), a reduce of (\a b -> [f_reduce a[0] b[0], g_reduce a[1] b[1]]) and a finalizer of (\row -> row[0] / row[1])
It sounds to me like we're pretty close to agreeing on the functionality here. @mlucy what you're describing sounds close enough to me to binding the stream to a variable that I think it's going to be easier to explain to users as do than split. I think concept of "if I want to use the same value multiple times without recomputing it I bind it to a variable" is universal enough that this is the easiest way to explain the functionality. And when people do hit the limitations of binding streams I don't think it's going to be harder to understand than it would be if it was a differently named function. Also we will eventually be overcoming a lot of those limitations so long term it's going to be very similar to do.
Also if we don't just roll the functionality in to do I really hate the name split. We're not splitting the stream what we're doing is better described as tee or maybe clone.
I think that if we do this, we should just use do. If people use the streams incorrectly, they'll get an informative error message about the limitations, and will know what went wrong and how to use the feature. If eventually we remove the limitation, we'll just let people know and they'll be able to essentially use the same API.
However, I still think this will require a porcelain syntax like #1725 (comment) to make it easier for users to understand/type/etc. I think it's worthwhile to try to figure out what that syntax is first. It might give us bigger bang for the buck to just implement the porcelain syntax directly and drop the more general purpose implementation.
What about multi_reduce instead of split? If the only legal operations on the variable are reductions, I think using plain-old do will be more confusing than helpful.
There isn't a good name here because no matter how you name it you're still introducing an unneeded and confusing concept. Users shouldn't have to think of it as multi_reduce for one thing users don't think of count and sum as falling under the heading of reduce. Passing a stream through do is something that I've already seen tons of users do expecting mostly the semantics we've described. From what I've seen the types of operations rarely run afoul of the limitations we're talking about but most importantly when they do we can give them a very clear error message so this isn't dangerous. We're just going to be giving them that same error message when they run afoul of the rules in another function so why not just put this functionality where people expect it?
As for the porcelain command. One of the things I really like about this proposal is that it removes reductions as nouns in ReQL. Having objects like r.avg("foo") floating around in group_by worked well at the time but IMO it's a lot easier to understand if you think of avg as a verb and you only see it in the context of foo.avg() which reads as "take the average of foo." ReQL's really built around chaining and the idea that foo.bar can be thought of as "apply the action bar to the noun foo." Bringing the syntax for groups in line with this seems both very powerful and like it lets us present a much tighter coherent API.
I'm failing to see what the combine proposal is adding. I don't think it really makes queries much shorter (if any shorter). And it gives us an entirely new way of calling every single reducing function that we need to implement (actually a decent amount of work) and document.
I'm failing to see what the combine proposal is adding.
Here is what I think it would get us, and what I was trying to convey. Suppose you're doing demographics analysis and want to determine an average salary in a city. Here is how you'd do it in SQL:
SELECT city, AVG(salary)
FROM residents
GROUP BY city
And here is what this query would look like under the current proposal:
r.table('residents').group('city').avg('salary')
Now, suppose you're thinking "hmm, I wonder what this will look like if I see the average age too". In SQL, all you have to do is add AVG(age):
SELECT city, AVG(salary), AVG(age)
FROM residents
GROUP BY city
The typing distance is very short, but most importantly the conceptual distance is very short too. You don't have to learn any new concepts at all. Now, here's what this query would look like in ReQL under proposed syntax (I'm assuming the do syntax, but the same thing applies under split):
Ignoring the question of labeling the fields and what this actually returns, we're asking users to make quite a large conceptual jump. They have to introduce the do command, lambdas, and arrays to get to their data.
Given how ridiculously common it is to want to do such a thing, I think we should think about simpler porcelain syntax. That being said, I agree combine is bad syntax, and that removing the notion of aggregators as nouns is a good idea. I think we should hunt around for a better solution than combine.
@danielmewes -- I'd like to propose taking this out of the reql discussion for 2.1/polish. There are already a lot of issues to discuss, and I think this one is unlikely to get a good discussion that it deserves. How do you feel about getting back to it after 2.1 is out (or possibly after 2.2)?
@danielmewes is it possible at the moment to construct a query that allows to generate faceted search? I'm especially interested in disjunctive faceting, like:
@dmitry Can you explain a bit more what exactly you mean by faceted search?
If the categories are disjunct, can't you just do a .group("category").count() for this?
If they are not disjunct, you could use a group("categories", {multi: true}).count(), where the categories field would need to be an array of categories in each document.
@danielmewes correct example could be made only in case of the several filters.
Lets say we have "categories" with ID numbers and "prices" with ranges (0-10, 10-20). When I'm selecting "0-10" in prices and 1 in categories, other "prices" and "categories" should also have count numbers, which tells how many "items" will filtered-in after I select those categories or price ranges. The problem here is when I'm selecting category 1, the other "categories" should be also grouped. But group aggregates values only after filter makes it work, so the group could not return count of category "2" when category "2" was selected in a filter.
Simply: "categories" group should somehow skip filter by "categories", but "prices" group should include filtering by the "categories".
Goal of the disjunctive faceted search is to show only possible variants of filters, and by including more selections in one filter (categories or prices) it extends search.
Activity
jdoliner commentedon Nov 30, 2013
If we going to allow streams to be bound to variables we should just allow
it in "do" no reason to add a new keyword for it.
On Friday, November 29, 2013, coffeemug wrote:
mlucy commentedon Nov 30, 2013
I was thinking of
splithaving semantics where the only legal operations on the value in the variable are reductions (sumetc. count as reductions for this). This constraint will let us have an efficient implementation because we won't have to deal with cases liketable.do{|t| t.map{|row| row.merge({count: t.count})}. Handling that case correctly would require loading the entire table into memory, or else using different values fortboth times it's referenced. By contrast, if we have a termsplitand only allow reductions on the bound "stream", we only have to load a single batch into memory at once to feed all the reductions.This is in subsequent, though, so let's avoid getting too deep into it right now.
coffeemug commentedon Nov 30, 2013
I think it would be much more intuitive to implement a command that combines reductions instead:
(The syntax above
ismight be weird, but a step in the right direction IMO; also, sorry for commenting on this out of discussion period).jdoliner commentedon Nov 30, 2013
@mlucy it seems like you're still going to have to do some fairly heavy duty analysis to make sure that your arguments to
splitdon't reference themselves. And I think that if we're going to do that it's not a huge step farther to just let people bind streams to variables and statically check that they're not evaluating things recursively. It also gets a lot better. For example with thisavgbecomes almost unnecessary. You can simply write:I'm not advocating we get rid of
avgbut having it be this flexible seems really powerful. I actually talked to @AtnNn about a scheme like this he had come up with and I believe actually wrote some code for in the haskell driver. I think we should definitely take a look at how nice we can get this.mlucy commentedon Dec 2, 2013
@jdoliner -- that example with
avgwould work with thesplitsemantics I'm proposing as well.The analysis is actually pretty straightforward; the variable
xbound by split:If we enforce those two constraints, we can do something efficient without too much work.
I think that supporting full-blown streams in variables in a way that we know will be efficient would be a lot more work, but I'm not necessarily opposed to it if there's an easy way to do it.
AtnNn commentedon Dec 2, 2013
Here is what I put in the Haskell driver:
http://hackage.haskell.org/package/rethinkdb-1.8.0.5/docs/Database-RethinkDB.html#v:groupBy
It takes any function and splits it into map, reduce and finalizer functions. When the function contains multiple aggregations, say
(\row -> f row / g row), it turns it into a map of(\row -> [f_map row, g_map row]), a reduce of(\a b -> [f_reduce a[0] b[0], g_reduce a[1] b[1]])and a finalizer of(\row -> row[0] / row[1])jdoliner commentedon Dec 2, 2013
It sounds to me like we're pretty close to agreeing on the functionality here. @mlucy what you're describing sounds close enough to me to binding the stream to a variable that I think it's going to be easier to explain to users as
dothansplit. I think concept of "if I want to use the same value multiple times without recomputing it I bind it to a variable" is universal enough that this is the easiest way to explain the functionality. And when people do hit the limitations of binding streams I don't think it's going to be harder to understand than it would be if it was a differently named function. Also we will eventually be overcoming a lot of those limitations so long term it's going to be very similar todo.Also if we don't just roll the functionality in to
doI really hate the namesplit. We're not splitting the stream what we're doing is better described asteeor maybeclone.coffeemug commentedon Dec 2, 2013
I think that if we do this, we should just use
do. If people use the streams incorrectly, they'll get an informative error message about the limitations, and will know what went wrong and how to use the feature. If eventually we remove the limitation, we'll just let people know and they'll be able to essentially use the same API.However, I still think this will require a porcelain syntax like #1725 (comment) to make it easier for users to understand/type/etc. I think it's worthwhile to try to figure out what that syntax is first. It might give us bigger bang for the buck to just implement the porcelain syntax directly and drop the more general purpose implementation.
mlucy commentedon Dec 2, 2013
What about
multi_reduceinstead ofsplit? If the only legal operations on the variable are reductions, I think using plain-olddowill be more confusing than helpful.jdoliner commentedon Dec 3, 2013
There isn't a good name here because no matter how you name it you're still introducing an unneeded and confusing concept. Users shouldn't have to think of it as
multi_reducefor one thing users don't think ofcountandsumas falling under the heading ofreduce. Passing a stream throughdois something that I've already seen tons of users do expecting mostly the semantics we've described. From what I've seen the types of operations rarely run afoul of the limitations we're talking about but most importantly when they do we can give them a very clear error message so this isn't dangerous. We're just going to be giving them that same error message when they run afoul of the rules in another function so why not just put this functionality where people expect it?As for the porcelain command. One of the things I really like about this proposal is that it removes reductions as nouns in ReQL. Having objects like
r.avg("foo")floating around ingroup_byworked well at the time but IMO it's a lot easier to understand if you think ofavgas a verb and you only see it in the context offoo.avg()which reads as "take the average of foo." ReQL's really built around chaining and the idea thatfoo.barcan be thought of as "apply the actionbarto the nounfoo." Bringing the syntax for groups in line with this seems both very powerful and like it lets us present a much tighter coherent API.I'm failing to see what the
combineproposal is adding. I don't think it really makes queries much shorter (if any shorter). And it gives us an entirely new way of calling every single reducing function that we need to implement (actually a decent amount of work) and document.coffeemug commentedon Dec 3, 2013
Here is what I think it would get us, and what I was trying to convey. Suppose you're doing demographics analysis and want to determine an average salary in a city. Here is how you'd do it in SQL:
And here is what this query would look like under the current proposal:
Now, suppose you're thinking "hmm, I wonder what this will look like if I see the average age too". In SQL, all you have to do is add
AVG(age):The typing distance is very short, but most importantly the conceptual distance is very short too. You don't have to learn any new concepts at all. Now, here's what this query would look like in ReQL under proposed syntax (I'm assuming the
dosyntax, but the same thing applies undersplit):Ignoring the question of labeling the fields and what this actually returns, we're asking users to make quite a large conceptual jump. They have to introduce the
docommand, lambdas, and arrays to get to their data.Given how ridiculously common it is to want to do such a thing, I think we should think about simpler porcelain syntax. That being said, I agree
combineis bad syntax, and that removing the notion of aggregators as nouns is a good idea. I think we should hunt around for a better solution thancombine.coffeemug commentedon Jun 24, 2014
Moving to 1.14-polish. This isn't as important as major features, and I'd like to agree on those first. We can tackle this later.
11 remaining items
coffeemug commentedon Apr 22, 2015
@danielmewes -- I'd like to propose taking this out of the reql discussion for 2.1/polish. There are already a lot of issues to discuss, and I think this one is unlikely to get a good discussion that it deserves. How do you feel about getting back to it after 2.1 is out (or possibly after 2.2)?
danielmewes commentedon Apr 22, 2015
Agreed on second thought, I already did.
dmitry commentedon Aug 26, 2016
As I understood, this feature will allow to aggregate results into a multiple separate groups as it's done in Eslasticsearch?
danielmewes commentedon Aug 26, 2016
@dmitry Not entirely sure what you mean. Do you have a link to documentation of the Elasticsearch feature you're referring to?
dmitry commentedon Aug 27, 2016
@dalanmiller bucket aggregations: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
And specifically I would be glad if the following will work (filter aggregations):
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html - specifically filter a values inside the aggregation bucket, not affecting on the other aggregations and the main filter (something like SQL subselect, that is totally independent on the other queries... but it should be much faster, than subselect).
Use case: faceted search
danielmewes commentedon Aug 29, 2016
@dmitry I'm not quite sure if this is the same as what this issue is proposing, but it looks like it allows for similar functionality.
The
splitsyntax that was proposed originally would allow for filtering for only some of the aggregations.dmitry commentedon Aug 29, 2016
@danielmewes is it possible at the moment to construct a query that allows to generate faceted search? I'm especially interested in disjunctive faceting, like:
danielmewes commentedon Aug 30, 2016
@dmitry Can you explain a bit more what exactly you mean by faceted search?
If the categories are disjunct, can't you just do a
.group("category").count()for this?If they are not disjunct, you could use a
group("categories", {multi: true}).count(), where thecategoriesfield would need to be an array of categories in each document.dmitry commentedon Aug 31, 2016
@danielmewes correct example could be made only in case of the several filters.
Lets say we have "categories" with ID numbers and "prices" with ranges (0-10, 10-20). When I'm selecting "0-10" in prices and 1 in categories, other "prices" and "categories" should also have count numbers, which tells how many "items" will filtered-in after I select those categories or price ranges. The problem here is when I'm selecting category 1, the other "categories" should be also grouped. But
groupaggregates values only afterfiltermakes it work, so thegroupcould not return count of category "2" when category "2" was selected in afilter.Simply: "categories"
groupshould somehow skipfilterby "categories", but "prices"groupshould include filtering by the "categories".Goal of the disjunctive faceted search is to show only possible variants of filters, and by including more selections in one filter (categories or prices) it extends search.