Skip to content

Commit

Permalink
$sink_*() and $write_*() functions invisibly return the input
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennebacher committed Apr 15, 2024
1 parent 18c6b51 commit 0c44bcb
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 15 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@
- Export the `Duration` datatype (#955).
- New active binding `<Series>$struct$fields` (#1002).
- rust-polars is updated to 0.39.0 (#937, #1034).
- All `$write_*()` and `$sink_*()` functions now invisibly return the input
data (#1039).
### Bug fixes
Expand Down
10 changes: 5 additions & 5 deletions R/dataframe__frame.R
Original file line number Diff line number Diff line change
Expand Up @@ -1970,7 +1970,7 @@ DataFrame_write_csv = function(
) |>
unwrap("in $write_csv():")

invisible(NULL)
invisible(self)
}


Expand Down Expand Up @@ -2009,7 +2009,7 @@ DataFrame_write_ipc = function(
) |>
unwrap("in $write_ipc():")

invisible(NULL)
invisible(self)
}


Expand Down Expand Up @@ -2044,7 +2044,7 @@ DataFrame_write_parquet = function(
) |>
unwrap("in $write_parquet():")

invisible(NULL)
invisible(self)
}

#' Write to JSON file
Expand Down Expand Up @@ -2075,7 +2075,7 @@ DataFrame_write_json = function(
.pr$DataFrame$write_json(self, file, pretty, row_oriented) |>
unwrap("in $write_json():")

invisible(NULL)
invisible(self)
}

#' Write to NDJSON file
Expand All @@ -2096,7 +2096,7 @@ DataFrame_write_ndjson = function(file) {
.pr$DataFrame$write_ndjson(self, file) |>
unwrap("in $write_ndjson():")

invisible(NULL)
invisible(self)
}

#' @inherit LazyFrame_rolling title description params details
Expand Down
25 changes: 17 additions & 8 deletions R/lazyframe__lazy.R
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ LazyFrame_collect_in_background = function() {
#' @inheritParams LazyFrame_collect
#'
#' @rdname IO_sink_parquet
#' @return Invisibly returns the input LazyFrame
#'
#' @examples
#' # sink table 'mtcars' from mem to parquet
Expand Down Expand Up @@ -655,8 +656,9 @@ LazyFrame_sink_parquet = function(
data_pagesize_limit,
maintain_order
) |>
unwrap("in $sink_parquet()") |>
invisible()
unwrap("in $sink_parquet()")

invisible(self)
}


Expand All @@ -674,6 +676,8 @@ LazyFrame_sink_parquet = function(
#' @inheritParams LazyFrame_group_by
#' @inheritParams DataFrame_unique
#'
#' @inherit LazyFrame_sink_parquet return
#'
#' @rdname IO_sink_ipc
#'
#' @examples
Expand Down Expand Up @@ -726,8 +730,9 @@ LazyFrame_sink_ipc = function(
compression %||% "uncompressed",
maintain_order
) |>
unwrap("in $sink_ipc()") |>
invisible()
unwrap("in $sink_ipc()")

invisible(self)
}


Expand All @@ -743,6 +748,7 @@ LazyFrame_sink_ipc = function(
#' @inheritParams LazyFrame_group_by
#' @inheritParams DataFrame_unique
#'
#' @inherit LazyFrame_sink_parquet return
#' @rdname IO_sink_csv
#'
#' @examples
Expand Down Expand Up @@ -817,8 +823,9 @@ LazyFrame_sink_csv = function(
quote_style,
maintain_order
) |>
unwrap("in $sink_csv()") |>
invisible()
unwrap("in $sink_csv()")

invisible(self)
}


Expand All @@ -834,6 +841,7 @@ LazyFrame_sink_csv = function(
#' @inheritParams LazyFrame_group_by
#' @inheritParams DataFrame_unique
#'
#' @inherit LazyFrame_sink_parquet return
#' @rdname IO_sink_ndjson
#'
#' @examples
Expand Down Expand Up @@ -880,8 +888,9 @@ LazyFrame_sink_ndjson = function(
path,
maintain_order
) |>
unwrap("in $sink_ndjson()") |>
invisible()
unwrap("in $sink_ndjson()")

invisible(self)
}


Expand Down
3 changes: 3 additions & 0 deletions man/IO_sink_csv.Rd

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

3 changes: 3 additions & 0 deletions man/IO_sink_ipc.Rd

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

3 changes: 3 additions & 0 deletions man/IO_sink_ndjson.Rd

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

3 changes: 3 additions & 0 deletions man/IO_sink_parquet.Rd

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

5 changes: 5 additions & 0 deletions tests/testthat/test-csv-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ test_that("write_csv: path works", {
)
})

test_that("write_csv returns the input data", {
x = dat_pl$write_csv(temp_out)
expect_identical(x$to_list(), dat_pl$to_list())
})

test_that("write_csv: null_values works", {
expect_grepl_error(
dat_pl$write_csv(temp_out, null_values = NULL)
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-ipc.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,10 @@ patrick::with_parameters_test_that("write and read Apache Arrow file",
compression = list(NULL, "uncompressed", "lz4", "zstd"),
.test_name = compression
)

test_that("write_ipc returns the input data", {
dat = pl$DataFrame(mtcars)
tmpf = tempfile(fileext = ".arrow")
x = dat$write_ipc(tmpf)
expect_identical(x$to_list(), dat$to_list())
})
7 changes: 7 additions & 0 deletions tests/testthat/test-json_write.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ test_that("write_ndjson: path works", {
ignore_attr = TRUE # rownames are lost when writing / reading from json
)
})

test_that("write_ndjson returns the input data", {
dat = pl$DataFrame(mtcars)
tmpf = tempfile(fileext = ".arrow")
x = dat$write_ndjson(tmpf)
expect_identical(x$to_list(), dat$to_list())
})
7 changes: 7 additions & 0 deletions tests/testthat/test-parquet.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,10 @@ test_that("throw error if invalid compression is passed", {
"Failed to set parquet compression method"
)
})

test_that("write_parquet returns the input data", {
dat = pl$DataFrame(mtcars)
tmpf = tempfile()
x = dat$write_parquet(tmpf)
expect_identical(x$to_list(), dat$to_list())
})
21 changes: 19 additions & 2 deletions tests/testthat/test-sink_stream.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ test_that("Test sinking data to parquet file", {
expect_grepl_error(lf$sink_parquet(tmpf, compression = "rar"))
lf$sink_parquet(tmpf)
expect_equal(pl$scan_parquet(tmpf)$collect()$to_data_frame(), rdf)

# return the input data
x = lf$sink_parquet(tmpf)
expect_identical(x$collect()$to_list(), lf$collect()$to_list())
})

test_that("Test sinking data to parquet file", {
test_that("Test sinking data to IPC file", {
tmpf = tempfile()
on.exit(unlink(tmpf))
lf$sink_ipc(tmpf)
Expand Down Expand Up @@ -51,6 +55,10 @@ test_that("Test sinking data to parquet file", {
expect_identical(rdf_in_bg$to_data_frame(), rdf)
}
)

# return the input data
x = lf$sink_ipc(tmpf)
expect_identical(x$collect()$to_list(), lf$collect()$to_list())
})


Expand Down Expand Up @@ -92,6 +100,10 @@ test_that("sink_csv works", {
dat[, c("drat", "mpg")],
ignore_attr = TRUE # ignore row names
)

# return the input data
x = dat_pl$sink_csv(temp_out)
expect_identical(x$collect()$to_list(), dat_pl$collect()$to_list())
})

test_that("sink_csv: null_values works", {
Expand Down Expand Up @@ -231,6 +243,11 @@ test_that("sink_csv: float_precision works", {

test_that("sink_ndjson works", {
temp_out = tempfile(fileext = ".json")
pl$LazyFrame(mtcars)$head(15)$select(pl$col("drat", "mpg"))$sink_ndjson(temp_out)
dat = pl$LazyFrame(mtcars)$head(15)$select(pl$col("drat", "mpg"))
dat$sink_ndjson(temp_out)
expect_snapshot_file(temp_out)

# return the input data
x = dat$sink_ndjson(temp_out)
expect_identical(x$collect()$to_list(), dat$collect()$to_list())
})

0 comments on commit 0c44bcb

Please sign in to comment.