Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions datafusion/common/src/types/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@
// specific language governing permissions and limitations
// under the License.

use arrow::datatypes::IntervalUnit::*;

use crate::types::{LogicalTypeRef, NativeType};
use std::sync::{Arc, LazyLock};

/// Create a singleton and accompanying static variable for a [`LogicalTypeRef`]
/// of a [`NativeType`].
/// * `name`: name of the static variable, must be unique.
/// * `getter`: name of the public function that will return the singleton instance
/// of the static variable.
/// * `ty`: the [`NativeType`].
macro_rules! singleton {
($name:ident, $getter:ident, $ty:ident) => {
static $name: LazyLock<LogicalTypeRef> =
Expand All @@ -31,6 +39,26 @@ macro_rules! singleton {
};
}

/// Similar to [`singleton`], but for native types that have variants, such as
/// `NativeType::Interval(MonthDayNano)`.
/// * `name`: name of the static variable, must be unique.
/// * `getter`: name of the public function that will return the singleton instance
/// of the static variable.
/// * `ty`: the [`NativeType`].
/// * `variant`: specific variant of the `ty`.
macro_rules! singleton_variant {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you potentially add some documentation here about what is different about this macro compared to the others in this module?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

($name:ident, $getter:ident, $ty:ident, $variant:ident) => {
static $name: LazyLock<LogicalTypeRef> =
LazyLock::new(|| Arc::new(NativeType::$ty($variant)));

#[doc = "Getter for singleton instance of a logical type representing"]
#[doc = concat!("[`NativeType::", stringify!($ty), "`] of unit [`", stringify!($variant),"`].`")]
pub fn $getter() -> LogicalTypeRef {
Arc::clone(&$name)
}
};
}

singleton!(LOGICAL_NULL, logical_null, Null);
singleton!(LOGICAL_BOOLEAN, logical_boolean, Boolean);
singleton!(LOGICAL_INT8, logical_int8, Int8);
Expand All @@ -47,3 +75,10 @@ singleton!(LOGICAL_FLOAT64, logical_float64, Float64);
singleton!(LOGICAL_DATE, logical_date, Date);
singleton!(LOGICAL_BINARY, logical_binary, Binary);
singleton!(LOGICAL_STRING, logical_string, String);

singleton_variant!(
LOGICAL_INTERVAL_MDN,
logical_interval_mdn,
Interval,
MonthDayNano
);
5 changes: 5 additions & 0 deletions datafusion/common/src/types/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,9 @@ impl NativeType {
pub fn is_binary(&self) -> bool {
matches!(self, NativeType::Binary | NativeType::FixedSizeBinary(_))
}

#[inline]
pub fn is_null(&self) -> bool {
matches!(self, NativeType::Null)
}
}
6 changes: 2 additions & 4 deletions datafusion/expr-common/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,7 @@ impl TypeSignatureClass {
}

/// Does the specified `NativeType` match this type signature class?
pub fn matches_native_type(
self: &TypeSignatureClass,
logical_type: &NativeType,
) -> bool {
pub fn matches_native_type(&self, logical_type: &NativeType) -> bool {
if logical_type == &NativeType::Null {
return true;
}
Expand Down Expand Up @@ -431,6 +428,7 @@ impl TypeSignatureClass {
TypeSignatureClass::Binary if native_type.is_binary() => {
Ok(origin_type.to_owned())
}
_ if native_type.is_null() => Ok(origin_type.to_owned()),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were missing this even though we check for it in matches_native_type; I think this will allow passing null types to coercible signatures without needing to explicitly specify null type handling

_ => internal_err!("May miss the matching logic in `matches_native_type`"),
}
}
Expand Down
Loading