This is an automated email from the ASF dual-hosted git repository.
dheres pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new d009d48ee9 Fix precision loss when coercing date_part utf8 argument
(#7846)
d009d48ee9 is described below
commit d009d48ee927caae40dfd51c74c6893b69c0cd57
Author: Daniƫl Heres <[email protected]>
AuthorDate: Tue Oct 17 20:15:28 2023 +0200
Fix precision loss when coercing date_part utf8 argument (#7846)
---
datafusion/core/tests/sql/expr.rs | 19 +++++++++++++++++++
datafusion/expr/src/built_in_function.rs | 20 ++++++++++----------
2 files changed, 29 insertions(+), 10 deletions(-)
diff --git a/datafusion/core/tests/sql/expr.rs
b/datafusion/core/tests/sql/expr.rs
index af33cfea65..1995a04015 100644
--- a/datafusion/core/tests/sql/expr.rs
+++ b/datafusion/core/tests/sql/expr.rs
@@ -717,6 +717,25 @@ async fn test_extract_date_part() -> Result<()> {
"date_part('nanosecond',
to_timestamp('2020-09-08T12:00:12.12345678+00:00'))",
"1.212345678e10"
);
+
+ // Keep precision when coercing Utf8 to Timestamp
+ test_expression!(
+ "date_part('second', '2020-09-08T12:00:12.12345678+00:00')",
+ "12.12345678"
+ );
+ test_expression!(
+ "date_part('millisecond', '2020-09-08T12:00:12.12345678+00:00')",
+ "12123.45678"
+ );
+ test_expression!(
+ "date_part('microsecond', '2020-09-08T12:00:12.12345678+00:00')",
+ "12123456.78"
+ );
+ test_expression!(
+ "date_part('nanosecond', '2020-09-08T12:00:12.12345678+00:00')",
+ "1.212345678e10"
+ );
+
Ok(())
}
diff --git a/datafusion/expr/src/built_in_function.rs
b/datafusion/expr/src/built_in_function.rs
index d7cfd9f420..f1af6e829b 100644
--- a/datafusion/expr/src/built_in_function.rs
+++ b/datafusion/expr/src/built_in_function.rs
@@ -1107,28 +1107,28 @@ impl BuiltinScalarFunction {
}
BuiltinScalarFunction::DatePart => Signature::one_of(
vec![
- Exact(vec![Utf8, Date32]),
- Exact(vec![Utf8, Date64]),
- Exact(vec![Utf8, Timestamp(Second, None)]),
+ Exact(vec![Utf8, Timestamp(Nanosecond, None)]),
Exact(vec![
Utf8,
- Timestamp(Second, Some(TIMEZONE_WILDCARD.into())),
+ Timestamp(Nanosecond, Some(TIMEZONE_WILDCARD.into())),
]),
- Exact(vec![Utf8, Timestamp(Microsecond, None)]),
+ Exact(vec![Utf8, Timestamp(Millisecond, None)]),
Exact(vec![
Utf8,
- Timestamp(Microsecond, Some(TIMEZONE_WILDCARD.into())),
+ Timestamp(Millisecond, Some(TIMEZONE_WILDCARD.into())),
]),
- Exact(vec![Utf8, Timestamp(Millisecond, None)]),
+ Exact(vec![Utf8, Timestamp(Microsecond, None)]),
Exact(vec![
Utf8,
- Timestamp(Millisecond, Some(TIMEZONE_WILDCARD.into())),
+ Timestamp(Microsecond, Some(TIMEZONE_WILDCARD.into())),
]),
- Exact(vec![Utf8, Timestamp(Nanosecond, None)]),
+ Exact(vec![Utf8, Timestamp(Second, None)]),
Exact(vec![
Utf8,
- Timestamp(Nanosecond, Some(TIMEZONE_WILDCARD.into())),
+ Timestamp(Second, Some(TIMEZONE_WILDCARD.into())),
]),
+ Exact(vec![Utf8, Date64]),
+ Exact(vec![Utf8, Date32]),
],
self.volatility(),
),