-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add documentation for UNION schema handling. #17248
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4bf5d7d
refactor: consolidate and document union intsersection of field metadata
wiedld f2ea8e5
chore: document how schemas are unioned in the analyzer
wiedld baa3f8d
chore: add concrete examples, linking to datafusion-examples
wiedld 24bb5e5
inline and rename function
alamb 6044d3d
Merge remote-tracking branch 'apache/main' into 12736/union-schema-ha…
alamb 6a35170
complete rename
alamb 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -957,6 +957,43 @@ fn coerce_case_expression(case: Case, schema: &DFSchema) -> Result<Case> { | |
| /// | ||
| /// This method presumes that the wildcard expansion is unneeded, or has already | ||
| /// been applied. | ||
| /// | ||
| /// ## Schema and Field Handling in Union Coercion | ||
| /// | ||
|
Comment on lines
+960
to
+962
Contributor
Author
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. Note that here in the analyzer, we handle union coercion differently than at construction. |
||
| /// **Processing order**: The function starts with the base schema (first input) and then | ||
| /// processes remaining inputs sequentially, with later inputs taking precedence in merging. | ||
| /// | ||
| /// **Schema-level metadata merging**: Later schemas take precedence for duplicate keys. | ||
| /// | ||
| /// **Field-level metadata merging**: Later fields take precedence for duplicate metadata keys. | ||
| /// | ||
| /// **Type coercion precedence**: The coerced type is determined by iteratively applying | ||
| /// `comparison_coercion()` between the accumulated type and each new input's type. The | ||
| /// result depends on type coercion rules, not input order. | ||
| /// | ||
| /// **Nullability merging**: Nullability is accumulated using logical OR (`||`). | ||
| /// Once any input field is nullable, the result field becomes nullable permanently. | ||
| /// Later inputs can make a field nullable but cannot make it non-nullable. | ||
| /// | ||
| /// **Field precedence**: Field names come from the first (base) schema, but the field properties | ||
| /// (nullability and field-level metadata) have later schemas taking precedence. | ||
| /// | ||
| /// **Example**: | ||
| /// ```sql | ||
| /// SELECT a, b FROM table1 -- a: Int32, metadata {"source": "t1"}, nullable=false | ||
| /// UNION | ||
| /// SELECT a, b FROM table2 -- a: Int64, metadata {"source": "t2"}, nullable=true | ||
| /// UNION | ||
| /// SELECT a, b FROM table3 -- a: Int32, metadata {"encoding": "utf8"}, nullable=false | ||
| /// -- Result: | ||
| /// -- a: Int64 (from type coercion), nullable=true (from table2), | ||
| /// -- metadata: {"source": "t2", "encoding": "utf8"} (later inputs take precedence) | ||
| /// ``` | ||
| /// | ||
| /// **Precedence Summary**: | ||
| /// - **Datatypes**: Determined by `comparison_coercion()` rules, not input order | ||
| /// - **Nullability**: Later inputs can add nullability but cannot remove it (logical OR) | ||
| /// - **Metadata**: Later inputs take precedence for same keys (HashMap::extend semantics) | ||
| pub fn coerce_union_schema(inputs: &[Arc<LogicalPlan>]) -> Result<DFSchema> { | ||
| coerce_union_schema_with_schema(&inputs[1..], inputs[0].schema()) | ||
| } | ||
|
|
||
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.
It would be good to describe the key and value, ideally with some example
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.
Good idea. Thank you.
Added: baa3f8d
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.
As we move to working with more field metadata, maybe should move this function into its own module --
datafusion/expr/src/metadata.rsor something 🤔As a follow on PR