Github user sun-rui commented on a diff in the pull request:
https://github.com/apache/spark/pull/8276#discussion_r37490835
--- Diff: R/pkg/R/DataFrame.R ---
@@ -628,18 +628,49 @@ setMethod("dim",
setMethod("collect",
signature(x = "DataFrame"),
function(x, stringsAsFactors = FALSE) {
- # listCols is a list of raw vectors, one per column
- listCols <- callJStatic("org.apache.spark.sql.api.r.SQLUtils",
"dfToCols", x@sdf)
- cols <- lapply(listCols, function(col) {
- objRaw <- rawConnection(col)
- numRows <- readInt(objRaw)
- col <- readCol(objRaw, numRows)
- close(objRaw)
- col
- })
- names(cols) <- columns(x)
- do.call(cbind.data.frame, list(cols, stringsAsFactors =
stringsAsFactors))
- })
+ names <- columns(x)
+ ncol <- length(names)
+ if (ncol <= 0) {
+ # empty data.frame with 0 columns and 0 rows
+ data.frame()
+ } else {
+ # listCols is a list of columns
+ listCols <-
callJStatic("org.apache.spark.sql.api.r.SQLUtils", "dfToCols", x@sdf)
+ stopifnot(length(listCols) == ncol)
+
+ # An empty data.frame with 0 columns and number of rows as
collected
+ nrow <- length(listCols[[1]])
+ if (nrow <= 0) {
+ df <- data.frame()
+ } else {
+ df <- data.frame(row.names = c(1 : nrow))
+ }
+
+ # Append columns one by one
+ for (colIndex in 1 : ncol) {
+ # Note: appending a column of list type into a data.frame
so that
+ # data of complex type can be held. But getting a cell
from a column
+ # of list type returns a list instead of a vector. So for
columns of
+ # non-complex type, append them as vector.
+ col <- listCols[[colIndex]]
+ if (length(col) <= 0) {
+ df[[names[colIndex]]] <- col
--- End diff --
This is special handling for empty columns where the DataFrame has >=1
column but nrow = 0. If a column is empty (list()), do.call(c, list()) will
return NULL.
Line 644 is for the case where DataFrame with 0 column and 0 row.
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]