-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix: handle column name collisions when combining UNION logical inputs & nested Column expressions in maybe_fix_physical_column_name #16064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
berkaysynnada
merged 9 commits into
apache:main
from
LiaCastaneda:lia/fix-union-schema-field-names
May 22, 2025
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1e3e9cb
Fix union schema name coercion
LiaCastaneda b1ccf2c
Merge branch 'main' into lia/fix-union-schema-field-names
LiaCastaneda 9c3b201
Address renaming for columns that are not in the top level as well
LiaCastaneda f756008
Add unit test
LiaCastaneda 12cfeba
Format
LiaCastaneda e69a28e
Use insta tests properly
LiaCastaneda 4dbac93
Merge branch 'main' into lia/fix-union-schema-field-names
LiaCastaneda 8d627db
Address review - comment + minor simplification change
LiaCastaneda 3bed3c0
Merge branch 'main' into lia/fix-union-schema-field-names
berkaysynnada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,7 +62,9 @@ use arrow::array::{builder::StringBuilder, RecordBatch}; | |
| use arrow::compute::SortOptions; | ||
| use arrow::datatypes::{Schema, SchemaRef}; | ||
| use datafusion_common::display::ToStringifiedPlan; | ||
| use datafusion_common::tree_node::{TreeNode, TreeNodeRecursion, TreeNodeVisitor}; | ||
| use datafusion_common::tree_node::{ | ||
| Transformed, TransformedResult, TreeNode, TreeNodeRecursion, TreeNodeVisitor, | ||
| }; | ||
| use datafusion_common::{ | ||
| exec_err, internal_datafusion_err, internal_err, not_impl_err, plan_err, DFSchema, | ||
| ScalarValue, | ||
|
|
@@ -2067,29 +2069,37 @@ fn maybe_fix_physical_column_name( | |
| expr: Result<Arc<dyn PhysicalExpr>>, | ||
| input_physical_schema: &SchemaRef, | ||
| ) -> Result<Arc<dyn PhysicalExpr>> { | ||
| if let Ok(e) = &expr { | ||
| if let Some(column) = e.as_any().downcast_ref::<Column>() { | ||
| let physical_field = input_physical_schema.field(column.index()); | ||
| let expr_col_name = column.name(); | ||
| let physical_name = physical_field.name(); | ||
|
|
||
| if physical_name != expr_col_name { | ||
| // handle edge cases where the physical_name contains ':'. | ||
| let colon_count = physical_name.matches(':').count(); | ||
| let mut splits = expr_col_name.match_indices(':'); | ||
| let split_pos = splits.nth(colon_count); | ||
|
|
||
| if let Some((idx, _)) = split_pos { | ||
| let base_name = &expr_col_name[..idx]; | ||
| if base_name == physical_name { | ||
| let updated_column = Column::new(physical_name, column.index()); | ||
| return Ok(Arc::new(updated_column)); | ||
| expr.and_then(|e| { | ||
| e.transform_down(|node| { | ||
|
||
| if let Some(column) = node.as_any().downcast_ref::<Column>() { | ||
| let idx = column.index(); | ||
| let physical_field = input_physical_schema.field(idx); | ||
| let expr_col_name = column.name(); | ||
| let physical_name = physical_field.name(); | ||
|
|
||
| if expr_col_name != physical_name { | ||
| // handle edge cases where the physical_name contains ':'. | ||
| let colon_count = physical_name.matches(':').count(); | ||
| let mut splits = expr_col_name.match_indices(':'); | ||
| let split_pos = splits.nth(colon_count); | ||
|
|
||
| if let Some((i, _)) = split_pos { | ||
| let base_name = &expr_col_name[..i]; | ||
| if base_name == physical_name { | ||
| let updated_column = Column::new(physical_name, idx); | ||
| return Ok(Transformed::yes(Arc::new(updated_column))); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // If names already match or fix is not possible, just leave it as it is | ||
| Ok(Transformed::no(node)) | ||
| } else { | ||
| Ok(Transformed::no(node)) | ||
| } | ||
| } | ||
| } | ||
| expr | ||
| }) | ||
| .data() | ||
| }) | ||
| } | ||
|
|
||
| struct OptimizationInvariantChecker<'a> { | ||
|
|
@@ -2193,8 +2203,11 @@ mod tests { | |
| use datafusion_common::{assert_contains, DFSchemaRef, TableReference}; | ||
| use datafusion_execution::runtime_env::RuntimeEnv; | ||
| use datafusion_execution::TaskContext; | ||
| use datafusion_expr::{col, lit, LogicalPlanBuilder, UserDefinedLogicalNodeCore}; | ||
| use datafusion_expr::{ | ||
| col, lit, LogicalPlanBuilder, Operator, UserDefinedLogicalNodeCore, | ||
| }; | ||
| use datafusion_functions_aggregate::expr_fn::sum; | ||
| use datafusion_physical_expr::expressions::{BinaryExpr, IsNotNullExpr}; | ||
| use datafusion_physical_expr::EquivalenceProperties; | ||
| use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType}; | ||
|
|
||
|
|
@@ -2711,6 +2724,47 @@ mod tests { | |
|
|
||
| assert_eq!(col.name(), "metric:avg"); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_maybe_fix_nested_column_name_with_colon() { | ||
| let schema = Schema::new(vec![Field::new("column", DataType::Int32, false)]); | ||
| let schema_ref: SchemaRef = Arc::new(schema); | ||
|
|
||
| // Construct the nested expr | ||
| let col_expr = Arc::new(Column::new("column:1", 0)) as Arc<dyn PhysicalExpr>; | ||
| let is_not_null_expr = Arc::new(IsNotNullExpr::new(col_expr.clone())); | ||
|
|
||
| // Create a binary expression and put the column inside | ||
| let binary_expr = Arc::new(BinaryExpr::new( | ||
| is_not_null_expr.clone(), | ||
| Operator::Or, | ||
| is_not_null_expr.clone(), | ||
| )) as Arc<dyn PhysicalExpr>; | ||
|
|
||
| let fixed_expr = | ||
LiaCastaneda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| maybe_fix_physical_column_name(Ok(binary_expr), &schema_ref).unwrap(); | ||
|
|
||
| let bin = fixed_expr | ||
| .as_any() | ||
| .downcast_ref::<BinaryExpr>() | ||
| .expect("Expected BinaryExpr"); | ||
|
|
||
| // Check that both sides where renamed | ||
| for expr in &[bin.left(), bin.right()] { | ||
| let is_not_null = expr | ||
| .as_any() | ||
| .downcast_ref::<IsNotNullExpr>() | ||
| .expect("Expected IsNotNull"); | ||
|
|
||
| let col = is_not_null | ||
| .arg() | ||
| .as_any() | ||
| .downcast_ref::<Column>() | ||
| .expect("Expected Column"); | ||
|
|
||
| assert_eq!(col.name(), "column"); | ||
| } | ||
| } | ||
| struct ErrorExtensionPlanner {} | ||
|
|
||
| #[async_trait] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.