houqp commented on a change in pull request #7253:
URL: https://github.com/apache/arrow/pull/7253#discussion_r429569531
##########
File path: rust/datafusion/src/optimizer/utils.rs
##########
@@ -131,116 +131,90 @@ pub fn exprlist_to_fields(expr: &[Expr], input_schema:
&Schema) -> Result<Vec<Fi
/// Given two datatypes, determine the supertype that both types can safely be
cast to
pub fn get_supertype(l: &DataType, r: &DataType) -> Result<DataType> {
- match _get_supertype(l, r) {
- Some(dt) => Ok(dt),
- None => _get_supertype(r, l).ok_or_else(|| {
- ExecutionError::InternalError(format!(
- "Failed to determine supertype of {:?} and {:?}",
- l, r
- ))
- }),
- }
-}
-
-/// Given two datatypes, determine the supertype that both types can safely be
cast to
-fn _get_supertype(l: &DataType, r: &DataType) -> Option<DataType> {
use arrow::datatypes::DataType::*;
- match (l, r) {
- (UInt8, Int8) => Some(Int8),
- (UInt8, Int16) => Some(Int16),
- (UInt8, Int32) => Some(Int32),
- (UInt8, Int64) => Some(Int64),
-
- (UInt16, Int16) => Some(Int16),
- (UInt16, Int32) => Some(Int32),
- (UInt16, Int64) => Some(Int64),
-
- (UInt32, Int32) => Some(Int32),
- (UInt32, Int64) => Some(Int64),
-
- (UInt64, Int64) => Some(Int64),
-
- (Int8, UInt8) => Some(Int8),
-
- (Int16, UInt8) => Some(Int16),
- (Int16, UInt16) => Some(Int16),
-
- (Int32, UInt8) => Some(Int32),
- (Int32, UInt16) => Some(Int32),
- (Int32, UInt32) => Some(Int32),
-
- (Int64, UInt8) => Some(Int64),
- (Int64, UInt16) => Some(Int64),
- (Int64, UInt32) => Some(Int64),
- (Int64, UInt64) => Some(Int64),
-
- (UInt8, UInt8) => Some(UInt8),
- (UInt8, UInt16) => Some(UInt16),
- (UInt8, UInt32) => Some(UInt32),
- (UInt8, UInt64) => Some(UInt64),
- (UInt8, Float32) => Some(Float32),
- (UInt8, Float64) => Some(Float64),
- (UInt16, UInt8) => Some(UInt16),
- (UInt16, UInt16) => Some(UInt16),
- (UInt16, UInt32) => Some(UInt32),
- (UInt16, UInt64) => Some(UInt64),
- (UInt16, Float32) => Some(Float32),
- (UInt16, Float64) => Some(Float64),
-
- (UInt32, UInt8) => Some(UInt32),
- (UInt32, UInt16) => Some(UInt32),
- (UInt32, UInt32) => Some(UInt32),
- (UInt32, UInt64) => Some(UInt64),
- (UInt32, Float32) => Some(Float32),
- (UInt32, Float64) => Some(Float64),
-
- (UInt64, UInt8) => Some(UInt64),
- (UInt64, UInt16) => Some(UInt64),
- (UInt64, UInt32) => Some(UInt64),
- (UInt64, UInt64) => Some(UInt64),
- (UInt64, Float32) => Some(Float32),
- (UInt64, Float64) => Some(Float64),
-
- (Int8, Int8) => Some(Int8),
- (Int8, Int16) => Some(Int16),
- (Int8, Int32) => Some(Int32),
- (Int8, Int64) => Some(Int64),
- (Int8, Float32) => Some(Float32),
- (Int8, Float64) => Some(Float64),
-
- (Int16, Int8) => Some(Int16),
- (Int16, Int16) => Some(Int16),
- (Int16, Int32) => Some(Int32),
- (Int16, Int64) => Some(Int64),
- (Int16, Float32) => Some(Float32),
- (Int16, Float64) => Some(Float64),
-
- (Int32, Int8) => Some(Int32),
- (Int32, Int16) => Some(Int32),
- (Int32, Int32) => Some(Int32),
- (Int32, Int64) => Some(Int64),
- (Int32, Float32) => Some(Float32),
- (Int32, Float64) => Some(Float64),
-
- (Int64, Int8) => Some(Int64),
- (Int64, Int16) => Some(Int64),
- (Int64, Int32) => Some(Int64),
- (Int64, Int64) => Some(Int64),
- (Int64, Float32) => Some(Float32),
- (Int64, Float64) => Some(Float64),
-
- (Float32, Float32) => Some(Float32),
- (Float32, Float64) => Some(Float64),
- (Float64, Float32) => Some(Float64),
- (Float64, Float64) => Some(Float64),
-
- (Utf8, _) => Some(Utf8),
- (_, Utf8) => Some(Utf8),
-
- (Boolean, Boolean) => Some(Boolean),
+ if l == r {
+ return Ok(l.clone());
+ }
+ let super_type = match l {
+ UInt8 => match r {
+ UInt16 | UInt32 | UInt64 => Some(r.clone()),
+ Int16 | Int32 | Int64 => Some(r.clone()),
+ Float32 | Float64 => Some(r.clone()),
+ _ => None,
+ },
+ UInt16 => match r {
+ UInt8 => Some(l.clone()),
+ UInt32 | UInt64 => Some(r.clone()),
+ Int8 => Some(l.clone()),
+ Int32 | Int64 => Some(r.clone()),
+ Float32 | Float64 => Some(r.clone()),
+ _ => None,
+ },
+ UInt32 => match r {
+ UInt8 | UInt16 => Some(l.clone()),
+ UInt64 => Some(r.clone()),
+ Int8 | Int16 => Some(l.clone()),
+ Int64 => Some(r.clone()),
+ Float32 | Float64 => Some(r.clone()),
+ _ => None,
+ },
+ UInt64 => match r {
+ UInt8 | UInt16 | UInt32 => Some(l.clone()),
+ Int8 | Int16 | Int32 => Some(l.clone()),
+ Float32 | Float64 => Some(r.clone()),
+ _ => None,
+ },
+ Int8 => match r {
Review comment:
should we support signed and unsigned with the same width as well? for
example (int8, uint8) -> int16.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]