datafusion_expr/type_coercion/mod.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Type coercion rules for DataFusion
6//!
7//! Coercion is performed automatically by DataFusion when the types
8//! of arguments passed to a function or needed by operators do not
9//! exactly match the types required by that function / operator. In
10//! this case, DataFusion will attempt to *coerce* the arguments to
11//! types accepted by the function by inserting CAST operations.
12//!
13//! CAST operations added by coercion are lossless and never discard
14//! information.
15//!
16//! For example coercion from i32 -> i64 might be
17//! performed because all valid i32 values can be represented using an
18//! i64. However, i64 -> i32 is never performed as there are i64
19//! values which can not be represented by i32 values.
20
21pub mod aggregates {
22 pub use datafusion_expr_common::type_coercion::aggregates::*;
23}
24pub mod functions;
25pub mod other;
26
27pub use datafusion_expr_common::type_coercion::binary;
28
29use arrow::datatypes::DataType;
30/// Determine whether the given data type `dt` represents signed numeric values.
31pub fn is_signed_numeric(dt: &DataType) -> bool {
32 matches!(
33 dt,
34 DataType::Int8
35 | DataType::Int16
36 | DataType::Int32
37 | DataType::Int64
38 | DataType::Float16
39 | DataType::Float32
40 | DataType::Float64
41 | DataType::Decimal32(_, _)
42 | DataType::Decimal64(_, _)
43 | DataType::Decimal128(_, _)
44 | DataType::Decimal256(_, _),
45 )
46}
47
48/// Determine whether the given data type `dt` is a `Timestamp`.
49pub fn is_timestamp(dt: &DataType) -> bool {
50 matches!(dt, DataType::Timestamp(_, _))
51}
52
53/// Determine whether the given data type 'dt' is a `Interval`.
54pub fn is_interval(dt: &DataType) -> bool {
55 matches!(dt, DataType::Interval(_))
56}
57
58/// Determine whether the given data type `dt` is a `Date` or `Timestamp`.
59pub fn is_datetime(dt: &DataType) -> bool {
60 matches!(
61 dt,
62 DataType::Date32 | DataType::Date64 | DataType::Timestamp(_, _)
63 )
64}