Skip to content

Commit f10d58a

Browse files
move into helper, with early returns
1 parent b5a41a0 commit f10d58a

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

R/bmerge.R

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ coerce_col = function(dt, col, from_type, to_type, from_name, to_name, from_deta
2525
set(dt, j=col, value=cast_with_attrs(dt[[col]], cast_fun))
2626
}
2727

28+
# data.table::as.ITime, chron::times, nanotime::nanotime
29+
.known_time_classes = c("Date", "POSIXt", "ITime", "times", "nanotime")
30+
.maybe_warn_mismatched_time_types = function(x_class, i_class, x_name, i_name) {
31+
x_class_time = intersect(class(x[[xcol]]), .known_time_classes)
32+
if (!length(x_class_time)) return(invisible())
33+
34+
i_class_time = intersect(class(i[[icol]]), .known_time_classes)
35+
if (!length(i_class_time)) return(invisible())
36+
37+
if (identical(x_class_time, i_class_time)) return(invisible())
38+
39+
warningf("Attempting to join column %s (%s) with column %s (%s). They are likely to be incompatible, so we suggest you convert one to the other's class.",
40+
xname, toString(x_class_time), iname, toString(i_class_time))
41+
}
42+
2843
bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbose)
2944
{
3045
if (roll != 0.0 && length(icols)) {
@@ -92,14 +107,9 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
92107
}
93108
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)
94109
}
95-
# data.table::as.ITime, chron::times, nanotime::nanotime
96-
time_classes = c("Date", "POSIXt", "ITime", "times", "nanotime")
97-
x_class_time = intersect(class(x[[xcol]]), time_classes)
98-
i_class_time = intersect(class(i[[icol]]), time_classes)
99-
if (length(x_class_time) > 0L && length(i_class_time) > 0L && !identical(x_class_time, i_class_time)) {
100-
warningf("Attempting to join column %s (%s) with column %s (%s). They are likely to be incompatible, so we suggest you convert one to the other's class.",
101-
xname, toString(x_class_time), iname, toString(i_class_time))
102-
}
110+
111+
.maybe_warn_mismatched_time_types(class(x[[xcol]]), class(i[[icol]]), x_name, i_name)
112+
103113
if (x_merge_type == i_merge_type) {
104114
if (verbose) catf("%s has same type (%s) as %s. No coercion needed.\n", iname, x_merge_type, xname)
105115
next

0 commit comments

Comments
 (0)