Skip to content

Roll the nested pluck syntax into other commands #1094

Closed

Description

@coffeemug

We should make the nested pluck syntax available in other commands. A few that come to mind:

  • without
  • orderBy
  • groupBy (or group, if that's what we end up doing)
  • withFields
  • hasFields
  • getField

Please add to this list if there is anything I missed.

Activity

  1. jdoliner commented on Jun 29, 2013

    @jdoliner
    Contributor

    This is that #1091 was for.

  2. coffeemug commented on Jun 29, 2013

    @coffeemug
    ContributorAuthor

    Whoops, sorry. Do you mind if I close #1091 instead of closing this one? (I linked a bunch of things to it, and don't want to go through all those links)

  3. ghost assigned on Jul 2, 2013
  4. mlucy commented on Jul 2, 2013

    @mlucy
    Member

    I'm totally cool with:

    • without
    • orderby
    • with_fields
    • has_fields
    • group_by

    But:

    • get_field -- what does this do if someone writes r({:a => 1, :b => 2}).get_field([:a, :b])?
  5. jdoliner commented on Jul 2, 2013

    @jdoliner
    Contributor
    r({:a => 1, :b => 2}).get_field([:a, :b])
    >>>[1,2]
    
  6. mlucy commented on Jul 2, 2013

    @mlucy
    Member

    That seems bad to me. I would rather just not make get_field take that syntax.

  7. jdoliner commented on Jul 2, 2013

    @jdoliner
    Contributor

    So one thing that we should consider is what happens with errors like #1095 in the other operations. Here's what I think we should do:

    without:

    r.expr({"foo" : 1}).without({"foo" : "bar"})
    >>> {"foo" : 1}
    

    This seems the most analogous to what pluck does and is probably what people want since saying without({"foo" : "bar"}) will leave the key "foo" in the map in the non error case.

    has_fields:
    This one seems easier since we're returning just returning whether or not we match. For example:

    r.expr({"foo" : 1}).has_fields({"foo" : "bar"})
    >>> false
    

    The rest of these I'm pretty sure have an obvious command that they behave the same as in this regard. orderby and group_by are like pluck and with_fields is a combination of has_fields and pluck.

  8. jdoliner commented on Jul 2, 2013

    @jdoliner
    Contributor

    So another question here is how you can use negatives in orderby with pattern matching syntax. Is there a way to say something like:

    table.order_by({"foo" : r.desc("bar")})
    
  9. coffeemug commented on Jul 2, 2013

    @coffeemug
    ContributorAuthor

    What's wrong with the example you picked?

  10. jdoliner commented on Jul 3, 2013

    @jdoliner
    Contributor

    So having thought about this a bit I think we should just drop the nested syntax from order_by the main reason is that it can potentially be very confusing since the order of the arguments matters in order_by but doesn't in other functions where we're applying this such as pluck. Specifically:

    table.order_by("foo", "bar")
    # might be expected to be equivalent to
    table.order_by(["foo", "bar"])
    # which we could conceivably make be equivalent but then what of
    table.order_by({"foo" : True, "bar" : True})
    # which is by definition equivalent to
    table.order_by({"bar" : True, "foo" : True})
    

    The pattern matching syntax also doesn't play great with r.desc and r.asc.

    I think a better thing to do would be to just have order_by accept a function in which one can use pluck.

  11. coffeemug commented on Jul 3, 2013

    @coffeemug
    ContributorAuthor

    👍

    To clarify, this is what we talked about in person:

    table.orderBy(r.row('i')('j'), r.desc(r.row('k')('l')('m')))

    This syntax is clear, unambiguous, and more than sufficient to get the job done.

  12. jdoliner commented on Jul 3, 2013

    @jdoliner
    Contributor

    We should also have a separate issue for this would you mind opening it @coffeemug?

  13. coffeemug commented on Jul 3, 2013

    @coffeemug
    ContributorAuthor

    NP, see #1120.

  14. jdoliner commented on Jul 8, 2013

    @jdoliner
    Contributor

    This is mostly implemented it just needs a bit of pair programming with @mlucy to complete.

  15. mlucy commented on Jul 9, 2013

    @mlucy
    Member

    This seems basically settled.

  16. 13 remaining items

  17. AtnNn commented on Aug 19, 2013

    @AtnNn
    Member

    @chrisabrams you can create an index on the nested attribute and get on that index, or use filter.

  18. chrisabrams commented on Aug 19, 2013

    @chrisabrams

    Let's say I have an object in RethinkDB in the database 'mydb' and the table 'mytable' (I'm working in Node.js):

     {
         meta: {
             md5: 'something'
         }
     }
    

    I want to find a specific object called "something"; I tried filter like so:

    var filter = {
        meta: {
            md5: 'something'
        }
    };
    
    r.db('mydb').table('mytable').filter(filter)
    

    Am I doing filter wrong? I think I should make the md5 an index, but I'm curious about my use of filter and whether it is correct.

    Filter returns with no results, even if there are multiple entries with the same value.

  19. jdoliner commented on Aug 19, 2013

    @jdoliner
    Contributor

    filter doesn't yet support nest syntax like that.

  20. chrisabrams commented on Aug 19, 2013

    @chrisabrams

    OK that is what I figured...what about creating an index on a nested attribute?

  21. jdoliner commented on Aug 19, 2013

    @jdoliner
    Contributor

    You can't do: table.index_create({"foo" : "bar"}) because we wouldn't know what to name the index. You can do:
    table.index_create("name", lambda x: x.pluck({"foo" : "bar"}))

  22. chrisabrams commented on Aug 19, 2013

    @chrisabrams

    Makes sense. Alright I'll try that!

  23. chrisabrams commented on Aug 19, 2013

    @chrisabrams

    Is there an example of how to do lambda in Node?

  24. jdoliner commented on Aug 19, 2013

    @jdoliner
    Contributor

    I think in Node say: function (row) { return row.pluck({"foo" : "bar"}); }

  25. neumino commented on Aug 19, 2013

    @neumino
    Member

    @chrisabrams, "lambda function" are what we call "anonymous function" in JavaScript.

  26. chrisabrams commented on Aug 19, 2013

    @chrisabrams

    @neumino thanks :)

    So...I did this:

    r.db('mydb').table('mytable').indexCreate("md5", function(row) {
      return row.pluck({
        "meta": "md5"
      });
    });
    

    But in the admin UI it says zero secondary indexes. When I run the following code (referencing the same code from above)

    r.db('mydb').table('mytable').getAll('something', {
      index: 'md5'
    }).run(connection, function(err, cursor) { // logic here });
    

    I get the error:

    Index `md5` was not found.
    

    Did I miss something?

  27. jdoliner commented on Aug 19, 2013

    @jdoliner
    Contributor

    Looks like your forgot to add .run() to the end of the first query.

  28. chrisabrams commented on Aug 19, 2013

    @chrisabrams

    Heh I did forget something.

  29. chrisabrams commented on Aug 19, 2013

    @chrisabrams

    K that created my index. Now to test a search :)

  30. chrisabrams commented on Aug 19, 2013

    @chrisabrams

    I don't think my index works; it returns nothing. Is there a way to get all contents by index (so I can debug and see if I actually wrote my index correctly)?

  31. jdoliner commented on Aug 19, 2013

    @jdoliner
    Contributor

    How are you trying to get things from it? Also maybe you'd like to stop by #rethinkdb on freenode. We can offer help a lot easier there and github issues aren't really the right forum for discussions like this.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions