1717
1818use std:: sync:: Arc ;
1919
20- use datafusion_common:: { DataFusionError , Result } ;
20+ use datafusion_common:: { DataFusionError , Result , TableReference } ;
21+ use datafusion_execution:: cache:: TableScopedPath ;
2122use datafusion_execution:: object_store:: ObjectStoreUrl ;
2223use datafusion_session:: Session ;
2324
@@ -41,6 +42,8 @@ pub struct ListingTableUrl {
4142 prefix : Path ,
4243 /// An optional glob expression used to filter files
4344 glob : Option < Pattern > ,
45+
46+ table_ref : Option < TableReference > ,
4447}
4548
4649impl ListingTableUrl {
@@ -145,7 +148,12 @@ impl ListingTableUrl {
145148 /// to create a [`ListingTableUrl`].
146149 pub fn try_new ( url : Url , glob : Option < Pattern > ) -> Result < Self > {
147150 let prefix = Path :: from_url_path ( url. path ( ) ) ?;
148- Ok ( Self { url, prefix, glob } )
151+ Ok ( Self {
152+ url,
153+ prefix,
154+ glob,
155+ table_ref : None ,
156+ } )
149157 }
150158
151159 /// Returns the URL scheme
@@ -255,7 +263,14 @@ impl ListingTableUrl {
255263 } ;
256264
257265 let list: BoxStream < ' a , Result < ObjectMeta > > = if self . is_collection ( ) {
258- list_with_cache ( ctx, store, & self . prefix , prefix. as_ref ( ) ) . await ?
266+ list_with_cache (
267+ ctx,
268+ store,
269+ self . table_ref . as_ref ( ) ,
270+ & self . prefix ,
271+ prefix. as_ref ( ) ,
272+ )
273+ . await ?
259274 } else {
260275 match store. head ( & full_prefix) . await {
261276 Ok ( meta) => futures:: stream:: once ( async { Ok ( meta) } )
@@ -264,7 +279,14 @@ impl ListingTableUrl {
264279 // If the head command fails, it is likely that object doesn't exist.
265280 // Retry as though it were a prefix (aka a collection)
266281 Err ( object_store:: Error :: NotFound { .. } ) => {
267- list_with_cache ( ctx, store, & self . prefix , prefix. as_ref ( ) ) . await ?
282+ list_with_cache (
283+ ctx,
284+ store,
285+ self . table_ref . as_ref ( ) ,
286+ & self . prefix ,
287+ prefix. as_ref ( ) ,
288+ )
289+ . await ?
268290 }
269291 Err ( e) => return Err ( e. into ( ) ) ,
270292 }
@@ -323,6 +345,15 @@ impl ListingTableUrl {
323345 Pattern :: new ( glob) . map_err ( |e| DataFusionError :: External ( Box :: new ( e) ) ) ?;
324346 Self :: try_new ( self . url , Some ( glob) )
325347 }
348+
349+ pub fn with_table_ref ( mut self , table_ref : TableReference ) -> Self {
350+ self . table_ref = Some ( table_ref) ;
351+ self
352+ }
353+
354+ pub fn get_table_ref ( & self ) -> & Option < TableReference > {
355+ & self . table_ref
356+ }
326357}
327358
328359/// Lists files with cache support, using prefix-aware lookups.
@@ -345,6 +376,7 @@ impl ListingTableUrl {
345376async fn list_with_cache < ' b > (
346377 ctx : & ' b dyn Session ,
347378 store : & ' b dyn ObjectStore ,
379+ table_ref : Option < & TableReference > ,
348380 table_base_path : & Path ,
349381 prefix : Option < & Path > ,
350382) -> Result < BoxStream < ' b , Result < ObjectMeta > > > {
@@ -367,9 +399,16 @@ async fn list_with_cache<'b>(
367399 // Convert prefix to Option<Path> for cache lookup
368400 let prefix_filter = prefix. cloned ( ) ;
369401
402+ println ! ( "list files cache is set" ) ;
403+
404+ let table_scoped_base_path =
405+ TableScopedPath ( table_ref. cloned ( ) , table_base_path. clone ( ) ) ;
406+
407+ println ! ( "table scoped path {table_scoped_base_path:?}" ) ;
408+
370409 // Try cache lookup with optional prefix filter
371410 let vec = if let Some ( res) =
372- cache. get_with_extra ( table_base_path , & prefix_filter)
411+ cache. get_with_extra ( & table_scoped_base_path , & prefix_filter)
373412 {
374413 debug ! ( "Hit list files cache" ) ;
375414 res. as_ref ( ) . clone ( )
@@ -380,7 +419,7 @@ async fn list_with_cache<'b>(
380419 . list ( Some ( table_base_path) )
381420 . try_collect :: < Vec < ObjectMeta > > ( )
382421 . await ?;
383- cache. put ( table_base_path , Arc :: new ( vec. clone ( ) ) ) ;
422+ cache. put ( & table_scoped_base_path , Arc :: new ( vec. clone ( ) ) ) ;
384423
385424 // If a prefix filter was requested, apply it to the results
386425 if prefix. is_some ( ) {
0 commit comments