Skip to content

Commit 771fbc0

Browse files
committed
introduced teh latest changes
1 parent 2a1c392 commit 771fbc0

File tree

2 files changed

+52
-56
lines changed

2 files changed

+52
-56
lines changed

R/merge.R

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,44 @@ merge.data.table = function(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FAL
1111
by = key(x)
1212
}
1313
}
14-
x0 = length(x)==0L
15-
y0 = length(y)==0L
16-
if (x0 || y0) {
17-
if (x0 && y0)
14+
x0 = length(x) == 0L
15+
y0 = length(y) == 0L
16+
17+
if (x0 || y0) {
18+
if (x0 && y0) {
1819
warningf("Neither of the input data.tables to join have columns.")
19-
else if (x0)
20-
warningf("Input data.table x has no columns.")
21-
else
22-
warningf("Input data.table y has no columns.")
20+
} else if (x0) {
21+
warningf("Input data.table '%s' has no columns.", "x")
22+
} else if (y0) {
23+
warningf("Input data.table '%s' has no columns.", "y")
24+
}
2325
}
24-
check_duplicate_names(x)
25-
check_duplicate_names(y)
2626

27-
nm_x = names(x)
28-
nm_y = names(y)
27+
check_duplicate_names(x)
28+
check_duplicate_names(y)
29+
30+
nm_x = names(x)
31+
nm_y = names(y)
32+
2933

3034
## set up 'by'/'by.x'/'by.y'
3135
if ( (!is.null(by.x) || !is.null(by.y)) && length(by.x)!=length(by.y) )
32-
stopf("by.x and by.y must be of same length.")
36+
stopf("'by.x' and 'by.y' must be of same length.")
3337
if (!missing(by) && !missing(by.x))
34-
warningf("Supplied both by and by.x/by.y. by argument will be ignored.")
38+
warningf("Supplied both by and 'by.x/by.y.' by argument will be ignored.")
3539
if (!is.null(by.x)) {
3640
if (length(by.x)==0L || !is.character(by.x) || !is.character(by.y))
37-
stopf("A non-empty vector of column names is required for by.x and by.y.")
41+
stopf("A non-empty vector of column names is required for 'by.x' and 'by.y'.")
3842
if (!all(by.x %chin% nm_x)) {
39-
missing_in_x <- setdiff(by.x, nm_x)
40-
stopf("The following columns listed in by.x are missing from x: %s",
41-
toString(missing_in_x))
42-
}
43-
if (!all(by.y %chin% nm_y)) {
44-
missing_in_y <- setdiff(by.y, nm_y)
45-
stopf("The following columns listed in by.y are missing from y: %s",
46-
toString(missing_in_y))
47-
}
48-
by = by.x
49-
names(by) = by.y
43+
missing_in_x <- setdiff(by.x, nm_x)
44+
stopf("The following columns listed in 'by.x' are missing from x: %s", brackify(missing_in_x))
45+
}
46+
if (!all(by.y %chin% nm_y)) {
47+
missing_in_y <- setdiff(by.y, nm_y)
48+
stopf("The following columns listed in 'by.y' are missing from y: %s", brackify(missing_in_y))
49+
}
50+
by = by.x
51+
names(by) = by.y
5052
} else {
5153
if (is.null(by))
5254
by = intersect(key(x), key(y))
@@ -59,14 +61,13 @@ merge.data.table = function(x, y, by = NULL, by.x = NULL, by.y = NULL, all = FAL
5961
missing_in_x <- setdiff(by, nm_x)
6062
missing_in_y <- setdiff(by, nm_y)
6163
if (length(missing_in_x) > 0 || length(missing_in_y) > 0) {
62-
stopf("The following columns are missing:%s%s",
63-
if (length(missing_in_x) > 0) sprintf(" - From x: %s", toString(missing_in_x)) else "",
64-
if (length(missing_in_y) > 0) sprintf(" - From y: %s", toString(missing_in_y)) else "")
64+
stopf(gettextf("The following columns are missing:\n%s%s",
65+
if (length(missing_in_x) > 0) gettextf(" - From x: %s\n", brackify(missing_in_x)) else "",
66+
if (length(missing_in_y) > 0) gettextf(" - From y: %s\n", brackify(missing_in_y)) else ""))
6567
}
6668
by = unname(by)
6769
by.x = by.y = by
68-
}
69-
70+
}
7071
# warn about unused arguments #2587
7172
if (length(list(...))) {
7273
ell = as.list(substitute(list(...)))[-1L]

inst/tests/tests.Rraw

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8563,26 +8563,21 @@ test(1600.2, names(DT1[DT2, .(id1=id1, val=val, bla=sum(z1, na.rm=TRUE)), on="id
85638563

85648564
# warn when merge empty data.table #597
85658565
DT0 = data.table(NULL)
8566-
DT1 = data.table(a=1)
8567-
8568-
# Test 1601.1: Merge DT1 with itself on column 'a'
8569-
test(1601.1, merge(DT1, DT1, by="a"), data.table(a=1, key="a"))
8570-
8571-
# Test 1601.2: Merge DT1 with DT0 on column 'a'
8572-
test(1601.2, merge(DT1, DT0, by="a"),
8573-
warning="Input data.table y has no columns.",
8574-
error="The following columns are missing: - From y: a")
8575-
8576-
# Test 1601.3: Merge DT0 with DT1 on column 'a'
8577-
test(1601.3, merge(DT0, DT1, by="a"),
8578-
warning="Input data.table x has no columns.",
8579-
error="The following columns are missing: - From x: a")
8580-
8581-
# Test 1601.4: Merge DT0 with DT0 on column 'a'
8582-
test(1601.4, merge(DT0, DT0, by="a"),
8583-
warning="Neither of the input data.tables to join have columns.",
8584-
error="The following columns are missing: - From x: a - From y: a")
8585-
8566+
DT1 = data.table(a = 1)
8567+
8568+
test(1601.1, merge(DT1, DT1, by = "a"), data.table(a = 1, key = "a"))
8569+
test(1601.2,
8570+
merge(DT1, DT0, by = "a"),
8571+
warning = "Input data.table 'y' has no columns.",
8572+
error = "The following columns are missing:\n - From y: [a]")
8573+
test(1601.3,
8574+
merge(DT0, DT1, by = "a"),
8575+
warning = "Input data.table 'x' has no columns.",
8576+
error = "The following columns are missing:\n - From x: [a]")
8577+
test(1601.4,
8578+
merge(DT0, DT0, by = "a"),
8579+
warning = "Neither of the input data.tables to join have columns.",
8580+
error = "The following columns are missing:\n - From x: [a]\n - From y: [a]")
85868581
# fix for #1549
85878582
d1 <- data.table(v1=1:2,x=x)
85888583
d2 <- data.table(v1=3:4)
@@ -13552,18 +13547,18 @@ test(1962.017, merge(DT1, DT2, by = 'V', by.x = 'a', by.y = 'a'),
1355213547
warning = 'Supplied both.*argument will be ignored')
1355313548
test(1962.018,
1355413549
merge(DT1, DT2, by.x = 'z', by.y = 'a'),
13555-
error = 'The following columns listed in by.x are missing from x: z')
13550+
error = "The following columns listed in 'by.x' are missing from x: [z]")
1355613551
test(1962.019,
1355713552
merge(DT1, DT2, by.x = 'a', by.y = 'z'),
13558-
error = 'The following columns listed in by.y are missing from y: z')
13553+
error = "The following columns listed in 'by.y' are missing from y: [z]")
1355913554
test(1962.0201, merge(DT1, DT2, by=character(0L)), ans) # was error before PR#5183
1356013555
test(1962.0202, merge(DT1, DT2, by=NULL), ans) # test explicit NULL too as missing() could be used inside merge()
1356113556
test(1962.021, {
1356213557
if (!"z" %in% colnames(DT1) || !"z" %in% colnames(DT2)) {
13563-
stop("The columns listed in `by` are missing from either x or y: z")
13558+
stop("The columns listed in `by` are missing from either x or y: [z]")
1356413559
}
1356513560
merge(DT1, DT2, by = 'z')
13566-
}, error = 'The columns listed in `by` are missing from either x or y: z')
13561+
}, error = 'The columns listed in `by` are missing from either x or y: [z]')
1356713562

1356813563
## frank.R
1356913564
x = c(1, 1, 2, 5, 4, 3, 4, NA, 6)
@@ -18024,7 +18019,7 @@ test(2230.4, setDF(merge(DT, y, by="k2", incomparables=c(1, NA, 4, 5))), merge(x
1802418019
test(2230.5, setDF(merge(DT, y, by="k2", incomparables=c(NA, 3, 4, 5))), merge(x, y, by="k2", incomparables=c(NA,3,4,5)))
1802518020
test(2230.6, merge(DT, y, by="k2", unk=1), merge(DT, y, by="k2"), warning="Unknown argument 'unk' has been passed.")
1802618021
test(2230.7, merge(DT, y, by="k2", NULL, NULL, FALSE, FALSE, FALSE, TRUE, c(".x", ".y"), TRUE, getOption("datatable.allow.cartesian"), NULL, 1L),
18027-
merge(DT, y, by="k2"), warning=c("Supplied both by and by.x/by.y. by argument will be ignored.", "Passed 1 unknown and unnamed arguments."))
18022+
merge(DT, y, by="k2"), warning=c("Supplied both by and 'by.x/by.y.' by argument will be ignored.", "Passed 1 unknown and unnamed arguments."))
1802818023

1802918024
# weighted.mean GForce optimized, #3977
1803018025
old = options(datatable.optimize=1L)

0 commit comments

Comments
 (0)