Skip to content

Commit

Permalink
feat: Parse timestamp strings as Date32
Browse files Browse the repository at this point in the history
  • Loading branch information
MazterQyou committed Mar 20, 2024
1 parent f32dc0f commit 435cb48
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions arrow/src/compute/kernels/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1652,11 +1652,20 @@ fn parse_naive_date(
}
}

if let Some(d) = delimiter {
chrono::NaiveDate::parse_from_str(v, &format!("%Y{}%m{}%d", d, d))
} else {
chrono::NaiveDate::parse_from_str(v, "%Y%m%d")
}
if let Ok(result) = {
if let Some(d) = delimiter {
chrono::NaiveDate::parse_from_str(v, &format!("%Y{}%m{}%d", d, d))
} else {
chrono::NaiveDate::parse_from_str(v, "%Y%m%d")
}
} {
return Ok(result);
};

// Try to parse full timestamp, this is valid in Postgres
chrono::NaiveDate::parse_from_str(v, "%Y-%m-%dT%H:%M:%S%.fZ")
.or_else(|_| chrono::NaiveDate::parse_from_str(v, "%Y-%m-%d %H:%M:%S%.f"))
.or_else(|_| chrono::NaiveDate::parse_from_str(v, "%Y-%m-%d %H:%M:%S"))
}

/// Casts generic string arrays to Date32Array
Expand Down Expand Up @@ -3132,20 +3141,23 @@ mod tests {
fn test_cast_string_to_date32() {
let a1 = Arc::new(StringArray::from(vec![
Some("2018-12-25"),
Some("2018-12-26 00:00:00"),
Some("Not a valid date"),
None,
])) as ArrayRef;
let a2 = Arc::new(LargeStringArray::from(vec![
Some("2018-12-25"),
Some("2018-12-26 00:00:00"),
Some("Not a valid date"),
None,
])) as ArrayRef;
for array in &[a1, a2] {
let b = cast(array, &DataType::Date32).unwrap();
let c = b.as_any().downcast_ref::<Date32Array>().unwrap();
assert_eq!(17890, c.value(0));
assert!(c.is_null(1));
assert_eq!(17891, c.value(1));
assert!(c.is_null(2));
assert!(c.is_null(3));
}
}

Expand Down

0 comments on commit 435cb48

Please sign in to comment.