nuno-faria commented on code in PR #27580:
URL: https://github.com/apache/datafusion/pull/27580#discussion_r4756969313
##########
datafusion/datasource-parquet/src/metadata.rs:
##########
@@ -269,15 +271,30 @@ impl<'a> DFParquetMetadata<'a> {
}
/// Set an optional [`FileMetadataCache`] used to avoid re-fetching
- /// [`ParquetMetaData`] for files that have already been read.
+ /// [`ParquetMetaData`] for files that have already been read, together
+ /// with the [`ObjectStoreUrl`] of the store this file is read from.
+ ///
+ /// The cache is shared by all object stores, so the URL and the file's
+ /// location together form the [`FileMetadataCacheKey`]. This way files
+ /// with the same path in different object stores do not share a cache
+ /// entry.
pub fn with_file_metadata_cache(
mut self,
- file_metadata_cache: Option<Arc<FileMetadataCache>>,
+ file_metadata_cache: Option<(Arc<FileMetadataCache>, ObjectStoreUrl)>,
) -> Self {
self.file_metadata_cache = file_metadata_cache;
self
}
+ /// The configured [`FileMetadataCache`] and this file's key in it.
+ fn metadata_cache(&self) -> Option<(&FileMetadataCache,
FileMetadataCacheKey)> {
+ let (cache, url) = self.file_metadata_cache.as_ref()?;
+ Some((
+ cache.as_ref(),
+ FileMetadataCacheKey::new(url, self.object_meta.location.clone()),
+ ))
+ }
+
Review Comment:
I'm not sure about this function, since it returns more than the cache. IMO
it would be better to keep the previous code + create the new key inline.
##########
datafusion/execution/src/object_store.rs:
##########
@@ -162,6 +174,19 @@ pub trait ObjectStoreRegistry: Send + Sync +
std::fmt::Debug + 'static {
/// the `url` and [`ObjectStoreRegistry`] implementation. An
[`ObjectStore`] may be lazily
/// created and registered.
fn get_store(&self, url: &Url) -> Result<Arc<dyn ObjectStore>>;
+
+ /// Returns the [`ObjectStoreUrl`] under which `store` is registered, if
+ /// it is known.
+ ///
+ /// This is the reverse of [`Self::get_store`]. It lets code that is only
+ /// handed an [`ObjectStore`], such as `FileFormat::infer_schema`, find the
+ /// identity of that store, for example to key the file metadata cache.
+ /// Returning `None` is always correct, and makes such callers skip work
+ /// that needs the store's identity, such as caching file metadata.
Review Comment:
This is very specific to the metadata cache so I think it can be removed.
##########
docs/source/library-user-guide/upgrading/57.0.0.md:
##########
@@ -168,6 +168,69 @@ The trait already required `'static`, which `Any` implies,
so existing
implementations need no changes. `ParquetSource::parquet_file_reader_factory`
returns the trait object to downcast.
+### `FileMetadataCache` is keyed by object store and path
Review Comment:
Could the upgrade guide be compacted? I feel it is currently too verbose.
##########
datafusion/datasource-parquet/src/metadata.rs:
##########
@@ -2137,4 +2159,99 @@ mod tests {
);
}
}
+
+ /// Writes a one row Parquet file with column `a` = `value` to `store`.
+ async fn put_single_value_file(
+ store: &dyn ObjectStore,
+ path: &Path,
+ value: i32,
+ ) -> u64 {
+ let batch = RecordBatch::try_from_iter([(
+ "a",
+ Arc::new(Int32Array::from(vec![value])) as _,
+ )])
+ .unwrap();
+ let mut data = Vec::new();
+ let mut writer = ArrowWriter::try_new(&mut data, batch.schema(),
None).unwrap();
+ writer.write(&batch).unwrap();
+ writer.close().unwrap();
+ let size = data.len() as u64;
+ store.put(path, data.into()).await.unwrap();
+ size
+ }
+
+ fn max_value_of_a(metadata: &ParquetMetaData) -> i32 {
+ match metadata.row_group(0).column(0).statistics() {
+ Some(ParquetStatistics::Int32(stats)) => *stats.max_opt().unwrap(),
+ other => panic!("unexpected statistics {other:?}"),
+ }
+ }
Review Comment:
nit: Let's turn this into a lambda like "fetch", since I doubt this will be
used outside this specific test.
##########
docs/source/library-user-guide/upgrading/57.0.0.md:
##########
@@ -168,6 +168,69 @@ The trait already required `'static`, which `Any` implies,
so existing
implementations need no changes. `ParquetSource::parquet_file_reader_factory`
returns the trait object to downcast.
+### `FileMetadataCache` is keyed by object store and path
+
+`FileMetadataCache` previously used the file's `object_store::path::Path` as
+its key. A path is only unique within one object store, but one cache is shared
+by every store of a `RuntimeEnv`, so files with the same path in different
+stores (for example `s3://bucket-a/t/x.parquet` and
`s3://bucket-b/t/x.parquet`)
+could be served each other's metadata. The key is now `FileMetadataCacheKey`,
+which holds the `ObjectStoreUrl` of the store (without credentials) and the
+path:
+
+```diff
+- pub type FileMetadataCache = dyn Cache<Path, CachedFileMetadataEntry>;
++ pub type FileMetadataCache = dyn Cache<FileMetadataCacheKey,
CachedFileMetadataEntry>;
+```
+
+```rust
+# /* comment to avoid running
+let key = FileMetadataCacheKey::new(&object_store_url,
object_meta.location.clone());
+let entry = file_metadata_cache.get(&key);
+# */
+```
+
+Related changes:
+
+- `CachedParquetFileReaderFactory::new` takes the `ObjectStoreUrl` of the store
+ as a new second argument.
+- `DFParquetMetadata::with_file_metadata_cache` and
+ `ParquetFileReader::with_metadata_cache` take the `ObjectStoreUrl` of the
+ store together with the cache:
+
+ ```rust
+ # /* comment to avoid running
+ // Before
+ let metadata = DFParquetMetadata::new(store, object_meta)
+ .with_file_metadata_cache(Some(file_metadata_cache));
+ // After
+ let metadata = DFParquetMetadata::new(store, object_meta)
+ .with_file_metadata_cache(Some((file_metadata_cache, object_store_url)));
+ # */
+ ```
+
+- `ObjectStoreRegistry` has a new required method `get_store_url`, the reverse
+ of `get_store`. `ParquetFormat`'s `FileFormat` methods, which are only given
+ the `ObjectStore`, use it to find the store's URL, and skip the file metadata
+ cache when it returns `None`. Custom registries must implement it: forward it
+ to the registry they wrap, if any, or return the URL under which `store` was
+ registered. Returning `None` is correct, but disables caching of metadata
read
+ while inferring schemas and statistics.
+
+ ```rust
+ # /* comment to avoid running
+ impl ObjectStoreRegistry for MyRegistry {
+ // ...
+ fn get_store_url(&self, store: &Arc<dyn ObjectStore>) ->
Option<ObjectStoreUrl> {
+ self.inner.get_store_url(store)
+ }
+ }
+ # */
+ ```
+
+- The `metadata_cache` table function in `datafusion-cli` has a new first
+ column, `object_store_url`.
+
Review Comment:
Also first render the guide and check how it looks, currently this section
is not well formatted.
##########
docs/source/library-user-guide/upgrading/57.0.0.md:
##########
@@ -168,6 +168,69 @@ The trait already required `'static`, which `Any` implies,
so existing
implementations need no changes. `ParquetSource::parquet_file_reader_factory`
returns the trait object to downcast.
+### `FileMetadataCache` is keyed by object store and path
+
+`FileMetadataCache` previously used the file's `object_store::path::Path` as
+its key. A path is only unique within one object store, but one cache is shared
+by every store of a `RuntimeEnv`, so files with the same path in different
+stores (for example `s3://bucket-a/t/x.parquet` and
`s3://bucket-b/t/x.parquet`)
+could be served each other's metadata. The key is now `FileMetadataCacheKey`,
+which holds the `ObjectStoreUrl` of the store (without credentials) and the
+path:
+
+```diff
+- pub type FileMetadataCache = dyn Cache<Path, CachedFileMetadataEntry>;
++ pub type FileMetadataCache = dyn Cache<FileMetadataCacheKey,
CachedFileMetadataEntry>;
+```
+
+```rust
+# /* comment to avoid running
Review Comment:
Is this necessary?
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]