-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Provide documentation of expose APIs to enable handling of type coercion at UNION plan construction. #12142
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
comphead
merged 3 commits into
apache:main
from
influxdata:12105/unions-coerce-at-construction
Aug 26, 2024
Merged
Provide documentation of expose APIs to enable handling of type coercion at UNION plan construction. #12142
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -1331,7 +1331,17 @@ pub fn validate_unique_names<'a>( | |
}) | ||
} | ||
|
||
/// Union two logical plans. | ||
/// Union two [`LogicalPlan`]s. | ||
/// | ||
/// Constructs the UNION plan, but does not perform type-coercion. Therefore the | ||
/// subtree expressions will not be properly typed until the optimizer pass. | ||
/// | ||
/// If a properly typed UNION plan is needed, refer to [`TypeCoercionRewriter::coerce_union`] | ||
/// or alternatively, merge the union input schema using [`coerce_union_schema`] and | ||
/// apply the expression rewrite with [`coerce_plan_expr_for_schema`]. | ||
/// | ||
/// [`TypeCoercionRewriter::coerce_union`]: https://docs.rs/datafusion-optimizer/latest/datafusion_optimizer/analyzer/type_coercion/struct.TypeCoercionRewriter.html#method.coerce_union | ||
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. 👍 |
||
/// [`coerce_union_schema`]: https://docs.rs/datafusion-optimizer/latest/datafusion_optimizer/analyzer/type_coercion/fn.coerce_union_schema.html | ||
pub fn union(left_plan: LogicalPlan, right_plan: LogicalPlan) -> Result<LogicalPlan> { | ||
// Temporarily use the schema from the left input and later rely on the analyzer to | ||
// coerce the two schemas into a common one. | ||
|
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 |
---|---|---|
|
@@ -56,6 +56,8 @@ use datafusion_expr::{ | |
Projection, ScalarUDF, Union, WindowFrame, WindowFrameBound, WindowFrameUnits, | ||
}; | ||
|
||
/// Performs type coercion by determining the schema | ||
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. ❤️ |
||
/// and performing the expression rewrites. | ||
#[derive(Default)] | ||
pub struct TypeCoercion {} | ||
|
||
|
@@ -128,16 +130,23 @@ fn analyze_internal( | |
.map_data(|plan| plan.recompute_schema()) | ||
} | ||
|
||
pub(crate) struct TypeCoercionRewriter<'a> { | ||
/// Rewrite expressions to apply type coercion. | ||
pub struct TypeCoercionRewriter<'a> { | ||
pub(crate) schema: &'a DFSchema, | ||
} | ||
|
||
impl<'a> TypeCoercionRewriter<'a> { | ||
/// Create a new [`TypeCoercionRewriter`] with a provided schema | ||
/// representing both the inputs and output of the [`LogicalPlan`] node. | ||
fn new(schema: &'a DFSchema) -> Self { | ||
Self { schema } | ||
} | ||
|
||
fn coerce_plan(&mut self, plan: LogicalPlan) -> Result<LogicalPlan> { | ||
/// Coerce the [`LogicalPlan`]. | ||
/// | ||
/// Refer to [`TypeCoercionRewriter::coerce_join`] and [`TypeCoercionRewriter::coerce_union`] | ||
/// for type-coercion approach. | ||
pub fn coerce_plan(&mut self, plan: LogicalPlan) -> Result<LogicalPlan> { | ||
match plan { | ||
LogicalPlan::Join(join) => self.coerce_join(join), | ||
LogicalPlan::Union(union) => Self::coerce_union(union), | ||
|
@@ -153,7 +162,7 @@ impl<'a> TypeCoercionRewriter<'a> { | |
/// | ||
/// For example, on_exprs like `t1.a = t2.b AND t1.x = t2.y` will be stored | ||
/// as a list of `(t1.a, t2.b), (t1.x, t2.y)` | ||
fn coerce_join(&mut self, mut join: Join) -> Result<LogicalPlan> { | ||
pub fn coerce_join(&mut self, mut join: Join) -> Result<LogicalPlan> { | ||
join.on = join | ||
.on | ||
.into_iter() | ||
|
@@ -176,7 +185,7 @@ impl<'a> TypeCoercionRewriter<'a> { | |
|
||
/// Coerce the union’s inputs to a common schema compatible with all inputs. | ||
/// This occurs after wildcard expansion and the coercion of the input expressions. | ||
fn coerce_union(union_plan: Union) -> Result<LogicalPlan> { | ||
pub fn coerce_union(union_plan: Union) -> Result<LogicalPlan> { | ||
let union_schema = Arc::new(coerce_union_schema(&union_plan.inputs)?); | ||
let new_inputs = union_plan | ||
.inputs | ||
|
@@ -809,7 +818,10 @@ fn coerce_case_expression(case: Case, schema: &DFSchema) -> Result<Case> { | |
} | ||
|
||
/// Get a common schema that is compatible with all inputs of UNION. | ||
fn coerce_union_schema(inputs: &[Arc<LogicalPlan>]) -> Result<DFSchema> { | ||
/// | ||
/// This method presumes that the wildcard expansion is unneeded, or has already | ||
/// been applied. | ||
pub fn coerce_union_schema(inputs: &[Arc<LogicalPlan>]) -> Result<DFSchema> { | ||
let base_schema = inputs[0].schema(); | ||
let mut union_datatypes = base_schema | ||
.fields() | ||
|
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.