Apache DataFusion SQL Query Engine
Fix precision loss when coercing date_part utf8 argument (#7846)
Daniël Heresd009d48ee927
diff --git a/datafusion/core/tests/sql/expr.rs b/datafusion/core/tests/sql/expr.rsindex 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.rsindex 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(),),