Skip to content

Commit 7f32b8f

Browse files
authored
Simplify match patterns in coercion rules (#12711)
Remove conditions where unnecessary. Refactor to improve readability.
1 parent 7b974a5 commit 7f32b8f

File tree

1 file changed

+45
-93
lines changed

1 file changed

+45
-93
lines changed

datafusion/expr/src/type_coercion/functions.rs

Lines changed: 45 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -602,89 +602,48 @@ fn coerced_from<'a>(
602602
Some(type_into.clone())
603603
}
604604
// coerced into type_into
605-
(Int8, _) if matches!(type_from, Null | Int8) => Some(type_into.clone()),
606-
(Int16, _) if matches!(type_from, Null | Int8 | Int16 | UInt8) => {
607-
Some(type_into.clone())
608-
}
609-
(Int32, _)
610-
if matches!(type_from, Null | Int8 | Int16 | Int32 | UInt8 | UInt16) =>
611-
{
612-
Some(type_into.clone())
613-
}
614-
(Int64, _)
615-
if matches!(
616-
type_from,
617-
Null | Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32
618-
) =>
619-
{
620-
Some(type_into.clone())
621-
}
622-
(UInt8, _) if matches!(type_from, Null | UInt8) => Some(type_into.clone()),
623-
(UInt16, _) if matches!(type_from, Null | UInt8 | UInt16) => {
624-
Some(type_into.clone())
625-
}
626-
(UInt32, _) if matches!(type_from, Null | UInt8 | UInt16 | UInt32) => {
627-
Some(type_into.clone())
628-
}
629-
(UInt64, _) if matches!(type_from, Null | UInt8 | UInt16 | UInt32 | UInt64) => {
630-
Some(type_into.clone())
631-
}
632-
(Float32, _)
633-
if matches!(
634-
type_from,
635-
Null | Int8
636-
| Int16
637-
| Int32
638-
| Int64
639-
| UInt8
640-
| UInt16
641-
| UInt32
642-
| UInt64
643-
| Float32
644-
) =>
645-
{
646-
Some(type_into.clone())
647-
}
648-
(Float64, _)
649-
if matches!(
650-
type_from,
651-
Null | Int8
652-
| Int16
653-
| Int32
654-
| Int64
655-
| UInt8
656-
| UInt16
657-
| UInt32
658-
| UInt64
659-
| Float32
660-
| Float64
661-
| Decimal128(_, _)
662-
) =>
663-
{
664-
Some(type_into.clone())
665-
}
666-
(Timestamp(TimeUnit::Nanosecond, None), _)
667-
if matches!(
668-
type_from,
669-
Null | Timestamp(_, None) | Date32 | Utf8 | LargeUtf8
670-
) =>
671-
{
672-
Some(type_into.clone())
673-
}
674-
(Interval(_), _) if matches!(type_from, Utf8 | LargeUtf8) => {
605+
(Int8, Null | Int8) => Some(type_into.clone()),
606+
(Int16, Null | Int8 | Int16 | UInt8) => Some(type_into.clone()),
607+
(Int32, Null | Int8 | Int16 | Int32 | UInt8 | UInt16) => Some(type_into.clone()),
608+
(Int64, Null | Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32) => {
675609
Some(type_into.clone())
676610
}
611+
(UInt8, Null | UInt8) => Some(type_into.clone()),
612+
(UInt16, Null | UInt8 | UInt16) => Some(type_into.clone()),
613+
(UInt32, Null | UInt8 | UInt16 | UInt32) => Some(type_into.clone()),
614+
(UInt64, Null | UInt8 | UInt16 | UInt32 | UInt64) => Some(type_into.clone()),
615+
(
616+
Float32,
617+
Null | Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64
618+
| Float32,
619+
) => Some(type_into.clone()),
620+
(
621+
Float64,
622+
Null
623+
| Int8
624+
| Int16
625+
| Int32
626+
| Int64
627+
| UInt8
628+
| UInt16
629+
| UInt32
630+
| UInt64
631+
| Float32
632+
| Float64
633+
| Decimal128(_, _),
634+
) => Some(type_into.clone()),
635+
(
636+
Timestamp(TimeUnit::Nanosecond, None),
637+
Null | Timestamp(_, None) | Date32 | Utf8 | LargeUtf8,
638+
) => Some(type_into.clone()),
639+
(Interval(_), Utf8 | LargeUtf8) => Some(type_into.clone()),
677640
// We can go into a Utf8View from a Utf8 or LargeUtf8
678-
(Utf8View, _) if matches!(type_from, Utf8 | LargeUtf8 | Null) => {
679-
Some(type_into.clone())
680-
}
641+
(Utf8View, Utf8 | LargeUtf8 | Null) => Some(type_into.clone()),
681642
// Any type can be coerced into strings
682643
(Utf8 | LargeUtf8, _) => Some(type_into.clone()),
683644
(Null, _) if can_cast_types(type_from, type_into) => Some(type_into.clone()),
684645

685-
(List(_), _) if matches!(type_from, FixedSizeList(_, _)) => {
686-
Some(type_into.clone())
687-
}
646+
(List(_), FixedSizeList(_, _)) => Some(type_into.clone()),
688647

689648
// Only accept list and largelist with the same number of dimensions unless the type is Null.
690649
// List or LargeList with different dimensions should be handled in TypeSignature or other places before this
@@ -695,18 +654,16 @@ fn coerced_from<'a>(
695654
Some(type_into.clone())
696655
}
697656
// should be able to coerce wildcard fixed size list to non wildcard fixed size list
698-
(FixedSizeList(f_into, FIXED_SIZE_LIST_WILDCARD), _) => match type_from {
699-
FixedSizeList(f_from, size_from) => {
700-
match coerced_from(f_into.data_type(), f_from.data_type()) {
701-
Some(data_type) if &data_type != f_into.data_type() => {
702-
let new_field =
703-
Arc::new(f_into.as_ref().clone().with_data_type(data_type));
704-
Some(FixedSizeList(new_field, *size_from))
705-
}
706-
Some(_) => Some(FixedSizeList(Arc::clone(f_into), *size_from)),
707-
_ => None,
708-
}
657+
(
658+
FixedSizeList(f_into, FIXED_SIZE_LIST_WILDCARD),
659+
FixedSizeList(f_from, size_from),
660+
) => match coerced_from(f_into.data_type(), f_from.data_type()) {
661+
Some(data_type) if &data_type != f_into.data_type() => {
662+
let new_field =
663+
Arc::new(f_into.as_ref().clone().with_data_type(data_type));
664+
Some(FixedSizeList(new_field, *size_from))
709665
}
666+
Some(_) => Some(FixedSizeList(Arc::clone(f_into), *size_from)),
710667
_ => None,
711668
},
712669
(Timestamp(unit, Some(tz)), _) if tz.as_ref() == TIMEZONE_WILDCARD => {
@@ -721,12 +678,7 @@ fn coerced_from<'a>(
721678
_ => None,
722679
}
723680
}
724-
(Timestamp(_, Some(_)), _)
725-
if matches!(
726-
type_from,
727-
Null | Timestamp(_, _) | Date32 | Utf8 | LargeUtf8
728-
) =>
729-
{
681+
(Timestamp(_, Some(_)), Null | Timestamp(_, _) | Date32 | Utf8 | LargeUtf8) => {
730682
Some(type_into.clone())
731683
}
732684
_ => None,

0 commit comments

Comments
 (0)