Skip to content

Commit 771310b

Browse files
committed
test: add skipped UPDATE ... FROM coverage for issue apache#19950
Extract test cases from the original apache#20745 change set into a tests-only PR. - add skipped Rust tests for UPDATE ... FROM planning and filter extraction - add skipped SQL integration coverage for original target-row projection - preserve SQLLogicTest cases as commented-out TODO(19950) coverage - add only minimal compile-time test scaffolding, without the implementation fix
1 parent 92078d9 commit 771310b

4 files changed

Lines changed: 652 additions & 4 deletions

File tree

‎datafusion/core/src/physical_planner.rs‎

Lines changed: 228 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,8 @@ impl DefaultPhysicalPlanner {
785785
// We pass the filters and let the provider handle the projection
786786
let filters = extract_dml_filters(input, table_name)?;
787787
// Extract assignments from the projection in input plan
788-
let assignments = extract_update_assignments(input)?;
788+
let assignments =
789+
extract_update_assignments(input, table_name)?;
789790
provider
790791
.table_provider
791792
.update(session_state, assignments, filters)
@@ -2235,7 +2236,10 @@ fn strip_column_qualifiers(expr: Expr) -> Result<Expr> {
22352236
/// over the source table. This function extracts column name and expression pairs
22362237
/// from the projection. Column qualifiers are stripped from the expressions.
22372238
///
2238-
fn extract_update_assignments(input: &Arc<LogicalPlan>) -> Result<Vec<(String, Expr)>> {
2239+
fn extract_update_assignments(
2240+
input: &Arc<LogicalPlan>,
2241+
_target_table: &TableReference,
2242+
) -> Result<Vec<(String, Expr)>> {
22392243
// The UPDATE input plan structure is:
22402244
// Projection(updated columns as expressions with aliases)
22412245
// Filter(optional WHERE clause)
@@ -3115,7 +3119,8 @@ mod tests {
31153119

31163120
use crate::execution::session_state::SessionStateBuilder;
31173121
use arrow::array::{ArrayRef, DictionaryArray, Int32Array};
3118-
use arrow::datatypes::{DataType, Field, Int32Type};
3122+
use arrow::datatypes::{DataType, Field, Int32Type, Schema};
3123+
use arrow::record_batch::RecordBatch;
31193124
use arrow_schema::SchemaRef;
31203125
use datafusion_common::config::ConfigOptions;
31213126
use datafusion_common::{
@@ -3125,13 +3130,79 @@ mod tests {
31253130
use datafusion_execution::runtime_env::RuntimeEnv;
31263131
use datafusion_expr::builder::subquery_alias;
31273132
use datafusion_expr::{
3128-
LogicalPlanBuilder, TableSource, UserDefinedLogicalNodeCore, col, lit,
3133+
DmlStatement, LogicalPlanBuilder, TableSource, UserDefinedLogicalNodeCore,
3134+
WriteOp, col, lit,
31293135
};
31303136
use datafusion_functions_aggregate::count::count_all;
31313137
use datafusion_functions_aggregate::expr_fn::sum;
31323138
use datafusion_physical_expr::EquivalenceProperties;
31333139
use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType};
31343140

3141+
async fn make_update_from_plan(
3142+
sql: &str,
3143+
) -> Result<(Arc<LogicalPlan>, TableReference)> {
3144+
let ctx = SessionContext::new();
3145+
let t1_schema = Arc::new(Schema::new(vec![
3146+
Field::new("a", DataType::Int32, false),
3147+
Field::new("b", DataType::Utf8, true),
3148+
Field::new("c", DataType::Float64, true),
3149+
Field::new("d", DataType::Int32, true),
3150+
]));
3151+
let t2_schema = Arc::new(Schema::new(vec![
3152+
Field::new("a", DataType::Int32, false),
3153+
Field::new("b", DataType::Utf8, true),
3154+
Field::new("c", DataType::Float64, true),
3155+
Field::new("d", DataType::Int32, true),
3156+
]));
3157+
let t1 = MemTable::try_new(
3158+
Arc::clone(&t1_schema),
3159+
vec![vec![RecordBatch::new_empty(Arc::clone(&t1_schema))]],
3160+
)?;
3161+
let t2 = MemTable::try_new(
3162+
Arc::clone(&t2_schema),
3163+
vec![vec![RecordBatch::new_empty(Arc::clone(&t2_schema))]],
3164+
)?;
3165+
ctx.register_table("t1", Arc::new(t1))?;
3166+
ctx.register_table("t2", Arc::new(t2))?;
3167+
3168+
let plan = ctx.sql(sql).await?.into_unoptimized_plan();
3169+
match plan {
3170+
LogicalPlan::Dml(DmlStatement {
3171+
table_name,
3172+
op: WriteOp::Update,
3173+
input,
3174+
..
3175+
}) => Ok((input, table_name)),
3176+
other => internal_err!("Expected UPDATE DML plan, got: {other}"),
3177+
}
3178+
}
3179+
3180+
async fn make_delete_plan(sql: &str) -> Result<(Arc<LogicalPlan>, TableReference)> {
3181+
let ctx = SessionContext::new();
3182+
let t1_schema = Arc::new(Schema::new(vec![
3183+
Field::new("a", DataType::Int32, false),
3184+
Field::new("b", DataType::Utf8, true),
3185+
Field::new("c", DataType::Float64, true),
3186+
Field::new("d", DataType::Int32, true),
3187+
]));
3188+
let t1 = MemTable::try_new(
3189+
Arc::clone(&t1_schema),
3190+
vec![vec![RecordBatch::new_empty(Arc::clone(&t1_schema))]],
3191+
)?;
3192+
ctx.register_table("t1", Arc::new(t1))?;
3193+
3194+
let plan = ctx.sql(sql).await?.into_unoptimized_plan();
3195+
match plan {
3196+
LogicalPlan::Dml(DmlStatement {
3197+
table_name,
3198+
op: WriteOp::Delete,
3199+
input,
3200+
..
3201+
}) => Ok((input, table_name)),
3202+
other => internal_err!("Expected DELETE DML plan, got: {other}"),
3203+
}
3204+
}
3205+
31353206
fn make_session_state() -> SessionState {
31363207
let runtime = Arc::new(RuntimeEnv::default());
31373208
let config = SessionConfig::new().with_target_partitions(4);
@@ -3418,6 +3489,159 @@ mod tests {
34183489
Ok(())
34193490
}
34203491

3492+
#[tokio::test]
3493+
#[ignore = "TODO(19950): enable once the implementation PR lands"]
3494+
async fn test_extract_update_assignments_preserves_source_qualifiers_for_update_from()
3495+
-> Result<()> {
3496+
// TODO(19950): enable once the implementation PR lands.
3497+
let (input, table_name) = make_update_from_plan(
3498+
"UPDATE t1 AS dst \
3499+
SET b = src.b, d = src.d \
3500+
FROM t2 AS src \
3501+
WHERE dst.a = src.a",
3502+
)
3503+
.await?;
3504+
3505+
let assignments = extract_update_assignments(&input, &table_name)?;
3506+
let b_expr = assignments
3507+
.iter()
3508+
.find(|(name, _)| name == "b")
3509+
.map(|(_, expr)| expr.to_string())
3510+
.ok_or_else(|| {
3511+
internal_datafusion_err!("Expected assignment for target column b")
3512+
})?;
3513+
let d_expr = assignments
3514+
.iter()
3515+
.find(|(name, _)| name == "d")
3516+
.map(|(_, expr)| expr.to_string())
3517+
.ok_or_else(|| {
3518+
internal_datafusion_err!("Expected assignment for target column d")
3519+
})?;
3520+
3521+
assert!(
3522+
b_expr.contains("src.b"),
3523+
"Unexpected b assignment: {b_expr}"
3524+
);
3525+
assert!(
3526+
d_expr.contains("src.d"),
3527+
"Unexpected d assignment: {d_expr}"
3528+
);
3529+
assert!(
3530+
assignments.iter().all(|(name, _)| name != "a"),
3531+
"Identity target columns should not be extracted as assignments"
3532+
);
3533+
3534+
Ok(())
3535+
}
3536+
3537+
#[tokio::test]
3538+
#[ignore = "TODO(19950): enable once the implementation PR lands"]
3539+
async fn test_extract_update_assignments_strips_target_qualifiers_single_table()
3540+
-> Result<()> {
3541+
// TODO(19950): enable once the implementation PR lands.
3542+
let (input, table_name) =
3543+
make_update_from_plan("UPDATE t1 AS dst SET d = dst.d + 1 WHERE dst.a > 0")
3544+
.await?;
3545+
3546+
let assignments = extract_update_assignments(&input, &table_name)?;
3547+
let d_expr = assignments
3548+
.iter()
3549+
.find(|(name, _)| name == "d")
3550+
.map(|(_, expr)| expr.to_string())
3551+
.ok_or_else(|| {
3552+
internal_datafusion_err!("Expected assignment for target column d")
3553+
})?;
3554+
3555+
assert!(
3556+
!d_expr.contains("dst."),
3557+
"Single-table assignment should not keep target qualifiers: {d_expr}"
3558+
);
3559+
assert!(
3560+
d_expr.contains("d"),
3561+
"Unexpected assignment expression: {d_expr}"
3562+
);
3563+
3564+
Ok(())
3565+
}
3566+
3567+
#[tokio::test]
3568+
#[ignore = "TODO(19950): enable once the implementation PR lands"]
3569+
async fn test_extract_update_assignments_preserves_self_join_source_aliases()
3570+
-> Result<()> {
3571+
// TODO(19950): enable once the implementation PR lands.
3572+
let (input, table_name) = make_update_from_plan(
3573+
"UPDATE t1 AS dst \
3574+
SET b = src.b, d = src.d \
3575+
FROM t1 AS src \
3576+
WHERE dst.a = src.a + 1",
3577+
)
3578+
.await?;
3579+
3580+
let assignments = extract_update_assignments(&input, &table_name)?;
3581+
let b_expr = assignments
3582+
.iter()
3583+
.find(|(name, _)| name == "b")
3584+
.map(|(_, expr)| expr.to_string())
3585+
.ok_or_else(|| {
3586+
internal_datafusion_err!("Expected assignment for target column b")
3587+
})?;
3588+
let d_expr = assignments
3589+
.iter()
3590+
.find(|(name, _)| name == "d")
3591+
.map(|(_, expr)| expr.to_string())
3592+
.ok_or_else(|| {
3593+
internal_datafusion_err!("Expected assignment for target column d")
3594+
})?;
3595+
3596+
assert!(
3597+
b_expr.contains("src.b"),
3598+
"Self-join source alias should be preserved: {b_expr}"
3599+
);
3600+
assert!(
3601+
d_expr.contains("src.d"),
3602+
"Self-join source alias should be preserved: {d_expr}"
3603+
);
3604+
3605+
Ok(())
3606+
}
3607+
3608+
#[tokio::test]
3609+
#[ignore = "TODO(19950): enable once the implementation PR lands"]
3610+
async fn test_extract_dml_filters_delete_limit_without_where() -> Result<()> {
3611+
// TODO(19950): enable once the implementation PR lands.
3612+
let (input, table_name) = make_delete_plan("DELETE FROM t1 LIMIT 10").await?;
3613+
3614+
let filters = extract_dml_filters(&input, &table_name)?;
3615+
assert!(
3616+
filters.is_empty(),
3617+
"DELETE ... LIMIT without WHERE should not synthesize filters: {filters:?}"
3618+
);
3619+
3620+
Ok(())
3621+
}
3622+
3623+
#[tokio::test]
3624+
#[ignore = "TODO(19950): enable once the implementation PR lands"]
3625+
async fn test_extract_dml_filters_delete_where_limit() -> Result<()> {
3626+
// TODO(19950): enable once the implementation PR lands.
3627+
let (input, table_name) =
3628+
make_delete_plan("DELETE FROM t1 WHERE a > 1 LIMIT 10").await?;
3629+
3630+
let filters = extract_dml_filters(&input, &table_name)?;
3631+
assert_eq!(
3632+
filters.len(),
3633+
1,
3634+
"Expected one target predicate from DELETE WHERE ... LIMIT"
3635+
);
3636+
let rendered = filters[0].to_string();
3637+
assert!(
3638+
rendered.starts_with("a > Int") && rendered.ends_with("(1)"),
3639+
"Unexpected rendered filter for DELETE WHERE ... LIMIT: {rendered}"
3640+
);
3641+
3642+
Ok(())
3643+
}
3644+
34213645
#[tokio::test]
34223646
async fn test_create_not() -> Result<()> {
34233647
let schema = Schema::new(vec![Field::new("a", DataType::Boolean, true)]);

0 commit comments

Comments
 (0)