Skip to content

Commit d009d48

Browse files
authored
Fix precision loss when coercing date_part utf8 argument (#7846)
1 parent efbd104 commit d009d48

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

datafusion/core/tests/sql/expr.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,25 @@ async fn test_extract_date_part() -> Result<()> {
717717
"date_part('nanosecond', to_timestamp('2020-09-08T12:00:12.12345678+00:00'))",
718718
"1.212345678e10"
719719
);
720+
721+
// Keep precision when coercing Utf8 to Timestamp
722+
test_expression!(
723+
"date_part('second', '2020-09-08T12:00:12.12345678+00:00')",
724+
"12.12345678"
725+
);
726+
test_expression!(
727+
"date_part('millisecond', '2020-09-08T12:00:12.12345678+00:00')",
728+
"12123.45678"
729+
);
730+
test_expression!(
731+
"date_part('microsecond', '2020-09-08T12:00:12.12345678+00:00')",
732+
"12123456.78"
733+
);
734+
test_expression!(
735+
"date_part('nanosecond', '2020-09-08T12:00:12.12345678+00:00')",
736+
"1.212345678e10"
737+
);
738+
720739
Ok(())
721740
}
722741

datafusion/expr/src/built_in_function.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,28 +1107,28 @@ impl BuiltinScalarFunction {
11071107
}
11081108
BuiltinScalarFunction::DatePart => Signature::one_of(
11091109
vec![
1110-
Exact(vec![Utf8, Date32]),
1111-
Exact(vec![Utf8, Date64]),
1112-
Exact(vec![Utf8, Timestamp(Second, None)]),
1110+
Exact(vec![Utf8, Timestamp(Nanosecond, None)]),
11131111
Exact(vec![
11141112
Utf8,
1115-
Timestamp(Second, Some(TIMEZONE_WILDCARD.into())),
1113+
Timestamp(Nanosecond, Some(TIMEZONE_WILDCARD.into())),
11161114
]),
1117-
Exact(vec![Utf8, Timestamp(Microsecond, None)]),
1115+
Exact(vec![Utf8, Timestamp(Millisecond, None)]),
11181116
Exact(vec![
11191117
Utf8,
1120-
Timestamp(Microsecond, Some(TIMEZONE_WILDCARD.into())),
1118+
Timestamp(Millisecond, Some(TIMEZONE_WILDCARD.into())),
11211119
]),
1122-
Exact(vec![Utf8, Timestamp(Millisecond, None)]),
1120+
Exact(vec![Utf8, Timestamp(Microsecond, None)]),
11231121
Exact(vec![
11241122
Utf8,
1125-
Timestamp(Millisecond, Some(TIMEZONE_WILDCARD.into())),
1123+
Timestamp(Microsecond, Some(TIMEZONE_WILDCARD.into())),
11261124
]),
1127-
Exact(vec![Utf8, Timestamp(Nanosecond, None)]),
1125+
Exact(vec![Utf8, Timestamp(Second, None)]),
11281126
Exact(vec![
11291127
Utf8,
1130-
Timestamp(Nanosecond, Some(TIMEZONE_WILDCARD.into())),
1128+
Timestamp(Second, Some(TIMEZONE_WILDCARD.into())),
11311129
]),
1130+
Exact(vec![Utf8, Date64]),
1131+
Exact(vec![Utf8, Date32]),
11321132
],
11331133
self.volatility(),
11341134
),

0 commit comments

Comments
 (0)