Skip to content

Commit 0f821fe

Browse files
committed
updated condition
1 parent e18779a commit 0f821fe

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

R/bmerge.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,22 @@ bmerge = function(i, x, icols, xcols, roll, rollends, nomatch, mult, ops, verbos
8686
}
8787
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)
8888
}
89+
if (i_merge_type == "complex") {
90+
if (any(Im(i[[icol]]) != 0, na.rm=TRUE)) {
91+
stopf("Joining on complex numbers with non-zero imaginary part is not supported. Column: %s", iname)
92+
}
93+
from_detail = gettext(" (complex with zero imaginary part)")
94+
coerce_col(i, icol, "complex", "double", iname, xname, from_detail=from_detail, verbose=verbose)
95+
i_merge_type = "double"
96+
}
97+
if (x_merge_type == "complex") {
98+
if (any(Im(x[[xcol]]) != 0, na.rm=TRUE)) {
99+
stopf("Joining on complex numbers with non-zero imaginary part is not supported. Column: %s", xname)
100+
}
101+
from_detail = gettext(" (complex with zero imaginary part)")
102+
coerce_col(x, xcol, "complex", "double", xname, iname, from_detail=from_detail, verbose=verbose)
103+
x_merge_type = "double"
104+
}
89105
# we check factors first to cater for the case when trying to do rolling joins on factors
90106
if (x_merge_type == i_merge_type) {
91107
if (verbose) catf("%s has same type (%s) as %s. No coercion needed.\n", iname, x_merge_type, xname)

inst/tests/tests.Rraw

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15746,7 +15746,7 @@ DT1 = data.table(a = sample(3L, 15L, TRUE) + .1, b=sample(c(TRUE, FALSE, NA), 15
1574615746
DT2 = data.table(a = sample(3L, 6L, TRUE) + .1, b=sample(c(TRUE, FALSE, NA), 6L, TRUE))
1574715747
test(2069.32, DT1[DT2, .(y = sum(b, na.rm=TRUE)), by=.EACHI, on=c(a = 'a', b="b")]$y, rep(0L, 6L))
1574815748
DT = data.table(z = 1i)
15749-
test(2069.33, DT[DT, on = 'z'], error = "Type 'complex' is not supported for joining/merging")
15749+
test(2069.33, DT[DT, on = 'z'], error = "Joining on complex numbers with non-zero imaginary part is not supported. Column: i.z")
1575015750

1575115751
# forder verbose message when !isReallyReal Date, #1738
1575215752
date_dbl = as.Date(as.double(seq(as.Date("2015-01-01"), as.Date("2015-01-05"), by="days")), origin="1970-01-01")
@@ -17326,7 +17326,7 @@ test(2182.75, melt(data.table(a=10, b=20), measure.vars=list(n="a"), variable.fa
1732617326
measurev = function(cols)cols # user-defined function for computing measure.vars, same name as data.table::measure but user-defined version should be used.
1732717327
test(2183.00001, melt(DT.wide, measure.vars=measurev()), data.table(variable=factor(c("a2","b1","b2")), value=c(2,1,2)))
1732817328
measurev = list("foo", "bar")#measurev below should not use this since it is not a function.
17329-
test(2183.00002, melt(DTid, measure.vars=measurev(list(value.name=NULL, num=as.complex), pattern="([ab])([12])")), error="Type 'complex' is not supported for joining/merging")
17329+
test(2183.00002, melt(DTid, measure.vars=measurev(list(value.name=NULL, num=as.complex), pattern="([ab])([12])")), error="variable_table does not support column type 'complex' for column 'num'")
1733017330
test(2183.00004, melt(DTid, measure.vars=measurev(list(value.name=NULL, istr=NULL), pattern="([ab])([12])"))[order(b)], data.table(id=1, istr=paste(c(1,2)), a=c(NA, 2), b=c(1,2)))
1733117331
test(2183.00005, melt(DTid, measure.vars=measurev(list(column=NULL, istr=NULL), pattern="([ab])([12])", multiple.keyword="column"))[order(b)], data.table(id=1, istr=paste(c(1,2)), a=c(NA, 2), b=c(1,2)))#same computation but different multiple.keyword
1733217332
iris.dt = data.table(iris)
@@ -17349,7 +17349,7 @@ test(2183.00060, melt(DTid, measure.vars=measurev(list(letter=myfac, value.name=
1734917349
measure = function(cols)cols # user-defined function for computing measure.vars, same name as data.table::measure but user-defined version should be used.
1735017350
test(2183.01, melt(DT.wide, measure.vars=measure()), data.table(variable=factor(c("a2","b1","b2")), value=c(2,1,2)))
1735117351
measure = list("foo", "bar")#measure below should not use this since it is not a function.
17352-
test(2183.02, melt(DTid, measure.vars=measure(value.name, num=as.complex, pattern="([ab])([12])")), error="Type 'complex' is not supported for joining/merging")
17352+
test(2183.02, melt(DTid, measure.vars=measure(value.name, num=as.complex, pattern="([ab])([12])")), error="variable_table does not support column type 'complex' for column 'num'")
1735317353
test(2183.03, melt(DTid, measure.vars=structure(list(a=c(NA,"a2"),b=c("b1","b2")), variable_table=data.table(number=as.complex(1:2)))), error="variable_table does not support column type 'complex' for column 'number'")
1735417354
test(2183.04, melt(DTid, measure.vars=measure(value.name, istr, pattern="([ab])([12])"))[order(b)], data.table(id=1, istr=paste(c(1,2)), a=c(NA, 2), b=c(1,2)))
1735517355
test(2183.05, melt(DTid, measure.vars=measure(column, istr, pattern="([ab])([12])", multiple.keyword="column"))[order(b)], data.table(id=1, istr=paste(c(1,2)), a=c(NA, 2), b=c(1,2)))#same computation but different multiple.keyword
@@ -21167,3 +21167,41 @@ test(2317.6, DT1[DF1, on='a', .(d = x.a + i.d)]$d, 5)
2116721167
test(2317.7, DT1[DF2, on='a', e := i.e]$e, 5)
2116821168
test(2317.8, DT1[DF2, on='a', e2 := x.a + i.e]$e2, 6)
2116921169
test(2317.9, DT1[DF2, on='a', .(e = x.a + i.e)]$e, 6)
21170+
21171+
#6627
21172+
test(2318.1, {
21173+
DT1 = data.table(a = complex(real = 1:3, imaginary = 0), x = letters[1:3])
21174+
DT2 = data.table(a = 2L)
21175+
res = DT1[DT2, on = "a"]
21176+
identical(res$x, "b") && is.numeric(res$a) && res$a == 2
21177+
})
21178+
test(2318.2, {
21179+
DT1 = data.table(a = complex(real = c(1, 2, 3), imaginary = 0), x = letters[1:3])
21180+
DT2 = data.table(a = c(2.0, 3.0)) # double
21181+
res = DT1[DT2, on = "a"]
21182+
identical(res$x, c("b", "c")) && typeof(res$a) == "double"
21183+
})
21184+
test(2318.3, {
21185+
DT1 = data.table(a = c(1L, 2L, 3L)) # integer
21186+
DT2 = data.table(a = complex(real = c(2, 3), imaginary = 0), y = letters[1:2])
21187+
res = DT1[DT2, on = "a"]
21188+
identical(res$y, c("a", "b")) && typeof(res$a) == "integer"
21189+
})
21190+
test(2318.4, {
21191+
DT1 = data.table(a = complex(real = c(1, 2, 3), imaginary = 0), x = letters[1:3])
21192+
DT2 = data.table(a = complex(real = c(2, 3), imaginary = 0))
21193+
res = DT1[DT2, on = "a"]
21194+
identical(res$x, c("b", "c")) && typeof(res$a) == "complex"
21195+
})
21196+
test(2318.5, {
21197+
DT1 = data.table(a = complex(real = c(1, 2), imaginary = c(0, 1)), x = letters[1:2])
21198+
DT2 = data.table(a = 2L)
21199+
msg = tryCatch(DT1[DT2, on = "a"], error = function(e) e$message)
21200+
grepl("non-zero imaginary part", msg)
21201+
})
21202+
test(2318.6, {
21203+
DT1 = data.table(a = 2L)
21204+
DT2 = data.table(a = complex(real = c(1, 2), imaginary = c(0, 1)), y = c("a", "b"))
21205+
msg = tryCatch(DT1[DT2, on = "a"], error = function(e) e$message)
21206+
grepl("non-zero imaginary part", msg)
21207+
})

0 commit comments

Comments
 (0)