apache / apache/arrow

[C++][R]Opening a multi-file dataset and writing a re-partitioned version of it fails

Open
#18,944 11 comments 0 reactions 0 assignees View on GitHub
Component: C++ Component: R Priority: Critical Status: needs champion Type: bug
Dominant language
C++
Stars
17.1k
Forks
4.3k
Avg merge
3d 13h
Merged PRs (30d)
88

Description

Attempting to open a multi-file dataset and write a re-partitioned version of it fails as it seems there is an attempt to collect data into memory first. This happens both for wide and long data.

Steps to reproduce the issue:
1. Create a large dataset (100k columns, 300k rows) and write it to disk and create 20 copies of it. Each file will have a footprint of roughly 7.5GB. 
```r

library(arrow)
library(dplyr)
library(fs)

rows <- 300000
cols <- 100000
partitions <- 20

wide_df <- as.data.frame(
matrix(
sample(1:32767, rows * cols / partitions, replace = TRUE),
ncol = cols)
)

schem <- sapply(colnames(wide_df), function(nm) {int16()})
schem <- do.call(schema, schem)

wide_tab <- Table$create(wide_df, schema = schem)

write_parquet(wide_tab, "~/Documents/arrow_playground/wide.parquet")

fs::dir_create("~/Documents/arrow_playground/wide_ds")
for (i in seq_len(partitions)) {
file.copy("~/Documents/arrow_playground/wide.parquet",
glue::glue("~/Documents/arrow_playground/wide_ds/wide-{i-1}.parquet"))
}

ds_wide <- open_dataset("~/Documents/arrow_playground/wide_ds/")
```
All the following steps fail:

2. Creating and writing a partitioned version of `{}ds_wide{`}.
```r

ds_wide %>%
mutate(grouper = round(V1 / 1024)) %>%
write_dataset("~/Documents/arrow_playground/partitioned",
partitioning = "grouper",
format = "parquet")
```
3. Writing a non-partitioned dataset:
```r

ds_wide %>%
write_dataset("~/Documents/arrow_playground/partitioned",
format = "parquet")
```
4. Creating the partitioning variable first and then attempting to write:
```r

ds2 <- ds_wide %>%
mutate(grouper = round(V1 / 1024))

ds2 %>%
write_dataset("~/Documents/arrow_playground/partitioned",
partitioning = "grouper",
format = "parquet")
```
5. Attempting to write to csv:
```r

ds_wide %>%
write_dataset("~/Documents/arrow_playground/csv_writing/test.csv",
format = "csv")
```
None of the failures seem to originate in R code and they all result in a similar behaviour: the R sessions consume increasing amounts of RAM until they crash.

**Environment**: M1 Mac, macOS Monterey 12.0.1, 16Gb RAM
R 4.1.1, {arrow} R package 6.0.0.2 (release) & 6.0.0.9000 (dev)
**Reporter**: [Dragoș Moldovan-Grünfeld](https://issues.apache.org/jira/browse/ARROW-14736) / @dragosmg
#### Related issues:
- [[R][C++] Arrow crashes (OOM) on R client with large remote parquet files](https://github.com/apache/arrow/issues/30594) (is related to)
- [[C++][Dataset] Change scanner readahead limits to be based on bytes instead of number of batches](https://github.com/apache/arrow/issues/30191) (depends upon)
- [[C++][Dataset] Devise a mechanism to limit the total "system ram" (process + cache) used by dataset writes](https://github.com/apache/arrow/issues/30179) (depends upon)
- [[C++][Datasets] Improve memory usage of datasets](https://github.com/apache/arrow/issues/30893) (is depended upon by)
#### Original Issue Attachments:
- [image-2021-11-17-14-43-37-127.png](https://issues.apache.org/jira/secure/attachment/13036230/image-2021-11-17-14-43-37-127.png)
- [image-2021-11-17-14-54-42-747.png](https://issues.apache.org/jira/secure/attachment/13036231/image-2021-11-17-14-54-42-747.png)
- [image-2021-11-17-14-55-08-597.png](https://issues.apache.org/jira/secure/attachment/13036232/image-2021-11-17-14-55-08-597.png)

**Note**: *This issue was originally created as [ARROW-14736](https://issues.apache.org/jira/browse/ARROW-14736). Please see the [migration documentation](https://github.com/apache/arrow/issues/14542) for further details.*

Contributor guide

Open the contributing guide

Research direction

Start from the R entry points open_dataset() and write_dataset(), then reproduce the four dataset-writing cases described with the wide and long datasets. Read the linked scanner readahead, total system RAM, and dataset memory-usage issues for the surrounding scope; done means multi-file dataset writes no longer grow memory until the R session crashes.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, r
Domain
data-engineering, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.