Skip to content

Apache DataFusion SQL Query Engine

largeutf to temporal (#8357)

Jay Zhana58812375978
diff --git a/datafusion/expr/src/type_coercion/binary.rs b/datafusion/expr/src/type_coercion/binary.rs
index 9ccddbfce0..1027e97d06 100644
--- a/datafusion/expr/src/type_coercion/binary.rs
+++ b/datafusion/expr/src/type_coercion/binary.rs
@@ -331,26 +331,27 @@ fn string_temporal_coercion(
rhs_type: &DataType,
) -> Option<DataType> {
use arrow::datatypes::DataType::*;
- match (lhs_type, rhs_type) {
- (Utf8, Date32) | (Date32, Utf8) => Some(Date32),
- (Utf8, Date64) | (Date64, Utf8) => Some(Date64),
- (Utf8, Time32(unit)) | (Time32(unit), Utf8) => {
- match is_time_with_valid_unit(Time32(unit.clone())) {
- false => None,
- true => Some(Time32(unit.clone())),
- }
- }
- (Utf8, Time64(unit)) | (Time64(unit), Utf8) => {
- match is_time_with_valid_unit(Time64(unit.clone())) {
- false => None,
- true => Some(Time64(unit.clone())),
- }
- }
- (Timestamp(_, tz), Utf8) | (Utf8, Timestamp(_, tz)) => {
- Some(Timestamp(TimeUnit::Nanosecond, tz.clone()))
+
+ fn match_rule(l: &DataType, r: &DataType) -> Option<DataType> {
+ match (l, r) {
+ // Coerce Utf8/LargeUtf8 to Date32/Date64/Time32/Time64/Timestamp
+ (Utf8, temporal) | (LargeUtf8, temporal) => match temporal {
+ Date32 | Date64 => Some(temporal.clone()),
+ Time32(_) | Time64(_) => {
+ if is_time_with_valid_unit(temporal.to_owned()) {
+ Some(temporal.to_owned())
+ } else {
+ None
+ }
+ }
+ Timestamp(_, tz) => Some(Timestamp(TimeUnit::Nanosecond, tz.clone())),
+ _ => None,
+ },
+ _ => None,
}
- _ => None,
}
+
+ match_rule(lhs_type, rhs_type).or_else(|| match_rule(rhs_type, lhs_type))
}
/// Coerce `lhs_type` and `rhs_type` to a common type for the purposes of a comparison operation
diff --git a/datafusion/sqllogictest/test_files/scalar.slt b/datafusion/sqllogictest/test_files/scalar.slt
index ecb7fe13fc..b3597c664f 100644
--- a/datafusion/sqllogictest/test_files/scalar.slt
+++ b/datafusion/sqllogictest/test_files/scalar.slt
@@ -1926,3 +1926,20 @@ A true
B false
C false
D false
+
+# test string_temporal_coercion
+query BBBBBBBBBB
+select
+ arrow_cast(to_timestamp('2020-01-01 01:01:11.1234567890Z'), 'Timestamp(Second, None)') == '2020-01-01T01:01:11',
+ arrow_cast(to_timestamp('2020-01-02 01:01:11.1234567890Z'), 'Timestamp(Second, None)') == arrow_cast('2020-01-02T01:01:11', 'LargeUtf8'),
+ arrow_cast(to_timestamp('2020-01-03 01:01:11.1234567890Z'), 'Time32(Second)') == '01:01:11',
+ arrow_cast(to_timestamp('2020-01-04 01:01:11.1234567890Z'), 'Time32(Second)') == arrow_cast('01:01:11', 'LargeUtf8'),
+ arrow_cast(to_timestamp('2020-01-05 01:01:11.1234567890Z'), 'Time64(Microsecond)') == '01:01:11.123456',
+ arrow_cast(to_timestamp('2020-01-06 01:01:11.1234567890Z'), 'Time64(Microsecond)') == arrow_cast('01:01:11.123456', 'LargeUtf8'),
+ arrow_cast('2020-01-07', 'Date32') == '2020-01-07',
+ arrow_cast('2020-01-08', 'Date64') == '2020-01-08',
+ arrow_cast('2020-01-09', 'Date32') == arrow_cast('2020-01-09', 'LargeUtf8'),
+ arrow_cast('2020-01-10', 'Date64') == arrow_cast('2020-01-10', 'LargeUtf8')
+;
+----
+true true true true true true true true true true