Skip to content

Commit 4d86ae0

Browse files
fix: pre-warm listing file statistics cache during listing table creation (#18971)
Pre-warm listing file statistics cache during create listing table flow as suggested in #18952. Reused `list_files_for_scan` to pre-warm. ## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #18952. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes unit tested. ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> --------- Signed-off-by: bharath-techie <[email protected]> Co-authored-by: Andrew Lamb <[email protected]>
1 parent 5842e23 commit 4d86ae0

File tree

1 file changed

+108
-5
lines changed

1 file changed

+108
-5
lines changed

datafusion/core/src/datasource/listing_table_factory.rs

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ impl TableProviderFactory for ListingTableFactory {
190190
.with_definition(cmd.definition.clone())
191191
.with_constraints(cmd.constraints.clone())
192192
.with_column_defaults(cmd.column_defaults.clone());
193+
194+
// Pre-warm statistics cache if collect_statistics is enabled
195+
if session_state.config().collect_statistics() {
196+
let filters = &[];
197+
let limit = None;
198+
if let Err(e) = table.list_files_for_scan(state, filters, limit).await {
199+
log::warn!("Failed to pre-warm statistics cache: {e}");
200+
}
201+
}
202+
193203
Ok(Arc::new(table))
194204
}
195205
}
@@ -205,17 +215,21 @@ fn get_extension(path: &str) -> String {
205215

206216
#[cfg(test)]
207217
mod tests {
218+
use super::*;
219+
use crate::{
220+
datasource::file_format::csv::CsvFormat, execution::context::SessionContext,
221+
test_util::parquet_test_data,
222+
};
223+
use datafusion_execution::cache::cache_manager::CacheManagerConfig;
224+
use datafusion_execution::cache::cache_unit::DefaultFileStatisticsCache;
225+
use datafusion_execution::cache::CacheAccessor;
208226
use datafusion_execution::config::SessionConfig;
227+
use datafusion_execution::runtime_env::RuntimeEnvBuilder;
209228
use glob::Pattern;
210229
use std::collections::HashMap;
211230
use std::fs;
212231
use std::path::PathBuf;
213232

214-
use super::*;
215-
use crate::{
216-
datasource::file_format::csv::CsvFormat, execution::context::SessionContext,
217-
};
218-
219233
use datafusion_common::parsers::CompressionTypeVariant;
220234
use datafusion_common::{Constraints, DFSchema, TableReference};
221235

@@ -519,4 +533,93 @@ mod tests {
519533
let listing_options = listing_table.options();
520534
assert!(listing_options.table_partition_cols.is_empty());
521535
}
536+
537+
#[tokio::test]
538+
async fn test_statistics_cache_prewarming() {
539+
let factory = ListingTableFactory::new();
540+
541+
let location = PathBuf::from(parquet_test_data())
542+
.join("alltypes_tiny_pages_plain.parquet")
543+
.to_string_lossy()
544+
.to_string();
545+
546+
// Test with collect_statistics enabled
547+
let file_statistics_cache = Arc::new(DefaultFileStatisticsCache::default());
548+
let cache_config = CacheManagerConfig::default()
549+
.with_files_statistics_cache(Some(file_statistics_cache.clone()));
550+
let runtime = RuntimeEnvBuilder::new()
551+
.with_cache_manager(cache_config)
552+
.build_arc()
553+
.unwrap();
554+
555+
let mut config = SessionConfig::new();
556+
config.options_mut().execution.collect_statistics = true;
557+
let context = SessionContext::new_with_config_rt(config, runtime);
558+
let state = context.state();
559+
let name = TableReference::bare("test");
560+
561+
let cmd = CreateExternalTable {
562+
name,
563+
location: location.clone(),
564+
file_type: "parquet".to_string(),
565+
schema: Arc::new(DFSchema::empty()),
566+
table_partition_cols: vec![],
567+
if_not_exists: false,
568+
or_replace: false,
569+
temporary: false,
570+
definition: None,
571+
order_exprs: vec![],
572+
unbounded: false,
573+
options: HashMap::new(),
574+
constraints: Constraints::default(),
575+
column_defaults: HashMap::new(),
576+
};
577+
578+
let _table_provider = factory.create(&state, &cmd).await.unwrap();
579+
580+
assert!(
581+
file_statistics_cache.len() > 0,
582+
"Statistics cache should be pre-warmed when collect_statistics is enabled"
583+
);
584+
585+
// Test with collect_statistics disabled
586+
let file_statistics_cache = Arc::new(DefaultFileStatisticsCache::default());
587+
let cache_config = CacheManagerConfig::default()
588+
.with_files_statistics_cache(Some(file_statistics_cache.clone()));
589+
let runtime = RuntimeEnvBuilder::new()
590+
.with_cache_manager(cache_config)
591+
.build_arc()
592+
.unwrap();
593+
594+
let mut config = SessionConfig::new();
595+
config.options_mut().execution.collect_statistics = false;
596+
let context = SessionContext::new_with_config_rt(config, runtime);
597+
let state = context.state();
598+
let name = TableReference::bare("test");
599+
600+
let cmd = CreateExternalTable {
601+
name,
602+
location,
603+
file_type: "parquet".to_string(),
604+
schema: Arc::new(DFSchema::empty()),
605+
table_partition_cols: vec![],
606+
if_not_exists: false,
607+
or_replace: false,
608+
temporary: false,
609+
definition: None,
610+
order_exprs: vec![],
611+
unbounded: false,
612+
options: HashMap::new(),
613+
constraints: Constraints::default(),
614+
column_defaults: HashMap::new(),
615+
};
616+
617+
let _table_provider = factory.create(&state, &cmd).await.unwrap();
618+
619+
assert_eq!(
620+
file_statistics_cache.len(),
621+
0,
622+
"Statistics cache should not be pre-warmed when collect_statistics is disabled"
623+
);
624+
}
522625
}

0 commit comments

Comments
 (0)