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

ARROW-3439: [R] R language bindings for Feather format #2947

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions r/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Collate:
'array.R'
'buffer.R'
'dictionary.R'
'feather.R'
'io.R'
'memory_pool.R'
'message.R'
Expand Down
18 changes: 18 additions & 0 deletions r/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ S3method(buffer,numeric)
S3method(buffer,raw)
S3method(buffer_reader,"arrow::Buffer")
S3method(buffer_reader,default)
S3method(feather_table_reader,"arrow::io::RandomAccessFile")
S3method(feather_table_reader,"arrow::ipc::feather::TableReader")
S3method(feather_table_reader,character)
S3method(feather_table_reader,default)
S3method(feather_table_reader,fs_path)
S3method(feather_table_writer,"arrow::io::OutputStream")
S3method(fixed_size_buffer_writer,"arrow::Buffer")
S3method(fixed_size_buffer_writer,default)
S3method(length,"arrow::Array")
Expand Down Expand Up @@ -51,6 +57,13 @@ S3method(record_batch_stream_reader,raw)
S3method(write_arrow,"arrow::RecordBatch")
S3method(write_arrow,"arrow::Table")
S3method(write_arrow,data.frame)
S3method(write_feather,"arrow::RecordBatch")
S3method(write_feather,data.frame)
S3method(write_feather,default)
S3method(write_feather_RecordBatch,"arrow::io::OutputStream")
S3method(write_feather_RecordBatch,character)
S3method(write_feather_RecordBatch,default)
S3method(write_feather_RecordBatch,fs_path)
S3method(write_record_batch,"arrow::io::OutputStream")
S3method(write_record_batch,"arrow::ipc::RecordBatchWriter")
S3method(write_record_batch,character)
Expand Down Expand Up @@ -78,6 +91,8 @@ export(date32)
export(date64)
export(decimal)
export(dictionary)
export(feather_table_reader)
export(feather_table_writer)
export(file_open)
export(file_output_stream)
export(fixed_size_buffer_writer)
Expand All @@ -96,6 +111,7 @@ export(mock_output_stream)
export(null)
export(print.integer64)
export(read_arrow)
export(read_feather)
export(read_message)
export(read_record_batch)
export(read_schema)
Expand All @@ -118,6 +134,8 @@ export(uint64)
export(uint8)
export(utf8)
export(write_arrow)
export(write_feather)
export(write_feather_RecordBatch)
export(write_record_batch)
export(write_table)
importFrom(R6,R6Class)
Expand Down
60 changes: 60 additions & 0 deletions r/R/RcppExports.R

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

160 changes: 160 additions & 0 deletions r/R/feather.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

#' @include R6.R

`arrow::ipc::feather::TableWriter` <- R6Class("arrow::ipc::feather::TableWriter", inherit = `arrow::Object`,
public = list(
SetDescription = function(description) ipc___feather___TableWriter__SetDescription(self, description),
SetNumRows = function(num_rows) ipc___feather___TableWriter__SetNumRows(self, num_rows),
Append = function(name, values) ipc___feather___TableWriter__Append(self, name, values),
Finalize = function() ipc___feather___TableWriter__Finalize(self)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a rationale for using 3 underscores in these methods like this (instead of 1 or 2)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of:

  • 3 underscores is for namespaces, so ipc:: becomes ipc___
  • 2 underscores is for methods of a class, so TableWriter.Finalize becomes TableWriter__Finalize
  • 1 underscore is sometimes used in the functions themselves

As of now, this is all generated manually, but maybe some day I'll sit down and have a way to generate these things (the R6 class, and the C++ functions) from a simple format. Something like what RcppR6 does, although IIRC we would need additional tooling for things like shared_ptr or unique_ptr, ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, is this an internal convention or an Rcpp thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not. There are no convention, and also no way (IIRC) to export a function that lives in a namespace. That's just something semi recognizable and easier to generate automatically if at some point we go that way (à la RcppR6)

)
)

`arrow::ipc::feather::TableReader` <- R6Class("arrow::ipc::feather::TableReader", inherit = `arrow::Object`,
public = list(
GetDescription = function() ipc___feather___TableReader__GetDescription(self),
HasDescription = function() ipc__feather___TableReader__HasDescription(self),
version = function() ipc___feather___TableReader__version(self),
num_rows = function() ipc___feather___TableReader__num_rows(self),
num_columns = function() ipc___feather___TableReader__num_columns(self),
GetColumnName = function(i) ipc___feather___TableReader__GetColumnName(self, i),
GetColumn = function(i) shared_ptr(`arrow::Column`, ipc___feather___TableReader__GetColumn(self, i)),
Read = function() shared_ptr(`arrow::Table`, ipc___feather___TableReader__Read(self))
)
)

#' Create TableWriter that writes into a stream
#'
#' @param stream an OutputStream
#'
#' @export
feather_table_writer <- function(stream) {
UseMethod("feather_table_writer")
}

#' @export
`feather_table_writer.arrow::io::OutputStream` <- function(stream){
unique_ptr(`arrow::ipc::feather::TableWriter`, ipc___feather___TableWriter__Open(stream))
}

#' Write data in the feather format
#'
#' @param data frame or arrow::RecordBatch
#' @param stream A file path or an arrow::io::OutputStream
#'
#' @export
write_feather <- function(data, stream) {
UseMethod("write_feather", data)
}

#' @export
write_feather.default <- function(data, stream) {
stop("unsupported")
}

#' @export
write_feather.data.frame <- function(data, stream) {
write_feather(record_batch(data), stream)
}

#' @method write_feather arrow::RecordBatch
#' @export
`write_feather.arrow::RecordBatch` <- function(data, stream) {
write_feather_RecordBatch(data, stream)
}

#' @rdname write_feather
#' @export
write_feather_RecordBatch <- function(data, stream) {
UseMethod("write_feather_RecordBatch", stream)
}

#' @export
#' @method write_feather_RecordBatch default
`write_feather_RecordBatch.default` <- function(data, stream) {
stop("unsupported")
}

#' @export
#' @method write_feather_RecordBatch character
`write_feather_RecordBatch.character` <- function(data, stream) {
`write_feather_RecordBatch.fs_path`(data, fs::path_abs(stream))
}

#' @export
#' @method write_feather_RecordBatch fs_path
`write_feather_RecordBatch.fs_path` <- function(data, stream) {
file_stream <- close_on_exit(file_output_stream(stream))
`write_feather_RecordBatch.arrow::io::OutputStream`(data, file_stream)
}

#' @export
#' @method write_feather_RecordBatch arrow::io::OutputStream
`write_feather_RecordBatch.arrow::io::OutputStream` <- function(data, stream) {
ipc___TableWriter__RecordBatch__WriteFeather(feather_table_writer(stream), data)
}

#' A arrow::ipc::feather::TableReader to read from a file
#'
#' @param file A file path, arrow::io::RandomAccessFile
#' @param mmap Is the file memory mapped (applicable to the character and fs_path methods)
#' @param ... extra parameters
#'
#' @export
feather_table_reader <- function(file, mmap = TRUE, ...){
UseMethod("feather_table_reader")
}

#' @export
feather_table_reader.default <- function(file, mmap = TRUE, ...) {
stop("unsupported")
}

#' @export
feather_table_reader.character <- function(file, mmap = TRUE, ...) {
feather_table_reader(fs::path_abs(file), mmap = mmap, ...)
}

#' @export
feather_table_reader.fs_path <- function(file, mmap = TRUE, ...) {
stream <- if(isTRUE(mmap)) mmap_open(file, ...) else file_open(file, ...)
feather_table_reader(stream)
}

#' @export
`feather_table_reader.arrow::io::RandomAccessFile` <- function(file, mmap = TRUE, ...){
unique_ptr(`arrow::ipc::feather::TableReader`, ipc___feather___TableReader__Open(file))
}

#' @export
`feather_table_reader.arrow::ipc::feather::TableReader` <- function(file, mmap = TRUE, ...){
file
}

#' Read a feather file
#'
#' @param file a arrow::ipc::feather::TableReader or whatever the [feather_table_reader()] function can handle
#' @param ... additional parameters
#'
#' @return an arrow::Table
#'
#' @export
read_feather <- function(file, ...){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It occurs to me that the semantics of this function are different from feather::read_feather (it returns an arrow::Table instead of data.frame), if we care

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO it would be a nice incentive for people to abandon the feather package in favor of arrow if we could say arrow::read_feather() / arrow::write_feathe()r are drop-in replacements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the goals of arrow is to make Array structures palpable in R, which feather didn't do. But yeah I guess we need both, i.e.

  • a function that returns an arrow::Table, not sure how to call this one.
  • a function, perhaps read_feather if this need to look like feather::read_feather that returns a tibble by calling as_tibble on the result of the first one.

Right now, it makes sense to go straight to tibble, but soon enough this (or other) packages will have more tooling to manipulate and compute directly on arrow memory.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add an as_tibble = TRUE argument to read_feather so this can be toggled on and off

feather_table_reader(file, ...)$Read()
}
18 changes: 18 additions & 0 deletions r/man/feather_table_reader.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions r/man/feather_table_writer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions r/man/read_feather.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions r/man/write_feather.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading