@@ -56,6 +56,8 @@ use datafusion_expr::{
5656 Projection , ScalarUDF , Union , WindowFrame , WindowFrameBound , WindowFrameUnits ,
5757} ;
5858
59+ /// Performs type coercion by determining the schema
60+ /// and performing the expression rewrites.
5961#[ derive( Default ) ]
6062pub struct TypeCoercion { }
6163
@@ -128,16 +130,23 @@ fn analyze_internal(
128130 . map_data ( |plan| plan. recompute_schema ( ) )
129131}
130132
131- pub ( crate ) struct TypeCoercionRewriter < ' a > {
133+ /// Rewrite expressions to apply type coercion.
134+ pub struct TypeCoercionRewriter < ' a > {
132135 pub ( crate ) schema : & ' a DFSchema ,
133136}
134137
135138impl < ' a > TypeCoercionRewriter < ' a > {
139+ /// Create a new [`TypeCoercionRewriter`] with a provided schema
140+ /// representing both the inputs and output of the [`LogicalPlan`] node.
136141 fn new ( schema : & ' a DFSchema ) -> Self {
137142 Self { schema }
138143 }
139144
140- fn coerce_plan ( & mut self , plan : LogicalPlan ) -> Result < LogicalPlan > {
145+ /// Coerce the [`LogicalPlan`].
146+ ///
147+ /// Refer to [`TypeCoercionRewriter::coerce_join`] and [`TypeCoercionRewriter::coerce_union`]
148+ /// for type-coercion approach.
149+ pub fn coerce_plan ( & mut self , plan : LogicalPlan ) -> Result < LogicalPlan > {
141150 match plan {
142151 LogicalPlan :: Join ( join) => self . coerce_join ( join) ,
143152 LogicalPlan :: Union ( union) => Self :: coerce_union ( union) ,
@@ -153,7 +162,7 @@ impl<'a> TypeCoercionRewriter<'a> {
153162 ///
154163 /// For example, on_exprs like `t1.a = t2.b AND t1.x = t2.y` will be stored
155164 /// as a list of `(t1.a, t2.b), (t1.x, t2.y)`
156- fn coerce_join ( & mut self , mut join : Join ) -> Result < LogicalPlan > {
165+ pub fn coerce_join ( & mut self , mut join : Join ) -> Result < LogicalPlan > {
157166 join. on = join
158167 . on
159168 . into_iter ( )
@@ -176,7 +185,7 @@ impl<'a> TypeCoercionRewriter<'a> {
176185
177186 /// Coerce the union’s inputs to a common schema compatible with all inputs.
178187 /// This occurs after wildcard expansion and the coercion of the input expressions.
179- fn coerce_union ( union_plan : Union ) -> Result < LogicalPlan > {
188+ pub fn coerce_union ( union_plan : Union ) -> Result < LogicalPlan > {
180189 let union_schema = Arc :: new ( coerce_union_schema ( & union_plan. inputs ) ?) ;
181190 let new_inputs = union_plan
182191 . inputs
@@ -809,7 +818,10 @@ fn coerce_case_expression(case: Case, schema: &DFSchema) -> Result<Case> {
809818}
810819
811820/// Get a common schema that is compatible with all inputs of UNION.
812- fn coerce_union_schema ( inputs : & [ Arc < LogicalPlan > ] ) -> Result < DFSchema > {
821+ ///
822+ /// This method presumes that the wildcard expansion is unneeded, or has already
823+ /// been applied.
824+ pub fn coerce_union_schema ( inputs : & [ Arc < LogicalPlan > ] ) -> Result < DFSchema > {
813825 let base_schema = inputs[ 0 ] . schema ( ) ;
814826 let mut union_datatypes = base_schema
815827 . fields ( )
0 commit comments