diff --git a/r/R/arrowExports.R b/r/R/arrowExports.R index 5670fff187c..5d34f924470 100644 --- a/r/R/arrowExports.R +++ b/r/R/arrowExports.R @@ -212,8 +212,8 @@ ChunkedArray__View <- function(array, type){ .Call(`_arrow_ChunkedArray__View` , array, type) } -util___Codec__Create <- function(codec){ - .Call(`_arrow_util___Codec__Create` , codec) +util___Codec__Create <- function(codec, compression_level){ + .Call(`_arrow_util___Codec__Create` , codec, compression_level) } io___CompressedOutputStream__Make <- function(codec, raw){ diff --git a/r/R/compression.R b/r/R/compression.R index 2b999589d05..5fbe53e0c48 100644 --- a/r/R/compression.R +++ b/r/R/compression.R @@ -31,14 +31,17 @@ #' a list of possible values. `type` may be upper- or lower-cased. Support #' for compression methods depends on build-time flags for the C++ library. #' Most builds support at least "gzip" and "snappy". +#' * `compression_level`: compression level, the default value (`NA`) uses the default +#' compression level for the selected compression `type`. #' @rdname Codec #' @name Codec #' @export Codec <- R6Class("Codec", inherit = Object) -Codec$create <- function(type = "gzip") { +Codec$create <- function(type = "gzip", compression_level = NA) { if (is.character(type)) { type <- unique_ptr(Codec, util___Codec__Create( - CompressionType[[match.arg(toupper(type), names(CompressionType))]] + CompressionType[[match.arg(toupper(type), names(CompressionType))]], + compression_level )) } assert_is(type, "Codec") @@ -62,7 +65,8 @@ Codec$create <- function(type = "gzip") { #' factory methods instantiate the object and take the following arguments: #' #' - `stream` An [InputStream] or [OutputStream], respectively -#' - `codec` A `Codec` +#' - `codec` A `Codec`, either a [Codec][Codec] instance or a string +#' - `compression_level` compression level for when the `codec` argument is given as a string #' #' @section Methods: #' @@ -70,8 +74,8 @@ Codec$create <- function(type = "gzip") { #' @export #' @include arrow-package.R CompressedOutputStream <- R6Class("CompressedOutputStream", inherit = OutputStream) -CompressedOutputStream$create <- function(stream, codec = "gzip"){ - codec <- Codec$create(codec) +CompressedOutputStream$create <- function(stream, codec = "gzip", compression_level = NA){ + codec <- Codec$create(codec, compression_level = compression_level) if (is.character(stream)) { stream <- FileOutputStream$create(stream) } @@ -84,8 +88,8 @@ CompressedOutputStream$create <- function(stream, codec = "gzip"){ #' @format NULL #' @export CompressedInputStream <- R6Class("CompressedInputStream", inherit = InputStream) -CompressedInputStream$create <- function(stream, codec = "gzip"){ - codec <- Codec$create(codec) +CompressedInputStream$create <- function(stream, codec = "gzip", compression_level = NA){ + codec <- Codec$create(codec, compression_level = compression_level) if (is.character(stream)) { stream <- ReadableFile$create(stream) } diff --git a/r/man/Codec.Rd b/r/man/Codec.Rd index 548c6b89c15..6619a64f51f 100644 --- a/r/man/Codec.Rd +++ b/r/man/Codec.Rd @@ -15,6 +15,8 @@ The \code{Codec$create()} factory method takes the following argument: a list of possible values. \code{type} may be upper- or lower-cased. Support for compression methods depends on build-time flags for the C++ library. Most builds support at least "gzip" and "snappy". +\item \code{compression_level}: compression level, the default value (\code{NA}) uses the default +compression level for the selected compression \code{type}. } } diff --git a/r/man/compression.Rd b/r/man/compression.Rd index 16130f8321e..f6a69201aa7 100644 --- a/r/man/compression.Rd +++ b/r/man/compression.Rd @@ -18,7 +18,8 @@ The \code{CompressedInputStream$create()} and \code{CompressedOutputStream$creat factory methods instantiate the object and take the following arguments: \itemize{ \item \code{stream} An \link{InputStream} or \link{OutputStream}, respectively -\item \code{codec} A \code{Codec} +\item \code{codec} A \code{Codec}, either a \link{Codec} instance or a string +\item \code{compression_level} compression level for when the \code{codec} argument is given as a string } } diff --git a/r/src/arrowExports.cpp b/r/src/arrowExports.cpp index f72ff256fe5..009d53b0be3 100644 --- a/r/src/arrowExports.cpp +++ b/r/src/arrowExports.cpp @@ -827,15 +827,16 @@ RcppExport SEXP _arrow_ChunkedArray__View(SEXP array_sexp, SEXP type_sexp){ // compression.cpp #if defined(ARROW_R_WITH_ARROW) -std::unique_ptr util___Codec__Create(arrow::Compression::type codec); -RcppExport SEXP _arrow_util___Codec__Create(SEXP codec_sexp){ +std::unique_ptr util___Codec__Create(arrow::Compression::type codec, int compression_level); +RcppExport SEXP _arrow_util___Codec__Create(SEXP codec_sexp, SEXP compression_level_sexp){ BEGIN_RCPP Rcpp::traits::input_parameter::type codec(codec_sexp); - return Rcpp::wrap(util___Codec__Create(codec)); + Rcpp::traits::input_parameter::type compression_level(compression_level_sexp); + return Rcpp::wrap(util___Codec__Create(codec, compression_level)); END_RCPP } #else -RcppExport SEXP _arrow_util___Codec__Create(SEXP codec_sexp){ +RcppExport SEXP _arrow_util___Codec__Create(SEXP codec_sexp, SEXP compression_level_sexp){ Rf_error("Cannot call util___Codec__Create(). Please use arrow::install_arrow() to install required runtime libraries. "); } #endif @@ -4253,7 +4254,7 @@ static const R_CallMethodDef CallEntries[] = { { "_arrow_ChunkArray__Slice1", (DL_FUNC) &_arrow_ChunkArray__Slice1, 2}, { "_arrow_ChunkArray__Slice2", (DL_FUNC) &_arrow_ChunkArray__Slice2, 3}, { "_arrow_ChunkedArray__View", (DL_FUNC) &_arrow_ChunkedArray__View, 2}, - { "_arrow_util___Codec__Create", (DL_FUNC) &_arrow_util___Codec__Create, 1}, + { "_arrow_util___Codec__Create", (DL_FUNC) &_arrow_util___Codec__Create, 2}, { "_arrow_io___CompressedOutputStream__Make", (DL_FUNC) &_arrow_io___CompressedOutputStream__Make, 2}, { "_arrow_io___CompressedInputStream__Make", (DL_FUNC) &_arrow_io___CompressedInputStream__Make, 2}, { "_arrow_compute___CastOptions__initialize", (DL_FUNC) &_arrow_compute___CastOptions__initialize, 3}, diff --git a/r/src/compression.cpp b/r/src/compression.cpp index 0a4a6d3aede..4f9bc1772f4 100644 --- a/r/src/compression.cpp +++ b/r/src/compression.cpp @@ -20,9 +20,10 @@ #if defined(ARROW_R_WITH_ARROW) // [[arrow::export]] -std::unique_ptr util___Codec__Create(arrow::Compression::type codec) { +std::unique_ptr util___Codec__Create(arrow::Compression::type codec, + int compression_level) { std::unique_ptr out; - STOP_IF_NOT_OK(arrow::util::Codec::Create(codec, &out)); + STOP_IF_NOT_OK(arrow::util::Codec::Create(codec, compression_level, &out)); return out; }