Skip to content

Commit 51960db

Browse files
authored
feat: Add expr_adapter_factory getter and with_expr_adapter_factory setter on ListingTable (apache#27317)
## Which issue does this PR close? Closes apache#27314. ## Rationale for this change In `ListingTable`, swapping or wrapping the `PhysicalExprAdapterFactory` of an existing table currently requires manually unpacking individual fields to construct a `ListingTableConfig` and calling `ListingTable::try_new(config)`. This is verbose, discards runtime table state (such as file statistics cache, table definitions, constraints, and column defaults), and redundantly re-evaluates schemas and fingerprints. While `ListingTableConfig` provides `with_expr_adapter_factory`, `ListingTable` was missing the matching getter and setter after `SchemaAdapterFactory` was replaced by `PhysicalExprAdapterFactory`. ## What changes are included in this PR? 1. Added `pub fn expr_adapter_factory(&self) -> Option<&Arc<dyn PhysicalExprAdapterFactory>>` to `ListingTable`. 2. Added `pub fn with_expr_adapter_factory(mut self, expr_adapter_factory: Arc<dyn PhysicalExprAdapterFactory>) -> Self` to `ListingTable`. 3. Added unit test in `datafusion/catalog-listing/src/table.rs` verifying adapter assignment, retrieval, and replacement on cloned tables without mutating the original table. ## Are these changes tested? Yes, tested via `test_with_expr_adapter_factory` in `datafusion-catalog-listing`. Verified clean formatting (`cargo fmt`) and zero clippy warnings.
1 parent ac19b2d commit 51960db

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

‎datafusion/catalog-listing/src/table.rs‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,20 @@ impl ListingTable {
342342
self
343343
}
344344

345+
/// Returns the [`PhysicalExprAdapterFactory`] used by this [`ListingTable`], if set.
346+
pub fn expr_adapter_factory(&self) -> Option<&Arc<dyn PhysicalExprAdapterFactory>> {
347+
self.expr_adapter_factory.as_ref()
348+
}
349+
350+
/// Set the [`PhysicalExprAdapterFactory`] for this [`ListingTable`].
351+
pub fn with_expr_adapter_factory(
352+
mut self,
353+
expr_adapter_factory: Arc<dyn PhysicalExprAdapterFactory>,
354+
) -> Self {
355+
self.expr_adapter_factory = Some(expr_adapter_factory);
356+
self
357+
}
358+
345359
/// Deprecated: Returns the [`SchemaAdapterFactory`] used by this [`ListingTable`].
346360
///
347361
/// `SchemaAdapterFactory` has been removed. Use `PhysicalExprAdapterFactory` instead.
@@ -1372,9 +1386,25 @@ fn prune_collected_files(
13721386
mod tests {
13731387
use super::*;
13741388
use arrow::compute::SortOptions;
1389+
use arrow::datatypes::Schema;
1390+
use datafusion_datasource_parquet::ParquetFormat;
13751391
use datafusion_physical_expr::expressions::Column;
1392+
use datafusion_physical_expr_adapter::DefaultPhysicalExprAdapterFactory;
13761393
use datafusion_physical_expr_common::sort_expr::PhysicalSortExpr;
13771394

1395+
fn create_test_listing_table() -> ListingTable {
1396+
let table_path = ListingTableUrl::parse("file:///test").unwrap();
1397+
let file_format = Arc::new(ParquetFormat::default());
1398+
let listing_options = ListingOptions::new(file_format);
1399+
let schema = Arc::new(Schema::empty());
1400+
1401+
let config = ListingTableConfig::new(table_path)
1402+
.with_listing_options(listing_options)
1403+
.with_schema(schema);
1404+
1405+
ListingTable::try_new(config).unwrap()
1406+
}
1407+
13781408
/// Helper to create a PhysicalSortExpr
13791409
fn sort_expr(
13801410
name: &str,
@@ -1662,4 +1692,34 @@ mod tests {
16621692
["f0"]
16631693
);
16641694
}
1695+
1696+
#[test]
1697+
fn test_with_expr_adapter_factory() {
1698+
let factory1: Arc<dyn PhysicalExprAdapterFactory> =
1699+
Arc::new(DefaultPhysicalExprAdapterFactory);
1700+
let factory2: Arc<dyn PhysicalExprAdapterFactory> =
1701+
Arc::new(DefaultPhysicalExprAdapterFactory);
1702+
1703+
let table =
1704+
create_test_listing_table().with_expr_adapter_factory(Arc::clone(&factory1));
1705+
1706+
assert!(Arc::ptr_eq(
1707+
table.expr_adapter_factory().unwrap(),
1708+
&factory1
1709+
));
1710+
1711+
let updated_table = table
1712+
.clone()
1713+
.with_expr_adapter_factory(Arc::clone(&factory2));
1714+
1715+
assert!(Arc::ptr_eq(
1716+
updated_table.expr_adapter_factory().unwrap(),
1717+
&factory2
1718+
));
1719+
1720+
assert!(Arc::ptr_eq(
1721+
table.expr_adapter_factory().unwrap(),
1722+
&factory1
1723+
));
1724+
}
16651725
}

0 commit comments

Comments
 (0)