-
Notifications
You must be signed in to change notification settings - Fork 995
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
Fixing bug on assigning with 0 row matches #2860
Changes from 2 commits
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 |
---|---|---|
|
@@ -277,7 +277,7 @@ SEXP assign(SEXP dt, SEXP rows, SEXP cols, SEXP newcolnames, SEXP values, SEXP v | |
// newcolnames : add these columns (if any) | ||
// cols : column names or numbers corresponding to the values to set | ||
// rows : row numbers to assign | ||
R_len_t i, j, nrow, targetlen, vlen, r, oldncol, oldtncol, coln, protecti=0, newcolnum, indexLength; | ||
R_len_t i, j, nrow, numToDo, targetlen, vlen, r, oldncol, oldtncol, coln, protecti=0, newcolnum, indexLength; | ||
SEXP targetcol, RHS, names, nullint, thisvalue, thisv, targetlevels, newcol, s, colnam, class, tmp, colorder, key, index, a, assignedNames, indexNames; | ||
SEXP bindingIsLocked = getAttrib(dt, install(".data.table.locked")); | ||
Rboolean verbose = LOGICAL(verb)[0], anytodelete=FALSE, isDataTable=FALSE; | ||
|
@@ -316,6 +316,7 @@ SEXP assign(SEXP dt, SEXP rows, SEXP cols, SEXP newcolnames, SEXP values, SEXP v | |
if (oldncol<1) error("Cannot use := to add columns to a null data.table (no columns), currently. You can use := to add (empty) columns to a 0-row data.table (1 or more empty columns), though."); | ||
nrow = length(VECTOR_ELT(dt,0)); | ||
if (isNull(rows)) { | ||
numToDo = nrow; | ||
targetlen = nrow; | ||
if (verbose) Rprintf("Assigning to all %d rows\n", nrow); | ||
// fast way to assign to whole column, without creating 1:nrow(x) vector up in R, or here in C | ||
|
@@ -328,7 +329,7 @@ SEXP assign(SEXP dt, SEXP rows, SEXP cols, SEXP newcolnames, SEXP values, SEXP v | |
if (!isInteger(rows)) | ||
error("i is type '%s'. Must be integer, or numeric is coerced with warning. If i is a logical subset, simply wrap with which(), and take the which() outside the loop if possible for efficiency.", type2char(TYPEOF(rows))); | ||
targetlen = length(rows); | ||
int numToDo = 0; | ||
numToDo = 0; | ||
for (i=0; i<targetlen; i++) { | ||
if ((INTEGER(rows)[i]<0 && INTEGER(rows)[i]!=NA_INTEGER) || INTEGER(rows)[i]>nrow) | ||
error("i[%d] is %d which is out of range [1,nrow=%d].",i+1,INTEGER(rows)[i],nrow); | ||
|
@@ -408,8 +409,10 @@ SEXP assign(SEXP dt, SEXP rows, SEXP cols, SEXP newcolnames, SEXP values, SEXP v | |
else colnam = STRING_ELT(newcolnames,coln-length(names)); | ||
if (coln+1 <= oldncol && isNull(thisvalue)) continue; // delete existing column(s) afterwards, near end of this function | ||
if (vlen<1 && nrow>0) { | ||
if (coln+1 <= oldncol) { | ||
error("RHS of assignment to existing column '%s' is zero length but not NULL. If you intend to delete the column use NULL. Otherwise, the RHS must have length > 0; e.g., NA_integer_. If you are trying to change the column type to be an empty list column then, as with all column type changes, provide a full length RHS vector such as vector('list',nrow(DT)); i.e., 'plonk' in the new column.", CHAR(STRING_ELT(names,coln))); | ||
if (coln+1 <= oldncol) { | ||
if(numToDo > 0){ //fixes #2829 | ||
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. It took me a while to grok numToDo, but yes. Looks good. Could it be one 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. It can be changed, but then the |
||
error("RHS of assignment to existing column '%s' is zero length but not NULL. If you intend to delete the column use NULL. Otherwise, the RHS must have length > 0; e.g., NA_integer_. If you are trying to change the column type to be an empty list column then, as with all column type changes, provide a full length RHS vector such as vector('list',nrow(DT)); i.e., 'plonk' in the new column.", CHAR(STRING_ELT(names,coln))); | ||
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. we use 2 spaces indention |
||
} | ||
} else if (TYPEOF(thisvalue)!=VECSXP) { // list() is ok for new columns | ||
newcolnum = coln-length(names); | ||
if (newcolnum<0 || newcolnum>=length(newcolnames)) | ||
|
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.
How about this to reflect the nuance : "Empty RHS of
:=
is no longer an error when thei
clause returns no rows to assign to anyway, [#2829]..."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.
Perfect, thanks! I have changed it in my latest commit.