-
Notifications
You must be signed in to change notification settings - Fork 993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: improve merge.data.table error messages for missing keys (#6556) #6713
base: master
Are you sure you want to change the base?
Changes from all commits
da77d10
ac9d594
214f93a
785a2af
2a1c392
771fbc0
ad66677
ed5bdb1
186cbd5
e849fe6
912d0cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8563,18 +8563,21 @@ test(1600.2, names(DT1[DT2, .(id1=id1, val=val, bla=sum(z1, na.rm=TRUE)), on="id | |
|
||
# warn when merge empty data.table #597 | ||
DT0 = data.table(NULL) | ||
DT1 = data.table(a=1) | ||
test(1601.1, merge(DT1, DT1, by="a"), data.table(a=1, key="a")) | ||
test(1601.2, merge(DT1, DT0, by="a"), | ||
warning="Input data.table 'y' has no columns.", | ||
error="Elements listed in `by`") | ||
test(1601.3, merge(DT0, DT1, by="a"), | ||
warning="Input data.table 'x' has no columns.", | ||
error="Elements listed in `by`") | ||
test(1601.4, merge(DT0, DT0, by="a"), | ||
warning="Neither of the input data.tables to join have columns.", | ||
error="Elements listed in `by`") | ||
|
||
DT1 = data.table(a = 1) | ||
|
||
test(1601.1, merge(DT1, DT1, by = "a"), data.table(a = 1, key = "a")) | ||
test(1601.2, | ||
merge(DT1, DT0, by = "a"), | ||
warning = "Input data.table 'y' has no columns.", | ||
error = "The following columns are missing:\n - From y: [a]") | ||
test(1601.3, | ||
merge(DT0, DT1, by = "a"), | ||
warning = "Input data.table 'x' has no columns.", | ||
error = "The following columns are missing:\n - From x: [a]") | ||
test(1601.4, | ||
merge(DT0, DT0, by = "a"), | ||
warning = "Neither of the input data.tables to join have columns.", | ||
error = "The following columns are missing:\n - From x: [a]\n - From y: [a]") | ||
# fix for #1549 | ||
d1 <- data.table(v1=1:2,x=x) | ||
d2 <- data.table(v1=3:4) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove newlines in error messages There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove, or add? I find the current output hard to read:
I would find this much more readable (possibly indenting the second line):
At a higher level, I wonder if translation would be easier if we instead structured the message like so:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The longer sentence structure would definitely be easier to translate. |
||
|
@@ -13546,14 +13549,17 @@ test(1962.016, merge(DT1, DT2, by.x = 'a', by.y = c('a', 'V')), | |
test(1962.017, merge(DT1, DT2, by = 'V', by.x = 'a', by.y = 'a'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please remove backticks, which are for markdown, not error messages There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Toby, sorry, I disagree. The backticks serve to highlight that this is a code object, and not a plain English word. Without them, a reader can easily be confused into thinking there's some grammatical mistake "in by", or otherwise struggle to parse the message they're given. Of course, we could choose some other convention (single/double quotes, e.g.), and we should try and pick one and stick to it throughout the codebase... but that's a separate issue. Personally, these days I am using |
||
data.table(a = 2:3, V.x = c("a", "a"), V.y = c("b", "b"), key = 'a'), | ||
warning = 'Supplied both.*argument will be ignored') | ||
test(1962.018, merge(DT1, DT2, by.x = 'z', by.y = 'a'), | ||
error = 'Elements listed in `by.x`') | ||
test(1962.019, merge(DT1, DT2, by.x = 'a', by.y = 'z'), | ||
error = 'Elements listed in `by.y`') | ||
test(1962.018, | ||
merge(DT1, DT2, by.x = 'z', by.y = 'a'), | ||
error = "The following columns listed in 'by.x' are missing from x: [z]") | ||
test(1962.019, | ||
merge(DT1, DT2, by.x = 'a', by.y = 'z'), | ||
error = "The following columns listed in 'by.y' are missing from y: [z]") | ||
test(1962.0201, merge(DT1, DT2, by=character(0L)), ans) # was error before PR#5183 | ||
test(1962.0202, merge(DT1, DT2, by=NULL), ans) # test explicit NULL too as missing() could be used inside merge() | ||
test(1962.021, merge(DT1, DT2, by = 'z'), | ||
error = 'must be valid column names in x and y') | ||
test(1962.021, { | ||
merge(DT1, DT2, by = 'z') | ||
}, error = 'The following columns are missing:\n - From x: [z]\n - From y: [z]') | ||
|
||
## frank.R | ||
x = c(1, 1, 2, 5, 4, 3, 4, NA, 6) | ||
|
@@ -16931,7 +16937,7 @@ test(2144, rbind(DT,list(c=4L,a=7L)), error="Column 1 ['c'] of item 2 is missing | |
A = data.table(A='a') | ||
B = data.table(B='b') | ||
test(2145.1, A[B, on=character(0)], error = "'on' argument should be a named atomic vector") | ||
test(2145.2, merge(A, B, by=character(0) ), error = "non-empty vector of column names for `by` is required.") | ||
test(2145.2, merge(A, B, by=character(0) ), error = "A non-empty vector of column names for by is required.") | ||
test(2145.3, merge(A, B, by.x=character(0), by.y=character(0)), error = "non-empty vector of column names is required") | ||
# Also shouldn't crash when using internal functions | ||
test(2145.4, bmerge(A, B, integer(), integer(), 0, c(FALSE, TRUE), NA, 'all', integer(), FALSE), error = 'icols and xcols must be non-empty') | ||
|
@@ -18014,7 +18020,7 @@ test(2230.4, setDF(merge(DT, y, by="k2", incomparables=c(1, NA, 4, 5))), merge(x | |
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))) | ||
test(2230.6, merge(DT, y, by="k2", unk=1), merge(DT, y, by="k2"), warning="Unknown argument 'unk' has been passed.") | ||
test(2230.7, merge(DT, y, by="k2", NULL, NULL, FALSE, FALSE, FALSE, TRUE, c(".x", ".y"), TRUE, getOption("datatable.allow.cartesian"), NULL, 1L), | ||
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.")) | ||
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.")) | ||
|
||
# weighted.mean GForce optimized, #3977 | ||
old = options(datatable.optimize=1L) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't need comments like this unless the test is really obscure. You definitely don't need to write 'Test 1601.1' when that is very obvious from the next line.
Such comments are very easy to fall out of sync with the actual code as it changes over time. See for example https://swimm.io/learn/code-collaboration/comments-in-code-best-practices-and-mistakes-to-avoid.
If the test case's purpose is not obvious from the written code, often that's a sign that the test is poorly designed -- typically we should strive for the purpose of the test to be immediately apparent, only rarely needing small clarifying comments.