Skip to content

Weird behaviour when joining #6799

Open

Description

@iagogv3

Reading the new join vignettes I found

- The date related `ProductReceivedProd2` was omitted from this new table.
```{r}
ProductReceivedProd2[ProductSalesProd2,
on = list(product_id, date < date)]
```

which I did not understood initially the full consequences of this, but later, working with my own data, I realised it means losing data from x (if doing x[y, on = .(xid = yid, date < ydate),...]).

I may use something like x[y, on = .(xid = yid), j = .(date = x.date), ...], but then I have to specify inside j = .(...) all the columns from x and y (which may be a lot). I have tried including .SD, x.SD, x..SD, i.SD, i..SD in distinct ways, but nothing of this works.

Is there an abbreviated way to include all x and y (i) columns in the join, while using j to specify some names or some concrete data columns?

Activity

  1. tdhock commented on Feb 10, 2025

    @tdhock
    Member

    related to #4949

  2. tdhock commented on Feb 10, 2025

    @tdhock
    Member

    probably fixed by #3093 (please look at that PR, it needs help)

  3. iagogv3 commented on Feb 13, 2025

    @iagogv3
    ContributorAuthor

    @tdhock Thanks for pointing me to the original issue and to the PR (I looked at it, but it is too much complex for the time I have now)

  4. tdhock commented on Feb 13, 2025

    @tdhock
    Member

    me too

  5. iagogv3 commented on Mar 4, 2025

    @iagogv3
    ContributorAuthor

    BTW, meanwhile, examples such as

    p1[p2, names(p1), on = .(symbol, date >= start_date, date <= end_date), with = FALSE]

    in #4949 (comment) could be added to the Join vignette, commenting this issue. WDYT @venom1204?

  6. venom1204 commented on Mar 4, 2025

    @venom1204
    Contributor

    Thank you for the suggestion! Including examples like

    p1[p2, names(p1), on = .(symbol, date >= start_date, date <= end_date), with = FALSE]
    

    in the Join vignette would indeed be helpful for users dealing with non-equi joins and complex conditions like date ranges. It addresses a common scenario and clarifies how to retain columns from the left table (p1) efficiently.

    However, to make the vignette even more user-friendly, we could:

    • Highlight the importance of using prefixes (x. or i.) to avoid ambiguity in join columns.
    • Provide additional examples that show how to handle situations where both tables have overlapping column names.
    • Include a brief explanation of why data.table behaves this way during joins (right outer join logic and column precedence).

    I believe this would make the documentation more comprehensive and address common pitfalls users face with non-equi joins. I'd be happy to collaborate or review if this is added to the vignette!

  7. added
    joins
    on Mar 26, 2025
  8. venom1204 commented on Jul 7, 2025

    @venom1204
    Contributor

    Hi @tdhock,

    Would a documentation update be helpful here?

    Since, as you mentioned, the root cause seems to be addressed by PR #3093, but that PR is still open, I think it would be useful to improve the documentation in the meantime. Many users find the current non-equi join behavior surprising—especially that x[i, on=..., j=...] drops x columns unless explicitly referenced.

    My plan:

    • Add a note to the non-equi join section of datatable-joins.Rmd explaining the current behavior.

    • Include an example showing the unexpected result and an idiomatic workaround using x., .SD, or a programmatic approach.

    This should help reduce confusion and improve usability until #3093 is merged. Let me know if this direction sounds good!

  9. tdhock commented on Jul 8, 2025

    @tdhock
    Member

    I think it would be better if you could put some work into fixing #3093

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

    joins

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions