Skip to content

[R] slice_sample does not sample randomly #45139

Closed as duplicate
Closed as duplicate

Description

@blongworth

Describe the bug, including details regarding any error messages, version, and platform.

slice_sample() in Arrow 18.1.0 does not randomly sample rows from an arrow dataset. Reprex follows. I'd expect a random sample of rows, so x distributed in 1:5000.

library(dplyr)
library(arrow)

df <- data.frame(x = 1:5000)

ds <- arrow_table(df)

slice_sample(ds, n = 10) |> 
  collect()
#> # A tibble: 10 × 1
#>        x
#>    <int>
#>  1     3
#>  2     4
#>  3    16
#>  4    17
#>  5    65
#>  6    66
#>  7    74
#>  8    93
#>  9   123
#> 10   129

Created on 2024-12-31 with reprex v2.1.1

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.2 (2024-10-31)
#>  os       macOS Sonoma 14.7
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/New_York
#>  date     2024-12-31
#>  pandoc   3.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  arrow       * 18.1.0  2024-12-05 [1] CRAN (R 4.4.1)
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.4.0)
#>  bit           4.0.5   2022-11-15 [1] CRAN (R 4.4.0)
#>  bit64         4.0.5   2020-08-30 [1] CRAN (R 4.4.0)
#>  cli           3.6.2   2023-12-11 [1] CRAN (R 4.4.0)
#>  digest        0.6.35  2024-03-11 [1] CRAN (R 4.4.0)
#>  dplyr       * 1.1.4   2023-11-17 [1] CRAN (R 4.4.0)
#>  evaluate      0.23    2023-11-01 [1] CRAN (R 4.4.0)
#>  fansi         1.0.6   2023-12-08 [1] CRAN (R 4.4.0)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.4.0)
#>  fs            1.6.4   2024-04-25 [1] CRAN (R 4.4.0)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.4.0)
#>  glue          1.7.0   2024-01-09 [1] CRAN (R 4.4.0)
#>  htmltools     0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0)
#>  knitr         1.46    2024-04-06 [1] CRAN (R 4.4.0)
#>  lifecycle     1.0.4   2023-11-07 [1] CRAN (R 4.4.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.4.0)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.4.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.4.0)
#>  purrr         1.0.2   2023-08-10 [1] CRAN (R 4.4.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.4.0)
#>  reprex        2.1.1   2024-07-06 [1] CRAN (R 4.4.0)
#>  rlang         1.1.3   2024-01-10 [1] CRAN (R 4.4.0)
#>  rmarkdown     2.26    2024-03-05 [1] CRAN (R 4.4.0)
#>  rstudioapi    0.16.0  2024-03-24 [1] CRAN (R 4.4.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.4.0)
#>  tibble        3.2.1   2023-03-20 [1] CRAN (R 4.4.0)
#>  tidyselect    1.2.1   2024-03-11 [1] CRAN (R 4.4.0)
#>  utf8          1.2.4   2023-10-22 [1] CRAN (R 4.4.0)
#>  vctrs         0.6.5   2023-12-01 [1] CRAN (R 4.4.0)
#>  withr         3.0.0   2024-01-16 [1] CRAN (R 4.4.0)
#>  xfun          0.49    2024-10-31 [1] CRAN (R 4.4.1)
#>  yaml          2.3.8   2023-12-11 [1] CRAN (R 4.4.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

As this issue could be dangerous for someone assuming a random sample, should there be a note in the docs or slice_sample() removed until it's fixed?

Component(s)

R

Activity

  1. changed the title [-]slice_sample does not sample randomly[/-] [+][R] slice_sample does not sample randomly[/+] on Jan 1, 2025
  2. twedl commented on Feb 3, 2025

    @twedl

    To add some context and detail to this bug to help us along:

    library(tidyverse)
    library(arrow)
    
    tbl <- tibble(int = 1:2)
    tab <- arrow_table(tbl)
    
    # dplyr version of slice_sample:
    dplyr_res <- replicate(
        1000, 
        tbl |> slice_sample(n=1), 
        simplify = FALSE
      ) |> 
      list_rbind()
    
    # arrow version:
    arrow_res <- replicate(
        1000, 
        tab |> slice_sample(n = 1) |> collect(), 
        simplify = FALSE
      ) |>
      list_rbind()
    
    # expect close to 0.5 for each 
    table(dplyr_res$int) / nrow(dplyr_res)
    #> 
    #>     1     2 
    #> 0.522 0.478
    table(arrow_res$int) / nrow(arrow_res)
    #> 
    #>         1         2 
    #> 0.6902107 0.3097893
    
    # and 1000 results
    nrow(dplyr_res)
    #> [1] 1000
    nrow(arrow_res)
    #> [1] 807

    Created on 2025-02-03 with reprex v2.1.1

    Session info

    sessionInfo()
    #> R version 4.4.2 (2024-10-31)
    #> Platform: aarch64-apple-darwin20
    #> Running under: macOS Sequoia 15.1.1
    #> 
    #> Matrix products: default
    #> BLAS:   /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib 
    #> LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0
    #> 
    #> locale:
    #> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    #> 
    #> time zone: America/Halifax
    #> tzcode source: internal
    #> 
    #> attached base packages:
    #> [1] stats     graphics  grDevices utils     datasets  methods   base     
    #> 
    #> other attached packages:
    #>  [1] arrow_19.0.0.9000 lubridate_1.9.3   forcats_1.0.0     stringr_1.5.1    
    #>  [5] dplyr_1.1.4       purrr_1.0.2       readr_2.1.5       tidyr_1.3.1      
    #>  [9] tibble_3.2.1      ggplot2_3.5.1     tidyverse_2.0.0  
    #> 
    #> loaded via a namespace (and not attached):
    #>  [1] bit_4.5.0         gtable_0.3.6      compiler_4.4.2    reprex_2.1.1     
    #>  [5] tidyselect_1.2.1  assertthat_0.2.1  scales_1.3.0      yaml_2.3.10      
    #>  [9] fastmap_1.2.0     R6_2.5.1          generics_0.1.3    knitr_1.49       
    #> [13] munsell_0.5.1     pillar_1.9.0      tzdb_0.4.0        rlang_1.1.4      
    #> [17] utf8_1.2.4        stringi_1.8.4     xfun_0.49         fs_1.6.5         
    #> [21] bit64_4.5.2       timechange_0.3.0  cli_3.6.3         withr_3.0.2      
    #> [25] magrittr_2.0.3    digest_0.6.37     grid_4.4.2        rstudioapi_0.17.1
    #> [29] hms_1.1.3         lifecycle_1.0.4   vctrs_0.6.5       evaluate_1.0.1   
    #> [33] glue_1.8.0        fansi_1.0.6       colorspace_2.1-1  rmarkdown_2.29   
    #> [37] tools_4.4.2       pkgconfig_2.0.3   htmltools_0.5.8.1

    It boils down to these two lines in slice_sample.arrow_dplyr_query:

    prop <- min(n_to_prop(.data, n) + 0.05, 1)

    And:

    .data <- head(.data, n)

    The current strategy is to convert n to prop, get answer via the prop version, then convert back to n by applying head, which will end up picking the first n rows that pass the prop filter. In @blongworth's example, it'll pick the first 10 out of the ~260 random rows selected out of 5000 (the 260 comes from L104 above: 10 + 0.05 * 5000). On top of that, the prop filter sometimes won't select enough rows, resulting in some empty samples (n<1, in my example).

    Swapping head to sample in L132 above should fix the reported bug for now. (Silly.) But someone might want to improve the sampling methodology altogether; e.g., referenced by the comment:

    # TODO(ARROW-17974): use Expression$create("random") instead of UDF hack

  3. thisisnic commented on Nov 20, 2026

    @thisisnic
    Member

    This still reproduces on 26.0.0: with the reprex above, every sample of 10 from 1:5000 comes back sorted and below about 280, and the two-row reprex gives roughly a 70/30 split with only around 800 rows from 1000 draws.

    It's the same underlying problem as #38638 (asking for n rows oversamples by proportion and then takes the first n rows that pass, which favours the start of the table and can return too few rows), so I'm closing this as a duplicate of that issue. A fix is in progress in #51385.

  4. twedl commented on Nov 20, 2026

    @twedl

    Amazing, thank you for the great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions