Skip to content

Commit ac21c83

Browse files
committed
clean up code
1 parent ecabac1 commit ac21c83

File tree

3 files changed

+29
-72
lines changed

3 files changed

+29
-72
lines changed

datafusion/expr-common/src/type_coercion/binary.rs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ use arrow::datatypes::{
2929
DECIMAL128_MAX_SCALE, DECIMAL256_MAX_PRECISION, DECIMAL256_MAX_SCALE,
3030
};
3131
use datafusion_common::types::NativeType;
32-
use datafusion_common::{
33-
exec_datafusion_err, exec_err, internal_err, plan_datafusion_err, plan_err, Result,
34-
};
32+
use datafusion_common::{exec_err, internal_err, plan_datafusion_err, plan_err, Result};
3533
use itertools::Itertools;
3634

3735
/// The type signature of an instantiation of binary operator expression such as
@@ -869,54 +867,6 @@ fn get_wider_decimal_type(
869867
}
870868
}
871869

872-
/// Returns the wider type among arguments `lhs` and `rhs`.
873-
/// The wider type is the type that can safely represent values from both types
874-
/// without information loss. Returns an Error if types are incompatible.
875-
pub fn get_wider_type(lhs: &DataType, rhs: &DataType) -> Result<DataType> {
876-
use arrow::datatypes::DataType::*;
877-
Ok(match (lhs, rhs) {
878-
(lhs, rhs) if lhs == rhs => lhs.clone(),
879-
// Right UInt is larger than left UInt.
880-
(UInt8, UInt16 | UInt32 | UInt64) | (UInt16, UInt32 | UInt64) | (UInt32, UInt64) |
881-
// Right Int is larger than left Int.
882-
(Int8, Int16 | Int32 | Int64) | (Int16, Int32 | Int64) | (Int32, Int64) |
883-
// Right Float is larger than left Float.
884-
(Float16, Float32 | Float64) | (Float32, Float64) |
885-
// Right String is larger than left String.
886-
(Utf8, LargeUtf8) |
887-
// Any right type is wider than a left hand side Null.
888-
(Null, _) => rhs.clone(),
889-
// Left UInt is larger than right UInt.
890-
(UInt16 | UInt32 | UInt64, UInt8) | (UInt32 | UInt64, UInt16) | (UInt64, UInt32) |
891-
// Left Int is larger than right Int.
892-
(Int16 | Int32 | Int64, Int8) | (Int32 | Int64, Int16) | (Int64, Int32) |
893-
// Left Float is larger than right Float.
894-
(Float32 | Float64, Float16) | (Float64, Float32) |
895-
// Left String is larger than right String.
896-
(LargeUtf8, Utf8) |
897-
// Any left type is wider than a right hand side Null.
898-
(_, Null) => lhs.clone(),
899-
(List(lhs_field), List(rhs_field)) => {
900-
let field_type =
901-
get_wider_type(lhs_field.data_type(), rhs_field.data_type())?;
902-
if lhs_field.name() != rhs_field.name() {
903-
return Err(exec_datafusion_err!(
904-
"There is no wider type that can represent both {lhs} and {rhs}."
905-
));
906-
}
907-
assert_eq!(lhs_field.name(), rhs_field.name());
908-
let field_name = lhs_field.name();
909-
let nullable = lhs_field.is_nullable() | rhs_field.is_nullable();
910-
List(Arc::new(Field::new(field_name, field_type, nullable)))
911-
}
912-
(_, _) => {
913-
return Err(exec_datafusion_err!(
914-
"There is no wider type that can represent both {lhs} and {rhs}."
915-
));
916-
}
917-
})
918-
}
919-
920870
/// Convert the numeric data type to the decimal data type.
921871
/// We support signed and unsigned integer types and floating-point type.
922872
fn coerce_numeric_type_to_decimal(numeric_type: &DataType) -> Option<DataType> {

datafusion/functions-nested/src/concat.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use datafusion_common::{
2929
cast::as_generic_list_array, exec_err, not_impl_err, plan_err, utils::list_ndims,
3030
};
3131
use datafusion_expr::{
32-
type_coercion::binary::get_wider_type, ColumnarValue, Documentation, ScalarUDFImpl,
32+
ColumnarValue, Documentation, ScalarUDFImpl,
3333
Signature, Volatility,
3434
};
3535
use datafusion_macros::user_doc;
@@ -276,25 +276,32 @@ impl ScalarUDFImpl for ArrayConcat {
276276
let mut expr_type = DataType::Null;
277277
let mut max_dims = 0;
278278
for arg_type in arg_types {
279-
match arg_type {
280-
DataType::List(field) => {
281-
if !field.data_type().equals_datatype(&DataType::Null) {
282-
let dims = list_ndims(arg_type);
283-
expr_type = match max_dims.cmp(&dims) {
284-
Ordering::Greater => expr_type,
285-
Ordering::Equal => get_wider_type(&expr_type, arg_type)?,
286-
Ordering::Less => {
287-
max_dims = dims;
288-
arg_type.clone()
289-
}
290-
};
279+
let DataType::List(field) = arg_type else {
280+
return plan_err!(
281+
"The array_concat function can only accept list as the args."
282+
);
283+
};
284+
if !field.data_type().equals_datatype(&DataType::Null) {
285+
let dims = list_ndims(arg_type);
286+
expr_type = match max_dims.cmp(&dims) {
287+
Ordering::Greater => expr_type,
288+
Ordering::Equal => {
289+
if expr_type == DataType::Null {
290+
arg_type.clone()
291+
} else if !expr_type.equals_datatype(arg_type) {
292+
return plan_err!(
293+
"It is not possible to concatenate arrays of different types. Expected: {}, got: {}", expr_type, arg_type
294+
);
295+
} else {
296+
expr_type
297+
}
291298
}
292-
}
293-
_ => {
294-
return plan_err!(
295-
"The array_concat function can only accept list as the args."
296-
)
297-
}
299+
300+
Ordering::Less => {
301+
max_dims = dims;
302+
arg_type.clone()
303+
}
304+
};
298305
}
299306
}
300307

datafusion/sqllogictest/test_files/array.slt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,14 +2876,14 @@ select array_concat(
28762876
[1, 2, 3]
28772877

28782878
# Concatenating Mixed types (doesn't work)
2879-
query error DataFusion error: Arrow error: Invalid argument error: It is not possible to concatenate arrays of different data types\.
2879+
query error DataFusion error: Error during planning: It is not possible to concatenate arrays of different types\. Expected: List\(Field \{ name: "item", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: \{\} \}\), got: List\(Field \{ name: "item", data_type: LargeUtf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: \{\} \}\)
28802880
select array_concat(
28812881
[arrow_cast('1', 'Utf8'), arrow_cast('2', 'Utf8')],
28822882
[arrow_cast('3', 'LargeUtf8')]
28832883
);
28842884

28852885
# Concatenating Mixed types (doesn't work)
2886-
query error DataFusion error: Execution error: There is no wider type that can represent both Utf8 and Utf8View\.
2886+
query error DataFusion error: Error during planning: It is not possible to concatenate arrays of different types\. Expected: List\(Field \{ name: "item", data_type: Utf8, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: \{\} \}\), got: List\(Field \{ name: "item", data_type: Utf8View, nullable: true, dict_id: 0, dict_is_ordered: false, metadata: \{\} \}\)
28872887
select array_concat(
28882888
[arrow_cast('1', 'Utf8'), arrow_cast('2', 'Utf8')],
28892889
[arrow_cast('3', 'Utf8View')]

0 commit comments

Comments
 (0)