@@ -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 }
0 commit comments