Skip to content

Commit 6b8f11f

Browse files
committed
add 2nd case
1 parent cfa5d71 commit 6b8f11f

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

R/bmerge.R

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,29 +103,46 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
103103
} else stopf("Incompatible join types: %s is type integer64 but %s is type double and contains fractions", nm[2L], nm[1L])
104104
} else {
105105
# just integer and double left
106+
ic_idx = which(ic == icols)
106107
if (i_merge_type=="double") {
108+
coerce_x = FALSE
107109
if (!isReallyReal(i[[ic]])) {
108110
# common case of ad hoc user-typed integers missing L postfix joining to correct integer keys
109111
# we've always coerced to int and returned int, for convenience.
110-
if (verbose) catf("Coercing double column %s (which contains no fractions) to type integer to match type of %s.\n", iname, xname)
111-
val = as.integer(i[[ic]])
112-
if (!is.null(attributes(i[[ic]]))) attributes(val) = attributes(i[[ic]]) # to retain Date for example; 3679
113-
set(i, j=ic, value=val)
114-
set(callersi, j=ic, value=val) # change the shallow copy of i up in [.data.table to reflect in the result, too.
115-
} else {
112+
for (b in which(x_merge_types[ic_idx] == "double")) {
113+
xb = xcols[b]
114+
if (isReallyReal(x[[xb]])) {
115+
coerce_x = TRUE
116+
break
117+
}
118+
}
119+
if (!coerce_x) {
120+
if (verbose) catf("Coercing double column %s (which contains no fractions) to type integer to match type of %s.\n", iname, xname)
121+
val = as.integer(i[[ic]])
122+
if (!is.null(attributes(i[[ic]]))) attributes(val) = attributes(i[[ic]]) # to retain Date for example; 3679
123+
set(i, j=ic, value=val)
124+
set(callersi, j=ic, value=val) # change the shallow copy of i up in [.data.table to reflect in the result, too.
125+
for (b in which(x_merge_types[ic_idx] == "double")) {
126+
xb = xcols[b]
127+
val = as.integer(x[[xb]])
128+
if (!is.null(attributes(x[[xb]]))) attributes(val) = attributes(x[[xb]])
129+
if (verbose) catf("Coercing double column %s (which contains no fractions) to type integer to match type of %s.\n", xnames[b], xname)
130+
set(x, j=xb, value=val)
131+
}
132+
}
133+
}
134+
if (coerce_x) {
135+
ic_idx = which()
116136
if (verbose) catf("Coercing integer column %s to type double to match type of %s which contains fractions.\n", xname, iname)
117137
set(x, j=xc, value=as.double(x[[xc]]))
118138
}
119139
} else {
120140
if (verbose) catf("Coercing integer column %s to type double for join to match type of %s.\n", iname, xname)
121141
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-
}
142+
for (b in which(x_merge_types[ic_idx] == "integer")) {
143+
xb = xcols[b]
144+
if (verbose) catf("Coercing integer column %s to type double for join to match type of %s.\n", xnames[b], xname)
145+
set(x, j=xb, value=as.double(x[[xb]]))
129146
}
130147
}
131148
}

inst/tests/tests.Rraw

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20601,6 +20601,9 @@ 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-
2060520604
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")
2060620605
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")
20606+
x = data.table(a=1)
20607+
y = data.table(c=1, d=2L)
20608+
test(2297.3, options=c(datatable.verbose=TRUE), y[x, on=.(c == a, d == a)], data.table(c=1L, d=1L), output="Coercing double column x.c (which contains no fractions) to type integer")
20609+
test(2297.4, options=c(datatable.verbose=TRUE), y[x, on=.(d == a, c == a)], data.table(c=1L, d=1L), output="Coercing double column x.c (which contains no fractions) to type integer")

0 commit comments

Comments
 (0)