Skip to content

Propsal: r.rename #881

Open
Open

Description

@mlucy

You should be able to rename attributes like:

> r({:a => 1, :b => 2}).rename(:a => :foo, :b => :bar).run
{'foo' => 1, 'bar' => 2}

Activity

  1. thelinuxlich commented on Aug 18, 2014

    @thelinuxlich

    +1

  2. modified the milestones: backlog, subsequent on Aug 20, 2014
  3. darky commented on Oct 26, 2015

    @darky

    👍

  4. OtterCode commented on Dec 2, 2015

    @OtterCode

    It's such a small feature, but oh-my-goodness, a quick, concise way to change field names throughout a table would be a miracle.

  5. modified the milestones: , subsequent on Dec 3, 2015
  6. danielmewes commented on Dec 3, 2015

    @danielmewes
    Member

    This should also work for nested fields.

    Proposal:

    > r({:a => {:b => 1}, :c => 2}).rename({:a => {:b => :bar}}).run
    {:a => {:bar => 1}}
    > r({:a => {:b => 1}, :c => 2}).rename({:a => {:b => :bar}, :a => :foo}).run
    {:foo => {:bar => 1}}

    Note that in this proposal, you can not move a field to a different level. E.g. you couldn't move a.b to the top level. I think that's probably fine.

    It is also slightly ambiguous in the second example > r({:a => {:b => 1}, :c => 2}).rename({:a => {:b => :bar}, :a => :foo}).run
    whether we refer to the field a by its old name a, or by its new name foo in the rename rule for a.b/foo.b. I suggest we always assume the old names (like I did here), so we don't need to imply any arbitrary ordering in which the renames will be applied. Basically all renames would be applied atomically in one step.

    I also suggest that we error on circular/conflicting renames (e.g. {:a => :b, :b => :a} or {:a => :b, :b => :c}).

    If a target field name already exists, should we just cobble it? Or error? Or have an optarg conflict: "error"|"cobble"|"merge"? Maybe with names consistent with insert instead: "error"|"replace"|"update"?

  7. encryptio commented on Dec 3, 2015

    @encryptio
    Contributor

    Most languages (including our datum_ts) won't let you define {:a => one thing, :a => something else} and get two pairs out of it. Some clobber, some error.

    You could do r.rename({ :a => { :b => :other } }).rename({:a => :top}) as a (not so pretty) workaround.

  8. danielmewes commented on Dec 3, 2015

    @danielmewes
    Member

    Ouch yeah. I didn't think that through. Thanks for pointing that out @encryptio .

    We could make rename variadic and allow multiple dictionaries to be passed in. Alternatively we could not do that, and require users to chain multiple renames together if they want to rename field names on multiple levels. That would also avoid the ordering ambiguity.

  9. OtterCode commented on Dec 3, 2015

    @OtterCode

    I can only comment on the JS syntax, but I would make it as close to the Update syntax as possible, since that's basically what you're doing. Something like:

    { status: "good" } // Before
    
    r.table( "posts" )
      .rename( { status: "goodness" } )
      .run()
    
    { goodness: "good" } // After
    

    Since this isn't a function that one should call casually, it should roll-back and spit an error by default on a conflict along the lines of:

    {
        message: "There were a few records that had conflicting field names."
        ids: [ "1stConflictID", "2ndConflictID" ... ] 
    }
    

    However, it would probably pay to make that behavior configurable. Perhaps include an options object that includes configuration for overwriting, skipping, and so on.

  10. mlucy commented on Dec 9, 2015

    @mlucy
    MemberAuthor

    I think the current proposal where you can rename nested fields is good.

    I think a conflict optarg is good, and that the default should be error.

    I think having update be a legal option is confusing, because I associate update with mutation more than merging. I also think that being consistent with insert is desirable though. How would people feel about changing insert so it read insert(conflict: 'merge') instead of insert(conflict: 'update')? (I think replace and error are fine names.)

    I think .rename should be non-destructive, so you would write r.table('posts').update{|row| row.rename(status: 'goodness')} rather than r.table('posts').rename(status: 'goodness') as in the above post.

  11. danielmewes commented on Dec 10, 2015

    @danielmewes
    Member

    insert(conflict: 'merge') reads very nicely to me. I like that.
    I think we should change that and keep supporting the old insert(conflict: 'update') as an alias for backwards compatibility.

    I agree that rename should just be a transformation, not a write operation.

  12. 8 remaining items

  13. mlucy commented on Dec 18, 2015

    @mlucy
    MemberAuthor

    Agreed, let's settle this with the simpler API, and once that's in we can open a new ReQL discussions for the extensions @marshall007 proposed.

  14. marshall007 commented on Dec 18, 2015

    @marshall007
    Contributor

    @danielmewes @mlucy yep, sounds good!

  15. marshall007 commented on Dec 18, 2015

    @marshall007
    Contributor

    @danielmewes: to elaborate on the array thing, while I agree it's probably not a typical use-case, trying to accomplish the same thing with merge is fairly cumbersome. I most often find myself wanting to do this when I'm dealing with grouped streams and need to pivot the reduction values into keys.

    r.expr({ group: 500, reduction: [ 'a', 'b', 'c' ] })
    .merge(row => row('reduction').map(r => [ r, row('group') ]).coerceTo('object'))
    .omit('group')

    Given that it doesn't result in any compromises to the settled proposal, it seems like a fairly good fit here. It also gives rename similar semantics to pluck style selectors, which I think is why (to me) it feels like something that should work.

  16. modified the milestones: , subsequent on Apr 7, 2016
  17. srh commented on May 17, 2017

    @srh
    Contributor

    r.alias is a proposed alternate name in #6331.

  18. Extarys commented on Jun 24, 2017

    @Extarys

    I would like to rename a field while keeping it's value. +1 :D

  19. PassTheMayo commented on Apr 5, 2018

    @PassTheMayo

    +1

  20. phablulo commented on Jun 7, 2018

    @phablulo

    +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