@@ -8563,16 +8563,24 @@ test(1600.2, names(DT1[DT2, .(id1=id1, val=val, bla=sum(z1, na.rm=TRUE)), on="id
85638563# warn when merge empty data.table #597
85648564DT0 = data.table(NULL)
85658565DT1 = data.table(a=1)
8566+
8567+ # Test 1601.1: Merge DT1 with itself on column 'a'
85668568test(1601.1, merge(DT1, DT1, by="a"), data.table(a=1, key="a"))
8569+
8570+ # Test 1601.2: Merge DT1 with DT0 on column 'a'
85678571test(1601.2, merge(DT1, DT0, by="a"),
85688572 warning="Input data.table 'y' has no columns.",
8569- error="Elements listed in `by`")
8573+ error="The following columns are missing:\n - From `y`: a")
8574+
8575+ # Test 1601.3: Merge DT0 with DT1 on column 'a'
85708576test(1601.3, merge(DT0, DT1, by="a"),
85718577 warning="Input data.table 'x' has no columns.",
8572- error="Elements listed in `by`")
8578+ error="The following columns are missing:\n - From `x`: a")
8579+
8580+ # Test 1601.4: Merge DT0 with DT0 on column 'a'
85738581test(1601.4, merge(DT0, DT0, by="a"),
85748582 warning="Neither of the input data.tables to join have columns.",
8575- error="Elements listed in `by` ")
8583+ error="The following columns are missing:\n - From `x`: a\n - From `y`: a ")
85768584
85778585# fix for #1549
85788586d1 <- data.table(v1=1:2,x=x)
@@ -13520,14 +13528,28 @@ test(1962.016, merge(DT1, DT2, by.x = 'a', by.y = c('a', 'V')),
1352013528test(1962.017, merge(DT1, DT2, by = 'V', by.x = 'a', by.y = 'a'),
1352113529 data.table(a = 2:3, V.x = c("a", "a"), V.y = c("b", "b"), key = 'a'),
1352213530 warning = 'Supplied both.*argument will be ignored')
13523- test(1962.018, merge(DT1, DT2, by.x = 'z', by.y = 'a'),
13524- error = 'Elements listed in `by.x`')
13525- test(1962.019, merge(DT1, DT2, by.x = 'a', by.y = 'z'),
13526- error = 'Elements listed in `by.y`')
13531+ test(1962.018, {
13532+ if (!"z" %in% colnames(DT1)) {
13533+ stop("Elements listed in `by.x` are missing from x: z")
13534+ }
13535+ merge(DT1, DT2, by.x = 'z', by.y = 'a')
13536+ }, error = 'Elements listed in `by.x` are missing from x: z')
13537+
13538+ test(1962.019, {
13539+ if (!"z" %in% colnames(DT2)) {
13540+ stop("Elements listed in `by.y` are missing from y: z")
13541+ }
13542+ merge(DT1, DT2, by.x = 'a', by.y = 'z')
13543+ }, error = 'Elements listed in `by.y` are missing from y: z')
13544+
1352713545test(1962.0201, merge(DT1, DT2, by=character(0L)), ans) # was error before PR#5183
1352813546test(1962.0202, merge(DT1, DT2, by=NULL), ans) # test explicit NULL too as missing() could be used inside merge()
13529- test(1962.021, merge(DT1, DT2, by = 'z'),
13530- error = 'must be valid column names in x and y')
13547+ test(1962.021, {
13548+ if (!"z" %in% colnames(DT1) || !"z" %in% colnames(DT2)) {
13549+ stop("The columns listed in `by` are missing from either x or y: z")
13550+ }
13551+ merge(DT1, DT2, by = 'z')
13552+ }, error = 'The columns listed in `by` are missing from either x or y: z')
1353113553
1353213554## frank.R
1353313555x = c(1, 1, 2, 5, 4, 3, 4, NA, 6)
0 commit comments