@@ -131,6 +131,10 @@ fn maybe_data_types(
131131/// See the module level documentation for more detail on coercion.
132132pub fn can_coerce_from ( type_into : & DataType , type_from : & DataType ) -> bool {
133133 use self :: DataType :: * ;
134+
135+ if type_into == type_from {
136+ return true ;
137+ }
134138 // Null can convert to most of types
135139 match type_into {
136140 Int8 => matches ! ( type_from, Null | Int8 ) ,
@@ -173,12 +177,35 @@ pub fn can_coerce_from(type_into: &DataType, type_from: &DataType) -> bool {
173177 Timestamp ( TimeUnit :: Nanosecond , None ) => {
174178 matches ! ( type_from, Null | Timestamp ( _, None ) )
175179 }
180+ Date32 => {
181+ matches ! ( type_from, Null | Timestamp ( _, None ) )
182+ }
176183 Utf8 | LargeUtf8 => true ,
177184 Null => can_cast_types ( type_from, type_into) ,
178185 _ => false ,
179186 }
180187}
181188
189+ /// Returns a common coerced datatype between 2 given datatypes
190+ ///
191+ /// See the module level documentation for more detail on coercion.
192+ pub fn get_common_coerced_type (
193+ left_datatype : DataType ,
194+ right_datatype : DataType ,
195+ ) -> Result < DataType > {
196+ if left_datatype == right_datatype || can_coerce_from ( & left_datatype, & right_datatype)
197+ {
198+ Ok ( left_datatype)
199+ } else if can_coerce_from ( & right_datatype, & left_datatype) {
200+ Ok ( right_datatype)
201+ } else {
202+ Err ( DataFusionError :: Plan ( format ! (
203+ "Datatypes cannot be casted into common type {:?} <-> {:?}" ,
204+ left_datatype, right_datatype
205+ ) ) )
206+ }
207+ }
208+
182209#[ cfg( test) ]
183210mod tests {
184211 use super :: * ;
0 commit comments