Skip to content

Commit 488bbb2

Browse files
felixcheungshivaram
authored andcommitted
[SPARK-12232][SPARKR] New R API for read.table to avoid name conflict
shivaram sorry it took longer to fix some conflicts, this is the change to add an alias for `table` Author: felixcheung <[email protected]> Closes #10406 from felixcheung/readtable.
1 parent beda901 commit 488bbb2

File tree

5 files changed

+21
-27
lines changed

5 files changed

+21
-27
lines changed

R/pkg/NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ export("as.DataFrame",
280280
"read.text",
281281
"sql",
282282
"str",
283-
"table",
283+
"tableToDF",
284284
"tableNames",
285285
"tables",
286286
"uncacheTable")

R/pkg/R/SQLContext.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ sql <- function(sqlContext, sqlQuery) {
352352
#' @param sqlContext SQLContext to use
353353
#' @param tableName The SparkSQL Table to convert to a DataFrame.
354354
#' @return DataFrame
355+
#' @rdname tableToDF
356+
#' @name tableToDF
355357
#' @export
356358
#' @examples
357359
#'\dontrun{
@@ -360,15 +362,14 @@ sql <- function(sqlContext, sqlQuery) {
360362
#' path <- "path/to/file.json"
361363
#' df <- read.json(sqlContext, path)
362364
#' registerTempTable(df, "table")
363-
#' new_df <- table(sqlContext, "table")
365+
#' new_df <- tableToDF(sqlContext, "table")
364366
#' }
365367

366-
table <- function(sqlContext, tableName) {
368+
tableToDF <- function(sqlContext, tableName) {
367369
sdf <- callJMethod(sqlContext, "table", tableName)
368370
dataFrame(sdf)
369371
}
370372

371-
372373
#' Tables
373374
#'
374375
#' Returns a DataFrame containing names of tables in the given database.

R/pkg/inst/tests/testthat/test_context.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@ test_that("Check masked functions", {
2424
func <- lapply(masked, function(x) { capture.output(showMethods(x))[[1]] })
2525
funcSparkROrEmpty <- grepl("\\(package SparkR\\)$|^$", func)
2626
maskedBySparkR <- masked[funcSparkROrEmpty]
27-
expect_equal(length(maskedBySparkR), 18)
28-
expect_equal(sort(maskedBySparkR), sort(c("describe", "cov", "filter", "lag", "na.omit",
29-
"predict", "sd", "var", "colnames", "colnames<-",
30-
"intersect", "rank", "rbind", "sample", "subset",
31-
"summary", "table", "transform")))
27+
namesOfMasked <- c("describe", "cov", "filter", "lag", "na.omit", "predict", "sd", "var",
28+
"colnames", "colnames<-", "intersect", "rank", "rbind", "sample", "subset",
29+
"summary", "transform")
30+
expect_equal(length(maskedBySparkR), length(namesOfMasked))
31+
expect_equal(sort(maskedBySparkR), sort(namesOfMasked))
3232
# above are those reported as masked when `library(SparkR)`
3333
# note that many of these methods are still callable without base:: or stats:: prefix
3434
# there should be a test for each of these, except followings, which are currently "broken"
3535
funcHasAny <- unlist(lapply(masked, function(x) {
3636
any(grepl("=\"ANY\"", capture.output(showMethods(x)[-1])))
3737
}))
3838
maskedCompletely <- masked[!funcHasAny]
39-
expect_equal(length(maskedCompletely), 4)
40-
expect_equal(sort(maskedCompletely), sort(c("cov", "filter", "sample", "table")))
39+
namesOfMaskedCompletely <- c("cov", "filter", "sample")
40+
expect_equal(length(maskedCompletely), length(namesOfMaskedCompletely))
41+
expect_equal(sort(maskedCompletely), sort(namesOfMaskedCompletely))
4142
})
4243

4344
test_that("repeatedly starting and stopping SparkR", {

R/pkg/inst/tests/testthat/test_sparkSQL.R

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ writeLines(mockLinesMapType, mapTypeJsonPath)
335335
test_that("Collect DataFrame with complex types", {
336336
# ArrayType
337337
df <- read.json(sqlContext, complexTypeJsonPath)
338-
339338
ldf <- collect(df)
340339
expect_equal(nrow(ldf), 3)
341340
expect_equal(ncol(ldf), 3)
@@ -490,19 +489,15 @@ test_that("insertInto() on a registered table", {
490489
unlink(parquetPath2)
491490
})
492491

493-
test_that("table() returns a new DataFrame", {
492+
test_that("tableToDF() returns a new DataFrame", {
494493
df <- read.json(sqlContext, jsonPath)
495494
registerTempTable(df, "table1")
496-
tabledf <- table(sqlContext, "table1")
495+
tabledf <- tableToDF(sqlContext, "table1")
497496
expect_is(tabledf, "DataFrame")
498497
expect_equal(count(tabledf), 3)
498+
tabledf2 <- tableToDF(sqlContext, "table1")
499+
expect_equal(count(tabledf2), 3)
499500
dropTempTable(sqlContext, "table1")
500-
501-
# nolint start
502-
# Test base::table is working
503-
#a <- letters[1:3]
504-
#expect_equal(class(table(a, sample(a))), "table")
505-
# nolint end
506501
})
507502

508503
test_that("toRDD() returns an RRDD", {

docs/sparkr.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,6 @@ The following functions are masked by the SparkR package:
375375
<td><code>sample</code> in <code>package:base</code></td>
376376
<td><code>base::sample(x, size, replace = FALSE, prob = NULL)</code></td>
377377
</tr>
378-
<tr>
379-
<td><code>table</code> in <code>package:base</code></td>
380-
<td><code><pre>base::table(...,
381-
exclude = if (useNA == "no") c(NA, NaN),
382-
useNA = c("no", "ifany", "always"),
383-
dnn = list.names(...), deparse.level = 1)</pre></code></td>
384-
</tr>
385378
</table>
386379

387380
Since part of SparkR is modeled on the `dplyr` package, certain functions in SparkR share the same names with those in `dplyr`. Depending on the load order of the two packages, some functions from the package loaded first are masked by those in the package loaded after. In such case, prefix such calls with the package name, for instance, `SparkR::cume_dist(x)` or `dplyr::cume_dist(x)`.
@@ -394,3 +387,7 @@ You can inspect the search path in R with [`search()`](https://stat.ethz.ch/R-ma
394387
## Upgrading From SparkR 1.5.x to 1.6
395388

396389
- Before Spark 1.6, the default mode for writes was `append`. It was changed in Spark 1.6.0 to `error` to match the Scala API.
390+
391+
## Upgrading From SparkR 1.6.x to 2.0
392+
393+
- The method `table` has been removed and replaced by `tableToDF`.

0 commit comments

Comments
 (0)