Apache DataFusion SQL Query Engine
Provide a builder for ListingOptions with fixups (#4207)
Andrew Lambc9361e021086
diff --git a/benchmarks/src/bin/tpch.rs b/benchmarks/src/bin/tpch.rsindex 4517016af7..8a1e3c2edf 100644--- a/benchmarks/src/bin/tpch.rs+++ b/benchmarks/src/bin/tpch.rs@@ -391,14 +391,11 @@ async fn get_table(};let schema = Arc::new(get_tpch_table_schema(table));- let options = ListingOptions {- format,- file_extension: extension.to_owned(),- target_partitions,- collect_stat: ctx.config.collect_statistics,- table_partition_cols: vec![],- file_sort_order: None,- };+ let options = ListingOptions::new(format)+ .with_file_extension(extension)+ .with_target_partitions(target_partitions)+ .with_collect_stat(ctx.config.collect_statistics)+ .with_table_partition_cols(vec![]);let table_path = ListingTableUrl::parse(path)?;let config = ListingTableConfig::new(table_path).with_listing_options(options);diff --git a/datafusion-examples/examples/parquet_sql_multiple_files.rs b/datafusion-examples/examples/parquet_sql_multiple_files.rsindex 6e8ef2397a..a85f2bf5d1 100644--- a/datafusion-examples/examples/parquet_sql_multiple_files.rs+++ b/datafusion-examples/examples/parquet_sql_multiple_files.rs@@ -33,14 +33,11 @@ async fn main() -> Result<()> {// Configure listing optionslet file_format = ParquetFormat::default().with_enable_pruning(true);- let listing_options = ListingOptions {- file_extension: FileType::PARQUET.get_ext(),- format: Arc::new(file_format),- table_partition_cols: vec![],- collect_stat: true,- target_partitions: 1,- file_sort_order: None,- };+ let listing_options = ListingOptions::new(Arc::new(file_format))+ .with_file_extension(FileType::PARQUET.get_ext())+ .with_table_partition_cols(vec![])+ .with_collect_stat(true)+ .with_target_partitions(1);// Register a listing table - this will use all files in the directory as data sources// for the querydiff --git a/datafusion/core/src/datasource/listing/table.rs b/datafusion/core/src/datasource/listing/table.rsindex ab08d6f184..55a06725e7 100644--- a/datafusion/core/src/datasource/listing/table.rs+++ b/datafusion/core/src/datasource/listing/table.rs@@ -254,6 +254,96 @@ impl ListingOptions {file_sort_order: None,}}+ /// Set file extension on [`ListingOptions`] and returns self.+ ///+ /// ```+ /// use std::sync::Arc;+ /// use datafusion::datasource::{listing::ListingOptions, file_format::parquet::ParquetFormat};+ ///+ /// let listing_options = ListingOptions::new(Arc::new(ParquetFormat::default()))+ /// .with_file_extension(".parquet");+ ///+ /// assert_eq!(listing_options.file_extension, ".parquet");+ /// ```+ pub fn with_file_extension(mut self, file_extension: impl Into<String>) -> Self {+ self.file_extension = file_extension.into();+ self+ }++ /// Set table partition column names on [`ListingOptions`] and returns self.+ ///+ /// ```+ /// use std::sync::Arc;+ /// use datafusion::datasource::{listing::ListingOptions, file_format::parquet::ParquetFormat};+ ///+ /// let listing_options = ListingOptions::new(Arc::new(ParquetFormat::default()))+ /// .with_table_partition_cols(vec!["col_a".to_string(), "col_b".to_string()]);+ ///+ /// assert_eq!(listing_options.table_partition_cols, vec!["col_a", "col_b"]);+ /// ```+ pub fn with_table_partition_cols(+ mut self,+ table_partition_cols: Vec<String>,+ ) -> Self {+ self.table_partition_cols = table_partition_cols;+ self+ }++ /// Set stat collection on [`ListingOptions`] and returns self.+ ///+ /// ```+ /// use std::sync::Arc;+ /// use datafusion::datasource::{listing::ListingOptions, file_format::parquet::ParquetFormat};+ ///+ /// // Enable stat collection+ /// let listing_options = ListingOptions::new(Arc::new(ParquetFormat::default()))+ /// .with_collect_stat(true);+ ///+ /// assert_eq!(listing_options.collect_stat, true);+ /// ```+ pub fn with_collect_stat(mut self, collect_stat: bool) -> Self {+ self.collect_stat = collect_stat;+ self+ }++ /// Set number of target partitions on [`ListingOptions`] and returns self.+ ///+ /// ```+ /// use std::sync::Arc;+ /// use datafusion::datasource::{listing::ListingOptions, file_format::parquet::ParquetFormat};+ ///+ /// let listing_options = ListingOptions::new(Arc::new(ParquetFormat::default()))+ /// .with_target_partitions(8);+ ///+ /// assert_eq!(listing_options.target_partitions, 8);+ /// ```+ pub fn with_target_partitions(mut self, target_partitions: usize) -> Self {+ self.target_partitions = target_partitions;+ self+ }++ /// Set number of target partitions on [`ListingOptions`] and returns self.+ ///+ /// ```+ /// use std::sync::Arc;+ /// use datafusion::prelude::{Expr, col};+ /// use datafusion::datasource::{listing::ListingOptions, file_format::parquet::ParquetFormat};+ ///+ ///+ /// // Tell datafusion that the files are sorted by column "a"+ /// let file_sort_order = Some(vec![+ /// col("a").sort(true, true)+ /// ]);+ ///+ /// let listing_options = ListingOptions::new(Arc::new(ParquetFormat::default()))+ /// .with_file_sort_order(file_sort_order.clone());+ ///+ /// assert_eq!(listing_options.file_sort_order, file_sort_order);+ /// ```+ pub fn with_file_sort_order(mut self, file_sort_order: Option<Vec<Expr>>) -> Self {+ self.file_sort_order = file_sort_order;+ self+ }/// Infer the schema of the files at the given path on the provided object store./// The inferred schema does not include the partitioning columns.@@ -721,14 +811,11 @@ mod tests {let path = String::from("table/p1=v1/file.avro");register_test_store(&ctx, &[(&path, 100)]);- let opt = ListingOptions {- file_extension: FileType::AVRO.get_ext(),- format: Arc::new(AvroFormat {}),- table_partition_cols: vec![String::from("p1")],- target_partitions: 4,- collect_stat: true,- file_sort_order: None,- };+ let opt = ListingOptions::new(Arc::new(AvroFormat {}))+ .with_file_extension(FileType::AVRO.get_ext())+ .with_table_partition_cols(vec![String::from("p1")])+ .with_target_partitions(4)+ .with_collect_stat(true);let table_path = ListingTableUrl::parse("test:///table/").unwrap();let file_schema =@@ -924,14 +1011,11 @@ mod tests {let format = AvroFormat {};- let opt = ListingOptions {- file_extension: "".to_owned(),- format: Arc::new(format),- table_partition_cols: vec![],- target_partitions,- collect_stat: true,- file_sort_order: None,- };+ let opt = ListingOptions::new(Arc::new(format))+ .with_file_extension("")+ .with_table_partition_cols(vec![])+ .with_target_partitions(target_partitions)+ .with_collect_stat(true);let schema = Schema::new(vec![Field::new("a", DataType::Boolean, false)]);@@ -962,14 +1046,11 @@ mod tests {let format = AvroFormat {};- let opt = ListingOptions {- file_extension: "".to_owned(),- format: Arc::new(format),- table_partition_cols: vec![],- target_partitions,- collect_stat: true,- file_sort_order: None,- };+ let opt = ListingOptions::new(Arc::new(format))+ .with_file_extension("")+ .with_table_partition_cols(vec![])+ .with_target_partitions(target_partitions)+ .with_collect_stat(true);let schema = Schema::new(vec![Field::new("a", DataType::Boolean, false)]);diff --git a/datafusion/core/src/datasource/listing_table_factory.rs b/datafusion/core/src/datasource/listing_table_factory.rsindex f57ffa9db9..eacd63f949 100644--- a/datafusion/core/src/datasource/listing_table_factory.rs+++ b/datafusion/core/src/datasource/listing_table_factory.rs@@ -61,14 +61,11 @@ impl TableProviderFactory for ListingTableFactory {FileType::JSON => Arc::new(JsonFormat::default()),};- let options = ListingOptions {- format: file_format,- collect_stat: true,- file_extension: file_extension.to_owned(),- target_partitions: 1,- table_partition_cols: vec![],- file_sort_order: None,- };+ let options = ListingOptions::new(file_format)+ .with_collect_stat(true)+ .with_file_extension(file_extension)+ .with_target_partitions(1)+ .with_table_partition_cols(vec![]);let table_path = ListingTableUrl::parse(&cmd.location)?;let resolved_schema = options.infer_schema(state, &table_path).await?;diff --git a/datafusion/core/src/execution/context.rs b/datafusion/core/src/execution/context.rsindex 8b7bf1ffb2..216e49f4a5 100644--- a/datafusion/core/src/execution/context.rs+++ b/datafusion/core/src/execution/context.rs@@ -558,14 +558,12 @@ impl SessionContext {} else {Some(Arc::new(cmd.schema.as_ref().to_owned().into()))};- let options = ListingOptions {- format: file_format,- collect_stat: self.copied_config().collect_statistics,- file_extension: file_extension.to_owned(),- target_partitions: self.copied_config().target_partitions,- table_partition_cols: cmd.table_partition_cols.clone(),- file_sort_order: None,- };+ let options = ListingOptions::new(file_format)+ .with_collect_stat(self.copied_config().collect_statistics)+ .with_file_extension(file_extension)+ .with_target_partitions(self.copied_config().target_partitions)+ .with_table_partition_cols(cmd.table_partition_cols.clone());+self.register_listing_table(cmd.name.as_str(),cmd.location.clone(),diff --git a/datafusion/core/src/execution/options.rs b/datafusion/core/src/execution/options.rsindex 041639f0fd..aa04c92f53 100644--- a/datafusion/core/src/execution/options.rs+++ b/datafusion/core/src/execution/options.rs@@ -145,14 +145,11 @@ impl<'a> CsvReadOptions<'a> {.with_schema_infer_max_rec(Some(self.schema_infer_max_records)).with_file_compression_type(self.file_compression_type.to_owned());- ListingOptions {- format: Arc::new(file_format),- collect_stat: false,- file_extension: self.file_extension.to_owned(),- target_partitions,- table_partition_cols: self.table_partition_cols.clone(),- file_sort_order: None,- }+ ListingOptions::new(Arc::new(file_format))+ .with_collect_stat(false)+ .with_file_extension(self.file_extension)+ .with_target_partitions(target_partitions)+ .with_table_partition_cols(self.table_partition_cols.clone())}}@@ -220,14 +217,11 @@ impl<'a> ParquetReadOptions<'a> {.with_enable_pruning(self.parquet_pruning).with_skip_metadata(self.skip_metadata);- ListingOptions {- format: Arc::new(file_format),- collect_stat: true,- file_extension: self.file_extension.to_owned(),- target_partitions,- table_partition_cols: self.table_partition_cols.clone(),- file_sort_order: None,- }+ ListingOptions::new(Arc::new(file_format))+ .with_collect_stat(true)+ .with_file_extension(self.file_extension)+ .with_target_partitions(target_partitions)+ .with_table_partition_cols(self.table_partition_cols.clone())}}@@ -270,14 +264,11 @@ impl<'a> AvroReadOptions<'a> {pub fn to_listing_options(&self, target_partitions: usize) -> ListingOptions {let file_format = AvroFormat::default();- ListingOptions {- format: Arc::new(file_format),- collect_stat: false,- file_extension: self.file_extension.to_owned(),- target_partitions,- table_partition_cols: self.table_partition_cols.clone(),- file_sort_order: None,- }+ ListingOptions::new(Arc::new(file_format))+ .with_collect_stat(false)+ .with_file_extension(self.file_extension)+ .with_target_partitions(target_partitions)+ .with_table_partition_cols(self.table_partition_cols.clone())}}@@ -343,13 +334,11 @@ impl<'a> NdJsonReadOptions<'a> {pub fn to_listing_options(&self, target_partitions: usize) -> ListingOptions {let file_format = JsonFormat::default().with_file_compression_type(self.file_compression_type.to_owned());- ListingOptions {- format: Arc::new(file_format),- collect_stat: false,- file_extension: self.file_extension.to_owned(),- target_partitions,- table_partition_cols: self.table_partition_cols.clone(),- file_sort_order: None,- }++ ListingOptions::new(Arc::new(file_format))+ .with_collect_stat(false)+ .with_file_extension(self.file_extension)+ .with_target_partitions(target_partitions)+ .with_table_partition_cols(self.table_partition_cols.clone())}}diff --git a/datafusion/core/tests/path_partition.rs b/datafusion/core/tests/path_partition.rsindex 2d7e783f15..cabcae62ea 100644--- a/datafusion/core/tests/path_partition.rs+++ b/datafusion/core/tests/path_partition.rs@@ -434,8 +434,10 @@ fn register_partitioned_aggregate_csv(MirroringObjectStore::new_arc(csv_file_path, store_paths),);- let mut options = ListingOptions::new(Arc::new(CsvFormat::default()));- options.table_partition_cols = partition_cols.iter().map(|&s| s.to_owned()).collect();+ let options = ListingOptions::new(Arc::new(CsvFormat::default()))+ .with_table_partition_cols(+ partition_cols.iter().map(|&s| s.to_owned()).collect(),+ );let table_path = ListingTableUrl::parse(table_path).unwrap();let config = ListingTableConfig::new(table_path)@@ -462,9 +464,9 @@ async fn register_partitioned_alltypes_parquet(MirroringObjectStore::new_arc(parquet_file_path.clone(), store_paths),);- let mut options = ListingOptions::new(Arc::new(ParquetFormat::default()));- options.table_partition_cols = partition_cols.iter().map(|&s| s.to_owned()).collect();- options.collect_stat = true;+ let options = ListingOptions::new(Arc::new(ParquetFormat::default()))+ .with_table_partition_cols(partition_cols.iter().map(|&s| s.to_owned()).collect())+ .with_collect_stat(true);let table_path = ListingTableUrl::parse(table_path).unwrap();let store_path =diff --git a/datafusion/proto/src/logical_plan.rs b/datafusion/proto/src/logical_plan.rsindex f3c4c678b8..9ab754c921 100644--- a/datafusion/proto/src/logical_plan.rs+++ b/datafusion/proto/src/logical_plan.rs@@ -459,14 +459,12 @@ impl AsLogicalPlan for LogicalPlanNode {.map(ListingTableUrl::parse).collect::<Result<Vec<_>, _>>()?;- let options = ListingOptions {- file_extension: scan.file_extension.clone(),- format: file_format,- table_partition_cols: scan.table_partition_cols.clone(),- collect_stat: scan.collect_stat,- target_partitions: scan.target_partitions as usize,- file_sort_order,- };+ let options = ListingOptions::new(file_format)+ .with_file_extension(scan.file_extension.clone())+ .with_table_partition_cols(scan.table_partition_cols.clone())+ .with_collect_stat(scan.collect_stat)+ .with_target_partitions(scan.target_partitions as usize)+ .with_file_sort_order(file_sort_order);let config =ListingTableConfig::new_with_multi_paths(table_paths.clone())