You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# - do type coercions if necessary on just the shallow local copies for the purpose of join
45
-
# - handle factor columns appropriately
46
-
# Note that if i is keyed, if this coerces i's key gets dropped by set()
47
-
ic=icols[a]
48
-
xc=xcols[a]
49
-
xtype=xtypes[a]
50
-
itype=itypes[a]
51
-
xname= paste0("x.", names(xhead)[a])
52
-
iname= paste0("i.", names(ihead)[a])
53
-
if (!xtype %chin% supported) stopf("%s is type %s which is not supported by data.table join", xname, xtype)
54
-
if (!itype %chin% supported) stopf("%s is type %s which is not supported by data.table join", iname, itype)
55
-
if (xtype=="factor"||itype=="factor") {
56
-
if (roll!=0.0&&a==length(icols))
57
-
stopf("Attempting roll join on factor column when joining %s to %s. Only integer, double or character columns may be roll joined.", xname, iname)
58
-
if (xtype=="factor"&&itype=="factor") {
59
-
if (verbose) catf("Matching %s factor levels to %s factor levels.\n", iname, xname)
60
-
set(i, j=ic, value=chmatch(levels(i[[ic]]), levels(x[[xc]]), nomatch=0L)[i[[ic]]]) # nomatch=0L otherwise a level that is missing would match to NA values
61
-
next
62
-
} else {
63
-
if (xtype=="character") {
64
-
if (verbose) catf("Coercing factor column %s to type character to match type of %s.\n", iname, xname)
65
-
set(i, j=ic, value=val<-as.character(i[[ic]]))
66
-
set(callersi, j=ic, value=val) # factor in i joining to character in x will return character and not keep x's factor; e.g. for antaresRead #3581
67
-
next
68
-
} elseif (itype=="character") {
69
-
if (verbose) catf("Matching character column %s to factor levels in %s.\n", iname, xname)
# - do type coercions if necessary on just the shallow local copies for the purpose of join
45
+
# - handle factor columns appropriately
46
+
# Note that if i is keyed, if this coerces i's key gets dropped by set()
47
+
ic=icols[a]
48
+
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]
53
+
if (!x_merge_type %chin% supported) stopf("%s is type %s which is not supported by data.table join", xname, x_merge_type)
54
+
if (!i_merge_type %chin% supported) stopf("%s is type %s which is not supported by data.table join", iname, i_merge_type)
55
+
if (x_merge_type=="factor"||i_merge_type=="factor") {
56
+
if (roll!=0.0&&a==length(icols))
57
+
stopf("Attempting roll join on factor column when joining %s to %s. Only integer, double or character columns may be roll joined.", xname, iname)
58
+
if (x_merge_type=="factor"&&i_merge_type=="factor") {
59
+
if (verbose) catf("Matching %s factor levels to %s factor levels.\n", iname, xname)
60
+
set(i, j=ic, value=chmatch(levels(i[[ic]]), levels(x[[xc]]), nomatch=0L)[i[[ic]]]) # nomatch=0L otherwise a level that is missing would match to NA values
61
+
next
62
+
} else {
63
+
if (x_merge_type=="character") {
64
+
if (verbose) catf("Coercing factor column %s to type character to match type of %s.\n", iname, xname)
65
+
set(i, j=ic, value=val<-as.character(i[[ic]]))
66
+
set(callersi, j=ic, value=val) # factor in i joining to character in x will return character and not keep x's factor; e.g. for antaresRead #3581
67
+
next
68
+
} elseif (i_merge_type=="character") {
69
+
if (verbose) catf("Matching character column %s to factor levels in %s.\n", iname, xname)
if (anyNA(i[[ic]])) newvalue[is.na(i[[ic]])] =NA_integer_# NA_character_ should match to NA in factor, #3809
72
+
set(i, j=ic, value=newvalue)
73
+
next
74
+
}
75
+
}
76
+
stopf("Incompatible join types: %s (%s) and %s (%s). Factor columns must join to factor or character columns.", xname, x_merge_type, iname, i_merge_type)
77
+
}
78
+
if (x_merge_type==i_merge_type) {
79
+
if (verbose) catf("%s has same type (%s) as %s. No coercion needed.\n", iname, x_merge_type, xname)
73
80
next
74
81
}
75
-
}
76
-
stopf("Incompatible join types: %s (%s) and %s (%s). Factor columns must join to factor or character columns.", xname, xtype, iname, itype)
77
-
}
78
-
if (xtype==itype) {
79
-
if (anyDuplicated(icols) &&!all() && duplicated(icols, fromLast=TRUE)[a]) {
80
-
set(x, j=xc, value=as.double(x[[xc]]))
81
-
set(i, j=ic, value=as.double(i[[ic]]))
82
-
if (verbose) catf("%s and %s are both Dates. R does not guarentee a type for Date internally, hence, coercing to double.\n", iname, xname)
83
-
} else {
84
-
if (verbose) catf("%s has same type (%s) as %s. No coercion needed.\n", iname, xtype, xname)
85
-
}
86
-
next
87
-
}
88
-
if (xtype=="character"||itype=="character"||
89
-
xtype=="logical"||itype=="logical"||
90
-
xtype=="factor"||itype=="factor") {
91
-
if (anyNA(i[[ic]]) && allNA(i[[ic]])) {
92
-
if (verbose) catf("Coercing all-NA %s (%s) to type %s to match type of %s.\n", iname, itype, xtype, xname)
if (xtype=="integer64") { w=i; wc=ic; wclass=itype; } else { w=x; wc=xc; wclass=xtype; nm=rev(nm) } # w is which to coerce
106
-
if (wclass=="integer"|| (wclass=="double"&&!isReallyReal(w[[wc]]))) {
107
-
if (verbose) catf("Coercing %s column %s%s to type integer64 to match type of %s.\n", wclass, nm[1L], if (wclass=="double") " (which contains no fractions)"else"", nm[2L])
108
-
set(w, j=wc, value=bit64::as.integer64(w[[wc]]))
109
-
} else stopf("Incompatible join types: %s is type integer64 but %s is type double and contains fractions", nm[2L], nm[1L])
110
-
} else {
111
-
# just integer and double left
112
-
if (itype=="double") {
113
-
if (!isReallyReal(i[[ic]])) {
114
-
# common case of ad hoc user-typed integers missing L postfix joining to correct integer keys
115
-
# we've always coerced to int and returned int, for convenience.
116
-
if (verbose) catf("Coercing double column %s (which contains no fractions) to type integer to match type of %s.\n", iname, xname)
117
-
val= as.integer(i[[ic]])
118
-
if (!is.null(attributes(i[[ic]]))) attributes(val) = attributes(i[[ic]]) # to retain Date for example; 3679
119
-
set(i, j=ic, value=val)
120
-
set(callersi, j=ic, value=val) # change the shallow copy of i up in [.data.table to reflect in the result, too.
82
+
if (x_merge_type=="character"||i_merge_type=="character"||
if (x_merge_type=="integer64"||i_merge_type=="integer64") {
98
+
nm= c(iname, xname)
99
+
if (x_merge_type=="integer64") { w=i; wc=ic; wclass=i_merge_type; } else { w=x; wc=xc; wclass=x_merge_type; nm=rev(nm) } # w is which to coerce
100
+
if (wclass=="integer"|| (wclass=="double"&&!isReallyReal(w[[wc]]))) {
101
+
if (verbose) catf("Coercing %s column %s%s to type integer64 to match type of %s.\n", wclass, nm[1L], if (wclass=="double") " (which contains no fractions)"else"", nm[2L])
102
+
set(w, j=wc, value=bit64::as.integer64(w[[wc]]))
103
+
} else stopf("Incompatible join types: %s is type integer64 but %s is type double and contains fractions", nm[2L], nm[1L])
121
104
} else {
122
-
if (verbose) catf("Coercing integer column %s to type double to match type of %s which contains fractions.\n", xname, iname)
123
-
set(x, j=xc, value=as.double(x[[xc]]))
105
+
# just integer and double left
106
+
if (i_merge_type=="double") {
107
+
if (!isReallyReal(i[[ic]])) {
108
+
# common case of ad hoc user-typed integers missing L postfix joining to correct integer keys
109
+
# 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 {
116
+
if (verbose) catf("Coercing integer column %s to type double to match type of %s which contains fractions.\n", xname, iname)
117
+
set(x, j=xc, value=as.double(x[[xc]]))
118
+
}
119
+
} else {
120
+
if (verbose) catf("Coercing integer column %s to type double for join to match type of %s.\n", iname, xname)
121
+
set(i, j=ic, value=as.double(i[[ic]]))
122
+
ic_idx= which(ic==icols)
123
+
if (length(ic_idx)>1) {
124
+
for (bin 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
+
}
130
+
}
124
131
}
125
-
} else {
126
-
if (verbose) catf("Coercing integer column %s to type double for join to match type of %s.\n", iname, xname)
127
-
set(i, j=ic, value=as.double(i[[ic]]))
128
132
}
129
133
}
130
-
}}
131
134
132
135
## after all modifications of x, check if x has a proper key on all xcols.
133
136
## If not, calculate the order. Also for non-equi joins, the order must be calculated.
0 commit comments