Skip to content

Commit

Permalink
better protection for intermediary results in mutate. #1231
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed Jul 6, 2015
1 parent 96c6124 commit 664985d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# dplyr 0.4.2.9000

* `mutate` better protects intermediary results (#1231).

* `mutate` and `arrange` works on empty data frames (#1142).

* `slice` handles NA (#1235).
Expand Down
25 changes: 13 additions & 12 deletions src/dplyr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1996,6 +1996,8 @@ SEXP mutate_not_grouped(DataFrame df, const LazyDots& dots){
}

CallProxy call_proxy(df) ;
List results(nexpr) ;

for( int i=0; i<nexpr; i++){
Rcpp::checkUserInterrupt() ;
const Lazy& lazy = dots[i] ;
Expand All @@ -2005,44 +2007,43 @@ SEXP mutate_not_grouped(DataFrame df, const LazyDots& dots){
Environment env = lazy.env() ;
call_proxy.set_env(env) ;

RObject result(R_NilValue) ;
if( TYPEOF(call) == SYMSXP ){
if(call_proxy.has_variable(call)){
result = call_proxy.get_variable(PRINTNAME(call)) ;
results[i] = call_proxy.get_variable(PRINTNAME(call)) ;
} else {
result = shared_SEXP(env.find(CHAR(PRINTNAME(call)))) ;
results[i] = shared_SEXP(env.find(CHAR(PRINTNAME(call)))) ;
}
} else if( TYPEOF(call) == LANGSXP ){
call_proxy.set_call( call );
result = call_proxy.eval() ;
results[i] = call_proxy.eval() ;
} else if( Rf_length(call) == 1 ){
boost::scoped_ptr<Gatherer> gather( constant_gatherer<DataFrame,LazySubsets>( call, nrows ) );
result = gather->collect() ;
results[i] = gather->collect() ;
} else if( Rf_isNull(call)) {
accumulator.rm(name) ;
continue ;
} else {
stop( "cannot handle" ) ;
}

check_supported_type(result, name) ;
check_supported_type(results[i], name) ;

if( Rf_inherits(result, "POSIXlt") ){
if( Rf_inherits(results[i], "POSIXlt") ){
stop("`mutate` does not support `POSIXlt` results");
}
int n_res = Rf_length(result) ;
int n_res = Rf_length(results[i]) ;
if( n_res == nrows ){
// ok
} else if( n_res == 1 && nrows != 0 ){
// recycle
boost::scoped_ptr<Gatherer> gather( constant_gatherer<DataFrame,LazySubsets>( result, df.nrows() ) );
result = gather->collect() ;
boost::scoped_ptr<Gatherer> gather( constant_gatherer<DataFrame,LazySubsets>( results[i] , df.nrows() ) );
results[i] = gather->collect() ;
} else {
stop( "wrong result size (%d), expected %d or 1", n_res, nrows ) ;
}

call_proxy.input( name, result ) ;
accumulator.set( name, result );
call_proxy.input( name, results[i] ) ;
accumulator.set( name, results[i] );
}
List res = structure_mutate(accumulator, df, classes_not_grouped() ) ;

Expand Down

2 comments on commit 664985d

@kevinushey
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this imply that the RObject class is not properly protecting its underlying object?

@romainfrancois
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, otherwise pretty much all the Rcpp empire would collapse right.

The problem was I need protection for longer than the scope of the RObject I used, i.e. I need the protection to be still in place after the loop, for this call:

List res = structure_mutate(accumulator, df, classes_not_grouped() ) ;

Please sign in to comment.