Skip to content

Commit

Permalink
add fix in one direction
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-schwen committed Nov 24, 2024
1 parent 1bb60bf commit 7ee12aa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
22 changes: 17 additions & 5 deletions R/bmerge.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,22 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
ans
}

if (nrow(i)) {
if (nrow(i)) {
x_merge_types = vapply_1c(x[0L, ..xcols], getClass)
i_merge_types = vapply_1c(x[0L, ..icols], getClass)
xnames = paste0("x.", names(x)[xcols])
inames = paste0("i.", names(i)[icols])
for (a in seq_along(icols)) {
# - check that join columns have compatible types
# - do type coercions if necessary on just the shallow local copies for the purpose of join
# - handle factor columns appropriately
# Note that if i is keyed, if this coerces i's key gets dropped by set()
ic = icols[a]
xc = xcols[a]
x_merge_type = getClass(x[[xc]])
i_merge_type = getClass(i[[ic]])
xname = paste0("x.", names(x)[xc])
iname = paste0("i.", names(i)[ic])
x_merge_type = x_merge_types[a]
i_merge_type = i_merge_types[a]
xname = xnames[a]
iname = inames[a]
if (!x_merge_type %chin% supported) stopf("%s is type %s which is not supported by data.table join", xname, x_merge_type)
if (!i_merge_type %chin% supported) stopf("%s is type %s which is not supported by data.table join", iname, i_merge_type)
if (x_merge_type=="factor" || i_merge_type=="factor") {
Expand Down Expand Up @@ -115,6 +119,14 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
} else {
if (verbose) catf("Coercing integer column %s to type double for join to match type of %s.\n", iname, xname)
set(i, j=ic, value=as.double(i[[ic]]))
ic_idx = which(ic == icols)
if (length(ic_idx)>1) {
for (b in which(x_merge_types[ic_idx] != "double")) {
xb = xcols[b]
if (verbose) catf("Coercing integer column %s to type double for join to match type of %s.\n", xnames[b], xname)
set(x, j=xb, value=as.double(x[[xb]]))
}
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions inst/tests/tests.Rraw
Original file line number Diff line number Diff line change
Expand Up @@ -20601,6 +20601,6 @@ test(2296, d2[x %no such operator% 1], error = '%no such operator%')
# coerce Dates to double if join on multiple columns, #6602
x = data.table(a=1L)
y = data.table(c=1L, d=2)
y[x, on=.(c == a, d == a)]

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.")
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")
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 comments on commit 7ee12aa

Please sign in to comment.