@@ -34,22 +34,17 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
3434 ans
3535 }
3636
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 ])
42- for (a in seq_along(icols )) {
37+ if (nrow(i )) for (a in seq_along(icols )) {
4338 # - check that join columns have compatible types
4439 # - do type coercions if necessary on just the shallow local copies for the purpose of join
4540 # - handle factor columns appropriately
4641 # Note that if i is keyed, if this coerces i's key gets dropped by set()
4742 ic = icols [a ]
4843 xc = xcols [a ]
49- x_merge_type = x_merge_types [ a ]
50- i_merge_type = i_merge_types [ a ]
51- xname = xnames [ a ]
52- iname = inames [ a ]
44+ x_merge_type = getClass( x [[ xc ]])
45+ i_merge_type = getClass( i [[ ic ]])
46+ xname = paste0( " x. " , names( x )[ xc ])
47+ iname = paste0( " i. " , names( i )[ ic ])
5348 if (! x_merge_type %chin % supported ) stopf(" %s is type %s which is not supported by data.table join" , xname , x_merge_type )
5449 if (! i_merge_type %chin % supported ) stopf(" %s is type %s which is not supported by data.table join" , iname , i_merge_type )
5550 if (x_merge_type == " factor" || i_merge_type == " factor" ) {
@@ -109,11 +104,14 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
109104 if (! isReallyReal(i [[ic ]])) {
110105 # common case of ad hoc user-typed integers missing L postfix joining to correct integer keys
111106 # we've always coerced to int and returned int, for convenience.
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
107+ if (length(ic_idx )> 1L ) {
108+ xc_idx = xcols [ic_idx ]
109+ for (b in which(vapply_1c(x [0L , ..xc_idx ], getClass ) == " double" )) {
110+ xb = xcols [b ]
111+ if (isReallyReal(x [[xb ]])) {
112+ coerce_x = TRUE
113+ break
114+ }
117115 }
118116 }
119117 if (! coerce_x ) {
@@ -122,12 +120,15 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
122120 if (! is.null(attributes(i [[ic ]]))) attributes(val ) = attributes(i [[ic ]]) # to retain Date for example; 3679
123121 set(i , j = ic , value = val )
124122 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 )
123+ if (length(ic_idx )> 1L ) {
124+ xc_idx = xcols [ic_idx ]
125+ for (b in which(vapply_1c(x [0L , ..xc_idx ], getClass ) == " 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 " , paste0(" x." , names(x )[xb ]), xname )
130+ set(x , j = xb , value = val )
131+ }
131132 }
132133 }
133134 }
@@ -139,15 +140,17 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
139140 } else {
140141 if (verbose ) catf(" Coercing integer column %s to type double for join to match type of %s.\n " , iname , xname )
141142 set(i , j = ic , value = as.double(i [[ic ]]))
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 ]]))
143+ if (length(ic_idx )> 1L ) {
144+ xc_idx = xcols [ic_idx ]
145+ for (b in which(vapply_1c(x [0L , ..xc_idx ], getClass ) == " integer" )) {
146+ xb = xcols [b ]
147+ if (verbose ) catf(" Coercing integer column %s to type double for join to match type of %s.\n " , paste0(" x." , names(x )[xb ]), xname )
148+ set(x , j = xb , value = as.double(x [[xb ]]))
149+ }
146150 }
147151 }
148152 }
149153 }
150- }
151154
152155 # # after all modifications of x, check if x has a proper key on all xcols.
153156 # # If not, calculate the order. Also for non-equi joins, the order must be calculated.
0 commit comments