Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

olarayej
Copy link

No description provided.

@SparkQA
Copy link

SparkQA commented Feb 16, 2016

Test build #51373 has finished for PR 11220 at commit e8c6415.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@@ -303,8 +303,28 @@ setMethod("colnames",
#' @rdname columns
#' @name colnames<-
setMethod("colnames<-",
signature(x = "DataFrame", value = "character"),
Copy link
Contributor

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?

Copy link
Member

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?

Copy link
Author

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 in colnames<-(*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<-.

Copy link
Contributor

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?

Copy link
Member

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.

Copy link
Author

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!

@olarayej
Copy link
Author

Jenkins, retest please

@SparkQA
Copy link

SparkQA commented Feb 22, 2016

Test build #51676 has finished for PR 11220 at commit 07e541b.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@sun-rui
Copy link
Contributor

sun-rui commented Mar 9, 2016

LGTM

@shivaram
Copy link
Contributor

LGTM. Merging this.

asfgit pushed a commit that referenced this pull request Mar 11, 2016
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>
@asfgit asfgit closed this in 416e71a Mar 11, 2016
@olarayej
Copy link
Author

Thanks, @shivaram!

roygao94 pushed a commit to roygao94/spark that referenced this pull request Mar 22, 2016
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants