datafusion_expr/type_coercion/
mod.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
18//! Type coercion rules for DataFusion
19//!
20//! Coercion is performed automatically by DataFusion when the types
21//! of arguments passed to a function or needed by operators do not
22//! exactly match the types required by that function / operator. In
23//! this case, DataFusion will attempt to *coerce* the arguments to
24//! types accepted by the function by inserting CAST operations.
25//!
26//! CAST operations added by coercion are lossless and never discard
27//! information.
28//!
29//! For example coercion from i32 -> i64 might be
30//! performed because all valid i32 values can be represented using an
31//! i64. However, i64 -> i32 is never performed as there are i64
32//! values which can not be represented by i32 values.
33
34pub mod aggregates {
35    pub use datafusion_expr_common::type_coercion::aggregates::*;
36}
37pub mod functions;
38pub mod other;
39
40pub use datafusion_expr_common::type_coercion::binary;
41
42use arrow::datatypes::DataType;
43/// Determine whether the given data type `dt` represents signed numeric values.
44pub fn is_signed_numeric(dt: &DataType) -> bool {
45    matches!(
46        dt,
47        DataType::Int8
48            | DataType::Int16
49            | DataType::Int32
50            | DataType::Int64
51            | DataType::Float16
52            | DataType::Float32
53            | DataType::Float64
54            | DataType::Decimal32(_, _)
55            | DataType::Decimal64(_, _)
56            | DataType::Decimal128(_, _)
57            | DataType::Decimal256(_, _),
58    )
59}
60
61/// Determine whether the given data type `dt` is `Null`.
62pub fn is_null(dt: &DataType) -> bool {
63    *dt == DataType::Null
64}
65
66/// Determine whether the given data type `dt` is a `Timestamp`.
67pub fn is_timestamp(dt: &DataType) -> bool {
68    matches!(dt, DataType::Timestamp(_, _))
69}
70
71/// Determine whether the given data type 'dt' is a `Interval`.
72pub fn is_interval(dt: &DataType) -> bool {
73    matches!(dt, DataType::Interval(_))
74}
75
76/// Determine whether the given data type `dt` is a `Date` or `Timestamp`.
77pub fn is_datetime(dt: &DataType) -> bool {
78    matches!(
79        dt,
80        DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, _)
81    )
82}
83
84/// Determine whether the given data type `dt` is a `Utf8` or `Utf8View` or `LargeUtf8`.
85pub fn is_utf8_or_utf8view_or_large_utf8(dt: &DataType) -> bool {
86    matches!(
87        dt,
88        DataType::Utf8 | DataType::Utf8View | DataType::LargeUtf8
89    )
90}
91
92/// Determine whether the given data type `dt` is a `Decimal`.
93pub fn is_decimal(dt: &DataType) -> bool {
94    matches!(
95        dt,
96        DataType::Decimal32(_, _)
97            | DataType::Decimal64(_, _)
98            | DataType::Decimal128(_, _)
99            | DataType::Decimal256(_, _)
100    )
101}