Skip to content

Commit 9cfed4e

Browse files
committed
refactor_bool_cast_module
1 parent 668f7a3 commit 9cfed4e

1 file changed

Lines changed: 48 additions & 46 deletions

File tree

  • native/spark-expr/src/conversion_funcs

native/spark-expr/src/conversion_funcs/cast.rs

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,26 +1166,16 @@ fn cast_binary_formatter(value: &[u8]) -> String {
11661166
/// Determines if DataFusion supports the given cast in a way that is
11671167
/// compatible with Spark
11681168
fn is_datafusion_spark_compatible(from_type: &DataType, to_type: &DataType) -> bool {
1169-
from_type == to_type
1170-
|| match from_type {
1171-
DataType::Null => {
1172-
matches!(to_type, DataType::List(_))
1173-
}
1174-
DataType::Boolean => is_df_cast_from_bool_spark_compatible(to_type),
1175-
DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 => {
1176-
matches!(
1177-
to_type,
1178-
DataType::Boolean
1179-
| DataType::Int8
1180-
| DataType::Int16
1181-
| DataType::Int32
1182-
| DataType::Int64
1183-
| DataType::Float32
1184-
| DataType::Float64
1185-
| DataType::Utf8
1186-
)
1187-
}
1188-
DataType::Float32 | DataType::Float64 => matches!(
1169+
if from_type == to_type {
1170+
return true;
1171+
}
1172+
match from_type {
1173+
DataType::Null => {
1174+
matches!(to_type, DataType::List(_))
1175+
}
1176+
DataType::Boolean => is_df_cast_from_bool_spark_compatible(to_type),
1177+
DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 => {
1178+
matches!(
11891179
to_type,
11901180
DataType::Boolean
11911181
| DataType::Int8
@@ -1194,34 +1184,46 @@ fn is_datafusion_spark_compatible(from_type: &DataType, to_type: &DataType) -> b
11941184
| DataType::Int64
11951185
| DataType::Float32
11961186
| DataType::Float64
1197-
),
1198-
DataType::Decimal128(_, _) | DataType::Decimal256(_, _) => matches!(
1187+
| DataType::Utf8
1188+
)
1189+
}
1190+
DataType::Float32 | DataType::Float64 => matches!(
1191+
to_type,
1192+
DataType::Boolean
1193+
| DataType::Int8
1194+
| DataType::Int16
1195+
| DataType::Int32
1196+
| DataType::Int64
1197+
| DataType::Float32
1198+
| DataType::Float64
1199+
),
1200+
DataType::Decimal128(_, _) | DataType::Decimal256(_, _) => matches!(
1201+
to_type,
1202+
DataType::Int8
1203+
| DataType::Int16
1204+
| DataType::Int32
1205+
| DataType::Int64
1206+
| DataType::Float32
1207+
| DataType::Float64
1208+
| DataType::Decimal128(_, _)
1209+
| DataType::Decimal256(_, _)
1210+
| DataType::Utf8 // note that there can be formatting differences
1211+
),
1212+
DataType::Utf8 => matches!(to_type, DataType::Binary),
1213+
DataType::Date32 => matches!(to_type, DataType::Int32 | DataType::Utf8),
1214+
DataType::Timestamp(_, _) => {
1215+
matches!(
11991216
to_type,
1200-
DataType::Int8
1201-
| DataType::Int16
1202-
| DataType::Int32
1203-
| DataType::Int64
1204-
| DataType::Float32
1205-
| DataType::Float64
1206-
| DataType::Decimal128(_, _)
1207-
| DataType::Decimal256(_, _)
1208-
| DataType::Utf8 // note that there can be formatting differences
1209-
),
1210-
DataType::Utf8 => matches!(to_type, DataType::Binary),
1211-
DataType::Date32 => matches!(to_type, DataType::Int32 | DataType::Utf8),
1212-
DataType::Timestamp(_, _) => {
1213-
matches!(
1214-
to_type,
1215-
DataType::Int64 | DataType::Date32 | DataType::Utf8 | DataType::Timestamp(_, _)
1216-
)
1217-
}
1218-
DataType::Binary => {
1219-
// note that this is not completely Spark compatible because
1220-
// DataFusion only supports binary data containing valid UTF-8 strings
1221-
matches!(to_type, DataType::Utf8)
1222-
}
1223-
_ => false,
1217+
DataType::Int64 | DataType::Date32 | DataType::Utf8 | DataType::Timestamp(_, _)
1218+
)
12241219
}
1220+
DataType::Binary => {
1221+
// note that this is not completely Spark compatible because
1222+
// DataFusion only supports binary data containing valid UTF-8 strings
1223+
matches!(to_type, DataType::Utf8)
1224+
}
1225+
_ => false,
1226+
}
12251227
}
12261228

12271229
/// Cast between struct types based on logic in

0 commit comments

Comments
 (0)