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::Decimal128(_, _)
55            | DataType::Decimal256(_, _),
56    )
57}
58
59/// Determine whether the given data type `dt` is `Null`.
60pub fn is_null(dt: &DataType) -> bool {
61    *dt == DataType::Null
62}
63
64/// Determine whether the given data type `dt` is a `Timestamp`.
65pub fn is_timestamp(dt: &DataType) -> bool {
66    matches!(dt, DataType::Timestamp(_, _))
67}
68
69/// Determine whether the given data type 'dt' is a `Interval`.
70pub fn is_interval(dt: &DataType) -> bool {
71    matches!(dt, DataType::Interval(_))
72}
73
74/// Determine whether the given data type `dt` is a `Date` or `Timestamp`.
75pub fn is_datetime(dt: &DataType) -> bool {
76    matches!(
77        dt,
78        DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, _)
79    )
80}
81
82/// Determine whether the given data type `dt` is a `Utf8` or `LargeUtf8`.
83pub fn is_utf8_or_large_utf8(dt: &DataType) -> bool {
84    matches!(dt, DataType::Utf8 | DataType::LargeUtf8)
85}
86
87/// Determine whether the given data type `dt` is a `Decimal`.
88pub fn is_decimal(dt: &DataType) -> bool {
89    matches!(dt, DataType::Decimal128(_, _) | DataType::Decimal256(_, _))
90}