Skip to content

Commit 03968e8

Browse files
committed
move helper outside bmerge
1 parent 637ff58 commit 03968e8

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

R/bmerge.R

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11

2+
3+
mergeType = function(x) {
4+
ans = typeof(x)
5+
if (ans=="integer") { if (is.factor(x)) ans = "factor" }
6+
else if (ans=="double") { if (inherits(x, "integer64")) ans = "integer64" }
7+
# do not call isReallyReal(x) yet because i) if both types are double we don't need to coerce even if one or both sides
8+
# are int-as-double, and ii) to save calling it until we really need it
9+
ans
10+
}
11+
12+
cast_with_atts = function(x, as.f) {
13+
ans = as.f(x)
14+
if (!is.null(attributes(x))) attributes(ans) = attributes(x)
15+
ans
16+
}
17+
18+
coerce_col = function(dt, col, from_type, to_type, from_name, to_name, verbose_msg=NULL) {
19+
if (!is.null(verbose_msg)) catf(verbose_msg, from_type, from_name, to_type, to_name, domain=NULL)
20+
set(dt, j=col, value=cast_with_atts(dt[[col]], match.fun(paste0("as.", to_type))))
21+
}
22+
223
bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbose)
324
{
425
callersi = i
@@ -25,26 +46,6 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
2546

2647
supported = c(ORDERING_TYPES, "factor", "integer64")
2748

28-
mergeType = function(x) {
29-
ans = typeof(x)
30-
if (ans=="integer") { if (is.factor(x)) ans = "factor" }
31-
else if (ans=="double") { if (inherits(x, "integer64")) ans = "integer64" }
32-
# do not call isReallyReal(x) yet because i) if both types are double we don't need to coerce even if one or both sides
33-
# are int-as-double, and ii) to save calling it until we really need it
34-
ans
35-
}
36-
37-
cast_with_atts = function(x, as.f) {
38-
ans = as.f(x)
39-
if (!is.null(attributes(x))) attributes(ans) = attributes(x)
40-
ans
41-
}
42-
43-
coerce_col = function(dt, col, from_type, to_type, from_name, to_name, verbose_msg) {
44-
if (verbose) catf(verbose_msg, from_type, from_name, to_type, to_name, domain=NULL)
45-
set(dt, j=col, value=cast_with_atts(dt[[col]], match.fun(paste0("as.", to_type))))
46-
}
47-
4849
if (nrow(i)) for (a in seq_along(icols)) {
4950
# - check that join columns have compatible types
5051
# - do type coercions if necessary on just the shallow local copies for the purpose of join
@@ -88,7 +89,7 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
8889
}
8990
cfl = c("character", "logical", "factor")
9091
if (x_merge_type %chin% cfl || i_merge_type %chin% cfl) {
91-
msg = gettext("Coercing all-NA %s column %s to type %s to match type of %s.\n")
92+
msg = if(verbose) gettext("Coercing all-NA %s column %s to type %s to match type of %s.\n") else NULL
9293
if (anyNA(i[[icol]]) && allNA(i[[icol]])) {
9394
coerce_col(i, icol, i_merge_type, x_merge_type, iname, xname, msg)
9495
next
@@ -125,7 +126,7 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
125126
}
126127
}
127128
if (coerce_x) {
128-
msg = gettext("Coercing %s column %s (which contains no fractions) to type %s to match type of %s.\n")
129+
msg = if (verbose) gettext("Coercing %s column %s (which contains no fractions) to type %s to match type of %s.\n") else NULL
129130
coerce_col(i, icol, "double", "integer", iname, xname, msg)
130131
set(callersi, j=icol, value=i[[icol]]) # change the shallow copy of i up in [.data.table to reflect in the result, too.
131132
if (length(ic_idx)>1L) {
@@ -137,11 +138,11 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
137138
}
138139
}
139140
if (!coerce_x) {
140-
msg = gettext("Coercing %s column %s to type %s to match type of %s which contains fractions.\n")
141+
msg = if (verbose) gettext("Coercing %s column %s to type %s to match type of %s which contains fractions.\n") else NULL
141142
coerce_col(x, xcol, "integer", "double", xname, iname, msg)
142143
}
143144
} else {
144-
msg = gettext("Coercing %s column %s to type %s for join to match type of %s.\n")
145+
msg = if (verbose) gettext("Coercing %s column %s to type %s for join to match type of %s.\n") else NULL
145146
coerce_col(i, icol, "integer", "double", iname, xname, msg)
146147
if (length(ic_idx)>1L) {
147148
xc_idx = xcols[ic_idx]

0 commit comments

Comments
 (0)