Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can read_fst use a filter condition beforehand? #274

Open
hope-data-science opened this issue Feb 21, 2023 · 1 comment
Open

Can read_fst use a filter condition beforehand? #274

hope-data-science opened this issue Feb 21, 2023 · 1 comment

Comments

@hope-data-science
Copy link

The from and to parameters in read_fst is great. I wonder if we can use a filter to select only the rows we needed. If this could be done on disk, it would be wonderful and make fst become a light and convenient database system, which would be great. I think this feature could boost fst to a new level. Any ideas to promote it? (I don't know the underlying tools, so I have no idea whether it would be easy to implemented or not. I just imagine that if we can just use the filter columns to retrieve the needed row ids and extract them using from and to might be efficient)

Thanks.

@MarcusKlik
Copy link
Collaborator

MarcusKlik commented Dec 1, 2023

Hi @hope-data-science, thanks for sharing your ideas!

And yes, you are very correct, the ability to apply a row-filter (and an ordering) during file reads is definitely the next step needed for fst.

If we have filtering and ordering we could do:

library(dplyr)
library(fst)

nr_of_rows <- 1000

# generate sample fst file
tibble(
  A = 1:nr_of_rows,
  B = LETTERS[sample(1:26, nr_of_rows, replace = TRUE)]
) |>
  write_fst("x.fst")


# filter and order during read
fst("x.fst") |>
  filter(A %% 2 == 0) |>
  arrange(B)

In the last steps, the following would happen:

  • first, the filter is calculated and stored in the fst_table object. for simple filters, the actual logical vector result might be calculated on the fly so that the columns needed for filtering don't have to be read into RAM, greatly reducing RAM requirements for large datasets.
  • second, the ordering would result in a (large-) integer vector that can be used to fill the result columns during the read action. This ensures that RAM is only used for the result data and not for intermediate results (again, reducing RAM requirements)

In future implementation, this mechanism could be extended with an on-disk sorting algorithm to allow sorting of larger-than-memory datasets and keep the data on disk as long as possible. Only when collect() is called, the data would actually be fully read.

To make this happen it's probably best to add a full dplyr implementation to the fst package first (so that the code above will actually run) and then implement the required back-end code in fstlib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants