Skip to content

Commit 7ee12aa

Browse files
committed
add fix in one direction
1 parent 1bb60bf commit 7ee12aa

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

R/bmerge.R

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,22 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
3434
ans
3535
}
3636

37-
if (nrow(i)) {
37+
if (nrow(i)) {
38+
x_merge_types = vapply_1c(x[0L, ..xcols], getClass)
39+
i_merge_types = vapply_1c(x[0L, ..icols], getClass)
40+
xnames = paste0("x.", names(x)[xcols])
41+
inames = paste0("i.", names(i)[icols])
3842
for (a in seq_along(icols)) {
3943
# - check that join columns have compatible types
4044
# - do type coercions if necessary on just the shallow local copies for the purpose of join
4145
# - handle factor columns appropriately
4246
# Note that if i is keyed, if this coerces i's key gets dropped by set()
4347
ic = icols[a]
4448
xc = xcols[a]
45-
x_merge_type = getClass(x[[xc]])
46-
i_merge_type = getClass(i[[ic]])
47-
xname = paste0("x.", names(x)[xc])
48-
iname = paste0("i.", names(i)[ic])
49+
x_merge_type = x_merge_types[a]
50+
i_merge_type = i_merge_types[a]
51+
xname = xnames[a]
52+
iname = inames[a]
4953
if (!x_merge_type %chin% supported) stopf("%s is type %s which is not supported by data.table join", xname, x_merge_type)
5054
if (!i_merge_type %chin% supported) stopf("%s is type %s which is not supported by data.table join", iname, i_merge_type)
5155
if (x_merge_type=="factor" || i_merge_type=="factor") {
@@ -115,6 +119,14 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
115119
} else {
116120
if (verbose) catf("Coercing integer column %s to type double for join to match type of %s.\n", iname, xname)
117121
set(i, j=ic, value=as.double(i[[ic]]))
122+
ic_idx = which(ic == icols)
123+
if (length(ic_idx)>1) {
124+
for (b in which(x_merge_types[ic_idx] != "double")) {
125+
xb = xcols[b]
126+
if (verbose) catf("Coercing integer column %s to type double for join to match type of %s.\n", xnames[b], xname)
127+
set(x, j=xb, value=as.double(x[[xb]]))
128+
}
129+
}
118130
}
119131
}
120132
}

inst/tests/tests.Rraw

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20601,6 +20601,6 @@ test(2296, d2[x %no such operator% 1], error = '%no such operator%')
2060120601
# coerce Dates to double if join on multiple columns, #6602
2060220602
x = data.table(a=1L)
2060320603
y = data.table(c=1L, d=2)
20604-
y[x, on=.(c == a, d == a)]
2060520604

20606-
test(2297.1, options=c(datatable.verbose=TRUE), y[x, on=.(c == a, d == a)], data.table(c=date_int, d=date_int, b=1L), output="coercing to double.")
20605+
test(2297.1, options=c(datatable.verbose=TRUE), y[x, on=.(c == a, d == a)], data.table(c=1L, d=1L), output="Coercing .*c to type double")
20606+
test(2297.2, options=c(datatable.verbose=TRUE), y[x, on=.(d == a, c == a)], data.table(c=1L, d=1L), output="Coercing .*c to type double")

0 commit comments

Comments
 (0)