Unexpected result from non-equi-join

Open
#4,949 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Reproduce the two non-equi-join examples from the issue with the supplied p1 and p2 data, then compare their subset results with the in-place modification result. Trace the non-equi-join subset behavior and determine whether the observed rows are intended; done means the behavior is clarified or corrected with coverage for this example.

Written by the indexing model from the issue text.

Description

non-equi joins

Suppose we have the following data.tables:

library(data.table)

p1 <- data.table(
  symbol = c("A", "A", "A", "B", "B", "B"),
  date = c(1L, 2L, 3L, 1L, 2L, 3L)
)

p2 <- data.table(
  symbol = c("A", "A", "B"),
  start_date = c(1L, 2L, 1L),
  end_date = c(1L, 3L, 2L),
  id = 1:3
)
> p1                                                                                                                                  
   symbol  date
   <char> <int>
1:      A     1
2:      A     2
3:      A     3
4:      B     1
5:      B     2
6:      B     3

> p2                                                                                                                                  
   symbol start_date end_date    id
   <char>      <int>    <int> <int>
1:      A          1        1     1
2:      A          2        3     2
3:      B          1        2     3

In-place modification with non-equi-join works as expected:

p1[p2, id := id, on = .(symbol, date >= start_date, date <= end_date)]
> p1                                                                                                                                  
   symbol  date    id
   <char> <int> <int>
1:      A     1     1
2:      A     2     2
3:      A     3     2
4:      B     1     3
5:      B     2     3
6:      B     3    NA

However, using the non-equi-join syntax to subset p1 with p2 does not seem to work consistently:

p1[p2, .(symbol, date), on = .(symbol, date >= start_date, date <= end_date)]
   symbol  date
   <char> <int>
1:      A     1
2:      A     2
3:      A     2
4:      B     1
5:      B     1

or

p1[p2, on = .(symbol, date >= start_date, date <= end_date)]
   symbol  date    id date.1  i.id
   <char> <int> <int>  <int> <int>
1:      A     1     1      1     1
2:      A     2     2      3     2
3:      A     2     2      3     2
4:      B     1     3      2     3
5:      B     1     3      2     3

But I expect the result to be like

p2 <- p1[!is.na(id)]
p2
   symbol  date    id
   <char> <int> <int>
1:      A     1     1
2:      A     2     2
3:      A     3     2
4:      B     1     3
5:      B     2     3

I wonder if this is a known behavior? Or is there an explanation why it is not the expected result like above? Am I missing something about the definition of the non-equi-join?

Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.