Repository navigation
Description
Background
The recently-added open_parquet_file function introduces a new variation on the existing AbstractBufferedFile caching strategies. That is, it compliments the KnownPartsOfAFile ("parts") strategy by performing format-aware "precaching" before the file is open (and before the "parts" cache is initialized).
The new open_parquet_file function is very powerful. However, toggling between the specialized function and fs.open in downstream libraries like Dask is a bit painful. I propose that we add new logic in fsspec's file-opening utilities to trigger the same precaching logic before the AbstractBufferedFile objects are created.
At the moment, I feel that there are two general solutions that may make sense:
Proposed API v1
The first possible solution here is to support the case that the user supplies "precache" and "precache_options" fields within the cache_options dictionary that is allready supported by AbstractBufferedFile:
fs.open(
path,
cache_type="parts",
cache_options={"precache": "parquet", "precache_options": {"columns": ...}},
)The value passes with the "precache" field ("parquet" in the example above) may be a string id for an "official" method or a callable function. The specification for this callable is that (1) it accepts the kwargs defined in precache_options as inputs, and (2) it outputs the data dictionary required for the "parts" cache.
Notes
- These new
cache_optionskeys will only be supported for the "parts" cache (for now, at least) - The new fields will need to be detected and acted upon in the open-related functions, and ignored in the
KnownPartsOfAFilelogic itself. That is, the file-opening functions will be expected to copy thecache_options, and populate the"data"field for the kwargs that are passed down tofs.open. Sincecache_optionsis processed whenKnownPartsOfAFileis initialized, it may seen tempting to move all precaching logic into the caching class itself. Unfortunately, this approach has the following problems:- The
KnownPartsOfAFileobject will only have access to a single file, and optimal/flexible precaching may require us to collect bytes for many files at once - The
KnownPartsOfAFileobject will not have direct access to the file system and path. The constrainedfetcherapproach is not ideal for the types of data transfer desired in the precaching stage
- The
Concerns
The only component of this first proposal that feels a bit icky to me is the fact that we are adding fields to "cache_options", but we are not actually acting on the new fields in the location where "cache_options" were originally intended to be processed. For this reason, I am also strongly considering the following alternative...
Proposed API v2
fs.open(
path,
cache_type="parts",
precache="parquet",
precache_options={"columns": ..., ...},
)Here, we add support for new precache and precache_options arguments that are completely distinct from cache_options. My attraction to this solution is that we are no longer using cache_options to store arguments that will not be processed in the cache object itself. The downside, however, is that we are adding distinct arguments that are currently only applicable for cache_type="parts".
One possible mitigation for this "downside" is to require the "precache" callable to accept a cache_type= kwarg, and to output a nested dictionary of values to be added to cache_options (with the outer-most keys being file paths). These requirements would allow the precaching logic to check that the desired caching strategy is consistent with the precaching output, and would also allow the precaching logic to determine the necessary cache_options addition(s) needed for each file.
Desired Feedback
- **Do either of these proposals sound reasonable? If so which one?
- v1: Adding "precache" and "precache_options" support within "cache_options"
- v2: Adding new
precacheandprecache_optionsarguments to file-opening functions
Feedback is VERY welcome here :)
cc @martindurant
Activity
rjzamora commentedon Jan 12, 2022
Note that I recently revised dask#8339 with API v1 in mind. However, my preference is now leaning closer to API v2. Although precaching is obviously coupled tightly with caching, it seems clear that precaching must be performed before any
KnownPartsOfAFileobjects are initialized.Perhaps an even better alternative (call it API v2b) is to add a
precacheclassmethod toBaseCache:This way the precache-cache compatibility check is handled within the cache class itself. So, for
KnownPartsOfAFile, we would just need to addself.precaches = {"parquet": parquet_precache}to__init__, and defineparquet_precacheappropriately. This would also allow us to get away with adding only aprecache_optionsargument to the file-open functions, with the precaching method/logic being specified with the"method"key.So, if the user calls
fs.openusingprecache_options:The logic within
fs.openshould be able to pass theprecache_optionsalong to the appropriate classmethod to populate dictionary of newcache_optionelements (to be used to update a copy of the inputcache_options):martindurant commentedon Jan 13, 2022
I do like the idea of associating the precache with the target cache type, so the class method does indeed make sense to me. It means that the code lives next to the code it's preprocessing for, and
openis changed minimally. Note thatopenis overridden by exactly one subclass as far as I know: reference. That should probably have its code moved to_openas with other implementations, so that you shouldn't worry about it (also, reference doesn't make real file objects anyway, it expects to work via cat/get alone).One caveat occurred to me: all of this machinery will only work with files that derive from AbstractBufferedFile, which is not all the files fsspec can make. E.g., a local filesystem makes a LocalFileOpener thin wrapper around a real file. In such cases, we should presumably not process the cache options at all, as it will repeat work.