impl(bigquery): parse date and time related types#6033
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements the FromSql trait for several types, including wkt::Timestamp, google_cloud_type::model::Date, google_cloud_type::model::TimeOfDay, and google_cloud_type::model::DateTime, to support converting BigQuery internal values to Rust types. It also adds comprehensive unit and integration tests. The review feedback correctly identifies that BigQuery's REST API returns TIMESTAMP values as decimal strings representing seconds since the epoch rather than integer microseconds. To prevent parsing failures, the reviewer suggests parsing these values as f64 and adjusting the parser and unit tests accordingly.
| impl FromSql for wkt::Timestamp { | ||
| fn from_sql(value: wkt::Value) -> Result<Self, ConvertError> { | ||
| match value { | ||
| wkt::Value::String(s) => { | ||
| let micros = s | ||
| .parse::<i64>() | ||
| .map_err(|e| ConvertError::Convert(Box::new(e)))?; | ||
| timestamp_from_micros(micros) | ||
| } | ||
| wkt::Value::Number(n) => { | ||
| let micros = n.as_i64().ok_or_else(|| { | ||
| ConvertError::Convert("timestamp number is not valid i64".into()) | ||
| })?; | ||
| timestamp_from_micros(micros) | ||
| } | ||
| wkt::Value::Null => Err(ConvertError::NotNull), | ||
| other => Err(ConvertError::TypeMismatch { | ||
| expected: "string or number", | ||
| got: other, | ||
| }), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn timestamp_from_micros(micros: i64) -> Result<wkt::Timestamp, ConvertError> { | ||
| wkt::Timestamp::new( | ||
| micros.div_euclid(1_000_000), | ||
| (micros.rem_euclid(1_000_000) * 1_000) as i32, | ||
| ) | ||
| .map_err(|e| ConvertError::Convert(Box::new(e))) | ||
| } |
There was a problem hiding this comment.
BigQuery's REST API returns TIMESTAMP values as a decimal string representing seconds since the epoch (e.g., "1779982200.000000"), rather than microseconds as an integer string (e.g., "1779982200000000").
Parsing the string directly as i64 will fail with a parsing error due to the decimal point, causing query execution to fail when retrieving timestamp columns.
To fix this, parse the value as f64 (seconds since epoch) and convert it to seconds and nanoseconds. This also correctly handles negative timestamps (before the epoch) and subsecond precision.
impl FromSql for wkt::Timestamp {
fn from_sql(value: wkt::Value) -> Result<Self, ConvertError> {
match value {
wkt::Value::String(s) => {
let seconds = s
.parse::<f64>()
.map_err(|e| ConvertError::Convert(Box::new(e)))?;
timestamp_from_seconds_f64(seconds)
}
wkt::Value::Number(n) => {
let seconds = n.as_f64().ok_or_else(|| {
ConvertError::Convert("timestamp number is not valid f64".into())
})?;
timestamp_from_seconds_f64(seconds)
}
wkt::Value::Null => Err(ConvertError::NotNull),
other => Err(ConvertError::TypeMismatch {
expected: "string or number",
got: other,
}),
}
}
}
fn timestamp_from_seconds_f64(seconds_f64: f64) -> Result<wkt::Timestamp, ConvertError> {
if !seconds_f64.is_finite() {
return Err(ConvertError::Convert("timestamp float is not finite".into()));
}
let total_nanos = (seconds_f64 * 1_000_000_000.0).round() as i64;
wkt::Timestamp::new(
total_nanos.div_euclid(1_000_000_000),
total_nanos.rem_euclid(1_000_000_000) as i32,
)
.map_err(|e| ConvertError::Convert(Box::new(e)))
}| #[test_case(wkt::Value::String("1779982200000000".to_string()) => Ok(wkt::Timestamp::new(1779982200, 0).unwrap()) ; "timestamp micro integer string")] | ||
| #[test_case(wkt::Value::Number(1779982200000000i64.into()) => Ok(wkt::Timestamp::new(1779982200, 0).unwrap()) ; "timestamp micro integer number")] |
There was a problem hiding this comment.
Update the unit test cases to use the correct BigQuery REST API timestamp format (seconds since epoch as a decimal string or integer) to match the updated parser implementation.
#[test_case(wkt::Value::String("1779982200.000000".to_string()) => Ok(wkt::Timestamp::new(1779982200, 0).unwrap()) ; "timestamp decimal string"]
#[test_case(wkt::Value::Number(1779982200i64.into()) => Ok(wkt::Timestamp::new(1779982200, 0).unwrap()) ; "timestamp integer number"]
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6033 +/- ##
=======================================
Coverage 96.96% 96.96%
=======================================
Files 248 248
Lines 62379 62477 +98
=======================================
+ Hits 60483 60580 +97
- Misses 1896 1897 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Towards #5844