1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
|
# 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.
#' Dataset file formats
#'
#' @description
#' A `FileFormat` holds information about how to read and parse the files
#' included in a `Dataset`. There are subclasses corresponding to the supported
#' file formats (`ParquetFileFormat` and `IpcFileFormat`).
#'
#' @section Factory:
#' `FileFormat$create()` takes the following arguments:
#' * `format`: A string identifier of the file format. Currently supported values:
#' * "parquet"
#' * "ipc"/"arrow"/"feather", all aliases for each other; for Feather, note that
#' only version 2 files are supported
#' * "csv"/"text", aliases for the same thing (because comma is the default
#' delimiter for text files
#' * "tsv", equivalent to passing `format = "text", delimiter = "\t"`
#' * `...`: Additional format-specific options
#'
#' `format = "parquet"`:
#' * `dict_columns`: Names of columns which should be read as dictionaries.
#' * Any Parquet options from [FragmentScanOptions].
#'
#' `format = "text"`: see [CsvParseOptions]. Note that you can specify them either
#' with the Arrow C++ library naming ("delimiter", "quoting", etc.) or the
#' `readr`-style naming used in [read_csv_arrow()] ("delim", "quote", etc.).
#' Not all `readr` options are currently supported; please file an issue if
#' you encounter one that `arrow` should support. Also, the following options are
#' supported. From [CsvReadOptions]:
#' * `skip_rows`
#' * `column_names`. Note that if a [Schema] is specified, `column_names` must match those specified in the schema.
#' * `autogenerate_column_names`
#' From [CsvFragmentScanOptions] (these values can be overridden at scan time):
#' * `convert_options`: a [CsvConvertOptions]
#' * `block_size`
#'
#' It returns the appropriate subclass of `FileFormat` (e.g. `ParquetFileFormat`)
#' @rdname FileFormat
#' @name FileFormat
#' @examplesIf arrow_with_dataset()
#' ## Semi-colon delimited files
#' # Set up directory for examples
#' tf <- tempfile()
#' dir.create(tf)
#' on.exit(unlink(tf))
#' write.table(mtcars, file.path(tf, "file1.txt"), sep = ";", row.names = FALSE)
#'
#' # Create FileFormat object
#' format <- FileFormat$create(format = "text", delimiter = ";")
#'
#' open_dataset(tf, format = format)
#' @export
FileFormat <- R6Class(
"FileFormat",
inherit = ArrowObject,
active = list(
# @description
# Return the `FileFormat`'s type
type = function() dataset___FileFormat__type_name(self)
)
)
FileFormat$create <- function(format, schema = NULL, partitioning = NULL, ...) {
opt_names <- names(list(...))
if (format %in% c("csv", "text", "txt") || any(opt_names %in% c("delim", "delimiter"))) {
CsvFileFormat$create(schema = schema, partitioning = partitioning, ...)
} else if (format == "tsv") {
# This delimiter argument is ignored.
CsvFileFormat$create(delimiter = "\t", schema = schema, partitioning = partitioning, ...)
} else if (format == "parquet") {
ParquetFileFormat$create(...)
} else if (format %in% c("ipc", "arrow", "feather")) {
# These are aliases for the same thing
dataset___IpcFileFormat__Make()
} else if (format == "json") {
JsonFileFormat$create(...)
} else {
stop("Unsupported file format: ", format, call. = FALSE)
}
}
#' @export
as.character.FileFormat <- function(x, ...) {
out <- x$type
# Slight hack: special case IPC -> feather, otherwise is just the type_name
ifelse(out == "ipc", "feather", out)
}
#' @usage NULL
#' @format NULL
#' @rdname FileFormat
#' @export
ParquetFileFormat <- R6Class("ParquetFileFormat", inherit = FileFormat)
ParquetFileFormat$create <- function(..., dict_columns = character(0)) {
options <- ParquetFragmentScanOptions$create(...)
dataset___ParquetFileFormat__Make(options, dict_columns)
}
#' @usage NULL
#' @format NULL
#' @rdname FileFormat
#' @export
IpcFileFormat <- R6Class("IpcFileFormat", inherit = FileFormat)
#' JSON dataset file format
#'
#' @description
#' A `JsonFileFormat` is a [FileFormat] subclass which holds information about how to
#' read and parse the files included in a JSON `Dataset`.
#'
#' @section Factory:
#' `JsonFileFormat$create()` can take options in the form of lists passed through as `parse_options`,
#' or `read_options` parameters.
#'
#' Available `read_options` parameters:
#' * `use_threads`: Whether to use the global CPU thread pool. Default `TRUE`. If `FALSE`, JSON input must end with an
#' empty line.
#' * `block_size`: Block size we request from the IO layer; also determines size of chunks when `use_threads`
#' is `TRUE`.
#'
#' Available `parse_options` parameters:
#' * `newlines_in_values`:Logical: are values allowed to contain CR (`0x0d` or `\r`) and LF (`0x0a` or `\n`)
#' characters? (default `FALSE`)
#'
#' @return A `JsonFileFormat` object
#' @rdname JsonFileFormat
#' @name JsonFileFormat
#' @seealso [FileFormat]
#' @examplesIf arrow_with_dataset()
#'
#' @export
JsonFileFormat <- R6Class("JsonFileFormat", inherit = FileFormat)
JsonFileFormat$create <- function(...) {
dots <- list2(...)
parse_opt_choices <- dots[names(dots) %in% names(formals(JsonParseOptions$create))]
read_opt_choices <- dots[names(dots) %in% names(formals(JsonReadOptions$create))]
parse_options <- do.call(JsonParseOptions$create, parse_opt_choices)
read_options <- do.call(JsonReadOptions$create, read_opt_choices)
dataset___JsonFileFormat__Make(parse_options, read_options)
}
#' CSV dataset file format
#'
#' @description
#' A `CSVFileFormat` is a [FileFormat] subclass which holds information about how to
#' read and parse the files included in a CSV `Dataset`.
#'
#' @section Factory:
#' `CSVFileFormat$create()` can take options in the form of lists passed through as `parse_options`,
#' `read_options`, or `convert_options` parameters. Alternatively, readr-style options can be passed
#' through individually. While it is possible to pass in `CSVReadOptions`, `CSVConvertOptions`, and `CSVParseOptions`
#' objects, this is not recommended as options set in these objects are not validated for compatibility.
#'
#' @return A `CsvFileFormat` object
#' @rdname CsvFileFormat
#' @name CsvFileFormat
#' @seealso [FileFormat]
#' @examplesIf arrow_with_dataset()
#' # Set up directory for examples
#' tf <- tempfile()
#' dir.create(tf)
#' on.exit(unlink(tf))
#' df <- data.frame(x = c("1", "2", "NULL"))
#' write.table(df, file.path(tf, "file1.txt"), sep = ",", row.names = FALSE)
#'
#' # Create CsvFileFormat object with Arrow-style null_values option
#' format <- CsvFileFormat$create(convert_options = list(null_values = c("", "NA", "NULL")))
#' open_dataset(tf, format = format)
#'
#' # Use readr-style options
#' format <- CsvFileFormat$create(na = c("", "NA", "NULL"))
#' open_dataset(tf, format = format)
#'
#' @export
CsvFileFormat <- R6Class("CsvFileFormat", inherit = FileFormat)
CsvFileFormat$create <- function(..., partitioning = NULL) {
dots <- list(...)
options <- check_csv_file_format_args(dots, partitioning = partitioning)
check_schema(options[["schema"]], partitioning, options[["read_options"]]$column_names)
dataset___CsvFileFormat__Make(options$parse_options, options$convert_options, options$read_options)
}
# Check all arguments are valid
check_csv_file_format_args <- function(args, partitioning = NULL) {
options <- list(
parse_options = args$parse_options,
convert_options = args$convert_options,
read_options = args$read_options,
schema = args$schema
)
check_unsupported_args(args)
check_unrecognised_args(args)
# Evaluate parse_options first to catch any unsupported arguments
if (is.null(args$parse_options)) {
options$parse_options <- do.call(csv_file_format_parse_opts, args)
} else if (is.list(args$parse_options)) {
options$parse_options <- do.call(csv_parse_options, args$parse_options)
}
# Set up read_options before convert_options since convert_options needs column names
if (is.null(args$read_options)) {
options$read_options <- do.call(csv_file_format_read_opts, c(args, list(partitioning = partitioning)))
} else if (is.list(args$read_options)) {
options$read_options <- do.call(csv_read_options, args$read_options)
}
# If col_names is provided, add it to read_options
if ("col_names" %in% names(args)) {
args$read_options <- list(col_names = args$col_names)
}
if (is.null(args$convert_options)) {
options$convert_options <- do.call(csv_file_format_convert_opts, c(args, list(read_options = options$read_options)))
} else if (is.list(args$convert_options)) {
options$convert_options <- do.call(csv_convert_options, args$convert_options)
}
options
}
check_unsupported_args <- function(args) {
opt_names <- get_opt_names(args)
# Filter out arguments meant for CsvConvertOptions/CsvReadOptions
supported_convert_opts <- c(names(formals(csv_convert_options)), "na", "quoted_na")
supported_read_opts <- c(
names(formals(csv_read_options)),
names(formals(readr_to_csv_read_options))
)
# We only currently support all of the readr options for parseoptions
supported_parse_opts <- c(
names(formals(csv_parse_options)),
names(formals(readr_to_csv_parse_options))
)
# Catch any readr-style options specified with full option names that are
# supported by read_delim_arrow() (and its wrappers) but are not yet
# supported here
unsup_readr_opts <- setdiff(
names(formals(read_delim_arrow)),
c(supported_convert_opts, supported_read_opts, supported_parse_opts, "schema")
)
is_unsup_opt <- opt_names %in% unsup_readr_opts
unsup_opts <- opt_names[is_unsup_opt]
if (length(unsup_opts)) {
stop(
"The following ",
ngettext(length(unsup_opts), "option is ", "options are "),
"supported in \"read_delim_arrow\" functions ",
"but not yet supported here: ",
oxford_paste(unsup_opts),
call. = FALSE
)
}
}
# unlists "parse_options", "convert_options", "read_options" and returns them along with
# names of options passed in individually via args. `get_opt_names()` ignores any
# CSV*Options objects passed in as these are not validated - users must ensure they've
# chosen reasonable values in this case.
get_opt_names <- function(args) {
opt_names <- names(args)
# extract names of parse_options, read_options, and convert_options
if ("parse_options" %in% names(args) && is.list(args[["parse_options"]])) {
opt_names <- c(opt_names, names(args[["parse_options"]]))
}
if ("read_options" %in% names(args) && is.list(args[["read_options"]])) {
opt_names <- c(opt_names, names(args[["read_options"]]))
}
if ("convert_options" %in% names(args) && is.list(args[["convert_options"]])) {
opt_names <- c(opt_names, names(args[["convert_options"]]))
}
setdiff(opt_names, c("parse_options", "read_options", "convert_options"))
}
check_unrecognised_args <- function(opts) {
# Catch any options with full or partial names that do not match any of the
# recognized Arrow C++ option names or readr-style option names
opt_names <- get_opt_names(opts)
arrow_opts <- c(
names(formals(csv_parse_options)),
names(formals(csv_read_options)),
names(formals(csv_convert_options)),
"schema"
)
readr_opts <- c(
names(formals(readr_to_csv_parse_options)),
names(formals(readr_to_csv_read_options)),
"na",
"quoted_na"
)
is_arrow_opt <- !is.na(pmatch(opt_names, arrow_opts))
is_readr_opt <- !is.na(pmatch(opt_names, readr_opts))
unrec_opts <- opt_names[!is_arrow_opt & !is_readr_opt]
if (length(unrec_opts)) {
stop(
"Unrecognized ",
ngettext(length(unrec_opts), "option", "options"),
": ",
oxford_paste(unrec_opts),
call. = FALSE
)
}
}
check_ambiguous_options <- function(passed_opts, opts1, opts2) {
is_ambig_opt <- is.na(pmatch(passed_opts, c(opts1, opts2)))
ambig_opts <- passed_opts[is_ambig_opt]
if (length(ambig_opts)) {
stop(
"Ambiguous ",
ngettext(length(ambig_opts), "option", "options"),
": ",
oxford_paste(ambig_opts),
". Use full argument names",
call. = FALSE
)
}
}
check_schema <- function(schema, partitioning, column_names) {
if (!is.null(schema) && !inherits(schema, "Schema")) {
abort(paste0(
"`schema` must be an object of class 'Schema' not '",
class(schema)[1],
"'."
))
}
schema_names <- setdiff(names(schema), names(partitioning))
if (!is.null(schema) && !identical(schema_names, column_names)) {
missing_from_schema <- setdiff(column_names, schema_names)
missing_from_colnames <- setdiff(schema_names, column_names)
message_colnames <- NULL
message_schema <- NULL
message_order <- NULL
if (length(missing_from_colnames) > 0) {
message_colnames <- paste(
oxford_paste(missing_from_colnames, quote_symbol = "`"),
"not present in `column_names`"
)
}
if (length(missing_from_schema) > 0) {
message_schema <- paste(
oxford_paste(missing_from_schema, quote_symbol = "`"),
"not present in `schema`"
)
}
if (length(missing_from_schema) == 0 && length(missing_from_colnames) == 0) {
message_order <- "`column_names` and `schema` field names match but are not in the same order"
}
abort(
c(
"Values in `column_names` must match `schema` field names",
x = message_order,
x = message_schema,
x = message_colnames
)
)
}
}
# Support both readr-style option names and Arrow C++ option names
csv_file_format_parse_opts <- function(...) {
opts <- list(...)
# Filter out arguments meant for CsvConvertOptions/CsvReadOptions
convert_opts <- c(names(formals(csv_convert_options)), "na", "quoted_na", "convert_options")
read_opts <- c(
names(formals(csv_read_options)),
names(formals(readr_to_csv_read_options)),
"read_options"
)
opts[convert_opts] <- NULL
opts[read_opts] <- NULL
opts[["schema"]] <- NULL
opts[["parse_options"]] <- NULL
opt_names <- get_opt_names(opts)
arrow_opts <- c(names(formals(csv_parse_options)))
readr_opts <- c(names(formals(readr_to_csv_parse_options)))
is_arrow_opt <- !is.na(pmatch(opt_names, arrow_opts))
is_readr_opt <- !is.na(pmatch(opt_names, readr_opts))
# Catch options with ambiguous partial names (such as "del") that make it
# unclear whether the user is specifying Arrow C++ options ("delimiter") or
# readr-style options ("delim")
check_ambiguous_options(opt_names, arrow_opts, readr_opts)
if (any(is_readr_opt)) {
# Catch cases when the user specifies a mix of Arrow C++ options and
# readr-style options
if (!all(is_readr_opt)) {
stop("Use either Arrow parse options or readr parse options, not both", call. = FALSE)
}
do.call(readr_to_csv_parse_options, opts) # all options have readr-style names
} else {
do.call(csv_parse_options, opts) # all options have Arrow C++ names
}
}
csv_file_format_convert_opts <- function(...) {
opts <- list(...)
# Filter out arguments meant for CsvParseOptions/CsvReadOptions
arrow_opts <- c(names(formals(csv_parse_options)), "parse_options")
readr_opts <- names(formals(readr_to_csv_parse_options))
read_opts <- c(
names(formals(csv_read_options)),
names(formals(readr_to_csv_read_options)),
"read_options"
)
opts[arrow_opts] <- NULL
opts[readr_opts] <- NULL
opts[read_opts] <- NULL
opts[["schema"]] <- NULL
opts[["convert_options"]] <- NULL
# map "na" to "null_values"
if ("na" %in% names(opts)) {
opts[["null_values"]] <- opts[["na"]]
opts[["na"]] <- NULL
}
if ("quoted_na" %in% names(opts)) {
opts[["strings_can_be_null"]] <- opts[["quoted_na"]]
opts[["quoted_na"]] <- NULL
}
# Handle readr-style col_types specification
if ("col_types" %in% names(opts) && is.character(opts[["col_types"]])) {
# Get column names from read_options if available
col_names <- if (!is.null(opts[["read_options"]])) {
if (!is.null(opts[["read_options"]]$column_names)) {
opts[["read_options"]]$column_names
} else if (!is.null(opts[["read_options"]]$col_names)) {
opts[["read_options"]]$col_names
} else {
abort("Compact specification for `col_types` requires column names in read_options")
}
} else if ("col_names" %in% names(opts)) {
opts[["col_names"]]
} else {
abort("Compact specification for `col_types` requires column names")
}
opts[["col_types"]] <- parse_compact_col_spec(opts[["col_types"]], col_names)
}
# Remove read_options from opts before calling csv_convert_options
opts[["read_options"]] <- NULL
do.call(csv_convert_options, opts)
}
csv_file_format_read_opts <- function(schema = NULL, partitioning = NULL, ...) {
opts <- list(...)
# Filter out arguments meant for CsvParseOptions/CsvConvertOptions
arrow_opts <- c(names(formals(csv_parse_options)), "parse_options")
readr_opts <- names(formals(readr_to_csv_parse_options))
convert_opts <- c(names(formals(csv_convert_options)), "na", "quoted_na", "convert_options")
opts[arrow_opts] <- NULL
opts[readr_opts] <- NULL
opts[convert_opts] <- NULL
opts[["read_options"]] <- NULL
opt_names <- names(opts)
arrow_opts <- c(names(formals(csv_read_options)))
readr_opts <- c(names(formals(readr_to_csv_read_options)))
is_arrow_opt <- !is.na(match(opt_names, arrow_opts))
is_readr_opt <- !is.na(match(opt_names, readr_opts))
check_ambiguous_options(opt_names, arrow_opts, readr_opts)
null_or_true <- function(x) {
is.null(x) || isTRUE(x)
}
if (!is.null(schema) && null_or_true(opts[["column_names"]]) && null_or_true(opts[["col_names"]])) {
if (any(is_readr_opt)) {
opts[["col_names"]] <- setdiff(names(schema), names(partitioning))
} else {
opts[["column_names"]] <- setdiff(names(schema), names(partitioning))
}
}
if (any(is_readr_opt)) {
# Catch cases when the user specifies a mix of Arrow C++ options and
# readr-style options
if (!all(is_readr_opt)) {
abort(c(
"Additional CSV reading options must be Arrow-style or readr-style, but not both.",
i = sprintf("Arrow options used: %s.", oxford_paste(opt_names[is_arrow_opt])),
i = sprintf("readr options used: %s.", oxford_paste(opt_names[is_readr_opt]))
))
}
do.call(readr_to_csv_read_options, opts) # all options have readr-style names
} else {
do.call(csv_read_options, opts) # all options have Arrow C++ names
}
}
#' Format-specific scan options
#'
#' @description
#' A `FragmentScanOptions` holds options specific to a `FileFormat` and a scan
#' operation.
#'
#' @section Factory:
#' `FragmentScanOptions$create()` takes the following arguments:
#' * `format`: A string identifier of the file format. Currently supported values:
#' * "parquet"
#' * "csv"/"text", aliases for the same format.
#' * `...`: Additional format-specific options
#'
#' `format = "parquet"`:
#' * `use_buffered_stream`: Read files through buffered input streams rather than
#' loading entire row groups at once. This may be enabled
#' to reduce memory overhead. Disabled by default.
#' * `buffer_size`: Size of buffered stream, if enabled. Default is 8KB.
#' * `pre_buffer`: Pre-buffer the raw Parquet data. This can improve performance
#' on high-latency filesystems. Disabled by default.
#' * `thrift_string_size_limit`: Maximum string size allocated for decoding thrift
#' strings. May need to be increased in order to read
#' files with especially large headers. Default value
#' 100000000.
#' * `thrift_container_size_limit`: Maximum size of thrift containers. May need to be
#' increased in order to read files with especially large
#' headers. Default value 1000000.
#
#' `format = "text"`: see [CsvConvertOptions]. Note that options can only be
#' specified with the Arrow C++ library naming. Also, "block_size" from
#' [CsvReadOptions] may be given.
#'
#' It returns the appropriate subclass of `FragmentScanOptions`
#' (e.g. `CsvFragmentScanOptions`).
#' @rdname FragmentScanOptions
#' @name FragmentScanOptions
#' @export
FragmentScanOptions <- R6Class(
"FragmentScanOptions",
inherit = ArrowObject,
active = list(
# @description
# Return the `FragmentScanOptions`'s type
type = function() dataset___FragmentScanOptions__type_name(self)
)
)
FragmentScanOptions$create <- function(format, ...) {
if (format %in% c("csv", "text", "tsv")) {
CsvFragmentScanOptions$create(...)
} else if (format == "parquet") {
ParquetFragmentScanOptions$create(...)
} else if (format == "json") {
JsonFragmentScanOptions$create(...)
} else {
stop("Unsupported file format: ", format, call. = FALSE)
}
}
#' @export
as.character.FragmentScanOptions <- function(x, ...) {
x$type
}
#' @usage NULL
#' @format NULL
#' @rdname FragmentScanOptions
#' @export
CsvFragmentScanOptions <- R6Class("CsvFragmentScanOptions", inherit = FragmentScanOptions)
CsvFragmentScanOptions$create <- function(
...,
convert_opts = csv_file_format_convert_opts(...),
read_opts = csv_file_format_read_opts(...)
) {
dataset___CsvFragmentScanOptions__Make(convert_opts, read_opts)
}
#' @usage NULL
#' @format NULL
#' @rdname FragmentScanOptions
#' @export
ParquetFragmentScanOptions <- R6Class("ParquetFragmentScanOptions", inherit = FragmentScanOptions)
ParquetFragmentScanOptions$create <- function(
use_buffered_stream = FALSE,
buffer_size = 8196,
pre_buffer = TRUE,
thrift_string_size_limit = 100000000,
thrift_container_size_limit = 1000000
) {
dataset___ParquetFragmentScanOptions__Make(
use_buffered_stream,
buffer_size,
pre_buffer,
thrift_string_size_limit,
thrift_container_size_limit
)
}
#' @usage NULL
#' @format NULL
#' @rdname FragmentScanOptions
#' @export
JsonFragmentScanOptions <- R6Class("JsonFragmentScanOptions", inherit = FragmentScanOptions)
JsonFragmentScanOptions$create <- function(...) {
dots <- list2(...)
valid_parse_options <- names(formals(JsonParseOptions$create))
valid_read_options <- names(formals(JsonReadOptions$create))
valid_options <- c(valid_parse_options, valid_read_options)
parse_opt_choices <- dots[names(dots) %in% valid_parse_options]
read_opt_choices <- dots[names(dots) %in% valid_read_options]
if (length(setdiff(names(dots), valid_options)) > 0) {
abort(
c(
paste("`JsonFragmentScanOptions` must match one or more of:", oxford_paste(valid_options, quote_symbol = "`")),
i = paste("Invalid selection(s):", oxford_paste(setdiff(names(dots), valid_options), quote_symbol = "`"))
)
)
}
parse_options <- do.call(JsonParseOptions$create, parse_opt_choices)
read_options <- do.call(JsonReadOptions$create, read_opt_choices)
dataset___JsonFragmentScanOptions__Make(parse_options, read_options)
}
#' Format-specific write options
#'
#' @description
#' A `FileWriteOptions` holds write options specific to a `FileFormat`.
FileWriteOptions <- R6Class(
"FileWriteOptions",
inherit = ArrowObject,
public = list(
update = function(column_names, ...) {
check_additional_args <- function(format, passed_args) {
if (format == "parquet") {
supported_args <- names(formals(write_parquet))
supported_args <- supported_args[supported_args != c("x", "sink")]
} else if (format == "ipc") {
supported_args <- c(
"use_legacy_format",
"metadata_version",
"codec",
"null_fallback"
)
} else if (format %in% c("csv", "tsv", "txt", "text")) {
supported_args <- c(
names(formals(csv_write_options)),
names(formals(readr_to_csv_write_options))
)
}
unsupported_passed_args <- setdiff(passed_args, supported_args)
if (length(unsupported_passed_args) > 0) {
err_header <- paste0(
oxford_paste(unsupported_passed_args, quote_symbol = "`"),
ngettext(
length(unsupported_passed_args),
" is not a valid argument ",
" are not valid arguments "
),
"for your chosen `format`."
)
err_info <- NULL
arg_info <- paste0(
"Supported arguments: ",
oxford_paste(unique(supported_args), quote_symbol = "`"),
"."
)
if ("compression" %in% unsupported_passed_args) {
err_info <- "You could try using `codec` instead of `compression`."
}
abort(c(err_header, i = err_info, i = arg_info))
}
}
args <- list(...)
check_additional_args(self$type, names(args))
if (self$type == "parquet") {
dataset___ParquetFileWriteOptions__update(
self,
ParquetWriterProperties$create(column_names, ...),
ParquetArrowWriterProperties$create(...)
)
} else if (self$type == "ipc") {
if (is.null(args$codec)) {
dataset___IpcFileWriteOptions__update1(
self,
get_ipc_use_legacy_format(args$use_legacy_format),
get_ipc_metadata_version(args$metadata_version)
)
} else {
dataset___IpcFileWriteOptions__update2(
self,
get_ipc_use_legacy_format(args$use_legacy_format),
args$codec,
get_ipc_metadata_version(args$metadata_version)
)
}
} else if (self$type %in% c("csv", "tsv", "txt", "text")) {
arrow_opts <- names(formals(csv_write_options))
readr_opts <- names(formals(readr_to_csv_write_options))
readr_only_opts <- setdiff(readr_opts, arrow_opts)
arrow_only_opts <- setdiff(arrow_opts, readr_opts)
is_arrow_opt <- !is.na(match(names(args), arrow_opts))
is_readr_opt <- !is.na(match(names(args), readr_opts))
is_arrow_only_opt <- !is.na(match(names(args), arrow_only_opts))
is_readr_only_opt <- !is.na(match(names(args), readr_only_opts))
if (any(is_arrow_only_opt) && any(is_readr_only_opt)) {
stop("Use either Arrow write options or readr write options, not both")
}
# These option names aren't mutually exclusive, so only use readr path
# if we have at least one readr-specific option.
if (sum(is_readr_only_opt)) {
dataset___CsvFileWriteOptions__update(
self,
do.call(readr_to_csv_write_options, args[is_readr_opt])
)
} else {
dataset___CsvFileWriteOptions__update(
self,
do.call(csv_write_options, args[is_arrow_opt])
)
}
}
invisible(self)
}
),
active = list(
type = function() dataset___FileWriteOptions__type_name(self)
)
)
FileWriteOptions$create <- function(format, ...) {
if (!inherits(format, "FileFormat")) {
format <- FileFormat$create(format)
}
options <- dataset___FileFormat__DefaultWriteOptions(format)
options$update(...)
}
|