Skip to content

Commit bb0d6e6

Browse files
modification in bmerge and merge
1 parent 5bbc4d5 commit bb0d6e6

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

R/bmerge.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,18 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
8484
next
8585
}
8686
}
87-
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)
87+
condition_message = sprintf("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)
88+
condition <- structure(
89+
list(
90+
message = condition_message,
91+
bmerge_x_arg_col_name = names(x)[xcol],
92+
bmerge_x_arg_type = x_merge_type,
93+
bmerge_i_arg_col_name = names(i)[icol],
94+
bmerge_i_arg_type = i_merge_type
95+
),
96+
class = c("bmerge_incompatible_type_error", "data.table_error", "error", "condition")
97+
)
98+
stop(condition)
8899
}
89100
# we check factors first to cater for the case when trying to do rolling joins on factors
90101
if (x_merge_type == i_merge_type) {

R/merge.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,25 @@ merge.data.table = function(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FAL
9292
if (!is.null(incomparables)) {
9393
# %fin% to be replaced when #5232 is implemented/closed
9494
"%fin%" = function(x, table) if (is.character(x) && is.character(table)) x %chin% table else x %in% table
95-
xind = rowSums(x[, lapply(.SD, function(x) !(x %fin% incomparables)), .SDcols=by.x]) == length(by)
96-
yind = rowSums(y[, lapply(.SD, function(x) !(x %fin% incomparables)), .SDcols=by.y]) == length(by)
95+
xind = rowSums(x[, lapply(.SD, function(x) !(x %fin% incomparables)), .SDcols=by.x]) == length(by.x)
96+
yind = rowSums(y[, lapply(.SD, function(x) !(x %fin% incomparables)), .SDcols=by.y]) == length(by.y)
9797
# subset both so later steps still work
9898
x = x[xind]
9999
y = y[yind]
100100
}
101-
dt = y[x, nomatch=if (all.x) NA else NULL, on=by, allow.cartesian=allow.cartesian] # includes JIS columns (with a i. prefix if conflict with x names)
101+
dt = tryCatch({
102+
y[x, nomatch=if (all.x) NA else NULL, on=by, allow.cartesian=allow.cartesian]
103+
},
104+
bmerge_incompatible_type_error = function(e) {
105+
x_part_col_name = e$bmerge_i_arg_col_name
106+
x_part_type = e$bmerge_i_arg_type
107+
y_part_col_name = e$bmerge_x_arg_col_name
108+
y_part_type = e$bmerge_x_arg_type
109+
110+
msg = sprintf("Incompatible join types: x.%s (%s) and i.%s (%s). Factor columns must join to factor or character columns.", x_part_col_name, x_part_type, y_part_col_name, y_part_type)
111+
stop(errorCondition(message = msg, class = c("datatable_merge_type_error", "data.table_error", "error", "condition")))
112+
}
113+
)
102114

103115
if (all.y && nrow(y)) { # If y does not have any rows, no need to proceed
104116
# Perhaps not very commonly used, so not a huge deal that the join is redone here.

0 commit comments

Comments
 (0)