Skip to content

Commit 5fcb927

Browse files
committed
feat: add UPDATE tests , filter pushdown support to CaptureUpdateProvider and corresponding tests
1 parent b3240bf commit 5fcb927

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

‎datafusion/core/tests/custom_sources_cases/dml_planning.rs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ struct CaptureUpdateProvider {
122122
schema: SchemaRef,
123123
received_filters: Arc<Mutex<Option<Vec<Expr>>>>,
124124
received_assignments: Arc<Mutex<Option<Vec<(String, Expr)>>>>,
125+
filter_pushdown: TableProviderFilterPushDown,
125126
}
126127

127128
impl CaptureUpdateProvider {
@@ -130,6 +131,19 @@ impl CaptureUpdateProvider {
130131
schema,
131132
received_filters: Arc::new(Mutex::new(None)),
132133
received_assignments: Arc::new(Mutex::new(None)),
134+
filter_pushdown: TableProviderFilterPushDown::Unsupported,
135+
}
136+
}
137+
138+
fn new_with_filter_pushdown(
139+
schema: SchemaRef,
140+
filter_pushdown: TableProviderFilterPushDown,
141+
) -> Self {
142+
Self {
143+
schema,
144+
received_filters: Arc::new(Mutex::new(None)),
145+
received_assignments: Arc::new(Mutex::new(None)),
146+
filter_pushdown,
133147
}
134148
}
135149

@@ -186,6 +200,13 @@ impl TableProvider for CaptureUpdateProvider {
186200
Field::new("count", DataType::UInt64, false),
187201
])))))
188202
}
203+
204+
fn supports_filters_pushdown(
205+
&self,
206+
filters: &[&Expr],
207+
) -> Result<Vec<TableProviderFilterPushDown>> {
208+
Ok(vec![self.filter_pushdown.clone(); filters.len()])
209+
}
189210
}
190211

191212
fn test_schema() -> SchemaRef {
@@ -361,6 +382,41 @@ async fn test_update_assignments() -> Result<()> {
361382
Ok(())
362383
}
363384

385+
#[tokio::test]
386+
async fn test_update_filter_pushdown_extracts_table_scan_filters() -> Result<()> {
387+
let provider = Arc::new(CaptureUpdateProvider::new_with_filter_pushdown(
388+
test_schema(),
389+
TableProviderFilterPushDown::Exact,
390+
));
391+
let ctx = SessionContext::new();
392+
ctx.register_table("t", Arc::clone(&provider) as Arc<dyn TableProvider>)?;
393+
394+
let df = ctx.sql("UPDATE t SET value = 100 WHERE id = 1").await?;
395+
let optimized_plan = df.clone().into_optimized_plan()?;
396+
397+
// Verify that the optimizer pushed down the filter into TableScan
398+
let mut scan_filters = Vec::new();
399+
optimized_plan.apply(|node| {
400+
if let LogicalPlan::TableScan(TableScan { filters, .. }) = node {
401+
scan_filters.extend(filters.clone());
402+
}
403+
Ok(TreeNodeRecursion::Continue)
404+
})?;
405+
406+
assert_eq!(scan_filters.len(), 1);
407+
assert!(scan_filters[0].to_string().contains("id"));
408+
409+
// Execute the UPDATE and verify filters were extracted and passed to update()
410+
df.collect().await?;
411+
412+
let filters = provider
413+
.captured_filters()
414+
.expect("filters should be captured");
415+
assert_eq!(filters.len(), 1);
416+
assert!(filters[0].to_string().contains("id"));
417+
Ok(())
418+
}
419+
364420
#[tokio::test]
365421
async fn test_unsupported_table_delete() -> Result<()> {
366422
let schema = test_schema();

0 commit comments

Comments
 (0)