datafusion-expr 56.0.0

Logical plan and expression representation for DataFusion query engine
Documentation
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements

use arrow::datatypes::{DataType, Field};
use datafusion_common::{Result, plan_datafusion_err, plan_err};

use super::binary::{comparison_coercion, type_union_coercion};

/// Fold `coerce_fn` over `types`, starting from `initial_type`.
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)
        })
}

/// Infer a common output type for one UNION column, in input order.
///
/// Field names are used only to report incompatible inputs. This does not
/// insert casts or require input expressions to have been analyzed.
pub fn coerce_union_type<'a>(
    fields: impl IntoIterator<Item = &'a Field>,
) -> Result<DataType> {
    let mut fields = fields.into_iter();
    let Some(first) = fields.next() else {
        return plan_err!("UNION requires at least one input field");
    };
    fields.try_fold(first.data_type().clone(), |data_type, field| {
        type_union_coercion(&data_type, field.data_type()).ok_or_else(|| {
            plan_datafusion_err!(
                "Incompatible inputs for Union: Previous inputs were \
                 of type {}, but got incompatible type {} on column '{}'",
                data_type,
                field.data_type(),
                field.name()
            )
        })
    })
}

/// Attempts to coerce the types of `list_types` to be comparable with the
/// `expr_type` for IN list predicates.
/// Returns the common data type for `expr_type` and `list_types`.
///
/// Uses comparison coercion because `x IN (a, b)` is semantically equivalent
/// to `x = a OR x = b`.
///
/// # Deprecated
///
/// This no longer matches how the planner coerces `IN`: `=` reads a
/// timezone-naive timestamp in `datafusion.execution.time_zone` when it is
/// compared against a timezone-aware one, and this function cannot, because it
/// has nowhere to take that setting from. Fold
/// [`comparison_coercion_with_session_timezone`] over the values instead:
///
/// ```
/// # use arrow::datatypes::DataType;
/// # use datafusion_expr::type_coercion::binary::comparison_coercion_with_session_timezone;
/// # let expr_type = DataType::Int32;
/// # let list_types = [DataType::Int64];
/// # let session_time_zone = Some("+08:00");
/// let coerced = list_types.iter().try_fold(expr_type, |left, right| {
///     comparison_coercion_with_session_timezone(&left, right, session_time_zone)
/// });
/// # assert_eq!(coerced, Some(DataType::Int64));
/// ```
///
/// [`comparison_coercion_with_session_timezone`]: super::binary::comparison_coercion_with_session_timezone
#[deprecated(
    since = "56.0.0",
    note = "Fold `comparison_coercion_with_session_timezone` over the values instead, so `IN` coerces exactly as `=` does"
)]
pub fn get_coerce_type_for_list(
    expr_type: &DataType,
    list_types: &[DataType],
) -> Option<DataType> {
    fold_coerce(expr_type, list_types, comparison_coercion)
}

/// Find a common coerceable type for `CASE expr WHEN val1 WHEN val2 ...`
/// conditions. Returns the common type for `case_type` and all `when_types`.
///
/// Uses comparison coercion because `CASE expr WHEN val` is semantically
/// equivalent to `expr = val`.
///
/// # Deprecated
///
/// Deprecated for the same reason as [`get_coerce_type_for_list`]: it cannot
/// apply the `datafusion.execution.time_zone` rule that `=` applies. Fold
/// [`comparison_coercion_with_session_timezone`] over `when_types` instead.
///
/// [`comparison_coercion_with_session_timezone`]: super::binary::comparison_coercion_with_session_timezone
#[deprecated(
    since = "56.0.0",
    note = "Fold `comparison_coercion_with_session_timezone` over the values instead, so `CASE expr WHEN` coerces exactly as `=` does"
)]
pub fn get_coerce_type_for_case_when(
    when_types: &[DataType],
    case_type: &DataType,
) -> Option<DataType> {
    fold_coerce(case_type, when_types, comparison_coercion)
}

/// Find a common coerceable type for CASE THEN/ELSE result expressions.
/// Returns the common data type for `then_types` and `else_type`.
///
/// Uses type union coercion because the result branches must be brought to a
/// common type (like UNION), not compared.
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)
}