Skip to content

eval doesn't work on join columns #1180

Open

Description

@eantonya
dt1 = data.table(a = 1, key = 'a')
dt2 = data.table(a = 1, b = list(quote(5*a)), key = 'a')

#this works:
dt1[dt2][, eval(b[[1]], .SD)]
#[1] 5

# this doesn't (but should produce exact same result as above)
dt1[dt2, eval(b[[1]], .SD)]
#Error in eval(b[[1]], .SD) : object 'b' not found

Relevant SO post - http://stackoverflow.com/a/30899285/817778

Activity

  1. eantonya commented on Jun 17, 2015

    @eantonya
    ContributorAuthor

    Actually thinking about this a bit - this is probably due to .SD only having LHS data.table columns, which is not that unreasonable, except that it's unclear how else one could do this kind of universal evaluation if using .SD is not an option.

    I can't remember if this was ever discussed - what should .SD be inside a join? My gut reaction is that it should be the same as what it is immediately after the join (basically same conversation we had about .EACHI a while back), i.e. should contain all of the columns and not just LHS.

  2. franknarf1 commented on Jun 17, 2015

    @franknarf1
    Contributor

    If it's agreed that .SD should only be for the LHS, that's fine, but it would be nice (and arguably essential) to also have i.SD, so you could eval(b,c(.SD,i.SD)) or something similar.

    The problem seems to go beyond eval, though:

    dt1[dt2,{y = c(.SD); NULL}] # works
    dt1[dt2,{y = c(b[[1]]); NULL}] # works
    dt1[dt2,{y = c(b[[1]],.SD); NULL}] # Error in eval(expr, envir, enclos) : object 'b' not found
    
  3. arunsrinivasan commented on Jun 17, 2015

    @arunsrinivasan
    Member

    Related to #935

    This'd work:

    dt1[dt2, eval(b[[1]], mget(names(dt2)))]
  4. MichaelChirico commented on Apr 11, 2019

    @MichaelChirico
    Member

    Is this a duplicate of #935?

  5. jangorecki commented on Mar 22, 2020

    @jangorecki
    Member

    @MichaelChirico it is not a duplicate of #935.
    AFAIU this issue occurs due to the fact that when j's root call is eval() call for which we are kicking in an optimization that is injecting quoted expression, which is evaluated in .SD already.

    qc = quote(5*a)
    dt1[dt2, eval(qc)]
    #[1] 5

    Following workaround will work.

    myeval = eval
    dt1[dt2, myeval(b[[1]], .SD)]
    #[1] 5

    issue would go away if we would deprecate special eval() handling in j.

  6. added
    programmingparameterizing queries: get, mget, eval, env
    and removed on Apr 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugprogrammingparameterizing queries: get, mget, eval, env

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions