You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "eitsupi (via GitHub)" <gi...@apache.org> on 2023/03/16 15:48:13 UTC

[GitHub] [arrow] eitsupi commented on issue #34589: [R] `open_dataset()` does not recognise/read csv files when explicit schema provided

eitsupi commented on issue #34589:
URL: https://github.com/apache/arrow/issues/34589#issuecomment-1472233438

   Is this perhaps related to the need to skip the header row when specifying schema in `read_csv_arrow`?
   https://arrow.apache.org/docs/r/reference/read_delim_arrow.html#ref-examples
   
   For CSV files, `col_types` should be used to change the type of a particular column.
   
   ``` r
   readr::readr_example("mtcars.csv") |>
     arrow::read_csv_arrow(schema = arrow::schema(cyl = arrow::utf8()))
   #> Error:
   #> ! Invalid: CSV parse error: Expected 1 columns, got 11: "mpg","cyl","disp","hp","drat","wt","qsec","vs","am","gear","carb"
   
   #> Backtrace:
   #>     ▆
   #>  1. └─arrow (local) `<fn>`(...)
   #>  2.   └─base::tryCatch(...)
   #>  3.     └─base (local) tryCatchList(expr, classes, parentenv, handlers)
   #>  4.       └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
   #>  5.         └─value[[3L]](cond)
   #>  6.           └─arrow:::augment_io_error_msg(e, call, schema = schema)
   #>  7.             └─rlang::abort(msg, call = call)
   
   readr::readr_example("mtcars.csv") |>
     arrow::open_dataset(schema = arrow::schema(cyl = arrow::utf8()), format = "csv") |>
     dplyr::collect()
   #> Error in `compute.Dataset()`:
   #> ! Invalid: Could not open CSV input source '/usr/local/lib/R/site-library/readr/extdata/mtcars.csv': Invalid: CSV parse error: Row #1: Expected 1 columns, got 11: "mpg","cyl","disp","hp","drat","wt","qsec","vs","am","gear","carb"
   
   #> Backtrace:
   #>      ▆
   #>   1. ├─dplyr::collect(...)
   #>   2. └─arrow:::collect.Dataset(...)
   #>   3.   ├─arrow:::collect.ArrowTabular(compute.Dataset(x), as_data_frame)
   #>   4.   │ └─base::as.data.frame(x, ...)
   #>   5.   └─arrow:::compute.Dataset(x)
   #>   6.     └─base::tryCatch(...)
   #>   7.       └─base (local) tryCatchList(expr, classes, parentenv, handlers)
   #>   8.         └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
   #>   9.           └─value[[3L]](cond)
   #>  10.             └─arrow:::augment_io_error_msg(e, call, schema = schema())
   #>  11.               └─rlang::abort(msg, call = call)
   
   readr::readr_example("mtcars.csv") |>
     arrow::read_csv_arrow(col_types = arrow::schema(cyl = arrow::utf8()))
   #> # A tibble: 32 × 11
   #>      mpg cyl    disp    hp  drat    wt  qsec    vs    am  gear  carb
   #>    <dbl> <chr> <dbl> <int> <dbl> <dbl> <dbl> <int> <int> <int> <int>
   #>  1  21   6      160    110  3.9   2.62  16.5     0     1     4     4
   #>  2  21   6      160    110  3.9   2.88  17.0     0     1     4     4
   #>  3  22.8 4      108     93  3.85  2.32  18.6     1     1     4     1
   #>  4  21.4 6      258    110  3.08  3.22  19.4     1     0     3     1
   #>  5  18.7 8      360    175  3.15  3.44  17.0     0     0     3     2
   #>  6  18.1 6      225    105  2.76  3.46  20.2     1     0     3     1
   #>  7  14.3 8      360    245  3.21  3.57  15.8     0     0     3     4
   #>  8  24.4 4      147.    62  3.69  3.19  20       1     0     4     2
   #>  9  22.8 4      141.    95  3.92  3.15  22.9     1     0     4     2
   #> 10  19.2 6      168.   123  3.92  3.44  18.3     1     0     4     4
   #> # … with 22 more rows
   
   readr::readr_example("mtcars.csv") |>
     arrow::open_dataset(col_types = arrow::schema(cyl = arrow::utf8()), format = "csv") |>
     dplyr::collect()
   #> # A tibble: 32 × 11
   #>      mpg cyl    disp    hp  drat    wt  qsec    vs    am  gear  carb
   #>    <dbl> <chr> <dbl> <int> <dbl> <dbl> <dbl> <int> <int> <int> <int>
   #>  1  21   6      160    110  3.9   2.62  16.5     0     1     4     4
   #>  2  21   6      160    110  3.9   2.88  17.0     0     1     4     4
   #>  3  22.8 4      108     93  3.85  2.32  18.6     1     1     4     1
   #>  4  21.4 6      258    110  3.08  3.22  19.4     1     0     3     1
   #>  5  18.7 8      360    175  3.15  3.44  17.0     0     0     3     2
   #>  6  18.1 6      225    105  2.76  3.46  20.2     1     0     3     1
   #>  7  14.3 8      360    245  3.21  3.57  15.8     0     0     3     4
   #>  8  24.4 4      147.    62  3.69  3.19  20       1     0     4     2
   #>  9  22.8 4      141.    95  3.92  3.15  22.9     1     0     4     2
   #> 10  19.2 6      168.   123  3.92  3.44  18.3     1     0     4     4
   #> # … with 22 more rows
   ```
   
   <sup>Created on 2023-03-16 with [reprex v2.0.2](https://reprex.tidyverse.org)</sup>
   
   If the schema option is used for Parquet files, only that columns will be read.
   
   ``` r
   readr::readr_example("mtcars.csv") |>
     readr::read_csv(show_col_types = FALSE) |>
     arrow::write_parquet("test.parquet")
   
   arrow::open_dataset("test.parquet", schema = arrow::schema(cyl = arrow::utf8())) |>
     dplyr::collect()
   #> # A tibble: 32 × 1
   #>    cyl
   #>    <chr>
   #>  1 6
   #>  2 6
   #>  3 4
   #>  4 6
   #>  5 8
   #>  6 6
   #>  7 8
   #>  8 4
   #>  9 4
   #> 10 6
   #> # … with 22 more rows
   ```
   
   <sup>Created on 2023-03-16 with [reprex v2.0.2](https://reprex.tidyverse.org)</sup>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org