Skip to content

Commit 6751f44

Browse files
refactor: Refactor spark make_interval signature away from user defined (#19027)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Part of #12725 ## Rationale for this change As per the goal stated that we should avoid using the `user_defined` in useful places <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? Refactored the `spark/mark_interval` <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> --------- Co-authored-by: Jeffrey Vo <[email protected]>
1 parent 0c6b654 commit 6751f44

File tree

1 file changed

+61
-26
lines changed

1 file changed

+61
-26
lines changed

datafusion/spark/src/function/datetime/make_interval.rs

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ use arrow::array::{Array, ArrayRef, IntervalMonthDayNanoBuilder, PrimitiveArray}
2222
use arrow::datatypes::DataType::Interval;
2323
use arrow::datatypes::IntervalUnit::MonthDayNano;
2424
use arrow::datatypes::{DataType, IntervalMonthDayNano};
25-
use datafusion_common::{
26-
exec_err, plan_datafusion_err, DataFusionError, Result, ScalarValue,
27-
};
25+
use datafusion_common::types::{logical_float64, logical_int32, NativeType};
26+
use datafusion_common::{plan_datafusion_err, DataFusionError, Result, ScalarValue};
2827
use datafusion_expr::{
29-
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
28+
Coercion, ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature,
29+
TypeSignatureClass, Volatility,
3030
};
3131
use datafusion_functions::utils::make_scalar_function;
3232

@@ -43,8 +43,64 @@ impl Default for SparkMakeInterval {
4343

4444
impl SparkMakeInterval {
4545
pub fn new() -> Self {
46+
let int32 = Coercion::new_implicit(
47+
TypeSignatureClass::Native(logical_int32()),
48+
vec![TypeSignatureClass::Integer],
49+
NativeType::Int32,
50+
);
51+
52+
let float64 = Coercion::new_implicit(
53+
TypeSignatureClass::Native(logical_float64()),
54+
vec![TypeSignatureClass::Numeric],
55+
NativeType::Float64,
56+
);
57+
58+
let variants = vec![
59+
TypeSignature::Nullary,
60+
// year
61+
TypeSignature::Coercible(vec![int32.clone()]),
62+
// year, month
63+
TypeSignature::Coercible(vec![int32.clone(), int32.clone()]),
64+
// year, month, week
65+
TypeSignature::Coercible(vec![int32.clone(), int32.clone(), int32.clone()]),
66+
// year, month, week, day
67+
TypeSignature::Coercible(vec![
68+
int32.clone(),
69+
int32.clone(),
70+
int32.clone(),
71+
int32.clone(),
72+
]),
73+
// year, month, week, day, hour
74+
TypeSignature::Coercible(vec![
75+
int32.clone(),
76+
int32.clone(),
77+
int32.clone(),
78+
int32.clone(),
79+
int32.clone(),
80+
]),
81+
// year, month, week, day, hour, minute
82+
TypeSignature::Coercible(vec![
83+
int32.clone(),
84+
int32.clone(),
85+
int32.clone(),
86+
int32.clone(),
87+
int32.clone(),
88+
int32.clone(),
89+
]),
90+
// year, month, week, day, hour, minute, second
91+
TypeSignature::Coercible(vec![
92+
int32.clone(),
93+
int32.clone(),
94+
int32.clone(),
95+
int32.clone(),
96+
int32.clone(),
97+
int32.clone(),
98+
float64.clone(),
99+
]),
100+
];
101+
46102
Self {
47-
signature: Signature::user_defined(Volatility::Immutable),
103+
signature: Signature::one_of(variants, Volatility::Immutable),
48104
}
49105
}
50106
}
@@ -74,27 +130,6 @@ impl ScalarUDFImpl for SparkMakeInterval {
74130
}
75131
make_scalar_function(make_interval_kernel, vec![])(&args.args)
76132
}
77-
78-
fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
79-
let length = arg_types.len();
80-
match length {
81-
x if x > 7 => {
82-
exec_err!(
83-
"make_interval expects between 0 and 7 arguments, got {}",
84-
arg_types.len()
85-
)
86-
}
87-
_ => Ok((0..arg_types.len())
88-
.map(|i| {
89-
if i == 6 {
90-
DataType::Float64
91-
} else {
92-
DataType::Int32
93-
}
94-
})
95-
.collect()),
96-
}
97-
}
98133
}
99134

100135
fn make_interval_kernel(args: &[ArrayRef]) -> Result<ArrayRef, DataFusionError> {

0 commit comments

Comments
 (0)