-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: preserve ProjectionExec metadata during serialization #25009
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
Open
goutamadwant
wants to merge
1
commit into
apache:main
Choose a base branch
from
goutamadwant:fix-projection-proto-metadata-24695
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,9 +43,176 @@ use datafusion_proto::physical_plan::{ | |
| }; | ||
| use datafusion_proto::protobuf; | ||
| use datafusion_proto::protobuf::PhysicalPlanNode; | ||
| use prost::Message; | ||
| use std::collections::HashMap; | ||
| use std::sync::Arc; | ||
| use std::vec; | ||
|
|
||
| #[test] | ||
| fn roundtrip_projection_metadata() -> Result<()> { | ||
| let input_schema = Arc::new(Schema::new(vec![Field::new( | ||
| "value", | ||
| DataType::Int32, | ||
| false, | ||
| )])); | ||
| let projected_schema = Schema::new_with_metadata( | ||
| vec![Field::new("value", DataType::Int32, false).with_metadata( | ||
| [("field-key".to_string(), "field-value".to_string())].into(), | ||
| )], | ||
| [("schema-key".to_string(), "schema-value".to_string())].into(), | ||
| ); | ||
| let plan = Arc::new(ProjectionExec::try_new_with_schema_metadata( | ||
| vec![(col("value", &input_schema)?, "value".to_string())], | ||
| Arc::new(EmptyExec::new(input_schema)), | ||
| &projected_schema, | ||
| )?); | ||
| let ctx = SessionContext::new(); | ||
| let codec = DefaultPhysicalExtensionCodec {}; | ||
| let converter = DefaultPhysicalProtoConverter {}; | ||
| let decoded = roundtrip_test_and_return(plan, &ctx, &codec, &converter)?; | ||
| assert_eq!(decoded.schema().as_ref(), &projected_schema); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn roundtrip_projection_metadata_overrides() -> Result<()> { | ||
| let field_metadata = [("field-key".to_string(), "field-value".to_string())].into(); | ||
| let extension_metadata = [ | ||
| ("ARROW:extension:name".to_string(), "arrow.uuid".to_string()), | ||
| ("ARROW:extension:metadata".to_string(), String::new()), | ||
| ] | ||
| .into(); | ||
| for (input_field, output_field, input_metadata) in [ | ||
| ( | ||
| Field::new("value", DataType::Int32, false).with_metadata(field_metadata), | ||
| Field::new("value", DataType::Int32, false), | ||
| [("input-schema".to_string(), "input-value".to_string())].into(), | ||
| ), | ||
| ( | ||
| Field::new("value", DataType::FixedSizeBinary(16), true), | ||
| Field::new("value", DataType::FixedSizeBinary(16), true) | ||
| .with_metadata(extension_metadata), | ||
| HashMap::new(), | ||
| ), | ||
| ] { | ||
| let input_schema = | ||
| Arc::new(Schema::new_with_metadata(vec![input_field], input_metadata)); | ||
| let projected_schema = Schema::new(vec![output_field]); | ||
| let plan = Arc::new(ProjectionExec::try_new_with_schema_metadata( | ||
| vec![(col("value", &input_schema)?, "value".to_string())], | ||
| Arc::new(EmptyExec::new(input_schema)), | ||
| &projected_schema, | ||
| )?); | ||
| let codec = DefaultPhysicalExtensionCodec {}; | ||
| let ctx = SessionContext::new(); | ||
| let node = PhysicalPlanNode::try_from_physical_plan(plan, &codec)?; | ||
| let Some(protobuf::physical_plan_node::PhysicalPlanType::Projection(projection)) = | ||
| node.physical_plan_type.as_ref() | ||
| else { | ||
| unreachable!("expected ProjectionExecNode") | ||
| }; | ||
| assert!(projection.schema.is_some()); | ||
| let node = PhysicalPlanNode::decode(node.encode_to_vec().as_slice()).unwrap(); | ||
| #[cfg(feature = "json")] | ||
| let node: PhysicalPlanNode = | ||
| serde_json::from_str(&serde_json::to_string(&node).unwrap()).unwrap(); | ||
| let decoded = node.try_into_physical_plan(&ctx.task_ctx(), &codec)?; | ||
| assert_eq!(decoded.schema().as_ref(), &projected_schema); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn roundtrip_projection_without_metadata_override() -> Result<()> { | ||
| let input_schema = Arc::new(Schema::new_with_metadata( | ||
| vec![Field::new("value", DataType::Int32, false).with_metadata( | ||
| [("field-key".to_string(), "field-value".to_string())].into(), | ||
| )], | ||
| [("schema-key".to_string(), "schema-value".to_string())].into(), | ||
| )); | ||
| let plan = Arc::new(ProjectionExec::try_new( | ||
| vec![(col("value", &input_schema)?, "value".to_string())], | ||
| Arc::new(EmptyExec::new(Arc::clone(&input_schema))), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is preserving the metadata through the child after decoding, not a proojection that needs to rederive the metadata completely from itself. Could we add a test that forces the projection to completely rederive the metadata from its own proto after roundtrip 👍 |
||
| )?); | ||
| let codec = DefaultPhysicalExtensionCodec {}; | ||
| let ctx = SessionContext::new(); | ||
| let node = PhysicalPlanNode::try_from_physical_plan(plan, &codec)?; | ||
| let Some(protobuf::physical_plan_node::PhysicalPlanType::Projection(projection)) = | ||
| node.physical_plan_type.as_ref() | ||
| else { | ||
| unreachable!("expected ProjectionExecNode") | ||
| }; | ||
| // The payload also represents plans encoded before the schema field existed. | ||
| assert!(projection.schema.is_none()); | ||
| let node = PhysicalPlanNode::decode(node.encode_to_vec().as_slice()).unwrap(); | ||
| #[cfg(feature = "json")] | ||
| let node: PhysicalPlanNode = | ||
| serde_json::from_str(&serde_json::to_string(&node).unwrap()).unwrap(); | ||
| let decoded = node.try_into_physical_plan(&ctx.task_ctx(), &codec)?; | ||
| assert_eq!(decoded.schema(), input_schema); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn decode_projection_schema_only_replaces_metadata() -> Result<()> { | ||
| let input_schema = Arc::new(Schema::new(vec![Field::new( | ||
| "input", | ||
| DataType::Int32, | ||
| false, | ||
| )])); | ||
| let plan = Arc::new(ProjectionExec::try_new( | ||
| vec![(col("input", &input_schema)?, "output".to_string())], | ||
| Arc::new(EmptyExec::new(input_schema)), | ||
| )?); | ||
| let codec = DefaultPhysicalExtensionCodec {}; | ||
| let ctx = SessionContext::new(); | ||
| let mut node = PhysicalPlanNode::try_from_physical_plan(plan, &codec)?; | ||
| let Some(protobuf::physical_plan_node::PhysicalPlanType::Projection(projection)) = | ||
| node.physical_plan_type.as_mut() | ||
| else { | ||
| unreachable!("expected ProjectionExecNode") | ||
| }; | ||
| let field_metadata = | ||
| HashMap::from([("field-key".to_string(), "field-value".to_string())]); | ||
| let schema_metadata = | ||
| HashMap::from([("schema-key".to_string(), "schema-value".to_string())]); | ||
| let metadata_schema = Schema::new_with_metadata( | ||
| vec![ | ||
| Field::new("ignored", DataType::Utf8, true) | ||
| .with_metadata(field_metadata.clone()), | ||
| ], | ||
| schema_metadata.clone(), | ||
| ); | ||
| projection.schema = Some((&metadata_schema).try_into()?); | ||
| let decoded = node.try_into_physical_plan(&ctx.task_ctx(), &codec)?; | ||
| assert_eq!( | ||
| decoded.schema().as_ref(), | ||
| &Schema::new_with_metadata( | ||
| vec![ | ||
| Field::new("output", DataType::Int32, false) | ||
| .with_metadata(field_metadata) | ||
| ], | ||
| schema_metadata, | ||
| ), | ||
| ); | ||
|
|
||
| let Some(protobuf::physical_plan_node::PhysicalPlanType::Projection(projection)) = | ||
| node.physical_plan_type.as_mut() | ||
| else { | ||
| unreachable!("expected ProjectionExecNode") | ||
| }; | ||
| projection.schema.as_mut().unwrap().columns.clear(); | ||
| let error = node | ||
| .try_into_physical_plan(&ctx.task_ctx(), &codec) | ||
| .unwrap_err(); | ||
| assert!( | ||
| error | ||
| .strip_backtrace() | ||
| .contains("Projection has 1 output fields but metadata schema has 0 fields") | ||
| ); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn roundtrip_date_time_interval() -> Result<()> { | ||
| let schema = Schema::new(vec![ | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This conditioins shouldnt just be if we are overriding metadata I believe. Rather we should be checking if we have metadata in general.