Skip to content

Commit 806c6a6

Browse files
committed
replace all coerciblev1
1 parent cab2eef commit 806c6a6

File tree

4 files changed

+65
-29
lines changed

4 files changed

+65
-29
lines changed

datafusion/expr-common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ path = "src/lib.rs"
4040
arrow = { workspace = true }
4141
datafusion-common = { workspace = true }
4242
itertools = { workspace = true }
43+
indexmap = { workspace = true }
4344
paste = "^1.0"

datafusion/expr-common/src/signature.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ use std::num::NonZeroUsize;
2424
use crate::type_coercion::aggregates::NUMERICS;
2525
use arrow::datatypes::{DataType, IntervalUnit, TimeUnit};
2626
use datafusion_common::types::{LogicalType, LogicalTypeRef, NativeType};
27-
use datafusion_common::{HashSet, Result};
27+
use datafusion_common::Result;
28+
use indexmap::IndexSet;
2829
use itertools::Itertools;
2930

3031
/// Constant that is used as a placeholder for any valid timezone.
@@ -230,19 +231,21 @@ impl Display for TypeSignatureClass {
230231
impl TypeSignatureClass {
231232
/// Return the default casted type for the given `TypeSignatureClass`
232233
/// We return the largest common type for the given `TypeSignatureClass`
233-
pub fn default_casted_type(&self, data_type: &DataType) -> Result<DataType> {
234+
pub fn default_casted_type(
235+
&self,
236+
logical_type: &NativeType,
237+
data_type: &DataType,
238+
) -> Result<DataType> {
234239
Ok(match self {
240+
// TODO: Able to elimnate this special case?
241+
// Not consistent with Postgres and DuckDB but to avoid regression we implicit cast string to timestamp
242+
TypeSignatureClass::Timestamp if logical_type == &NativeType::String => {
243+
DataType::Timestamp(TimeUnit::Nanosecond, None)
244+
}
235245
TypeSignatureClass::Native(logical_type) => {
236246
return logical_type.native().default_cast_for(data_type)
237247
}
238-
TypeSignatureClass::Timestamp => {
239-
DataType::Timestamp(TimeUnit::Nanosecond, None)
240-
}
241-
TypeSignatureClass::Date => DataType::Date64,
242-
TypeSignatureClass::Time => DataType::Time64(TimeUnit::Nanosecond),
243-
TypeSignatureClass::Interval => DataType::Interval(IntervalUnit::DayTime),
244-
TypeSignatureClass::Duration => DataType::Duration(TimeUnit::Nanosecond),
245-
TypeSignatureClass::Integer => DataType::Int64,
248+
_ => data_type.clone(),
246249
})
247250
}
248251
}
@@ -407,7 +410,7 @@ impl TypeSignature {
407410
TypeSignature::CoercibleV2(coercions) => coercions
408411
.iter()
409412
.map(|c| {
410-
let mut all_types: HashSet<DataType> =
413+
let mut all_types: IndexSet<DataType> =
411414
get_possible_types_from_signature_classes(&c.desired_type)
412415
.into_iter()
413416
.collect();

datafusion/expr/src/type_coercion/functions.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ fn is_well_supported_signature(type_signature: &TypeSignature) -> bool {
203203
| TypeSignature::Numeric(_)
204204
| TypeSignature::String(_)
205205
| TypeSignature::Coercible(_)
206+
| TypeSignature::CoercibleV2(_)
206207
| TypeSignature::Any(_)
207208
| TypeSignature::Nullary
208209
| TypeSignature::Comparable(_)
@@ -617,8 +618,9 @@ fn get_valid_types(
617618
.iter()
618619
.any(|t| is_matched_type(t, &current_logical_type))
619620
{
620-
let casted_type =
621-
param.desired_type.default_casted_type(current_type)?;
621+
let casted_type = param
622+
.desired_type
623+
.default_casted_type(&current_logical_type, current_type)?;
622624
new_types.push(casted_type);
623625
} else {
624626
return internal_err!(

datafusion/functions/src/datetime/date_part.rs

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use datafusion_expr::{
4545
ColumnarValue, Documentation, ReturnInfo, ReturnTypeArgs, ScalarUDFImpl, Signature,
4646
TypeSignature, Volatility,
4747
};
48-
use datafusion_expr_common::signature::TypeSignatureClass;
48+
use datafusion_expr_common::signature::{Coercion, TypeSignatureClass};
4949
use datafusion_macros::user_doc;
5050

5151
#[user_doc(
@@ -95,25 +95,55 @@ impl DatePartFunc {
9595
Self {
9696
signature: Signature::one_of(
9797
vec![
98-
TypeSignature::Coercible(vec![
99-
TypeSignatureClass::Native(logical_string()),
100-
TypeSignatureClass::Timestamp,
98+
TypeSignature::CoercibleV2(vec![
99+
Coercion {
100+
desired_type: TypeSignatureClass::Native(logical_string()),
101+
allowed_casts: vec![],
102+
},
103+
Coercion {
104+
desired_type: TypeSignatureClass::Timestamp,
105+
allowed_casts: vec![],
106+
},
101107
]),
102-
TypeSignature::Coercible(vec![
103-
TypeSignatureClass::Native(logical_string()),
104-
TypeSignatureClass::Date,
108+
TypeSignature::CoercibleV2(vec![
109+
Coercion {
110+
desired_type: TypeSignatureClass::Native(logical_string()),
111+
allowed_casts: vec![],
112+
},
113+
Coercion {
114+
desired_type: TypeSignatureClass::Date,
115+
allowed_casts: vec![],
116+
},
105117
]),
106-
TypeSignature::Coercible(vec![
107-
TypeSignatureClass::Native(logical_string()),
108-
TypeSignatureClass::Time,
118+
TypeSignature::CoercibleV2(vec![
119+
Coercion {
120+
desired_type: TypeSignatureClass::Native(logical_string()),
121+
allowed_casts: vec![],
122+
},
123+
Coercion {
124+
desired_type: TypeSignatureClass::Time,
125+
allowed_casts: vec![],
126+
},
109127
]),
110-
TypeSignature::Coercible(vec![
111-
TypeSignatureClass::Native(logical_string()),
112-
TypeSignatureClass::Interval,
128+
TypeSignature::CoercibleV2(vec![
129+
Coercion {
130+
desired_type: TypeSignatureClass::Native(logical_string()),
131+
allowed_casts: vec![],
132+
},
133+
Coercion {
134+
desired_type: TypeSignatureClass::Interval,
135+
allowed_casts: vec![],
136+
},
113137
]),
114-
TypeSignature::Coercible(vec![
115-
TypeSignatureClass::Native(logical_string()),
116-
TypeSignatureClass::Duration,
138+
TypeSignature::CoercibleV2(vec![
139+
Coercion {
140+
desired_type: TypeSignatureClass::Native(logical_string()),
141+
allowed_casts: vec![],
142+
},
143+
Coercion {
144+
desired_type: TypeSignatureClass::Duration,
145+
allowed_casts: vec![],
146+
},
117147
]),
118148
],
119149
Volatility::Immutable,

0 commit comments

Comments
 (0)