Skip to content

Commit

Permalink
better error handling in join functions. closes #371
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Apr 4, 2014
1 parent b7d22cc commit 1e61396
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@

* internal `DataDots` class protects against missing variables in verbs (#314).

* join functions throws error instead of crashing when there are no common
variables between the data frames, and also give a better error message when
only one data frame has a by variable (#371).

# dplyr 0.1.3

## Bug fixes
Expand Down
8 changes: 7 additions & 1 deletion inst/include/dplyr/DataFrameJoinVisitors.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ namespace dplyr{
std::string name ;
for( int i=0; i<nvisitors; i++){
name = names_[i] ;
visitors[i] = join_visitor( left[name], right[name], name) ;
try{
visitors[i] = join_visitor( left[name], right[name], name) ;
} catch( ... ){
std::stringstream s ;
s << "cannot join on column '" << name << "'" ;
stop(s.str());
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions inst/tests/test-joins.r
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,18 @@ test_that("indices don't get mixed up when nrow(x) > nrow(y). #365",{
expect_equal( res$V2, c("a", "b"))
expect_equal( res$V3, c("n", "m"))
})

test_that("join functions error on column not found #371", {
expect_error(
left_join(data.frame(x=1:5), data.frame(y=1:5), by="x"),
"cannot join on column 'x'"
)
expect_error(
left_join(data.frame(x=1:5), data.frame(y=1:5), by="y"),
"cannot join on column 'y'"
)
expect_error(
left_join(data.frame(x=1:5), data.frame(y=1:5)),
"no common variables"
)
})

0 comments on commit 1e61396

Please sign in to comment.