datafusion_expr/type_coercion/
other.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use arrow::datatypes::DataType;
19
20use super::binary::comparison_coercion;
21
22/// Attempts to coerce the types of `list_types` to be comparable with the
23/// `expr_type`.
24/// Returns the common data type for `expr_type` and `list_types`
25pub fn get_coerce_type_for_list(
26    expr_type: &DataType,
27    list_types: &[DataType],
28) -> Option<DataType> {
29    list_types
30        .iter()
31        .try_fold(expr_type.clone(), |left_type, right_type| {
32            comparison_coercion(&left_type, right_type)
33        })
34}
35
36/// Find a common coerceable type for all `when_or_then_types` as well
37/// and the `case_or_else_type`, if specified.
38/// Returns the common data type for `when_or_then_types` and `case_or_else_type`
39pub fn get_coerce_type_for_case_expression(
40    when_or_then_types: &[DataType],
41    case_or_else_type: Option<&DataType>,
42) -> Option<DataType> {
43    let case_or_else_type = match case_or_else_type {
44        None => when_or_then_types[0].clone(),
45        Some(data_type) => data_type.clone(),
46    };
47    when_or_then_types
48        .iter()
49        .try_fold(case_or_else_type, |left_type, right_type| {
50            // TODO: now just use the `equal` coercion rule for case when. If find the issue, and
51            // refactor again.
52            comparison_coercion(&left_type, right_type)
53        })
54}