Skip to content

Commit

Permalink
[SPARK-20544][SPARKR] R wrapper for input_file_name
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?

Adds wrapper for `o.a.s.sql.functions.input_file_name`

## How was this patch tested?

Existing unit tests, additional unit tests, `check-cran.sh`.

Author: zero323 <zero323@users.noreply.github.com>

Closes #17818 from zero323/SPARK-20544.
  • Loading branch information
zero323 authored and Felix Cheung committed May 4, 2017
1 parent 9c36aa2 commit f21897f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions R/pkg/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ exportMethods("%<=>%",
"hypot",
"ifelse",
"initcap",
"input_file_name",
"instr",
"isNaN",
"isNotNull",
Expand Down
21 changes: 21 additions & 0 deletions R/pkg/R/functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -3975,3 +3975,24 @@ setMethod("grouping_id",
jc <- callJStatic("org.apache.spark.sql.functions", "grouping_id", jcols)
column(jc)
})

#' input_file_name
#'
#' Creates a string column with the input file name for a given row
#'
#' @rdname input_file_name
#' @name input_file_name
#' @family normal_funcs
#' @aliases input_file_name,missing-method
#' @export
#' @examples \dontrun{
#' df <- read.text("README.md")
#'
#' head(select(df, input_file_name()))
#' }
#' @note input_file_name since 2.3.0
setMethod("input_file_name", signature("missing"),
function() {
jc <- callJStatic("org.apache.spark.sql.functions", "input_file_name")
column(jc)
})
6 changes: 6 additions & 0 deletions R/pkg/R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,12 @@ setGeneric("hypot", function(y, x) { standardGeneric("hypot") })
#' @export
setGeneric("initcap", function(x) { standardGeneric("initcap") })

#' @param x empty. Should be used with no argument.
#' @rdname input_file_name
#' @export
setGeneric("input_file_name",
function(x = "missing") { standardGeneric("input_file_name") })

#' @rdname instr
#' @export
setGeneric("instr", function(y, x) { standardGeneric("instr") })
Expand Down
5 changes: 5 additions & 0 deletions R/pkg/inst/tests/testthat/test_sparkSQL.R
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,11 @@ test_that("column functions", {
expect_equal(collect(df2)[[3, 1]], FALSE)
expect_equal(collect(df2)[[3, 2]], TRUE)

# Test that input_file_name()
actual_names <- sort(collect(distinct(select(df, input_file_name()))))
expect_equal(length(actual_names), 1)
expect_equal(basename(actual_names[1, 1]), basename(jsonPath))

df3 <- select(df, between(df$name, c("Apache", "Spark")))
expect_equal(collect(df3)[[1, 1]], TRUE)
expect_equal(collect(df3)[[2, 1]], FALSE)
Expand Down

0 comments on commit f21897f

Please sign in to comment.