Skip to content

ReQL proposal: implicitly coerce streams to arrays #2721

Open

Description

@coffeemug

I've spoken with many users over time who are frustrated with having to call coerceTo('array') explicitly. Now that we enforce an array limit size (and are about to make it configurable), I think we should drop this behavior and perform the conversion implicitly. It should make lots of queries much more pleasant to write.

Activity

  1. added this to the subsequent milestone on Jul 18, 2014
  2. mlucy commented on Jul 20, 2014

    @mlucy
    Member

    How would to feel about a compromise proposal where we make it easier to explicitly convert, and make the error message clearer? .coerce_to('array') is a lot more painful than .to_array, and it often isn't obvious that that's the solution.

    I'm extremely hesitant to do this automatically. The difference between arrays and streams is already confusing for people, and unlike most confusing things you can sweep under the rug, it's absolutely essential for getting any sort of performance out of RethinkDB (or, indeed, for writing queries that won't blow up in your face as soon as you hit production).

    I'm sympathetic to the view that ease-of-use is more important than forcing people to think about performance, but this really is a special case. Implicitly converting streams to arrays would be like implicitly converting files to strings in Python. They're different because they work on different scales.

    Another option would be to consider loosening some of our restrictions about storing streams in arrays and objects, but we'd have to be very careful about that.

  3. coffeemug commented on Jul 22, 2014

    @coffeemug
    ContributorAuthor

    I'm extremely hesitant to do this automatically.

    I am too. I usually have a very negative reaction to hiding distinct concepts from users that might have large performance or semantics implications on their programs, but thinking about it carefully, I think in this particular case it's justified. Here is why.

    At this point I've seen dozens of examples of user code where they're forced to coerce to an array, and in every single case the user was doing something sensible (meaning, they were storing a resultset that was guaranteed to be small in an array). I haven't seen a single example where the user is doing something unreasonable (like coercing a potentially large table to an array).

    Empirically, the explicit conversion ends up penalizing the overwhelmingly common case to make a very small minority of the cases safer. But if you consider the worst case scenario, it actually isn't that bad. The user can't do anything catastrophic -- in the limit case they're just going to trip up the array limit. One might argue that it's bad for obvious reasons (and we had that argument many times before), but I think in practice the benefits of allowing seamless array features coupled with an array limit dramatically outweigh the downsides (if they don't, we should be reconsidering the whole thing altogether).

    I think the explicit conversion made sense before we introduces the array limit because converting implicitly would have been too dangerous. But now it's just a nuisance. I don't think the explicit conversion produces the kind of reaction we originally expected in people's minds. Instead of thinking "arrays are different from streams, I should restructure my code" the user typically thinks "arghhh, my resultset is always going to be small, why do I have deal with this?" Then they just tack on a coercion and move on, without thinking much about it further.

    So I think the benefit of pushing this behavior further down (when they hit the 100k limit) far outweighs the disadvantages. IMO making people convert explicitly introduces a pretty serious nuisance and doesn't actually result in what we want (people don't think about this carefully, they just get frustrated, fix it, and move on).

  4. mlucy commented on Jul 22, 2014

    @mlucy
    Member

    At this point I've seen dozens of examples of user code where they're forced to coerce to an array, and in every single case the user was doing something sensible (meaning, they were storing a resultset that was guaranteed to be small in an array). I haven't seen a single example where the user is doing something unreasonable (like coercing a potentially large table to an array).

    That argument seems wrong to me. Your observation is that when you see code where people turn streams into arrays, they're usually doing so for a good reason. One interpretation of that is "99% of the time streams can be safely turned into arrays", but another interpretation is "the current system is successfully preventing people from turning streams into arrays unless they have a good reason".

    The availability heuristic doesn't work very well for determining whether or not you're successfully preventing something bad from happening.

  5. coffeemug commented on Jul 22, 2014

    @coffeemug
    ContributorAuthor

    One interpretation of that is "99% of the time streams can be safely turned into arrays", but another interpretation is "the current system is successfully preventing people from turning streams into arrays unless they have a good reason".

    I know what you're saying, but I have a strong intuition that it isn't the case (I can't prove it, though).

    @AtnNn @danielmewes @neumino -- do you have an opinion on this since you spend a lot of time helping users?

  6. AtnNn commented on Jul 22, 2014

    @AtnNn
    Member

    This usually comes up when users are confused by the expected DATUM error. Most of these users will happily use .coerceTo('array').

    Some users complain about having to use coerceTo because that is always what they mean. They don't seem to run into cases where the code was wrong and using coerceTo is the wrong solution. They don't want to have to think about whether it is a stream or an array.

    A surprising number of users take this to the extreme and add .coerceTo('array') everywhere. I have on a few occasions seen code such as r.table(...).coerceTo('array').filter(...).coerceTo('array').map(..).coerceTo('array').

    I think that streams would be silently coerced into arrays when necessary. Also, streams and arrays should be a hidden implementation detail of sequences, and that only the term sequence should be exposed to users in the documentation and in error messages.

  7. coffeemug commented on Jul 22, 2014

    @coffeemug
    ContributorAuthor

    One interpretation of that is "99% of the time streams can be safely turned into arrays", but another interpretation is "the current system is successfully preventing people from turning streams into arrays unless they have a good reason".

    I know what you're saying, but I have a strong intuition that it isn't the case (I can't prove it, though).

    BTW, I fully appreciate the irony of invoking intuition in response to a mention of availability heuristic (in case anyone is wondering 😀).

  8. neumino commented on Jul 22, 2014

    @neumino
    Member

    Some people do put a coerceTo("ARRAY") at the end of a query because it's annoying to deal with a cursor (especially in JavaScript), see for example

    I think we should automatically have the driver fetch all the data from a cursor and if people really want a cursor for some reason, they can pass an optarg to run. I basically think we should do neumino/rethinkdbdash#31

    The case that is really annoying (where it adds frictions for no real benefits) is when people do NoSQL-ish join (using merge and getAll). The have to put a coerceTo("ARRAY") at the end of every sub queries.
    This friction is really useful since if they somehow have more than 100.00 elements in a sub array, they will just spend some time figuring out what is this OPAQUE SELECTION error (and if you look at the results provided by Google, it's not easy to understand what's the problem). And then they will just hit the limit on arrays and have to rewrite the whole thing with a SQL-ish query.

    I also think that it's a pain for users to require them to understand what is the difference between a stream and an array. It's reflected by the number of questions we get about cursors.
    Users should have to understand how RethinkDB behaves under the hood only in advanced cases, and not as soon as they try to do a join.

  9. danielmewes commented on Jul 22, 2014

    @danielmewes
    Member

    I'm in favor of automatically coercing results to an array in the driver, unless the user provides a special opt arg that they want to get the cursor.

    The reason is this:

    • In almost all cases, users want the full result set anyway. Returning an array just makes that easier and avoids confusion over which queries return arrays and which return cursors.
    • In those cases, there is no gain in exposing the cursor. The user will load all results into memory on the client in either case. Huge result sets blow no matter whether we provide the cursor or array interface by default.
    • If a user expects a huge result and want to be able to handle it, they will typically write special code in their application anyway. I can only think of two cases where this works: a) Results can be streamed to some other place (e.g. a browser), and don't have to be kept in memory before being sent out. b) the application performs some client-side reduction on the result, like counting or summation.
      In case a) users will typically be aware of the fact that they need a cursor to accomplish this. Having to specify a special flag to get the cursor seems very reasonable. In case b) it might be less obvious, because both getting an array or a cursor works on a semantic level. However I still think that requiring memory for the whole query result on the client (by default) is expected even then.

    So in summary: Returning a cursor doesn't provide any advantage in the typical case. If a user wants to take advantage of a cursor they will usually have to take special measures anyway (like streaming results to a consumer). Having to add a flag to run in such cases doesn't seem unexpected.

  10. coffeemug commented on Jul 22, 2014

    @coffeemug
    ContributorAuthor

    @danielmewes -- we're talking about something different here, though. The question is -- if you write a query that, say, does a join that returns a stream, and tries to write that stream to a field of a document, do we coerce implicitly or force people to coerce explicitly?

    Coercion of cursors to arrays is a related, but different question.

  11. danielmewes commented on Jul 22, 2014

    @danielmewes
    Member

    Ah I thought this was about both.

    Intuitively I'd say that implicitly coercing streams to an array for terms that only accept arrays is what I'd expect, but I'd have to think about it more to form a justified opinion.

  12. neumino commented on Jul 22, 2014

    @neumino
    Member

    There is one last case that I forgot yesterday.

    Sometimes you want to pass a stream to do. The do command currently works only with datum, so you have to put a coerceTo("ARRAY").
    Not so many people run into this issue though (at least as far as I can remember).

  13. mlucy commented on Jul 22, 2014

    @mlucy
    Member

    Let's talk about the cursor thing separately. I'm hesitant to automatically convert arrays (although I'm coming around a bit), but I'm outright opposed to getting rid of cursors by default. Even if you ignore the practical benefits, they drastically improve the perceived latency of queries in blocking languages, and as far as I can tell perceived latency is 99% of what people are talking about when they say something feels slow. (This is also why we make the first batch smaller in the current code.)

  14. mlucy commented on Jul 22, 2014

    @mlucy
    Member

    (Also, some things (like .changes()) have to use cursors, so it's better to get people used to them. Also also, writing r.table('test').run shouldn't just block forever, and it's a pretty bad experience to have it block for a long time and then blow up.)

  15. 28 remaining items

  16. coffeemug commented on Nov 4, 2014

    @coffeemug
    ContributorAuthor

    One way to get a single element out of RethinkDB right now is to append .nth(0).default({}) to your query (you can drop .default if you know you'll be getting at least one element, or if you're ok with getting an error if there isn't anything returned by the query). We should make this simpler -- I've opened #3283 with a proposal.

  17. internalfx commented on Nov 7, 2014

    @internalfx

    I've been developing against RTDB almost everyday for the past 7 months. So I'll throw in what I've noticed...

    Having to coerce to arrays IS a nuisance, its an easily overcome one though. Just one more step that is often forgotten.

    @danielmewes is right. Wanting a cursor IS the edge case, I have plenty of toArray calls in my Nodejs server code. The only time I specifically wanted a cursor was when I was working on a data import/conversion script. It was specifically designed to iterate to keep memory usage low.

  18. modified the milestones: , 1.16 on Nov 25, 2014
  19. modified the milestones: subsequent, on Dec 24, 2014
  20. neumino commented on Feb 10, 2015

    @neumino
    Member

    Note that this would help for neumino/thinky#179 as we currently cannot pass a stream to do.

    Context about the issue linked:

    Posts.getJoin({ comments: function(seq) { return seq.count() } })
    

    Becomes

    r.table('posts').merge(function(post) {
        return {
            comments: r.table('comments').getAll(post('id'), {index: 'postId'}).count().coerceTo('ARRAY')
    });
    

    If streams are automatically coerced, I could just remove the coerceTo('ARRAY') and let RethinkDB decide if it needs to cast the stream to an array or not.

  21. knownasilya commented on Feb 13, 2015

    @knownasilya

    What about making implicit conversion a configurable option? Where you set it as you make your DB connection..

  22. danielmewes commented on Feb 13, 2015

    @danielmewes
    Member

    @knownasilya we had decided to have the implicit coercion always enabled, though this issue is blocking on better array size limits (#1375).

    I wouldn't be opposed to an optarg to disable this in general, but I'm not sure which use case(s) that would have.

  23. geddski commented on Jul 20, 2015

    @geddski

    +1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions