use arrow::datatypes::DataType;
use super::binary::{comparison_coercion, type_union_coercion};
fn fold_coerce(
initial_type: &DataType,
types: &[DataType],
coerce_fn: fn(&DataType, &DataType) -> Option<DataType>,
) -> Option<DataType> {
types
.iter()
.try_fold(initial_type.clone(), |left_type, right_type| {
coerce_fn(&left_type, right_type)
})
}
pub fn get_coerce_type_for_list(
expr_type: &DataType,
list_types: &[DataType],
) -> Option<DataType> {
fold_coerce(expr_type, list_types, comparison_coercion)
}
pub fn get_coerce_type_for_case_when(
when_types: &[DataType],
case_type: &DataType,
) -> Option<DataType> {
fold_coerce(case_type, when_types, comparison_coercion)
}
pub fn get_coerce_type_for_case_expression(
then_types: &[DataType],
else_type: Option<&DataType>,
) -> Option<DataType> {
let (initial_type, remaining) = match else_type {
None => then_types.split_first()?,
Some(data_type) => (data_type, then_types),
};
fold_coerce(initial_type, remaining, type_union_coercion)
}