Skip to content

Commit 37d7a65

Browse files
committed
feat(spark): set the signature to be taking exactly one Date32 type
Signed-off-by: Alan Tang <jmtangcs@gmail.com>
1 parent b61738d commit 37d7a65

File tree

2 files changed

+24
-44
lines changed

2 files changed

+24
-44
lines changed

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

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ use std::sync::Arc;
2121
use arrow::array::{ArrayRef, AsArray, Date32Array};
2222
use arrow::datatypes::{DataType, Date32Type};
2323
use chrono::{Datelike, Duration, NaiveDate};
24-
use datafusion_common::types::NativeType;
25-
use datafusion_common::{exec_datafusion_err, exec_err, plan_err, Result, ScalarValue};
24+
use datafusion_common::{exec_datafusion_err, internal_err, Result, ScalarValue};
2625
use datafusion_expr::{
2726
ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
2827
};
@@ -41,7 +40,7 @@ impl Default for SparkLastDay {
4140
impl SparkLastDay {
4241
pub fn new() -> Self {
4342
Self {
44-
signature: Signature::user_defined(Volatility::Immutable),
43+
signature: Signature::exact(vec![DataType::Date32], Volatility::Immutable),
4544
}
4645
}
4746
}
@@ -66,7 +65,7 @@ impl ScalarUDFImpl for SparkLastDay {
6665
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
6766
let ScalarFunctionArgs { args, .. } = args;
6867
let [arg] = args.as_slice() else {
69-
return exec_err!(
68+
return internal_err!(
7069
"Spark `last_day` function requires 1 argument, got {}",
7170
args.len()
7271
);
@@ -91,33 +90,14 @@ impl ScalarUDFImpl for SparkLastDay {
9190
Ok(Arc::new(result) as ArrayRef)
9291
}
9392
other => {
94-
exec_err!("Unsupported data type {other:?} for Spark function `last_day`")
93+
internal_err!("Unsupported data type {other:?} for Spark function `last_day`")
9594
}
9695
}?;
9796
Ok(ColumnarValue::Array(result))
9897
}
99-
other => exec_err!("Unsupported arg {other:?} for Spark function `last_day"),
100-
}
101-
}
102-
103-
fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
104-
if arg_types.len() != 1 {
105-
return exec_err!(
106-
"Spark `last_day` function requires 1 argument, got {}",
107-
arg_types.len()
108-
);
109-
}
110-
111-
let current_native_type: NativeType = (&arg_types[0]).into();
112-
if matches!(current_native_type, NativeType::Date)
113-
|| matches!(current_native_type, NativeType::String)
114-
|| matches!(current_native_type, NativeType::Null)
115-
{
116-
Ok(vec![DataType::Date32])
117-
} else {
118-
plan_err!(
119-
"The first argument of the Spark `last_day` function can only be a date or string, but got {}", &arg_types[0]
120-
)
98+
other => {
99+
internal_err!("Unsupported arg {other:?} for Spark function `last_day")
100+
}
121101
}
122102
}
123103
}

datafusion/sqllogictest/test_files/spark/datetime/last_day.slt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,79 +22,79 @@
2222
# https://github.com/apache/datafusion/issues/15914
2323

2424
query D
25-
SELECT last_day('2009-01-12'::string);
25+
SELECT last_day('2009-01-12'::DATE);
2626
----
2727
2009-01-31
2828

2929

3030
query D
31-
SELECT last_day('2015-02-28'::string);
31+
SELECT last_day('2015-02-28'::DATE);
3232
----
3333
2015-02-28
3434

3535
query D
36-
SELECT last_day('2015-03-27'::string);
36+
SELECT last_day('2015-03-27'::DATE);
3737
----
3838
2015-03-31
3939

4040
query D
41-
SELECT last_day('2015-04-26'::string);
41+
SELECT last_day('2015-04-26'::DATE);
4242
----
4343
2015-04-30
4444

4545
query D
46-
SELECT last_day('2015-05-25'::string);
46+
SELECT last_day('2015-05-25'::DATE);
4747
----
4848
2015-05-31
4949

5050
query D
51-
SELECT last_day('2015-06-24'::string);
51+
SELECT last_day('2015-06-24'::DATE);
5252
----
5353
2015-06-30
5454

5555
query D
56-
SELECT last_day('2015-07-23'::string);
56+
SELECT last_day('2015-07-23'::DATE);
5757
----
5858
2015-07-31
5959

6060
query D
61-
SELECT last_day('2015-08-01'::string);
61+
SELECT last_day('2015-08-01'::DATE);
6262
----
6363
2015-08-31
6464

6565
query D
66-
SELECT last_day('2015-09-02'::string);
66+
SELECT last_day('2015-09-02'::DATE);
6767
----
6868
2015-09-30
6969

7070
query D
71-
SELECT last_day('2015-10-03'::string);
71+
SELECT last_day('2015-10-03'::DATE);
7272
----
7373
2015-10-31
7474

7575
query D
76-
SELECT last_day('2015-11-04'::string);
76+
SELECT last_day('2015-11-04'::DATE);
7777
----
7878
2015-11-30
7979

8080
query D
81-
SELECT last_day('2015-12-05'::string);
81+
SELECT last_day('2015-12-05'::DATE);
8282
----
8383
2015-12-31
8484

8585

8686
query D
87-
SELECT last_day('2016-01-06'::string);
87+
SELECT last_day('2016-01-06'::DATE);
8888
----
8989
2016-01-31
9090

9191
query D
92-
SELECT last_day('2016-02-07'::string);
92+
SELECT last_day('2016-02-07'::DATE);
9393
----
9494
2016-02-29
9595

9696

97-
query ?
98-
SELECT NULL;
97+
query D
98+
SELECT last_day(null::DATE);
9999
----
100-
NULL
100+
NULL

0 commit comments

Comments
 (0)