Skip to content

Apache DataFusion SQL Query Engine

Minor: add `ListingOptions::with_file_extension_opt` (#12461)

Andrew Lamb88b5970094d7
diff --git a/datafusion/core/src/datasource/listing/table.rs b/datafusion/core/src/datasource/listing/table.rs
index 9246226d43..225995ca4f 100644
--- a/datafusion/core/src/datasource/listing/table.rs
+++ b/datafusion/core/src/datasource/listing/table.rs
@@ -245,6 +245,7 @@ impl ListingOptions {
/// Set file extension on [`ListingOptions`] and returns self.
///
+ /// # Example
/// ```
/// # use std::sync::Arc;
/// # use datafusion::prelude::SessionContext;
@@ -262,6 +263,33 @@ impl ListingOptions {
self
}
+ /// Optionally set file extension on [`ListingOptions`] and returns self.
+ ///
+ /// If `file_extension` is `None`, the file extension will not be changed
+ ///
+ /// # Example
+ /// ```
+ /// # use std::sync::Arc;
+ /// # use datafusion::prelude::SessionContext;
+ /// # use datafusion::datasource::{listing::ListingOptions, file_format::parquet::ParquetFormat};
+ /// let extension = Some(".parquet");
+ /// let listing_options = ListingOptions::new(Arc::new(
+ /// ParquetFormat::default()
+ /// ))
+ /// .with_file_extension_opt(extension);
+ ///
+ /// assert_eq!(listing_options.file_extension, ".parquet");
+ /// ```
+ pub fn with_file_extension_opt<S>(mut self, file_extension: Option<S>) -> Self
+ where
+ S: Into<String>,
+ {
+ if let Some(file_extension) = file_extension {
+ self.file_extension = file_extension.into();
+ }
+ self
+ }
+
/// Set `table partition columns` on [`ListingOptions`] and returns self.
///
/// "partition columns," used to support [Hive Partitioning], are