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
-
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
43
+
# - check that join columns have compatible types
44
+
# - 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
74
}
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
75
}
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)
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)
80
+
next
81
+
}
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])
104
-
} else {
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)
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])
104
+
} else {
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.
119
115
} 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
-
}
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]]))
129
128
}
130
129
}
131
130
}
132
-
}
131
+
}
132
+
}
133
133
}
134
134
135
135
## after all modifications of x, check if x has a proper key on all xcols.
0 commit comments