-
Notifications
You must be signed in to change notification settings - Fork 28.1k
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
[SPARK-13327][SPARKR] Added parameter validations for colnames<- #11220
Conversation
Test build #51373 has finished for PR 11220 at commit
|
@@ -303,8 +303,28 @@ setMethod("colnames", | |||
#' @rdname columns | |||
#' @name colnames<- | |||
setMethod("colnames<-", | |||
signature(x = "DataFrame", value = "character"), |
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.
I would prefer keeping the current signature instead of checking the class of the parameter inside the method.
@felixcheung, what's your opinion?
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.
I agree - letting R do type matching for the method signature seems like a better approach?
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.
The reason I added that check was because of this:
colnames(irisDF) <- 1
Error incolnames<-
(*tmp*
, value = 1) :
'dimnames' applied to non-array
After I ran that, I saw this:
showMethods("colnames<-")
Function: colnames<- (package SparkR)
x="ANY", value="ANY"
x="DataFrame", value="character"
x="DataFrame", value="numeric"
(inherited from: x="ANY", value="ANY")
So looks like R automatically adds definitions of colnames<- if value is other than character.
This does not happen with coltypes<-, as it's not part of base package and doesn't have an (ANY,ANY) signature.
coltypes(irisDF) <- 1
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘coltypes<-’ for signature ‘"DataFrame", "numeric"’
Therefore, I believe we do need to do this data type check inside colnames<-.
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.
It is interesting. @felixcheung, do you know the reason behind this?
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.
I think in this case the base:: definition is implemented in such a way that it should (or at least it attempts to) handle different parameter types:
> getMethod("colnames<-")
Method Definition (Class "derivedDefaultMethod"):
function (x, value)
{
if (is.data.frame(x)) {
names(x) <- value
}
else {
dn <- dimnames(x)
if (is.null(dn)) {
if (is.null(value))
return(x)
if ((nd <- length(dim(x))) < 2L)
stop("attempt to set 'colnames' on an object with less than two dimensions")
dn <- vector("list", nd)
}
if (length(dn) < 2L)
stop("attempt to set 'colnames' on an object with less than two dimensions")
if (is.null(value))
dn[2L] <- list(NULL)
else dn[[2L]] <- value
dimnames(x) <- dn
}
x
}
<bytecode: 0x34c9058>
<environment: namespace:base>
Signatures:
x value
target "ANY" "ANY"
defined "ANY" "ANY"
So from what I can see the error you see actually comes from the base implement, as "sort of" expected. Though I'm ok if we are to add explicit checks to make the error more clear for the user.
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.
Thanks @felixcheung for investigating this further!
Jenkins, retest please |
Test build #51676 has finished for PR 11220 at commit
|
LGTM |
LGTM. Merging this. |
Author: Oscar D. Lara Yejas <odlaraye@oscars-mbp.attlocal.net> Author: Oscar D. Lara Yejas <odlaraye@oscars-mbp.usca.ibm.com> Closes #11220 from olarayej/SPARK-13312-3. (cherry picked from commit 416e71a) Signed-off-by: Shivaram Venkataraman <shivaram@cs.berkeley.edu>
Thanks, @shivaram! |
Author: Oscar D. Lara Yejas <odlaraye@oscars-mbp.attlocal.net> Author: Oscar D. Lara Yejas <odlaraye@oscars-mbp.usca.ibm.com> Closes apache#11220 from olarayej/SPARK-13312-3.
No description provided.