Github user felixcheung commented on a diff in the pull request:

    https://github.com/apache/spark/pull/9012#discussion_r42199616
  
    --- Diff: R/pkg/R/DataFrame.R ---
    @@ -1452,15 +1452,147 @@ setMethod("join",
                 dataFrame(sdf)
               })
     
    -#' @rdname merge
    +#'
     #' @name merge
     #' @aliases join
    +#' @title Merges two data frames
    +#' @param x the first data frame to be joined
    +#' @param y the second data frame to be joined
    +#' @param by a character vector specifying the join columns. If by is not
    +#'   specified, the common column names in \code{x} and \code{y} will be 
used.
    +#' @param by.x a character vector specifying the joining columns for x.
    +#' @param by.y a character vector specifying the joining columns for y.
    +#' @param all.x a boolean value indicating whether all the rows in x should
    +#'              be including in the join
    +#' @param all.y a boolean value indicating whether all the rows in y should
    +#'              be including in the join
    +#' @param sort a logical argument indicating whether the resulting columns 
should be sorted
    +#' @details  If all.x and all.y are set to FALSE, a natural join will be 
returned. If
    +#'   all.x is set to TRUE and all.y is set to FALSE, a left outer join will
    +#'   be returned. If all.x is set to FALSE and all.y is set to TRUE, a 
right
    +#'   outer join will be returned. If all.x and all.y are set to TRUE, a 
full
    +#'   outer join will be returned.
    +#' @rdname merge
    +#' @export
    +#' @examples
    +#'\dontrun{
    +#' sc <- sparkR.init()
    +#' sqlContext <- sparkRSQL.init(sc)
    +#' df1 <- jsonFile(sqlContext, path)
    +#' df2 <- jsonFile(sqlContext, path2)
    +#' merge(df1, df2) # Performs a Cartesian
    +#' merge(df1, df2, by = "col1") # Performs an inner join based on 
expression
    +#' merge(df1, df2, by.x = "col1", by.y = "col2", all.y = TRUE)
    +#' merge(df1, df2, by.x = "col1", by.y = "col2", all.x = TRUE)
    +#' merge(df1, df2, by.x = "col1", by.y = "col2", all.x = TRUE, all.y = 
TRUE)
    +#' }
     setMethod("merge",
               signature(x = "DataFrame", y = "DataFrame"),
    -          function(x, y, joinExpr = NULL, joinType = NULL, ...) {
    -            join(x, y, joinExpr, joinType)
    -          })
    +          function(x, y, by = intersect(names(x), names(y)), by.x = NULL, 
by.y = NULL,
    +                   all = FALSE, all.x = FALSE, all.y = FALSE,
    +                   sort = TRUE, suffixes = c("_x","_y"), ... ) {
    +
    +            if (missing(x) | missing(y)) {
    +              stop("x and y has to be specified")
    +            }
    +
    +            if (length(suffixes) != 2) {
    +              stop("suffixes must have length 2")
    +            }
    +
    +            # join type is identified based on the values of all, all.x 
and all.y
    +            # default join type is inner, according to R it should be 
natural but since it
    +            # is not supported in spark inner join is used
    +            joinType <- "inner"
    +            if (all | (all.x & all.y)) {
    +              joinType <- "outer"
    +            } else if (all.x) {
    +              joinType <- "left_outer"
    +            } else if (all.y) {
    +              joinType <- "right_outer"
    +            }
    +
    +            # join expression is based on by.x, by.y if both by.x and by.y 
are not missing
    +            # and by if by.x or by.y are missing or have different lengths
    +            if (length(by.x) > 0 & length(by.x) == length(by.y)) {
    +              joinX <- by.x
    +              joinY <- by.y
    +            } else if (length(by) > 0) {
    +              joinX <- by
    +              joinY <- by
    +            } else {
    +              stop("The intersection of dataframes is empty")
    +            }
    +
    +            # sets alias for making colnames unique in dataframes 'x' and 
'y'
    +            colsX <- generateAliasesForIntersectedCols(x, names(x), by, 
suffixes[1])
    +            colsY <- generateAliasesForIntersectedCols(y, names(y), by, 
suffixes[2])
    +
    +            # selects columns with their aliases from dataframes
    +            # in case same column names are present in both data frames
    +            xsel <- select(x, colsX)
    +            ysel <- select(y, colsY)
    +
    +            joinColumns <- lapply(seq_len(length(joinX)), function(i) {
    +              colX <- joinX[[i]]
    +              colY <- joinY[[i]]
     
    +              if (colX %in% by) {
    +                colX <- paste(colX, suffixes[1], sep = "")
    +              }
    +              if (colY %in% by) {
    +                colY <- paste(colY, suffixes[2], sep = "")
    +              }
    +
    +              colX <- getColumn(xsel, colX)
    +              colY <- getColumn(ysel, colY)
    +
    +              colX == colY
    +            })
    +
    +            # concatanates join columns together with '&'
    +            for (i in 1:length(joinColumns)) {
    +              if (i == 1) {
    +                joinExpr <- joinColumns[[i]]
    +              } else {
    +                joinExpr <- joinExpr & joinColumns[[i]]
    +              }
    +            }
    +
    +            joinRes <- join(xsel, ysel, joinExpr, joinType)
    +
    +            # sorts the result by 'by' columns if sort = TRUE
    +            if (sort & length(by) > 0) {
    +              colNameWithSuffix <- paste(by, suffixes[2], sep = "")
    +              joinRes <- do.call("arrange", c(joinRes, colNameWithSuffix, 
decreasing = FALSE))
    +            }
    +
    +            joinRes
    +          })
    +
    +#' 
    +#' Creates a list of columns by replacing the intersected ones with 
aliases.
    +#' The name of the alias column is formed by concatanating the original 
column name and a suffix.
    +#'
    +#' @param x a DataFrame on which the
    +#' @param allColNames a list of column names
    +#' @param intersectedColNames a list of intersected column names
    +#' @param suffix a suffix for the column name
    +#' @return list of columns
    +#'
    +generateAliasesForIntersectedCols <- function (x, allColNames, 
intersectedColNames, suffix) {
    +  # sets alias for making colnames unique in dataframe 'x'
    +  cols <- lapply(seq_len(length(allColNames)), function(i) {
    --- End diff --
    
    I'm a bit confused - why create a seq of indices? instead can't we use 
lapply on allColNames?
    
    Also, nit, why pass allColNames if we could just do `names(x)` here?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to