Skip to content

Commit 8469aa1

Browse files
authored
minor: implement more arms for get_data_types() for NativeType (#19449)
Work towards a minor TODO item. Also some minor doc updates.
1 parent 8ac500b commit 8469aa1

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

datafusion/expr-common/src/signature.rs

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
2020
use std::fmt::Display;
2121
use std::hash::Hash;
22+
use std::sync::Arc;
2223

2324
use crate::type_coercion::aggregates::NUMERICS;
24-
use arrow::datatypes::{DataType, Decimal128Type, DecimalType, IntervalUnit, TimeUnit};
25+
use arrow::datatypes::{
26+
DECIMAL32_MAX_PRECISION, DECIMAL64_MAX_PRECISION, DECIMAL128_MAX_PRECISION, DataType,
27+
Decimal128Type, DecimalType, Field, IntervalUnit, TimeUnit,
28+
};
2529
use datafusion_common::types::{LogicalType, LogicalTypeRef, NativeType};
2630
use datafusion_common::utils::ListCoercion;
2731
use datafusion_common::{Result, internal_err, plan_err};
@@ -328,14 +332,23 @@ impl TypeSignature {
328332
/// arguments that can be coerced to a particular class of types.
329333
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Hash)]
330334
pub enum TypeSignatureClass {
335+
/// Timestamps, allowing arbitrary (or no) timezones
331336
Timestamp,
337+
/// All time types
332338
Time,
339+
/// All interval types
333340
Interval,
341+
/// All duration types
334342
Duration,
343+
/// A specific native type
335344
Native(LogicalTypeRef),
345+
/// Signed and unsigned integers
336346
Integer,
347+
/// All float types
337348
Float,
349+
/// All decimal types, allowing arbitrary precision & scale
338350
Decimal,
351+
/// Integers, floats and decimals
339352
Numeric,
340353
/// Encompasses both the native Binary/LargeBinary types as well as arbitrarily sized FixedSizeBinary types
341354
Binary,
@@ -888,8 +901,56 @@ fn get_data_types(native_type: &NativeType) -> Vec<DataType> {
888901
NativeType::String => {
889902
vec![DataType::Utf8, DataType::LargeUtf8, DataType::Utf8View]
890903
}
891-
// TODO: support other native types
892-
_ => vec![],
904+
NativeType::Decimal(precision, scale) => {
905+
// We assume incoming NativeType is valid already, in terms of precision & scale
906+
let mut types = vec![DataType::Decimal256(*precision, *scale)];
907+
if *precision <= DECIMAL32_MAX_PRECISION {
908+
types.push(DataType::Decimal32(*precision, *scale));
909+
}
910+
if *precision <= DECIMAL64_MAX_PRECISION {
911+
types.push(DataType::Decimal64(*precision, *scale));
912+
}
913+
if *precision <= DECIMAL128_MAX_PRECISION {
914+
types.push(DataType::Decimal128(*precision, *scale));
915+
}
916+
types
917+
}
918+
NativeType::Timestamp(time_unit, timezone) => {
919+
vec![DataType::Timestamp(*time_unit, timezone.to_owned())]
920+
}
921+
NativeType::Time(TimeUnit::Second) => vec![DataType::Time32(TimeUnit::Second)],
922+
NativeType::Time(TimeUnit::Millisecond) => {
923+
vec![DataType::Time32(TimeUnit::Millisecond)]
924+
}
925+
NativeType::Time(TimeUnit::Microsecond) => {
926+
vec![DataType::Time64(TimeUnit::Microsecond)]
927+
}
928+
NativeType::Time(TimeUnit::Nanosecond) => {
929+
vec![DataType::Time64(TimeUnit::Nanosecond)]
930+
}
931+
NativeType::Duration(time_unit) => vec![DataType::Duration(*time_unit)],
932+
NativeType::Interval(interval_unit) => vec![DataType::Interval(*interval_unit)],
933+
NativeType::FixedSizeBinary(size) => vec![DataType::FixedSizeBinary(*size)],
934+
NativeType::FixedSizeList(logical_field, size) => {
935+
get_data_types(logical_field.logical_type.native())
936+
.iter()
937+
.map(|child_dt| {
938+
let field = Field::new(
939+
logical_field.name.clone(),
940+
child_dt.clone(),
941+
logical_field.nullable,
942+
);
943+
DataType::FixedSizeList(Arc::new(field), *size)
944+
})
945+
.collect()
946+
}
947+
// TODO: implement for nested types
948+
NativeType::List(_)
949+
| NativeType::Struct(_)
950+
| NativeType::Union(_)
951+
| NativeType::Map(_) => {
952+
vec![]
953+
}
893954
}
894955
}
895956

0 commit comments

Comments
 (0)